All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
savehist.py
Go to the documentation of this file.
1 import ROOT
2 import util
3 import argparse
4 from array import array
5 import math
6 
7 def main(args):
8  util.optstat(args)
9  hist = util.get_tobject(args, args.hist)
10  util.validate_hists([args.hist], [hist])
11  hist = util.resize_histo(args, hist)
12 
13  if args.make_percentages:
14  if isinstance(hist, ROOT.TH2D):
15  for i in range(1, hist.GetNbinsX()+1):
16  this_norm = hist.Integral(i, i, 1, hist.GetNbinsY())
17  for j in range(1, hist.GetNbinsY()+1):
18  if this_norm < 1e-3:
19  hist.SetBinContent(i, j, 0)
20  else:
21  hist.SetBinContent(i, j, hist.GetBinContent(i, j) / this_norm)
22  else:
23  for k in range(1, hist.GetNbinsZ()+1):
24  for i in range(1, hist.GetNbinsX()+1):
25  this_norm = hist.Integral(i, i, 1, hist.GetNbinsY(), k, k)
26  for j in range(1, hist.GetNbinsY()+1):
27  if this_norm < 1e-3:
28  hist.SetBinContent(i, j, k, 0)
29  else:
30  hist.SetBinContent(i, j, k, hist.GetBinContent(i, j, k) / this_norm)
31 
32  hist2 = None
33  if args.meanY:
34  hist2 = mean_and_err(hist)
35 
36  canvas = ROOT.TCanvas("canvas", "Canvas", 250,100,700,500)
37  drawstr = ""
38  if args.drawstr is not None:
39  drawstr = args.drawstr
40  elif isinstance(hist, ROOT.TH2D):
41  drawstr = "COLZ"
42  if args.draw_text:
43  drawstr += " TEXT"
44  hist.SetMarkerSize(3)
45  ROOT.gStyle.SetPaintTextFormat("1.3f")
46  elif isinstance(hist, ROOT.TH1D):
47  drawstr = "HIST"
48  elif isinstance(hist, ROOT.TGraph):
49  drawstr = "AL"
50  hist.Draw(drawstr)
51  if hist2:
52  hist2.SetLineColor(ROOT.kRed)
53  hist2.SetLineWidth(3)
54  hist2.SetMarkerSize(1)
55  hist2.SetMarkerStyle(21)
56  hist2.SetMarkerColor(ROOT.kRed)
57  hist2.Draw("P")
58  if args.title is not None:
59  hist.SetTitle(args.title)
60  util.style(args, canvas, hist)
61 
62  if args.logy:
63  canvas.SetLogy()
64  if args.logz:
65  canvas.SetLogz()
66  box = util.draw_text(args)
67 
68  canvas.Update()
69 
70  util.wait(args)
71  util.write(args, canvas)
72 
73 def project_variance(hist):
74  # calculate the variance in each y-axis -- this is the y-value
75  var = []
76  xs = []
77  for i in range(1, hist.GetNbinsX()+1):
78  this_sum = 0
79  n_entries = 0
80  for j in range(1, hist.GetNbinsY()+1):
81  this_sum += hist.GetYaxis().GetBinCenter(j) * hist.GetBinContent(i, j)
82  n_entries += hist.GetBinContent(i, j)
83  if n_entries < 1e-3: continue
84  this_mean = this_sum / n_entries if n_entries > 1e-3 else this_sum
85  this_var_sum = 0
86  for j in range(1, hist.GetNbinsY()+1):
87  this_var_sum += hist.GetBinContent(i, j) * (hist.GetYaxis().GetBinCenter(j) - this_mean)**2
88  xs.append(hist.GetXaxis().GetBinCenter(i))
89  this_var = this_var_sum / n_entries if n_entries > 1e-3 else this_var_sum
90  var.append(math.sqrt(this_var))
91  return ROOT.TGraph(len(xs), array('d', xs), array('d', var))
92 
93 def project_mean(hist):
94  # calculate the variance in each y-axis -- this is the y-value
95  mean = []
96  xs = []
97  for i in range(1, hist.GetNbinsX()+1):
98  this_sum = 0
99  n_entries = 0
100  for j in range(1, hist.GetNbinsY()+1):
101  this_sum += hist.GetYaxis().GetBinCenter(j) * hist.GetBinContent(i, j)
102  n_entries += hist.GetBinContent(i, j)
103  if n_entries < 1e-3: continue
104  this_mean = this_sum / n_entries if n_entries > 1e-3 else this_sum
105  mean.append(this_mean)
106  xs.append(hist.GetXaxis().GetBinCenter(i))
107  return ROOT.TGraph(len(xs), array('d', xs), array('d', mean))
108 
109 def mean_and_err(hist):
110  hist = hist.Clone()
111  hist.RebinX(4)
112  g_mean = project_mean(hist)
113  g_err = project_variance(hist)
114  x_err = array('d', [0 for i in range(len(g_mean.GetX()))])
115  return ROOT.TGraphErrors(len(g_mean.GetX()), g_mean.GetX(), g_mean.GetY(), x_err, g_err.GetY())
116 
117 if __name__ == "__main__":
118  parser = argparse.ArgumentParser()
119  parser = util.with_io_args(parser)
120  parser = util.with_histosize_args(parser)
121  parser = util.with_histostyle_args(parser)
122  parser = util.with_text_args(parser)
123  parser.add_argument("-t" ,"--title", default=None)
124  parser.add_argument("-hn", "--hist", required=True)
125  parser.add_argument("-ly", "--logy", action="store_true")
126  parser.add_argument("-lz", "--logz", action="store_true")
127  parser.add_argument("-meanY", "--meanY", action="store_true")
128  parser.add_argument("-mp", "--make_percentages", action="store_true")
129  parser.add_argument("-dt", "--draw_text", action="store_true")
130  parser.add_argument("-ds", "--drawstr", default=None)
131  main(parser.parse_args())
132 
def validate_hists
Definition: util.py:352
def with_text_args
Definition: util.py:54
def get_tobject
Definition: util.py:47
def main
Definition: savehist.py:7
def style
Definition: util.py:237
def write
Definition: util.py:23
def project_variance
Definition: savehist.py:73
def project_mean
Definition: savehist.py:93
def with_histostyle_args
Definition: util.py:220
def with_histosize_args
Definition: util.py:100
def wait
Definition: util.py:19
def optstat
Definition: util.py:233
def draw_text
Definition: util.py:62
def with_io_args
Definition: util.py:84
def resize_histo
Definition: util.py:118
def mean_and_err
Definition: savehist.py:109