12 Validation of KLM strip efficiency calibration.
17 from prompt
import ValidationSettings
19 from ROOT.Belle2
import BKLMElementNumbers, KLMCalibrationChecker, KLMElementNumbers
27 settings = ValidationSettings(name=
'KLM strip efficiency',
29 download_files=[
'stdout'],
35 def save_graph_to_root(graph_name):
37 Save a TGraph in a ROOT file.
39 graph = ROOT.gPad.GetPrimitive(
'Graph')
40 assert isinstance(graph, ROOT.TGraph) == 1
41 graph.SetName(graph_name)
45 def save_graph_to_pdf(canvas, root_file, graph_name, exp, chunk):
47 Save a drawn TGraph in a PDF file.
49 graph = root_file.Get(graph_name)
50 assert isinstance(graph, ROOT.TGraph) == 1
51 graph.SetMarkerStyle(ROOT.EMarkerStyle.kFullDotSmall)
52 graph.SetMarkerColor(ROOT.EColor.kAzure + 10)
53 graph.GetXaxis().SetTitle(f
'Exp. {exp} -- Run number')
54 graph.GetYaxis().SetTitle(
'Plane efficiency')
59 canvas.SaveAs(f
'efficiency_exp{exp}_chunk{chunk}_{graph_name}.pdf')
62 def run_validation(job_path, input_data_path, requested_iov, expert_config):
66 - job_path will be replaced with path/to/calibration_results
67 - input_data_path will be replaced with path/to/data_path used for calibration, e.g. /group/belle2/dataprod/Data/PromptSkim/
71 expert_config = json.loads(expert_config)
72 chunk_size = expert_config[
'chunk_size']
75 ROOT.PyConfig.IgnoreCommandLineOptions =
True
77 ROOT.gROOT.SetBatch(
True)
79 ROOT.gROOT.SetStyle(
"BELLE2")
81 ROOT.gStyle.SetOptStat(0)
84 database_file = f
'{job_path}/KLMStripEfficiency/outputdb/database.txt'
87 bklm = KLMElementNumbers.c_BKLM
88 eklm = KLMElementNumbers.c_EKLM
89 first_rpc = BKLMElementNumbers.c_FirstRPCLayer
90 graph_dictionary = {
'barrel_rpcs': f
'subdetector=={bklm} && layer>={first_rpc}',
91 'barrel_scintillators': f
'subdetector=={bklm} && layer<{first_rpc}',
92 'endcap_scintillators': f
'subdetector=={eklm}'}
97 with open(database_file)
as f:
99 fields = line.split(
' ')
100 if (fields[0] ==
'dbstore/KLMStripEfficiency'):
101 iov = fields[2].split(
',')
104 if (exp != previous_exp):
105 exp_run_dict[exp] = [run]
108 exp_run_dict[exp].append(run)
112 for exp, run_list
in exp_run_dict.items():
114 if len(run_list) > 1:
115 if run_list[0] == 0
and run_list[1] > 5:
116 run_list[0] = run_list[1] - 5
119 for exp, run_list
in exp_run_dict.items():
121 checker = KLMCalibrationChecker()
122 checker.setExperimentRun(exp, run)
123 checker.setTestingPayload(database_file)
124 basf2.B2INFO(f
'Creating strip efficiency results tree for experiment {exp}, run {run}.')
125 checker.setStripEfficiencyResultsFile(f
'strip_efficiency_exp{exp}_run{run}.root')
126 checker.checkStripEfficiency()
129 for exp, run_list
in exp_run_dict.items():
131 chunks = math.ceil(len(run_list) / chunk_size)
132 for chunk
in range(chunks):
133 file_name = f
'strip_efficiency_exp{exp}_chunk{chunk}.root'
135 f
'strip_efficiency_exp{exp}_run{run}.root' for run
in run_list[chunk * chunk_size:(chunk + 1) * chunk_size]]
136 subprocess.run([
'hadd',
'-f', file_name] + run_files, check=
True)
137 input_file = ROOT.TFile(f
'{file_name}')
138 output_file = ROOT.TFile(f
'histograms_{file_name}',
'recreate')
140 tree = input_file.Get(
'efficiency')
141 assert isinstance(tree, ROOT.TTree) == 1
142 canvas = ROOT.TCanvas(f
'canvas_exp{exp}_chunk{chunk}',
'canvas', 800, 500)
144 for graph_name, graph_cut
in graph_dictionary.items():
145 tree.Draw(
'efficiency:run', graph_cut)
146 save_graph_to_root(graph_name)
147 save_graph_to_pdf(canvas, output_file, graph_name, exp, chunk)
151 for run_file
in run_files:
155 basf2.B2ERROR(f
'The file {run_file} can not be removed: {e.strerror}')
158 if __name__ ==
"__main__":