10Airflow script for automatic CDC dEdx calibration. It is the electron based
11calibration, where at present only RunGain, injection time, Cosine, WireGain and 1D are implemented.
12The remaining two 2D will be implemented in the near future.
16from ROOT
import gSystem
17from ROOT.Belle2
import CDCDedxRunGainAlgorithm, CDCDedxCosineAlgorithm, CDCDedxWireGainAlgorithm
18from ROOT.Belle2
import CDCDedxCosEdgeAlgorithm, CDCDedxBadWireAlgorithm, CDCDedxInjectTimeAlgorithm
19from ROOT.Belle2
import CDCDedx1DCellAlgorithm, CDCDedxValidationAlgorithm, CDCDedxCosLayerAlgorithm
21from caf.framework
import Calibration
22from caf.strategies
import SingleIOV, SequentialRunByRun, SequentialBoundaries
23from prompt
import CalibrationSettings, INPUT_DATA_FILTERS
24import reconstruction
as recon
25from random
import seed
28gSystem.Load(
'libreconstruction.so')
29ROOT.gROOT.SetBatch(
True)
31settings = CalibrationSettings(
33 expert_username=
"renu92garg",
36 input_data_formats=[
"cdst"],
37 input_data_names=[
"bhabha_combined_calib"],
39 "payload_boundaries": [],
40 "calib_datamode":
False,
44 "adjustment": 1.00798,
46 "calibration_procedure": {
"rungain0": 0,
"rungain1": 0,
"rungain2": 0}
49 "bhabha_combined_calib": [
50 INPUT_DATA_FILTERS[
'Run Type'][
'physics'],
51 INPUT_DATA_FILTERS[
'Data Tag'][
'bhabha_combined_calib'],
52 INPUT_DATA_FILTERS[
'Data Quality Tag'][
'Good Or Recoverable'],
53 INPUT_DATA_FILTERS[
'Magnet'][
'On'],
54 INPUT_DATA_FILTERS[
'Beam Energy'][
'4S'],
55 INPUT_DATA_FILTERS[
'Beam Energy'][
'Continuum'],
56 INPUT_DATA_FILTERS[
'Beam Energy'][
'Scan']]},
58 produced_payloads=[
"CDCDedxInjectionTime",
"CDCDedxCosineEdge",
"CDCDedxBadWires",
59 "CDCDedxWireGain",
"CDCDedx1DCell",
"CDCDedxCosineCor",
"CDCDedxRunGain"])
62def get_calibrations(input_data, **kwargs):
63 """ REQUIRED FUNCTION used by b2caf-prompt-run tool
64 This function return a list of Calibration
65 objects we assign to the CAF process
69 file_to_iov_physics = input_data[
"bhabha_combined_calib"]
71 expert_config = kwargs.get(
"expert_config")
72 calib_mode = expert_config[
"calib_mode"]
75 fulldataMode = expert_config[
"calib_datamode"]
76 adjustment = expert_config[
"adjustment"]
79 input_files_rungain = list(file_to_iov_physics.keys())
80 input_files_coscorr = list(file_to_iov_physics.keys())
81 input_files_wiregain = list(file_to_iov_physics.keys())
85 maxevt_rg = expert_config[
"maxevt_rg"]
86 maxevt_cc = expert_config[
"maxevt_cc"]
87 maxevt_wg = expert_config[
"maxevt_wg"]
89 from prompt.utils import filter_by_max_events_per_run, filter_by_select_max_events_from_files
92 max_files_for_maxevents = maxevt_rg
93 reduced_file_to_iov_rungain = filter_by_max_events_per_run(file_to_iov_physics, max_files_for_maxevents,
True)
94 input_files_rungain = list(reduced_file_to_iov_rungain.keys())
95 basf2.B2INFO(f
"Total number of files used for rungains = {len(input_files_rungain)}")
98 input_files_coscorr = filter_by_select_max_events_from_files(list(file_to_iov_physics.keys()), maxevt_cc)
99 basf2.B2INFO(f
"Total number of files used for cosine = {len(input_files_coscorr)}")
100 if not input_files_coscorr:
102 f
"Cosine: all requested ({maxevt_cc}) events not found")
105 if maxevt_wg == maxevt_cc:
106 input_files_wiregain = input_files_coscorr
108 input_files_wiregain = filter_by_select_max_events_from_files(list(file_to_iov_physics.keys()), maxevt_wg)
110 basf2.B2INFO(f
"Total number of files used for wiregains = {len(input_files_wiregain)}")
111 if not input_files_wiregain:
113 f
"WireGain: all requested ({maxevt_wg}) events not found")
115 requested_iov = kwargs.get(
"requested_iov",
None)
116 from caf.utils
import ExpRun, IoV
117 output_iov = IoV(requested_iov.exp_low, requested_iov.run_low, -1, -1)
119 payload_boundaries = [ExpRun(output_iov.exp_low, output_iov.run_low)]
120 payload_boundaries.extend([ExpRun(*boundary)
for boundary
in expert_config[
"payload_boundaries"]])
121 basf2.B2INFO(f
"Expert set payload boundaries are: {expert_config['payload_boundaries']}")
123 collector_granularity =
'all'
124 if expert_config[
"payload_boundaries"]
is not None:
125 basf2.B2INFO(
'Found payload_boundaries: set collector granularity to run')
126 collector_granularity =
'run'
128 if calib_mode ==
"full":
129 calibration_procedure = {
147 elif calib_mode ==
"quick":
148 calibration_procedure = {
160 elif calib_mode ==
"manual":
161 calibration_procedure = expert_config[
"calibration_procedure"]
163 basf2.B2FATAL(f
"Calibration mode is not defined {calib_mode}, should be full, quick, or manual")
165 calib_keys = list(calibration_procedure)
166 cals = [
None]*len(calib_keys)
167 basf2.B2INFO(f
"Run calibration mode = {calib_mode}:")
169 for i
in range(len(cals)):
170 max_iter = calibration_procedure[calib_keys[i]]
172 data_files = [input_files_rungain, input_files_coscorr, input_files_wiregain]
173 cal_name =
''.join([i
for i
in calib_keys[i]
if not i.isdigit()])
174 if cal_name ==
"rungain":
175 alg = [rungain_algo(calib_keys[i], adjustment)]
176 elif cal_name ==
"coslayer":
177 alg = [coslayer_algo()]
178 elif cal_name ==
"coscorr":
180 elif cal_name ==
"cosedge":
181 alg = [cosedge_algo()]
182 elif cal_name ==
"timegain":
183 alg = [injection_time_algo()]
184 elif cal_name ==
"badwire":
185 alg = [badwire_algo()]
186 elif cal_name ==
"wiregain":
187 alg = [wiregain_algo()]
188 elif cal_name ==
"onedcell":
189 alg = [onedcell_algo()]
190 elif cal_name ==
"validation":
191 alg = [validation_algo()]
193 basf2.B2FATAL(f
"The calibration is not defined, check spelling: calib {i}: {calib_keys[i]}")
195 basf2.B2INFO(f
"calibration for {calib_keys[i]} with number of iteration={max_iter}")
197 cals[i] = CDCDedxCalibration(name=calib_keys[i],
199 input_file_dict=data_files,
200 max_iterations=max_iter,
201 collector_granularity=collector_granularity,
202 dependencies=[cals[i-1]]
if i > 0
else None
204 if payload_boundaries:
205 basf2.B2INFO(
"Found payload_boundaries: calibration strategies set to SequentialBoundaries.")
206 if cal_name ==
"rungain" or cal_name ==
"timegain":
207 cals[i].strategies = SequentialRunByRun
208 for algorithm
in cals[i].algorithms:
209 algorithm.params = {
"iov_coverage": output_iov}
210 if calib_keys[i] ==
"rungain0" or calib_keys[i] ==
"rungain1" or calib_keys[i] ==
"timegain0":
211 cals[i].save_payloads =
False
212 elif cal_name ==
"onedcell":
213 cals[i].strategies = SingleIOV
214 for algorithm
in cals[i].algorithms:
215 algorithm.params = {
"apply_iov": output_iov}
216 if calib_keys[i] ==
"onedcell0":
217 cals[i].save_payloads =
False
219 cals[i].strategies = SequentialBoundaries
220 for algorithm
in cals[i].algorithms:
221 algorithm.params = {
"iov_coverage": output_iov,
"payload_boundaries": payload_boundaries}
222 if (calib_keys[i] ==
"coscorr0" or calib_keys[i] ==
"coslayer0" or calib_keys[i] ==
"coslayer1"
223 or calib_keys[i] ==
"wiregain0"):
224 cals[i].save_payloads =
False
227 for algorithm
in cals[i].algorithms:
228 algorithm.params = {
"apply_iov": output_iov}
234def pre_collector(name='rg'):
236 Define pre collection.
238 name : name of the calibration
239 rungain rungain0 by Default.
241 path : path for pre collection
244 reco_path = basf2.create_path()
246 if (name ==
"validation"):
247 basf2.B2INFO(
"no trigger skim")
248 elif (name ==
"timegain" or name ==
"onedcell"):
249 trg_bhabhaskim = reco_path.add_module(
251 triggerLines=[
"software_trigger_cut&skim&accept_bhabha_cdc"])
252 trg_bhabhaskim.if_value(
"==0", basf2.Path(), basf2.AfterConditionPath.END)
253 ps_bhabhaskim = reco_path.add_module(
"Prescale", prescale=0.80)
254 ps_bhabhaskim.if_value(
"==0", basf2.Path(), basf2.AfterConditionPath.END)
256 elif (name ==
"cosedge"):
257 trg_bhabhaskim = reco_path.add_module(
260 "software_trigger_cut&skim&accept_bhabha",
261 "software_trigger_cut&filter&ee_flat_90_180",
262 "software_trigger_cut&filter&ee_flat_0_19"])
263 trg_bhabhaskim.if_value(
"==0", basf2.Path(), basf2.AfterConditionPath.END)
265 trg_bhabhaskim = reco_path.add_module(
"TriggerSkim", triggerLines=[
"software_trigger_cut&skim&accept_bhabha"])
266 trg_bhabhaskim.if_value(
"==0", basf2.Path(), basf2.AfterConditionPath.END)
268 recon.prepare_cdst_analysis(path=reco_path)
270 reco_path.add_module(
272 relativeCorrections=
False,
285def collector(granularity='all', name=''):
287 Create a cdcdedx calibration collector
289 name : name of calibration
290 granularity : granularity : all or run
292 collector : collector module
295 from basf2
import register_module
296 if name ==
"validation":
297 col = register_module(
'ElectronValCollector', cleanupCuts=
True)
301 col = register_module(
'CDCDedxElectronCollector', cleanupCuts=
True)
302 if name ==
"timegain":
303 CollParam = {
'isRun':
True,
'isInjTime':
True,
'isRadee':
True,
'granularity':
'run'}
305 elif name ==
"coslayer":
306 CollParam = {
'isCharge':
True,
'isCosth':
True,
'islLayer':
True,
'islDedx':
True,
'granularity': granularity}
308 elif name ==
"coscorr" or name ==
"cosedge":
309 CollParam = {
'isCharge':
True,
'isCosth':
True,
'granularity': granularity}
311 elif name ==
"badwire":
313 CollParam = {
'isWire':
True,
'isDedxhit': isHit,
'isADCcorr':
not isHit,
'granularity': granularity}
315 elif name ==
"wiregain":
316 CollParam = {
'isWire':
True,
'isDedxhit':
True,
'isCosth':
True,
'granularity': granularity}
318 elif name ==
"onedcell":
326 'granularity': granularity}
329 CollParam = {
'isRun':
True,
'granularity':
'run'}
337def rungain_algo(name, adjustment):
339 Create a rungain calibration algorithm.
341 algo : rungain algorithm
343 algo = CDCDedxRunGainAlgorithm()
344 algo.setMonitoringPlots(
True)
345 if name ==
"rungain2":
346 algo.setAdjustment(adjustment)
352def injection_time_algo():
354 Create a injection time calibration algorithm.
356 algo : injection time algorithm
358 algo = CDCDedxInjectTimeAlgorithm()
359 algo.setMonitoringPlots(
True)
367 Create a cosine calibration algorithm.
369 algo : cosine algorithm
371 algo = CDCDedxCosLayerAlgorithm()
372 algo.setMonitoringPlots(
True)
381 Create a cosine calibration algorithm.
383 algo : cosine algorithm
385 algo = CDCDedxCosineAlgorithm()
386 algo.setMonitoringPlots(
True)
394 Create a cosine edge calibration algorithm.
396 algo : cosine edge algorithm
398 algo = CDCDedxCosEdgeAlgorithm()
399 algo.setMonitoringPlots(
True)
407 Create a badwire calibration algorithm.
409 algo : badwire algorithm
411 algo = CDCDedxBadWireAlgorithm()
413 algo.setHighFracThres(0.2)
414 algo.setMeanThres(0.4)
415 algo.setRMSThres(0.4)
416 algo.setHistPars(150, 0, 5)
417 algo.setMonitoringPlots(
True)
425 Create a wire gain calibration algorithm.
427 algo : wiregain algorithm
429 algo = CDCDedxWireGainAlgorithm()
430 algo.enableExtraPlots(
True)
436 Create oned cell calibration algorithim.
438 algo : oned cell correction algorithm
440 algo = CDCDedx1DCellAlgorithm()
441 algo.enableExtraPlots(
True)
442 algo.setMergePayload(
True)
446def validation_algo():
448 Create validation algorithm
450 algo : validation algorithm
452 algo = CDCDedxValidationAlgorithm()
458 CDCDedxCalibration is a specialized calibration for cdcdedx.
467 collector_granularity='All'):
470 name: name of calibration
471 algorithims: algorithm of calibration
472 input_file_dict: input files list
473 max_iterations: maximum number of iterations
474 dependenices: depends on the previous calibration
475 collector_granularity: granularity : all or run
478 algorithms=algorithms
481 from caf.framework
import Collection
482 cal_name =
''.join([i
for i
in name
if not i.isdigit()])
483 if cal_name ==
"badwire" or cal_name ==
"wiregain":
484 collection =
Collection(collector=
collector(granularity=collector_granularity, name=cal_name),
485 input_files=input_file_dict[2],
486 pre_collector_path=pre_collector(cal_name)
488 elif cal_name ==
"coscorr" or cal_name ==
"cosedge" or cal_name ==
"onedcell":
489 collection =
Collection(collector=
collector(granularity=collector_granularity, name=cal_name),
490 input_files=input_file_dict[1],
491 pre_collector_path=pre_collector(cal_name)
494 collection =
Collection(collector=
collector(granularity=collector_granularity, name=cal_name),
495 input_files=input_file_dict[0],
496 pre_collector_path=pre_collector(cal_name)
498 self.add_collection(name=cal_name, collection=collection)
503 if dependencies
is not None:
504 for dep
in dependencies:
__init__(self, name, algorithms, input_file_dict, max_iterations=5, dependencies=None, collector_granularity='All')
max_iterations
maximum iterations