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