All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stack_spectra.py
Go to the documentation of this file.
1 import ROOT
2 import util
3 import argparse
4 
5 
6 def main(args):
7  hists = [[util.get_tobject(args, h.lstrip("-")) for h in hlist] for hlist in args.hstack]
8  util.validate_hists(args.hstack, hists)
9  hists = [[util.resize_histo(args, h) for h in hlist] for hlist in hists]
10 
11  canvas = ROOT.TCanvas("canvas", "Canvas", 250,100,700,500)
12  hstack = ROOT.THStack()
13 
14  for i, (hlist,hnames) in enumerate(zip(hists, args.hstack)):
15  h = hlist[0]
16  if len(hlist) > 1:
17  for hadd,hname in zip(hlist[1:],hnames[1:]):
18  #if "InTime-" in hname:
19  # h.Add(hadd, 10. / 1837.)
20  if hname.startswith("-"):
21  h.Add(hadd, -1)
22  else:
23  h.Add(hadd)
24 
25  if args.area_normalize and h.Integral() > 1e-4: h.Scale(1. / h.Integral())
26  color = util.namecolors(args.names[i] if args.names else hnames[0])
27  if args.stack:
28  h.SetFillColor(color)
29  else:
30  h.SetLineColor(color)
31  h.SetLineWidth(3)
32  if args.names:
33  name = args.names[i]
34  if args.nevent_in_legend:
35  name += " (%i)" % int(h.Integral())
36  h.SetTitle(name)
37  hstack.Add(h)
38 
39  drawstr = "HIST" if args.stack else "NOSTACK HIST"
40  hstack.Draw(drawstr)
41  util.style(args, canvas, hstack)
42  if args.legend_position: legend = ROOT.gPad.BuildLegend(*(args.legend_position + [""]))
43  if args.logy:
44  canvas.SetLogy()
45 
46  box = util.draw_text(args)
47  canvas.Update()
48 
49  util.wait(args)
50  util.write(args, canvas)
51 
52 if __name__ == "__main__":
53  parser = argparse.ArgumentParser()
54  parser = util.with_io_args(parser)
55  parser = util.with_histosize_args(parser)
56  parser = util.with_histostyle_args(parser)
57  parser = util.with_text_args(parser)
58  parser.add_argument("-ly", "--logy", action="store_true")
59  parser.add_argument("-hs", "--hstack", required=True, type=util.histo_list)
60  parser.add_argument("-s", "--stack", action="store_true")
61  parser.add_argument("-n", "--names", default=None, type=util.comma_separated)
62  parser.add_argument("-a", "--area_normalize", action="store_true")
63  parser.add_argument("--nevent_in_legend", action="store_true")
64  parser.add_argument("-lp", "--legend_position", default=None, type=util.legend_position)
65  main(parser.parse_args())
66 
def validate_hists
Definition: util.py:352
def with_text_args
Definition: util.py:54
def get_tobject
Definition: util.py:47
def style
Definition: util.py:237
def write
Definition: util.py:23
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def namecolors
Definition: util.py:276
def with_histostyle_args
Definition: util.py:220
def with_histosize_args
Definition: util.py:100
def wait
Definition: util.py:19
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
def draw_text
Definition: util.py:62
def with_io_args
Definition: util.py:84
def resize_histo
Definition: util.py:118