plots.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 (decl_plot, save_plots, hist_plot, hist2d_plot, Plot)
  7. @decl_plot
  8. def plot_residual(rs, var, layer, hit, projection=None):
  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(\mu \mathrm{m})$",
  23. ('z', 2): r"$\Delta z_2(\mu \mathrm{m})$",
  24. ('phi', 1): r"$\Delta \phi_1(\mathrm{millirad})$",
  25. ('phi', 2): r"$\Delta \phi_2(\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)
  33. elif projection == 'x': # projecting onto eta axis
  34. h = h.ProjectionX()
  35. hist_plot(hist(h), xlabel=r'$\eta$')
  36. else:
  37. hist2d_plot(hist2d(h, rescale_y=scale), ylabel=varlabel, xlabel=r"$\eta$")
  38. plt.text(0.9, 0.9, layer,
  39. bbox={'facecolor': 'white', 'alpha': 0.7},
  40. transform=plt.gca().transAxes)
  41. @decl_plot
  42. def plot_residuals_v_ladder(rs, var, layer):
  43. even, odd = {('phi', 'B1'): (rs.dphi_v_eta_first_hits_in_B1_even_ladder, rs.dphi_v_eta_first_hits_in_B1_odd_ladder),
  44. ('phi', 'B2'): (rs.dphi_v_eta_first_hits_in_B2_even_ladder, rs.dphi_v_eta_first_hits_in_B2_odd_ladder),
  45. ('z', 'B1'): (rs.dz_v_eta_first_hits_in_B1_even_ladder, rs.dz_v_eta_first_hits_in_B1_odd_ladder),
  46. ('z', 'B2'): (rs.dz_v_eta_first_hits_in_B2_even_ladder, rs.dz_v_eta_first_hits_in_B2_odd_ladder),
  47. }[(var, layer)]
  48. unit = {'phi': 'urad',
  49. 'z': 'um',
  50. }[var]
  51. fmt_var = {'phi': r'\phi',
  52. 'z': 'z',
  53. }[var]
  54. scale = {'phi': 10**6,
  55. 'z': 10**4,
  56. }[var]
  57. xlabel = {'phi': r'$\Delta \phi_1$(rad)',
  58. 'z': r"$\Delta z_1$(cm)",
  59. }[var]
  60. even = even.ProjectionY()
  61. odd = odd.ProjectionY()
  62. hist_plot(hist(even), include_errors=True, label='even ladders')
  63. hist_plot(hist(odd), include_errors=True, color='r', label='odd ladders')
  64. plt.xlabel(xlabel)
  65. even_mean = even.GetMean()*scale
  66. odd_mean = odd.GetMean()*scale
  67. txt = r'$ \hat{{\Delta {0} }}_{{1,\textrm{{ {1} }} }}={2:4.2g}${3}'
  68. plt.text(0.05, .8, txt.format(fmt_var, 'odd', odd_mean, unit), transform=plt.gca().transAxes)
  69. plt.text(0.05, .7, txt.format(fmt_var, 'even', even_mean, unit), transform=plt.gca().transAxes)
  70. plt.legend()
  71. @decl_plot
  72. def sc_extrapolation_first(rs, var, layer, hit, even_odd, log=False, norm=None, cut=None):
  73. ''' Raphael's plots '''
  74. errors = True
  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_either,
  84. ('phi', 'B2', 1, 'either'): rs.sc_first_hits_in_B2_dphi_either,
  85. ('z', 'B1', 1, 'either'): rs.sc_first_hits_in_B1_dz_either,
  86. ('z', 'B2', 1, 'either'): rs.sc_first_hits_in_B2_dz_either,
  87. ('phi', 'B2', 2, 'either'): rs.sc_second_hits_in_B2_dphi_either,
  88. ('phi', 'B3', 2, 'either'): rs.sc_second_hits_in_B3_dphi_either,
  89. ('z', 'B2', 2, 'either'): rs.sc_second_hits_in_B2_dz_either,
  90. ('z', 'B3', 2, 'either'): rs.sc_second_hits_in_B3_dz_either,
  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**6,
  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. h = hist(h.ProjectionY(), rescale_x=scale)
  117. if cut:
  118. h = hist_slice(h, (-cut, cut))
  119. hist_plot(h,
  120. include_errors=True,
  121. log=log,
  122. norm=norm,
  123. xlabel=varlabel)
  124. def generate_dashboard(plots):
  125. from jinja2 import Environment, PackageLoader, select_autoescape
  126. from os.path import join
  127. from urllib.parse import quote
  128. env = Environment(
  129. loader=PackageLoader('plots', 'templates'),
  130. autoescape=select_autoescape(['htm', 'html', 'xml'])
  131. )
  132. def render_to_file(template_name, **kwargs):
  133. with open(join('output', template_name), 'w') as tempout:
  134. template = env.get_template(template_name)
  135. tempout.write(template.render(**kwargs))
  136. def get_by_n(l, n=2):
  137. l = list(l)
  138. while l:
  139. yield l[:n]
  140. l = l[n:]
  141. render_to_file('dashboard.htm', plots=get_by_n(plots, 3),
  142. outdir="figures/",
  143. quote=quote,
  144. enumerate=enumerate)
  145. if __name__ == '__main__':
  146. # First create a ResultSet object which loads all of the objects from output.root
  147. # into memory and makes them available as attributes
  148. if len(sys.argv) != 2:
  149. raise ValueError("please supply root file")
  150. rs = ResultSet("DY2LL", sys.argv[1])
  151. # Next, declare all of the (sub)plots that will be assembled into full
  152. # figures later
  153. dphi1_v_eta_B1 = (plot_residual, (rs, 'phi', 'B1', 1), {})
  154. dphi1_v_eta_B2 = (plot_residual, (rs, 'phi', 'B2', 1), {})
  155. dz1_v_eta_B1 = (plot_residual, (rs, 'z', 'B1', 1), {})
  156. dz1_v_eta_B2 = (plot_residual, (rs, 'z', 'B2', 1), {})
  157. dphi2_v_eta_B2 = (plot_residual, (rs, 'phi', 'B2', 2), {})
  158. dphi2_v_eta_B3 = (plot_residual, (rs, 'phi', 'B3', 2), {})
  159. dz2_v_eta_B2 = (plot_residual, (rs, 'z', 'B2', 2), {})
  160. dz2_v_eta_B3 = (plot_residual, (rs, 'z', 'B3', 2), {})
  161. projectY = {'projection': 'y'}
  162. dphi1_B1 = (plot_residual, (rs, 'phi', 'B1', 1), projectY)
  163. dphi1_B2 = (plot_residual, (rs, 'phi', 'B2', 1), projectY)
  164. dz1_B1 = (plot_residual, (rs, 'z', 'B1', 1), projectY)
  165. dz1_B2 = (plot_residual, (rs, 'z', 'B2', 1), projectY)
  166. dphi2_B2 = (plot_residual, (rs, 'phi', 'B2', 2), projectY)
  167. dphi2_B3 = (plot_residual, (rs, 'phi', 'B3', 2), projectY)
  168. dz2_B2 = (plot_residual, (rs, 'z', 'B2', 2), projectY)
  169. dz2_B3 = (plot_residual, (rs, 'z', 'B3', 2), projectY)
  170. projectX = {'projection': 'x'}
  171. eta1_B1 = (plot_residual, (rs, 'phi', 'B1', 1), projectX)
  172. eta1_B2 = (plot_residual, (rs, 'phi', 'B2', 1), projectX)
  173. eta2_B2 = (plot_residual, (rs, 'phi', 'B2', 2), projectX)
  174. eta2_B3 = (plot_residual, (rs, 'phi', 'B3', 2), projectX)
  175. dphi1_v_ladder_B1 = (plot_residuals_v_ladder, (rs, 'phi', 'B1'), {})
  176. dphi1_v_ladder_B2 = (plot_residuals_v_ladder, (rs, 'phi', 'B2'), {})
  177. dz1_v_ladder_B1 = (plot_residuals_v_ladder, (rs, 'z', 'B1'), {})
  178. dz1_v_ladder_B2 = (plot_residuals_v_ladder, (rs, 'z', 'B2'), {})
  179. dphi1_sc_ex_B1 = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'either'), {'log': True, 'norm': 1.0})
  180. dphi1_sc_ex_B2 = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'either'), {'log': True, 'norm': 1.0})
  181. dz1_sc_ex_B1 = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'either'), {'norm': 1.0})
  182. dz1_sc_ex_B2 = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'either'), {'norm': 1.0})
  183. dphi1_sc_ex_B1_even = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'even'), {'log': True, 'norm': 1.0})
  184. dphi1_sc_ex_B2_even = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'even'), {'log': True, 'norm': 1.0})
  185. dz1_sc_ex_B1_even = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'even'), {'norm': 1.0})
  186. dz1_sc_ex_B2_even = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'even'), {'norm': 1.0})
  187. dphi1_sc_ex_B1_odd = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'odd'), {'log': True, 'norm': 1.0})
  188. dphi1_sc_ex_B2_odd = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'odd'), {'log': True, 'norm': 1.0})
  189. dz1_sc_ex_B1_odd = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'odd'), {'norm': 1.0})
  190. dz1_sc_ex_B2_odd = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'odd'), {'norm': 1.0})
  191. # Now assemble the plots into figures.
  192. plots = [Plot([[dphi1_v_eta_B1, dphi1_v_eta_B2],
  193. [dz1_v_eta_B1, dz1_v_eta_B2 ]],
  194. 'res1_v_eta'),
  195. Plot([[dphi2_v_eta_B2, dphi2_v_eta_B3],
  196. [dz2_v_eta_B2, dz2_v_eta_B3 ]],
  197. 'res2_v_eta'),
  198. Plot([[dphi1_B1, dphi1_B2],
  199. [dz1_B1, dz1_B2 ]],
  200. 'res1'),
  201. Plot([[eta1_B1, eta1_B2],
  202. [eta2_B2, eta2_B3 ]],
  203. 'eta_dist'),
  204. Plot([[dphi2_B2, dphi2_B3],
  205. [dz2_B2, dz2_B3 ]],
  206. 'res2'),
  207. Plot([[dphi1_v_ladder_B1, dphi1_v_ladder_B2],
  208. [dz1_v_ladder_B1, dz1_v_ladder_B2]],
  209. 'res_v_ladder'),
  210. Plot([[dphi1_sc_ex_B1, dphi1_sc_ex_B2],
  211. [dz1_sc_ex_B1, dz1_sc_ex_B2]],
  212. 'sc_ex_1'),
  213. Plot([[dphi1_sc_ex_B1_even, dphi1_sc_ex_B2_even],
  214. [dz1_sc_ex_B1_even, dz1_sc_ex_B2_even]],
  215. 'sc_ex_1_even'),
  216. Plot([[dphi1_sc_ex_B1_odd, dphi1_sc_ex_B2_odd],
  217. [dz1_sc_ex_B1_odd, dz1_sc_ex_B2_odd]],
  218. 'sc_ex_1_odd'),
  219. ]
  220. # Finally, render and save the plots and generate the html+bootstrap
  221. # dashboard to view them
  222. save_plots(plots)
  223. generate_dashboard(plots)