All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
integrate_hist.py
Go to the documentation of this file.
1 import ROOT
2 import util
3 import argparse
4 
5 
6 def main(args):
7  f = ROOT.TFile(args.input)
8  hist = f.Get(args.hist)
9  if args.range_hi is not None and args.range_lo is not None:
10  lo_bin = hist.GetXaxis().FindBin(args.range_lo)
11  hi_bin = hist.GetXaxis().FindBin(args.range_hi)
12  integral = hist.Integral(lo_bin, hi_bin)
13  else:
14  integral = hist.Integral()
15  if args.ratio:
16  if args.overflow:
17  total = sum([hist.GetBinContent(i) for i in range(hist.GetNbinsX()+2)])
18  else:
19  total = hist.Integral()
20  integral = integral / total
21  print integral
22 
23 if __name__ == "__main__":
24  parser = argparse.ArgumentParser()
25  parser = util.with_input_args(parser)
26  parser.add_argument("-rl", "--range_lo", default=None, type=float)
27  parser.add_argument("-rh", "--range_hi", default=None, type=float)
28  parser.add_argument("-hn", "--hist", required=True)
29  parser.add_argument("-r", "--ratio", action="store_true")
30  parser.add_argument("-o", "--overflow", action="store_true")
31  main(parser.parse_args())
32 
def with_input_args
Definition: util.py:27