4 Validation of KLM strip efficiency calibration.
9 from prompt
import ValidationSettings
11 from ROOT.Belle2
import BKLMElementNumbers, KLMCalibrationChecker, KLMElementNumbers
18 settings = ValidationSettings(name=
'KLM strip efficiency',
20 download_files=[
'stdout'],
24 def save_graph_to_root(graph_name):
26 Save a TGraph in a ROOT file.
28 graph = ROOT.gPad.GetPrimitive(
'Graph')
29 assert isinstance(graph, ROOT.TGraph) == 1
30 graph.SetName(graph_name)
34 def save_graph_to_pdf(canvas, root_file, graph_name, exp, chunk):
36 Save a drawn TGraph in a PDF file.
38 graph = root_file.Get(graph_name)
39 assert isinstance(graph, ROOT.TGraph) == 1
40 graph.SetMarkerStyle(ROOT.EMarkerStyle.kFullDotSmall)
41 graph.SetMarkerColor(ROOT.EColor.kRed + 1)
42 graph.GetXaxis().SetTitle(f
'Exp. {exp} -- Run number')
43 graph.GetYaxis().SetTitle(
'Plane efficiency')
48 canvas.SaveAs(f
'efficiency_exp{exp}_chunk{chunk}_{graph_name}.pdf')
51 def run_validation(job_path, input_data_path, requested_iov, expert_config, **kwargs):
55 - job_path will be replaced with path/to/calibration_results
56 - input_data_path will be replaced with path/to/data_path used for calibration, e.g. /group/belle2/dataprod/Data/PromptSkim/
63 ROOT.PyConfig.IgnoreCommandLineOptions =
True
65 ROOT.gROOT.SetBatch(
True)
67 ROOT.gROOT.SetStyle(
"BELLE2")
69 ROOT.gStyle.SetOptStat(0)
72 database_file = f
'{job_path}/KLMStripEfficiency/outputdb/database.txt'
75 bklm = KLMElementNumbers.c_BKLM
76 eklm = KLMElementNumbers.c_EKLM
77 first_rpc = BKLMElementNumbers.c_FirstRPCLayer
78 graph_dictionary = {
'barrel_rpcs': f
'subdetector=={bklm} && layer>={first_rpc}',
79 'barrel_scintillators': f
'subdetector=={bklm} && layer<{first_rpc}',
80 'endcap_scintillators': f
'subdetector=={eklm}'}
85 with open(database_file)
as f:
87 fields = line.split(
' ')
88 if (fields[0] ==
'dbstore/KLMStripEfficiency'):
89 iov = fields[2].split(
',')
92 if (exp != previous_exp):
93 exp_run_dict[exp] = [run]
96 exp_run_dict[exp].append(run)
100 for exp, run_list
in exp_run_dict.items():
102 if len(run_list) > 1:
103 if run_list[0] == 0
and run_list[1] > 5:
104 run_list[0] = run_list[1] - 5
107 for exp, run_list
in exp_run_dict.items():
109 checker = KLMCalibrationChecker()
110 checker.setExperimentRun(exp, run)
111 checker.setTestingPayload(database_file)
112 basf2.B2INFO(f
'Creating strip efficiency results tree for experiment {exp}, run {run}.')
113 checker.setStripEfficiencyResultsFile(f
'strip_efficiency_exp{exp}_run{run}.root')
114 checker.checkStripEfficiency()
117 for exp, run_list
in exp_run_dict.items():
119 chunks = math.ceil(len(run_list) / chunk_size)
120 for chunk
in range(chunks):
121 file_name = f
'strip_efficiency_exp{exp}_chunk{chunk}.root'
122 run_file_names = [f
'strip_efficiency_exp{exp}_run{run}.root' for run
in run_list[chunk:(chunk + chunk_size)]]
123 subprocess.run([
'hadd',
'-f', file_name] + run_file_names, check=
True)
124 input_file = ROOT.TFile(f
'{file_name}')
125 output_file = ROOT.TFile(f
'histograms_{file_name}',
'recreate')
127 tree = input_file.Get(
'efficiency')
128 assert isinstance(tree, ROOT.TTree) == 1
129 canvas = ROOT.TCanvas(f
'canvas_exp{exp}_chunk{chunk}',
'canvas', 800, 500)
131 for graph_name, graph_cut
in graph_dictionary.items():
132 tree.Draw(
'efficiency:run', graph_cut)
133 save_graph_to_root(graph_name)
134 save_graph_to_pdf(canvas, output_file, graph_name, exp, chunk)
139 if __name__ ==
"__main__":
140 run_validation(*sys.argv[1:])