11from pathlib
import Path
16import matplotlib.pyplot
as plt
17import matplotlib.ticker
as ticker
20from prompt
import ValidationSettings
24r.PyConfig.IgnoreCommandLineOptions =
True
28plt.style.use(
"belle2")
31settings = ValidationSettings(name=
"caf_svd_time",
38 '''job_path will be replaced with path/to/calibration_results
39 input_data_path will be replaced with path/to/data_path used
for calibration
40 e.g. /group/belle2/dataprod/Data/PromptSkim/
'''
42 collector_output_dir = Path(job_path) / 'SVDTimeValidation/0/collector_output/default/'
43 output_dir = Path(kwargs.get(
'output_dir',
'SVDTimeValidation_output'))
44 plots_per_run = output_dir /
'runs'
46 plots_per_run.mkdir(parents=
True, exist_ok=
True)
48 files = list(collector_output_dir.glob(
'**/CollectorOutput.root'))
50 agreements = {algo: {}
for algo
in vu.time_algorithms}
51 precisions = {algo: {}
for algo
in vu.time_algorithms}
52 discriminations = {algo: {}
for algo
in vu.time_algorithms}
53 shift_agreements = {algo: {}
for algo
in vu.time_algorithms}
54 entries_onTracks = {algo: {}
for algo
in vu.time_algorithms}
55 entries_eventT0 = {algo: {}
for algo
in vu.time_algorithms}
57 roc_U = {algo: {}
for algo
in vu.time_algorithms}
58 roc_V = {algo: {}
for algo
in vu.time_algorithms}
60 CollectorHistograms = vu.get_merged_collector_histograms(files)
64 for algo
in CollectorHistograms:
65 for exp
in CollectorHistograms[algo]:
66 nRun = len(CollectorHistograms[algo][exp])
68 if nRun > max_total_run:
70 total_length = max_total_run * len(vu.time_algorithms)
72 print(f
'Looping over {total_item} items')
74 vu.progress(0, total_item)
77 shift_histos_merged_over_ladder = {}
79 for algo
in CollectorHistograms:
80 shift_histos[algo] = {}
81 shift_histos_merged_over_ladder[algo] = {}
82 for exp
in CollectorHistograms[algo]:
83 for run
in CollectorHistograms[algo][exp]:
86 histos = vu.get_histos(CollectorHistograms[algo][exp][run])
89 print(f
'Skipping file algo {algo} exp {exp} run {run}')
94 entries_eventT0_ = histos[
'eventT0'].GetEntries()
95 if run
not in entries_eventT0[algo]
or entries_eventT0_ > entries_eventT0[algo][run]:
96 agreements[algo][run] = {key: vu.get_agreement(histos[
'eventT0'], h_diff)
97 for key, h_diff
in histos[
'diff'].items()}
98 precisions[algo][run] = {key: vu.get_precision(h_diff)
99 for key, h_diff
in histos[
'diff'].items()}
100 discriminations[algo][run] = {key: vu.get_roc_auc(histos[
'onTracks'][key], histos[
'offTracks'][key])
101 for key
in histos[
'onTracks']}
102 shift_agreements[algo][run] = {key: vu.get_shift_agreement(hShift)
103 for key, hShift
in histos[
'timeShifter'].items()}
104 entries_onTracks[algo][run] = {key: val.GetEntries()
for key, val
in histos[
'onTracks'].items()}
105 entries_eventT0[algo][run] = entries_eventT0_
107 for key, hShift
in histos[
'timeShifter'].items():
108 if key
in shift_histos[algo]:
109 shift_histos[algo][key].Add(hShift)
111 shift_histos[algo][key] = hShift.Clone()
112 shift_histos[algo][key].SetDirectory(0)
113 sensor_id = re.findall(
r'\d+', key) + [key[-1]]
114 keyGroup = f
'L{sensor_id[0]}S{sensor_id[2]}{sensor_id[3]}'
115 if keyGroup
in shift_histos_merged_over_ladder[algo]:
116 shift_histos_merged_over_ladder[algo][keyGroup].Add(hShift)
118 shift_histos_merged_over_ladder[algo][keyGroup] = hShift.Clone()
119 shift_histos_merged_over_ladder[algo][keyGroup].SetDirectory(0)
121 vu.make_combined_plot(
'*U', histos,
122 title=f
'exp {exp} run {run} U {algo}')
123 plt.savefig(plots_per_run / f
'{exp}_{run}_U_{algo}.pdf')
126 vu.make_combined_plot(
'*V', histos,
127 title=f
'exp {exp} run {run} V {algo}')
128 plt.savefig(plots_per_run / f
'{exp}_{run}_V_{algo}.pdf')
131 roc_U[algo][run] = vu.make_roc(vu.get_combined(histos[
'onTracks'],
'*U'),
132 vu.get_combined(histos[
'offTracks'],
'*U'))
133 roc_V[algo][run] = vu.make_roc(vu.get_combined(histos[
'onTracks'],
'*V'),
134 vu.get_combined(histos[
'offTracks'],
'*V'))
135 except AttributeError:
136 print(f
'Skipping file algo {algo} exp {exp} run {run}')
138 vu.progress(count + 1, total_item)
143 for algo, KeyHisto
in shift_histos.items():
144 c2 = r.TCanvas(
"c2",
"c2", 640, 480)
145 outPDF = f
"{output_dir}/shift_histograms_{algo}.pdf"
146 c2.Print(outPDF +
"[")
147 onePad = r.TPad(
"onePad",
"onePad", 0, 0, 1, 1)
148 onePad.SetMargin(0.1, 0.2, 0.1, 0.1)
152 hShiftHisto = vu.get_shift_plot(shift_histos_merged_over_ladder[algo])
153 hShiftHisto.Draw(
'COLZ')
154 c2.Print(outPDF,
"Title:" + hShiftHisto.GetName())
156 c1 = r.TCanvas(
"c1",
"c1", 640, 480)
157 topPad = r.TPad(
"topPad",
"topPad", 0, 0.5, 1, 1)
158 btmPad = r.TPad(
"btmPad",
"btmPad", 0, 0, 1, 0.5)
159 topPad.SetMargin(0.1, 0.1, 0, 0.149)
160 btmPad.SetMargin(0.1, 0.1, 0.303, 0)
166 for key, hShift
in KeyHisto.items():
168 for yn
in range(hShift.GetNbinsY()):
169 norm = (hShift.ProjectionX(
"tmp", yn + 1, yn + 1,
"")).GetMaximum()
172 for xn
in range(hShift.GetNbinsX()):
173 hShift.SetBinContent(xn + 1, yn + 1, hShift.GetBinContent(xn + 1, yn + 1) / norm)
180 c1.Print(outPDF,
"Title:" + hShift.GetName())
182 c1.Print(outPDF +
"]")
185 runs = sorted(agreements[vu.time_algorithms[0]])
186 dd[
'run'] = sum([[i]*len(vu.names_sides)
for i
in runs], [])
187 dd[
'name'] = vu.names_sides*len(runs)
188 dd[
'side'] = [i[-1]
for i
in dd[
'name']]
190 for algo
in vu.time_algorithms:
191 dd[f
'agreement_{algo}'] = [agreements[algo][run][side]
for run, side
in zip(dd[
'run'], dd[
'name'])]
192 dd[f
'precision_{algo}'] = [precisions[algo][run][side]
for run, side
in zip(dd[
'run'], dd[
'name'])]
193 dd[f
'discrimination_{algo}'] = [discriminations[algo][run][side]
for run, side
in zip(dd[
'run'], dd[
'name'])]
194 dd[f
'shift_agreement_{algo}'] = [shift_agreements[algo][run][side]
for run, side
in zip(dd[
'run'], dd[
'name'])]
195 dd[f
'entries_onTracks_{algo}'] = [entries_onTracks[algo][run][side]
for run, side
in zip(dd[
'run'], dd[
'name'])]
196 dd[f
'entries_eventT0_{algo}'] = [entries_eventT0[algo][run]
for run, side
in zip(dd[
'run'], dd[
'name'])]
201 plt.plot(*roc_U[
'CoG6'][run],
'k-', label=
'CoG6 U')
202 plt.plot(*roc_V[
'CoG6'][run],
'k:', label=
'CoG6 V')
203 plt.plot(*roc_U[
'CoG3'][run],
'b-', label=
'CoG3 U')
204 plt.plot(*roc_V[
'CoG3'][run],
'b:', label=
'CoG3 V')
205 plt.plot(*roc_U[
'ELS3'][run],
'r-', label=
'ELS3 U')
206 plt.plot(*roc_V[
'ELS3'][run],
'r:', label=
'ELS3 V')
207 plt.legend(loc=
'lower left')
208 plt.xlabel(
'sgn efficiency')
209 plt.ylabel(
'bkg rejection')
210 plt.title(f
'ROC run {run}')
214 plt.savefig(plots_per_run / f
'ROC_{run}.pdf')
217 df = pd.DataFrame(dd)
218 df.to_pickle(output_dir /
'df.pkl')
222 print(
'Making combined plots')
224 for algo
in vu.time_algorithms:
225 plt.figure(figsize=(6.4*max(2, total_length/30), 4.8*2))
226 ax = sns.violinplot(x=
'run', y=f
'agreement_{algo}', hue=
'side', data=df, split=
True)
228 ax.xaxis.set_minor_locator(ticker.NullLocator())
229 plt.axhline(0, color=
'black', linestyle=
'--')
230 plt.axhline(0.5, color=
'black', linestyle=
':')
231 plt.axhline(-0.5, color=
'black', linestyle=
':')
232 plt.setp(ax.get_xticklabels(), rotation=90)
234 plt.savefig(output_dir / f
'agreement_{algo}.pdf')
237 plt.figure(figsize=(6.4*max(2, total_length/30), 4.8*2))
238 ax = sns.violinplot(x=
'run', y=f
'precision_{algo}', hue=
'side', data=df, split=
True)
240 ax.xaxis.set_minor_locator(ticker.NullLocator())
241 plt.axhline(10, color=
'black', linestyle=
':')
242 plt.axhline(20, color=
'black', linestyle=
':')
243 plt.setp(ax.get_xticklabels(), rotation=90)
245 plt.savefig(output_dir / f
'precision_{algo}.pdf')
248 plt.figure(figsize=(6.4*max(2, total_length/30), 4.8*2))
249 ax = sns.violinplot(x=
'run', y=f
'discrimination_{algo}', hue=
'side', data=df, split=
True)
250 ax.set_ylim([0.5, 1])
251 ax.xaxis.set_minor_locator(ticker.NullLocator())
252 plt.axhline(0.8, color=
'black', linestyle=
':')
253 plt.axhline(0.9, color=
'black', linestyle=
':')
254 plt.setp(ax.get_xticklabels(), rotation=90)
256 plt.savefig(output_dir / f
'discrimination_{algo}.pdf')
259 plt.figure(figsize=(6.4*max(2, total_length/30), 4.8*2))
260 ax = sns.violinplot(x=
'run', y=f
'shift_agreement_{algo}', hue=
'side', data=df, split=
True, cut=0)
261 ax.xaxis.set_minor_locator(ticker.NullLocator())
262 ax.set_ylim([0.0, 3.5])
263 plt.axhline(0, color=
'black', linestyle=
'--')
264 plt.axhline(0.5, color=
'black', linestyle=
':')
265 plt.axhline(1.0, color=
'black', linestyle=
':')
266 plt.axhline(2.0, color=
'black', linestyle=
':')
267 plt.setp(ax.get_xticklabels(), rotation=90)
269 plt.savefig(output_dir / f
'shift_agreement_{algo}.pdf')
272 plt.figure(figsize=(6.4*max(2, total_length/30), 4.8*2))
273 ax = sns.violinplot(x=
'run', y=f
'entries_onTracks_{algo}', hue=
'side', data=df, split=
True, cut=0)
274 ax.xaxis.set_minor_locator(ticker.NullLocator())
275 plt.setp(ax.get_xticklabels(), rotation=90)
277 plt.savefig(output_dir / f
'entries_onTracks_{algo}.pdf')
280 plt.figure(figsize=(6.4*max(2, total_length/30), 4.8*2))
281 ax = sns.violinplot(x=
'run', y=f
'entries_eventT0_{algo}', hue=
'side', data=df, split=
True)
282 ax.xaxis.set_minor_locator(ticker.NullLocator())
283 plt.setp(ax.get_xticklabels(), rotation=90)
285 plt.savefig(output_dir / f
'entries_eventT0_{algo}.pdf')
289if __name__ ==
'__main__':
292 parser = argparse.ArgumentParser(description=__doc__,
293 formatter_class=argparse.RawTextHelpFormatter)
298 parser.add_argument(
'calibration_results_dir',
299 help=
'The directory that contains the collector outputs',
302 parser.add_argument(
'-o',
'--output_dir',
303 help=
'The directory where all the output will be saved',
304 default=
'SVDTimeValidation_output')
305 args = parser.parse_args()
307 run_validation(args.calibration_results_dir[0], output_dir=args.output_dir)