Belle II Software development
process_onedcell.py
1
8
9"""Implements 1D correction"""
10
11import pandas as pd
12import matplotlib.pyplot as plt
13from matplotlib.backends.backend_pdf import PdfPages
14import ROOT
15from ROOT.Belle2 import CDCDedxValidationAlgorithm
16import process_cosgain as cg
17ROOT.gROOT.SetBatch(True)
18
19
20def process_onedgain(onedpath, gt):
21 """Main function to process 1D cell gain data and generate plots."""
22 import os
23 os.makedirs('plots/constant', exist_ok=True)
24
25 database_file = f'{onedpath}/database.txt'
26 exp_run_dict = cg.parse_database(database_file, 'dbstore/CDCDedx1DCell')
27
28 group_labels = ["SL0", "SL1", "SL2-8"]
29
30 for exp, run_list in exp_run_dict.items():
31 for run in run_list:
32 print(f"[INFO] Processing exp={exp}, run={run}")
33
34 cal = CDCDedxValidationAlgorithm()
35 cal.setGlobalTag(gt)
36 prev_data = cal.getonedgain(exp, run)
37
38 cal.setGlobalTag("")
39 cal.setTestingPayload(database_file)
40 new_data = cal.getonedgain(exp, run)
41 cal.setTestingPayload("")
42
43 prev_corr = prev_data.oneDcorr if prev_data else [[], [], []]
44 new_corr = new_data.oneDcorr if new_data else [[], [], []]
45 enta = new_data.enta if new_data else []
46
47 enta = list(enta) if enta is not None else []
48 df_enta = pd.DataFrame(enta, columns=['enta'])
49
50 pdf_path = f'plots/constant/onedgain_e{exp}_r{run}.pdf'
51 with PdfPages(pdf_path) as pdf:
52 fig, ax = plt.subplots(3, 2, figsize=(18, 16))
53
54 for igroup, glabel in enumerate(group_labels):
55 prev_vals = prev_corr[igroup] if len(prev_corr) > igroup else []
56 new_vals = new_corr[igroup] if len(new_corr) > igroup else []
57
58 df_prev = pd.DataFrame([[x] for x in prev_vals], columns=['oned'])
59 df_new = pd.DataFrame([[x] for x in new_vals], columns=['oned'])
60
61 # constants
62 cg.hist(0.7, 1.5, xlabel="entrance angle", ylabel=f"{glabel} constants", ax=ax[igroup, 0])
63 ax[igroup, 0].plot(df_enta['enta'], df_new['oned'], '-', rasterized=True, label=f"{glabel} (new)")
64 ax[igroup, 0].plot(df_enta['enta'], df_prev['oned'], '-', rasterized=True, label=f"{glabel} (prev)")
65 ax[igroup, 0].legend(fontsize=12)
66
67 # ratio
68 cg.hist(0.9, 1.4, xlabel="entrance angle", ylabel="Ratio (new/prev)", ax=ax[igroup, 1])
69 if len(df_prev) > 0 and len(df_new) > 0:
70 ratio = df_new['oned'] / df_prev['oned']
71 ax[igroup, 1].plot(df_enta['enta'], ratio, '-', rasterized=True, label=f"{glabel} ratio")
72 ax[igroup, 1].legend(fontsize=12)
73
74 fig.suptitle(f"OneD Gain Calibration - Experiment {exp}, Run {run}", fontsize=20)
75 plt.tight_layout()
76 pdf.savefig(fig)
77 plt.close(fig)
Definition plot.py:1