plots.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, render_plots, hist_plot, hist2d_plot, Plot)
  7. def generate_dashboard(plots, output='dashboard.htm'):
  8. from jinja2 import Environment, PackageLoader, select_autoescape
  9. from os.path import join
  10. from urllib.parse import quote
  11. env = Environment(
  12. loader=PackageLoader('plots', 'templates'),
  13. autoescape=select_autoescape(['htm', 'html', 'xml']),
  14. )
  15. env.globals.update({'quote': quote,
  16. 'enumerate': enumerate,
  17. 'zip': zip,
  18. })
  19. def render_to_file(template_name, **kwargs):
  20. with open(join('output', template_name), 'w') as tempout:
  21. template = env.get_template(template_name)
  22. tempout.write(template.render(**kwargs))
  23. def get_by_n(l, n=2):
  24. l = list(l)
  25. while l:
  26. yield l[:n]
  27. l = l[n:]
  28. render_to_file('dashboard.htm', plots=get_by_n(plots, 3),
  29. outdir="figures/")
  30. @decl_plot
  31. def plot_eff(rs, old):
  32. r''' '''
  33. if old:
  34. accept = hist(rs.accept_eff)
  35. reco = hist(rs.reco_eff)
  36. id_ = hist(rs.id_eff_old)
  37. ult = hist(rs.ult_eff_old)
  38. else:
  39. accept = hist(rs.accept_eff)
  40. reco = hist(rs.reco_eff)
  41. id_ = hist(rs.id_eff)
  42. ult = hist(rs.ult_eff)
  43. hist_plot(accept, include_errors=True, stats=False,
  44. color='k', label='acceptance eff wrt gen')
  45. hist_plot(reco, include_errors=True, stats=False,
  46. color='g', label='reco eff wrt accept')
  47. hist_plot(id_, include_errors=True, stats=False,
  48. color='b', label='id eff wrt reco')
  49. hist_plot(ult, include_errors=True, stats=False,
  50. color='r', label='id eff wrt gen')
  51. plt.xlabel('$p_T$(GeV)')
  52. plt.legend()
  53. @decl_plot
  54. def plot_eta(rs):
  55. r''' '''
  56. gen_eta = hist(rs.gen_eta)
  57. pf_eta = hist(rs.pf_eta)
  58. hist_plot(gen_eta, include_errors=True, stats=False,
  59. color='r', label='gen taus')
  60. hist_plot(pf_eta, include_errors=True, stats=False,
  61. color='g', label='pf taus(no id req)')
  62. plt.xlabel(r'$\eta$')
  63. plt.legend()
  64. if __name__ == '__main__':
  65. # First create a ResultSet object which loads all of the objects from output.root
  66. # into memory and makes them available as attributes
  67. # if len(sys.argv) != 2:
  68. # raise ValueError("please supply root file")
  69. rs_ft = ResultSet("ft", 'tau_ft.root')
  70. rs_dy = ResultSet("dy", 'tau_dy.root')
  71. # Next, declare all of the (sub)plots that will be assembled into full
  72. # figures later
  73. eff_ft = (plot_eff, (rs_ft, False), {})
  74. eff_dy = (plot_eff, (rs_dy, False), {})
  75. eff_ft_old = (plot_eff, (rs_ft, True), {})
  76. eff_dy_old = (plot_eff, (rs_dy, True), {})
  77. eta_ft = (plot_eta, (rs_ft,), {})
  78. eta_dy = (plot_eta, (rs_dy,), {})
  79. # Now assemble the plots into figures.
  80. plots = [
  81. Plot([[eff_ft],
  82. [eff_dy]],
  83. 'eff_byTightIsolationMVArun2v1DBdR03oldDMwLT'),
  84. Plot([[eff_ft_old],
  85. [eff_dy_old]],
  86. 'eff_byTightIsolationMVArun2v1DBoldDMwLT'),
  87. Plot([[eta_ft],
  88. [eta_dy]],
  89. 'eta'),
  90. ]
  91. # Finally, render and save the plots and generate the html+bootstrap
  92. # dashboard to view them
  93. render_plots(plots, to_disk=False)
  94. generate_dashboard(plots)