10Validation of KLM strip efficiency calibration.
15from prompt
import ValidationSettings
17from ROOT.Belle2
import BKLMElementNumbers, KLMCalibrationChecker, KLMElementNumbers
25settings = ValidationSettings(name=
'KLM strip efficiency',
27 download_files=[
'stdout'],
33def save_graph_to_root(graph_name):
35 Save a TGraph in a ROOT file.
37 graph = ROOT.gPad.GetPrimitive('Graph')
38 assert isinstance(graph, ROOT.TGraph) == 1
39 graph.SetName(graph_name)
43def save_graph_to_pdf(canvas, root_file, graph_name, exp, chunk):
45 Save a drawn TGraph in a PDF file.
47 graph = root_file.Get(graph_name)
48 assert isinstance(graph, ROOT.TGraph) == 1
49 graph.SetMarkerStyle(ROOT.EMarkerStyle.kFullDotSmall)
50 graph.SetMarkerColor(ROOT.EColor.kAzure + 10)
51 graph.GetXaxis().SetTitle(f
'Exp. {exp} -- Run number')
52 graph.GetYaxis().SetTitle(
'Plane efficiency')
57 canvas.SaveAs(f
'efficiency_exp{exp}_chunk{chunk}_{graph_name}.pdf')
60def run_validation(job_path, input_data_path, requested_iov, expert_config):
64 - job_path will be replaced with path/to/calibration_results
65 - input_data_path will be replaced
with path/to/data_path used
for calibration, e.g. /group/belle2/dataprod/Data/PromptSkim/
69 expert_config = json.loads(expert_config)
70 chunk_size = expert_config[
'chunk_size']
73 ROOT.PyConfig.IgnoreCommandLineOptions =
True
75 ROOT.gROOT.SetBatch(
True)
77 ROOT.gROOT.SetStyle(
"BELLE2")
79 ROOT.gStyle.SetOptStat(0)
82 database_file = f
'{job_path}/KLMStripEfficiency/outputdb/database.txt'
85 bklm = KLMElementNumbers.c_BKLM
86 eklm = KLMElementNumbers.c_EKLM
87 first_rpc = BKLMElementNumbers.c_FirstRPCLayer
88 graph_dictionary = {
'barrel_rpcs': f
'subdetector=={bklm} && layer>={first_rpc}',
89 'barrel_scintillators': f
'subdetector=={bklm} && layer<{first_rpc}',
90 'endcap_scintillators': f
'subdetector=={eklm}'}
95 with open(database_file)
as f:
97 fields = line.split(
' ')
98 if (fields[0] ==
'dbstore/KLMStripEfficiency'):
99 iov = fields[2].split(
',')
102 if (exp != previous_exp):
103 exp_run_dict[exp] = [run]
106 exp_run_dict[exp].append(run)
110 for exp, run_list
in exp_run_dict.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.items():
119 checker = KLMCalibrationChecker()
120 checker.setExperimentRun(exp, run)
121 checker.setTestingPayload(database_file)
122 basf2.B2INFO(f
'Creating strip efficiency results tree for experiment {exp}, run {run}.')
123 checker.setStripEfficiencyResultsFile(f
'strip_efficiency_exp{exp}_run{run}.root')
124 checker.checkStripEfficiency()
127 for exp, run_list
in exp_run_dict.items():
129 chunks = math.ceil(len(run_list) / chunk_size)
130 for chunk
in range(chunks):
131 file_name = f
'strip_efficiency_exp{exp}_chunk{chunk}.root'
133 f
'strip_efficiency_exp{exp}_run{run}.root' for run
in run_list[chunk * chunk_size:(chunk + 1) * chunk_size]]
134 subprocess.run([
'hadd',
'-f', file_name] + run_files, check=
True)
135 input_file = ROOT.TFile(f
'{file_name}')
136 output_file = ROOT.TFile(f
'histograms_{file_name}',
'recreate')
138 tree = input_file.Get(
'efficiency')
139 assert isinstance(tree, ROOT.TTree) == 1
140 canvas = ROOT.TCanvas(f
'canvas_exp{exp}_chunk{chunk}',
'canvas', 800, 500)
142 for graph_name, graph_cut
in graph_dictionary.items():
143 tree.Draw(
'efficiency:run', graph_cut)
144 save_graph_to_root(graph_name)
145 save_graph_to_pdf(canvas, output_file, graph_name, exp, chunk)
149 for run_file
in run_files:
153 basf2.B2ERROR(f
'The file {run_file} can not be removed: {e.strerror}')
156if __name__ ==
"__main__":