9"""Implements wirgain correction"""
13import matplotlib.pyplot
as plt
15from ROOT.Belle2
import CDCDedxValidationAlgorithm
16import process_cosgain
as cg
17from matplotlib.backends.backend_pdf
import PdfPages
18ROOT.gROOT.SetBatch(
True)
23 il = df.loc[(df.index < 1280) & (df[
'wiregain'] != 0)].copy()
26 ol = df.loc[(df.index >= 1280) & (df[
'wiregain'] != 0)].copy()
32 il = df[df.index < 8].copy()
33 ol = df[df.index >= 8].copy()
37def process_wiregain_pair(cal, exp, run, database_file, gt):
38 """Process wiregain for a given exp-run pair."""
41 prev_data = cal.getwiregain(exp, run)
43 cal.setTestingPayload(database_file)
44 new_data = cal.getwiregain(exp, run)
45 cal.setTestingPayload(
"")
48 prev_wg = prev_data.wiregain
if prev_data
else []
49 new_wg = new_data.wiregain
if new_data
else []
52 if not prev_wg
or not new_wg:
53 print(f
"[WARNING] Empty wiregain data for exp={exp}, run={run}. Skipping.")
57 df_prev = pd.DataFrame([[x]
for x
in prev_wg], columns=[
'wiregain'])
58 df_new = pd.DataFrame([[x]
for x
in new_wg], columns=[
'wiregain'])
61 il_prev, ol_prev = remove_nowg(df_prev)
62 il_new, ol_new = remove_nowg(df_new)
64 return il_prev, ol_prev, il_new, ol_new
67def process_layermean(cal, exp, run, database_file, gt):
71 prev_data = cal.getwiregain(exp, run)
73 cal.setTestingPayload(database_file)
74 new_data = cal.getwiregain(exp, run)
75 cal.setTestingPayload(
"")
77 df_prev_lm = pd.DataFrame([[x]
for x
in prev_data.layermean], columns=[
'layergain'])
78 df_new_lm = pd.DataFrame([[x]
for x
in new_data.layermean], columns=[
'layergain'])
80 il_lm_prev, ol_lm_prev = sep_inout(df_prev_lm)
81 il_lm_new, ol_lm_new = sep_inout(df_new_lm)
83 il_ratio = (il_lm_new[
'layergain'] / il_lm_prev[
'layergain']).dropna()
84 ol_ratio = (ol_lm_new[
'layergain'] / ol_lm_prev[
'layergain']).dropna()
86 return il_ratio, ol_ratio
89def plot_wiregain_all(il_prev, ol_prev, il_new, ol_new, exp, run, pdf, il_lay, ol_lay):
90 """Plot overlay, ratio and difference into a single PDF page."""
92 fig, ax = plt.subplots(2, 2, figsize=(20, 12))
95 ax[0, 0].set_title(
"Inner previous Wiregain")
96 cg.hist(0., 4, xlabel=
"#wire", ylabel=
"wg constant", space=100, ax=ax[0, 0])
97 ax[0, 0].
plot(il_prev[
'wiregain'],
'o', label=
"Inner (prev)", markersize=5, alpha=0.7)
98 ax[0, 0].legend(fontsize=12)
100 ax[0, 1].set_title(
"Outer previous Wiregain")
101 cg.hist(0., 3, xlabel=
"#wire", ylabel=
"wg constant", space=1000, ax=ax[0, 1])
102 ax[0, 1].
plot(ol_prev[
'wiregain'],
'^', label=
"Outer (prev)", markersize=5, alpha=0.7)
103 ax[0, 1].legend(fontsize=12)
105 ax[1, 0].set_title(
"Inner new Wiregain")
106 cg.hist(0., 4, xlabel=
"#wire", ylabel=
"wg constant", space=100, ax=ax[1, 0])
107 ax[1, 0].
plot(il_new[
'wiregain'],
's', label=
"Inner (new)", markersize=5, alpha=0.7)
108 ax[1, 0].legend(fontsize=12)
110 ax[1, 1].set_title(
"Outer new Wiregain")
111 cg.hist(0., 3, xlabel=
"#wire", ylabel=
"wg constant", space=1000, ax=ax[1, 1])
112 ax[1, 1].
plot(ol_new[
'wiregain'],
'D', label=
"Outer (new)", markersize=5, alpha=0.7)
113 ax[1, 1].legend(fontsize=12)
115 fig.suptitle(f
"WireGain Calibration - Experiment {exp}", fontsize=20)
120 fig, ax = plt.subplots(2, 2, figsize=(20, 12))
123 ax[0, 0].set_title(
"Inner Wiregain Ratio")
124 il_r = (il_new[
'wiregain'] / il_prev[
'wiregain']).dropna()
125 ol_r = (ol_new[
'wiregain'] / ol_prev[
'wiregain']).dropna()
126 cg.hist(0.7, 1.5, xlabel=
"#wire", ylabel=
"wiregain ratio", space=100, ax=ax[0, 0])
127 ax[0, 0].
plot(il_r,
'*', rasterized=
True, label=
'Inner Layer')
128 ax[0, 0].legend(fontsize=12)
130 ax[0, 1].set_title(
"Outer Wiregain Ratio")
131 il_r = (il_new[
'wiregain'] / il_prev[
'wiregain']).dropna()
132 ol_r = (ol_new[
'wiregain'] / ol_prev[
'wiregain']).dropna()
133 cg.hist(0.7, 1.5, xlabel=
"#wire", ylabel=
"wiregain ratio", space=1000, ax=ax[0, 1])
134 ax[0, 1].
plot(ol_r,
'*', rasterized=
True, label=
'Outer Layer')
135 ax[0, 1].legend(fontsize=12)
138 ax[1, 0].set_title(
"Inner Wiregain Ratio (zoom)")
139 cg.hist(0.92, 1.05, xlabel=
"#wire", ylabel=
"wiregain ratio", space=100, ax=ax[1, 0])
140 ax[1, 0].
plot(il_r,
'*', rasterized=
True, label=
'Inner Layer')
141 ax[1, 0].legend(fontsize=12)
143 ax[1, 1].set_title(
"Outer Wiregain Ratio (zoom)")
144 cg.hist(0.92, 1.05, xlabel=
"#wire", ylabel=
"wiregain ratio", space=1000, ax=ax[1, 1])
145 ax[1, 1].
plot(ol_r,
'*', rasterized=
True, label=
'Outer Layer')
146 ax[1, 1].legend(fontsize=12)
152 fig, ax = plt.subplots(1, 2, figsize=(20, 6))
155 ax[0].set_title(
"Wiregain Ratio histo")
156 cg.hist(x_min=0.7, x_max=1.5, xlabel=
r"$\Delta$ wiregains", ylabel=
"Normalized count", fs1=10, fs2=6, ax=ax[0])
158 scale1 = 1 / il_r.shape[0]
if il_r.shape[0] > 0
else 1
159 scale2 = 1 / ol_r.shape[0]
if ol_r.shape[0] > 0
else 1
161 counts1, bins1 = np.histogram(il_r, bins=200)
162 counts2, bins2 = np.histogram(ol_r, bins=200)
164 ax[0].hist(bins1[:-1], bins=200, histtype=
'step', linewidth=2.5,
165 weights=counts1 * scale1, label=
'Inner layer')
166 ax[0].hist(bins2[:-1], bins=200, histtype=
'step', linewidth=2.5,
167 weights=counts2 * scale2, label=
'Outer Layer')
169 ax[0].legend(fontsize=12)
172 ax[1].set_title(
"Layer Gain Ratio")
173 cg.hist(0.9, 1.1, xlabel=
"#layer", ylabel=
"layer average ratio", space=5, fs1=10, fs2=6, ax=ax[1])
174 ax[1].
plot(il_lay,
'*', label=
"Inner Layers")
175 ax[1].
plot(ol_lay,
'*', label=
"Outer Layers")
176 ax[1].legend(fontsize=12)
184 """Main function to process wiregain data and generate plots."""
186 os.makedirs(
'plots/constant', exist_ok=
True)
187 database_file = f
'{wgpath}/database.txt'
189 exp_run_dict = cg.parse_database(database_file,
"dbstore/CDCDedxWireGain")
191 for exp, run_list
in exp_run_dict.items():
193 print(f
"[INFO] Processing exp={exp}, run={run}")
194 cal = CDCDedxValidationAlgorithm()
195 il_prev, ol_prev, il_new, ol_new = process_wiregain_pair(cal, exp, run, database_file, gt)
197 if il_prev
is None or ol_prev
is None or il_new
is None or ol_new
is None:
200 il_lay, ol_lay = process_layermean(cal, exp, run, database_file, gt)
202 pdf_path = f
'plots/constant/wiregain_e{exp}_r{run}.pdf'
203 with PdfPages(pdf_path)
as pdf:
204 plot_wiregain_all(il_prev, ol_prev, il_new, ol_new, exp, run, pdf, il_lay, ol_lay)