plots.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #!/usr/bin/env python
  2. import sys
  3. import matplotlib.pyplot as plt
  4. from uproot import open as root_open
  5. from matplotboard import decl_fig, render, generate_report
  6. @decl_fig
  7. def plot_residual(rs, var, layer, hit, projection=None, display_layer=True):
  8. r'''
  9. Plots of $\Delta \phi$, $\Delta z$, or $\Delta r$ (vs $\eta$) between RecHit
  10. and SimHit for the nth hit in the matched seed.
  11. '''
  12. h = {('z', 'B1', 1): rs.dz_v_eta_first_hits_in_B1,
  13. ('z', 'B2', 1): rs.dz_v_eta_first_hits_in_B2,
  14. ('phi', 'B1', 1): rs.dphi_v_eta_first_hits_in_B1,
  15. ('phi', 'B2', 1): rs.dphi_v_eta_first_hits_in_B2,
  16. ('z', 'B2', 2): rs.dz_v_eta_second_hits_in_B2,
  17. ('z', 'B3', 2): rs.dz_v_eta_second_hits_in_B3,
  18. ('phi', 'B2', 2): rs.dphi_v_eta_second_hits_in_B2,
  19. ('phi', 'B3', 2): rs.dphi_v_eta_second_hits_in_B3,
  20. }[(var, layer, hit)]
  21. varlabel = {('z', 1): r"$\Delta z_1^{\textrm{sim}}(\mu \mathrm{m})$",
  22. ('z', 2): r"$\Delta z_2^{\textrm{sim}}(\mu \mathrm{m})$",
  23. ('phi', 1): r"$\Delta \phi_1^{\textrm{sim}}(\mathrm{millirad})$",
  24. ('phi', 2): r"$\Delta \phi_2^{\textrm{sim}}(\mathrm{millirad})$"
  25. }[(var, hit)]
  26. scale = {'z': 10**4, # cm -> um
  27. 'phi': 10**3 # rad -> millirad
  28. }[var]
  29. if projection == 'y': # projecting onto var axis
  30. h = h.ProjectionY()
  31. hist_plot(hist(h, rescale_x=scale), xlabel=varlabel, title='Layer - ' + layer)
  32. elif projection == 'x': # projecting onto eta axis
  33. h = h.ProjectionX()
  34. hist_plot(hist(h), xlabel=r'$\eta$', title=layer)
  35. else:
  36. hist2d_plot(hist2d(h, rescale_y=scale), ylabel=varlabel, xlabel=r"$\eta$")
  37. # if display_layer:
  38. # plt.text(0.9, 0.9, layer,
  39. # bbox={'facecolor': 'white', 'alpha': 0.7},
  40. # transform=plt.gca().transAxes)
  41. @decl_fig
  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_fig
  72. def sc_extrapolation_first(rs, var, layer, hit, even_odd, log=False, norm=None, cut=None, display_layer=True):
  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_even, rs.sc_first_hits_in_B1_dphi_odd),
  83. ('phi', 'B2', 1, 'either'): (rs.sc_first_hits_in_B2_dphi_even, rs.sc_first_hits_in_B1_dphi_odd),
  84. ('z', 'B1', 1, 'either'): (rs.sc_first_hits_in_B1_dz_even, rs.sc_first_hits_in_B1_dz_odd),
  85. ('z', 'B2', 1, 'either'): (rs.sc_first_hits_in_B2_dz_even, rs.sc_first_hits_in_B2_dz_odd),
  86. ('phi', 'B2', 2, 'either'): (rs.sc_second_hits_in_B2_dphi_even, rs.sc_second_hits_in_B2_dphi_odd),
  87. ('phi', 'B3', 2, 'either'): (rs.sc_second_hits_in_B3_dphi_even, rs.sc_second_hits_in_B3_dphi_odd),
  88. ('z', 'B2', 2, 'either'): (rs.sc_second_hits_in_B2_dz_even, rs.sc_second_hits_in_B2_dz_odd),
  89. ('z', 'B3', 2, 'either'): (rs.sc_second_hits_in_B3_dz_even, rs.sc_second_hits_in_B3_dz_odd),
  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. try:
  116. h = hist(h.ProjectionY(), rescale_x=scale)
  117. except AttributeError:
  118. h = hist_add(hist(h[0].ProjectionY(), rescale_x=scale), hist(h[1].ProjectionY(), rescale_x=scale))
  119. if cut:
  120. h = hist_slice(h, (-cut, cut))
  121. hist_plot(h,
  122. include_errors=True,
  123. log=log,
  124. norm=norm,
  125. xlabel=varlabel,
  126. title='Layer - ' + layer)
  127. # if display_layer:
  128. # plt.text(0.9, 0.9, layer,
  129. # bbox={'facecolor': 'white', 'alpha': 0.7},
  130. # transform=plt.gca().transAxes)
  131. # def generate_dashboard(plots):
  132. # from jinja2 import Environment, PackageLoader, select_autoescape
  133. # from os.path import join
  134. # from urllib.parse import quote
  135. #
  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. #
  145. # def render_to_file(template_name, **kwargs):
  146. # with open(join('output', template_name), 'w') as tempout:
  147. # template = env.get_template(template_name)
  148. # tempout.write(template.render(**kwargs))
  149. #
  150. # def get_by_n(l, n=2):
  151. # l = list(l)
  152. # while l:
  153. # yield l[:n]
  154. # l = l[n:]
  155. #
  156. # render_to_file('dashboard.htm', plots=get_by_n(plots, 3),
  157. # outdir="figures/")
  158. if __name__ == '__main__':
  159. # First create a ResultSet object which loads all of the objects from output.root
  160. # into memory and makes them available as attributes
  161. if len(sys.argv) != 2:
  162. raise ValueError("please supply root file")
  163. rs = ResultSet("DY2LL", sys.argv[1])
  164. # Next, declare all of the (sub)plots that will be assembled into full
  165. # figures later
  166. dphi1_v_eta_B1 = (plot_residual, (rs, 'phi', 'B1', 1), {})
  167. dphi1_v_eta_B2 = (plot_residual, (rs, 'phi', 'B2', 1), {})
  168. dz1_v_eta_B1 = (plot_residual, (rs, 'z', 'B1', 1), {})
  169. dz1_v_eta_B2 = (plot_residual, (rs, 'z', 'B2', 1), {})
  170. dphi2_v_eta_B2 = (plot_residual, (rs, 'phi', 'B2', 2), {})
  171. dphi2_v_eta_B3 = (plot_residual, (rs, 'phi', 'B3', 2), {})
  172. dz2_v_eta_B2 = (plot_residual, (rs, 'z', 'B2', 2), {})
  173. dz2_v_eta_B3 = (plot_residual, (rs, 'z', 'B3', 2), {})
  174. projectY = {'projection': 'y'}
  175. dphi1_B1 = (plot_residual, (rs, 'phi', 'B1', 1), projectY)
  176. dphi1_B2 = (plot_residual, (rs, 'phi', 'B2', 1), projectY)
  177. dz1_B1 = (plot_residual, (rs, 'z', 'B1', 1), projectY)
  178. dz1_B2 = (plot_residual, (rs, 'z', 'B2', 1), projectY)
  179. dphi2_B2 = (plot_residual, (rs, 'phi', 'B2', 2), projectY)
  180. dphi2_B3 = (plot_residual, (rs, 'phi', 'B3', 2), projectY)
  181. dz2_B2 = (plot_residual, (rs, 'z', 'B2', 2), projectY)
  182. dz2_B3 = (plot_residual, (rs, 'z', 'B3', 2), projectY)
  183. projectX = {'projection': 'x'}
  184. eta1_B1 = (plot_residual, (rs, 'phi', 'B1', 1), projectX)
  185. eta1_B2 = (plot_residual, (rs, 'phi', 'B2', 1), projectX)
  186. eta2_B2 = (plot_residual, (rs, 'phi', 'B2', 2), projectX)
  187. eta2_B3 = (plot_residual, (rs, 'phi', 'B3', 2), projectX)
  188. dphi1_v_ladder_B1 = (plot_residuals_v_ladder, (rs, 'phi', 'B1'), {})
  189. dphi1_v_ladder_B2 = (plot_residuals_v_ladder, (rs, 'phi', 'B2'), {})
  190. dz1_v_ladder_B1 = (plot_residuals_v_ladder, (rs, 'z', 'B1'), {})
  191. dz1_v_ladder_B2 = (plot_residuals_v_ladder, (rs, 'z', 'B2'), {})
  192. opts = {'log': False, 'norm': None, 'cut': 150}
  193. dphi1_sc_ex_B1 = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'either'), opts)
  194. dphi1_sc_ex_B2 = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'either'), opts)
  195. dz1_sc_ex_B1 = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'either'), {})
  196. dz1_sc_ex_B2 = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'either'), {})
  197. dphi2_sc_ex_B1 = (sc_extrapolation_first, (rs, 'phi', 'B1', 2, 'either'), opts)
  198. dphi2_sc_ex_B2 = (sc_extrapolation_first, (rs, 'phi', 'B2', 2, 'either'), opts)
  199. dz2_sc_ex_B1 = (sc_extrapolation_first, (rs, 'z', 'B1', 2, 'either'), {})
  200. dz2_sc_ex_B2 = (sc_extrapolation_first, (rs, 'z', 'B2', 2, 'either'), {})
  201. dphi1_sc_ex_B1_even = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'even'), opts)
  202. dphi1_sc_ex_B2_even = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'even'), opts)
  203. dz1_sc_ex_B1_even = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'even'), {})
  204. dz1_sc_ex_B2_even = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'even'), {})
  205. dphi1_sc_ex_B1_odd = (sc_extrapolation_first, (rs, 'phi', 'B1', 1, 'odd'), opts)
  206. dphi1_sc_ex_B2_odd = (sc_extrapolation_first, (rs, 'phi', 'B2', 1, 'odd'), opts)
  207. dz1_sc_ex_B1_odd = (sc_extrapolation_first, (rs, 'z', 'B1', 1, 'odd'), {})
  208. dz1_sc_ex_B2_odd = (sc_extrapolation_first, (rs, 'z', 'B2', 1, 'odd'), {})
  209. # Now assemble the plots into figures.
  210. plots = [
  211. # Plot([[dphi1_v_eta_B1, dphi1_v_eta_B2],
  212. # [dz1_v_eta_B1, dz1_v_eta_B2 ]],
  213. # 'res1_v_eta'),
  214. # Plot([[dphi2_v_eta_B2, dphi2_v_eta_B3],
  215. # [dz2_v_eta_B2, dz2_v_eta_B3 ]],
  216. # 'res2_v_eta'),
  217. # Plot([[dphi1_B1, dphi1_B2],
  218. # [dz1_B1, dz1_B2 ]],
  219. # 'res1'),
  220. # Plot([[eta1_B1, eta1_B2],
  221. # [eta2_B2, eta2_B3 ]],
  222. # 'eta_dist'),
  223. # Plot([[dphi2_B2, dphi2_B3],
  224. # [dz2_B2, dz2_B3 ]],
  225. # 'res2'),
  226. # Plot([[dphi1_v_ladder_B1, dphi1_v_ladder_B2],
  227. # [dz1_v_ladder_B1, dz1_v_ladder_B2]],
  228. # 'res_v_ladder'),
  229. # Plot([[dphi1_sc_ex_B1, dphi1_sc_ex_B2],
  230. # [dz1_sc_ex_B1, dz1_sc_ex_B2]],
  231. # 'sc_ex_1'),
  232. # Plot([[dphi1_sc_ex_B1_even, dphi1_sc_ex_B2_even],
  233. # [dz1_sc_ex_B1_even, dz1_sc_ex_B2_even]],
  234. # 'sc_ex_1_even'),
  235. # Plot([[dphi1_sc_ex_B1_odd, dphi1_sc_ex_B2_odd],
  236. # [dz1_sc_ex_B1_odd, dz1_sc_ex_B2_odd]],
  237. # 'sc_ex_1_odd'),
  238. # Plot([[(dphi1_sc_ex_B1_odd, dphi1_sc_ex_B1_even), "FL"],
  239. # [dz1_sc_ex_B1_odd, dz1_sc_ex_B1_even]],
  240. # 'sc_ex_1_odd_v_even'),
  241. Plot([[dphi1_sc_ex_B1],
  242. [dphi1_B1]],
  243. 'sc_ex_v_sim_phi1_B1'),
  244. Plot([[dphi1_sc_ex_B2],
  245. [dphi1_B2]],
  246. 'sc_ex_v_sim_phi1_B2'),
  247. Plot([[dphi2_sc_ex_B2],
  248. [dphi2_B2]],
  249. 'sc_ex_v_sim_phi2_B2'),
  250. Plot([[dz1_sc_ex_B1],
  251. [dz1_B1]],
  252. 'sc_ex_v_sim_z1_B1'),
  253. Plot([[dz2_sc_ex_B2],
  254. [dz2_B2]],
  255. 'sc_ex_v_sim_z2_B2'),
  256. Plot([[dz1_sc_ex_B1],
  257. [dz1_sc_ex_B1_even],
  258. [dz1_sc_ex_B1_odd]],
  259. 'even_odd'),
  260. ]
  261. # Finally, render and save the plots and generate the html+bootstrap
  262. # dashboard to view them
  263. render(plots)
  264. generate_report(plots)