eff_plots.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. #!/usr/bin/env python
  2. from itertools import product
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. from uproot import open as root_open
  6. from matplottery.utils import Hist1D, Hist2D, to_html_table
  7. from matplottery.plotter import set_defaults, plot_2d
  8. import matplotboard as mpb
  9. matching_cuts = {
  10. 'new-extra-narrow': [
  11. dict(
  12. dPhiMaxHighEt=0.025,
  13. dPhiMaxHighEtThres=20.0,
  14. dPhiMaxLowEtGrad=-0.002,
  15. dRzMaxHighEt=9999.0,
  16. dRzMaxHighEtThres=0.0,
  17. dRzMaxLowEtGrad=0.0,
  18. ),
  19. dict(
  20. dPhiMaxHighEt=0.0015,
  21. dPhiMaxHighEtThres=0.0,
  22. dPhiMaxLowEtGrad=0.0,
  23. dRzMaxHighEt=0.025,
  24. dRzMaxHighEtThres=30.0,
  25. dRzMaxLowEtGrad=-0.002,
  26. ),
  27. dict(
  28. dPhiMaxHighEt=0.0015,
  29. dPhiMaxHighEtThres=0.0,
  30. dPhiMaxLowEtGrad=0.0,
  31. dRzMaxHighEt=0.025,
  32. dRzMaxHighEtThres=30.0,
  33. dRzMaxLowEtGrad=-0.002,
  34. )
  35. ],
  36. 'new-default': [
  37. dict(
  38. dPhiMaxHighEt=0.05,
  39. dPhiMaxHighEtThres=20.0,
  40. dPhiMaxLowEtGrad=-0.002,
  41. dRzMaxHighEt=9999.0,
  42. dRzMaxHighEtThres=0.0,
  43. dRzMaxLowEtGrad=0.0,
  44. ),
  45. dict(
  46. dPhiMaxHighEt=0.003,
  47. dPhiMaxHighEtThres=0.0,
  48. dPhiMaxLowEtGrad=0.0,
  49. dRzMaxHighEt=0.05,
  50. dRzMaxHighEtThres=30.0,
  51. dRzMaxLowEtGrad=-0.002,
  52. ),
  53. dict(
  54. dPhiMaxHighEt=0.003,
  55. dPhiMaxHighEtThres=0.0,
  56. dPhiMaxLowEtGrad=0.0,
  57. dRzMaxHighEt=0.05,
  58. dRzMaxHighEtThres=30.0,
  59. dRzMaxLowEtGrad=-0.002,
  60. )
  61. ],
  62. 'new-wide': [
  63. dict(
  64. dPhiMaxHighEt=0.10,
  65. dPhiMaxHighEtThres=20.0,
  66. dPhiMaxLowEtGrad=-0.002,
  67. dRzMaxHighEt=9999.0,
  68. dRzMaxHighEtThres=0.0,
  69. dRzMaxLowEtGrad=0.0,
  70. ),
  71. dict(
  72. dPhiMaxHighEt=0.006,
  73. dPhiMaxHighEtThres=0.0,
  74. dPhiMaxLowEtGrad=0.0,
  75. dRzMaxHighEt=0.10,
  76. dRzMaxHighEtThres=30.0,
  77. dRzMaxLowEtGrad=-0.002,
  78. ),
  79. dict(
  80. dPhiMaxHighEt=0.006,
  81. dPhiMaxHighEtThres=0.0,
  82. dPhiMaxLowEtGrad=0.0,
  83. dRzMaxHighEt=0.10,
  84. dRzMaxHighEtThres=30.0,
  85. dRzMaxLowEtGrad=-0.002,
  86. )
  87. ],
  88. 'new-extra-wide': [
  89. dict(
  90. dPhiMaxHighEt=0.15,
  91. dPhiMaxHighEtThres=20.0,
  92. dPhiMaxLowEtGrad=-0.002,
  93. dRzMaxHighEt=9999.0,
  94. dRzMaxHighEtThres=0.0,
  95. dRzMaxLowEtGrad=0.0,
  96. ),
  97. dict(
  98. dPhiMaxHighEt=0.009,
  99. dPhiMaxHighEtThres=0.0,
  100. dPhiMaxLowEtGrad=0.0,
  101. dRzMaxHighEt=0.15,
  102. dRzMaxHighEtThres=30.0,
  103. dRzMaxLowEtGrad=-0.002,
  104. ),
  105. dict(
  106. dPhiMaxHighEt=0.009,
  107. dPhiMaxHighEtThres=0.0,
  108. dPhiMaxLowEtGrad=0.0,
  109. dRzMaxHighEt=0.15,
  110. dRzMaxHighEtThres=30.0,
  111. dRzMaxLowEtGrad=-0.002,
  112. )
  113. ],
  114. }
  115. samples = None
  116. def load_samples():
  117. global samples
  118. if samples is None:
  119. print("loading samples")
  120. samples = {(proc, wp): root_open(f'../hists/{proc}-{wp}.root') for proc, wp in product(procs, wps)}
  121. return samples
  122. def calc_window(et, eta, hit, variable, cut_sel):
  123. idx = min(hit-1, 2)
  124. cuts = matching_cuts[cut_sel][idx]
  125. if 'etaBins' in cuts:
  126. for eta_idx, bin_high in enumerate(cuts['etaBins']):
  127. if eta < bin_high:
  128. high_et = cuts[f'{variable}MaxHighEt'][eta_idx]
  129. high_et_thres = cuts[f'{variable}MaxHighEtThres'][eta_idx]
  130. low_et_grad = cuts[f'{variable}MaxLowEtGrad'][eta_idx]
  131. break
  132. else: # highest bin
  133. high_et = cuts[f'{variable}MaxHighEt'][-1]
  134. high_et_thres = cuts[f'{variable}MaxHighEtThres'][-1]
  135. low_et_grad = cuts[f'{variable}MaxLowEtGrad'][-1]
  136. else:
  137. high_et = cuts[f'{variable}MaxHighEt']
  138. high_et_thres = cuts[f'{variable}MaxHighEtThres']
  139. low_et_grad = cuts[f'{variable}MaxLowEtGrad']
  140. return high_et + min(0, et-high_et_thres)*low_et_grad
  141. def hist_integral_ratio(num, den):
  142. num_int = num.integral
  143. den_int = den.integral
  144. ratio = num_int / den_int
  145. error = np.sqrt(den_int) / den_int # TODO: Check this definition of error
  146. return ratio, error
  147. def center_text(x, y, txt, **kwargs):
  148. plt.text(x, y, txt,
  149. horizontalalignment='center', verticalalignment='center',
  150. transform=plt.gca().transAxes, size=24, **kwargs)
  151. def hist_plot(h: Hist1D, *args, include_errors=False, line_width=1, **kwargs):
  152. """ Plots a 1D ROOT histogram object using matplotlib """
  153. counts = h.counts
  154. edges = h.edges
  155. left, right = edges[:-1], edges[1:]
  156. x = np.array([left, right]).T.flatten()
  157. y = np.array([counts, counts]).T.flatten()
  158. plt.plot(x, y, *args, linewidth=line_width, **kwargs)
  159. if include_errors:
  160. if h.errors_up is not None:
  161. errors = np.vstack((h.errors_down, h.errors_up))
  162. else:
  163. errors = h.errors
  164. plt.errorbar(h.bin_centers, h.counts, yerr=errors,
  165. color='k', marker=None, linestyle='None',
  166. barsabove=True, elinewidth=.7, capsize=1)
  167. def hist2d_percent_contour(h: Hist1D, percent: float, axis: str):
  168. values = h.counts
  169. try:
  170. axis = axis.lower()
  171. axis_idx = {'x': 1, 'y': 0}[axis]
  172. except KeyError:
  173. raise ValueError('axis must be \'x\' or \'y\'')
  174. if percent < 0 or percent > 1:
  175. raise ValueError('percent must be in [0,1]')
  176. with np.warnings.catch_warnings():
  177. np.warnings.filterwarnings('ignore', 'invalid value encountered in true_divide')
  178. values = values / np.sum(values, axis=axis_idx, keepdims=True)
  179. np.nan_to_num(values, copy=False)
  180. values = np.cumsum(values, axis=axis_idx)
  181. idxs = np.argmax(values > percent, axis=axis_idx)
  182. x_centers, y_centers = h.bin_centers
  183. if axis == 'x':
  184. return x_centers[idxs], y_centers
  185. else:
  186. return x_centers, y_centers[idxs]
  187. @mpb.decl_fig
  188. def plot_residuals(sample, layer, hit, variable, subdet):
  189. load_samples()
  190. proc, wp = sample
  191. track_matched = samples[sample][f'{variable}_{subdet}_L{layer}_H{hit}_v_Et_TrackMatched']
  192. # no_match = samples[sample][f'{variable}_{subdet}_L{layer}_H{hit}_v_Et_NoMatch']
  193. h_real = Hist2D(track_matched)
  194. # h_fake = Hist2D(no_match)
  195. def do_plot(h):
  196. plot_2d(h, cmap='viridis')
  197. xs, ys = hist2d_percent_contour(h, .90, 'x')
  198. plt.plot(xs, ys, color='green', label='90\% contour')
  199. xs, ys = hist2d_percent_contour(h, .995, 'x')
  200. plt.plot(xs, ys, color='darkgreen', label='99.5\% contour')
  201. ets = h.edges[1]
  202. cuts = [calc_window(et, 0, hit, variable, wp) for et in ets]
  203. plt.plot(cuts, ets, color='red', label='Cut Value')
  204. plt.xlabel({'dPhi': r'$\delta \phi$ (rads)',
  205. 'dRz': r'$\delta R/z$ (cm)'}[variable])
  206. # plt.sca(plt.subplot(1, 2, 1))
  207. do_plot(h_real)
  208. plt.title('Truth-Matched Seeds')
  209. plt.ylabel('$E_T$ (GeV)')
  210. # plt.sca(plt.subplot(1, 2, 2))
  211. # do_plot(h_fake)
  212. # plt.title('Not Truth-Matched Seeds')
  213. plt.legend(loc='upper right')
  214. @mpb.decl_fig
  215. def plot_residuals_eta(sample, hit, variable):
  216. load_samples()
  217. h = Hist2D(samples[sample][f'{variable}_residuals_v_eta_H{hit}'])
  218. plot_2d(h)
  219. xs, ys = hist2d_percent_contour(h, .90, 'x')
  220. plt.plot(xs, ys, color='green', label='90\% contour')
  221. xs, ys = hist2d_percent_contour(h, .995, 'x')
  222. plt.plot(xs, ys, color='darkgreen', label='99.5\% contour')
  223. plt.xlabel({'dPhi': r'$\delta \phi$ (rads)',
  224. 'dRz': r'$\delta R/z$ (cm)'}[variable])
  225. plt.ylabel(r'$|\eta|$')
  226. @mpb.decl_fig
  227. def plot_hit_vs_layer(sample, region):
  228. load_samples()
  229. h = Hist2D(samples[sample][f'hit_vs_layer_{region}'])
  230. plot_2d(h, colz_fmt='2.0f')
  231. plt.xlabel('Layer #')
  232. plt.ylabel('Hit #')
  233. @mpb.decl_fig
  234. def plot_roc_curve(pfx, ext=''):
  235. load_samples()
  236. show_fr = pfx == "tracking"
  237. def get_num_den(sample, basename):
  238. num = Hist1D(sample[f'{basename}_num'])
  239. den = Hist1D(sample[f'{basename}_den'])
  240. return hist_integral_ratio(num, den)
  241. rows = []
  242. for (proc, wp), sample in samples.items():
  243. sample_name = f'{proc}-{wp}'
  244. eff, eff_err = get_num_den(sample, f'{pfx}_eff_v_phi{ext}')
  245. pur, pur_err = get_num_den(sample, f'{pfx}_pur_v_phi{ext}')
  246. if show_fr:
  247. fr, fr_err = get_num_den(sample, f'fake_rate_v_phi')
  248. rows.append([wp,
  249. rf'${eff*100:0.2f}\pm{eff_err*100:0.2f}\%$',
  250. rf'${pur*100:0.2f}\pm{pur_err*100:0.2f}\%$',
  251. rf'${fr*100:0.2f}\pm{fr_err*100:0.2f}\%$'])
  252. plt.errorbar([pur], [eff], xerr=[pur_err], yerr=[eff_err],
  253. label=sample_name, marker='o', color=color(proc, wp))
  254. center_text(0.3, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  255. plt.axis('equal')
  256. plt.xlim((0.5, 1.02))
  257. plt.ylim((0.5, 1.02))
  258. plt.xlabel('Purity')
  259. plt.ylabel('Efficiency')
  260. plt.grid()
  261. plt.legend(loc='lower right')
  262. col_labels = ['Sample', 'Working Point', 'Efficiency', 'Purity']
  263. if show_fr:
  264. col_labels.append("Fake Rate")
  265. row_labels = [r'$Z \rightarrow ee$', '', '', r'$t\bar{t}$', '', '']
  266. return to_html_table(rows, col_labels, row_labels, 'table-condensed')
  267. @mpb.decl_fig
  268. def plot_kinematic_eff(pref, ext='', ylim=(None, None), norm=None, label_pfx='', incl_sel=True,
  269. bins_pt=None, bins_eta=None, bins_phi=None,
  270. xlim_pt=(None, None), xlim_eta=(None, None), xlim_phi=(None, None),
  271. is_ratio=False):
  272. load_samples()
  273. ax_pt = plt.subplot(221)
  274. ax_eta = plt.subplot(222)
  275. ax_phi = plt.subplot(223)
  276. errors = True
  277. for (proc, wp), sample in samples.items():
  278. sample_name = f'{proc}-{wp}'
  279. l = sample_name
  280. c = color(proc, wp)
  281. def do_plot(ax, name, bins):
  282. plt.sca(ax)
  283. if is_ratio:
  284. num = Hist1D(sample[name+"_num"], no_overflow=True)
  285. den = Hist1D(sample[name+"_den"], no_overflow=True)
  286. if bins:
  287. num.rebin(bins)
  288. den.rebin(bins)
  289. h = num // den
  290. else:
  291. h = Hist1D(sample[name], no_overflow=True)
  292. if norm:
  293. h = h / (norm*h.integral)
  294. if bins:
  295. h.rebin(bins)
  296. hist_plot(h, include_errors=errors, label=l, color=c)
  297. do_plot(ax_pt, f'{pref}_v_pt{ext}', bins_pt)
  298. do_plot(ax_eta, f'{pref}_v_eta{ext}', bins_eta)
  299. do_plot(ax_phi, f'{pref}_v_phi{ext}', bins_phi)
  300. plt.sca(ax_pt)
  301. if not incl_sel: center_text(0.5, 0.15, r'$|\eta|<2.5$')
  302. plt.xlabel(fr"{label_pfx} $p_T$")
  303. plt.ylim(ylim)
  304. plt.xlim(xlim_pt)
  305. plt.sca(ax_eta)
  306. if not incl_sel: center_text(0.5, 0.15, r'$p_T>20$')
  307. plt.xlabel(fr"{label_pfx} $\eta$")
  308. plt.ylim(ylim)
  309. plt.xlim(xlim_eta)
  310. plt.sca(ax_phi)
  311. if not incl_sel: center_text(0.5, 0.15, r'$p_T>20$ and $|\eta|<2.4$')
  312. plt.xlabel(fr"{label_pfx} $\phi$")
  313. plt.ylim(ylim)
  314. plt.xlim(xlim_phi)
  315. plt.tight_layout()
  316. plt.legend(loc='upper left', bbox_to_anchor=(0.6, 0.45), bbox_transform=plt.gcf().transFigure)
  317. @mpb.decl_fig
  318. def plot_ecal_rel_res():
  319. load_samples()
  320. for sample_name, sample in samples.items():
  321. h = Hist1D(sample['ecal_energy_resolution'])
  322. h = h / h.integral
  323. hist_plot(h, label=sample_name)
  324. plt.xlabel(r"ECAL $E_T$ relative error")
  325. plt.legend()
  326. @mpb.decl_fig
  327. def plot_res_contour(proc, hit_number, var, layers, ext='_TrackMatched'):
  328. load_samples()
  329. _, axs = plt.subplots(1, 2, sharey=True)
  330. def do_plot(ax, sample):
  331. plt.sca(ax)
  332. wp = sample[1]
  333. plt.title(wp)
  334. h = None
  335. for subdet, layer in layers:
  336. h = Hist2D(samples[sample][f'{var}_{subdet}_L{layer}_H{hit_number}_v_Et{ext}'])
  337. xs, ys = hist2d_percent_contour(h, .99, 'x')
  338. plt.plot(xs, ys, label=f'{subdet} - L{layer}')
  339. ets = h.edges[1]
  340. cuts = [calc_window(et, 0, hit_number, var, wp) for et in ets]
  341. plt.plot(cuts, ets, color='k', label='Cut Value')
  342. for ax, wp in zip(axs, ['new-default', 'new-wide']):
  343. do_plot(ax, (proc, wp))
  344. plt.sca(axs[-1])
  345. plt.legend(loc='upper right')
  346. @mpb.decl_fig
  347. def simple_dist(hist_name, rebin=(), norm=1, xlabel="", ylabel="", xlim=None, ylim=None, line_width=1):
  348. load_samples()
  349. for (proc, wp), sample in samples.items():
  350. sample_name = f'{proc}-{wp}'
  351. h = Hist1D(sample[hist_name])
  352. if rebin:
  353. h.rebin(*rebin)
  354. mean = np.sum(h.counts * h.bin_centers) / h.integral
  355. if norm is not None:
  356. h = h * (norm / h.integral)
  357. hist_plot(h, label=f'{sample_name} ($\\mu={mean:.2f}$)',
  358. color=color(proc, wp), line_width=line_width)
  359. if xlim:
  360. plt.xlim(xlim)
  361. if ylim:
  362. plt.ylim(ylim)
  363. plt.xlabel(xlabel)
  364. plt.ylabel(ylabel)
  365. plt.legend()
  366. @mpb.decl_fig
  367. def simple_dist2d(hist_name, proc, wp, xlabel="", ylabel="", xlim=None, ylim=None, norm=None):
  368. load_samples()
  369. sample = samples[(proc, wp)]
  370. # sample_name = f'{proc}-{wp}'
  371. h = Hist2D(sample[hist_name])
  372. if norm is not None:
  373. h = h * (norm / h.integral)
  374. plot_2d(h, colz_fmt='g')
  375. if xlim:
  376. plt.xlim(xlim)
  377. if ylim:
  378. plt.ylim(ylim)
  379. plt.xlabel(xlabel)
  380. plt.ylabel(ylabel)
  381. def all_cut_plots(refresh=True, publish=False):
  382. figures = {
  383. 'tracking_roc_curve': (plot_roc_curve, ('tracking',)),
  384. 'tracking_roc_curve_dR': (plot_roc_curve, ('tracking',), {'ext': '_dR'}),
  385. 'seeding_roc_curve': (plot_roc_curve, ('seed',)),
  386. 'number_of_seeds': (simple_dist, ('n_seeds',),
  387. dict(xlabel='Number of Seeds', rebin=(50, -0.5, 200.5))),
  388. 'number_of_good_seeds': (simple_dist, ('n_good_seeds',),
  389. dict(xlabel='Number of Seeds', rebin=(50, -0.5, 200.5))),
  390. 'number_of_scls': (simple_dist, ('n_scl',),
  391. dict(xlabel='Number of Super-Clusters', xlim=(-0.5, 25.5))),
  392. 'number_of_good_scls': (simple_dist, ('n_good_scl',),
  393. dict(xlabel='Number of Super-Clusters', xlim=(-0.5, 25.5))),
  394. 'number_of_sim_els': (simple_dist, ('n_good_sim',),
  395. dict(xlabel='Number of prompt(ish) electrons', xlim=(-0.5, 20.5))),
  396. 'number_of_gsf_tracks': (simple_dist, ('n_gsf_track',),
  397. dict(xlabel='Number of reco electrons', xlim=(-0.5, 20.5))),
  398. 'number_of_prompt': (simple_dist, ('n_prompt',),
  399. dict(xlabel='Number of prompt electrons', xlim=(-0.5, 20.5))),
  400. 'number_of_nonprompt': (simple_dist, ('n_nonprompt',),
  401. dict(xlabel='Number of nonprompt electrons', xlim=(-0.5, 20.5))),
  402. 'number_of_matched': (simple_dist, ('n_matched',),
  403. dict(xlabel='Number of matched electrons', xlim=(-0.5, 10.5), line_width=4)),
  404. 'number_of_merged': (simple_dist, ('n_merged',),
  405. dict(xlabel='Number of merged electrons', xlim=(-0.5, 10.5), line_width=4)),
  406. 'number_of_lost': (simple_dist, ('n_lost',),
  407. dict(xlabel='Number of lost electrons', xlim=(-0.5, 10.5), line_width=4)),
  408. 'number_of_split': (simple_dist, ('n_split',),
  409. dict(xlabel='Number of split electrons', xlim=(-0.5, 10.5), line_width=4)),
  410. 'number_of_faked': (simple_dist, ('n_faked',),
  411. dict(xlabel='Number of faked electrons', xlim=(-0.5, 10.5), line_width=4)),
  412. 'number_of_flipped': (simple_dist, ('n_flipped',),
  413. dict(xlabel='Number of flipped electrons', xlim=(-0.5, 10.5), line_width=4)),
  414. 'matched_dR': (simple_dist, ('matched_dR',),
  415. dict(xlabel='dR between sim and reco')),
  416. 'matched_dpT': (simple_dist, ('matched_dpT',),
  417. dict(xlabel='dpT between sim and reco')),
  418. 'number_of_matched_dR': (simple_dist, ('n_matched_dR',),
  419. dict(xlabel='Number of matched electrons - dR Matched', xlim=(-0.5, 10.5),
  420. line_width=4)),
  421. 'number_of_merged_dR': (simple_dist, ('n_merged_dR',),
  422. dict(xlabel='Number of merged electrons - dR Matched', xlim=(-0.5, 10.5),
  423. line_width=4)),
  424. 'number_of_lost_dR': (simple_dist, ('n_lost_dR',),
  425. dict(xlabel='Number of lost electrons - dR Matched', xlim=(-0.5, 10.5),
  426. line_width=4)),
  427. 'number_of_split_dR': (simple_dist, ('n_split_dR',),
  428. dict(xlabel='Number of split electrons - dR Matched', xlim=(-0.5, 10.5),
  429. line_width=4)),
  430. 'number_of_faked_dR': (simple_dist, ('n_faked_dR',),
  431. dict(xlabel='Number of faked electrons - dR Matched', xlim=(-0.5, 10.5),
  432. line_width=4)),
  433. 'number_of_flipped_dR': (simple_dist, ('n_flipped_dR',),
  434. dict(xlabel='Number of flipped electrons - dR Matched', xlim=(-0.5, 10.5),
  435. line_width=4)),
  436. 'matched_dR_dR': (simple_dist, ('matched_dR_dR',),
  437. dict(xlabel='dR between sim and reco - dR Matched')),
  438. 'matched_dpT_dR': (simple_dist, ('matched_dpT_dR',),
  439. dict(xlabel='dpT between sim and reco - dR Matched')),
  440. 'tm_corr': (simple_dist2d, ('tm_corr', 'zee', 'old-default'),
  441. dict(xlabel='Seed Matched', ylabel='Track Matched', norm=1)),
  442. 'ecal_rel_res': plot_ecal_rel_res,
  443. 'hit_v_layer_BPIX_new-default_zee': (plot_hit_vs_layer, (('zee', 'new-default'), 'barrel')),
  444. 'hit_v_layer_FPIX_new-default_zee': (plot_hit_vs_layer, (('zee', 'new-default'), 'forward')),
  445. 'hit_v_layer_BPIX_new-default_tt': (plot_hit_vs_layer, (('tt', 'new-default'), 'barrel')),
  446. 'hit_v_layer_FPIX_new-default_tt': (plot_hit_vs_layer, (('tt', 'new-default'), 'forward')),
  447. 'hit_v_layer_BPIX_new-wide_zee': (plot_hit_vs_layer, (('zee', 'new-wide'), 'barrel')),
  448. 'hit_v_layer_FPIX_new-wide_zee': (plot_hit_vs_layer, (('zee', 'new-wide'), 'forward')),
  449. 'hit_v_layer_BPIX_new-wide_tt': (plot_hit_vs_layer, (('tt', 'new-wide'), 'barrel')),
  450. 'hit_v_layer_FPIX_new-wide_tt': (plot_hit_vs_layer, (('tt', 'new-wide'), 'forward')),
  451. 'good_sim_kinem': (plot_kinematic_eff, ('good_sim',),
  452. dict(norm=1, ylim=(0, None), bins_eta=30, bins_phi=30)),
  453. 'gsf_track_kinem': (plot_kinematic_eff, ('gsf_track',),
  454. dict(norm=1, ylim=(0, None), bins_eta=30, bins_phi=30)),
  455. 'seed_kinem': (plot_kinematic_eff, ('seed',),
  456. dict(norm=1, ylim=(0, None), bins_eta=30, bins_phi=30)),
  457. 'scl_kinem': (plot_kinematic_eff, ('scl',),
  458. dict(norm=1, ylim=(0, None), bins_eta=30, bins_phi=30)),
  459. 'prompt_kinem': (plot_kinematic_eff, ('prompt',),
  460. dict(norm=1, ylim=(0, None), bins_pt=30, bins_eta=30, bins_phi=30)),
  461. 'nonprompt_kinem': (plot_kinematic_eff, ('nonprompt',),
  462. dict(norm=1, ylim=(0, None), xlim_pt=(0, 5), bins_eta=30, bins_phi=30)),
  463. }
  464. def add_num_den(key, func, args, kwargs):
  465. base_ext = kwargs.get('ext', '')
  466. bins_pt_ = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200, 300]
  467. kwargs['bins_pt'] = kwargs.get('bins_pt', bins_pt_)
  468. kwargs['bins_eta'] = kwargs.get('bins_eta', 15)
  469. kwargs['bins_phi'] = kwargs.get('bins_phi', 15)
  470. figures[key] = (func, args, dict(**kwargs, ylim=(0, 1.1), is_ratio=True))
  471. kwargs_ = kwargs.copy()
  472. kwargs_['ext'] = base_ext+'_num'
  473. figures[key+'_num'] = (func, args, kwargs_)
  474. kwargs_ = kwargs.copy()
  475. kwargs_['ext'] = base_ext+'_den'
  476. figures[key+'_den'] = (func, args, kwargs_)
  477. add_num_den('tracking_eff', plot_kinematic_eff, ('tracking_eff',), dict(incl_sel=False))
  478. add_num_den('tracking_pur', plot_kinematic_eff, ('tracking_pur',), dict(incl_sel=False))
  479. add_num_den('tracking_eff_dR', plot_kinematic_eff, ('tracking_eff',), dict(ext='_dR', incl_sel=False))
  480. add_num_den('tracking_pur_dR', plot_kinematic_eff, ('tracking_pur',), dict(ext='_dR', incl_sel=False))
  481. add_num_den('prompt_eff', plot_kinematic_eff, ('prompt_eff',), dict(incl_sel=False))
  482. add_num_den('prompt_pur', plot_kinematic_eff, ('prompt_pur',), dict(incl_sel=False))
  483. add_num_den('prompt_eff_dR', plot_kinematic_eff, ('prompt_eff',), dict(ext='_dR', incl_sel=False))
  484. add_num_den('prompt_pur_dR', plot_kinematic_eff, ('prompt_pur',), dict(ext='_dR', incl_sel=False))
  485. add_num_den('nonprompt_eff', plot_kinematic_eff, ('nonprompt_eff',), dict(incl_sel=False))
  486. add_num_den('nonprompt_pur', plot_kinematic_eff, ('nonprompt_pur',), dict(incl_sel=False))
  487. add_num_den('nonprompt_eff_dR', plot_kinematic_eff, ('nonprompt_eff',), dict(ext='_dR', incl_sel=False))
  488. add_num_den('nonprompt_pur_dR', plot_kinematic_eff, ('nonprompt_pur',), dict(ext='_dR', incl_sel=False))
  489. add_num_den('seeding_eff', plot_kinematic_eff, ('seed_eff',), dict(incl_sel=False))
  490. add_num_den('seeding_pur', plot_kinematic_eff, ('seed_pur',), dict(incl_sel=False))
  491. add_num_den('fake_rate_incl', plot_kinematic_eff, ('fake_rate_incl',), {})
  492. add_num_den('fake_rate_no_e_match_incl', plot_kinematic_eff, ('fake_rate_no_e_match_incl',), {})
  493. add_num_den('partial_fake_rate_incl', plot_kinematic_eff, ('partial_fake_rate_incl',), {})
  494. add_num_den('full_fake_rate_incl', plot_kinematic_eff, ('full_fake_rate_incl',), {})
  495. add_num_den('clean_fake_rate_incl', plot_kinematic_eff, ('clean_fake_rate_incl',), {})
  496. #
  497. add_num_den('fake_rate', plot_kinematic_eff, ('fake_rate',), dict(incl_sel=False))
  498. add_num_den('fake_rate_no_e_match', plot_kinematic_eff, ('fake_rate_no_e_match',), dict(incl_sel=False))
  499. add_num_den('partial_fake_rate', plot_kinematic_eff, ('partial_fake_rate',), dict(incl_sel=False))
  500. add_num_den('full_fake_rate', plot_kinematic_eff, ('full_fake_rate',), dict(incl_sel=False))
  501. add_num_den('clean_fake_rate', plot_kinematic_eff, ('clean_fake_rate',), dict(incl_sel=False))
  502. hit_layers = [(1, 1), (1, 2), (2, 2), (2, 3), (3, 3), (3, 4)]
  503. for proc, wp, (hit, layer), var, subdet in product(['zee', 'tt'], ['new-default', 'new-wide'],
  504. hit_layers,
  505. ['dPhi', 'dRz'], ['BPIX', 'FPIX']):
  506. figures.update({
  507. f'res_{subdet}_L{layer}_H{hit}_{var}_{proc}_{wp}':
  508. (plot_residuals, ((proc, wp), layer, hit, var, subdet))})
  509. rel_layers = {1: [('BPIX', 1), ('BPIX', 2), ('FPIX', 1), ('FPIX', 2)],
  510. 2: [('BPIX', 2), ('BPIX', 3), ('FPIX', 2), ('FPIX', 3)],
  511. 3: [('BPIX', 3), ('BPIX', 4), ('FPIX', 3)], }
  512. for proc, hit, var in product(['zee', 'tt'], [1, 2, 3], ['dPhi', 'dRz']):
  513. figures.update({
  514. f'resall_H{hit}_{var}_{proc}': (plot_res_contour, (proc, hit, var, rel_layers[hit]))})
  515. for proc, wp, hit, var in product(['zee', 'tt'], ['new-default', 'new-wide'], [1, 2, 3], ['dPhi', 'dRz']):
  516. figures.update({
  517. f'res_v_eta_H{hit}_{var}_{proc}_{wp}': (plot_residuals_eta, ((proc, wp), hit, var))})
  518. mpb.render(figures, refresh=refresh)
  519. mpb.generate_report(figures, 'Electron Seeding Studies',
  520. output=f'hists.html',
  521. source=__file__)
  522. # mpb.generate_report(figures, 'Update',
  523. # output='report.html',
  524. # body='../docs/reports/report_2018_05_30.md')
  525. if publish:
  526. mpb.publish()
  527. def color(proc, wp):
  528. from matplotlib.colors import XKCD_COLORS
  529. def f(name):
  530. return XKCD_COLORS['xkcd:'+name]
  531. return {
  532. ('zee', 'new-extra-narrow'): f('kelly green'),
  533. ('tt', 'new-extra-narrow'): f('grey green'),
  534. ('zee', 'new-default'): f('red'),
  535. ('tt', 'new-default'): f('pink'),
  536. ('zee', 'new-wide'): f('strong blue'),
  537. ('tt', 'new-wide'): f('bright blue'),
  538. ('zee', 'new-extra-wide'): f('indigo'),
  539. ('tt', 'new-extra-wide'): f('bright purple'),
  540. ('zee', 'old-default'): f('black'),
  541. ('tt', 'old-default'): f('blue grey'),
  542. }[(proc, wp)]
  543. if __name__ == '__main__':
  544. set_defaults()
  545. mpb.configure(output_dir='seeding_studies',
  546. multiprocess=True,
  547. publish_remote="caleb@fangmeier.tech",
  548. publish_dir="/var/www/eg",
  549. publish_url="eg.fangmeier.tech/",
  550. )
  551. procs = {
  552. 'zee': r'$Z\rightarrow e^+e_-}$',
  553. 'tt': r'$t\bar{t}$'
  554. }
  555. wps = {
  556. 'new-default': 'HLT Settings',
  557. 'new-wide': 'Wide Settings',
  558. 'old-default': 'Old Seeding',
  559. }
  560. all_cut_plots(refresh=True, publish=True)