-
Notifications
You must be signed in to change notification settings - Fork 0
/
showFractions.py
246 lines (214 loc) · 6.41 KB
/
showFractions.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import ROOT
import sys
from math import sqrt
from fnmatch import fnmatch
from CanvasUtilities import *
from Measurement import *
canvases = [ ]
#def getObjectsFromCanvas(canvas,type,name=None):
# result = [ ]
# for obj in canvas.GetListOfPrimitives():
# if obj.InheritsFrom(type):
# if name==None or obj.GetName()==name:
# result.append(obj)
# return result
#def getObjectsFromDirectory(dir,type,name=None):
# result = [ ]
# ROOT.gROOT.cd()
# for key in dir.GetListOfKeys():
# obj = key.ReadObj()
# if obj.InheritsFrom(type):
# if name==None or obj.GetName()==name:
# result.append(obj)
# return result
def getYield(hist,ibin):
return Measurement(hist.GetBinContent(ibin),hist.GetBinError(ibin))
def getYields(hists,ibin):
result = [ ]
for h in hists:
result.append( getYield(h,ibin) )
return result
def getSumYield(hist):
result = Measurement()
for i in range(hist.GetNbinsX()):
result += Measurement(hist.GetBinContent(i+1), hist.GetBinError(i+1))
return result
def getSumYields(hists):
result = [ ]
for h in hists:
result.append( getSumYield(h) )
return result
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-b", dest="batch", action="store_true", default=False)
parser.add_option("--showcolors", dest="showcolors", action="store_true", default=False)
parser.add_option("--integral", dest="integral", action="store_true", default=False)
parser.add_option("--color", "-c", dest="color", type="int", default=None)
parser.add_option("--listSamples", "-l", dest="listSamples", action="store_true", default=False)
parser.add_option("--sampleNames", "-s", dest="sampleNames", action="append")
(options, args) = parser.parse_args()
matchToGroup = { }
if options.sampleNames!=None:
for gNames in options.sampleNames:
fields = gNames.split(":")
assert len(fields)==2
matchToGroup[fields[0]] = fields[1].split(",")
tf = ROOT.TFile(args[0])
cnv = getObjectsFromDirectory(tf,ROOT.TCanvas.Class())[0]
if not options.batch:
cnv.Draw()
pad = getObjectsFromCanvas(cnv,ROOT.TPad.Class(),"p1")[0]
stack = getObjectsFromCanvas(pad,ROOT.THStack.Class())[0]
labels = [ ]
colors = [ ]
markers = [ ]
groupNames = [ ]
groupIndices = { }
nh = 0
leg = getObjectsFromCanvas(pad,ROOT.TLegend.Class())[0]
for le in leg.GetListOfPrimitives():
o = le.GetObject()
if o.GetFillStyle()==0 or o.GetFillColor()==0:
print "Found data histogram",le.GetLabel()
continue
label = le.GetLabel()
labels.append(label)
colors.append(o.GetFillColor())
markers.append(o.GetMarkerStyle())
matched = False
for g in matchToGroup:
for m in matchToGroup[g]:
if fnmatch(label,m):
if not g in groupIndices:
groupIndices[g] = [ ]
groupNames.append(g)
groupIndices[g].append(nh)
matched = True
break
if not matched:
assert not label in groupIndices
groupIndices[label] = [ nh ]
groupNames.append(label)
nh += 1
if options.listSamples:
print "List of sample names:"
for l in labels:
print l
print groupNames
sys.exit(0)
htot = stack.GetStack().Last()
nbins = htot.GetNbinsX()
if not options.integral:
assert nbins%2==1
nreg = (nbins-1)/2
sgns = [ -1, 1 ]
else:
nreg = 1
sgns = [ None ]
ROOT.gROOT.cd()
allObjects = [ ]
for sgn in sgns:
if sgn!=None:
rname = "CR" if sgn<0 else "SR"
else:
rname = "Sum"
hRs = [ ]
hRFs = [ ]
stackF = ROOT.THStack(rname+"Stack",rname+"Stack")
stackHistos = stack.GetHists()
groupHistos = [ ]
for gn in groupNames:
h = None
for ig in groupIndices[gn]:
hStack = stackHistos[ig]
assert hStack.GetFillColor()==colors[ig]
assert hStack.GetMarkerStyle()==markers[ig]
if h==None:
h = stackHistos[ig].Clone()
else:
h.Add(stackHistos[ig])
groupHistos.append(h)
for ih,n in enumerate(groupNames):
h = groupHistos[ih]
if options.integral:
hn = "h"+rname+str(ih)
else:
hn = "h"+rname
hRs.append(ROOT.TH1D(hn,hn,nreg,0.5,nreg+0.5))
hRs[-1].SetLineColor(h.GetFillColor())
hRs[-1].SetMarkerStyle(20)
hRs[-1].SetMarkerColor(h.GetFillColor())
allObjects.append(hRs[-1])
if options.integral:
hn = "h"+rname+"F"+str(ih)
else:
hn = "h"+rname+"F"
hRFs.append(ROOT.TH1D(hn,hn,nreg,0.5,nreg+0.5))
hRFs[-1].SetFillStyle(h.GetFillStyle())
hRFs[-1].SetFillColor(h.GetFillColor())
allObjects.append(hRFs[-1])
stackF.Add(hRFs[-1])
allObjects.append(stackF)
for ir in range(nreg):
if not options.integral:
ctotal = getYield(htot,sgn*(ir+1)+nreg+1)
csamples = getYields(groupHistos,sgn*(ir+1)+nreg+1)
else:
ctotal = getSumYield(htot)
csamples = getSumYields(groupHistos)
sv = 0.
se2 = 0.
for c in csamples:
sv += c.value()
se2 += c.error()**2
if ctotal.value()>1e-6:
assert abs(ctotal.value()-sv)/ctotal.value()<1.e-4
if ctotal.error()>1e-6:
assert abs(ctotal.error()**2-se2)/ctotal.error()**2<1.e-4
# print "Total:",ir,ctotal.value()
for ih,n in enumerate(groupNames):
c = csamples[ih].value()
if ctotal.value()>1.e-4:
f = c / ctotal.value()
ef = ctotal.value()*(ctotal.value()-2*c)*csamples[ih].error()**2 + (c*ctotal.error())**2
ef = sqrt(ef) / ctotal.value()**2
hRs[ih].SetBinContent(ir+1,f)
hRs[ih].SetBinError(ir+1,ef)
hRFs[ih].SetBinContent(ir+1,f)
for ih,h in enumerate(groupHistos):
if ih==0:
cnv = ROOT.TCanvas(rname,rname)
allObjects.append(cnv)
hRs[ih].SetMaximum(1.05)
hRs[ih].Draw()
hRs[ih].Draw("same hist")
else:
hRs[ih].Draw("same")
hRs[ih].Draw("same hist")
# assert len(labels)==len(groupHistos)
wlab = 18
wval = 6
werr = 4
print ""
print "Fractions for",rname
line = wlab*" "
for ir in range(nreg):
if options.integral:
fmt = "{0:>"+str(wval+3+werr)+"s}"
line += fmt.format(rname)
else:
fmt = "{0:>"+str(wval+3+werr-2)+"s}{1:02d}"
line += fmt.format(rname,ir+1)
print line
for il,label in enumerate(groupNames):
fmt = "{0:"+str(wlab)+"s}"
line = fmt.format(label[:wlab])
for ir in range(nreg):
fmt = "{0:"+str(wval)+".1f}+-{1:"+str(werr)+".1f}%"
line += fmt.format(100*hRs[il].GetBinContent(ir+1),100*hRs[il].GetBinError(ir+1))
print line
cnv.Update()
cnv = ROOT.TCanvas(rname+"F",rname+"F")
allObjects.append(cnv)
stackF.Draw()
cnv.Update()