Functions
HandyFuncs Namespace Reference

Functions

def PrintError (val, err)
 
def printlist (mylist)
 
def GetHists (c1)
 
def VerticalRange (hists, xrange=(0, 0), ratio=False, forceOne=True, ignoreError=False, maxerr=0.25, absMax=-999, absMin=-999, buffer=0.05)
 
def HorizontalRange (hists, mustinclude=[])
 
def ChisqCount (h1, h2)
 
def MakeGradient (nsteps, start, end)
 
def Intersections (hist, value, interp=False)
 
def gIntersections (graph, value, xrng, nsteps=1000)
 
def Interpolate (x1, y1, x2, y2, yvalue)
 
def pbloop (iterable, name="Entries")
 
def BinWidthNormalize (h, width=-1)
 
def MakeBins (bmin, bmax, nbins, log=False)
 
def DrawDiv (x, c1)
 
def DrawLine (c1, val=0)
 
def mean (lst)
 
def stdev (lst)
 
def rms (lst)
 
def ProfileX (hist)
 
def ProfileY (hist)
 

Function Documentation

def HandyFuncs.BinWidthNormalize (   h,
  width = -1 
)

Definition at line 222 of file HandyFuncs.py.

222 def BinWidthNormalize(h, width = -1):
223  if width < 0:
224  width = h.GetBinWidth(1)
225 
226  for i in range(1, h.GetNbinsX()+1):
227  wi = h.GetBinWidth(i)
228  w = wi/width
229  c = h.GetBinContent(i)
230  e = h.GetBinError(i)
231  h.SetBinContent(i, c/w)
232  h.SetBinError(i, e/w)
233 
234 
235 
def BinWidthNormalize(h, width=-1)
Definition: HandyFuncs.py:222
def HandyFuncs.ChisqCount (   h1,
  h2 
)

Definition at line 116 of file HandyFuncs.py.

116 def ChisqCount(h1, h2):
117  sum1 = 0
118  sum2 = 0
119  w1 = 0
120  w2 = 0
121 
122  for bin in range(1, h1.GetNbinsX()+1):
123  sum1 += h1.GetBinContent(bin)
124  sum2 += h2.GetBinContent(bin)
125  ew1 = h1.GetBinError(bin)
126  ew2 = h2.GetBinError(bin)
127  w1 += ew1*ew1
128  w2 += ew2*ew2
129 
130  # My simple chisquared method
131  delta = sum1 - sum2
132  sigmasq = w1 + w2
133  chi2 = delta*delta / sigmasq
134  prob = TMath.Prob(chi2, 1)
135 
136  return prob
137 
def ChisqCount(h1, h2)
Definition: HandyFuncs.py:116
def HandyFuncs.DrawDiv (   x,
  c1 
)

Definition at line 248 of file HandyFuncs.py.

248 def DrawDiv(x, c1):
249  c1.cd()
250  c1.Update()
251  ldiv = TLine(x, c1.GetUymin(), x, c1.GetUymax())
252  ldiv.SetLineStyle(7)
253  ldiv.SetLineWidth(2)
254  ldiv.Draw()
255  return ldiv
256 
def DrawDiv(x, c1)
Definition: HandyFuncs.py:248
def HandyFuncs.DrawLine (   c1,
  val = 0 
)

Definition at line 257 of file HandyFuncs.py.

257 def DrawLine(c1, val = 0):
258  c1.cd()
259  c1.Update()
260  ldiv = TLine(c1.GetUxmin(), val, c1.GetUxmax(), val)
261  ldiv.SetLineStyle(1)
262  ldiv.SetLineWidth(1)
263  ldiv.Draw()
264  return ldiv
265 
def DrawLine(c1, val=0)
Definition: HandyFuncs.py:257
def HandyFuncs.GetHists (   c1)

Definition at line 31 of file HandyFuncs.py.

31 def GetHists(c1):
32  prims = list(c1.GetListOfPrimitives())
33  return [x for x in prims if x.InheritsFrom("TH1")]
34 
35 
def GetHists(c1)
Definition: HandyFuncs.py:31
def HandyFuncs.gIntersections (   graph,
  value,
  xrng,
  nsteps = 1000 
)

