eff_plots.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/env python
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from filval.result_set import ResultSet
  5. from filval.histogram import hist, hist_add, hist_norm, hist_scale, hist2d
  6. from filval.plotting import (decl_plot, render_plots, hist_plot, hist2d_plot,
  7. Plot, generate_dashboard, simple_plot)
  8. def center_text(x, y, txt, **kwargs):
  9. plt.text(x, y, txt,
  10. horizontalalignment='center', verticalalignment='center',
  11. transform=plt.gca().transAxes, **kwargs)
  12. @decl_plot
  13. def plot_residuals(rs, layer, hit, variable, plot_cuts=True):
  14. matching_cuts = [
  15. dict(
  16. dPhiMaxHighEt=0.05,
  17. dPhiMaxHighEtThres=20.0,
  18. dPhiMaxLowEtGrad=-0.002,
  19. dRzMaxHighEt=9999.0,
  20. dRzMaxHighEtThres=0.0,
  21. dRzMaxLowEtGrad=0.0,
  22. ),
  23. dict(
  24. dPhiMaxHighEt=0.003,
  25. dPhiMaxHighEtThres=0.0,
  26. dPhiMaxLowEtGrad=0.0,
  27. dRzMaxHighEt=0.05,
  28. dRzMaxHighEtThres=30.0,
  29. dRzMaxLowEtGrad=-0.002,
  30. ),
  31. dict(
  32. dPhiMaxHighEt=0.003,
  33. dPhiMaxHighEtThres=0.0,
  34. dPhiMaxLowEtGrad=0.0,
  35. dRzMaxHighEt=0.05,
  36. dRzMaxHighEtThres=30.0,
  37. dRzMaxLowEtGrad=-0.002,
  38. )
  39. ]
  40. h = hist2d(getattr(rs, f'{variable}_BPIX_L{layer}_H{hit}_v_Et'))
  41. def calc_window(et):
  42. idx = min(hit-1, 2)
  43. high_et = matching_cuts[idx][f'{variable}MaxHighEt']
  44. high_et_thres = matching_cuts[idx][f'{variable}MaxHighEtThres']
  45. low_et_grad = matching_cuts[idx][f'{variable}MaxLowEtGrad']
  46. return high_et + min(0, et-high_et_thres)*low_et_grad
  47. hist2d_plot(h, colorbar=True)
  48. if plot_cuts:
  49. ets = h[3][:, 0]
  50. cuts = [calc_window(et) for et in ets]
  51. plt.plot(cuts, ets, color='red', label='Cut Value')
  52. plt.legend()
  53. plt.xlabel({'dPhi': r'$\delta \phi$ (rads)',
  54. 'dRz': r'$\delta R/z$ (cm)'}[variable])
  55. plt.ylabel('$E_T$ (GeV)')
  56. @decl_plot
  57. def plot_seed_eff(rs):
  58. r"""## ECAL-Driven Seeding Efficiency
  59. The proportion of gen-level electrons originating in the luminous region that have
  60. an associated Seed, matched via rechit-simhit associations in the pixel detector. Cuts are on simtrack quantities.
  61. """
  62. ax_pt = plt.subplot(221)
  63. ax_eta = plt.subplot(222)
  64. ax_phi = plt.subplot(223)
  65. errors = True
  66. plt.sca(ax_pt)
  67. hist_plot(hist(rs.seed_eff_v_pt), include_errors=errors)
  68. center_text(0.5, 0.3, r'$|\eta|<2.4$')
  69. plt.xlabel(r"Sim-Track $p_T$")
  70. plt.ylim((0, 1.1))
  71. plt.sca(ax_eta)
  72. hist_plot(hist(rs.seed_eff_v_eta), include_errors=errors)
  73. center_text(0.5, 0.3, r'$p_T>20$')
  74. plt.xlabel(r"Sim-Track $\eta$")
  75. plt.ylim((0, 1.1))
  76. plt.sca(ax_phi)
  77. hist_plot(hist(rs.seed_eff_v_phi), include_errors=errors)
  78. center_text(0.5, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  79. plt.xlabel(r"Sim-Track $\phi$")
  80. plt.ylim((0, 1.1))
  81. @decl_plot
  82. def plot_tracking_eff(rs):
  83. r"""## GSF Tracking Efficiency
  84. The proportion of electrons origination in the luminous region from the that have
  85. an associated GSF track. Cuts are on simtrack quantities.
  86. """
  87. ax_pt = plt.subplot(221)
  88. ax_eta = plt.subplot(222)
  89. ax_phi = plt.subplot(223)
  90. errors = True
  91. plt.sca(ax_pt)
  92. hist_plot(hist(rs.tracking_eff_v_pt), include_errors=errors)
  93. center_text(0.5, 0.3, r'$|\eta|<2.4$')
  94. plt.xlabel(r"Sim-Track $p_T$")
  95. # plt.ylim((0, 1.1))
  96. plt.sca(ax_eta)
  97. hist_plot(hist(rs.tracking_eff_v_eta), include_errors=errors)
  98. center_text(0.5, 0.3, r'$p_T>20$')
  99. plt.xlabel(r"Sim-Track $\eta$")
  100. # plt.ylim((0, 1.1))
  101. plt.sca(ax_phi)
  102. hist_plot(hist(rs.tracking_eff_v_phi), include_errors=errors)
  103. center_text(0.5, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  104. plt.xlabel(r"Sim-Track $\phi$")
  105. # plt.ylim((0, 1.1))
  106. @decl_plot
  107. def plot_seed_purity(rs, ext=""):
  108. r"""## ECAL-Driven Seed Purity
  109. The proportion of ECAL-driven seeds that have a matched gen-level electron originating in
  110. the luminous region. Cuts are on seed quantities.
  111. """
  112. ax_pt = plt.subplot(221)
  113. ax_eta = plt.subplot(222)
  114. ax_phi = plt.subplot(223)
  115. def get_hist(base_name):
  116. return hist(getattr(rs, base_name+ext))
  117. errors = True
  118. plt.sca(ax_pt)
  119. hist_plot(get_hist("seed_pur_v_pt"), include_errors=errors)
  120. center_text(0.5, 0.3, r'$|\eta|<2.4$')
  121. plt.xlabel(r"Seed $p_T$")
  122. if not ext:
  123. plt.ylim((0, 1.1))
  124. plt.sca(ax_eta)
  125. hist_plot(get_hist("seed_pur_v_eta"), include_errors=errors)
  126. center_text(0.5, 0.3, r'$p_T>20$')
  127. plt.xlabel(r"Seed $\eta$")
  128. if not ext:
  129. plt.ylim((0, 1.1))
  130. plt.sca(ax_phi)
  131. hist_plot(get_hist("seed_pur_v_phi"), include_errors=errors)
  132. center_text(0.5, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  133. plt.xlabel(r"Seed $\phi$")
  134. if not ext:
  135. plt.ylim((0, 1.1))
  136. @decl_plot
  137. def plot_track_purity(rs, ext=""):
  138. r"""## GSF Track Purity
  139. The proportion of GSF-tracks w\ ECAL-driven seeds that have a matched gen-level electron originating in
  140. the luminous region. Cuts are on GSF track quantities.
  141. """
  142. ax_pt = plt.subplot(221)
  143. ax_eta = plt.subplot(222)
  144. ax_phi = plt.subplot(223)
  145. def get_hist( base_name):
  146. return hist(getattr(rs, base_name+ext))
  147. errors = True
  148. plt.sca(ax_pt)
  149. hist_plot(get_hist("tracking_pur_v_pt"), include_errors=errors)
  150. center_text(0.5, 0.3, r'$|\eta|<2.4$')
  151. plt.xlabel(r"GSF-Track $p_T$")
  152. if not ext:
  153. plt.ylim((0, 1.1))
  154. plt.sca(ax_eta)
  155. hist_plot(get_hist("tracking_pur_v_eta"), include_errors=errors)
  156. center_text(0.5, 0.3, r'$p_T>20$')
  157. plt.xlabel(r"GSF-Track $\eta$")
  158. if not ext:
  159. plt.ylim((0, 1.1))
  160. plt.sca(ax_phi)
  161. hist_plot(get_hist("tracking_pur_v_phi"), include_errors=errors)
  162. center_text(0.5, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  163. plt.xlabel(r"GSF-Track $\phi$")
  164. if not ext:
  165. plt.ylim((0, 1.1))
  166. @decl_plot
  167. def plot_hit_vs_layer(rs, region):
  168. h = hist2d(getattr(rs, f'hit_vs_layer_{region}'))
  169. hist2d_plot(h, txt_format='{:2.0f}')
  170. plt.xlabel('Layer \#')
  171. plt.ylabel('Hit \#')
  172. if __name__ == '__main__':
  173. rs = ResultSet('seeds', '../hists/new_seeding.root')
  174. # Next, declare all of the (sub)plots that will be assembled into full
  175. # figures later
  176. seed_eff = plot_seed_eff, (rs, )
  177. tracking_eff = plot_tracking_eff, (rs,)
  178. seed_pur = plot_seed_purity, rs
  179. seed_pur_num = plot_seed_purity, (rs,), {'ext': '_num'}
  180. seed_pur_den = plot_seed_purity, (rs,), {'ext': '_den'}
  181. track_pur = plot_track_purity, (rs,)
  182. track_pur_num = plot_track_purity, (rs,), {'ext': '_num'}
  183. track_pur_den = plot_track_purity, (rs,), {'ext': '_den'}
  184. residuals_L1_H1_dPhi = plot_residuals, (rs, 1, 1, 'dPhi')
  185. residuals_L2_H2_dPhi = plot_residuals, (rs, 2, 2, 'dPhi')
  186. residuals_L3_H3_dPhi = plot_residuals, (rs, 3, 3, 'dPhi')
  187. residuals_L1_H1_dRz = plot_residuals, (rs, 1, 1, 'dRz')
  188. residuals_L1_H1_dRz_no_cuts = plot_residuals, (rs, 1, 1, 'dRz'), {'plot_cuts': False}
  189. residuals_L2_H2_dRz = plot_residuals, (rs, 2, 2, 'dRz')
  190. residuals_L3_H3_dRz = plot_residuals, (rs, 3, 3, 'dRz')
  191. hit_vs_layer_barrel = plot_hit_vs_layer, (rs, 'barrel')
  192. hit_vs_layer_forward = plot_hit_vs_layer, (rs, 'forward')
  193. # Now assemble the plots into figures.
  194. plots = [
  195. Plot(residuals_L1_H1_dPhi, 'Phi Residuals Layer 1 Hit 1'),
  196. Plot(residuals_L2_H2_dPhi, 'Phi Residuals Layer 2 Hit 2'),
  197. Plot(residuals_L3_H3_dPhi, 'Phi Residuals Layer 3 Hit 3'),
  198. Plot(residuals_L1_H1_dRz, 'dZ Residuals Layer 1 Hit 1'),
  199. Plot(residuals_L1_H1_dRz_no_cuts, 'dZ Residuals Layer 1 Hit 1 w/o cuts'),
  200. Plot(residuals_L2_H2_dRz, 'dZ Residuals Layer 2 Hit 2'),
  201. Plot(residuals_L3_H3_dRz, 'dZ Residuals Layer 3 Hit 3'),
  202. Plot(seed_eff, 'ECAL-Driven Seeding Efficiency'),
  203. Plot(tracking_eff, 'GSF Tracking Efficiency'),
  204. Plot(hit_vs_layer_barrel, 'Hit vs Layer - Barrel'),
  205. Plot(hit_vs_layer_forward, 'Hit vs Layer - Forward'),
  206. Plot(track_pur, 'GSF Track Purity'),
  207. Plot(track_pur_num, 'GSF Track Purity Numerator'),
  208. Plot(track_pur_den, 'GSF Track Purity Denominator'),
  209. ]
  210. # Finally, render and save the plots and generate the html+bootstrap
  211. # dashboard to view them
  212. render_plots(plots, to_disk=False)
  213. generate_dashboard(plots, 'Seeding Efficiency',
  214. output='eff_plots.html',
  215. source=__file__,
  216. config=rs.config)