eff_plots.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_utils import hist, hist_add, hist_normalize, hist_scale, hist2d
  6. from filval.plotter import (decl_plot, render_plots, hist_plot, hist2d_plot,
  7. hist_plot_stack, Plot, generate_dashboard)
  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_seed_eff(old_seeds, new_seeds):
  14. r"""## ECAL-Driven Seeding Efficiency
  15. The proportion of gen-level electrons originating in the luminous region that have
  16. an associated Seed, matched via rechit-simhit associations in the pixel detector. Cuts are on simtrack quantities.
  17. """
  18. ax_pt = plt.subplot(221)
  19. ax_eta = plt.subplot(222)
  20. ax_phi = plt.subplot(223)
  21. errors = True
  22. plt.sca(ax_pt)
  23. hist_plot(hist(new_seeds.seed_eff_v_pt), include_errors=errors, label='New')
  24. hist_plot(hist(old_seeds.seed_eff_v_pt), include_errors=errors, label='Old')
  25. center_text(0.5, 0.3, r'$|\eta|<2.4$')
  26. plt.xlabel(r"Sim-Track $p_T$")
  27. plt.ylim((0, 1.1))
  28. plt.sca(ax_eta)
  29. hist_plot(hist(new_seeds.seed_eff_v_eta), include_errors=errors, label='New')
  30. hist_plot(hist(old_seeds.seed_eff_v_eta), include_errors=errors, label='Old')
  31. center_text(0.5, 0.3, r'$p_T>20$')
  32. plt.xlabel(r"Sim-Track $\eta$")
  33. plt.ylim((0, 1.1))
  34. plt.legend()
  35. plt.sca(ax_phi)
  36. hist_plot(hist(new_seeds.seed_eff_v_phi), include_errors=errors, label='New')
  37. hist_plot(hist(old_seeds.seed_eff_v_phi), include_errors=errors, label='Old')
  38. center_text(0.5, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  39. plt.xlabel(r"Sim-Track $\phi$")
  40. plt.ylim((0, 1.1))
  41. @decl_plot
  42. def plot_track_eff(old_seeds, new_seeds):
  43. r"""## GSF Tracking Efficiency
  44. The proportion of electrons origination in the luminous region from the that have
  45. an associated GSF track. Cuts are on simtrack quantities.
  46. """
  47. ax_pt = plt.subplot(221)
  48. ax_eta = plt.subplot(222)
  49. ax_phi = plt.subplot(223)
  50. errors = True
  51. plt.sca(ax_pt)
  52. hist_plot(hist(new_seeds.track_eff_v_pt), include_errors=errors, label='New')
  53. hist_plot(hist(old_seeds.track_eff_v_pt), include_errors=errors, label='Old')
  54. center_text(0.5, 0.3, r'$|\eta|<2.4$')
  55. plt.xlabel(r"Sim-Track $p_T$")
  56. plt.ylim((0, 1.1))
  57. plt.sca(ax_eta)
  58. hist_plot(hist(new_seeds.track_eff_v_eta), include_errors=errors, label='New')
  59. hist_plot(hist(old_seeds.track_eff_v_eta), include_errors=errors, label='Old')
  60. center_text(0.5, 0.3, r'$p_T>20$')
  61. plt.xlabel(r"Sim-Track $\eta$")
  62. plt.ylim((0, 1.1))
  63. plt.legend()
  64. plt.sca(ax_phi)
  65. hist_plot(hist(new_seeds.track_eff_v_phi), include_errors=errors, label='New')
  66. hist_plot(hist(old_seeds.track_eff_v_phi), include_errors=errors, label='Old')
  67. center_text(0.5, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  68. plt.xlabel(r"Sim-Track $\phi$")
  69. plt.ylim((0, 1.1))
  70. @decl_plot
  71. def plot_seed_purity(old_seeds, new_seeds, ext=""):
  72. r"""## ECAL-Driven Seed Purity
  73. The proportion of ECAL-driven seeds that have a matched gen-level electron originating in
  74. the luminous region. Cuts are on seed quantities.
  75. """
  76. ax_pt = plt.subplot(221)
  77. ax_eta = plt.subplot(222)
  78. ax_phi = plt.subplot(223)
  79. def get_hist(rs, base_name):
  80. return hist(getattr(rs, base_name+ext))
  81. errors = True
  82. plt.sca(ax_pt)
  83. hist_plot(get_hist(new_seeds, "seed_pur_v_pt"), include_errors=errors, label='New')
  84. hist_plot(get_hist(old_seeds, "seed_pur_v_pt"), include_errors=errors, label='Old')
  85. center_text(0.5, 0.3, r'$|\eta|<2.4$')
  86. plt.xlabel(r"Seed $p_T$")
  87. if not ext:
  88. plt.ylim((0, 1.1))
  89. plt.sca(ax_eta)
  90. hist_plot(get_hist(new_seeds, "seed_pur_v_eta"), include_errors=errors, label='New')
  91. hist_plot(get_hist(old_seeds, "seed_pur_v_eta"), include_errors=errors, label='Old')
  92. center_text(0.5, 0.3, r'$p_T>20$')
  93. plt.xlabel(r"Seed $\eta$")
  94. if not ext:
  95. plt.ylim((0, 1.1))
  96. plt.legend()
  97. plt.sca(ax_phi)
  98. hist_plot(get_hist(new_seeds, "seed_pur_v_phi"), include_errors=errors, label='New')
  99. hist_plot(get_hist(old_seeds, "seed_pur_v_phi"), include_errors=errors, label='Old')
  100. center_text(0.5, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  101. plt.xlabel(r"Seed $\phi$")
  102. if not ext:
  103. plt.ylim((0, 1.1))
  104. @decl_plot
  105. def plot_track_purity(old_seeds, new_seeds, ext=""):
  106. r"""## GSF Track Purity
  107. The proportion of GSF-tracks w\ ECAL-driven seeds that have a matched gen-level electron originating in
  108. the luminous region. Cuts are on GSF track quantities.
  109. """
  110. ax_pt = plt.subplot(221)
  111. ax_eta = plt.subplot(222)
  112. ax_phi = plt.subplot(223)
  113. def get_hist(rs, base_name):
  114. return hist(getattr(rs, base_name+ext))
  115. errors = True
  116. plt.sca(ax_pt)
  117. hist_plot(get_hist(new_seeds, "track_pur_v_pt"), include_errors=errors, label='New')
  118. hist_plot(get_hist(old_seeds, "track_pur_v_pt"), include_errors=errors, label='Old')
  119. center_text(0.5, 0.3, r'$|\eta|<2.4$')
  120. plt.xlabel(r"GSF-Track $p_T$")
  121. if not ext:
  122. plt.ylim((0, 1.1))
  123. plt.sca(ax_eta)
  124. hist_plot(get_hist(new_seeds, "track_pur_v_eta"), include_errors=errors, label='New')
  125. hist_plot(get_hist(old_seeds, "track_pur_v_eta"), include_errors=errors, label='Old')
  126. center_text(0.5, 0.3, r'$p_T>20$')
  127. plt.xlabel(r"GSF-Track $\eta$")
  128. if not ext:
  129. plt.ylim((0, 1.1))
  130. plt.legend()
  131. plt.sca(ax_phi)
  132. hist_plot(get_hist(new_seeds, "track_pur_v_phi"), include_errors=errors, label='New')
  133. hist_plot(get_hist(old_seeds, "track_pur_v_phi"), include_errors=errors, label='Old')
  134. center_text(0.5, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  135. plt.xlabel(r"GSF-Track $\phi$")
  136. if not ext:
  137. plt.ylim((0, 1.1))
  138. @decl_plot
  139. def plot_nhits(old_seeds, new_seeds):
  140. ax_sim = plt.subplot(211)
  141. ax_rec_new = plt.subplot(223)
  142. ax_rec_old = plt.subplot(224)
  143. def _plot(histo):
  144. hit_hist2d = hist2d(histo)
  145. hist2d_plot(hit_hist2d)
  146. weights, _, xs, ys = hit_hist2d
  147. hits = (ys[1:, :-1] + ys[:-1, :-1])/2
  148. etas = (xs[0, :-1] + xs[0, 1:])/2
  149. avg_hits = np.ma.average(hits, axis=0, weights=weights)
  150. plt.plot(etas, avg_hits, 'wo', label=r'Average in $\eta$ bin')
  151. plt.xlabel(r'Track $\eta$')
  152. plt.ylabel(r'\# of pixel layers with Hit')
  153. plt.sca(ax_sim)
  154. _plot(new_seeds.simpixlay_v_eta)
  155. plt.legend()
  156. plt.colorbar()
  157. center_text(0.5, 0.1, "Sim Tracks", bbox=dict(facecolor='white', alpha=0.5))
  158. plt.sca(ax_rec_old)
  159. _plot(old_seeds.recpixlay_v_eta)
  160. plt.colorbar()
  161. center_text(0.5, 0.1, "GSF-Tracks (Old Seeding)", bbox=dict(facecolor='white', alpha=0.5))
  162. plt.sca(ax_rec_new)
  163. _plot(new_seeds.recpixlay_v_eta)
  164. plt.colorbar()
  165. center_text(0.5, 0.1, "GSF-Tracks (New Seeding)", bbox=dict(facecolor='white', alpha=0.5))
  166. if __name__ == '__main__':
  167. old_seeds = ResultSet("old_seeds", 'old_seeding2.root')
  168. new_seeds = ResultSet("new_seeds", 'new_seeding2.root')
  169. # Next, declare all of the (sub)plots that will be assembled into full
  170. # figures later
  171. seed_eff = plot_seed_eff, (old_seeds, new_seeds)
  172. track_eff = plot_track_eff, (old_seeds, new_seeds)
  173. seed_pur = plot_seed_purity, (old_seeds, new_seeds)
  174. seed_pur_num = plot_seed_purity, (old_seeds, new_seeds), {'ext': '_num'}
  175. seed_pur_den = plot_seed_purity, (old_seeds, new_seeds), {'ext': '_den'}
  176. track_pur = plot_track_purity, (old_seeds, new_seeds)
  177. track_pur_num = plot_track_purity, (old_seeds, new_seeds), {'ext': '_num'}
  178. track_pur_den = plot_track_purity, (old_seeds, new_seeds), {'ext': '_den'}
  179. nHits = (plot_nhits, (old_seeds, new_seeds), {})
  180. # Now assemble the plots into figures.
  181. plots = [
  182. Plot([[seed_eff]],
  183. 'ECAL-Driven Seeding Efficiency'),
  184. Plot([[seed_pur]],
  185. 'ECAL-Driven Seed Purity'),
  186. Plot([[seed_pur_num]],
  187. 'ECAL-Driven Seed Purity Numerator'),
  188. Plot([[seed_pur_den]],
  189. 'ECAL-Driven Seed Purity Denominator'),
  190. Plot([[track_eff]],
  191. 'GSF Tracking Efficiency'),
  192. Plot([[track_pur]],
  193. 'GSF Track Purity'),
  194. Plot([[track_pur_num]],
  195. 'GSF Track Purity Numerator'),
  196. Plot([[track_pur_den]],
  197. 'GSF Track Purity Denominator'),
  198. Plot([[nHits]],
  199. 'Hits'),
  200. ]
  201. # Finally, render and save the plots and generate the html+bootstrap
  202. # dashboard to view them
  203. render_plots(plots, to_disk=False)
  204. generate_dashboard(plots, 'Seeding Efficiency', output='eff_plots.html', source_file=__file__)