4 Validation of KLM strip efficiency calibration.
9 from prompt
import ValidationSettings
11 from ROOT.Belle2
import BKLMElementNumbers, KLMCalibrationChecker, KLMElementNumbers
19 settings = ValidationSettings(name=
'KLM strip efficiency',
21 download_files=[
'stdout'],
27 def save_graph_to_root(graph_name):
29 Save a TGraph in a ROOT file.
31 graph = ROOT.gPad.GetPrimitive(
'Graph')
32 assert isinstance(graph, ROOT.TGraph) == 1
33 graph.SetName(graph_name)
37 def save_graph_to_pdf(canvas, root_file, graph_name, exp, chunk):
39 Save a drawn TGraph in a PDF file.
41 graph = root_file.Get(graph_name)
42 assert isinstance(graph, ROOT.TGraph) == 1
43 graph.SetMarkerStyle(ROOT.EMarkerStyle.kFullDotSmall)
44 graph.SetMarkerColor(ROOT.EColor.kAzure + 10)
45 graph.GetXaxis().SetTitle(f
'Exp. {exp} -- Run number')
46 graph.GetYaxis().SetTitle(
'Plane efficiency')
51 canvas.SaveAs(f
'efficiency_exp{exp}_chunk{chunk}_{graph_name}.pdf')
54 def run_validation(job_path, input_data_path, requested_iov, expert_config):
58 - job_path will be replaced with path/to/calibration_results
59 - input_data_path will be replaced with path/to/data_path used for calibration, e.g. /group/belle2/dataprod/Data/PromptSkim/
63 expert_config = json.loads(expert_config)
64 chunk_size = expert_config[
'chunk_size']
67 ROOT.PyConfig.IgnoreCommandLineOptions =
True
69 ROOT.gROOT.SetBatch(
True)
71 ROOT.gROOT.SetStyle(
"BELLE2")
73 ROOT.gStyle.SetOptStat(0)
76 database_file = f
'{job_path}/KLMStripEfficiency/outputdb/database.txt'
79 bklm = KLMElementNumbers.c_BKLM
80 eklm = KLMElementNumbers.c_EKLM
81 first_rpc = BKLMElementNumbers.c_FirstRPCLayer
82 graph_dictionary = {
'barrel_rpcs': f
'subdetector=={bklm} && layer>={first_rpc}',
83 'barrel_scintillators': f
'subdetector=={bklm} && layer<{first_rpc}',
84 'endcap_scintillators': f
'subdetector=={eklm}'}
89 with open(database_file)
as f:
91 fields = line.split(
' ')
92 if (fields[0] ==
'dbstore/KLMStripEfficiency'):
93 iov = fields[2].split(
',')
96 if (exp != previous_exp):
97 exp_run_dict[exp] = [run]
100 exp_run_dict[exp].append(run)
104 for exp, run_list
in exp_run_dict.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
111 for exp, run_list
in exp_run_dict.items():
113 checker = KLMCalibrationChecker()
114 checker.setExperimentRun(exp, run)
115 checker.setTestingPayload(database_file)
116 basf2.B2INFO(f
'Creating strip efficiency results tree for experiment {exp}, run {run}.')
117 checker.setStripEfficiencyResultsFile(f
'strip_efficiency_exp{exp}_run{run}.root')
118 checker.checkStripEfficiency()
121 for exp, run_list
in exp_run_dict.items():
123 chunks = math.ceil(len(run_list) / chunk_size)
124 for chunk
in range(chunks):
125 file_name = f
'strip_efficiency_exp{exp}_chunk{chunk}.root'
127 f
'strip_efficiency_exp{exp}_run{run}.root' for run
in run_list[chunk * chunk_size:(chunk + 1) * chunk_size]]
128 subprocess.run([
'hadd',
'-f', file_name] + run_files, check=
True)
129 input_file = ROOT.TFile(f
'{file_name}')
130 output_file = ROOT.TFile(f
'histograms_{file_name}',
'recreate')
132 tree = input_file.Get(
'efficiency')
133 assert isinstance(tree, ROOT.TTree) == 1
134 canvas = ROOT.TCanvas(f
'canvas_exp{exp}_chunk{chunk}',
'canvas', 800, 500)
136 for graph_name, graph_cut
in graph_dictionary.items():
137 tree.Draw(
'efficiency:run', graph_cut)
138 save_graph_to_root(graph_name)
139 save_graph_to_pdf(canvas, output_file, graph_name, exp, chunk)
143 for run_file
in run_files:
147 basf2.B2ERROR(f
'The file {run_file} can not be removed: {e.strerror}')
150 if __name__ ==
"__main__":
151 run_validation(*sys.argv[1:])