12"""Full CDC tracking calibration."""
13from prompt
import CalibrationSettings, INPUT_DATA_FILTERS
16from ROOT
import Belle2
17from random
import choice
18from caf.framework
import Calibration
19from caf
import strategies
23settings = CalibrationSettings(name=
"CDC Tracking",
24 expert_username=
"dvthanh",
26 input_data_formats=[
"raw"],
27 input_data_names=[
"mumu_tight_or_highm_calib",
"hadron_calib",
"cosmic_calib"],
28 input_data_filters={
"mumu_tight_or_highm_calib":
29 [INPUT_DATA_FILTERS[
"Data Tag"][
"mumu_tight_or_highm_calib"],
30 INPUT_DATA_FILTERS[
"Data Quality Tag"][
"Good"],
31 INPUT_DATA_FILTERS[
"Magnet"][
"On"]],
33 [INPUT_DATA_FILTERS[
"Data Tag"][
"hadron_calib"],
34 INPUT_DATA_FILTERS[
"Data Quality Tag"][
"Good"],
35 INPUT_DATA_FILTERS[
"Magnet"][
"On"]],
37 [INPUT_DATA_FILTERS[
"Data Tag"][
"cosmic_calib"],
38 INPUT_DATA_FILTERS[
"Data Quality Tag"][
"Good"],
39 INPUT_DATA_FILTERS[
"Magnet"][
"On"]]},
42 "min_events_per_file": 500,
43 "max_events_per_file": 20000,
44 "max_events_per_file_hadron_for_tz_tw": 5000,
45 "max_events_per_file_hadron_for_xt_sr": 12000,
46 "min_events_for_tz_tw_calibration": 500000,
47 "max_events_for_tz_tw_calibration": 1000000,
48 "min_events_for_xt_sr_calibration": 1000000,
49 "max_events_for_xt_sr_calibration": 10000000,
50 "fractions_for_each_type": [0.5, 1, 0.5],
51 "max_job_for_each_type": [400, 700, 400],
52 "calib_mode":
"quick",
53 "calibration_procedure": {
"tz0": 1,
"xt0": 0,
"sr_tz0": 0,
"tz2": 0},
54 "payload_boundaries": [],
55 "backend_args": {
"request_memory":
"4 GB"}
59def select_files(all_input_files, min_events, max_events, max_processed_events_per_file, max_job=800, min_events_per_file=500):
60 basf2.B2INFO(f
"Minimum number of events: {min_events}")
61 basf2.B2INFO(f
"Maximum number of events: {max_events}")
62 basf2.B2INFO(f
"Conditions: ({min_events_per_file} < #Event/file < {max_processed_events_per_file}) and max_job = {max_job}")
67 while total_events < max_events
and njob < max_job:
70 if not all_input_files:
73 new_file_choice = choice(all_input_files)
75 all_input_files.remove(new_file_choice)
77 total_events_in_file = events_in_basf2_file(new_file_choice)
78 if not total_events_in_file
or total_events_in_file < min_events_per_file:
81 events_contributed = 0
82 if total_events_in_file < max_processed_events_per_file:
84 events_contributed = total_events_in_file
86 events_contributed = max_processed_events_per_file
87 chosen_files.append(new_file_choice)
88 total_events += events_contributed
91 basf2.B2INFO(f
"Total chosen files = {len(chosen_files)}")
92 basf2.B2INFO(f
"Total events in chosen files = {total_events}")
93 if total_events < min_events:
95 f
"There is not enough required events with setup max_processed_events_per_file = {max_processed_events_per_file}")
104def get_calibrations(input_data, **kwargs):
108 expert_config = kwargs.get(
"expert_config")
109 calib_mode = expert_config[
"calib_mode"]
111 min_events_per_file = expert_config[
"min_events_per_file"]
112 max_events_per_file = expert_config[
"max_events_per_file"]
114 min_events_for_tz_tw = expert_config[
"min_events_for_tz_tw_calibration"]
115 max_events_for_tz_tw = expert_config[
"max_events_for_tz_tw_calibration"]
116 max_events_per_file_hadron_for_tz_tw = expert_config[
"max_events_per_file_hadron_for_tz_tw"]
118 min_events_for_xt_sr = expert_config[
"min_events_for_xt_sr_calibration"]
119 max_events_for_xt_sr = expert_config[
"max_events_for_xt_sr_calibration"]
120 max_events_per_file_hadron_for_xt_sr = expert_config[
"max_events_per_file_hadron_for_xt_sr"]
122 Max_events_per_file_for_tz_tw = [max_events_per_file, max_events_per_file_hadron_for_tz_tw, max_events_per_file]
123 Max_events_per_file_for_xt_sr = [max_events_per_file, max_events_per_file_hadron_for_xt_sr, max_events_per_file]
125 fraction_of_event_for_types = expert_config[
"fractions_for_each_type"]
126 max_jobs = expert_config[
"max_job_for_each_type"]
127 basf2.B2INFO(f
"Number of job for each type are limited at [di-muon, hadron, cosmic]: {max_jobs}")
128 basf2.B2INFO(f
"Fraction for [di-muon, hadron, cosmic]: {fraction_of_event_for_types}")
129 if len(fraction_of_event_for_types) != 3:
130 basf2.B2FATAL(
"fraction of event must be an array with the size of 3, with order [mumu, hadron, cosmic]")
132 payload_boundaries = []
133 payload_boundaries.extend([ExpRun(*boundary)
for boundary
in expert_config[
"payload_boundaries"]])
134 basf2.B2INFO(f
"Payload boundaries from expert_config: {payload_boundaries}")
136 files_for_xt_sr_dict = {}
137 files_for_tz_tw_dict = {}
140 if fraction_of_event_for_types[1] > 0:
141 basf2.B2INFO(
"*********************** Select Hadron data for calibration ****************")
142 min_hadron_events_for_tz_tw = fraction_of_event_for_types[1] * min_events_for_tz_tw
143 max_hadron_events_for_tz_tw = fraction_of_event_for_types[1] * max_events_for_tz_tw
144 min_hadron_events_for_xt_sr = fraction_of_event_for_types[1] * min_events_for_xt_sr
145 max_hadron_events_for_xt_sr = fraction_of_event_for_types[1] * max_events_for_xt_sr
147 file_to_iov_hadron = input_data[
"hadron_calib"]
149 basf2.B2INFO(
"----> For T0 and Time walk correction")
150 chosen_files_hadron_for_tz_tw = select_files(list(file_to_iov_hadron.keys()),
151 min_hadron_events_for_tz_tw,
152 max_hadron_events_for_tz_tw,
153 max_events_per_file_hadron_for_tz_tw,
157 basf2.B2INFO(
"----> For XT, space resolution calib")
158 chosen_files_hadron_for_xt_sr = select_files(list(file_to_iov_hadron.keys()),
159 min_hadron_events_for_xt_sr,
160 max_hadron_events_for_xt_sr,
161 max_events_per_file_hadron_for_xt_sr,
165 files_for_xt_sr_dict[
"hadron_calib"] = chosen_files_hadron_for_xt_sr
166 files_for_tz_tw_dict[
"hadron_calib"] = chosen_files_hadron_for_tz_tw
168 if fraction_of_event_for_types[0] > 0:
169 basf2.B2INFO(
"***********************Select di-muon data for calibration ***************")
170 min_mumu_events_for_xt_sr = fraction_of_event_for_types[0] * min_events_for_xt_sr
171 max_mumu_events_for_xt_sr = fraction_of_event_for_types[0] * max_events_for_xt_sr
172 min_mumu_events_for_tz_tw = fraction_of_event_for_types[0] * min_events_for_tz_tw
173 max_mumu_events_for_tz_tw = fraction_of_event_for_types[0] * max_events_for_tz_tw
174 file_to_iov_mumu = input_data[
"mumu_tight_or_highm_calib"]
175 basf2.B2INFO(
"----> For T0 and Time walk correction")
176 chosen_files_mumu_for_tz_tw = select_files(list(file_to_iov_mumu.keys()),
177 min_mumu_events_for_tz_tw,
178 max_mumu_events_for_tz_tw,
184 basf2.B2INFO(
"----> For XT, space resolution calib")
186 chosen_files_mumu_for_xt_sr = select_files(list(file_to_iov_mumu.keys()),
187 min_mumu_events_for_xt_sr,
188 max_mumu_events_for_xt_sr,
193 files_for_xt_sr_dict[
"mumu_tight_or_highm_calib"] = chosen_files_mumu_for_xt_sr
194 files_for_tz_tw_dict[
"mumu_tight_or_highm_calib"] = chosen_files_mumu_for_tz_tw
196 ''' For cosmic data '''
197 if fraction_of_event_for_types[2] > 0:
198 basf2.B2INFO(
"********************* Select cosmic data for calibration *******************")
199 min_cosmic_events_for_tz_tw = fraction_of_event_for_types[2] * min_events_for_tz_tw
200 max_cosmic_events_for_tz_tw = fraction_of_event_for_types[2] * max_events_for_tz_tw
201 min_cosmic_events_for_xt_sr = fraction_of_event_for_types[2] * min_events_for_xt_sr
202 max_cosmic_events_for_xt_sr = fraction_of_event_for_types[2] * max_events_for_xt_sr
204 file_to_iov_cosmic = input_data[
"cosmic_calib"]
207 basf2.B2INFO(
"---->For T0 and Time walk correction")
208 chosen_files_cosmic_for_tz_tw = select_files(list(file_to_iov_cosmic.keys()),
209 min_cosmic_events_for_tz_tw,
210 max_cosmic_events_for_tz_tw,
216 basf2.B2INFO(
"----> For T0 and Time walk correction")
217 chosen_files_cosmic_for_xt_sr = select_files(list(file_to_iov_cosmic.keys()),
218 min_cosmic_events_for_xt_sr,
219 max_cosmic_events_for_xt_sr,
223 files_for_xt_sr_dict[
"cosmic_calib"] = chosen_files_cosmic_for_xt_sr
224 files_for_tz_tw_dict[
"cosmic_calib"] = chosen_files_cosmic_for_tz_tw
226 basf2.B2INFO(
"Complete input data selection.")
229 requested_iov = kwargs.get(
"requested_iov",
None)
231 from caf.utils
import IoV
233 output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
236 collector_granularity =
'all'
237 if payload_boundaries:
238 basf2.B2INFO(
'Found payload_boundaries: set collector granularity to run')
239 collector_granularity =
'run'
240 if calib_mode ==
"full":
241 calibration_procedure = {
253 elif calib_mode ==
"quick":
254 calibration_procedure = {
264 elif calib_mode ==
"manual":
265 calibration_procedure = expert_config[
"calibration_procedure"]
267 basf2.B2FATAL(f
"Calibration mode is not defined {calib_mode}, should be quick, full, or manual")
269 calib_keys = list(calibration_procedure)
270 cals = [
None]*len(calib_keys)
271 basf2.B2INFO(f
"Run calibration mode = {calib_mode}:")
273 for i
in range(len(cals)):
274 max_iter = calibration_procedure[calib_keys[i]]
277 cal_name =
''.join([i
for i
in calib_keys[i]
if not i.isdigit()])
280 elif cal_name ==
"tw":
282 elif cal_name ==
"xt":
284 elif cal_name ==
"sr_tz":
285 alg = [sr_algo(), tz_algo()]
287 basf2.B2FATAL(f
"The calibration is not defined, check spelling: calib {i}: {calib_keys[i]}")
289 if cal_name ==
"xt" or cal_name ==
"sr_tz":
290 max_event = Max_events_per_file_for_xt_sr
291 data_files = files_for_xt_sr_dict
293 max_event = Max_events_per_file_for_tz_tw
294 data_files = files_for_tz_tw_dict
295 basf2.B2INFO(f
"calibration for {calib_keys[i]} with number of iteration={max_iter}")
296 cals[i] = CDCCalibration(name=calib_keys[i],
298 input_file_dict=data_files,
299 max_iterations=max_iter,
300 max_events=max_event,
301 use_badWires=
True if calib_keys[i] ==
"tz" else False,
302 collector_granularity=collector_granularity,
303 backend_args=expert_config[
"backend_args"],
304 dependencies=[cals[i-1]]
if i > 0
else None
306 if payload_boundaries:
307 basf2.B2INFO(
"Found payload_boundaries: calibration strategies set to SequentialBoundaries.")
308 cals[0].strategies = strategies.SequentialBoundaries
310 for algorithm
in alg.algorithms:
311 algorithm.params = {
"iov_coverage": output_iov,
"payload_boundaries": payload_boundaries}
315 for algorithm
in alg.algorithms:
316 algorithm.params = {
"apply_iov": output_iov}
322def pre_collector(max_events=None, is_cosmic=False, use_badWires=False):
324 Define pre collection (reconstruction in our purpose).
325 Probably, we need only CDC
and ECL data.
327 max_events [int] : number of events to be processed.
328 All events by Default.
330 path : path
for pre collection
332 from basf2
import create_path, register_module
334 reco_path = create_path()
335 if max_events
is None:
336 root_input = register_module(
338 branchNames=HLT_INPUT_OBJECTS
341 root_input = register_module(
343 branchNames=HLT_INPUT_OBJECTS,
346 reco_path.add_module(root_input)
348 gearbox = register_module(
'Gearbox')
349 reco_path.add_module(gearbox)
350 reco_path.add_module(
'Geometry', useDB=
True)
351 Components = [
'CDC',
'ECL']
353 from rawdata
import add_unpackers
354 add_unpackers(reco_path, components=Components)
357 from reconstruction
import add_cosmics_reconstruction
359 add_cosmics_reconstruction(path=reco_path,
360 components=Components,
365 from reconstruction
import default_event_abort, add_prefilter_pretracking_reconstruction
366 from tracking
import add_prefilter_tracking_reconstruction
369 doom = reco_path.add_module(
"EventsOfDoomBuster")
370 default_event_abort(doom,
">=1", Belle2.EventMetaData.c_ReconstructionAbort)
371 reco_path.add_module(
'StatisticsSummary').set_name(
'Sum_EventsofDoomBuster')
374 add_prefilter_pretracking_reconstruction(reco_path, components=Components)
377 add_prefilter_tracking_reconstruction(path=reco_path,
378 components=Components,
379 trackFitHypotheses=[211],
380 prune_temporary_tracks=
False,
382 append_full_grid_cdc_eventt0=
True,
383 skip_full_grid_cdc_eventt0_if_svd_time_present=
False)
384 reco_path.add_module(
'StatisticsSummary').set_name(
'Sum_Tracking')
386 reco_path.add_module(
'Progress')
388 for module
in reco_path.modules():
389 if module.name() ==
"TFCDC_WireHitPreparer":
390 module.param({
"useBadWires": use_badWires})
395def collector(bField=True, is_cosmic=False, granularity='all'):
397 Create a cdc calibration collector
399 bField [bool] : True if B field
is on,
else False
400 isCosmic [bool] :
True if cosmic events,
401 else (collision)
False.
403 collector : collector module
405 from basf2
import register_module
406 col = register_module(
'CDCCalibrationCollector',
407 granularity=granularity,
408 calExpectedDriftTime=
True,
409 eventT0Extraction=
True,
416def tz_algo(max_rmsDt=0.25, max_badChannel=50):
418 Create a T0 calibration algorithm.
422 from ROOT
import Belle2
424 algo.storeHisto(
True)
425 algo.setMaxMeanDt(0.2)
426 algo.setMaxRMSDt(max_rmsDt)
427 algo.setMaxBadChannel(max_badChannel)
428 algo.setMinimumNDF(25)
435 Create a time walk calibration algorithm.
439 from ROOT
import Belle2
441 algo.setStoreHisto(
True)
448 Create a XT calibration algorithm.
450 prefix : prefixed name for algorithm,
451 which should be consistent
with one of collector..
455 from ROOT
import Belle2
457 algo.setStoreHisto(
True)
458 algo.setLRSeparate(
True)
459 algo.setThreshold(0.1)
465 Create a Spacial resolution calibration algorithm.
467 prefix : prefixed name for algorithm,
468 which should be consistent
with one of collector..
470 algo : Spacial algorithm
472 from ROOT
import Belle2
474 algo.setStoreHisto(
True)
475 algo.setThreshold(0.1)
481 CDCCalibration is a specialized calibration
class for cdc.
482 Since collector
is same
in all elements, no need to specify it.
491 max_events=[20000, 10000, 20000],
493 collector_granularity='All',
495 for algo
in algorithms:
496 algo.setHistFileName(name)
498 super().__init__(name=name,
499 algorithms=algorithms
502 from caf.framework
import Collection
504 for skim_type, file_list
in input_file_dict.items():
505 if skim_type ==
"cosmic_calib":
507 granularity=collector_granularity),
508 input_files=file_list,
509 pre_collector_path=pre_collector(max_events=max_events[2],
511 use_badWires=use_badWires),
512 backend_args=backend_args)
513 elif skim_type ==
"hadron_calib":
515 input_files=file_list,
516 pre_collector_path=pre_collector(max_events=max_events[1],
517 use_badWires=use_badWires),
518 backend_args=backend_args)
521 input_files=file_list,
522 pre_collector_path=pre_collector(max_events=max_events[0],
523 use_badWires=use_badWires),
524 backend_args=backend_args)
526 self.add_collection(name=skim_type, collection=collection)
528 self.max_iterations = max_iterations
530 if dependencies
is not None:
531 for dep
in dependencies:
Class for Space resolution calibration.
Class for T0 Correction .
Class for Time walk calibration.
Class to perform xt calibration for drift chamber.