eff_plots.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. row_labels = []
  243. for proc in procs:
  244. row_labels.append(procs[proc])
  245. row_labels.extend(['']*(len(wps)-1))
  246. for wp in wps:
  247. sample = samples[(proc, wp)]
  248. sample_name = f'{proc}-{wp}'
  249. eff, eff_err = get_num_den(sample, f'{pfx}_eff_v_phi{ext}')
  250. pur, pur_err = get_num_den(sample, f'{pfx}_pur_v_phi{ext}')
  251. if show_fr:
  252. fr, fr_err = get_num_den(sample, f'fake_rate_no_e_match_v_phi')
  253. rows.append([wp,
  254. rf'${eff*100:0.2f}\pm{eff_err*100:0.2f}\%$',
  255. rf'${pur*100:0.2f}\pm{pur_err*100:0.2f}\%$',
  256. rf'${fr*100:0.2f}\pm{fr_err*100:0.2f}\%$'])
  257. plt.errorbar([pur], [eff], xerr=[pur_err], yerr=[eff_err],
  258. label=sample_name, marker='o', color=color(proc, wp))
  259. center_text(0.3, 0.3, r'$p_T>20$ and $|\eta|<2.5$')
  260. plt.axis('equal')
  261. plt.xlim((0.5, 1.02))
  262. plt.ylim((0.5, 1.02))
  263. plt.xlabel('Purity')
  264. plt.ylabel('Efficiency')
  265. plt.grid()
  266. plt.legend(loc='lower right')
  267. col_labels = ['Sample', 'Working Point', 'Efficiency', 'Purity']
  268. if show_fr:
  269. col_labels.append("Fake Rate")
  270. return to_html_table(rows, col_labels, row_labels, 'table-condensed')
  271. @mpb.decl_fig
  272. def plot_kinematic_eff(pref, ext='', ylim=(None, None), norm=None, label_pfx='', incl_sel=True,
  273. bins_pt=None, bins_eta=None, bins_phi=None,
  274. xlim_pt=(None, None), xlim_eta=(None, None), xlim_phi=(None, None),
  275. is_ratio=False):
  276. load_samples()
  277. ax_pt = plt.subplot(221)
  278. ax_eta = plt.subplot(222)
  279. ax_phi = plt.subplot(223)
  280. errors = True
  281. for (proc, wp), sample in samples.items():
  282. sample_name = f'{proc}-{wp}'
  283. l = sample_name
  284. c = color(proc, wp)
  285. def do_plot(ax, name, bins):
  286. plt.sca(ax)
  287. if is_ratio:
  288. num = Hist1D(sample[name+"_num"], no_overflow=True)
  289. den = Hist1D(sample[name+"_den"], no_overflow=True)
  290. if bins:
  291. num.rebin(bins)
  292. den.rebin(bins)
  293. h = num // den
  294. else:
  295. h = Hist1D(sample[name], no_overflow=True)
  296. if norm:
  297. h = h / (norm*h.integral)
  298. if bins:
  299. h.rebin(bins)
  300. hist_plot(h, include_errors=errors, label=l, color=c)
  301. do_plot(ax_pt, f'{pref}_v_pt{ext}', bins_pt)
  302. do_plot(ax_eta, f'{pref}_v_eta{ext}', bins_eta)
  303. do_plot(ax_phi, f'{pref}_v_phi{ext}', bins_phi)
  304. plt.sca(ax_pt)
  305. if not incl_sel: center_text(0.5, 0.15, r'$|\eta|<2.5$')
  306. plt.xlabel(fr"{label_pfx} $p_T$")
  307. plt.ylim(ylim)
  308. plt.xlim(xlim_pt)
  309. plt.sca(ax_eta)
  310. if not incl_sel: center_text(0.5, 0.15, r'$p_T>20$')
  311. plt.xlabel(fr"{label_pfx} $\eta$")
  312. plt.ylim(ylim)
  313. plt.xlim(xlim_eta)
  314. plt.sca(ax_phi)
  315. if not incl_sel: center_text(0.5, 0.15, r'$p_T>20$ and $|\eta|<2.5$')
  316. plt.xlabel(fr"{label_pfx} $\phi$")
  317. plt.ylim(ylim)
  318. plt.xlim(xlim_phi)
  319. plt.tight_layout()
  320. plt.legend(loc='upper left', bbox_to_anchor=(0.6, 0.45), bbox_transform=plt.gcf().transFigure,
  321. prop={'size': 20})
  322. @mpb.decl_fig
  323. def plot_ecal_rel_res():
  324. load_samples()
  325. for sample_name, sample in samples.items():
  326. h = Hist1D(sample['ecal_energy_resolution'])
  327. h = h / h.integral
  328. hist_plot(h, label=sample_name)
  329. plt.xlabel(r"ECAL $E_T$ relative error")
  330. plt.legend()
  331. @mpb.decl_fig
  332. def plot_res_contour(proc, hit_number, var, layers, ext='_TrackMatched'):
  333. load_samples()
  334. _, axs = plt.subplots(1, 2, sharey=True)
  335. def do_plot(ax, sample):
  336. plt.sca(ax)
  337. wp = sample[1]
  338. plt.title(wp)
  339. h = None
  340. for subdet, layer in layers:
  341. h = Hist2D(samples[sample][f'{var}_{subdet}_L{layer}_H{hit_number}_v_Et{ext}'])
  342. xs, ys = hist2d_percent_contour(h, .99, 'x')
  343. plt.plot(xs, ys, label=f'{subdet} - L{layer}')
  344. ets = h.edges[1]
  345. cuts = [calc_window(et, 0, hit_number, var, wp) for et in ets]
  346. plt.plot(cuts, ets, color='k', label='Cut Value')
  347. for ax, wp in zip(axs, ['new-default', 'new-wide']):
  348. do_plot(ax, (proc, wp))
  349. plt.sca(axs[-1])
  350. plt.legend(loc='upper right')
  351. @mpb.decl_fig
  352. def simple_dist(hist_name, rebin=(), norm=1, xlabel="", ylabel="", xlim=None, ylim=None, line_width=1):
  353. load_samples()
  354. for (proc, wp), sample in samples.items():
  355. sample_name = f'{proc}-{wp}'
  356. h = Hist1D(sample[hist_name])
  357. if rebin:
  358. h.rebin(*rebin)
  359. mean = np.sum(h.counts * h.bin_centers) / h.integral
  360. if norm is not None:
  361. h = h * (norm / h.integral)
  362. hist_plot(h, label=f'{sample_name} ($\\mu={mean:.2f}$)',
  363. color=color(proc, wp), line_width=line_width)
  364. if xlim:
  365. plt.xlim(xlim)
  366. if ylim:
  367. plt.ylim(ylim)
  368. plt.xlabel(xlabel)
  369. plt.ylabel(ylabel)
  370. plt.legend()
  371. @mpb.decl_fig
  372. def simple_dist2d(hist_name, proc, wp, xlabel="", ylabel="", xlim=None, ylim=None, norm=None):
  373. load_samples()
  374. sample = samples[(proc, wp)]
  375. # sample_name = f'{proc}-{wp}'
  376. h = Hist2D(sample[hist_name])
  377. if norm is not None:
  378. h = h * (norm / h.integral)
  379. plot_2d(h, colz_fmt='g')
  380. if xlim:
  381. plt.xlim(xlim)
  382. if ylim:
  383. plt.ylim(ylim)
  384. plt.xlabel(xlabel)
  385. plt.ylabel(ylabel)
  386. def all_cut_plots(refresh=True, publish=False):
  387. figures = {
  388. 'tracking_roc_curve': (plot_roc_curve, ('tracking',)),
  389. 'tracking_roc_curve_dR': (plot_roc_curve, ('tracking',), {'ext': '_dR'}),
  390. 'seeding_roc_curve': (plot_roc_curve, ('seed',)),
  391. 'number_of_seeds': (simple_dist, ('n_seeds',),
  392. dict(xlabel='Number of Seeds', rebin=(50, -0.5, 200.5))),
  393. 'number_of_good_seeds': (simple_dist, ('n_good_seeds',),
  394. dict(xlabel='Number of Seeds', rebin=(50, -0.5, 200.5))),
  395. 'number_of_scls': (simple_dist, ('n_scl',),
  396. dict(xlabel='Number of Super-Clusters', xlim=(-0.5, 25.5))),
  397. 'number_of_good_scls': (simple_dist, ('n_good_scl',),
  398. dict(xlabel='Number of Super-Clusters', xlim=(-0.5, 25.5))),
  399. 'number_of_sim_els': (simple_dist, ('n_good_sim',),
  400. dict(xlabel='Number of prompt(ish) electrons', xlim=(-0.5, 20.5))),
  401. 'number_of_gsf_tracks': (simple_dist, ('n_gsf_track',),
  402. dict(xlabel='Number of reco electrons', xlim=(-0.5, 20.5))),
  403. 'number_of_prompt': (simple_dist, ('n_prompt',),
  404. dict(xlabel='Number of prompt electrons', xlim=(-0.5, 20.5))),
  405. 'number_of_nonprompt': (simple_dist, ('n_nonprompt',),
  406. dict(xlabel='Number of nonprompt electrons', xlim=(-0.5, 20.5))),
  407. 'number_of_matched': (simple_dist, ('n_matched',),
  408. dict(xlabel='Number of matched electrons', xlim=(-0.5, 10.5), line_width=4)),
  409. 'number_of_merged': (simple_dist, ('n_merged',),
  410. dict(xlabel='Number of merged electrons', xlim=(-0.5, 10.5), line_width=4)),
  411. 'number_of_lost': (simple_dist, ('n_lost',),
  412. dict(xlabel='Number of lost electrons', xlim=(-0.5, 10.5), line_width=4)),
  413. 'number_of_split': (simple_dist, ('n_split',),
  414. dict(xlabel='Number of split electrons', xlim=(-0.5, 10.5), line_width=4)),
  415. 'number_of_faked': (simple_dist, ('n_faked',),
  416. dict(xlabel='Number of faked electrons', xlim=(-0.5, 10.5), line_width=4)),
  417. 'number_of_flipped': (simple_dist, ('n_flipped',),
  418. dict(xlabel='Number of flipped electrons', xlim=(-0.5, 10.5), line_width=4)),
  419. 'matched_dR': (simple_dist, ('matched_dR',),
  420. dict(xlabel='dR between sim and reco')),
  421. 'matched_dpT': (simple_dist, ('matched_dpT',),
  422. dict(xlabel='dpT between sim and reco')),
  423. 'number_of_matched_dR': (simple_dist, ('n_matched_dR',),
  424. dict(xlabel='Number of matched electrons - dR Matched', xlim=(-0.5, 10.5),
  425. line_width=4)),
  426. 'number_of_merged_dR': (simple_dist, ('n_merged_dR',),
  427. dict(xlabel='Number of merged electrons - dR Matched', xlim=(-0.5, 10.5),
  428. line_width=4)),
  429. 'number_of_lost_dR': (simple_dist, ('n_lost_dR',),
  430. dict(xlabel='Number of lost electrons - dR Matched', xlim=(-0.5, 10.5),
  431. line_width=4)),
  432. 'number_of_split_dR': (simple_dist, ('n_split_dR',),
  433. dict(xlabel='Number of split electrons - dR Matched', xlim=(-0.5, 10.5),
  434. line_width=4)),
  435. 'number_of_faked_dR': (simple_dist, ('n_faked_dR',),
  436. dict(xlabel='Number of faked electrons - dR Matched', xlim=(-0.5, 10.5),
  437. line_width=4)),
  438. 'number_of_flipped_dR': (simple_dist, ('n_flipped_dR',),
  439. dict(xlabel='Number of flipped electrons - dR Matched', xlim=(-0.5, 10.5),
  440. line_width=4)),
  441. 'matched_dR_dR': (simple_dist, ('matched_dR_dR',),
  442. dict(xlabel='dR between sim and reco - dR Matched')),
  443. 'matched_dpT_dR': (simple_dist, ('matched_dpT_dR',),
  444. dict(xlabel='dpT between sim and reco - dR Matched')),
  445. 'tm_corr': (simple_dist2d, ('tm_corr', 'zee', 'old-default'),
  446. dict(xlabel='Seed Matched', ylabel='Track Matched', norm=1)),
  447. 'ecal_rel_res': plot_ecal_rel_res,
  448. 'hit_v_layer_BPIX_new-default_zee': (plot_hit_vs_layer, (('zee', 'new-default'), 'barrel')),
  449. 'hit_v_layer_FPIX_new-default_zee': (plot_hit_vs_layer, (('zee', 'new-default'), 'forward')),
  450. 'hit_v_layer_BPIX_new-default_tt': (plot_hit_vs_layer, (('tt', 'new-default'), 'barrel')),
  451. 'hit_v_layer_FPIX_new-default_tt': (plot_hit_vs_layer, (('tt', 'new-default'), 'forward')),
  452. 'hit_v_layer_BPIX_new-wide_zee': (plot_hit_vs_layer, (('zee', 'new-wide'), 'barrel')),
  453. 'hit_v_layer_FPIX_new-wide_zee': (plot_hit_vs_layer, (('zee', 'new-wide'), 'forward')),
  454. 'hit_v_layer_BPIX_new-wide_tt': (plot_hit_vs_layer, (('tt', 'new-wide'), 'barrel')),
  455. 'hit_v_layer_FPIX_new-wide_tt': (plot_hit_vs_layer, (('tt', 'new-wide'), 'forward')),
  456. 'good_sim_kinem': (plot_kinematic_eff, ('good_sim',),
  457. dict(norm=1, ylim=(0, None), bins_eta=30, bins_phi=30)),
  458. 'gsf_track_kinem': (plot_kinematic_eff, ('gsf_track',),
  459. dict(norm=1, ylim=(0, None), bins_eta=30, bins_phi=30)),
  460. 'seed_kinem': (plot_kinematic_eff, ('seed',),
  461. dict(norm=1, ylim=(0, None), bins_eta=30, bins_phi=30)),
  462. 'scl_kinem': (plot_kinematic_eff, ('scl',),
  463. dict(norm=1, ylim=(0, None), bins_eta=30, bins_phi=30)),
  464. 'prompt_kinem': (plot_kinematic_eff, ('prompt',),
  465. dict(norm=1, ylim=(0, None), bins_pt=30, bins_eta=30, bins_phi=30)),
  466. 'nonprompt_kinem': (plot_kinematic_eff, ('nonprompt',),
  467. dict(norm=1, ylim=(0, None), xlim_pt=(0, 5), bins_eta=30, bins_phi=30)),
  468. }
  469. def add_num_den(key, func, args, kwargs):
  470. base_ext = kwargs.get('ext', '')
  471. bins_pt_ = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200, 300]
  472. kwargs['bins_pt'] = kwargs.get('bins_pt', bins_pt_)
  473. kwargs['bins_eta'] = kwargs.get('bins_eta', 15)
  474. kwargs['bins_phi'] = kwargs.get('bins_phi', 15)
  475. figures[key] = (func, args, dict(**kwargs, ylim=(0, 1.1), is_ratio=True))
  476. kwargs_ = kwargs.copy()
  477. kwargs_['ext'] = base_ext+'_num'
  478. figures[key+'_num'] = (func, args, kwargs_)
  479. kwargs_ = kwargs.copy()
  480. kwargs_['ext'] = base_ext+'_den'
  481. figures[key+'_den'] = (func, args, kwargs_)
  482. add_num_den('tracking_eff', plot_kinematic_eff, ('tracking_eff',), dict(incl_sel=False))
  483. add_num_den('tracking_pur', plot_kinematic_eff, ('tracking_pur',), dict(incl_sel=False))
  484. add_num_den('tracking_eff_dR', plot_kinematic_eff, ('tracking_eff',), dict(ext='_dR', incl_sel=False))
  485. add_num_den('tracking_pur_dR', plot_kinematic_eff, ('tracking_pur',), dict(ext='_dR', incl_sel=False))
  486. add_num_den('prompt_eff', plot_kinematic_eff, ('prompt_eff',), dict(incl_sel=False))
  487. add_num_den('prompt_pur', plot_kinematic_eff, ('prompt_pur',), dict(incl_sel=False))
  488. add_num_den('prompt_eff_dR', plot_kinematic_eff, ('prompt_eff',), dict(ext='_dR', incl_sel=False))
  489. add_num_den('prompt_pur_dR', plot_kinematic_eff, ('prompt_pur',), dict(ext='_dR', incl_sel=False))
  490. add_num_den('nonprompt_eff', plot_kinematic_eff, ('nonprompt_eff',), dict(incl_sel=False))
  491. add_num_den('nonprompt_pur', plot_kinematic_eff, ('nonprompt_pur',), dict(incl_sel=False))
  492. add_num_den('nonprompt_eff_dR', plot_kinematic_eff, ('nonprompt_eff',), dict(ext='_dR', incl_sel=False))
  493. add_num_den('nonprompt_pur_dR', plot_kinematic_eff, ('nonprompt_pur',), dict(ext='_dR', incl_sel=False))
  494. add_num_den('seeding_eff', plot_kinematic_eff, ('seed_eff',), dict(incl_sel=False))
  495. add_num_den('seeding_pur', plot_kinematic_eff, ('seed_pur',), dict(incl_sel=False))
  496. add_num_den('fake_rate_incl', plot_kinematic_eff, ('fake_rate_incl',), {})
  497. add_num_den('fake_rate_no_e_match_incl', plot_kinematic_eff, ('fake_rate_no_e_match_incl',), {})
  498. add_num_den('partial_fake_rate_incl', plot_kinematic_eff, ('partial_fake_rate_incl',), {})
  499. add_num_den('full_fake_rate_incl', plot_kinematic_eff, ('full_fake_rate_incl',), {})
  500. add_num_den('clean_fake_rate_incl', plot_kinematic_eff, ('clean_fake_rate_incl',), {})
  501. add_num_den('fake_rate', plot_kinematic_eff, ('fake_rate',), dict(incl_sel=False))
  502. add_num_den('fake_rate_no_e_match', plot_kinematic_eff, ('fake_rate_no_e_match',), dict(incl_sel=False))
  503. add_num_den('partial_fake_rate', plot_kinematic_eff, ('partial_fake_rate',), dict(incl_sel=False))
  504. add_num_den('full_fake_rate', plot_kinematic_eff, ('full_fake_rate',), dict(incl_sel=False))
  505. add_num_den('clean_fake_rate', plot_kinematic_eff, ('clean_fake_rate',), dict(incl_sel=False))
  506. hit_layers = [(1, 1), (1, 2), (2, 2), (2, 3), (3, 3), (3, 4)]
  507. for proc, wp, (hit, layer), var, subdet in product(['zee', 'tt'], ['new-default', 'new-wide'],
  508. hit_layers,
  509. ['dPhi', 'dRz'], ['BPIX', 'FPIX']):
  510. figures.update({
  511. f'res_{subdet}_L{layer}_H{hit}_{var}_{proc}_{wp}':
  512. (plot_residuals, ((proc, wp), layer, hit, var, subdet))})
  513. rel_layers = {1: [('BPIX', 1), ('BPIX', 2), ('FPIX', 1), ('FPIX', 2)],
  514. 2: [('BPIX', 2), ('BPIX', 3), ('FPIX', 2), ('FPIX', 3)],
  515. 3: [('BPIX', 3), ('BPIX', 4), ('FPIX', 3)], }
  516. for proc, hit, var in product(['zee', 'tt'], [1, 2, 3], ['dPhi', 'dRz']):
  517. figures.update({
  518. f'resall_H{hit}_{var}_{proc}': (plot_res_contour, (proc, hit, var, rel_layers[hit]))})
  519. for proc, wp, hit, var in product(['zee', 'tt'], ['new-default', 'new-wide'], [1, 2, 3], ['dPhi', 'dRz']):
  520. figures.update({
  521. f'res_v_eta_H{hit}_{var}_{proc}_{wp}': (plot_residuals_eta, ((proc, wp), hit, var))})
  522. mpb.render(figures, refresh=refresh)
  523. mpb.generate_report(figures, 'Electron Seeding Studies',
  524. output=f'hists.html',
  525. source=__file__)
  526. # mpb.generate_report(figures, 'Update',
  527. # output='report.html',
  528. # body='../docs/reports/report_2018_05_30.md')
  529. if publish:
  530. mpb.publish()
  531. def color(proc, wp):
  532. from matplotlib.colors import XKCD_COLORS
  533. def f(name):
  534. return XKCD_COLORS['xkcd:'+name]
  535. return {
  536. ('zee', 'new-extra-narrow'): f('kelly green'),
  537. ('tt', 'new-extra-narrow'): f('grey green'),
  538. ('zee', 'new-default'): f('red'),
  539. ('tt', 'new-default'): f('pink'),
  540. ('zee', 'new-wide'): f('strong blue'),
  541. ('tt', 'new-wide'): f('bright blue'),
  542. ('zee', 'new-extra-wide'): f('indigo'),
  543. ('tt', 'new-extra-wide'): f('bright purple'),
  544. ('zee', 'old-default'): f('black'),
  545. ('tt', 'old-default'): f('blue grey'),
  546. }[(proc, wp)]
  547. if __name__ == '__main__':
  548. set_defaults()
  549. mpb.configure(output_dir='seeding_studies',
  550. multiprocess=True,
  551. publish_remote="caleb@fangmeier.tech",
  552. publish_dir="/var/www/eg",
  553. publish_url="eg.fangmeier.tech/",
  554. )
  555. procs = {
  556. 'zee': r'$Z\rightarrow e^+e^-$',
  557. 'tt': r'$t\bar{t}$'
  558. }
  559. wps = {
  560. 'new-default': 'HLT Settings',
  561. 'new-wide': 'Wide Settings',
  562. 'old-default': 'Old Seeding',
  563. }
  564. all_cut_plots(refresh=True, publish=False)