This repository was archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
93 lines (83 loc) · 2.87 KB
/
Copy pathhelpers.py
File metadata and controls
93 lines (83 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from rootpy.plotting import Hist, Graph
from math import sqrt
from itertools import izip
def not_empty(d, key):
if key not in d:
return False
if d[key] == '':
return False
if d[key] == None:
return False
return True
def is_true(d, key):
if key not in d:
return False
return d[key]
def histify(plottable):
if 'Hist' in plottable.__class__.__name__:
return plottable
elif 'Graph' in plottable.__class__.__name__:
if len(list(plottable.x())) == 0:
print plottable
bins = [(x-err[0],x+err[1]) for x,err in izip(plottable.x(), plottable.xerr())]
bins = [x[0] for x in bins] + [bins[-1][1]]
hist = Hist(bins)
for i,y,yerr in izip(hist.bins_range(), plottable.y(), plottable.yerr()):
hist.SetBinContent(i, y)
hist.SetBinError(i, 0.5*(yerr[0]+yerr[1]))
# Transfer all the graphic properties
hist.decorate(plottable)
hist.title = plottable.title
return hist
else:
raise TypeError("Don't know how to create a histogram from " + plottable.__class__.__name__)
def xscale(hist, scale):
bins = list(hist.xedges())
h = Hist([b*scale for b in bins])
for b1,b2 in izip(h.bins(),hist.bins()):
b1.value = b2.value
b1.error = b2.error
h.decorate(hist)
h.title = hist.title
h.drawstyle = hist.drawstyle
return h
def efficiency_divide(h1, h2):
''' Efficiency with correct errors from numerator and denominator hists '''
eff = Graph()
eff.Divide(h1,h2,"cl=0.683 b(1,1) mode")
eff.decorate(h1)
eff.title = h1.title
return histify(eff)
def sqrt_hist(hist):
hist = hist.Clone()
for i in hist.bins_range():
rel_unc = hist.GetBinError(i)/hist.GetBinContent(i)
hist.SetBinContent(i,sqrt(hist.GetBinContent(i)))
hist.SetBinError(i, hist.GetBinContent(i) * rel_unc * .05)
return hist
def running_integral(hist, neg=False):
result = histify(hist.Clone())
for x in result.bins_range(overflow=True):
if neg:
# Integral up to the value
y,yerr = hist.integral(xbin2=x, error=True, overflow=True)
else:
# Integral starting at the value
y,yerr = hist.integral(xbin1=x, error=True, overflow=True)
result.SetBinContent(x,y)
result.SetBinError(x,yerr)
return result
def flat_hist(hist, index):
val, err = hist.GetBinContent(abs(index)), hist.GetBinError(abs(index))
result = hist.Clone()
for x in result.bins_range(overflow=True):
result.SetBinContent(x, val)
result.SetBinError(x, err)
return result
def flat_integral(hist):
val, err = hist.integral(overflow=True, error=True)
result = hist.Clone()
for x in result.bins_range(overflow=True):
result.SetBinContent(x, val)
result.SetBinError(x, err)
return result