Definition at line 185 of file HandyFuncs.py.

185 def gIntersections(graph, value, xrng, nsteps=1000):
186  intersections = []
187 
188  xmin, xmax = xrng
189  xstp = (xmax - xmin)//(nsteps - 1)
190 
191  for xi in range(nsteps-1):
192  x1 = xmin + (xi+0)*xstp
193  x2 = xmin + (xi+1)*xstp
194  y1 = graph.Eval(x1)
195  y2 = graph.Eval(x2)
196  lowleft = (y1 < value and y2 > value)
197  lowright = (y1 > value and y2 < value)
198 
199  if lowleft or lowright:
200  intersections += [ Interpolate(x1, y1, x2, y2, value) ]
201  return intersections
202 
203 
def Interpolate(x1, y1, x2, y2, yvalue)
Definition: HandyFuncs.py:204
def gIntersections(graph, value, xrng, nsteps=1000)
Definition: HandyFuncs.py:185
def HandyFuncs.HorizontalRange (   hists,
  mustinclude = [] 
)

Definition at line 93 of file HandyFuncs.py.

93 def HorizontalRange(hists, mustinclude=[]):
94  low = 1e90
95  high = -1e90
96 
97  for hist in hists:
98  mean = hist.GetMean()
99  rms = hist.GetRMS()
100  low = min(low, mean-2*rms)
101  high = max(high, mean+2*rms)
102 
103  for m in mustinclude:
104  low = min(low, m)
105  high = max(high, m)
106 
107 
108  adjust = (high-low)*0.05
109  low -= adjust
110  high += adjust
111 
112  for hist in hists:
113  hist.GetXaxis().SetRangeUser(low,high)
114 
115 
def HorizontalRange(hists, mustinclude=[])
Definition: HandyFuncs.py:93
static int max(int a, int b)
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
def HandyFuncs.Interpolate (   x1,
  y1,
  x2,
  y2,
  yvalue 
)

Definition at line 204 of file HandyFuncs.py.

204 def Interpolate(x1, y1, x2, y2, yvalue):
205  m = (y2-y1)/(x2-x1)
206  return x1 + (yvalue-y1)/m
207 
208 
def Interpolate(x1, y1, x2, y2, yvalue)
Definition: HandyFuncs.py:204
def HandyFuncs.Intersections (   hist,
  value,
  interp = False 
)

Definition at line 164 of file HandyFuncs.py.

164 def Intersections(hist, value, interp = False):
165  intersections = []
166  for xb in range(1,hist.GetNbinsX()):
167  x1 = hist.GetXaxis().GetBinLowEdge(xb)
168  y1 = hist.GetBinContent(xb)
169  x2 = hist.GetXaxis().GetBinLowEdge(xb+1)
170  y2 = hist.GetBinContent(xb+1)
171  x3 = hist.GetXaxis().GetBinLowEdge(xb+2)
172  lowleft = (y1 < value and y2 > value)
173  lowright = (y1 > value and y2 < value)
174 
175  if lowleft or lowright:
176  if interp:
177  intersections += [ Interpolate(x1, y1, x2, y2, value) ]
178  elif lowleft:
179  intersections += [ x3 ]
180  elif lowright:
181  intersections += [ x2 ]
182 
183  return intersections
184 
def Interpolate(x1, y1, x2, y2, yvalue)
Definition: HandyFuncs.py:204
def Intersections(hist, value, interp=False)
Definition: HandyFuncs.py:164
def HandyFuncs.MakeBins (   bmin,
  bmax,
  nbins,
  log = False 
)

Definition at line 236 of file HandyFuncs.py.

236 def MakeBins(bmin, bmax, nbins, log=False):
237  bins = []
238  if log:
239  bmin = log10(bmin)
240  bmax = log10(bmax)
241  bins = [ x*(bmax-bmin)/(nbins - 1.) + bmin for x in range(nbins) ]
242  if log:
243  bins = [10.**x for x in bins]
244  return array('d',bins)
245 
246 
247 
def MakeBins(bmin, bmax, nbins, log=False)
Definition: HandyFuncs.py:236
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
def HandyFuncs.MakeGradient (   nsteps,
  start,
  end 
)

