Belle II Software development
cdcdedx_validation.py
1
8
9'''
10Validation plots for CDC dedx calibration.
11'''
12
13import sys
14import os
15import json
16import matplotlib.pyplot as plt
17import pandas as pd
18import numpy as np
19from matplotlib.backends.backend_pdf import PdfPages
20import shutil
21import basf2
22import process_wiregain as pw
23import process_cosgain as pc
24import process_onedcell as oned
25import process_rungain as rg
26
27from prompt import ValidationSettings
28
29settings = ValidationSettings(name="CDC dedx",
30 description=__doc__,
31 download_files=[],
32 expert_config={
33 "GT": "data_prompt_rel09",
34 })
35
36
37def save_to_pdf(pdf, fig):
38 fig.tight_layout()
39 pdf.savefig(fig)
40 plt.close(fig)
41
42
43def read_txt(filepath, columns, sep=r"\s+"):
44 if not os.path.exists(filepath):
45 basf2.B2ERROR(f"File not found: {filepath}")
46 return None
47 return pd.read_csv(filepath, sep=sep, header=None, names=columns)
48
49
50def make_pdf_path(prefix, suffix):
51 pdf_path = os.path.join("plots", "validation", f"{prefix}_{suffix}.pdf")
52 os.makedirs(os.path.dirname(pdf_path), exist_ok=True)
53 return pdf_path
54
55
56def get_positive_minmax(series):
57 positive = series[series > 0]
58 ymin = positive.min() if not positive.empty else series.min()
59 ymax = series.max()
60 return ymin, ymax
61
62
63def rungain_validation(path, suffix):
64 val_path = os.path.join(path, "plots", "run", f"dedx_vs_run_{suffix}.txt")
65 df = read_txt(val_path, ["run", "mean", "mean_err", "reso", "reso_err"])
66 if df is None:
67 return
68
69 df['run'] = df['run'].astype(str)
70
71 pdf_path = make_pdf_path("dedx_vs_run", suffix)
72
73 with PdfPages(pdf_path) as pdf:
74 fig, ax = plt.subplots(1, 2, figsize=(20, 6))
75
76 # Mean plot
77 ymin, ymax = get_positive_minmax(df['mean'])
78 pc.hist(y_min=ymin-0.02, y_max=ymax+0.02, xlabel="Run range", ylabel="dE/dx mean", space=30, ax=ax[0])
79 ax[0].errorbar(df['run'], df['mean'], yerr=df['mean_err'], fmt='*', markersize=8, rasterized=True, label='Bhabha mean')
80 ax[0].legend(fontsize=12)
81 ax[0].set_title('dE/dx Mean vs Run', fontsize=14)
82
83 # Reso plot
84 ymin, ymax = get_positive_minmax(df['reso'])
85 pc.hist(y_min=ymin-0.01, y_max=ymax+0.01, xlabel="Run range", ylabel="dE/dx reso", space=30, ax=ax[1])
86 ax[1].errorbar(df['run'], df['reso'], yerr=df['reso_err'], fmt='*', markersize=8, rasterized=True, label='Bhabha reso')
87 ax[1].legend(fontsize=12)
88 ax[1].set_title('dE/dx Resolution vs Run', fontsize=14)
89
90 fig.suptitle("dE/dx vs Run", fontsize=20)
91 save_to_pdf(pdf, fig)
92
93
94def wiregain_validation(path, suffix):
95
96 val_path_gwire = os.path.join(path, "plots", "wire", f"dedx_mean_gwire_{suffix}.txt")
97 val_path_bwire = os.path.join(path, "plots", "wire", f"dedx_mean_badwire_{suffix}.txt")
98 val_path_layer = os.path.join(path, "plots", "wire", f"dedx_mean_layer_{suffix}.txt")
99
100 df_gwire = read_txt(val_path_gwire, ["wire", "mean"])
101 df_bwire = read_txt(val_path_bwire, ["wire", "mean"])
102 df_layer = read_txt(val_path_layer, ["layer", "mean", "gmean"])
103
104 if df_gwire is None or df_bwire is None or df_layer is None:
105 return
106
107 pdf_path = make_pdf_path("dedx_vs_wire_layer", suffix)
108
109 with PdfPages(pdf_path) as pdf:
110 fig, ax = plt.subplots(2, 2, figsize=(20, 12))
111
112 ymin, ymax = get_positive_minmax(df_gwire['mean'])
113
114 pc.hist(y_min=ymin-0.05, y_max=ymax+0.05, xlabel="Wire", ylabel="dE/dx mean", space=1000, ax=ax[0, 0])
115 ax[0, 0].plot(df_gwire['wire'], df_gwire['mean'], '*', markersize=5, rasterized=True)
116 ax[0, 0].set_title('dE/dx Mean vs good Wire', fontsize=14)
117
118 ymin, ymax = get_positive_minmax(df_bwire['mean'])
119
120 pc.hist(y_min=ymin-0.05, y_max=ymax+0.05, xlabel="Wire", ylabel="dE/dx mean", space=1000, ax=ax[1, 0])
121 ax[1, 0].plot(df_bwire['wire'], df_bwire['mean'], '*', markersize=5, rasterized=True)
122 ax[1, 0].set_title('dE/dx Mean vs bad Wire', fontsize=14)
123
124 ymin, ymax = get_positive_minmax(df_layer['mean'])
125
126 pc.hist(x_min=0, x_max=56, y_min=ymin-0.05, y_max=ymax+0.05, xlabel="Layer", ylabel="dE/dx mean", space=3, ax=ax[0, 1])
127 ax[0, 1].plot(df_layer['layer'], df_layer['mean'], '*', markersize=10, rasterized=True)
128 ax[0, 1].set_title('dE/dx Mean vs Layer', fontsize=14)
129
130 ymin, ymax = get_positive_minmax(df_layer['gmean'])
131 pc.hist(x_min=0, x_max=56, y_min=ymin-0.02, y_max=ymax+0.02, xlabel="Layer", ylabel="dE/dx mean", space=3, ax=ax[1, 1])
132 ax[1, 1].plot(df_layer['layer'], df_layer['gmean'], '*', markersize=10, rasterized=True)
133 ax[1, 1].set_title('dE/dx Mean vs Layer (good wires)', fontsize=14)
134
135 fig.suptitle("dE/dx vs #wire", fontsize=20)
136 save_to_pdf(pdf, fig)
137
138
139def cosgain_validation(path, suffix):
140 val_path_el = os.path.join(path, "plots", "costh", f"dedx_vs_cos_electrons_{suffix}.txt")
141 val_path_po = os.path.join(path, "plots", "costh", f"dedx_vs_cos_positrons_{suffix}.txt")
142
143 df_el = read_txt(val_path_el, ["cos", "mean", "mean_err", "reso", "reso_err"])
144 df_po = read_txt(val_path_po, ["cos", "mean", "mean_err", "reso", "reso_err"])
145
146 if df_el is None or df_po is None:
147 return
148
149 # Ensure both dataframes are sorted by 'cos' so addition is element-wise correct
150 df_el = df_el.sort_values(by='cos').reset_index(drop=True)
151 df_po = df_po.sort_values(by='cos').reset_index(drop=True)
152
153 # New DataFrame with summed means
154 mean_avg = (df_el['mean'] + df_po['mean']) / 2
155 err_avg = 0.5 * np.sqrt(df_el['mean_err']**2 + df_po['mean_err']**2)
156 df_sum = pd.DataFrame({'cos': df_el['cos'], 'mean_sum': mean_avg, 'err_avg': err_avg})
157
158 pdf_path = make_pdf_path("dedx_vs_cosine", suffix)
159
160 with PdfPages(pdf_path) as pdf:
161 fig, ax = plt.subplots(1, 2, figsize=(20, 6)) # Two plots side-by-side
162 # mean
163 pc.hist(x_min=-1.0, x_max=1.0, y_min=0.96, y_max=1.03, xlabel=r"cos#theta", ylabel="dE/dx mean", space=0.1, ax=ax[0])
164 ax[0].errorbar(
165 df_el['cos'],
166 df_el['mean'],
167 yerr=df_el['mean_err'],
168 fmt='*',
169 markersize=10,
170 rasterized=True,
171 label='electron')
172 ax[0].errorbar(
173 df_po['cos'],
174 df_po['mean'],
175 yerr=df_po['mean_err'],
176 fmt='*',
177 markersize=10,
178 rasterized=True,
179 label='positrons')
180 ax[0].errorbar(df_sum['cos'], df_sum['mean_sum'], yerr=df_sum['err_avg'], fmt='*',
181 markersize=10, rasterized=True, label=r'average of e^{+} and e^{-}')
182 ax[0].legend(fontsize=17)
183 ax[0].set_title('dE/dx Mean vs cosine', fontsize=14)
184
185 # reso
186 pc.hist(x_min=-1.0, x_max=1.0, y_min=0.04, y_max=0.13, xlabel=r"cos#theta", ylabel="dE/dx reso", space=0.1, ax=ax[1])
187 ax[1].errorbar(
188 df_el['cos'],
189 df_el['reso'],
190 yerr=df_el['reso_err'],
191 fmt='*',
192 markersize=10,
193 rasterized=True,
194 label='electron')
195 ax[1].errorbar(
196 df_po['cos'],
197 df_po['reso'],
198 yerr=df_po['reso_err'],
199 fmt='*',
200 markersize=10,
201 rasterized=True,
202 label='positrons')
203 ax[1].legend(fontsize=17)
204 ax[1].set_title('dE/dx Resolution vs cosine', fontsize=14)
205
206 fig.suptitle(r"dE/dx vs cos$\theta$", fontsize=20)
207 save_to_pdf(pdf, fig)
208
209
210def injection_validation(path, suffix):
211
212 cols = ["var", "bin", "mean", "mean_err", "reso", "reso_err"]
213 # corrected files
214 val_path_ler = os.path.join(path, "plots", "injection", f"dedx_vs_inj_ler_{suffix}.txt")
215 val_path_her = os.path.join(path, "plots", "injection", f"dedx_vs_inj_her_{suffix}.txt")
216
217 df_ler = read_txt(val_path_ler, cols)
218 df_her = read_txt(val_path_her, cols)
219
220 # no-correction files
221 val_path_ler_nocor = os.path.join(path, "plots", "injection", f"dedx_vs_inj_nocor_ler_{suffix}.txt")
222 val_path_her_nocor = os.path.join(path, "plots", "injection", f"dedx_vs_inj_nocor_her_{suffix}.txt")
223
224 df_ler_nocor = read_txt(val_path_ler_nocor, cols)
225 df_her_nocor = read_txt(val_path_her_nocor, cols)
226
227 if df_ler is None or df_her is None or df_ler_nocor is None or df_her_nocor is None:
228 return
229
230 for df in [df_ler, df_her, df_ler_nocor, df_her_nocor]:
231 df["bin"] = df["bin"].astype(str)
232
233 pdf_path = make_pdf_path("dedx_mean_inj", suffix)
234
235 with PdfPages(pdf_path) as pdf:
236
237 fig, ax = plt.subplots(1, 1, figsize=(20, 6))
238
239 ymin, ymax = get_positive_minmax(df_ler['mean'])
240
241 pc.hist(y_min=ymin - 0.01, y_max=ymax + 0.01,
242 xlabel="injection time", ylabel="dE/dx mean",
243 space=3, ax=ax)
244
245 ax.errorbar(df_ler['bin'], df_ler['mean'], yerr=df_ler['mean_err'],
246 fmt='*', markersize=10, rasterized=True, label='LER')
247 ax.errorbar(df_her['bin'], df_her['mean'], yerr=df_her['mean_err'],
248 fmt='*', markersize=10, rasterized=True, label='HER')
249 ax.legend(fontsize=19)
250
251 fig.suptitle("dE/dx vs Injection time", fontsize=20)
252 save_to_pdf(pdf, fig)
253
254 # Page 2: overlay corr and no corr
255 fig, ax = plt.subplots(1, 1, figsize=(20, 6))
256
257 all_means = pd.concat([
258 df_ler["mean"], df_her["mean"],
259 df_ler_nocor["mean"], df_her_nocor["mean"]
260 ])
261 positive_means = all_means[all_means > 0]
262
263 ymin2 = positive_means.min() if not positive_means.empty else all_means.min()
264 ymax2 = all_means.max()
265
266 pc.hist(y_min=ymin2 - 0.01, y_max=ymax2 + 0.01,
267 xlabel="injection time", ylabel="dE/dx mean",
268 space=3, ax=ax)
269
270 datasets = [
271 ("LER corr", df_ler, "o"),
272 ("HER corr", df_her, "s"),
273 ("LER no corr", df_ler_nocor, "o"),
274 ("HER no corr", df_her_nocor, "s"),
275 ]
276
277 for label, df, marker in datasets:
278 ax.errorbar(df['bin'], df['mean'], yerr=df['mean_err'],
279 fmt=marker, markersize=6, rasterized=True, label=label)
280 ax.legend(fontsize=19)
281 fig.suptitle("dE/dx vs Injection time: corrected vs no correction", fontsize=20)
282 save_to_pdf(pdf, fig)
283
284
285def mom_validation(path, suffix):
286
287 cos_labels = [
288 "acos",
289 "cos$\\theta > 0.0$",
290 "cos$\\theta < 0.0$",
291 "cos$\\theta \\leq -0.8$",
292 "cos$\\theta > -0.8$ and $\\cos\\theta \\leq -0.6$",
293 "cos$\\theta > -0.6$ and $\\cos\\theta \\leq -0.4$",
294 "cos$\\theta > -0.4$ and $\\cos\\theta \\leq -0.2$",
295 "cos$\\theta > -0.2$ and $\\cos\\theta \\leq 0$",
296 "cos$\\theta > 0$ and $\\cos\\theta \\leq 0.2$",
297 "cos$\\theta > 0.2$ and $\\cos\\theta \\leq 0.4$",
298 "cos$\\theta > 0.4$ and $\\cos\\theta \\leq 0.6$",
299 "cos$\\theta > 0.6$ and $\\cos\\theta \\leq 0.8$",
300 "cos$\\theta > 0.8$"
301 ]
302
303 # Define output PDFs
304 pdf_paths = {
305 "low": make_pdf_path("dedx_vs_mom", suffix),
306 "high": make_pdf_path("dedx_vs_mom", f"{suffix}_cosbins"),
307 }
308
309 with PdfPages(pdf_paths["low"]) as pdf_low, PdfPages(pdf_paths["high"]) as pdf_high:
310 for i in range(13):
311 cols = ["mom", "mean", "mean_err", "reso", "reso_err"]
312 val_path_el = os.path.join(path, "plots", "mom", f"dedx_vs_mom_{i}_elec_{suffix}.txt")
313 val_path_po = os.path.join(path, "plots", "mom", f"dedx_vs_mom_{i}_posi_{suffix}.txt")
314
315 df_el = read_txt(val_path_el, cols)
316 df_po = read_txt(val_path_po, cols)
317
318 if df_el is None or df_po is None:
319 continue
320
321 df_el['mom'] *= -1 # flip electron momentum
322
323 fig, ax = plt.subplots(2, 2, figsize=(20, 12))
324
325 ymin, ymax = get_positive_minmax(df_el['mean'])
326
327 panels = [
328 {"xlim": (-7, 7), "ylim": (ymin-0.01, ymax+0.01),
329 "ylabel": "dE/dx mean", "df_col": "mean", "err_col": "mean_err",
330 "title": "dE/dx Mean vs momentum"},
331 {"xlim": (-7, 7), "ylim": (0.04, 0.1),
332 "ylabel": "dE/dx reso", "df_col": "reso", "err_col": "reso_err",
333 "title": "dE/dx resolution vs momentum"},
334 {"xlim": (-3, 3), "ylim": (ymin-0.01, ymax+0.01),
335 "ylabel": "dE/dx mean", "df_col": "mean", "err_col": "mean_err",
336 "title": "dE/dx Mean vs momentum (zoomed)"},
337 {"xlim": (-3, 3), "ylim": (0.04, 0.1),
338 "ylabel": "dE/dx reso", "df_col": "reso", "err_col": "reso_err",
339 "title": "dE/dx resolution vs momentum (zoomed)"},
340 ]
341
342 for ax_i, panel in zip(ax.flat, panels):
343 pc.hist(x_min=panel["xlim"][0], x_max=panel["xlim"][1],
344 y_min=panel["ylim"][0], y_max=panel["ylim"][1],
345 xlabel="Momentum", ylabel=panel["ylabel"],
346 space=1, ax=ax_i)
347
348 ax_i.errorbar(df_el['mom'], df_el[panel["df_col"]],
349 yerr=df_el[panel["err_col"]],
350 fmt='*', markersize=10, rasterized=True, label='electron')
351 ax_i.errorbar(df_po['mom'], df_po[panel["df_col"]],
352 yerr=df_po[panel["err_col"]],
353 fmt='*', markersize=10, rasterized=True, label='positron')
354 ax_i.legend(fontsize=17)
355 ax_i.set_title(panel["title"], fontsize=14)
356 if i == 3 and panel["df_col"] == "reso":
357 ymin, ymax = ax_i.get_ylim()
358 ax_i.set_ylim(ymin, ymax * 1.5)
359
360 fig.suptitle(f"dE/dx vs Momentum ({cos_labels[i]})", fontsize=20)
361 plt.tight_layout()
362
363 # Save to correct PDF
364 if i <= 2:
365 save_to_pdf(pdf_low, fig)
366 else:
367 save_to_pdf(pdf_high, fig)
368
369
370def oneDcell_validation(path, suffix):
371
372 val_path_il = os.path.join(path, "plots", "oneD", f"dedx_vs_1D_IL_{suffix}.txt")
373 val_path_ol = os.path.join(path, "plots", "oneD", f"dedx_vs_1D_OL_{suffix}.txt")
374
375 df_il = read_txt(val_path_il, ["enta", "mean"])
376 df_ol = read_txt(val_path_ol, ["enta", "mean"])
377
378 if df_il is None or df_ol is None:
379 return
380
381 pdf_path = make_pdf_path("dedx_vs_enta", suffix)
382
383 with PdfPages(pdf_path) as pdf:
384 fig, ax = plt.subplots(2, 2, figsize=(20, 12))
385
386 pc.hist(x_min=-1.5, x_max=1.5, y_min=0.9, y_max=1.07, xlabel=r"entaRS", ylabel="dE/dx mean", space=0.3, ax=ax[0, 0])
387 ax[0, 0].plot(df_il['enta'], df_il['mean'], '-', markersize=10, rasterized=True, label='IL')
388 ax[0, 0].legend(fontsize=17)
389 ax[0, 0].set_title('dE/dx Mean vs entaRS (IL)', fontsize=14)
390
391 pc.hist(x_min=-1.5, x_max=1.5, y_min=0.9, y_max=1.05, xlabel=r"entaRS", ylabel="dE/dx mean", space=0.3, ax=ax[0, 1])
392 ax[0, 1].plot(df_ol['enta'], df_ol['mean'], '-', markersize=10, rasterized=True, label='OL')
393 ax[0, 1].legend(fontsize=17)
394 ax[0, 1].set_title('dE/dx Mean vs entaRS (OL)', fontsize=14)
395
396 pc.hist(x_min=-0.2, x_max=0.2, y_min=0.9, y_max=1.07, xlabel=r"entaRS", ylabel="dE/dx mean", space=0.02, ax=ax[1, 0])
397 ax[1, 0].plot(df_il['enta'], df_il['mean'], '-', markersize=10, rasterized=True, label='IL')
398 ax[1, 0].legend(fontsize=17)
399 ax[1, 0].set_title('dE/dx Mean vs entaRS (IL) zoom', fontsize=14)
400
401 pc.hist(x_min=-0.2, x_max=0.2, y_min=0.9, y_max=1.05, xlabel=r"entaRS", ylabel="dE/dx mean", space=0.02, ax=ax[1, 1])
402 ax[1, 1].plot(df_ol['enta'], df_ol['mean'], '-', markersize=10, rasterized=True, label='OL')
403 ax[1, 1].legend(fontsize=17)
404 ax[1, 1].set_title('dE/dx Mean vs entaRS (OL) zoom', fontsize=14)
405
406 fig.suptitle("dE/dx vs entaRS", fontsize=20)
407 save_to_pdf(pdf, fig)
408
409
410def run_validation(job_path, input_data_path, requested_iov, expert_config, **kwargs):
411 '''
412 Makes validation plots
413 :job_path: path to cdcdedx calibration output
414 :input_data_path: path to the input files
415 :requested_iov: required argument but not used
416 :expert_config: required argument
417 '''
418 os.makedirs('plots/validation', exist_ok=True)
419 os.makedirs('plots/constant', exist_ok=True)
420
421 expert_config = json.loads(expert_config)
422 GT = expert_config["GT"]
423
424 basf2.B2INFO("Starting validation...")
425
426 basf2.B2INFO("Processing run gain payloads...")
427 gtpath = os.path.join(job_path, 'rungain2', 'outputdb')
428 rg.getRunGain(gtpath, GT)
429
430 basf2.B2INFO("Processing coscorr payloads...")
431 ccpath = os.path.join(job_path, 'coscorr1', 'outputdb')
432 pc.process_cosgain(ccpath, GT)
433
434 basf2.B2INFO("Processing wire gain payloads...")
435 wgpath = os.path.join(job_path, 'wiregain0', 'outputdb')
436 exp_run_dict = pw.process_wiregain(wgpath, GT)
437
438 basf2.B2INFO("Processing 1D gain payloads...")
439 onedpath = os.path.join(job_path, 'onedcell0', 'outputdb')
440 oned.process_onedgain(onedpath, GT)
441
442 basf2.B2INFO("Generating validation plots...")
443 val_path = os.path.join(job_path, 'validation0', '0', 'algorithm_output')
444
445 validators = [
446 ("rungain validation plots", rungain_validation),
447 ("wire gain validation plots", wiregain_validation),
448 ("cosine correction validation plots", cosgain_validation),
449 ("injection time validation plots", injection_validation),
450 ("momentum validation plots", mom_validation),
451 ("1D validation plots", oneDcell_validation),
452 ]
453
454 for exp, run_list in exp_run_dict.items():
455 for run in run_list:
456 suffix = f"e{exp}_r{run}"
457 for msg, func in validators:
458 basf2.B2INFO(f"Processing {msg} for {suffix}...")
459 func(val_path, suffix)
460
461 source_path = os.path.join(job_path, 'validation0', '0', 'algorithm_output', 'plots')
462 shutil.copy(source_path+f"/costh/dedxpeaks_vs_cos_{suffix}.pdf", 'plots/validation/')
463
464 shutil.copy(source_path+f"/mom/dedxpeaks_vs_mom_{suffix}.pdf", 'plots/validation/')
465
466
467if __name__ == "__main__":
468 run_validation(*sys.argv[1:])
Definition plot.py:1