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