9"""Full CDC tracking calibration."""
10from prompt
import CalibrationSettings, INPUT_DATA_FILTERS
13from ROOT
import Belle2
14from random
import choice
15from caf.framework
import Calibration
16from caf
import strategies
20settings = CalibrationSettings(name=
"CDC Tracking",
21 expert_username=
"guanyu",
24 input_data_formats=[
"raw"],
25 input_data_names=[
"mumu_tight_or_highm_calib",
"hadron_calib",
"cosmic_calib"],
26 input_data_filters={
"mumu_tight_or_highm_calib":
27 [INPUT_DATA_FILTERS[
"Data Tag"][
"mumu_tight_or_highm_calib"],
28 INPUT_DATA_FILTERS[
"Data Quality Tag"][
"Good"],
29 INPUT_DATA_FILTERS[
"Magnet"][
"On"]],
31 [INPUT_DATA_FILTERS[
"Data Tag"][
"hadron_calib"],
32 INPUT_DATA_FILTERS[
"Data Quality Tag"][
"Good"],
33 INPUT_DATA_FILTERS[
"Magnet"][
"On"]],
35 [INPUT_DATA_FILTERS[
"Data Tag"][
"cosmic_calib"],
36 INPUT_DATA_FILTERS[
"Data Quality Tag"][
"Good"],
37 INPUT_DATA_FILTERS[
"Magnet"][
"On"]]},
40 "min_events_per_file": 500,
41 "max_events_per_file": 20000,
42 "max_events_per_file_hadron_for_tz_tw": 5000,
43 "max_events_per_file_hadron_for_xt_sr": 12000,
44 "min_events_for_tz_tw_calibration": 500000,
45 "max_events_for_tz_tw_calibration": 1000000,
46 "min_events_for_xt_sr_calibration": 1000000,
47 "max_events_for_xt_sr_calibration": 10000000,
48 "fractions_for_each_type": [0.5, 1, 0.5],
49 "max_job_for_each_type": [400, 700, 400],
50 "calib_mode":
"quick",
51 "calibration_procedure": {
"tz0": 1,
"xt0": 0,
"sr_tz0": 0,
"tz2": 0},
52 "payload_boundaries": [],
53 "backend_args": {
"request_memory":
"4 GB"}},
54 produced_payloads=[
"CDCTimeZeros",
"CDCTimeWalks",
"CDCXtRelations",
"CDCSpaceResols"])
57def select_files(all_input_files, min_events, max_events, max_processed_events_per_file, max_job=800, min_events_per_file=500):
58 basf2.B2INFO(f
"Minimum number of events: {min_events}")
59 basf2.B2INFO(f
"Maximum number of events: {max_events}")
60 basf2.B2INFO(f
"Conditions: ({min_events_per_file} < #Event/file < {max_processed_events_per_file}) and max_job = {max_job}")
65 while total_events < max_events
and njob < max_job:
68 if not all_input_files:
71 new_file_choice = choice(all_input_files)
73 all_input_files.remove(new_file_choice)
75 total_events_in_file = events_in_basf2_file(new_file_choice)
76 if not total_events_in_file
or total_events_in_file < min_events_per_file:
79 events_contributed = 0
80 if total_events_in_file < max_processed_events_per_file:
82 events_contributed = total_events_in_file
84 events_contributed = max_processed_events_per_file
85 chosen_files.append(new_file_choice)
86 total_events += events_contributed
89 basf2.B2INFO(f
"Total chosen files = {len(chosen_files)}")
90 basf2.B2INFO(f
"Total events in chosen files = {total_events}")
91 if total_events < min_events:
93 f
"There is not enough required events with setup max_processed_events_per_file = {max_processed_events_per_file}")
102def get_calibrations(input_data, **kwargs):
106 expert_config = kwargs.get(
"expert_config")
107 calib_mode = expert_config[
"calib_mode"]
109 min_events_per_file = expert_config[
"min_events_per_file"]
110 max_events_per_file = expert_config[
"max_events_per_file"]
112 min_events_for_tz_tw = expert_config[
"min_events_for_tz_tw_calibration"]
113 max_events_for_tz_tw = expert_config[
"max_events_for_tz_tw_calibration"]
114 max_events_per_file_hadron_for_tz_tw = expert_config[
"max_events_per_file_hadron_for_tz_tw"]
116 min_events_for_xt_sr = expert_config[
"min_events_for_xt_sr_calibration"]
117 max_events_for_xt_sr = expert_config[
"max_events_for_xt_sr_calibration"]
118 max_events_per_file_hadron_for_xt_sr = expert_config[
"max_events_per_file_hadron_for_xt_sr"]
120 Max_events_per_file_for_tz_tw = [max_events_per_file, max_events_per_file_hadron_for_tz_tw, max_events_per_file]
121 Max_events_per_file_for_xt_sr = [max_events_per_file, max_events_per_file_hadron_for_xt_sr, max_events_per_file]
123 fraction_of_event_for_types = expert_config[
"fractions_for_each_type"]
124 max_jobs = expert_config[
"max_job_for_each_type"]
125 basf2.B2INFO(f
"Number of job for each type are limited at [di-muon, hadron, cosmic]: {max_jobs}")
126 basf2.B2INFO(f
"Fraction for [di-muon, hadron, cosmic]: {fraction_of_event_for_types}")
127 if len(fraction_of_event_for_types) != 3:
128 basf2.B2FATAL(
"fraction of event must be an array with the size of 3, with order [mumu, hadron, cosmic]")
130 payload_boundaries = []
131 payload_boundaries.extend([ExpRun(*boundary)
for boundary
in expert_config[
"payload_boundaries"]])
132 basf2.B2INFO(f
"Payload boundaries from expert_config: {payload_boundaries}")
134 files_for_xt_sr_dict = {}
135 files_for_tz_tw_dict = {}
138 if fraction_of_event_for_types[1] > 0:
139 basf2.B2INFO(
"*********************** Select Hadron data for calibration ****************")
140 min_hadron_events_for_tz_tw = fraction_of_event_for_types[1] * min_events_for_tz_tw
141 max_hadron_events_for_tz_tw = fraction_of_event_for_types[1] * max_events_for_tz_tw
142 min_hadron_events_for_xt_sr = fraction_of_event_for_types[1] * min_events_for_xt_sr
143 max_hadron_events_for_xt_sr = fraction_of_event_for_types[1] * max_events_for_xt_sr
145 file_to_iov_hadron = input_data[
"hadron_calib"]
147 basf2.B2INFO(
"----> For T0 and Time walk correction")
148 chosen_files_hadron_for_tz_tw = select_files(list(file_to_iov_hadron.keys()),
149 min_hadron_events_for_tz_tw,
150 max_hadron_events_for_tz_tw,
151 max_events_per_file_hadron_for_tz_tw,
155 basf2.B2INFO(
"----> For XT, space resolution calib")
156 chosen_files_hadron_for_xt_sr = select_files(list(file_to_iov_hadron.keys()),
157 min_hadron_events_for_xt_sr,
158 max_hadron_events_for_xt_sr,
159 max_events_per_file_hadron_for_xt_sr,
163 files_for_xt_sr_dict[
"hadron_calib"] = chosen_files_hadron_for_xt_sr
164 files_for_tz_tw_dict[
"hadron_calib"] = chosen_files_hadron_for_tz_tw
166 if fraction_of_event_for_types[0] > 0:
167 basf2.B2INFO(
"***********************Select di-muon data for calibration ***************")
168 min_mumu_events_for_xt_sr = fraction_of_event_for_types[0] * min_events_for_xt_sr
169 max_mumu_events_for_xt_sr = fraction_of_event_for_types[0] * max_events_for_xt_sr
170 min_mumu_events_for_tz_tw = fraction_of_event_for_types[0] * min_events_for_tz_tw
171 max_mumu_events_for_tz_tw = fraction_of_event_for_types[0] * max_events_for_tz_tw
172 file_to_iov_mumu = input_data[
"mumu_tight_or_highm_calib"]
173 basf2.B2INFO(
"----> For T0 and Time walk correction")
174 chosen_files_mumu_for_tz_tw = select_files(list(file_to_iov_mumu.keys()),
175 min_mumu_events_for_tz_tw,
176 max_mumu_events_for_tz_tw,
182 basf2.B2INFO(
"----> For XT, space resolution calib")
184 chosen_files_mumu_for_xt_sr = select_files(list(file_to_iov_mumu.keys()),
185 min_mumu_events_for_xt_sr,
186 max_mumu_events_for_xt_sr,
191 files_for_xt_sr_dict[
"mumu_tight_or_highm_calib"] = chosen_files_mumu_for_xt_sr
192 files_for_tz_tw_dict[
"mumu_tight_or_highm_calib"] = chosen_files_mumu_for_tz_tw
194 ''' For cosmic data '''
195 if fraction_of_event_for_types[2] > 0:
196 basf2.B2INFO(
"********************* Select cosmic data for calibration *******************")
197 min_cosmic_events_for_tz_tw = fraction_of_event_for_types[2] * min_events_for_tz_tw
198 max_cosmic_events_for_tz_tw = fraction_of_event_for_types[2] * max_events_for_tz_tw
199 min_cosmic_events_for_xt_sr = fraction_of_event_for_types[2] * min_events_for_xt_sr
200 max_cosmic_events_for_xt_sr = fraction_of_event_for_types[2] * max_events_for_xt_sr
202 file_to_iov_cosmic = input_data[
"cosmic_calib"]
205 basf2.B2INFO(
"---->For T0 and Time walk correction")
206 chosen_files_cosmic_for_tz_tw = select_files(list(file_to_iov_cosmic.keys()),
207 min_cosmic_events_for_tz_tw,
208 max_cosmic_events_for_tz_tw,
214 basf2.B2INFO(
"----> For T0 and Time walk correction")
215 chosen_files_cosmic_for_xt_sr = select_files(list(file_to_iov_cosmic.keys()),
216 min_cosmic_events_for_xt_sr,
217 max_cosmic_events_for_xt_sr,
221 files_for_xt_sr_dict[
"cosmic_calib"] = chosen_files_cosmic_for_xt_sr
222 files_for_tz_tw_dict[
"cosmic_calib"] = chosen_files_cosmic_for_tz_tw
224 basf2.B2INFO(
"Complete input data selection.")
227 requested_iov = kwargs.get(
"requested_iov",
None)
229 from caf.utils
import IoV
231 output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
234 collector_granularity =
'all'
235 if payload_boundaries:
236 basf2.B2INFO(
'Found payload_boundaries: set collector granularity to run')
237 collector_granularity =
'run'
238 if calib_mode ==
"full":
239 calibration_procedure = {
251 elif calib_mode ==
"quick":
252 calibration_procedure = {
262 elif calib_mode ==
"manual":
263 calibration_procedure = expert_config[
"calibration_procedure"]
265 basf2.B2FATAL(f
"Calibration mode is not defined {calib_mode}, should be quick, full, or manual")
267 calib_keys = list(calibration_procedure)
268 cals = [
None]*len(calib_keys)
269 basf2.B2INFO(f
"Run calibration mode = {calib_mode}:")
271 for i
in range(len(cals)):
272 max_iter = calibration_procedure[calib_keys[i]]
275 cal_name =
''.join([i
for i
in calib_keys[i]
if not i.isdigit()])
278 elif cal_name ==
"tw":
280 elif cal_name ==
"xt":
282 elif cal_name ==
"sr_tz":
283 alg = [sr_algo(), tz_algo()]
285 basf2.B2FATAL(f
"The calibration is not defined, check spelling: calib {i}: {calib_keys[i]}")
287 if cal_name ==
"xt" or cal_name ==
"sr_tz":
288 max_event = Max_events_per_file_for_xt_sr
289 data_files = files_for_xt_sr_dict
291 max_event = Max_events_per_file_for_tz_tw
292 data_files = files_for_tz_tw_dict
293 basf2.B2INFO(f
"calibration for {calib_keys[i]} with number of iteration={max_iter}")
294 cals[i] = CDCCalibration(name=calib_keys[i],
296 input_file_dict=data_files,
297 max_iterations=max_iter,
298 max_events=max_event,
299 use_badWires=
True if calib_keys[i] ==
"tz" else False,
300 collector_granularity=collector_granularity,
301 backend_args=expert_config[
"backend_args"],
302 dependencies=[cals[i-1]]
if i > 0
else None
304 if payload_boundaries:
305 basf2.B2INFO(
"Found payload_boundaries: calibration strategies set to SequentialBoundaries.")
308 for algorithm
in alg.algorithms:
309 algorithm.params = {
"iov_coverage": output_iov,
"payload_boundaries": payload_boundaries}
313 for algorithm
in alg.algorithms:
314 algorithm.params = {
"apply_iov": output_iov}
320def pre_collector(max_events=None, is_cosmic=False, use_badWires=False):
322 Define pre collection (reconstruction in our purpose).
323 Probably, we need only CDC and ECL data.
325 max_events [int] : number of events to be processed.
326 All events by Default.
328 path : path for pre collection
330 from basf2
import create_path, register_module
332 reco_path = create_path()
333 if max_events
is None:
334 root_input = register_module(
336 branchNames=HLT_INPUT_OBJECTS
339 root_input = register_module(
341 branchNames=HLT_INPUT_OBJECTS,
344 reco_path.add_module(root_input)
346 gearbox = register_module(
'Gearbox')
347 reco_path.add_module(gearbox)
348 reco_path.add_module(
'Geometry', useDB=
True)
349 Components = [
'CDC',
'ECL']
351 from rawdata
import add_unpackers
352 add_unpackers(reco_path, components=Components)
355 from reconstruction
import add_cosmics_reconstruction
357 add_cosmics_reconstruction(path=reco_path,
358 components=Components,
363 from reconstruction
import default_event_abort, add_prefilter_pretracking_reconstruction
364 from tracking
import add_prefilter_tracking_reconstruction
367 doom = reco_path.add_module(
"EventsOfDoomBuster")
368 default_event_abort(doom,
">=1", Belle2.EventMetaData.c_ReconstructionAbort)
369 reco_path.add_module(
'StatisticsSummary').set_name(
'Sum_EventsofDoomBuster')
372 add_prefilter_pretracking_reconstruction(reco_path, components=Components)
375 add_prefilter_tracking_reconstruction(path=reco_path,
376 components=Components,
377 trackFitHypotheses=[211],
378 prune_temporary_tracks=
False,
380 append_full_grid_cdc_eventt0=
True,
381 skip_full_grid_cdc_eventt0_if_svd_time_present=
False)
382 reco_path.add_module(
'StatisticsSummary').set_name(
'Sum_Tracking')
384 reco_path.add_module(
'Progress')
386 for module
in reco_path.modules():
387 if module.name() ==
"TFCDC_WireHitPreparer":
388 module.param({
"useBadWires": use_badWires})
393def collector(is_cosmic=False, granularity='all'):
395 Create a cdc calibration collector
397 bField [bool] : True if B field is on, else False
398 isCosmic [bool] : True if cosmic events,
399 else (collision) False.
401 collector : collector module
403 from basf2
import register_module
404 col = register_module(
'CDCCalibrationCollector',
405 granularity=granularity,
406 calExpectedDriftTime=
True,
407 eventT0Extraction=
True,
413def tz_algo(max_rmsDt=0.25, max_badChannel=50):
415 Create a T0 calibration algorithm.
419 from ROOT
import Belle2
421 algo.storeHisto(
True)
422 algo.setMaxMeanDt(0.2)
423 algo.setMaxRMSDt(max_rmsDt)
424 algo.setMaxBadChannel(max_badChannel)
425 algo.setMinimumNDF(25)
432 Create a time walk calibration algorithm.
436 from ROOT
import Belle2
438 algo.setStoreHisto(
True)
445 Create a XT calibration algorithm.
447 prefix : prefixed name for algorithm,
448 which should be consistent with one of collector..
452 from ROOT
import Belle2
454 algo.setStoreHisto(
True)
455 algo.setLRSeparate(
True)
456 algo.setThreshold(0.1)
462 Create a Spacial resolution calibration algorithm.
464 prefix : prefixed name for algorithm,
465 which should be consistent with one of collector..
467 algo : Spacial algorithm
469 from ROOT
import Belle2
471 algo.setStoreHisto(
True)
472 algo.setThreshold(0.1)
478 CDCCalibration is a specialized calibration class for cdc.
479 Since collector is same in all elements, no need to specify it.
488 max_events=[20000, 10000, 20000],
490 collector_granularity='All',
494 for algo
in algorithms:
495 algo.setHistFileName(name)
498 algorithms=algorithms
501 from caf.framework
import Collection
503 for skim_type, file_list
in input_file_dict.items():
504 if skim_type ==
"cosmic_calib":
506 granularity=collector_granularity),
507 input_files=file_list,
508 pre_collector_path=pre_collector(max_events=max_events[2],
510 use_badWires=use_badWires),
511 backend_args=backend_args)
512 elif skim_type ==
"hadron_calib":
514 input_files=file_list,
515 pre_collector_path=pre_collector(max_events=max_events[1],
516 use_badWires=use_badWires),
517 backend_args=backend_args)
520 input_files=file_list,
521 pre_collector_path=pre_collector(max_events=max_events[0],
522 use_badWires=use_badWires),
523 backend_args=backend_args)
525 self.add_collection(name=skim_type, collection=collection)
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.
max_iterations
set number of maximum iterations
__init__(self, name, algorithms, input_file_dict, max_iterations=5, dependencies=None, max_events=[20000, 10000, 20000], use_badWires=False, collector_granularity='All', backend_args=None)