plots.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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, hist_add
  6. from filval.plotter import (decl_plot, save_plots, hist_plot, hist2d_plot, Plot)
  7. @decl_plot
  8. def plot_residual(rs, var, layer, hit, projection=None, display_layer=True):
  9. r'''
  10. Plots of $\Delta \phi$, $\Delta z$, or $\Delta r$ (vs $\eta$) between RecHit
  11. and SimHit for the nth hit in the matched seed.
  12. '''
  13. h = {('z', 'B1', 1): rs.dz_v_eta_first_hits_in_B1,
  14. ('z', 'B2', 1): rs.dz_v_eta_first_hits_in_B2,
  15. ('phi', 'B1', 1): rs.dphi_v_eta_first_hits_in_B1,
  16. ('phi', 'B2', 1): rs.dphi_v_eta_first_hits_in_B2,
  17. ('z', 'B2', 2): rs.dz_v_eta_second_hits_in_B2,
  18. ('z', 'B3', 2): rs.dz_v_eta_second_hits_in_B3,
  19. ('phi', 'B2', 2): rs.dphi_v_eta_second_hits_in_B2,
  20. ('phi', 'B3', 2): rs.dphi_v_eta_second_hits_in_B3,
  21. }[(var, layer, hit)]
  22. varlabel = {('z', 1): r"$\Delta z_1^{\textrm{sim}}(\mu \mathrm{m})$",
  23. ('z', 2): r"$\Delta z_2^{\textrm{sim}}(\mu \mathrm{m})$",
  24. ('phi', 1): r"$\Delta \phi_1^{\textrm{sim}}(\mathrm{millirad})$",
  25. ('phi', 2): r"$\Delta \phi_2^{\textrm{sim}}(\mathrm{millirad})$"
  26. }[(var, hit)]
  27. scale = {'z': 10**4, # cm -> um
  28. 'phi': 10**3 # rad -> millirad
  29. }[var]
  30. if projection == 'y': # projecting onto var axis
  31. h = h.ProjectionY()
  32. hist_plot(hist(h, rescale_x=scale), xlabel=varlabel, title='Layer - ' + layer)
  33. elif projection == 'x': # projecting onto eta axis
  34. h = h.ProjectionX()
  35. hist_plot(hist(h), xlabel=r'$\eta$', title=layer)
  36. else:
  37. hist2d_plot(hist2d(h, rescale_y=scale), ylabel=varlabel, xlabel=r"$\eta$")
  38. # if display_layer:
  39. # plt.text(0.9, 0.9, layer,
  40. # bbox={'facecolor': 'white', 'alpha': 0.7},
  41. # transform=plt.gca().transAxes)
  42. @decl_plot
  43. def plot_residuals_v_ladder(rs, var, layer):
  44. even, odd = {('phi', 'B1'): (rs.dphi_v_eta_first_hits_in_B1_even_ladder, rs.dphi_v_eta_first_hits_in_B1_odd_ladder),
  45. ('phi', 'B2'): (rs.dphi_v_eta_first_hits_in_B2_even_ladder, rs.dphi_v_eta_first_hits_in_B2_odd_ladder),
  46. ('z', 'B1'): (rs.dz_v_eta_first_hits_in_B1_even_ladder, rs.dz_v_eta_first_hits_in_B1_odd_ladder),
  47. ('z', 'B2'): (rs.dz_v_eta_first_hits_in_B2_even_ladder, rs.dz_v_eta_first_hits_in_B2_odd_ladder),
  48. }[(var, layer)]
  49. unit = {'phi': 'urad',
  50. 'z': 'um',
  51. }[var]
  52. fmt_var = {'phi': r'\phi',
  53. 'z': 'z',
  54. }[var]
  55. scale = {'phi': 10**3,
  56. 'z': 10**4,
  57. }[var]
  58. xlabel = {'phi': r'$\Delta \phi_1$(rad)',
  59. 'z': r"$\Delta z_1$(cm)",
  60. }[var]
  61. even = even.ProjectionY()
  62. odd = odd.ProjectionY()
  63. hist_plot(hist(even), include_errors=True, label='even ladders')
  64. hist_plot(hist(odd), include_errors=True, color='r', label='odd ladders')
  65. plt.xlabel(xlabel)
  66. even_mean = even.GetMean()*scale
  67. odd_mean = odd.GetMean()*scale
  68. txt = r'$ \hat{{\Delta {0} }}_{{1,\textrm{{ {1} }} }}={2:4.2g}${3}'
  69. plt.text(0.05, .8, txt.format(fmt_var, 'odd', odd_mean, unit), transform=plt.gca().transAxes)
  70. plt.text(0.05, .7, txt.format(fmt_var, 'even', even_mean, unit), transform=plt.gca().transAxes)
  71. plt.legend()
  72. @decl_plot
  73. def sc_extrapolation_first(rs, var, layer, hit, even_odd, log=False, norm=None, cut=None, display_layer=True):
  74. ''' Raphael's plots '''
  75. # def preproc(h):
  76. # return hist_slice(hist(h.ProjectionY()), (-0.07, 0.07))
  77. # hist_plot(hist(rs.sc_first_hits_in_B1_dz.ProjectionY()),
  78. # include_errors=errors,
  79. # norm=norm,
  80. # log=log,
  81. # xlabel=r"$\Delta z_1$(cm)",
  82. # title="BPIX - Layer 1")
  83. h = {('phi', 'B1', 1, 'either'): (rs.sc_first_hits_in_B1_dphi_even, rs.sc_first_hits_in_B1_dphi_odd),
  84. ('phi', 'B2', 1, 'either'): (rs.sc_first_hits_in_B2_dphi_even, rs.sc_first_hits_in_B1_dphi_odd),
  85. ('z', 'B1', 1, 'either'): (rs.sc_first_hits_in_B1_dz_even, rs.sc_first_hits_in_B1_dz_odd),
  86. ('z', 'B2', 1, 'either'): (rs.sc_first_hits_in_B2_dz_even, rs.sc_first_hits_in_B2_dz_odd),
  87. ('phi', 'B2', 2, 'either'): (rs.sc_second_hits_in_B2_dphi_even, rs.sc_second_hits_in_B2_dphi_odd),
  88. ('phi', 'B3', 2, 'either'): (rs.sc_second_hits_in_B3_dphi_even, rs.sc_second_hits_in_B3_dphi_odd),
  89. ('z', 'B2', 2, 'either'): (rs.sc_second_hits_in_B2_dz_even, rs.sc_second_hits_in_B2_dz_odd),
  90. ('z', 'B3', 2, 'either'): (rs.sc_second_hits_in_B3_dz_even, rs.sc_second_hits_in_B3_dz_odd),
  91. ('phi', 'B1', 1, 'even'): rs.sc_first_hits_in_B1_dphi_even,
  92. ('phi', 'B2', 1, 'even'): rs.sc_first_hits_in_B2_dphi_even,
  93. ('z', 'B1', 1, 'even'): rs.sc_first_hits_in_B1_dz_even,
  94. ('z', 'B2', 1, 'even'): rs.sc_first_hits_in_B2_dz_even,
  95. ('phi', 'B2', 2, 'even'): rs.sc_second_hits_in_B2_dphi_even,
  96. ('phi', 'B3', 2, 'even'): rs.sc_second_hits_in_B3_dphi_even,
  97. ('z', 'B2', 2, 'even'): rs.sc_second_hits_in_B2_dz_even,
  98. ('z', 'B3', 2, 'even'): rs.sc_second_hits_in_B3_dz_even,
  99. ('phi', 'B1', 1, 'odd'): rs.sc_first_hits_in_B1_dphi_odd,
  100. ('phi', 'B2', 1, 'odd'): rs.sc_first_hits_in_B2_dphi_odd,
  101. ('z', 'B1', 1, 'odd'): rs.sc_first_hits_in_B1_dz_odd,
  102. ('z', 'B2', 1, 'odd'): rs.sc_first_hits_in_B2_dz_odd,
  103. ('phi', 'B2', 2, 'odd'): rs.sc_second_hits_in_B2_dphi_odd,
  104. ('phi', 'B3', 2, 'odd'): rs.sc_second_hits_in_B3_dphi_odd,
  105. ('z', 'B2', 2, 'odd'): rs.sc_second_hits_in_B2_dz_odd,
  106. ('z', 'B3', 2, 'odd'): rs.sc_second_hits_in_B3_dz_odd,
  107. }[(var, layer, hit, even_odd)]
  108. scale = {'phi': 10**3,
  109. 'z': 10**4,
  110. }[var]
  111. varlabel = {('z', 1): r"$\Delta z_1(\mu \mathrm{m})$",
  112. ('z', 2): r"$\Delta z_2(\mu \mathrm{m})$",
  113. ('phi', 1): r"$\Delta \phi_1(\mathrm{millirad})$",
  114. ('phi', 2): r"$\Delta \phi_2(\mathrm{millirad})$"
  115. }[(var, hit)]
  116. try:
  117. h = hist(h.ProjectionY(), rescale_x=scale)
  118. except AttributeError:
  119. h = hist_add(hist(h[0].ProjectionY(), rescale_x=scale), hist(h[1].ProjectionY(), rescale_x=scale))
  120. if cut:
  121. h = hist_slice(h, (-cut, cut))
  122. hist_plot(h,
  123. include_errors=True,
  124. log=log,
  125. norm=norm,
  126. xlabel=varlabel,
  127. title='Layer - ' + layer)
  128. # if display_layer:
  129. # plt.text(0.9, 0.9, layer,
  130. # bbox={'facecolor': 'white', 'alpha': 0.7},
  131. # transform=plt.gca().transAxes)
  132. def generate_dashboard(plots):
  133. from jinja2 import Environment, PackageLoader, select_autoescape
  134. from os.path import join
  135. from urllib.parse import quote
  136. env = Environment(
  137. loader=PackageLoader('plots', 'templates'),
  138. autoescape=select_autoescape(['htm', 'html', 'xml']),
  139. )
  140. env.globals.update({'quote': quote,
  141. 'enumerate': enumerate,
  142. 'zip': zip,
  143. })
  144. def render_to_file(template_name, **kwargs):
  145. with open(join('output', template_name), 'w') as tempout:
  146. template = env.get_template(template_name)
  147. tempout.write(template.render(**kwargs))
  148. def get_by_n(l, n=2):
  149. l = list(l)
  150. while l:
  151. yield l[:n]
  152. l = l[n:]
  153. render_to_file('dashboard.htm', plots=get_by_n(plots, 3),
  154. outdir="figures/")
  155. if __name__ == '__main__':
  156. # First create a ResultSet object which loads all of the objects from output.root
  157. # into memory and makes them available as attributes
  158. if len(sys.argv) != 2:
  159. raise ValueError("please supply root file")
  160. rs = ResultSet("DY2LL", sys.argv[1])
  161. # Next, declare all of the (sub)plots that will be assembled into full
  162. # figures later
  163. dphi1_v_eta_B1 = (plot_residual, (rs, 'phi', 'B1', 1), {})
  164. dphi1_v_eta_B2 = (plot_residual, (rs, 'phi', 'B2', 1), {})
  165. dz1_v_eta_B1 = (plot_residual, (rs, 'z', 'B1', 1), {})
  166. dz1_v_eta_B2 = (plot_residual, (rs, 'z', 'B2', 1), {})
  167. dphi2_v_eta_B2 = (plot_residual, (rs, 'phi', 'B2', 2), {})
  168. dphi2_v_eta_B3 = (plot_residual, (rs, 'phi', 'B3', 2), {})
  169. dz2_v_eta_B2 = (plot_residual, (rs, 'z', 'B2', 2), {})
  170. dz2_v_eta_B3 = (plot_residual, (rs, 'z', 'B3', 2), {})
  171. projectY = {'projection': 'y'}
  172. dphi1_B1 = (plot_residual, (rs, 'phi', 'B1', 1), projectY)
  173. dphi1_B2 = (plot_residual, (rs, 'phi', 'B2', 1), projectY)
  174. dz1_B1 = (plot_residual, (rs, 'z', 'B1', 1), projectY)
  175. dz1_B2 = (plot_residual, (rs, 'z', 'B2', 1), projectY)
  176. dphi2_B2 = (plot_residual, (rs, 'phi', 'B2', 2), projectY)
  177. dphi2_B3 = (plot_residual, (rs, 'phi', 'B3', 2), projectY)
  178. dz2_B2 = (plot_residual, (rs, 'z', 'B2', 2), projectY)
  179. dz2_B3 = (plot_residual, (rs, 'z', 'B3', 2), projectY)
  180. projectX = {'projection': 'x'}
  181. eta1_B1 = (plot_residual, (rs, 'phi', 'B1', 1), projectX)
  182. eta1_B2 = (plot_residual, (rs, 'phi', 'B2', 1), projectX)
  183. eta2_B2 = (plot_residual, (rs, 'phi', 'B2', 2), projectX)
  184. eta2_B3 = (plot_residual, (rs, 'phi', 'B3', 2), projectX)
  185. dphi1_v_ladder_B1 = (plot_residuals_v_ladder, (rs, 'phi', 'B1'), {})
  186. dphi1_v_ladder_B2 = (plot_residuals_v_ladder, (rs, 'phi', 'B2'), {})
  187. dz1_v_ladder_B1 = (plot_residuals_v_ladder, (rs, 'z', 'B1'), {})
  188. dz1_v_ladder_B2 = (plot_residuals_v_ladder, (rs, 'z', 'B2'), {})
  189. opts = {'log': False, 'norm': None, 'cut': 150}
  190. dphi1_sc_ex_B1 = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'either'), opts)
  191. dphi1_sc_ex_B2 = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'either'), opts)
  192. dz1_sc_ex_B1 = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'either'), {})
  193. dz1_sc_ex_B2 = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'either'), {})
  194. dphi2_sc_ex_B1 = (sc_extrapolation_first, (rs, 'phi', 'B1', 2, 'either'), opts)
  195. dphi2_sc_ex_B2 = (sc_extrapolation_first, (rs, 'phi', 'B2', 2, 'either'), opts)
  196. dz2_sc_ex_B1 = (sc_extrapolation_first, (rs, 'z', 'B1', 2, 'either'), {})
  197. dz2_sc_ex_B2 = (sc_extrapolation_first, (rs, 'z', 'B2', 2, 'either'), {})
  198. dphi1_sc_ex_B1_even = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'even'), opts)
  199. dphi1_sc_ex_B2_even = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'even'), opts)
  200. dz1_sc_ex_B1_even = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'even'), {})
  201. dz1_sc_ex_B2_even = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'even'), {})
  202. dphi1_sc_ex_B1_odd = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'odd'), opts)
  203. dphi1_sc_ex_B2_odd = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'odd'), opts)
  204. dz1_sc_ex_B1_odd = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'odd'), {})
  205. dz1_sc_ex_B2_odd = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'odd'), {})
  206. # Now assemble the plots into figures.
  207. plots = [
  208. # Plot([[dphi1_v_eta_B1, dphi1_v_eta_B2],
  209. # [dz1_v_eta_B1, dz1_v_eta_B2 ]],
  210. # 'res1_v_eta'),
  211. # Plot([[dphi2_v_eta_B2, dphi2_v_eta_B3],
  212. # [dz2_v_eta_B2, dz2_v_eta_B3 ]],
  213. # 'res2_v_eta'),
  214. # Plot([[dphi1_B1, dphi1_B2],
  215. # [dz1_B1, dz1_B2 ]],
  216. # 'res1'),
  217. # Plot([[eta1_B1, eta1_B2],
  218. # [eta2_B2, eta2_B3 ]],
  219. # 'eta_dist'),
  220. # Plot([[dphi2_B2, dphi2_B3],
  221. # [dz2_B2, dz2_B3 ]],
  222. # 'res2'),
  223. # Plot([[dphi1_v_ladder_B1, dphi1_v_ladder_B2],
  224. # [dz1_v_ladder_B1, dz1_v_ladder_B2]],
  225. # 'res_v_ladder'),
  226. # Plot([[dphi1_sc_ex_B1, dphi1_sc_ex_B2],
  227. # [dz1_sc_ex_B1, dz1_sc_ex_B2]],
  228. # 'sc_ex_1'),
  229. # Plot([[dphi1_sc_ex_B1_even, dphi1_sc_ex_B2_even],
  230. # [dz1_sc_ex_B1_even, dz1_sc_ex_B2_even]],
  231. # 'sc_ex_1_even'),
  232. # Plot([[dphi1_sc_ex_B1_odd, dphi1_sc_ex_B2_odd],
  233. # [dz1_sc_ex_B1_odd, dz1_sc_ex_B2_odd]],
  234. # 'sc_ex_1_odd'),
  235. # Plot([[(dphi1_sc_ex_B1_odd, dphi1_sc_ex_B1_even), "FL"],
  236. # [dz1_sc_ex_B1_odd, dz1_sc_ex_B1_even]],
  237. # 'sc_ex_1_odd_v_even'),
  238. Plot([[dphi1_sc_ex_B1],
  239. [dphi1_B1]],
  240. 'sc_ex_v_sim_phi1_B1'),
  241. Plot([[dphi1_sc_ex_B2],
  242. [dphi1_B2]],
  243. 'sc_ex_v_sim_phi1_B2'),
  244. Plot([[dphi2_sc_ex_B2],
  245. [dphi2_B2]],
  246. 'sc_ex_v_sim_phi2_B2'),
  247. Plot([[dz1_sc_ex_B1],
  248. [dz1_B1]],
  249. 'sc_ex_v_sim_z1_B1'),
  250. Plot([[dz2_sc_ex_B2],
  251. [dz2_B2]],
  252. 'sc_ex_v_sim_z2_B2'),
  253. Plot([[dz1_sc_ex_B1],
  254. [dz1_sc_ex_B1_even],
  255. [dz1_sc_ex_B1_odd]],
  256. 'even_odd'),
  257. ]
  258. # Finally, render and save the plots and generate the html+bootstrap
  259. # dashboard to view them
  260. save_plots(plots)
  261. generate_dashboard(plots)