Belle II Software  release-05-01-25
run_channelT0_laser_calibration.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 # ---------------------------------------------------------------------------------------
5 # CAF calibration script: channel T0 with laser (including pre-calibration of BS13d)
6 # data type: local runs with laser
7 #
8 # usage: basf2 run_channelT0_laser_calibration.py expNo run_1 run_2 ... run_n
9 #
10 # authors: U. Tamponi, M. Staric
11 # ---------------------------------------------------------------------------------------
12 
13 import basf2
14 import sys
15 import os
16 import glob
17 from caf import backends
18 from caf.framework import Calibration, CAF
19 from caf.strategies import SequentialRunByRun, SingleIOV, SimpleRunByRun
20 from ROOT import Belle2
21 from ROOT.Belle2 import TOP
22 from basf2 import B2ERROR
23 from top_calibration import BS13d_calibration_local
24 
25 # ----- those parameters need to be adjusted before running -----------------------
26 #
27 globalTags = ['Reco_master_patch', 'data_reprocessing_proc10'] # highest priority first
28 localDBs = [] # highest priority first, local DB's have higher priority than global tags
29 data_dir = '/ghi/fs01/belle2/bdata/group/detector/TOP/2019-*/data_sroot_global/'
30 main_output_dir = 'top_calibration'
31 look_back = 28 # look-back window setting (set to 0 if look-back setting available in DB)
32 tts_file = '/group/belle2/group/detector/TOP/calibration/MCreferences/TTSParametrizations.root'
33 laser_mc_fit = '/group/belle2/group/detector/TOP/calibration/MCreferences/laserMCFit.root'
34 fit_mode = 'calibration' # can be either monitoring, MC or calibration
35 #
36 # ---------------------------------------------------------------------------------------
37 
38 # Argument parsing
39 argvs = sys.argv
40 if len(argvs) < 3:
41  print("usage: basf2", argvs[0], "experiment run_1 run_2 ... run_n")
42  sys.exit()
43 experiment = int(argvs[1])
44 run_numbers = sorted([int(r) for r in argvs[2:]])
45 run_first = run_numbers[0]
46 run_last = run_numbers[-1]
47 
48 # Make list of files
49 inputFiles = []
50 expNo = 'e' + '{:0=4d}'.format(experiment)
51 for run in run_numbers:
52  expRun = '{:0=4d}'.format(experiment) + '.' + '{:0=5d}'.format(run)
53  filename = f"{data_dir}/top.{expRun}.*.sroot"
54  inputFiles += glob.glob(filename)
55 if len(inputFiles) == 0:
56  runs = "".join([str(r) + "," for r in run_numbers])[:-1]
57  B2ERROR(f'No sroot files found in {data_dir} for exp={str(experiment)} runs={runs}')
58  sys.exit()
59 
60 # Check the existence of additional input files
61 if not os.path.isfile(tts_file):
62  B2ERROR(f"File {tts_file} not found")
63  sys.exit()
64 if not os.path.isfile(laser_mc_fit):
65  B2ERROR(f"File {laser_mc_fit} not found")
66  sys.exit()
67 
68 # Output folder name
69 run_range = 'r' + '{:0=5d}'.format(run_first) + '-' + '{:0=5d}'.format(run_last)
70 output_dir = f"{main_output_dir}/channelT0-local-{expNo}-{run_range}"
71 
72 # Suppress messages during processing
73 # basf2.set_log_level(basf2.LogLevel.WARNING)
74 
75 
76 def channelT0_calibration():
77  ''' calibration of channel T0 with laser data '''
78 
79  # create path
80  main = basf2.create_path()
81 
82  # basic modules
83  main.add_module('SeqRootInput')
84  main.add_module('TOPGeometryParInitializer')
85  main.add_module('TOPUnpacker')
86  main.add_module('TOPRawDigitConverter',
87  useSampleTimeCalibration=True,
88  useAsicShiftCalibration=True,
89  useChannelT0Calibration=False,
90  useModuleT0Calibration=False,
91  useCommonT0Calibration=False,
92  calpulseHeightMin=320,
93  calpulseHeightMax=680,
94  calpulseWidthMin=1.5,
95  calpulseWidthMax=2.2,
96  calibrationChannel=0,
97  lookBackWindows=look_back)
98 
99  # collector module
100  collector = basf2.register_module('TOPLaserCalibratorCollector')
101  collector.param('useReferencePulse', True)
102  collector.param('storeMCTruth', False)
103  collector.param('refChannel', 0) # Do not change this unless this channel is bad
104  collector.param('refSlot', 4) # Do not change this unless this slot is bad
105  # collector.param('pulserDeltaT', 37.5) # In some runs taken in fall 2019 the delay has been increased
106 
107  # algorithm
108  algorithm = TOP.TOPLocalCalFitter()
109  algorithm.setFitMode(fit_mode)
110  algorithm.setTTSFileName(tts_file)
111  algorithm.setFitConstraintsFileName(laser_mc_fit)
112 
113  # define calibration
114  cal = Calibration(name='TOP_channelT0', collector=collector,
115  algorithms=algorithm, input_files=inputFiles)
116  for globalTag in reversed(globalTags):
117  cal.use_central_database(globalTag)
118  for localDB in reversed(localDBs):
119  cal.use_local_database(localDB)
120  cal.pre_collector_path = main
121  cal.max_files_per_collector_job = 1
122  cal.strategies = SingleIOV # merge all runs together to gain statistics
123  return cal
124 
125 
126 # Define calibrations
127 cal1 = BS13d_calibration_local(inputFiles, look_back, globalTags, localDBs)
128 cal2 = channelT0_calibration()
129 cal1.backend_args = {"queue": "l"}
130 cal2.backend_args = {"queue": "l"}
131 cal2.depends_on(cal1)
132 
133 # Add calibrations to CAF
134 cal_fw = CAF()
135 cal_fw.add_calibration(cal1)
136 cal_fw.add_calibration(cal2)
137 cal_fw.output_dir = output_dir
138 cal_fw.backend = backends.LSF()
139 
140 # Run calibration
141 cal_fw.run()
backends.LSF
Definition: backends.py:1567
Calibration
Definition: Calibration.py:1