-
Notifications
You must be signed in to change notification settings - Fork 0
/
TurnOn.py
executable file
·217 lines (184 loc) · 6.93 KB
/
TurnOn.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
#!/usr/bin/env python
# encoding: utf-8
"""
turnOnCurves.py
Created by Bryn Mathias on 2011-12-10.
Copyright (c) 2011 Imperial College. All rights reserved.
"""
import sys
import os
import ROOT as r
import math
class TurnOn(object):
"""Turn on curve producer"""
def __init__(self, numerator = None, denominator = None):
super(TurnOn, self).__init__()
self.denominator = denominator
self.numerator = numerator
self.weights = None
self.listOfTurnOns = []
self.Approximate = False
self.finalPlot = None
self.Debug = True
self.TotNumerator = None
self.TotDenominator = None
self.ListOfTurnOns()
self.MakeTotalHists()
def ListOfTurnOns(self):
"""docstring for ListOfTurnOns"""
i = 0
for nom,denom in zip(self.numerator,self.denominator):
if self.Approximate is not True:
graph = r.TGraphAsymmErrors()
graph.SetTitle(nom.GetTitle())
graph.Divide(nom,denom)
graph.GetYaxis().SetRangeUser(0.,1.1)
self.listOfTurnOns.append(graph)
if self.Approximate is True:
a = nom.Clone()
if self.weights is not None:
a.Scale(weights[i])
a.Divde(denom)
graph = r.TGraph(a)
graph.GetYaxis().SetRangeUser(0.,1.1)
self.listOfTurnOns.append(graph)
i+=1
return self.listOfTurnOns
pass
def SumOfTurnOns(self):
"""docstring for SumOfTurnOns"""
self.ListOfTurnOns()
self.finalPlot = r.TGraphAsymmErrors()
self.finalPlot.Divide(self.TotNumerator,self.TotDenominator)
for point in range(self.finalPlot.GetN()):
sum_w_i = r.Double(0.)
sum_w_i_plus = r.Double(0.)
sum_w_i_minus = r.Double(0.)
sum_eff_times_w_i = r.Double(0.)
xVal = r.Double(0)
tmp = r.Double(0)
self.finalPlot.GetPoint(point,xVal,tmp)
bin = self.TotDenominator.FindBin(xVal)
if self.Debug is True:
print "="*25
print "Start Bin %f, %f, binWidth = %f "%(self.TotDenominator.GetBinLowEdge(bin),xVal-(self.TotDenominator.GetBinWidth(1)/2.),self.TotDenominator.GetBinWidth(1) )
print "="*25
for TurnOn,denominator in zip(self.listOfTurnOns,self.denominator):
efficiency = r.Double(0)
xvalAtPoint = r.Double(0)
w_i = r.Double(0.)
ErrorYhigh = r.Double(0.)
ErrorYlow = r.Double(0.)
TurnOn.SetTitle(denominator.GetTitle())
TurnOn.GetPoint(point,xvalAtPoint,efficiency)
for p in range(TurnOn.GetN()):
xvalAtPoint = r.Double(0)
TurnOn.GetPoint(p,xvalAtPoint,efficiency)
ErrorYhigh = r.Double(0.)
ErrorYlow = r.Double(0.)
if xvalAtPoint == xVal:
ErrorYhigh = TurnOn.GetErrorYhigh(point)
ErrorYlow = TurnOn.GetErrorYlow(point)
break
# print "Error ylow = %f, Error yHigh = %f, xVal = %f, xvalAtPoint = %f graphName = %s"%(ErrorYlow,ErrorYhigh, xVal,xvalAtPoint,TurnOn.GetTitle())
if denominator.GetBinContent(bin) > 0.:
if self.Debug: print ErrorYhigh, bin, denominator.GetBinLowEdge(bin), denominator.GetBinContent(bin)
try:
valHighErrY = ErrorYhigh**2
except OverflowError:
valHighErrY = 0.
if valHighErrY > 0.: w_i_plus = r.Double(1./(ErrorYhigh**2))
else: w_i_plus = 1.
sum_w_i_plus += w_i_plus
try:
valLowErrY = ErrorYlow**2
except OverflowError:
valLowErrY = 0.
if valLowErrY > 0.: w_i_minus = 1./valLowErrY
else: w_i_minus = 1.
sum_w_i_minus += w_i_minus
w_i = max(w_i_minus,w_i_plus)
sum_w_i += w_i
sum_eff_times_w_i += (efficiency*w_i)
if self.Debug is True:
print " w_i_plus = %f, w_i_minus = %f, sum_w_i %f, Efficiency = %f, efficiency*w_i = %f, at x = %f"%(w_i_plus,w_i_minus,sum_w_i, efficiency, efficiency*w_i, xvalAtPoint)
if sum_w_i > 0.:
AvEff = sum_eff_times_w_i/sum_w_i
else : AvEff = 0.
if sum_w_i_minus > 0.:
error_minus = math.sqrt(1./sum_w_i_minus)
else: error_minus = 0.
if sum_w_i_plus > 0.:
error_plus = math.sqrt(1./sum_w_i_plus)
else: error_plus = 0.
self.finalPlot.SetPoint(point,xVal,AvEff)
if AvEff+error_plus > 1.:
error_plus = 1.-AvEff
# if AvEff-error_minus < 1.:
# error_minus = AvEff - 1
self.finalPlot.SetPointError(point,self.finalPlot.GetErrorXlow(point),self.finalPlot.GetErrorXhigh(point),error_minus,error_plus )
self.finalPlot.GetYaxis().SetRangeUser(0.,1.1)
return self.finalPlot
pass
def MakeTotalHists(self):
"""docstring for TotalNumerator"""
for hist in self.numerator:
if self.TotNumerator is None:
self.TotNumerator = hist.Clone()
else: self.TotNumerator.Add(hist)
for hist in self.denominator:
if self.TotDenominator is None:
self.TotDenominator = hist.Clone()
else:self.TotDenominator.Add(hist)
pass
def WeightedSumOfTurnOns(TotDenominator = None,TurnOnHist = None,graphList = None,denominatorList = None ):
"""docstring for WeightedSumOfTurnOns"""
finalPlot = r.TGraphAsymmErrors(TurnOnHist)
finalPlot.Draw("ap")
# raw_input()
for bin in range(TotDenominator.GetNbinsX()):
point = bin - 1
sum_w_i = 0.
sum_w_i_plus = 0.
sum_w_i_minus = 0.
sum_eff_times_w_i = 0.
xVal = r.Double(0)
finalPlot.GetPoint(point,xVal,r.Double(0))
for TurnOn,denominator in zip(graphList,denominatorList):
efficiency = r.Double(0)
xvalAtPoint = r.Double(0)
w_i = 0.
ErrorYhigh = TurnOn.GetErrorYhigh(point)
ErrorYlow = TurnOn.GetErrorYlow(point)
TurnOn.GetPoint(point,xvalAtPoint,efficiency)
if denominator.GetBinContent(bin) > 0.:
if ErrorYhigh**2 > 0.: w_i_plus = 1./(ErrorYhigh**2)
else: w_i_plus = 0.
sum_w_i_plus += w_i_plus
if ErrorYlow**2 > 0. :w_i_minus = 1./(ErrorYlow**2)
else: w_i_minus = 0.
sum_w_i_minus += w_i_minus
w_i = max(w_i_minus,w_i_plus)
sum_w_i += w_i
sum_eff_times_w_i += (efficiency*w_i)
print "w_i_plus = %f, w_i_minus = %f, sum_w_i %f, Efficiency = %f, efficiency*w_i = %f, at x = %f"%(w_i_plus,w_i_minus,sum_w_i, efficiency, efficiency*w_i, xvalAtPoint)
if sum_w_i > 0.:
AvEff = sum_eff_times_w_i/sum_w_i
else : AvEff = 0.
if sum_w_i_minus > 0.:
error_minus = math.sqrt(1./sum_w_i_minus)
else: error_minus = 0.
if sum_w_i_plus > 0.:
error_plus = math.sqrt(1./sum_w_i_plus)
else: error_plus = 0.
finalPlot.SetPoint(point,xVal,AvEff)
# if AvEff+error_plus > 1.:
# error_plus = 1.-AvEff
# if AvEff-error_minus < 1.:
# error_minus = AvEff - 1
finalPlot.SetPointError(point,finalPlot.GetErrorXlow(point),finalPlot.GetErrorXhigh(point),error_minus,error_plus )
return finalPlot
def main():
pass
if __name__ == '__main__':
main()