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