Belle II Software  release-06-02-00
top_calibration.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 # ---------------------------------------------------------------------------------------
13 # CAF calibration functions
14 # ---------------------------------------------------------------------------------------
15 
16 import basf2
17 from caf.framework import Calibration, Collection
18 from caf.strategies import SequentialRunByRun, SingleIOV, SequentialBoundaries
19 from ROOT.Belle2 import TOP
20 
21 
22 def BS13d_calibration_local(inputFiles, look_back=28, globalTags=None, localDBs=None):
23  '''
24  Returns calibration object for carrier shift calibration of BS13d with local runs
25  (laser, single-pulse or double-pulse).
26  :param inputFiles: A list of input files in sroot format
27  :param look_back: look-back window setting (set it to 0 to use the one from DB)
28  :param globalTags: a list of global tags, highest priority first
29  :param localDBs: a list of local databases, highest priority first
30  '''
31 
32  # create path
33  main = basf2.create_path()
34 
35  # add basic modules
36  main.add_module('SeqRootInput')
37  main.add_module('TOPGeometryParInitializer')
38  main.add_module('TOPUnpacker')
39  main.add_module('TOPRawDigitConverter', lookBackWindows=look_back,
40  useAsicShiftCalibration=False, useChannelT0Calibration=False)
41 
42  # collector module
43  collector = basf2.register_module('TOPAsicShiftsBS13dCollector')
44 
45  # algorithm
46  algorithm = TOP.TOPAsicShiftsBS13dAlgorithm()
47  algorithm.setWindowSize(0)
48 
49  # define calibration
50  cal = Calibration(name='TOP_BS13dCalibration', collector=collector,
51  algorithms=algorithm, input_files=inputFiles)
52  if globalTags:
53  for globalTag in reversed(globalTags):
54  cal.use_central_database(globalTag)
55  if localDBs:
56  for localDB in reversed(localDBs):
57  cal.use_local_database(localDB)
58  cal.pre_collector_path = main
59  cal.strategies = SequentialRunByRun
60 
61  return cal
62 
63 
64 def BS13d_calibration_rawdata(inputFiles, globalTags=None, localDBs=None):
65  '''
66  Returns calibration object for carrier shift calibration of BS13d with raw data.
67  :param inputFiles: A list of input files in raw data format
68  :param globalTags: a list of global tags, highest priority first
69  :param localDBs: a list of local databases, highest priority first
70  '''
71 
72  # create path
73  main = basf2.create_path()
74 
75  # add basic modules
76  main.add_module('RootInput')
77  main.add_module('TOPGeometryParInitializer')
78  main.add_module('TOPUnpacker')
79  main.add_module('TOPRawDigitConverter',
80  useAsicShiftCalibration=False, useChannelT0Calibration=False)
81 
82  # collector module
83  collector = basf2.register_module('TOPAsicShiftsBS13dCollector')
84 
85  # algorithm
86  algorithm = TOP.TOPAsicShiftsBS13dAlgorithm()
87 
88  # define calibration
89  cal = Calibration(name='TOP_BS13dCalibration', collector=collector,
90  algorithms=algorithm, input_files=inputFiles)
91  if globalTags:
92  for globalTag in reversed(globalTags):
93  cal.use_central_database(globalTag)
94  if localDBs:
95  for localDB in reversed(localDBs):
96  cal.use_local_database(localDB)
97  cal.pre_collector_path = main
98  cal.strategies = SequentialRunByRun
99 
100  return cal
101 
102 
103 def BS13d_calibration_cdst(inputFiles, time_offset=0, globalTags=None, localDBs=None,
104  new_cdst_format=True):
105  '''
106  Returns calibration object for carrier shift calibration of BS13d with processed data.
107  :param inputFiles: A list of input files in cdst data format
108  :param time_offset: time offset [ns]
109  :param globalTags: a list of global tags, highest priority first
110  :param localDBs: a list of local databases, highest priority first
111  :param new_cdst_format: True or False for new or old cdst format, respectively
112  '''
113 
114  # create path
115  main = basf2.create_path()
116 
117  # add basic modules
118  main.add_module('RootInput')
119  if new_cdst_format:
120  main.add_module('Gearbox')
121  main.add_module('Geometry')
122  main.add_module('Ext')
123  main.add_module('TOPUnpacker')
124  main.add_module('TOPRawDigitConverter')
125  main.add_module('TOPChannelMasker')
126  main.add_module('TOPBunchFinder', autoRange=True, subtractRunningOffset=False)
127  main.add_module('TOPTimeRecalibrator',
128  useAsicShiftCalibration=False, useChannelT0Calibration=True)
129  else:
130  main.add_module('TOPGeometryParInitializer')
131  main.add_module('TOPTimeRecalibrator',
132  useAsicShiftCalibration=False, useChannelT0Calibration=True)
133 
134  # collector module
135  collector = basf2.register_module('TOPAsicShiftsBS13dCollector',
136  timeOffset=time_offset, requireRecBunch=True)
137 
138  # algorithm
139  algorithm = TOP.TOPAsicShiftsBS13dAlgorithm()
140 
141  # define calibration
142  cal = Calibration(name='TOP_BS13dCalibration', collector=collector,
143  algorithms=algorithm, input_files=inputFiles)
144  if globalTags:
145  for globalTag in reversed(globalTags):
146  cal.use_central_database(globalTag)
147  if localDBs:
148  for localDB in reversed(localDBs):
149  cal.use_local_database(localDB)
150  cal.pre_collector_path = main
151  cal.strategies = SequentialRunByRun
152 
153  return cal
154 
155 
156 def moduleT0_calibration_DeltaT(inputFiles, globalTags=None, localDBs=None,
157  new_cdst_format=True):
158  '''
159  Returns calibration object for rough module T0 calibration with method DeltaT
160  :param inputFiles: A list of input files in cdst data format
161  :param globalTags: a list of global tags, highest priority first
162  :param localDBs: a list of local databases, highest priority first
163  :param new_cdst_format: True or False for new or old cdst format, respectively
164  '''
165 
166  # create path
167  main = basf2.create_path()
168 
169  # add basic modules
170  main.add_module('RootInput')
171  if new_cdst_format:
172  main.add_module('Gearbox')
173  main.add_module('Geometry')
174  main.add_module('Ext')
175  main.add_module('TOPUnpacker')
176  main.add_module('TOPRawDigitConverter')
177  main.add_module('TOPChannelMasker')
178  main.add_module('TOPBunchFinder', autoRange=True, subtractRunningOffset=False)
179  else:
180  main.add_module('TOPGeometryParInitializer')
181  main.add_module('TOPTimeRecalibrator', subtractBunchTime=False)
182  main.add_module('TOPChannelMasker')
183  main.add_module('TOPBunchFinder', usePIDLikelihoods=True, autoRange=True,
184  subtractRunningOffset=False)
185 
186  # collector module
187  collector = basf2.register_module('TOPModuleT0DeltaTCollector')
188  collector.param('granularity', 'run')
189 
190  # algorithm
191  algorithm = TOP.TOPModuleT0DeltaTAlgorithm()
192 
193  # define calibration
194  cal = Calibration(name='TOP_moduleT0_rough', collector=collector,
195  algorithms=algorithm, input_files=inputFiles)
196  if globalTags:
197  for globalTag in reversed(globalTags):
198  cal.use_central_database(globalTag)
199  if localDBs:
200  for localDB in reversed(localDBs):
201  cal.use_local_database(localDB)
202  cal.pre_collector_path = main
203  cal.strategies = SequentialBoundaries # Was SingleIOV before proc12
204 
205  return cal
206 
207 
208 def moduleT0_calibration_LL(inputFiles, sample='dimuon', globalTags=None, localDBs=None,
209  new_cdst_format=True):
210  '''
211  Returns calibration object for final module T0 calibration with method LL
212  :param inputFiles: A list of input files in cdst data format
213  :param sample: data sample ('dimuon' or 'bhabha')
214  :param globalTags: a list of global tags, highest priority first
215  :param localDBs: a list of local databases, highest priority first
216  :param new_cdst_format: True or False for new or old cdst format, respectively
217  '''
218 
219  # create path
220  main = basf2.create_path()
221 
222  # add basic modules
223  main.add_module('RootInput')
224  if new_cdst_format:
225  main.add_module('Gearbox')
226  main.add_module('Geometry')
227  main.add_module('Ext')
228  main.add_module('TOPUnpacker')
229  main.add_module('TOPRawDigitConverter')
230  main.add_module('TOPChannelMasker')
231  main.add_module('TOPBunchFinder', autoRange=True, subtractRunningOffset=False)
232  else:
233  main.add_module('TOPGeometryParInitializer')
234  main.add_module('TOPTimeRecalibrator', subtractBunchTime=False)
235  main.add_module('TOPChannelMasker')
236  main.add_module('TOPBunchFinder', usePIDLikelihoods=True, autoRange=True,
237  subtractRunningOffset=False)
238 
239  # collector module
240  collector = basf2.register_module('TOPModuleT0LLCollector')
241  collector.param('sample', sample)
242  collector.param('granularity', 'run')
243 
244  # algorithm
245  algorithm = TOP.TOPModuleT0LLAlgorithm()
246 
247  # define calibration
248  cal = Calibration(name='TOP_moduleT0_final', collector=collector,
249  algorithms=algorithm, input_files=inputFiles)
250  if globalTags:
251  for globalTag in reversed(globalTags):
252  cal.use_central_database(globalTag)
253  if localDBs:
254  for localDB in reversed(localDBs):
255  cal.use_local_database(localDB)
256  cal.pre_collector_path = main
257  cal.strategies = SequentialBoundaries # Was SingleIOV before proc12
258 
259  return cal
260 
261 
262 def commonT0_calibration_BF(inputFiles, globalTags=None, localDBs=None,
263  new_cdst_format=True):
264  '''
265  Returns calibration object for common T0 calibration with method BF
266  :param inputFiles: A list of input files in cdst data format
267  :param globalTags: a list of global tags, highest priority first
268  :param localDBs: a list of local databases, highest priority first
269  :param new_cdst_format: True or False for new or old cdst format, respectively
270  '''
271 
272  # create path
273  main = basf2.create_path()
274 
275  # add basic modules
276  main.add_module('RootInput')
277  if new_cdst_format:
278  main.add_module('Gearbox')
279  main.add_module('Geometry')
280  main.add_module('Ext')
281  main.add_module('TOPUnpacker')
282  main.add_module('TOPRawDigitConverter')
283  main.add_module('TOPChannelMasker')
284  main.add_module('TOPBunchFinder', autoRange=True, subtractRunningOffset=False)
285  else:
286  main.add_module('TOPGeometryParInitializer')
287  main.add_module('TOPTimeRecalibrator', subtractBunchTime=False)
288  main.add_module('TOPChannelMasker')
289  main.add_module('TOPBunchFinder', usePIDLikelihoods=True, autoRange=True,
290  subtractRunningOffset=False)
291 
292  # collector module
293  collector = basf2.register_module('TOPCommonT0BFCollector')
294 
295  # algorithm
296  algorithm = TOP.TOPCommonT0BFAlgorithm()
297 
298  # define calibration
299  cal = Calibration(name='TOP_commonT0Calibration', collector=collector,
300  algorithms=algorithm, input_files=inputFiles)
301  if globalTags:
302  for globalTag in reversed(globalTags):
303  cal.use_central_database(globalTag)
304  if localDBs:
305  for localDB in reversed(localDBs):
306  cal.use_local_database(localDB)
307  cal.pre_collector_path = main
308  cal.strategies = SequentialRunByRun
309 
310  return cal
311 
312 
313 def commonT0_calibration_LL(inputFiles, sample='dimuon', globalTags=None, localDBs=None,
314  new_cdst_format=True):
315  '''
316  Returns calibration object for common T0 calibration with method LL
317  :param inputFiles: A list of input files in cdst data format
318  :param sample: data sample ('dimuon' or 'bhabha')
319  :param globalTags: a list of global tags, highest priority first
320  :param localDBs: a list of local databases, highest priority first
321  :param new_cdst_format: True or False for new or old cdst format, respectively
322  '''
323 
324  # create path
325  main = basf2.create_path()
326 
327  # basic modules
328  main.add_module('RootInput')
329  if new_cdst_format:
330  main.add_module('Gearbox')
331  main.add_module('Geometry')
332  main.add_module('Ext')
333  main.add_module('TOPUnpacker')
334  main.add_module('TOPRawDigitConverter')
335  main.add_module('TOPChannelMasker')
336  main.add_module('TOPBunchFinder', autoRange=True, subtractRunningOffset=False)
337  else:
338  main.add_module('TOPGeometryParInitializer')
339  main.add_module('TOPTimeRecalibrator', subtractBunchTime=False)
340  main.add_module('TOPChannelMasker')
341  main.add_module('TOPBunchFinder', usePIDLikelihoods=True, autoRange=True,
342  subtractRunningOffset=False)
343 
344  # collector module
345  collector = basf2.register_module('TOPCommonT0LLCollector')
346  collector.param('sample', sample)
347 
348  # algorithm
349  algorithm = TOP.TOPCommonT0LLAlgorithm()
350 
351  # define calibration
352  cal = Calibration(name='TOP_commonT0Calibration', collector=collector,
353  algorithms=algorithm, input_files=inputFiles)
354  if globalTags:
355  for globalTag in reversed(globalTags):
356  cal.use_central_database(globalTag)
357  if localDBs:
358  for localDB in reversed(localDBs):
359  cal.use_local_database(localDB)
360  cal.pre_collector_path = main
361  cal.strategies = SequentialRunByRun
362 
363  return cal
364 
365 
366 def pulseHeight_calibration_laser(inputFiles, t_min=-50.0, t_max=0.0, look_back=28,
367  globalTags=None, localDBs=None):
368  '''
369  Returns calibration object for calibration of pulse-height distributions and
370  threshold efficiencies with local laser runs.
371  :param inputFiles: A list of input files in sroot format
372  :param t_min: lower edge of time window to select laser signal [ns]
373  :param t_max: upper edge of time window to select laser signal [ns]
374  :param look_back: look-back window setting (set it to 0 to use the one from DB)
375  :param globalTags: a list of global tags, highest priority first
376  :param localDBs: a list of local databases, highest priority first
377  '''
378 
379  # create path
380  main = basf2.create_path()
381 
382  # add basic modules
383  main.add_module('SeqRootInput')
384  main.add_module('TOPGeometryParInitializer')
385  main.add_module('TOPUnpacker')
386  main.add_module('TOPRawDigitConverter', lookBackWindows=look_back)
387 
388  # collector module
389  collector = basf2.register_module('TOPPulseHeightCollector')
390  collector.param('timeWindow', [t_min, t_max])
391  collector.param('granularity', 'all')
392 
393  # algorithm
394  algorithm = TOP.TOPPulseHeightAlgorithm()
395 
396  # define calibration
397  cal = Calibration(name='TOP_pulseHeightCalibration', collector=collector,
398  algorithms=algorithm, input_files=inputFiles)
399  if globalTags:
400  for globalTag in reversed(globalTags):
401  cal.use_central_database(globalTag)
402  if localDBs:
403  for localDB in reversed(localDBs):
404  cal.use_local_database(localDB)
405  cal.pre_collector_path = main
406  cal.strategies = SingleIOV
407 
408  return cal
409 
410 
411 def pulseHeight_calibration_rawdata(inputFiles, globalTags=None, localDBs=None):
412  '''
413  Returns calibration object for calibration of pulse-height distributions and
414  threshold efficiencies with raw data
415  :param inputFiles: A list of input files in raw data format
416  :param globalTags: a list of global tags, highest priority first
417  :param localDBs: a list of local databases, highest priority first
418  '''
419 
420  # create path
421  main = basf2.create_path()
422 
423  # add basic modules
424  main.add_module('RootInput')
425  main.add_module('TOPGeometryParInitializer')
426  main.add_module('TOPUnpacker')
427  main.add_module('TOPRawDigitConverter')
428 
429  # collector module
430  collector = basf2.register_module('TOPPulseHeightCollector')
431  collector.param('granularity', 'all')
432 
433  # algorithm
434  algorithm = TOP.TOPPulseHeightAlgorithm()
435 
436  # define calibration
437  cal = Calibration(name='TOP_pulseHeightCalibration', collector=collector,
438  algorithms=algorithm, input_files=inputFiles)
439  if globalTags:
440  for globalTag in reversed(globalTags):
441  cal.use_central_database(globalTag)
442  if localDBs:
443  for localDB in reversed(localDBs):
444  cal.use_local_database(localDB)
445  cal.pre_collector_path = main
446  cal.strategies = SingleIOV
447 
448  return cal
449 
450 
451 def module_alignment(inputFiles, sample='dimuon', fixedParameters=None,
452  globalTags=None, localDBs=None, new_cdst_format=True,
453  backend_args=None):
454  '''
455  Returns calibration object for alignment of TOP modules.
456  :param inputFiles: A list of input files in cdst data format
457  :param sample: data sample ('dimuon' or 'bhabha')
458  :fixedParameters: a list of parameters to be fixed (parameter names: basf2 -m TOPAlignmentCollector)
459  :param globalTags: a list of global tags, highest priority first
460  :param localDBs: a list of local databases, highest priority first
461  :param new_cdst_format: True or False for new or old cdst format, respectively
462  :param backend_args: Dictionary of backend args for the Collection object to use
463  '''
464 
465  # define calibration
466  cal = Calibration(name='TOP_alignment')
467  if globalTags:
468  for globalTag in reversed(globalTags):
469  cal.use_central_database(globalTag)
470  if localDBs:
471  for localDB in reversed(localDBs):
472  cal.use_local_database(localDB)
473  cal.strategies = SingleIOV
474 
475  # add collections
476  for slot in range(1, 17):
477  # create path
478  main = basf2.create_path()
479 
480  # add basic modules
481  main.add_module('RootInput')
482  if new_cdst_format:
483  main.add_module('Gearbox')
484  main.add_module('Geometry')
485  main.add_module('Ext')
486  main.add_module('TOPUnpacker')
487  main.add_module('TOPRawDigitConverter')
488  main.add_module('TOPChannelMasker')
489  main.add_module('TOPBunchFinder', autoRange=True, subtractRunningOffset=False)
490  else:
491  main.add_module('TOPGeometryParInitializer')
492  main.add_module('TOPTimeRecalibrator', subtractBunchTime=False)
493  main.add_module('TOPChannelMasker')
494  main.add_module('TOPBunchFinder', autoRange=True,
495  usePIDLikelihoods=True, subtractRunningOffset=False)
496 
497  # collector module: executing iterative alignment method
498  collector = basf2.register_module('TOPAlignmentCollector')
499  collector.param('sample', sample)
500  if fixedParameters:
501  collector.param('parFixed', fixedParameters)
502  collector.param('targetModule', slot)
503  collector.param('granularity', 'all')
504 
505  # define collection:
506  # because of iterative nature of the method we must run exactly one job per collection
507  # which must process all the files from input list
508  collection = Collection(collector=collector, input_files=inputFiles,
509  pre_collector_path=main, max_collector_jobs=1)
510  if globalTags:
511  for globalTag in reversed(globalTags):
512  collection.use_central_database(globalTag)
513  if localDBs:
514  for localDB in reversed(localDBs):
515  collection.use_local_database(localDB)
516  if backend_args:
517  collection.backend_args = backend_args
518 
519  # add collection to calibration
520  cal.add_collection(name='slot_' + '{:0=2d}'.format(slot), collection=collection)
521 
522  # algorithm: it just greps the last iterations of collections and prepares the payload
523  algorithm = TOP.TOPAlignmentAlgorithm()
524  cal.algorithms = algorithm
525 
526  return cal
527 
528 
529 def channel_mask_calibration(inputFiles, globalTags=None, localDBs=None, unpack=True):
530  '''
531  Returns calibration object for channel masking
532  :param inputFiles: A list of input files in raw data or cdst format
533  :param globalTags: a list of global tags, highest priority first
534  :param localDBs: a list of local databases, highest priority first
535  :param unpack: True if data unpacking is required (i.e. for raw data or for new cdst format)
536  '''
537 
538  # create path
539  main = basf2.create_path()
540 
541  # add basic modules
542  main.add_module('RootInput')
543  main.add_module('TOPGeometryParInitializer')
544  if unpack:
545  main.add_module('TOPUnpacker')
546  main.add_module('TOPRawDigitConverter')
547 
548  # collector module
549  collector = basf2.register_module('TOPChannelMaskCollector')
550 
551  # algorithm
552  algorithm = TOP.TOPChannelMaskAlgorithm()
553 
554  # define calibration
555  cal = Calibration(name='TOP_ChannelMaskCalibration', collector=collector,
556  algorithms=algorithm, input_files=inputFiles)
557  if globalTags:
558  for globalTag in reversed(globalTags):
559  cal.use_central_database(globalTag)
560  if localDBs:
561  for localDB in reversed(localDBs):
562  cal.use_local_database(localDB)
563  cal.pre_collector_path = main
564  cal.strategies = SequentialRunByRun
565 
566  return cal
567 
568 
569 def calibration_validation(inputFiles, sample='dimuon', globalTags=None, localDBs=None, new_cdst_format=True):
570  '''
571  Returns calibration object for final module T0 calibration with method LL
572  :param inputFiles: A list of input files in cdst data format
573  :param sample: data sample ('dimuon' or 'bhabha')
574  :param globalTags: a list of global tags, highest priority first
575  :param localDBs: a list of local databases, highest priority first
576  :param new_cdst_format: True or False for new or old cdst format, respectively
577  '''
578 
579  # create path
580  main = basf2.create_path()
581 
582  # add basic modules
583  main.add_module('RootInput')
584  if new_cdst_format:
585  main.add_module('Gearbox')
586  main.add_module('Geometry')
587  main.add_module('Ext')
588  main.add_module('TOPUnpacker')
589  main.add_module('TOPRawDigitConverter')
590  main.add_module('TOPChannelMasker')
591  main.add_module('TOPBunchFinder')
592  else:
593  main.add_module('TOPGeometryParInitializer')
594  main.add_module('TOPTimeRecalibrator', subtractBunchTime=False)
595  main.add_module('TOPChannelMasker')
596  main.add_module('TOPBunchFinder', usePIDLikelihoods=True)
597 
598  # collector module
599  collector = basf2.register_module('TOPValidationCollector')
600  collector.param('sample', sample)
601  collector.param('granularity', 'run')
602 
603  # algorithm
604  algorithm = TOP.TOPValidationAlgorithm()
605 
606  # define calibration
607  cal = Calibration(name='TOP_validation', collector=collector,
608  algorithms=algorithm, input_files=inputFiles)
609  if globalTags:
610  for globalTag in reversed(globalTags):
611  cal.use_central_database(globalTag)
612  if localDBs:
613  for localDB in reversed(localDBs):
614  cal.use_local_database(localDB)
615  cal.pre_collector_path = main
616  cal.strategies = SingleIOV
617 
618  return cal