plots.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #!/usr/bin/env python
  2. import sys
  3. import matplotlib.pyplot as plt
  4. from filval.result_set import ResultSet
  5. from filval.histogram_utils import hist, hist2d, hist_slice
  6. from filval.plotter import make_plot, plot_registry, hist_plot, hist2d_plot
  7. @make_plot(scale=0.85)
  8. def first_hits_v_eta(rs):
  9. r'''
  10. Plots of $\Delta \phi$, $\Delta z$, and $\Delta r$ vs $\eta$ between RecHit and SimHit for
  11. the first matched hit in the seed.
  12. '''
  13. plt.subplot(321)
  14. hist2d_plot(hist2d(rs.dphi_v_eta_first_hits_in_B1),
  15. ylabel=r"$\Delta \phi_1$(rad)",
  16. title="BPIX - Layer 1")
  17. plt.subplot(322)
  18. hist2d_plot(hist2d(rs.dphi_v_eta_first_hits_in_B2),
  19. title="BPIX - Layer 2")
  20. plt.subplot(323)
  21. hist2d_plot(hist2d(rs.dz_v_eta_first_hits_in_B1),
  22. ylabel=r"$\Delta \textrm{z}_1$(cm)",
  23. )
  24. plt.subplot(324)
  25. hist2d_plot(hist2d(rs.dz_v_eta_first_hits_in_B2))
  26. plt.subplot(325)
  27. hist2d_plot(hist2d(rs.dr_v_eta_first_hits_in_B1),
  28. ylabel=r"$\Delta r_1$(cm)",
  29. xlabel=r"$\eta$"
  30. )
  31. plt.subplot(326)
  32. hist2d_plot(hist2d(rs.dr_v_eta_first_hits_in_B2),
  33. xlabel=r"$\eta$"
  34. )
  35. @make_plot(scale=0.85)
  36. def second_hits_v_eta(rs):
  37. plt.subplot(321)
  38. hist2d_plot(hist2d(rs.dphi_v_eta_second_hits_in_B2),
  39. ylabel=r"$\Delta \phi_2$(rad)",
  40. title="BPIX - Layer 2")
  41. plt.subplot(322)
  42. hist2d_plot(hist2d(rs.dphi_v_eta_second_hits_in_B3),
  43. title="BPIX - Layer 3")
  44. plt.subplot(323)
  45. hist2d_plot(hist2d(rs.dz_v_eta_second_hits_in_B2),
  46. ylabel=r"$\Delta \textrm{z}_2$(cm)")
  47. plt.subplot(324)
  48. hist2d_plot(hist2d(rs.dz_v_eta_second_hits_in_B3))
  49. plt.subplot(325)
  50. hist2d_plot(hist2d(rs.dr_v_eta_second_hits_in_B2),
  51. ylabel=r"$\Delta r_2$(cm)",
  52. xlabel=r"$\eta$")
  53. plt.subplot(326)
  54. hist2d_plot(hist2d(rs.dr_v_eta_second_hits_in_B3),
  55. xlabel=r"$\eta$")
  56. @make_plot(scale=0.85)
  57. def first_hits(rs):
  58. plt.subplot(321)
  59. hist_plot(hist(rs.dphi_v_eta_first_hits_in_B1.ProjectionY()),
  60. include_errors=True, xlabel=r"$\Delta \phi_1$(rad)",
  61. title="BPIX - Layer 1")
  62. plt.subplot(322)
  63. hist_plot(hist(rs.dphi_v_eta_first_hits_in_B2.ProjectionY()),
  64. include_errors=True,
  65. xlabel=r"$\Delta \phi_1$(rad)",
  66. title="BPIX - Layer 2")
  67. plt.subplot(323)
  68. hist_plot(hist(rs.dz_v_eta_first_hits_in_B1.ProjectionY()),
  69. include_errors=True,
  70. xlabel=r"$\Delta z_1$(cm)")
  71. plt.subplot(324)
  72. hist_plot(hist(rs.dz_v_eta_first_hits_in_B2.ProjectionY()),
  73. include_errors=True,
  74. xlabel=r"$\Delta z_1$(cm)")
  75. plt.subplot(325)
  76. hist_plot(hist(rs.dr_v_eta_first_hits_in_B1.ProjectionY()),
  77. include_errors=True,
  78. xlabel=r"$\Delta r_1$(cm)")
  79. plt.subplot(326)
  80. hist_plot(hist(rs.dr_v_eta_first_hits_in_B2.ProjectionY()),
  81. include_errors=True,
  82. xlabel="r$\Delta r_1$(cm)")
  83. @make_plot(scale=0.85)
  84. def second_hits(rs):
  85. plt.subplot(321)
  86. hist_plot(hist(rs.dphi_v_eta_second_hits_in_B2.ProjectionY()),
  87. include_errors=True,
  88. xlabel=r"$\Delta \phi_2$(rad)",
  89. title="BPIX - Layer 2")
  90. plt.subplot(322)
  91. hist_plot(hist(rs.dphi_v_eta_second_hits_in_B3.ProjectionY()),
  92. include_errors=True,
  93. xlabel=r"$\Delta \phi_2$(rad)",
  94. title="BPIX - Layer 3")
  95. plt.subplot(323)
  96. hist_plot(hist(rs.dz_v_eta_second_hits_in_B2.ProjectionY()),
  97. include_errors=True,
  98. xlabel=r"$\Delta z_2$(cm)")
  99. plt.subplot(324)
  100. hist_plot(hist(rs.dz_v_eta_second_hits_in_B3.ProjectionY()),
  101. include_errors=True,
  102. xlabel=r"$\Delta z_2$(cm)")
  103. plt.subplot(325)
  104. hist_plot(hist(rs.dr_v_eta_second_hits_in_B2.ProjectionY()),
  105. include_errors=True,
  106. xlabel=r"$\Delta r_2$(cm)")
  107. plt.subplot(326)
  108. hist_plot(hist(rs.dr_v_eta_second_hits_in_B3.ProjectionY()),
  109. include_errors=True,
  110. xlabel=r"$\Delta r_2$(cm)")
  111. @make_plot(scale=0.85)
  112. def delta_phi_z_v_ladder(rs):
  113. def even_odd_plot(even, odd, var, scale, unit, **kwargs):
  114. even_dist = even.ProjectionY()
  115. odd_dist = odd.ProjectionY()
  116. hist_plot(hist(even_dist), include_errors=True, label="even ladders")
  117. hist_plot(hist(odd_dist), include_errors=True, color='r', label="odd ladders",
  118. **kwargs)
  119. even_mean = even_dist.GetMean()*scale
  120. odd_mean = odd_dist.GetMean()*scale
  121. axes = plt.gca()
  122. txt = r"$ \hat{{\Delta {0} }}_{{1,\textrm{{ {1} }} }}={2:4.2g}${3}"
  123. axes.text(0.05, .7, txt.format(var, "odd", odd_mean, unit), transform=axes.transAxes)
  124. axes.text(0.05, .6, txt.format(var, "even", even_mean, unit), transform=axes.transAxes)
  125. plt.subplot(221)
  126. even_odd_plot(rs.dphi_v_eta_first_hits_in_B1_even_ladder, rs.dphi_v_eta_first_hits_in_B1_odd_ladder,
  127. r"\phi", 10**6, "urad", xlabel=r"$\Delta \phi_1$(rad)", title="BPIX - Layer 1")
  128. plt.subplot(222)
  129. even_odd_plot(rs.dphi_v_eta_first_hits_in_B2_even_ladder, rs.dphi_v_eta_first_hits_in_B2_odd_ladder,
  130. r"\phi", 10**6, "urad", xlabel=r"$\Delta \phi_1$(rad)", title="BPIX - Layer 2")
  131. plt.legend()
  132. plt.subplot(223)
  133. even_odd_plot(rs.dz_v_eta_first_hits_in_B1_even_ladder, rs.dz_v_eta_first_hits_in_B1_odd_ladder,
  134. "z", 10**4, "um", xlabel=r"$\Delta z_1$(cm)")
  135. plt.subplot(224)
  136. even_odd_plot(rs.dz_v_eta_first_hits_in_B2_even_ladder, rs.dz_v_eta_first_hits_in_B2_odd_ladder,
  137. "z", 10**4, "um", xlabel=r"$\Delta z_1$(cm)")
  138. @make_plot(scale=0.85)
  139. def sc_extrapolation_first(rs):
  140. ''' Raphael's plots '''
  141. norm = 1
  142. errors = True
  143. def preproc(h):
  144. return hist_slice(hist(h.ProjectionY()), (-0.07, 0.07))
  145. plt.subplot(221)
  146. hist_plot(hist(rs.sc_first_hits_in_B1_dz.ProjectionY()),
  147. include_errors=errors,
  148. norm=norm,
  149. log=False,
  150. xlabel=r"$\Delta z_1$(cm)",
  151. title="BPIX - Layer 1")
  152. plt.subplot(222)
  153. hist_plot(hist(rs.sc_first_hits_in_B2_dz.ProjectionY()),
  154. include_errors=errors,
  155. norm=norm,
  156. log=False,
  157. xlabel=r"$\Delta z_1$(cm)",
  158. title="BPIX - Layer 2")
  159. plt.subplot(223)
  160. hist_plot(preproc(rs.sc_first_hits_in_B1_dphi),
  161. include_errors=errors,
  162. norm=norm,
  163. log=False,
  164. # ylim=(0.5E-3, 5),
  165. xlabel=r"$\Delta \phi_1$(rad)",
  166. # xlim=(-0.06, 0.06)
  167. )
  168. plt.subplot(224)
  169. hist_plot(preproc(rs.sc_first_hits_in_B2_dphi),
  170. include_errors=errors,
  171. norm=norm,
  172. log=False,
  173. # ylim=(0.5E-3, 5),
  174. xlabel=r"$\Delta \phi_1$(rad)",
  175. # xlim=(-0.06, 0.06)
  176. )
  177. @make_plot(scale=0.85)
  178. def sc_extrapolation_second(rs):
  179. from scipy.stats import norm
  180. def gauss(x, A, mu, sigma):
  181. return A*norm.pdf(x, mu, sigma)
  182. def gauss2(x, A1, mu1, sigma1, A2, mu2, sigma2):
  183. return (A1*norm.pdf(x, mu1, sigma1) +
  184. A2*norm.pdf(x, mu2, sigma2))
  185. integral = 1
  186. errors = True
  187. def preproc(h, slice_=None):
  188. h = hist(h.ProjectionY())
  189. if slice_:
  190. h = hist_slice(h, slice_)
  191. return h
  192. plt.subplot(221)
  193. hist_plot(preproc(rs.sc_second_hits_in_B2_dz),
  194. include_errors=errors,
  195. norm=integral,
  196. log=False,
  197. fit=(gauss2, (1, 0, 0.025, 1, 0, 0.005)),
  198. xlabel=r"$\Delta z_2$(cm)",
  199. title="BPIX - Layer 2")
  200. plt.subplot(222)
  201. hist_plot(preproc(rs.sc_second_hits_in_B3_dz),
  202. include_errors=errors,
  203. norm=integral,
  204. log=False,
  205. fit=(gauss2, (1, 0, 0.025, 1, 0, 0.005)),
  206. xlabel=r"$\Delta z_2$(cm)",
  207. title="BPIX - Layer 3")
  208. plt.subplot(223)
  209. hist_plot(preproc(rs.sc_second_hits_in_B2_dphi, (-0.09, 0.09)),
  210. include_errors=errors,
  211. norm=integral,
  212. log=False,
  213. fit=(gauss2, (1, 0, 0.015, 1, 0, 0.005)),
  214. xlabel=r"$\Delta \phi_2$(rad)")
  215. plt.subplot(224)
  216. hist_plot(preproc(rs.sc_second_hits_in_B3_dphi, (-0.09, 0.09)),
  217. include_errors=errors,
  218. norm=integral,
  219. log=False,
  220. fit=(gauss2, (1, 0, 0.025, 1, 0, 0.005)),
  221. xlabel=r"$\Delta \phi_2$(rad)")
  222. def generate_dashboard():
  223. from jinja2 import Environment, PackageLoader, select_autoescape
  224. from os.path import join
  225. from urllib.parse import quote
  226. env = Environment(
  227. loader=PackageLoader('plots', 'templates'),
  228. autoescape=select_autoescape(['htm', 'html', 'xml'])
  229. )
  230. def render_to_file(template_name, **kwargs):
  231. with open(join('output', template_name), 'w') as tempout:
  232. template = env.get_template(template_name)
  233. tempout.write(template.render(**kwargs))
  234. def get_by_n(l, n=2):
  235. l = list(l)
  236. while l:
  237. yield l[:n]
  238. l = l[n:]
  239. render_to_file('dashboard.htm', plots=get_by_n(plot_registry.values(), 3),
  240. quote=quote)
  241. if __name__ == '__main__':
  242. # First create a ResultSet object which loads all of the objects from output.root
  243. # into memory and makes them available as attributes
  244. if len(sys.argv) != 2:
  245. raise ValueError("please supply root file")
  246. rs = ResultSet("DY2LL", sys.argv[1])
  247. first_hits_v_eta(rs)
  248. second_hits_v_eta(rs)
  249. first_hits(rs)
  250. second_hits(rs)
  251. delta_phi_z_v_ladder(rs)
  252. sc_extrapolation_first(rs)
  253. sc_extrapolation_second(rs)
  254. generate_dashboard()