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