All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cut_metrics.py
Go to the documentation of this file.
1 import ROOT
2 import util
3 import argparse
4 from array import array
5 
6 def get_histos(f, variable, cut, names):
7  return [f.Get(variable + "_" + n + cut) for n in names]
8 
9 def get_integral(f, variable, cut, names):
10  return sum([h.Integral() for h in get_histos(f, variable, cut, names)])
11 
12 def main(args):
13  f = ROOT.TFile(args.input)
14  effs = []
15  puritys = []
16 
17  signal_base = get_integral(f, args.variable, args.base_cut, args.signal_names)
18  background_base = get_integral(f, args.variable, args.base_cut, args.background_names)
19 
20  effs.append(1.)
21  puritys.append(signal_base / (signal_base + background_base))
22 
23  for cut in args.cuts:
24  this_signal = get_integral(f, args.variable, cut, args.signal_names)
25  this_background = get_integral(f, args.variable, cut, args.background_names)
26  this_eff = this_signal / signal_base
27  this_purity = this_signal / (this_signal + this_background)
28  effs.append(this_eff)
29  puritys.append(this_purity)
30 
31  effs = array('d', effs)
32  puritys = array('d', puritys)
33  xvals = array('d', range(len(effs)))
34 
35  g_eff = ROOT.TGraph(len(effs), xvals, effs)
36  g_eff.SetName("Efficiency")
37  g_eff.SetTitle("Efficiency")
38 
39  g_purity = ROOT.TGraph(len(puritys), xvals, puritys)
40  g_purity.SetName("Purity")
41  g_purity.SetTitle("Purity")
42 
43  canvas = ROOT.TCanvas("canvas", "Canvas", 250,100,700,500)
44 
45  g_eff.Draw("AL")
46  g_eff.SetLineColor(ROOT.kGreen)
47  util.style(args, g_eff)
48  for i in range(len(effs)):
49  g_eff.GetXaxis().SetBinLabel(g_eff.GetXaxis().FindBin(i), args.names[i])
50  g_purity.Draw("SAME")
51  g_purity.SetLineColor(ROOT.kRed)
52 
53  legend = ROOT.gPad.BuildLegend(0.75,0.75,0.95,0.95,"")
54  canvas.Update()
55 
56  util.wait(args)
57  util.write(args, canvas)
58 
59 if __name__ == "__main__":
60  parser = argparse.ArgumentParser()
61  parser = util.with_io_args(parser)
62  parser = util.with_histostyle_args(parser)
63  parser.add_argument("-s", "--signal_names", type=util.comma_separated, required=True)
64  parser.add_argument("-b", "--background_names", type=util.comma_separated, required=True)
65  parser.add_argument("-bc", "--base_cut", required=True)
66  parser.add_argument("-c", "--cuts", type=util.comma_separated, required=True)
67  parser.add_argument("-n", "--names", type=util.comma_separated, required=True)
68  parser.add_argument("-v", "--variable", required=True)
69  main(parser.parse_args())
70 
def get_histos
Definition: cut_metrics.py:6
def get_integral
Definition: cut_metrics.py:9
def style
Definition: util.py:237
def write
Definition: util.py:23
def with_histostyle_args
Definition: util.py:220
def wait
Definition: util.py:19
def with_io_args
Definition: util.py:84