Definition at line 138 of file HandyFuncs.py.

138 def MakeGradient(nsteps, start, end):
139  from ROOT import TColor
140  r1, g1, b1 = start
141  r2, g2, b2 = end
142 
143 # print start, "to", end
144 
145 
146  gradient = []
147 
148  rstep = float(r2 - r1)/float(nsteps-1)
149  gstep = float(g2 - g1)/float(nsteps-1)
150  bstep = float(b2 - b1)/float(nsteps-1)
151 
152 # print "steps", rstep, gstep, bstep
153 
154  for i in range(nsteps):
155  r = r1 + rstep*i
156  g = g1 + gstep*i
157  b = b1 + bstep*i
158  color = TColor.GetColor(r, g, b)
159 # print "(%.2f, %.2f, %.2f) = %i" %(r,g,b, color)
160  gradient.append(color)
161 
162  return gradient
163 
def MakeGradient(nsteps, start, end)
Definition: HandyFuncs.py:138
def HandyFuncs.mean (   lst)

Definition at line 266 of file HandyFuncs.py.

266 def mean(lst):
267  return sum(lst)/len(lst)
268 
def mean(lst)
Definition: HandyFuncs.py:266
def HandyFuncs.pbloop (   iterable,
  name = "Entries" 
)

Definition at line 209 of file HandyFuncs.py.

209 def pbloop(iterable, name = "Entries"):
210  widgets = [ name+': ',pb.Value(), '/', pb.Total(), ' ', pb.Percentage(), ' ',
211  pb.Bar(marker='=',left='[',right=']'),
212  ' ',pb.ETA() ]
213  pbar = pb.ProgressBar(widgets=widgets, maxval=len(iterable), term_width=100)
214  pbar.start()
215  for i, val in enumerate(iterable):
216  pbar.update(i)
217  yield val
218  pbar.finish()
219  print("")
220 
221 
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def pbloop(iterable, name="Entries")
Definition: HandyFuncs.py:209
def HandyFuncs.PrintError (   val,
  err 
)

Definition at line 10 of file HandyFuncs.py.

10 def PrintError(val, err):
11  precision = max(int(-math.log10(err)), 0)+1
12  form = "{0:.%if} #pm {1:.%if}" % (precision, precision)
13  return form.format(val, err)
14 
15 
def PrintError(val, err)
Definition: HandyFuncs.py:10
static int max(int a, int b)
def HandyFuncs.printlist (   mylist)

Definition at line 16 of file HandyFuncs.py.

16 def printlist(mylist):
17  rows, columns = os.popen('stty size', 'r').read().split()
18  maxw = max(list(map(len,mylist)))
19  ncols = int(columns)//(maxw+1)
20 
21  col = 0
22  for word in mylist:
23  print(word.ljust(maxw), " ", end=' ')
24  col += 1
25  if col >= ncols:
26  print("")
27  col = 0
28  print("")
29 
30 
static int max(int a, int b)
def printlist(mylist)
Definition: HandyFuncs.py:16
def HandyFuncs.ProfileX (   hist)

Definition at line 277 of file HandyFuncs.py.

277 def ProfileX(hist):
278  axis = hist.GetXaxis()
279  axisother = hist.GetYaxis()
280 
281  prof = TH1D(hist.GetName()+"_profX", hist.GetTitle(), axis.GetNbins(), axis.GetBinLowEdge(1), axis.GetBinLowEdge(axis.GetNbins()+1))
282  prof.SetXTitle(axis.GetTitle())
283 
284  for nb in range(1, axis.GetNbins()+1):
285  val = 99999999.
286  for nbo in range(1, axisother.GetNbins()+1):
287  val = min(val, hist.GetBinContent(nb, nbo))
288  prof.SetBinContent(nb, val)
289 
290  return prof
291 
292 
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
def ProfileX(hist)
Definition: HandyFuncs.py:277
def HandyFuncs.ProfileY (   hist)

