ソースを参照

Adds plotting routines for reco eff and yields

Caleb Fangmeier 6 年 前
コミット
311168c5fe
共有6 個のファイルを変更した314 個の追加0 個の削除を含む
  1. 5 0
      .gitignore
  2. 3 0
      .gitmodules
  3. 1 0
      filval
  4. 112 0
      plots.py
  5. 100 0
      templates/dashboard.htm
  6. 93 0
      yields.py

+ 5 - 0
.gitignore

@@ -0,0 +1,5 @@
+*__pycache__/
+output/
+env/
+
+*.root

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "filval"]
+	path = filval
+	url = gogs@git.fangmeier.tech:caleb/filval.git

+ 1 - 0
filval

@@ -0,0 +1 @@
+Subproject commit f7ef387b4ef4fdd81c7789f2fa35b72a9465ebfb

+ 112 - 0
plots.py

@@ -0,0 +1,112 @@
+#!/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, hist_add
+from filval.plotter import (decl_plot, render_plots, hist_plot, hist2d_plot, Plot)
+
+def generate_dashboard(plots, output='dashboard.htm'):
+    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']),
+    )
+    env.globals.update({'quote': quote,
+                        'enumerate': enumerate,
+                        'zip': zip,
+                        })
+
+    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(plots, 3),
+                   outdir="figures/")
+
+
+@decl_plot
+def plot_eff(rs, old):
+    r''' '''
+    if old:
+        accept = hist(rs.accept_eff)
+        reco = hist(rs.reco_eff)
+        id_ = hist(rs.id_eff_old)
+        ult = hist(rs.ult_eff_old)
+    else:
+        accept = hist(rs.accept_eff)
+        reco = hist(rs.reco_eff)
+        id_ = hist(rs.id_eff)
+        ult = hist(rs.ult_eff)
+    hist_plot(accept, include_errors=True, stats=False,
+              color='k', label='acceptance eff wrt gen')
+    hist_plot(reco, include_errors=True, stats=False,
+              color='g', label='reco eff wrt accept')
+    hist_plot(id_, include_errors=True, stats=False,
+              color='b', label='id eff wrt reco')
+    hist_plot(ult, include_errors=True, stats=False,
+              color='r', label='id eff wrt gen')
+    plt.xlabel('$p_T$(GeV)')
+    plt.legend()
+
+
+@decl_plot
+def plot_eta(rs):
+    r''' '''
+    gen_eta = hist(rs.gen_eta)
+    pf_eta = hist(rs.pf_eta)
+    hist_plot(gen_eta, include_errors=True, stats=False,
+              color='r', label='gen taus')
+    hist_plot(pf_eta, include_errors=True, stats=False,
+              color='g', label='pf taus(no id req)')
+    plt.xlabel(r'$\eta$')
+    plt.legend()
+
+
+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_ft = ResultSet("ft", 'tau_ft.root')
+    rs_dy = ResultSet("dy", 'tau_dy.root')
+
+    # Next, declare all of the (sub)plots that will be assembled into full
+    # figures later
+    eff_ft = (plot_eff, (rs_ft, False), {})
+    eff_dy = (plot_eff, (rs_dy, False), {})
+
+    eff_ft_old = (plot_eff, (rs_ft, True), {})
+    eff_dy_old = (plot_eff, (rs_dy, True), {})
+
+    eta_ft = (plot_eta, (rs_ft,), {})
+    eta_dy = (plot_eta, (rs_dy,), {})
+
+    # Now assemble the plots into figures.
+    plots = [
+        Plot([[eff_ft],
+              [eff_dy]],
+             'eff_byTightIsolationMVArun2v1DBdR03oldDMwLT'),
+        Plot([[eff_ft_old],
+              [eff_dy_old]],
+             'eff_byTightIsolationMVArun2v1DBoldDMwLT'),
+        Plot([[eta_ft],
+              [eta_dy]],
+             'eta'),
+    ]
+
+    # Finally, render and save the plots and generate the html+bootstrap
+    # dashboard to view them
+    render_plots(plots, to_disk=False)
+    generate_dashboard(plots)

+ 100 - 0
templates/dashboard.htm

