ATLAS Offline Software
Loading...
Searching...
No Matches
JESUncertainty_GraphUtils.py
Go to the documentation of this file.
1#!/bin/python
2
3# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
4# -*- coding: utf-8 -*-
5
6from numpy import isnan
7from ROOT import Double, TH1F
8from array import *
9
10
13
14def flagLowStatPoints (graph, debug) :
15
16 #stick all the indices of the bad points in this vector
17 points = []
18
19 #loop on all data points of the graph
20 nPoints = graph.GetN()
21
22 for iPoint in xrange(0,nPoints) :
23 dataPointX = Double(0)
24 dataPointY = Double(0)
25 error = Double(0)
26 errorX = graph.GetErrorX(iPoint)
27 errorY = graph.GetErrorY(iPoint)
28
29 graph.GetPoint(iPoint,dataPointX,dataPointY)
30
31
32 if isnan(dataPointY) :
33 dataPointY = 0.000001
34 #if dataPointY < 0.000001 :
35 #dataPointY = 0.000001
36 points.append(iPoint)
37
38
39 if errorY < 0.00000001 :
40 points.append(iPoint)
41
42
43 if errorY > 0.01 :
44 points.append(iPoint)
45
46 if debug == True:
47 print "DEBUG: List of bad points for plot", graph.GetName(), ":"
48 print points
49
50 return points
51
52
55
57
58 #stick all the indices of the bad points in this vector
59 points = []
60
61 #loop on all data points of the graph
62 nPoints = graph.GetN()
63
64 for iPoint in xrange(0,nPoints) :
65 dataPointX = Double(0)
66 dataPointY = Double(0)
67 error = Double(0)
68 errorX = graph.GetErrorX(iPoint)
69 errorY = graph.GetErrorY(iPoint)
70
71 graph.GetPoint(iPoint,dataPointX,dataPointY)
72
73
74 if isnan(dataPointY) :
75 dataPointY = 0.000001
76 #if dataPointY < 0.000001 :
77 #dataPointY = 0.000001
78 points.append(iPoint)
79
80
81 if errorY < 0.00000001 :
82 points.append(iPoint)
83
84
85 if debug == True:
86 print "DEBUG: List of bad points for plot", graph.GetName(), ":"
87 print points
88
89 return points
90
91def eliminateLowStatPoints(graph, listOfBadPoints) :
92
93 #loop on all data points of the graph
94 nPoints = graph.GetN()
95
96 for iPoint in xrange(0,nPoints) :
97 dataPointX = Double(0)
98 dataPointY = Double(0)
99 error = Double(0)
100 errorX = graph.GetErrorX(iPoint)
101 errorY = graph.GetErrorY(iPoint)
102
103 graph.GetPoint(iPoint,dataPointX,dataPointY)
104
105 if iPoint in listOfBadPoints :
106
107 graph.SetPoint(iPoint, dataPointX, -999)
108 graph.SetPointError(iPoint, errorX, 0)
109
110def rescaleToGeV(graph) :
111
112 #loop on all data points of the graph
113 nPoints = graph.GetN()
114
115 for iPoint in xrange(0,nPoints) :
116 dataPointX = Double(0)
117 dataPointY = Double(0)
118 error = Double(0)
119 errorX = graph.GetErrorX(iPoint)
120 errorY = graph.GetErrorY(iPoint)
121
122 graph.GetPoint(iPoint,dataPointX,dataPointY)
123
124 graph.SetPoint(iPoint, dataPointX/1000, dataPointY)
125 graph.SetPointError(iPoint, errorX/1000, errorY)
126
128
129 #loop on all data points of the graph
130 nPoints = graph.GetN()
131
132 print graph.GetName()
133
134 for iPoint in xrange(0,nPoints) :
135 dataPointX = Double(0)
136 dataPointY = Double(0)
137 error = Double(0)
138 errorX = graph.GetErrorX(iPoint)
139 errorY = graph.GetErrorY(iPoint)
140
141 graph.GetPoint(iPoint,dataPointX,dataPointY)
142
143 print dataPointX, ":", dataPointY
144
145def removeXErrors(graph) :
146
147 #loop on all data points of the graph
148 nPoints = graph.GetN()
149
150 for iPoint in xrange(0,nPoints) :
151 errorY = graph.GetErrorY(iPoint)
152 graph.SetPointError(iPoint, 0, errorY)
153
154def removeYErrors(graph) :
155
156 #loop on all data points of the graph
157 nPoints = graph.GetN()
158
159 for iPoint in xrange(0,nPoints) :
160 errorX = graph.GetErrorX(iPoint)
161 graph.SetPointError(iPoint, errorX, 0.00000001)
162
163
164def scaleGraph(graphToScale, scalingInputGraph, oneMinus = False) :
165
166 #get number of points for the two graphs
167 nPointsGraphToScale= graphToScale.GetN()
168 nPointsScalingInputGraph = scalingInputGraph.GetN()
169
170 if nPointsGraphToScale!=nPointsScalingInputGraph :
171 print "ERROR in JESUncertainty_RatioUtils::scaleGraph: graphs don't have the same number of points"
172 return None
173
174 #prepare the final graph
175 scaledGraph = graphToScale.Clone()
176 scaledGraph.SetName(graphToScale.GetName()+"_Scaled")
177
178 #loop on points of the graphs
179 for iPoint in xrange(0,nPointsGraphToScale) :
180
181 #make the new point
182 maxPoint = Double(0)
183 maxPointError = Double(0)
184
185 #retrieve the point
186 dataPointXgraphToScale = Double(0)
187 dataPointYgraphToScale = Double(0)
188 errorXgraphToScale= graphToScale.GetErrorX(iPoint)
189 errorYgraphToScale = graphToScale.GetErrorY(iPoint)
190 graphToScale.GetPoint(iPoint,dataPointXgraphToScale,dataPointYgraphToScale)
191
192 dataPointXscalingInputGraph = Double(0)
193 dataPointYscalingInputGraph = Double(0)
194 errorXscalingInputGraph = scalingInputGraph.GetErrorX(iPoint)
195 errorYscalingInputGraph = scalingInputGraph.GetErrorY(iPoint)
196 scalingInputGraph.GetPoint(iPoint,dataPointXscalingInputGraph,dataPointYscalingInputGraph)
197
198 if abs(dataPointXgraphToScale-dataPointXscalingInputGraph) > 0.0001:
199 print "ERROR in JESUncertainty_RatioUtils::scaleGraph: x coordinates of data points do not match"
200 return None
201
202 #set the point/error of the output graph to the scaled point/error of the input one (protect against negative scaling)
203 if abs(dataPointYscalingInputGraph) < 0:
204 print "WARNING in JESUncertainty_RatioUtils::scaleGraph: negative scale factor for point: ", iPoint, ", X:", dataPointXgraphToScale
205 scaledGraph.SetPoint(iPoint, dataPointXgraphToScale, dataPointYscalingInputGraph*dataPointYgraphToScale)
206 scaledGraph.SetPointError(iPoint, errorXgraphToScale, abs(dataPointYscalingInputGraph)*dataPointYgraphToScale)
207
208 if oneMinus :
209 scaledGraph.SetPoint(iPoint, dataPointXgraphToScale, (1-dataPointYscalingInputGraph)*dataPointYgraphToScale)
210 scaledGraph.SetPointError(iPoint, errorXgraphToScale, abs(dataPointYscalingInputGraph)*dataPointYgraphToScale)
211
212 return scaledGraph
213
214def extendLastGoodBin_2011(graph, warn) :
215
216 #loop on all data points of the graph
217 nPoints = graph.GetN()
218
219 #it should not happen that the first bin is bad, but just in case we keep it obvious
220 lastGoodBin = -999
221 lastGoodError = -999
222
223 for iPoint in xrange(0,nPoints) :
224 dataPointX = Double(0)
225 dataPointY = Double(0)
226 error = Double(0)
227 errorX = graph.GetErrorX(iPoint)
228 errorY = graph.GetErrorY(iPoint)
229
230 graph.GetPoint(iPoint,dataPointX,dataPointY)
231
232 #HACK: this is rather arbitrary!
233 if dataPointY > 2. or errorY > 0.1:
234 if warn :
235 print "WARNING in JESUncertainty_RatioUtils::extendLastGoodBin: "
236 print "found bad point for graph:", graph.GetName(), "at point: ", iPoint, ", X:", dataPointX, "Y:", dataPointY
237 print "extending previous point:", lastGoodBin
238 newDataPointY=lastGoodBin
239 newErrorY = lastGoodError
240 graph.SetPoint(iPoint,dataPointX,newDataPointY)
241 graph.SetPointError(iPoint, errorX, newErrorY)
242 else :
243 lastGoodBin = dataPointY
244 lastGoodError = errorY
245
246
247#this fcn works with TGraphs with symmetric x errors
248def TGraphToTH1 (graph) :
249
250 #loop on all data points
251 nPoints = graph.GetN()
252 #print "nPoints", nPoints
253
254 x=array('f')
255 y=array('f')
256 ey=array('f')
257
258 dataPointX = None
259 dataErrorX = None
260
261 for iPoint in xrange(0,nPoints) :
262 #get info on data point
263 dataPointX = Double(0)
264 dataPointY = Double(0)
265 graph.GetPoint(iPoint,dataPointX,dataPointY)
266 dataErrorX = graph.GetErrorX(iPoint)
267 dataErrorY = graph.GetErrorY(iPoint)
268
269 x.append(dataPointX-dataErrorX)
270 y.append(dataPointY)
271 ey.append(dataErrorY)
272
273 #this needs to be done at the end of the loop, for the upper limit
274 x.append(dataPointX+dataErrorX)
275
276 #safety checks
277 for iPoint in xrange(0,nPoints) :
278 if isnan(y[iPoint]) :
279 y[iPoint]=0
280 if isnan(x[iPoint]) :
281 x[iPoint]=0
282 if y[iPoint]<0.00000001 :
283 y[iPoint]=0
284 ey[iPoint]=0
285
286 myHist=TH1F("Hist"+graph.GetName(),graph.GetTitle(),nPoints,x)
287
288 for iPoint in xrange(0,nPoints) :
289
290 myHist.SetBinContent(iPoint+1,y[iPoint])#the +1 is needed because TH1 have overflows
291 #myHist.SetBinError(iPoint+1,ey[iPoint])#the +1 is needed because TH1 have overflows
292 myHist.SetBinError(iPoint+1,0)#avoid poissonian errors
293
294 return myHist
295
296#this fcn works with TGraphs with symmetric x errors
298
299 #loop on all data points
300 nPoints = graph.GetN()
301 #print "nPoints", nPoints
302
303 x=array('f')
304 y=array('f')
305 ey=array('f')
306
307 dataPointX = None
308 dataErrorX = None
309
310 for iPoint in xrange(0,nPoints) :
311 #get info on data point
312 dataPointX = Double(0)
313 dataPointY = Double(0)
314 graph.GetPoint(iPoint,dataPointX,dataPointY)
315 dataErrorX = graph.GetErrorX(iPoint)
316 dataErrorY = graph.GetErrorY(iPoint)
317
318 x.append(dataPointX-dataErrorX)
319 y.append(dataPointY)
320 ey.append(dataErrorY)
321
322 #this needs to be done at the end of the loop, for the upper limit
323 x.append(dataPointX+dataErrorX)
324
325 #safety checks
326 for iPoint in xrange(0,nPoints) :
327 if isnan(y[iPoint]) :
328 y[iPoint]=0
329 if isnan(x[iPoint]) :
330 x[iPoint]=0
331 if y[iPoint]<0.00000001 :
332 y[iPoint]=0
333 ey[iPoint]=0
334
335 myHist=TH1F("Hist"+graph.GetName(),graph.GetTitle(),nPoints,x)
336
337 for iPoint in xrange(0,nPoints) :
338
339 myHist.SetBinContent(iPoint+1,y[iPoint])#the +1 is needed because TH1 have overflows
340 myHist.SetBinError(iPoint+1,ey[iPoint])#the +1 is needed because TH1 have overflows
341 #myHist.SetBinError(iPoint+1,0)#avoid poissonian errors
342
343 return myHist
STL class.
void xrange(TH1 *h, bool symmetric)
scaleGraph(graphToScale, scalingInputGraph, oneMinus=False)
eliminateLowStatPoints(graph, listOfBadPoints)
flagLowStatPoints_noLargeErrorCheck(graph, debug)
Avoid low statistics points, but do not check on upper bound of error.
flagLowStatPoints(graph, debug)
Avoid low statistics points.