Definition at line 293 of file HandyFuncs.py.

293 def ProfileY(hist):
294  axis = hist.GetYaxis()
295  axisother = hist.GetXaxis()
296 
297  prof = TH1D(hist.GetName()+"_profX", hist.GetTitle(), axis.GetNbins(), axis.GetBinLowEdge(1), axis.GetBinLowEdge(axis.GetNbins()+1))
298  prof.SetXTitle(axis.GetTitle())
299 
300  for nb in range(1, axis.GetNbins()+1):
301  val = 99999999.
302  for nbo in range(1, axisother.GetNbins()+1):
303  val = min(val, hist.GetBinContent(nbo, nb))
304  prof.SetBinContent(nb, val)
305 
306  return prof
307 
308 
def ProfileY(hist)
Definition: HandyFuncs.py:293
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
def HandyFuncs.rms (   lst)

Definition at line 273 of file HandyFuncs.py.

273 def rms(lst):
274  return sqrt( sum([(x)**2 for x in lst]) / len(lst) )
275 
276 
def rms(lst)
Definition: HandyFuncs.py:273
def HandyFuncs.stdev (   lst)

Definition at line 269 of file HandyFuncs.py.

269 def stdev(lst):
270  m = mean(lst)
271  return sqrt( sum([(x-m)**2 for x in lst]) / (len(lst)-1.) )
272 
def stdev(lst)
Definition: HandyFuncs.py:269
def mean(lst)
Definition: HandyFuncs.py:266
def HandyFuncs.VerticalRange (   hists,
  xrange = (0,0),
  ratio = False,
  forceOne = True,
  ignoreError = False,
  maxerr = 0.25,
  absMax = -999,
  absMin = -999,
  buffer = 0.05 
)

Definition at line 36 of file HandyFuncs.py.

36 def VerticalRange(hists, xrange=(0,0), ratio=False, forceOne=True, ignoreError=False, maxerr=0.25, absMax=-999, absMin=-999, buffer=0.05):
37  if xrange == (0,0):
38  minbin = 1
39  maxbin = hists[0].GetNbinsX()
40  else:
41  minbin = hists[0].FindBin(xrange[0])
42  maxbin = hists[0].FindBin(xrange[1])
43 
44  min = 999999.
45  max = -999999.
46 
47  for hist in hists:
48  for b in range(minbin, maxbin+1):
49  denom = hist.GetBinContent(b)
50  if denom == 0: denom = 0.00001
51  errratio = abs(hist.GetBinError(b) / denom)
52 
53  if ignoreError:
54  lowval = hist.GetBinContent(b)
55  highval = hist.GetBinContent(b)
56  else:
57  lowval = hist.GetBinContent(b) - hist.GetBinError(b)
58  highval = hist.GetBinContent(b) + hist.GetBinError(b)
59  #print "Bin = %i, Cont = %f, Low = %f, High = %f, ER = %f" %(b, hist.GetBinContent(b), lowval, highval, errratio)
60 
61 
62  if (errratio < maxerr) or ignoreError:
63  if lowval < min and not (hist.GetBinContent(b) == 0 and ratio):
64  min = lowval
65  if highval > max:
66  max = highval
67 
68  if ratio:
69  if forceOne:
70  if min > 1: min = 1
71  if max < 1: max = 1
72  adjust = (max - min)*buffer/2.
73  max = max + adjust
74  min = min - adjust
75  else:
76  min = 0
77  max = (1+buffer)*max
78 
79  if absMax != -999:
80  if max > absMax:
81  max = absMax
82  if absMin != -999:
83  if min < absMin:
84  min = absMin
85 
86 
87  #print "Min = %f, Max = %f" %(min, max)
88  for hist in hists:
89  hist.SetMinimum(min)
90  hist.SetMaximum(max)
91 
92 
int FindBin(double value, std::vector< double > binning)
T abs(T value)
def VerticalRange(hists, xrange=(0, 0), ratio=False, forceOne=True, ignoreError=False, maxerr=0.25, absMax=-999, absMin=-999, buffer=0.05)
Definition: HandyFuncs.py:36