10Validation of KLM time calibration (cable delay and time constants).
15from prompt
import ValidationSettings
17from ROOT.Belle2
import KLMCalibrationChecker
23from ROOT
import TH1F, TH2F, TCanvas, TFile, gStyle
26settings = ValidationSettings(name=
'KLM time',
28 download_files=[
'stdout'],
31 "LowerTimeBoundaryRPC": -800.0,
32 "UpperTimeBoundaryRPC": -600.0,
33 "LowerTimeBoundaryScintillatorsBKLM": -4800.0,
34 "UpperTimeBoundaryScintillatorsBKLM": -4600.0,
35 "LowerTimeBoundaryScintillatorsEKLM": -4850.0,
36 "UpperTimeBoundaryScintillatorsEKLM": -4600.0,
40basf2.conditions.override_globaltags()
43def run_validation(job_path, input_data_path, requested_iov, expert_config):
47 - job_path will be replaced with path/to/calibration_results
48 - input_data_path will be replaced with path/to/data_path used for calibration, e.g. /group/belle2/dataprod/Data/PromptSkim/
52 expert_config = json.loads(expert_config)
54 chunk_size = expert_config[
'chunk_size']
55 LowerTimeBoundaryRPC = expert_config[
'LowerTimeBoundaryRPC']
56 UpperTimeBoundaryRPC = expert_config[
'UpperTimeBoundaryRPC']
57 LowerTimeBoundaryScintillatorsBKLM = expert_config[
'LowerTimeBoundaryScintillatorsBKLM']
58 UpperTimeBoundaryScintillatorsBKLM = expert_config[
'UpperTimeBoundaryScintillatorsBKLM']
59 LowerTimeBoundaryScintillatorsEKLM = expert_config[
'LowerTimeBoundaryScintillatorsEKLM']
60 UpperTimeBoundaryScintillatorsEKLM = expert_config[
'UpperTimeBoundaryScintillatorsEKLM']
63 ROOT.PyConfig.IgnoreCommandLineOptions =
True
65 ROOT.gROOT.SetBatch(
True)
67 ROOT.gROOT.SetStyle(
"BELLE2")
69 ROOT.gStyle.SetOptStat(0)
72 database_file = os.path.join(f
'{job_path}',
'KLMTime',
'outputdb',
'database.txt')
75 exp_run_dict_cabledelay = {}
76 exp_run_dict_constants = {}
77 previous_exp_cabledelay = -666
78 previous_exp_constants = -666
80 with open(database_file)
as f:
82 fields = line.split(
' ')
83 if fields[0] ==
'dbstore/KLMTimeCableDelay':
84 iov = fields[2].split(
',')
87 if exp != previous_exp_cabledelay:
88 exp_run_dict_cabledelay[exp] = [run]
89 previous_exp_cabledelay = exp
91 exp_run_dict_cabledelay[exp].append(run)
92 elif fields[0] ==
'dbstore/KLMTimeConstants':
93 iov = fields[2].split(
',')
96 if exp != previous_exp_constants:
97 exp_run_dict_constants[exp] = [run]
98 previous_exp_constants = exp
100 exp_run_dict_constants[exp].append(run)
104 for exp, run_list
in exp_run_dict_cabledelay.items():
106 if len(run_list) > 1:
107 if run_list[0] == 0
and run_list[1] > 5:
108 run_list[0] = run_list[1] - 5
110 for exp, run_list
in exp_run_dict_constants.items():
112 if len(run_list) > 1:
113 if run_list[0] == 0
and run_list[1] > 5:
114 run_list[0] = run_list[1] - 5
117 for exp, run_list
in exp_run_dict_cabledelay.items():
119 checker = KLMCalibrationChecker()
120 checker.setExperimentRun(exp, run)
121 checker.setTestingPayload(database_file)
122 basf2.B2INFO(f
'Creating time cable delay results tree for experiment {exp}, run {run}.')
123 checker.setTimeCableDelayResultsFile(f
'time_cabledelay_exp{exp}_run{run}.root')
124 checker.checkTimeCableDelay()
127 for exp, run_list
in exp_run_dict_constants.items():
129 checker = KLMCalibrationChecker()
130 checker.setExperimentRun(exp, run)
131 checker.setTestingPayload(database_file)
132 basf2.B2INFO(f
'Creating time constants results tree for experiment {exp}, run {run}.')
133 checker.setTimeConstantsResultsFile(f
'time_constants_exp{exp}_run{run}.root')
134 checker.checkTimeConstants()
137 for exp, run_list
in exp_run_dict_cabledelay.items():
139 chunks = math.ceil(len(run_list) / chunk_size)
141 for chunk
in range(chunks):
142 file_name = f
'time_cabledelay_exp{exp}_chunk{chunk}.root'
144 f
'time_cabledelay_exp{exp}_run{run}.root' for run
in run_list[chunk * chunk_size:(chunk + 1) * chunk_size]]
145 subprocess.run([
'hadd',
'-f', file_name] + run_files, check=
True)
146 chunk_files.append(file_name)
149 for run_file
in run_files:
153 basf2.B2ERROR(f
'The file {run_file} can not be removed: {e.strerror}')
156 final_file_name = f
'time_cabledelay_exp{exp}_all.root'
157 subprocess.run([
'hadd',
'-f', final_file_name] + chunk_files, check=
True)
158 input_file = ROOT.TFile(final_file_name)
160 gStyle.SetOptStat(1111111)
162 barrel_RPC = TH1F(
"barrel_RPC",
"time cable delay for Barrel RPC", 100, LowerTimeBoundaryRPC, UpperTimeBoundaryRPC)
163 barrel_scintillator = TH1F(
"barrel_scintillator",
"time cable delay for Barrel scintillator",
164 100, LowerTimeBoundaryScintillatorsBKLM, UpperTimeBoundaryScintillatorsBKLM)
165 endcap_scintillator = TH1F(
"endcap_scintillator",
"time cable delay for endcap scintillator",
166 100, LowerTimeBoundaryScintillatorsEKLM, UpperTimeBoundaryScintillatorsEKLM)
168 tree = input_file.Get(
"cabledelay")
169 assert isinstance(tree, ROOT.TTree) == 1
172 tree.Draw(
"timeDelay>>barrel_RPC",
"subdetector==1 & layer>=3")
173 barrel_RPC.GetXaxis().SetTitle(
"T_{cable} (ns)")
174 barrel_RPC.GetYaxis().SetTitle(
"Entries")
175 myC.Print(
"barrel_RPC.png")
177 tree.Draw(
"timeDelay>>barrel_scintillator",
"subdetector==1 & layer<3")
178 barrel_scintillator.GetXaxis().SetTitle(
"T_{cable} (ns)")
179 barrel_scintillator.GetYaxis().SetTitle(
"Entries")
180 myC.Print(
"barrel_scintillator.png")
182 tree.Draw(
"timeDelay>>endcap_scintillator",
"subdetector==2")
183 endcap_scintillator.GetXaxis().SetTitle(
"T_{cable} (ns)")
184 endcap_scintillator.GetYaxis().SetTitle(
"Entries")
185 myC.Print(
"endcap_scintillator.png")
188 fout = TFile(
"KLMTimeCableDelay.root",
"recreate")
190 barrel_scintillator.Write()
191 endcap_scintillator.Write()
196 for chunk_file
in chunk_files:
198 os.remove(chunk_file)
200 basf2.B2ERROR(f
'The file {chunk_file} can not be removed: {e.strerror}')
203 for exp, run_list
in exp_run_dict_constants.items():
205 chunks = math.ceil(len(run_list) / chunk_size)
207 for chunk
in range(chunks):
208 file_name = f
'time_constants_exp{exp}_chunk{chunk}.root'
210 f
'time_constants_exp{exp}_run{run}.root' for run
in run_list[chunk * chunk_size:(chunk + 1) * chunk_size]]
211 subprocess.run([
'hadd',
'-f', file_name] + run_files, check=
True)
212 chunk_files.append(file_name)
215 for run_file
in run_files:
219 basf2.B2ERROR(f
'The file {run_file} can not be removed: {e.strerror}')
222 final_file_name = f
'time_constants_exp{exp}_all.root'
223 subprocess.run([
'hadd',
'-f', final_file_name] + chunk_files, check=
True)
224 input_file = ROOT.TFile(final_file_name)
225 gStyle.SetOptStat(1111111)
227 barrel_RPCPhi = TH2F(
"barrel_RPCPhi",
228 "time constants for Barrel RPC phi readout", 100, 30000, 65000, 100, 0.004, 0.015)
229 barrel_RPCZ = TH2F(
"barrel_RPCZ",
230 "time constants for Barrel RPC Z readout", 100, 30000, 65000, 100, 0.0, 0.0025)
231 barrel_scintillator_constants = TH2F(
"barrel_scintillator_constants",
232 "time constants for Barrel scintillator", 100, 30000, 65000, 100, 0.075, 0.09)
233 endcap_scintillator_constants = TH2F(
"endcap_scintillator_constants",
234 "time constants for endcap scintillator", 100, 0, 16000, 100, 0.060, 0.075)
236 tree = input_file.Get(
"constants")
237 assert isinstance(tree, ROOT.TTree) == 1
240 tree.Draw(
"delayRPCPhi:channelNumber>>barrel_RPCPhi",
"subdetector==1 & layer>=3",
"colz")
241 barrel_RPCPhi.GetXaxis().SetTitle(
"Channel number")
242 barrel_RPCPhi.GetYaxis().SetTitle(
"Time Delay constants")
243 myC.Print(
"barrel_RPCPhi.png")
245 tree.Draw(
"delayRPCZ:channelNumber>>barrel_RPCZ",
"subdetector==1 & layer>=3",
"colz")
246 barrel_RPCZ.GetXaxis().SetTitle(
"Channel number")
247 barrel_RPCZ.GetYaxis().SetTitle(
"Time Delay constants")
248 myC.Print(
"barrel_RPCZ.png")
250 tree.Draw(
"delayBKLM:channelNumber>>barrel_scintillator_constants",
"subdetector==1 & layer<3",
"colz")
251 barrel_scintillator_constants.GetXaxis().SetTitle(
"Channel number")
252 barrel_scintillator_constants.GetYaxis().SetTitle(
"Time Delay constants")
253 myC.Print(
"barrel_scintillator_constants.png")
255 tree.Draw(
"delayEKLM:channelNumber>>endcap_scintillator_constants",
"subdetector==2",
"colz")
256 endcap_scintillator_constants.GetXaxis().SetTitle(
"Channel number")
257 endcap_scintillator_constants.GetYaxis().SetTitle(
"Time Delay constants")
258 myC.Print(
"endcap_scintillator_constants.png")
260 fout = TFile(
"KLMTimeConstants.root",
"recreate")
261 barrel_RPCPhi.Write()
263 barrel_scintillator_constants.Write()
264 endcap_scintillator_constants.Write()
269 for chunk_file
in chunk_files:
271 os.remove(chunk_file)
273 basf2.B2ERROR(f
'The file {chunk_file} can not be removed: {e.strerror}')
276if __name__ ==
"__main__":