12 from pathlib
import Path
18 import matplotlib.pyplot
as plt
19 import matplotlib.ticker
as ticker
21 from prompt
import ValidationSettings
25 r.PyConfig.IgnoreCommandLineOptions =
True
28 plt.style.use(
"belle2")
31 settings = ValidationSettings(name=
"caf_svd_time",
37 def progress(count, total):
39 filled_len = int(round(bar_len * count / total))
40 percents = round(100 * count / total, 1)
41 bar =
'=' * filled_len +
'-' * (bar_len - filled_len)
42 sys.stdout.write(f
'[{bar}] {percents}%\r')
47 '''job_path will be replaced with path/to/calibration_results
48 input_data_path will be replaced with path/to/data_path used for calibration
49 e.g. /group/belle2/dataprod/Data/PromptSkim/'''
51 collector_output_dir = Path(job_path) /
'SVDTimeValidation/0/collector_output/default/'
52 output_dir = Path(kwargs.get(
'output_dir',
'SVDTimeValidation_output'))
53 plots_per_run = output_dir /
'runs'
55 plots_per_run.mkdir(parents=
True, exist_ok=
True)
57 files = list(collector_output_dir.glob(
'**/CollectorOutput.root'))
59 agreements = {algo: {}
for algo
in vu.time_algorithms}
60 precisions = {algo: {}
for algo
in vu.time_algorithms}
61 discriminations = {algo: {}
for algo
in vu.time_algorithms}
62 entries_onTracks = {algo: {}
for algo
in vu.time_algorithms}
63 entries_eventT0 = {algo: {}
for algo
in vu.time_algorithms}
65 roc_U = {algo: {}
for algo
in vu.time_algorithms}
66 roc_V = {algo: {}
for algo
in vu.time_algorithms}
68 num_files = len(files)
69 print(f
'Looping over {num_files} files')
70 progress(0, num_files)
71 for count, in_file_name
in enumerate(files):
73 in_file = r.TFile(str(in_file_name))
75 for algo
in vu.time_algorithms:
77 histos, exp, run = vu.get_histos(in_file, algo)
80 print(f
'Skipping file {in_file_name} for {algo}')
85 entries_eventT0_ = histos[
'eventT0'].GetEntries()
86 if run
not in entries_eventT0[algo]
or entries_eventT0_ > entries_eventT0[algo][run]:
87 agreements[algo][run] = {key: vu.get_agreament(histos[
'eventT0'], h_diff)
88 for key, h_diff
in histos[
'diff'].items()}
89 precisions[algo][run] = {key: vu.get_precision(h_diff)
90 for key, h_diff
in histos[
'diff'].items()}
91 discriminations[algo][run] = {key: vu.get_roc_auc(histos[
'onTracks'][key], histos[
'offTracks'][key])
92 for key
in histos[
'onTracks']}
93 entries_onTracks[algo][run] = {key: val.GetEntries()
for key, val
in histos[
'onTracks'].items()}
94 entries_eventT0[algo][run] = entries_eventT0_
96 vu.make_combined_plot(
'*U', histos,
97 title=f
'exp {exp} run {run} U {algo}')
98 plt.savefig(plots_per_run / f
'{exp}_{run}_U_{algo}.pdf')
101 vu.make_combined_plot(
'*V', histos,
102 title=f
'exp {exp} run {run} V {algo}')
103 plt.savefig(plots_per_run / f
'{exp}_{run}_V_{algo}.pdf')
106 roc_U[algo][run] = vu.make_roc(vu.get_combined(histos[
'onTracks'],
'*U'),
107 vu.get_combined(histos[
'offTracks'],
'*U'))
108 roc_V[algo][run] = vu.make_roc(vu.get_combined(histos[
'onTracks'],
'*V'),
109 vu.get_combined(histos[
'offTracks'],
'*V'))
110 except AttributeError:
111 print(f
'Skipping file {in_file_name} for {algo}')
117 progress(count+1, num_files)
122 runs = sorted(agreements[vu.time_algorithms[0]])
123 dd[
'run'] = sum([[i]*len(vu.names_sides)
for i
in runs], [])
124 dd[
'name'] = vu.names_sides*len(runs)
125 dd[
'side'] = [i[-1]
for i
in dd[
'name']]
127 for algo
in vu.time_algorithms:
128 dd[f
'agreement_{algo}'] = [agreements[algo][run][side]
for run, side
in zip(dd[
'run'], dd[
'name'])]
129 dd[f
'precision_{algo}'] = [precisions[algo][run][side]
for run, side
in zip(dd[
'run'], dd[
'name'])]
130 dd[f
'discrimination_{algo}'] = [discriminations[algo][run][side]
for run, side
in zip(dd[
'run'], dd[
'name'])]
131 dd[f
'entries_onTracks_{algo}'] = [entries_onTracks[algo][run][side]
for run, side
in zip(dd[
'run'], dd[
'name'])]
132 dd[f
'entries_eventT0_{algo}'] = [entries_eventT0[algo][run]
for run, side
in zip(dd[
'run'], dd[
'name'])]
137 plt.plot(*roc_U[
'CoG6'][run],
'k-', label=
'CoG6 U')
138 plt.plot(*roc_V[
'CoG6'][run],
'k:', label=
'CoG6 V')
139 plt.plot(*roc_U[
'CoG3'][run],
'b-', label=
'CoG3 U')
140 plt.plot(*roc_V[
'CoG3'][run],
'b:', label=
'CoG3 V')
141 plt.plot(*roc_U[
'ELS3'][run],
'r-', label=
'ELS3 U')
142 plt.plot(*roc_V[
'ELS3'][run],
'r:', label=
'ELS3 V')
143 plt.legend(loc=
'lower left')
144 plt.xlabel(
'sgn efficiency')
145 plt.ylabel(
'bkg rejection')
146 plt.title(f
'ROC run {run}')
150 plt.savefig(plots_per_run / f
'ROC_{run}.pdf')
153 df = pd.DataFrame(dd)
154 df.to_pickle(output_dir /
'df.pkl')
158 print(
'Making combined plots')
160 for algo
in vu.time_algorithms:
161 plt.figure(figsize=(6.4*max(2, num_files/30), 4.8*2))
162 ax = sns.violinplot(x=
'run', y=f
'agreement_{algo}', hue=
'side', data=df, split=
True)
164 ax.xaxis.set_minor_locator(ticker.NullLocator())
165 plt.axhline(0, color=
'black', linestyle=
'--')
166 plt.axhline(0.5, color=
'black', linestyle=
':')
167 plt.axhline(-0.5, color=
'black', linestyle=
':')
168 plt.setp(ax.get_xticklabels(), rotation=90)
170 plt.savefig(output_dir / f
'agreement_{algo}.pdf')
173 plt.figure(figsize=(6.4*max(2, num_files/30), 4.8*2))
174 ax = sns.violinplot(x=
'run', y=f
'precision_{algo}', hue=
'side', data=df, split=
True)
176 ax.xaxis.set_minor_locator(ticker.NullLocator())
177 plt.axhline(10, color=
'black', linestyle=
':')
178 plt.axhline(20, color=
'black', linestyle=
':')
179 plt.setp(ax.get_xticklabels(), rotation=90)
181 plt.savefig(output_dir / f
'precision_{algo}.pdf')
184 plt.figure(figsize=(6.4*max(2, num_files/30), 4.8*2))
185 ax = sns.violinplot(x=
'run', y=f
'discrimination_{algo}', hue=
'side', data=df, split=
True)
186 ax.set_ylim([0.5, 1])
187 ax.xaxis.set_minor_locator(ticker.NullLocator())
188 plt.axhline(0.8, color=
'black', linestyle=
':')
189 plt.axhline(0.9, color=
'black', linestyle=
':')
190 plt.setp(ax.get_xticklabels(), rotation=90)
192 plt.savefig(output_dir / f
'discrimination_{algo}.pdf')
195 plt.figure(figsize=(6.4*max(2, num_files/30), 4.8*2))
196 ax = sns.violinplot(x=
'run', y=f
'entries_onTracks_{algo}', hue=
'side', data=df, split=
True, cut=0)
197 ax.xaxis.set_minor_locator(ticker.NullLocator())
198 plt.setp(ax.get_xticklabels(), rotation=90)
200 plt.savefig(output_dir / f
'entries_onTracks_{algo}.pdf')
203 plt.figure(figsize=(6.4*max(2, num_files/30), 4.8*2))
204 ax = sns.violinplot(x=
'run', y=f
'entries_eventT0_{algo}', hue=
'side', data=df, split=
True)
205 ax.xaxis.set_minor_locator(ticker.NullLocator())
206 plt.setp(ax.get_xticklabels(), rotation=90)
208 plt.savefig(output_dir / f
'entries_eventT0_{algo}.pdf')
212 if __name__ ==
'__main__':
215 parser = argparse.ArgumentParser(description=__doc__,
216 formatter_class=argparse.RawTextHelpFormatter)
221 parser.add_argument(
'calibration_results_dir',
222 help=
'The directory that contains the collector outputs',
225 parser.add_argument(
'-o',
'--output_dir',
226 help=
'The directory where all the output will be saved',
227 default=
'SVDTimeValidation_output')
228 args = parser.parse_args()
230 run_validation(args.calibration_results_dir[0], output_dir=args.output_dir)