@@ -0,0 +1,100 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <title>TTTT Dashboard</title>
+  <meta charset="utf-8">
+  <meta name="viewport" content="width=device-width, initial-scale=2">
+  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
+  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
+  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
+
+<script type="text/x-mathjax-config">
+MathJax.Hub.Config({
+  config: ["MMLorHTML.js"],
+  jax: ["input/TeX", "output/HTML-CSS", "output/NativeMML"],
+  extensions: ["MathMenu.js", "MathZoom.js"]
+});
+</script>
+<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS_HTML-full"> </script>
+</head>
+<body>
+<div class="container-fluid">
+{% for r, plot_row in enumerate(plots) %}
+  <div class="row">
+  {% for c, plot in enumerate(plot_row) %}
+    <div class="col-md-4">
+      <div class="well">
+    {% if plot.title %}
+        <h3 class="text-center">{{ plot.title }}</h3>
+    {% endif %}
+        <div>
+          <a href="#" title="{{ plot.name }}">
+            <img src="data:img/png;base64,{{ plot.data }}" style="width:100%" class="thumbnail img-responsive">
+          </a>
+        </div>
+        <div class="caption">
+          <p class="text-center"> {{ plot.name }} </p>
+          <div class="panel-group" id="accordion">
+    {% for id, (i,j) in enumerate(plot.docs.keys()) %}
+            <div class="panel-heading">
+              <h4 class="panel-title">
+                <button data-toggle="collapse" data-parent="#accordion" class="btn btn-info" href="#collapse{{r}}-{{c}}-{{id}}">
+                  Plot at ({{i+1}}, {{j+1}})</button>
+              </h4>
+            </div>
+            <div id="collapse{{r}}-{{c}}-{{id}}" class="panel-collapse collapse">
+              <div class="panel-body">
+      {% for doc, argdict in zip(plot.docs[(i,j)], plot.argdicts[(i,j)]) %}
+                <p class="text-left">{{ doc|safe }}</p>
+                <hr>
+                <p class="text-left"><strong>Plot Arguments</strong></p>
+                <table class="table table-hover">
+                  <tbody>
+        {% for key, val in argdict.items() %}
+                    <tr>
+                      <td>{{ key }}</td> <td>{{ val }}</td>
+                    </tr>
+        {% endfor %}
+                  </tbody>
+                </table>
+      {% endfor %}
+              </div>
+            </div>
+    {% endfor %}
+          </div>
+        </div>
+      </div>
+    </div>
+  {% endfor %}
+  </div>
+{% endfor %}
+</div>
+<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
+  <div class="modal-dialog modal-lg">
+    <div class="modal-content">
+      <div class="modal-header">
+        <button type="button" class="close" data-dismiss="modal">×</button>
+        <h3 class="modal-title">Heading</h3>
+      </div>
+      <div class="modal-body">
+
+      </div>
+   </div>
+  </div>
+</div>
+</body>
+<style>
+.modal-dialog {width:900px;}
+.thumbnail {margin-bottom:6px;}
+.modal-title {text-align:center;}
+</style>
+<script>
+$('.thumbnail').click(function(){
+    $('.modal-body').empty();
+    var title = $(this).parent('a').attr("title");
+    $('.modal-title').html(title);
+    $($(this).parents('div').html()).appendTo('.modal-body');
+    $('#myModal').modal({show:true});
+});
+</script>
+</html>

+ 93 - 0
yields.py

@@ -0,0 +1,93 @@
+#!/usr/bin/env python
+import matplotlib.pyplot as plt
+
+from filval.result_set import ResultSet
+from filval.histogram_utils import hist
+from filval.plotter import (decl_plot, render_plots, hist_plot, Plot)
+
+
+def generate_dashboard(plots, output='dashboard.htm'):
+    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']),
+    )
+    env.globals.update({'quote': quote,
+                        'enumerate': enumerate,
+                        'zip': zip,
+                        })
+
+    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(objs, n=2):
+        objs = list(objs)
+        while objs:
+            yield objs[:n]
+            objs = objs[n:]
+
+    render_to_file(output, plots=get_by_n(plots, 3),
+                   outdir="figures/")
+
+
+@decl_plot
+def plot_yield(rss):
+    r''' '''
+    from filval.plotter import StackHist
+    ft, ttw, ttz, tth = map(lambda rs: hist(rs.SRs), rss)
+    # ft10 = ft[0]*10, ft[1]*10, ft[2]
+    sh = StackHist()
+    sh.add_mc_background(rss[1].SRs, 'TTW')
+    sh.set_mc_signal(rss[0].SRs, 'TTTT')
+    sh.draw(plt.gca())
+    # hist_plot(ft10, include_errors=False, stats=False,
+    #           color='k', label='TTTT (x10)')
+    
+    # bg = 
+    # plt.hist(
+    # hist_plot(ttw, include_errors=False, stats=False,
+    #           color='g', label='TTW')
+    # hist_plot(ttz, include_errors=False, stats=False,
+    #           color='r', label='TTZ')
+    # hist_plot(tth, include_errors=False, stats=False,
+    #           color='b', label='TTH')
+    # plt.xlabel('Signal Region')
+    # plt.legend()
+
+
+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")
+    rss = (ResultSet("ft", 'yield_ft.root'),
+           ResultSet("ttw", 'yield_ttw.root'),
+           ResultSet("ttz", 'yield_ttz.root'),
+           ResultSet("tth", 'yield_tth.root'))
+    rss_notau = (ResultSet("ft_notau", 'yield_ft_notau.root'),
+                 ResultSet("ttw_notau", 'yield_ttw_notau.root'),
+                 ResultSet("ttz_notau", 'yield_ttz_notau.root'),
+                 ResultSet("tth_notau", 'yield_tth_notau.root'))
+
+    # Next, declare all of the (sub)plots that will be assembled into full
+    # figures later
+    yield_tau = (plot_yield, (rss,), {})
+    yield_notau = (plot_yield, (rss_notau,), {})
+
+    # Now assemble the plots into figures.
+    plots = [
+        Plot([[yield_tau]],
+             'Yield With Tau'),
+        Plot([[yield_notau]],
+             'Yield Without Tau'),
+    ]
+
+    # Finally, render and save the plots and generate the html+bootstrap
+    # dashboard to view them
+    render_plots(plots, to_disk=False)
+    generate_dashboard(plots)