eff_plots.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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):
  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. errors = True
  80. plt.sca(ax_pt)
  81. hist_plot(hist(new_seeds.seed_pur_v_pt), include_errors=errors, label='New')
  82. hist_plot(hist(old_seeds.seed_pur_v_pt), include_errors=errors, label='Old')
  83. center_text(0.5, 0.3, r'$|\eta|<2.4$')
  84. plt.xlabel(r"Seed $p_T$")
  85. plt.ylim((0, 1.1))
  86. plt.sca(ax_eta)
  87. hist_plot(hist(new_seeds.seed_pur_v_eta), include_errors=errors, label='New')
  88. hist_plot(hist(old_seeds.seed_pur_v_eta), include_errors=errors, label='Old')
  89. center_text(0.5, 0.3, r'$p_T>20$')
  90. plt.xlabel(r"Seed $\eta$")
  91. plt.ylim((0, 1.1))
  92. plt.legend()
  93. plt.sca(ax_phi)
  94. hist_plot(hist(new_seeds.seed_pur_v_phi), include_errors=errors, label='New')
  95. hist_plot(hist(old_seeds.seed_pur_v_phi), include_errors=errors, label='Old')
  96. center_text(0.5, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  97. plt.xlabel(r"Seed $\phi$")
  98. plt.ylim((0, 1.1))
  99. @decl_plot
  100. def plot_track_purity(old_seeds, new_seeds):
  101. r"""## GSF Track Purity
  102. The proportion of GSF-tracks w\ ECAL-driven seeds that have a matched gen-level electron originating in
  103. the luminous region. Cuts are on GSF track quantities.
  104. """
  105. ax_pt = plt.subplot(221)
  106. ax_eta = plt.subplot(222)
  107. ax_phi = plt.subplot(223)
  108. errors = True
  109. plt.sca(ax_pt)
  110. hist_plot(hist(new_seeds.track_pur_v_pt), include_errors=errors, label='New')
  111. hist_plot(hist(old_seeds.track_pur_v_pt), include_errors=errors, label='Old')
  112. center_text(0.5, 0.3, r'$|\eta|<2.4$')
  113. plt.xlabel(r"GSF-Track $p_T$")
  114. plt.ylim((0, 1.1))
  115. plt.sca(ax_eta)
  116. hist_plot(hist(new_seeds.track_pur_v_eta), include_errors=errors, label='New')
  117. hist_plot(hist(old_seeds.track_pur_v_eta), include_errors=errors, label='Old')
  118. center_text(0.5, 0.3, r'$p_T>20$')
  119. plt.xlabel(r"GSF-Track $\eta$")
  120. plt.ylim((0, 1.1))
  121. plt.legend()
  122. plt.sca(ax_phi)
  123. hist_plot(hist(new_seeds.track_pur_v_phi), include_errors=errors, label='New')
  124. hist_plot(hist(old_seeds.track_pur_v_phi), include_errors=errors, label='Old')
  125. center_text(0.5, 0.3, r'$p_T>20$ and $|\eta|<2.4$')
  126. plt.xlabel(r"GSF-Track $\phi$")
  127. plt.ylim((0, 1.1))
  128. @decl_plot
  129. def plot_nhits(old_seeds, new_seeds):
  130. ax_sim = plt.subplot(211)
  131. ax_rec_new = plt.subplot(223)
  132. ax_rec_old = plt.subplot(224)
  133. def _plot(histo):
  134. hit_hist2d = hist2d(histo)
  135. hist2d_plot(hit_hist2d)
  136. weights, _, xs, ys = hit_hist2d
  137. hits = (ys[1:, :-1] + ys[:-1, :-1])/2
  138. etas = (xs[0, :-1] + xs[0, 1:])/2
  139. avg_hits = np.ma.average(hits, axis=0, weights=weights)
  140. plt.plot(etas, avg_hits, 'wo', label=r'Average in $\eta$ bin')
  141. plt.xlabel(r'Track $\eta$')
  142. plt.ylabel(r'\# of pixel layers with Hit')
  143. plt.sca(ax_sim)
  144. _plot(new_seeds.simpixlay_v_eta)
  145. plt.legend()
  146. plt.colorbar()
  147. center_text(0.5, 0.1, "Sim Tracks", bbox=dict(facecolor='white', alpha=0.5))
  148. plt.sca(ax_rec_old)
  149. _plot(old_seeds.recpixlay_v_eta)
  150. plt.colorbar()
  151. center_text(0.5, 0.1, "GSF-Tracks (Old Seeding)", bbox=dict(facecolor='white', alpha=0.5))
  152. plt.sca(ax_rec_new)
  153. _plot(new_seeds.recpixlay_v_eta)
  154. plt.colorbar()
  155. center_text(0.5, 0.1, "GSF-Tracks (New Seeding)", bbox=dict(facecolor='white', alpha=0.5))
  156. if __name__ == '__main__':
  157. old_seeds = ResultSet("old_seeds", 'old_seeding.root')
  158. new_seeds = ResultSet("new_seeds", 'new_seeding.root')
  159. # Next, declare all of the (sub)plots that will be assembled into full
  160. # figures later
  161. seed_eff = (plot_seed_eff, (old_seeds, new_seeds), {})
  162. track_eff = (plot_track_eff, (old_seeds, new_seeds), {})
  163. seed_pur = (plot_seed_purity, (old_seeds, new_seeds), {})
  164. track_pur = (plot_track_purity, (old_seeds, new_seeds), {})
  165. nHits = (plot_nhits, (old_seeds, new_seeds), {})
  166. # Now assemble the plots into figures.
  167. plots = [
  168. Plot([[seed_eff]],
  169. 'ECAL-Driven Seeding Efficiency'),
  170. Plot([[seed_pur]],
  171. 'ECAL-Driven Seed Purity'),
  172. Plot([[track_eff]],
  173. 'GSF Tracking Efficiency'),
  174. Plot([[track_pur]],
  175. 'GSF Track Purity'),
  176. Plot([[nHits]],
  177. 'Hits'),
  178. ]
  179. # Finally, render and save the plots and generate the html+bootstrap
  180. # dashboard to view them
  181. render_plots(plots, to_disk=False)
  182. generate_dashboard(plots, 'Seeding Efficiency', output='eff_plots.html', source_file=__file__)