plots.py 10 KB

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