plots.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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**3,
  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. # def preproc(h):
  75. # return hist_slice(hist(h.ProjectionY()), (-0.07, 0.07))
  76. # hist_plot(hist(rs.sc_first_hits_in_B1_dz.ProjectionY()),
  77. # include_errors=errors,
  78. # norm=norm,
  79. # log=log,
  80. # xlabel=r"$\Delta z_1$(cm)",
  81. # title="BPIX - Layer 1")
  82. h = {('phi', 'B1', 1, 'either'): rs.sc_first_hits_in_B1_dphi_either,
  83. ('phi', 'B2', 1, 'either'): rs.sc_first_hits_in_B2_dphi_either,
  84. ('z', 'B1', 1, 'either'): rs.sc_first_hits_in_B1_dz_either,
  85. ('z', 'B2', 1, 'either'): rs.sc_first_hits_in_B2_dz_either,
  86. ('phi', 'B2', 2, 'either'): rs.sc_second_hits_in_B2_dphi_either,
  87. ('phi', 'B3', 2, 'either'): rs.sc_second_hits_in_B3_dphi_either,
  88. ('z', 'B2', 2, 'either'): rs.sc_second_hits_in_B2_dz_either,
  89. ('z', 'B3', 2, 'either'): rs.sc_second_hits_in_B3_dz_either,
  90. ('phi', 'B1', 1, 'even'): rs.sc_first_hits_in_B1_dphi_even,
  91. ('phi', 'B2', 1, 'even'): rs.sc_first_hits_in_B2_dphi_even,
  92. ('z', 'B1', 1, 'even'): rs.sc_first_hits_in_B1_dz_even,
  93. ('z', 'B2', 1, 'even'): rs.sc_first_hits_in_B2_dz_even,
  94. ('phi', 'B2', 2, 'even'): rs.sc_second_hits_in_B2_dphi_even,
  95. ('phi', 'B3', 2, 'even'): rs.sc_second_hits_in_B3_dphi_even,
  96. ('z', 'B2', 2, 'even'): rs.sc_second_hits_in_B2_dz_even,
  97. ('z', 'B3', 2, 'even'): rs.sc_second_hits_in_B3_dz_even,
  98. ('phi', 'B1', 1, 'odd'): rs.sc_first_hits_in_B1_dphi_odd,
  99. ('phi', 'B2', 1, 'odd'): rs.sc_first_hits_in_B2_dphi_odd,
  100. ('z', 'B1', 1, 'odd'): rs.sc_first_hits_in_B1_dz_odd,
  101. ('z', 'B2', 1, 'odd'): rs.sc_first_hits_in_B2_dz_odd,
  102. ('phi', 'B2', 2, 'odd'): rs.sc_second_hits_in_B2_dphi_odd,
  103. ('phi', 'B3', 2, 'odd'): rs.sc_second_hits_in_B3_dphi_odd,
  104. ('z', 'B2', 2, 'odd'): rs.sc_second_hits_in_B2_dz_odd,
  105. ('z', 'B3', 2, 'odd'): rs.sc_second_hits_in_B3_dz_odd,
  106. }[(var, layer, hit, even_odd)]
  107. scale = {'phi': 10**3,
  108. 'z': 10**4,
  109. }[var]
  110. varlabel = {('z', 1): r"$\Delta z_1(\mu \mathrm{m})$",
  111. ('z', 2): r"$\Delta z_2(\mu \mathrm{m})$",
  112. ('phi', 1): r"$\Delta \phi_1(\mathrm{millirad})$",
  113. ('phi', 2): r"$\Delta \phi_2(\mathrm{millirad})$"
  114. }[(var, hit)]
  115. h = hist(h.ProjectionY(), rescale_x=scale)
  116. if cut:
  117. h = hist_slice(h, (-cut, cut))
  118. hist_plot(h,
  119. include_errors=True,
  120. log=log,
  121. norm=norm,
  122. xlabel=varlabel)
  123. def generate_dashboard(plots):
  124. from jinja2 import Environment, PackageLoader, select_autoescape
  125. from os.path import join
  126. from urllib.parse import quote
  127. env = Environment(
  128. loader=PackageLoader('plots', 'templates'),
  129. autoescape=select_autoescape(['htm', 'html', 'xml']),
  130. )
  131. env.globals.update({'quote': quote,
  132. 'enumerate': enumerate,
  133. 'zip': zip,
  134. })
  135. def render_to_file(template_name, **kwargs):
  136. with open(join('output', template_name), 'w') as tempout:
  137. template = env.get_template(template_name)
  138. tempout.write(template.render(**kwargs))
  139. def get_by_n(l, n=2):
  140. l = list(l)
  141. while l:
  142. yield l[:n]
  143. l = l[n:]
  144. render_to_file('dashboard.htm', plots=get_by_n(plots, 3),
  145. outdir="figures/")
  146. if __name__ == '__main__':
  147. # First create a ResultSet object which loads all of the objects from output.root
  148. # into memory and makes them available as attributes
  149. if len(sys.argv) != 2:
  150. raise ValueError("please supply root file")
  151. rs = ResultSet("DY2LL", sys.argv[1])
  152. # Next, declare all of the (sub)plots that will be assembled into full
  153. # figures later
  154. dphi1_v_eta_B1 = (plot_residual, (rs, 'phi', 'B1', 1), {})
  155. dphi1_v_eta_B2 = (plot_residual, (rs, 'phi', 'B2', 1), {})
  156. dz1_v_eta_B1 = (plot_residual, (rs, 'z', 'B1', 1), {})
  157. dz1_v_eta_B2 = (plot_residual, (rs, 'z', 'B2', 1), {})
  158. dphi2_v_eta_B2 = (plot_residual, (rs, 'phi', 'B2', 2), {})
  159. dphi2_v_eta_B3 = (plot_residual, (rs, 'phi', 'B3', 2), {})
  160. dz2_v_eta_B2 = (plot_residual, (rs, 'z', 'B2', 2), {})
  161. dz2_v_eta_B3 = (plot_residual, (rs, 'z', 'B3', 2), {})
  162. projectY = {'projection': 'y'}
  163. dphi1_B1 = (plot_residual, (rs, 'phi', 'B1', 1), projectY)
  164. dphi1_B2 = (plot_residual, (rs, 'phi', 'B2', 1), projectY)
  165. dz1_B1 = (plot_residual, (rs, 'z', 'B1', 1), projectY)
  166. dz1_B2 = (plot_residual, (rs, 'z', 'B2', 1), projectY)
  167. dphi2_B2 = (plot_residual, (rs, 'phi', 'B2', 2), projectY)
  168. dphi2_B3 = (plot_residual, (rs, 'phi', 'B3', 2), projectY)
  169. dz2_B2 = (plot_residual, (rs, 'z', 'B2', 2), projectY)
  170. dz2_B3 = (plot_residual, (rs, 'z', 'B3', 2), projectY)
  171. projectX = {'projection': 'x'}
  172. eta1_B1 = (plot_residual, (rs, 'phi', 'B1', 1), projectX)
  173. eta1_B2 = (plot_residual, (rs, 'phi', 'B2', 1), projectX)
  174. eta2_B2 = (plot_residual, (rs, 'phi', 'B2', 2), projectX)
  175. eta2_B3 = (plot_residual, (rs, 'phi', 'B3', 2), projectX)
  176. dphi1_v_ladder_B1 = (plot_residuals_v_ladder, (rs, 'phi', 'B1'), {})
  177. dphi1_v_ladder_B2 = (plot_residuals_v_ladder, (rs, 'phi', 'B2'), {})
  178. dz1_v_ladder_B1 = (plot_residuals_v_ladder, (rs, 'z', 'B1'), {})
  179. dz1_v_ladder_B2 = (plot_residuals_v_ladder, (rs, 'z', 'B2'), {})
  180. opts = {'log': False, 'norm': 1.0, 'cut': 25}
  181. dphi1_sc_ex_B1 = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'either'), opts )
  182. dphi1_sc_ex_B2 = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'either'), opts)
  183. dz1_sc_ex_B1 = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'either'), {'norm': 1.0})
  184. dz1_sc_ex_B2 = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'either'), {'norm': 1.0})
  185. dphi1_sc_ex_B1_even = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'even'), opts)
  186. dphi1_sc_ex_B2_even = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'even'), opts)
  187. dz1_sc_ex_B1_even = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'even'), {'norm': 1.0})
  188. dz1_sc_ex_B2_even = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'even'), {'norm': 1.0})
  189. dphi1_sc_ex_B1_odd = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'odd'), opts)
  190. dphi1_sc_ex_B2_odd = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'odd'), opts)
  191. dz1_sc_ex_B1_odd = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'odd'), {'norm': 1.0})
  192. dz1_sc_ex_B2_odd = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'odd'), {'norm': 1.0})
  193. # Now assemble the plots into figures.
  194. plots = [Plot([[dphi1_v_eta_B1, dphi1_v_eta_B2],
  195. [dz1_v_eta_B1, dz1_v_eta_B2 ]],
  196. 'res1_v_eta'),
  197. Plot([[dphi2_v_eta_B2, dphi2_v_eta_B3],
  198. [dz2_v_eta_B2, dz2_v_eta_B3 ]],
  199. 'res2_v_eta'),
  200. Plot([[dphi1_B1, dphi1_B2],
  201. [dz1_B1, dz1_B2 ]],
  202. 'res1'),
  203. Plot([[eta1_B1, eta1_B2],
  204. [eta2_B2, eta2_B3 ]],
  205. 'eta_dist'),
  206. Plot([[dphi2_B2, dphi2_B3],
  207. [dz2_B2, dz2_B3 ]],
  208. 'res2'),
  209. Plot([[dphi1_v_ladder_B1, dphi1_v_ladder_B2],
  210. [dz1_v_ladder_B1, dz1_v_ladder_B2]],
  211. 'res_v_ladder'),
  212. Plot([[dphi1_sc_ex_B1, dphi1_sc_ex_B2],
  213. [dz1_sc_ex_B1, dz1_sc_ex_B2]],
  214. 'sc_ex_1'),
  215. Plot([[dphi1_sc_ex_B1_even, dphi1_sc_ex_B2_even],
  216. [dz1_sc_ex_B1_even, dz1_sc_ex_B2_even]],
  217. 'sc_ex_1_even'),
  218. Plot([[dphi1_sc_ex_B1_odd, dphi1_sc_ex_B2_odd],
  219. [dz1_sc_ex_B1_odd, dz1_sc_ex_B2_odd]],
  220. 'sc_ex_1_odd'),
  221. Plot([[[dphi1_sc_ex_B1_odd, dphi1_sc_ex_B1_even], "FL"],
  222. [dz1_sc_ex_B1_odd, dz1_sc_ex_B1_even]],
  223. 'sc_ex_1_odd_v_even'),
  224. ]
  225. # Finally, render and save the plots and generate the html+bootstrap
  226. # dashboard to view them
  227. save_plots(plots)
  228. generate_dashboard(plots)