123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- #!/usr/bin/env python
- import sys
- import matplotlib.pyplot as plt
- from filval.result_set import ResultSet
- from filval.histogram_utils import hist, hist2d, hist_slice
- from filval.plotter import make_plot, plot_registry, hist_plot, hist2d_plot
- @make_plot(scale=0.85)
- def first_hits_v_eta(rs):
- r'''
- Plots of $\Delta \phi$, $\Delta z$, and $\Delta r$ vs $\eta$ between RecHit and SimHit for
- the first matched hit in the seed.
- '''
- plt.subplot(321)
- hist2d_plot(hist2d(rs.dphi_v_eta_first_hits_in_B1),
- ylabel=r"$\Delta \phi_1$(rad)",
- title="BPIX - Layer 1")
- plt.subplot(322)
- hist2d_plot(hist2d(rs.dphi_v_eta_first_hits_in_B2),
- title="BPIX - Layer 2")
- plt.subplot(323)
- hist2d_plot(hist2d(rs.dz_v_eta_first_hits_in_B1),
- ylabel=r"$\Delta \textrm{z}_1$(cm)",
- )
- plt.subplot(324)
- hist2d_plot(hist2d(rs.dz_v_eta_first_hits_in_B2))
- plt.subplot(325)
- hist2d_plot(hist2d(rs.dr_v_eta_first_hits_in_B1),
- ylabel=r"$\Delta r_1$(cm)",
- xlabel=r"$\eta$"
- )
- plt.subplot(326)
- hist2d_plot(hist2d(rs.dr_v_eta_first_hits_in_B2),
- xlabel=r"$\eta$"
- )
- @make_plot(scale=0.85)
- def second_hits_v_eta(rs):
- plt.subplot(321)
- hist2d_plot(hist2d(rs.dphi_v_eta_second_hits_in_B2),
- ylabel=r"$\Delta \phi_2$(rad)",
- title="BPIX - Layer 2")
- plt.subplot(322)
- hist2d_plot(hist2d(rs.dphi_v_eta_second_hits_in_B3),
- title="BPIX - Layer 3")
- plt.subplot(323)
- hist2d_plot(hist2d(rs.dz_v_eta_second_hits_in_B2),
- ylabel=r"$\Delta \textrm{z}_2$(cm)")
- plt.subplot(324)
- hist2d_plot(hist2d(rs.dz_v_eta_second_hits_in_B3))
- plt.subplot(325)
- hist2d_plot(hist2d(rs.dr_v_eta_second_hits_in_B2),
- ylabel=r"$\Delta r_2$(cm)",
- xlabel=r"$\eta$")
- plt.subplot(326)
- hist2d_plot(hist2d(rs.dr_v_eta_second_hits_in_B3),
- xlabel=r"$\eta$")
- @make_plot(scale=0.85)
- def first_hits(rs):
- plt.subplot(321)
- hist_plot(hist(rs.dphi_v_eta_first_hits_in_B1.ProjectionY()),
- include_errors=True, xlabel=r"$\Delta \phi_1$(rad)",
- title="BPIX - Layer 1")
- plt.subplot(322)
- hist_plot(hist(rs.dphi_v_eta_first_hits_in_B2.ProjectionY()),
- include_errors=True,
- xlabel=r"$\Delta \phi_1$(rad)",
- title="BPIX - Layer 2")
- plt.subplot(323)
- hist_plot(hist(rs.dz_v_eta_first_hits_in_B1.ProjectionY()),
- include_errors=True,
- xlabel=r"$\Delta z_1$(cm)")
- plt.subplot(324)
- hist_plot(hist(rs.dz_v_eta_first_hits_in_B2.ProjectionY()),
- include_errors=True,
- xlabel=r"$\Delta z_1$(cm)")
- plt.subplot(325)
- hist_plot(hist(rs.dr_v_eta_first_hits_in_B1.ProjectionY()),
- include_errors=True,
- xlabel=r"$\Delta r_1$(cm)")
- plt.subplot(326)
- hist_plot(hist(rs.dr_v_eta_first_hits_in_B2.ProjectionY()),
- include_errors=True,
- xlabel="r$\Delta r_1$(cm)")
- @make_plot(scale=0.85)
- def second_hits(rs):
- plt.subplot(321)
- hist_plot(hist(rs.dphi_v_eta_second_hits_in_B2.ProjectionY()),
- include_errors=True,
- xlabel=r"$\Delta \phi_2$(rad)",
- title="BPIX - Layer 2")
- plt.subplot(322)
- hist_plot(hist(rs.dphi_v_eta_second_hits_in_B3.ProjectionY()),
- include_errors=True,
- xlabel=r"$\Delta \phi_2$(rad)",
- title="BPIX - Layer 3")
- plt.subplot(323)
- hist_plot(hist(rs.dz_v_eta_second_hits_in_B2.ProjectionY()),
- include_errors=True,
- xlabel=r"$\Delta z_2$(cm)")
- plt.subplot(324)
- hist_plot(hist(rs.dz_v_eta_second_hits_in_B3.ProjectionY()),
- include_errors=True,
- xlabel=r"$\Delta z_2$(cm)")
- plt.subplot(325)
- hist_plot(hist(rs.dr_v_eta_second_hits_in_B2.ProjectionY()),
- include_errors=True,
- xlabel=r"$\Delta r_2$(cm)")
- plt.subplot(326)
- hist_plot(hist(rs.dr_v_eta_second_hits_in_B3.ProjectionY()),
- include_errors=True,
- xlabel=r"$\Delta r_2$(cm)")
- @make_plot(scale=0.85)
- def delta_phi_z_v_ladder(rs):
- def even_odd_plot(even, odd, var, scale, unit, **kwargs):
- even_dist = even.ProjectionY()
- odd_dist = odd.ProjectionY()
- hist_plot(hist(even_dist), include_errors=True, label="even ladders")
- hist_plot(hist(odd_dist), include_errors=True, color='r', label="odd ladders",
- **kwargs)
- even_mean = even_dist.GetMean()*scale
- odd_mean = odd_dist.GetMean()*scale
- axes = plt.gca()
- txt = r"$ \hat{{\Delta {0} }}_{{1,\textrm{{ {1} }} }}={2:4.2g}${3}"
- axes.text(0.05, .7, txt.format(var, "odd", odd_mean, unit), transform=axes.transAxes)
- axes.text(0.05, .6, txt.format(var, "even", even_mean, unit), transform=axes.transAxes)
- plt.subplot(221)
- even_odd_plot(rs.dphi_v_eta_first_hits_in_B1_even_ladder, rs.dphi_v_eta_first_hits_in_B1_odd_ladder,
- r"\phi", 10**6, "urad", xlabel=r"$\Delta \phi_1$(rad)", title="BPIX - Layer 1")
- plt.subplot(222)
- even_odd_plot(rs.dphi_v_eta_first_hits_in_B2_even_ladder, rs.dphi_v_eta_first_hits_in_B2_odd_ladder,
- r"\phi", 10**6, "urad", xlabel=r"$\Delta \phi_1$(rad)", title="BPIX - Layer 2")
- plt.legend()
- plt.subplot(223)
- even_odd_plot(rs.dz_v_eta_first_hits_in_B1_even_ladder, rs.dz_v_eta_first_hits_in_B1_odd_ladder,
- "z", 10**4, "um", xlabel=r"$\Delta z_1$(cm)")
- plt.subplot(224)
- even_odd_plot(rs.dz_v_eta_first_hits_in_B2_even_ladder, rs.dz_v_eta_first_hits_in_B2_odd_ladder,
- "z", 10**4, "um", xlabel=r"$\Delta z_1$(cm)")
- @make_plot(scale=0.85)
- def sc_extrapolation_first(rs):
- ''' Raphael's plots '''
- norm = 1
- errors = True
- def preproc(h):
- return hist_slice(hist(h.ProjectionY()), (-0.07, 0.07))
- plt.subplot(221)
- hist_plot(hist(rs.sc_first_hits_in_B1_dz.ProjectionY()),
- include_errors=errors,
- norm=norm,
- log=False,
- xlabel=r"$\Delta z_1$(cm)",
- title="BPIX - Layer 1")
- plt.subplot(222)
- hist_plot(hist(rs.sc_first_hits_in_B2_dz.ProjectionY()),
- include_errors=errors,
- norm=norm,
- log=False,
- xlabel=r"$\Delta z_1$(cm)",
- title="BPIX - Layer 2")
- plt.subplot(223)
- hist_plot(preproc(rs.sc_first_hits_in_B1_dphi),
- include_errors=errors,
- norm=norm,
- log=False,
- # ylim=(0.5E-3, 5),
- xlabel=r"$\Delta \phi_1$(rad)",
- # xlim=(-0.06, 0.06)
- )
- plt.subplot(224)
- hist_plot(preproc(rs.sc_first_hits_in_B2_dphi),
- include_errors=errors,
- norm=norm,
- log=False,
- # ylim=(0.5E-3, 5),
- xlabel=r"$\Delta \phi_1$(rad)",
- # xlim=(-0.06, 0.06)
- )
- @make_plot(scale=0.85)
- def sc_extrapolation_second(rs):
- from scipy.stats import norm
- def gauss(x, A, mu, sigma):
- return A*norm.pdf(x, mu, sigma)
- def gauss2(x, A1, mu1, sigma1, A2, mu2, sigma2):
- return (A1*norm.pdf(x, mu1, sigma1) +
- A2*norm.pdf(x, mu2, sigma2))
- integral = 1
- errors = True
- def preproc(h, slice_=None):
- h = hist(h.ProjectionY())
- if slice_:
- h = hist_slice(h, slice_)
- return h
- plt.subplot(221)
- hist_plot(preproc(rs.sc_second_hits_in_B2_dz),
- include_errors=errors,
- norm=integral,
- log=False,
- fit=(gauss2, (1, 0, 0.025, 1, 0, 0.005)),
- xlabel=r"$\Delta z_2$(cm)",
- title="BPIX - Layer 2")
- plt.subplot(222)
- hist_plot(preproc(rs.sc_second_hits_in_B3_dz),
- include_errors=errors,
- norm=integral,
- log=False,
- fit=(gauss2, (1, 0, 0.025, 1, 0, 0.005)),
- xlabel=r"$\Delta z_2$(cm)",
- title="BPIX - Layer 3")
- plt.subplot(223)
- hist_plot(preproc(rs.sc_second_hits_in_B2_dphi, (-0.09, 0.09)),
- include_errors=errors,
- norm=integral,
- log=False,
- fit=(gauss2, (1, 0, 0.015, 1, 0, 0.005)),
- xlabel=r"$\Delta \phi_2$(rad)")
- plt.subplot(224)
- hist_plot(preproc(rs.sc_second_hits_in_B3_dphi, (-0.09, 0.09)),
- include_errors=errors,
- norm=integral,
- log=False,
- fit=(gauss2, (1, 0, 0.025, 1, 0, 0.005)),
- xlabel=r"$\Delta \phi_2$(rad)")
- def generate_dashboard():
- from jinja2 import Environment, PackageLoader, select_autoescape
- from os.path import join
- from urllib.parse import quote
- env = Environment(
- loader=PackageLoader('plots', 'templates'),
- autoescape=select_autoescape(['htm', 'html', 'xml'])
- )
- def render_to_file(template_name, **kwargs):
- with open(join('output', template_name), 'w') as tempout:
- template = env.get_template(template_name)
- tempout.write(template.render(**kwargs))
- def get_by_n(l, n=2):
- l = list(l)
- while l:
- yield l[:n]
- l = l[n:]
- render_to_file('dashboard.htm', plots=get_by_n(plot_registry.values(), 3),
- quote=quote)
- if __name__ == '__main__':
- # First create a ResultSet object which loads all of the objects from output.root
- # into memory and makes them available as attributes
- if len(sys.argv) != 2:
- raise ValueError("please supply root file")
- rs = ResultSet("DY2LL", sys.argv[1])
- first_hits_v_eta(rs)
- second_hits_v_eta(rs)
- first_hits(rs)
- second_hits(rs)
- delta_phi_z_v_ladder(rs)
- sc_extrapolation_first(rs)
- sc_extrapolation_second(rs)
- generate_dashboard()
|