11from prompt
import ValidationSettings
17import matplotlib.pyplot
as plt
18from matplotlib.backends.backend_pdf
import PdfPages
21settings = ValidationSettings(name=
'KLM alignment',
23 download_files=[
'stdout'],
27def get_result(job_path, tmp_dir):
28 from ROOT
import Belle2
29 from ROOT.Belle2
import KLMCalibrationChecker
31 database_file = f
'{job_path}/outputdb/database.txt'
33 with open(database_file)
as f:
35 fields = line.split(
' ')
36 if (fields[0] ==
'dbstore/BKLMAlignment'):
37 iov = fields[2].split(
',')
38 exp_run_list.append([int(iov[0]), int(iov[1])])
40 for exp_run
in exp_run_list:
43 checker = KLMCalibrationChecker()
44 checker.setExperimentRun(exp, run)
45 checker.setTestingPayload(database_file)
46 basf2.B2INFO(f
'Creating alignment results tree for experiment {exp}, run {run}.')
47 checker.setAlignmentResultsFile(tmp_dir+f
'/alignment_{exp}_{run}.root')
48 checker.checkAlignment()
53def get_residuals(data_path, data_path_prev):
55 data_prev = uproot.open(data_path_prev)
56 data = uproot.open(data_path)
57 BKLMModule_prev = data_prev[data_prev.keys()[0]]
58 EKLMModule_prev = data_prev[data_prev.keys()[1]]
59 BKLMModule = data[data.keys()[0]]
60 EKLMModule = data[data.keys()[1]]
61 EKLM = [EKLMModule_prev.arrays(library=
'pd'), EKLMModule.arrays(library=
'pd')]
62 BKLM = [BKLMModule_prev.arrays(library=
'pd'), BKLMModule.arrays(library=
'pd')]
65 EKLM_values = np.zeros((2, len(EKLM[0][
'section'].unique()),
66 len(EKLM[0][
'sector'].unique()),
67 3, len(EKLM[0][
'layer'].unique())))
68 EKLM_errors = np.zeros((2, len(EKLM[0][
'section'].unique()),
69 len(EKLM[0][
'sector'].unique()),
70 3, len(EKLM[0][
'layer'].unique())))
72 BKLM_values = np.zeros((2, len(BKLM[0][
'section'].unique()),
73 len(BKLM[0][
'sector'].unique()),
74 3, len(BKLM[0][
'layer'].unique())))
75 BKLM_errors = np.zeros((2, len(BKLM[0][
'section'].unique()),
76 len(BKLM[0][
'sector'].unique()),
77 3, len(BKLM[0][
'layer'].unique())))
79 pars = {1: 1, 2: 2, 3: 6}
82 for section
in EKLM[i][
'section'].unique():
83 for sector
in EKLM[i][
'sector'].unique():
84 for param
in range(0, len(pars)):
86 (EKLM[i][
'section'] == section) & (
87 EKLM[i][
'sector'] == sector) & (
88 EKLM[i][
'param'] == list(
89 pars.values())[param]))
91 EKLM_values[i][section-1][sector-1][list(pars.keys())[param] -
92 1] = np.append(np.array(EKLM[i][req][
'value']), [0, 0])
93 EKLM_errors[i][section-1][sector-1][list(pars.keys())[param] -
94 1] = np.append(np.array(EKLM[i][req][
'error']), [0, 0])
96 EKLM_values[i][section-1][sector-1][list(pars.keys())[param]-1] = np.array(EKLM[i][req][
'value'])
97 EKLM_errors[i][section-1][sector-1][list(pars.keys())[param]-1] = np.array(EKLM[i][req][
'error'])
100 for section
in BKLM[i][
'section'].unique():
101 for sector
in BKLM[i][
'sector'].unique():
102 for param
in range(0, len(pars)):
104 (BKLM[i][
'section'] == section) & (
105 BKLM[i][
'sector'] == sector) & (
106 BKLM[i][
'param'] == list(
107 pars.values())[param]))
108 BKLM_values[i][section][sector-1][list(pars.keys())[param]-1] = np.array(BKLM[i][req][
'value'])
109 BKLM_errors[i][section][sector-1][list(pars.keys())[param]-1] = np.array(BKLM[i][req][
'error'])
112 EKLM_res_values = np.zeros((len(EKLM[0][
'section'].unique()),
113 len(EKLM[0][
'sector'].unique()),
114 3, len(EKLM[0][
'layer'].unique())))
115 EKLM_res_errors = np.zeros((len(EKLM[0][
'section'].unique()),
116 len(EKLM[0][
'sector'].unique()),
117 3, len(EKLM[0][
'layer'].unique())))
119 BKLM_res_values = np.zeros((len(BKLM[0][
'section'].unique()),
120 len(BKLM[0][
'sector'].unique()),
121 3, len(BKLM[0][
'layer'].unique())))
122 BKLM_res_errors = np.zeros((len(BKLM[0][
'section'].unique()),
123 len(BKLM[0][
'sector'].unique()),
124 3, len(BKLM[0][
'layer'].unique())))
126 for section
in range(0, EKLM_values[0].shape[0]):
127 for sector
in range(0, EKLM_values[0].shape[1]):
128 for param
in range(0, EKLM_values[0].shape[2]):
129 EKLM_res_values[section][sector][param] = EKLM_values[1][section][sector][param] - \
130 EKLM_values[0][section][sector][param]
131 EKLM_res_errors[section][sector][param] = np.sqrt(
132 EKLM_errors[1][section][sector][param]**2 +
133 EKLM_errors[0][section][sector][param]**2)
135 for section
in range(0, BKLM_values[0].shape[0]):
136 for sector
in range(0, BKLM_values[0].shape[1]):
137 for param
in range(0, BKLM_values[0].shape[2]):
138 BKLM_res_values[section][sector][param] = BKLM_values[1][section][sector][param] - \
139 BKLM_values[0][section][sector][param]
140 BKLM_res_errors[section][sector][param] = np.sqrt(
141 BKLM_errors[1][section][sector][param]**2 +
142 BKLM_errors[0][section][sector][param]**2)
144 EKLM_chi2 = np.zeros((len(EKLM[0][
'section'].unique()),
145 len(EKLM[0][
'sector'].unique()),
146 3, len(EKLM[0][
'layer'].unique())))
148 BKLM_chi2 = np.zeros((len(BKLM[0][
'section'].unique()),
149 len(BKLM[0][
'sector'].unique()),
150 3, len(BKLM[0][
'layer'].unique())))
152 for section
in range(0, EKLM_res_values.shape[0]):
153 for sector
in range(0, EKLM_res_values.shape[1]):
154 for param
in range(0, EKLM_res_values.shape[2]):
155 for layer
in range(0, EKLM_res_values.shape[3]):
156 if ((EKLM_res_values[section][sector][param][layer] == 0) |
157 (EKLM_res_errors[section][sector][param][layer] == 0)):
158 EKLM_chi2[section][sector][param][layer] = 0
160 EKLM_chi2[section][sector][param][layer] = (
161 EKLM_res_values[section][sector][param][layer]**2)/(EKLM_res_errors[section][sector][param][layer]**2)
163 for section
in range(0, BKLM_res_values.shape[0]):
164 for sector
in range(0, BKLM_res_values.shape[1]):
165 for param
in range(0, BKLM_res_values.shape[2]):
166 for layer
in range(0, BKLM_res_values.shape[3]):
167 if ((BKLM_res_values[section][sector][param][layer] == 0) |
168 (BKLM_res_errors[section][sector][param][layer] == 0)):
169 BKLM_chi2[section][sector][param][layer] = 0
171 BKLM_chi2[section][sector][param][layer] = (
172 BKLM_res_values[section][sector][param][layer]**2)/(BKLM_res_errors[section][sector][param][layer]**2)
174 return [EKLM_res_values, EKLM_res_errors, EKLM_chi2, BKLM_res_values, BKLM_res_errors, BKLM_chi2]
177def draw_EKLM_pics(EKLM_values, EKLM_errors, EKLM_chi2, pdfPages):
179 plt.rcParams.update({
181 'figure.figsize': (11, 10),
183 'grid.linestyle':
'-',
185 'lines.markersize': 5.0,
186 'xtick.minor.visible':
True,
187 'xtick.direction':
'in',
188 'xtick.major.size': 20.0,
189 'xtick.minor.size': 10.0,
191 'ytick.minor.visible':
True,
192 'ytick.direction':
'in',
193 'ytick.major.size': 20.0,
194 'ytick.minor.size': 10.0,
196 'errorbar.capsize': 0.0,
198 param_meaning = {0:
'x', 1:
'y', 2:
r'$\alpha$'}
199 section_meaning = {0:
'b', 1:
'f'}
200 layers = {
'EKLM': np.arange(1, 15, 1),
'BKLM': np.arange(1, 16, 1)}
201 layers_err = {
'EKLM': np.full(14, 0.5),
'BKLM': np.full(15, 0.5)}
202 for section
in [0, 1]:
203 fig, axs = plt.subplots(4, 3, figsize=(20, 20))
204 for i
in range(0, 12):
207 plt.sca(axs[sector][param])
210 xerr=layers_err[
'EKLM'],
211 y=EKLM_values[section][sector][param],
212 yerr=EKLM_errors[section][sector][param],
218 section_meaning[section] +
226 axis=3)[section][sector][param],
228 plt.hlines(0, 0, 14, color=
'red')
230 plt.ylim(-0.02, 0.02)
231 plt.ylabel(
r'$\Delta$'+param_meaning[param]+
' rad')
234 plt.ylabel(
r'$\Delta$'+param_meaning[param]+
' cm')
236 axs[sector][param].yaxis.set_label_coords(-0.1, 0.5)
239 plt.savefig(pdfPages, format=
'pdf')
243def draw_BKLM_pics(BKLM_values, BKLM_errors, BKLM_chi2, pdfPages):
245 plt.rcParams.update({
247 'figure.figsize': (11, 10),
249 'grid.linestyle':
'-',
251 'lines.markersize': 5.0,
252 'xtick.minor.visible':
True,
253 'xtick.direction':
'in',
254 'xtick.major.size': 20.0,
255 'xtick.minor.size': 10.0,
257 'ytick.minor.visible':
True,
258 'ytick.direction':
'in',
259 'ytick.major.size': 20.0,
260 'ytick.minor.size': 10.0,
262 'errorbar.capsize': 0.0,
264 param_meaning = {0:
'x', 1:
'y', 2:
r'$\alpha$'}
265 section_meaning = {0:
'b', 1:
'f'}
266 layers = {
'EKLM': np.arange(1, 15, 1),
'BKLM': np.arange(1, 16, 1)}
267 layers_err = {
'EKLM': np.full(14, 0.5),
'BKLM': np.full(15, 0.5)}
268 for section
in [0, 1]:
269 for sector_shift
in [0, 4]:
270 fig, axs = plt.subplots(4, 3, figsize=(20, 20))
271 for i
in range(0, 12):
272 sector = i//3+sector_shift
274 plt.sca(axs[sector-sector_shift][param])
277 xerr=layers_err[
'BKLM'],
278 y=BKLM_values[section][sector][param],
279 yerr=BKLM_errors[section][sector][param],
285 section_meaning[section] +
293 axis=3)[section][sector][param],
295 plt.hlines(0, 0, 15, color=
'red')
297 plt.ylim(-0.02, 0.02)
298 plt.ylabel(
r'$\Delta$'+param_meaning[param]+
' rad')
301 plt.ylabel(
r'$\Delta$'+param_meaning[param]+
' cm')
303 axs[sector-sector_shift][param].yaxis.set_label_coords(-0.1, 0.5)
306 plt.savefig(pdfPages, format=
'pdf')
313 The script compares the most recent alignment result with the previous results by calculating the residuals.
315 tmp_work_dir = os.path.join(os.getcwd(), 'tmp_work')
316 tmp_plot_dir = os.path.join(os.getcwd(),
'tmp_plot')
317 if not os.path.exists(tmp_work_dir):
318 os.makedirs(tmp_work_dir)
319 if not os.path.exists(tmp_plot_dir):
320 os.makedirs(tmp_plot_dir)
323 exp_run_list = get_result(job_path, tmp_work_dir)
324 exp_run_list_prev = get_result(job_path_prev, tmp_work_dir)
326 sorted_exp_run_list = sorted(exp_run_list + exp_run_list_prev)
328 for i
in range(0, len(sorted_exp_run_list)-1):
329 exp_prev = sorted_exp_run_list[i][0]
330 run_prev = sorted_exp_run_list[i][1]
331 exp = sorted_exp_run_list[i+1][0]
332 run = sorted_exp_run_list[i+1][1]
333 data_path = tmp_work_dir+f
'/alignment_{exp_prev}_{run_prev}.root'
334 data_path_prev = tmp_work_dir+f
'/alignment_{exp}_{run}.root'
335 EKLM_values, EKLM_errors, EKLM_chi2, BKLM_values, BKLM_errors, BKLM_chi2 = get_residuals(data_path, data_path_prev)
336 pdfPages = PdfPages(tmp_plot_dir+
'/e'+str(exp_prev)+
'r'+str(run_prev)+
'_e'+str(exp)+
'r'+str(run)+
'.pdf')
337 draw_EKLM_pics(EKLM_values, EKLM_errors, EKLM_chi2, pdfPages)
338 draw_BKLM_pics(BKLM_values, BKLM_errors, BKLM_chi2, pdfPages)
342if __name__ ==
"__main__":