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
27 # Parse the database to get exp -> [runs] mapping
28 exp_run_dict = cg.parse_database(database_file, 'dbstore/CDCDedx1DCell')
29
30 # Process each exp-run pair
31 for exp, run_list in exp_run_dict.items():
32 for run in run_list:
33 print(f"[INFO] Processing exp={exp}, run={run}")
34 cal = CDCDedxValidationAlgorithm()
35 cal.setGlobalTag(gt)
36 prev_data = cal.getonedgain(exp, run)
37 cal.setGlobalTag("")
38 cal.setTestingPayload(database_file)
39 new_data = cal.getonedgain(exp, run)
40 cal.setTestingPayload("")
41
42 # Extract vectors
43 prev_inner = prev_data.inner1D if prev_data else []
44 new_inner = new_data.inner1D if new_data else []
45 prev_outer = prev_data.outer1D if prev_data else []
46 new_outer = new_data.outer1D if new_data else []
47 enta = new_data.Enta if new_data else []
48
49 # Convert to DataFrames
50 df_prev_in = pd.DataFrame([[x] for x in prev_inner], columns=['oned'])
51 df_new_in = pd.DataFrame([[x] for x in new_inner], columns=['oned'])
52 df_prev_out = pd.DataFrame([[x] for x in prev_outer], columns=['oned'])
53 df_new_out = pd.DataFrame([[x] for x in new_outer], columns=['oned'])
54 df_enta = pd.DataFrame([[x] for x in enta], columns=['enta'])
55
56 # Create a PDF file to save the plots
57 pdf_path = f'plots/constant/onedgain_e{exp}_r{run}.pdf'
58 with PdfPages(pdf_path) as pdf:
59 # Create 2x2 subplots
60 fig, ax = plt.subplots(2, 2, figsize=(20, 12))
61
62 # Top-left: Inner 1D
63 cg.hist(0.85, 1.12, xlabel="entrance angle", ylabel="Inner 1D constants", ax=ax[0, 0])
64 ax[0, 0].plot(df_enta['enta'], df_new_in['oned'], '-', rasterized=True, label="inner 1D (new)")
65 ax[0, 0].plot(df_enta['enta'], df_prev_in['oned'], '-', rasterized=True, label="inner 1D (prev)")
66 ax[0, 0].legend(fontsize=15)
67
68 # Top-right: Outer 1D
69 cg.hist(0.7, 1.12, xlabel="entrance angle", ylabel="Outer 1D constants", ax=ax[0, 1])
70 ax[0, 1].plot(df_enta['enta'], df_new_out['oned'], '-', rasterized=True, label="outer 1D (new)")
71 ax[0, 1].plot(df_enta['enta'], df_prev_out['oned'], '-', rasterized=True, label="outer 1D (prev)")
72 ax[0, 1].legend(fontsize=15)
73
74 # Bottom-left: Inner ratio
75 cg.hist(0.94, 1.06, xlabel="entrance angle", ylabel="Ratio (new/prev)", ax=ax[1, 0])
76 ratio_inner = df_new_in['oned'] / df_prev_in['oned']
77 ax[1, 0].plot(df_enta['enta'], ratio_inner, '-', rasterized=True, label="inner ratio")
78 ax[1, 0].legend(fontsize=15)
79
80 # Bottom-right: Outer ratio
81 cg.hist(0.96, 1.04, xlabel="entrance angle", ylabel="Ratio (new/prev)", ax=ax[1, 1])
82 ratio_outer = df_new_out['oned'] / df_prev_out['oned']
83 ax[1, 1].plot(df_enta['enta'], ratio_outer, '-', rasterized=True, label="outer ratio")
84 ax[1, 1].legend(fontsize=15)
85
86 fig.suptitle(f"OneD Gain Calibration - Experiment {exp}", fontsize=20)
87 plt.tight_layout()
88 pdf.savefig(fig)
89 plt.close()
Definition plot.py:1