-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRootFunctions.py
More file actions
400 lines (294 loc) · 9.99 KB
/
RootFunctions.py
File metadata and controls
400 lines (294 loc) · 9.99 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
############################################################
##
## Macros for making and manipulating histograms go here
##
############################################################
import sys
import ROOT
def my_range(start, end, step):
#
# for looping over discreet steps of arbitrary size
# usage:
# for xx in my_range(1, 1.3, 0.01):
#
# will set xx to 1,1.01,1.02 ... 1.29 in loop
while start <= end:
yield start
start += step
def RateIntegrator(h):
print h.GetName()
hout= h.Clone()
hout.SetName(h.GetName()+ "_clone")
nbins=h.GetNbinsX()
for ibin in range(1,nbins+1):
ibin1=ibin+1
ibin2=nbins
cont =h.GetBinContent(ibin+1)
err =h.GetBinError(ibin+1)
ferr=0.
if (cont>0.): ferr=err/cont
# fint=h.Integral(ibin1,ibin2,"width");
# fint=h.Integral(ibin1,ibin2,"");
errorVal = ROOT.Double(0)
fint=h.IntegralAndError(ibin1,ibin2,errorVal);
ferr=errorVal
# print ibin," -- Integral: ",fint
cont=fint
# err = ferr*fint
err = ferr
hout.SetBinContent(ibin+1,cont);
hout.SetBinError(ibin+1,err);
return hout
def RebinHist(h,theBins,divideByBinWidth=True):
##
## Rebin histogram with variable width bins
## pass histogram and python list containing bin lower edges and upper edge of last bin
##
##
from array import array
newbins=array("d",theBins)
n=len(newbins)
hname=h.GetName()
hname=hname + " -- Rebinned"
hnew=h.Rebin(n-1,hname,newbins)
if divideByBinWidth:
## divide by the bin width
for i in range(hnew.GetNbinsX()):
bw=hnew.GetXaxis().GetBinWidth(i+1)
# print bw
cont=hnew.GetBinContent(i+1)
err=hnew.GetBinError(i+1)
cont=cont/bw
err=err/bw
hnew.SetBinContent(i+1,cont)
hnew.SetBinError(i+1,err)
print hnew
return hnew
def DivideByBinWidth(h):
hnew=h.Clone()
## divide by the bin width
for i in range(hnew.GetNbinsX()):
bw=hnew.GetXaxis().GetBinWidth(i+1)
# print bw
cont=hnew.GetBinContent(i+1)
err=hnew.GetBinError(i+1)
cont=cont/bw
err=err/bw
hnew.SetBinContent(i+1,cont)
hnew.SetBinError(i+1,err)
return hnew
def GetHist(fileName,histName,whichDir=""):
import os
from copy import copy
Debug=False
# Debug=True
#if not os.path.exists(fileName):
# print "Histogram: ",fileName," does not exist"
# sys.exit(1)
f = ROOT.TFile.Open(fileName)
f.cd()
if Debug:
print "Input file: ",fileName
print "Histogram: ",histName
# f.ls()
if whichDir != "":
f.cd(whichDir)
if Debug:
print "XXX"
f.ls()
h=ROOT.gDirectory.Get(histName)
if h==None:
print "Could not find histo with name: ",histName, "in file ",fileName
sys.exit()
hnew=h.Clone()
# print hnew
ROOT.TH3F.AddDirectory( False )
ROOT.TH2F.AddDirectory( False )
ROOT.TH1F.AddDirectory( False )
hnew.SetDirectory(0) # retain histogram after closing file
f.Close()
return copy(hnew)
def hExist(rootfile,hname):
retval=True
key = rootfile.FindKey(hname);
if (key ==0):
print "!!Histogram ",hname, " does not exist!!"
retval=False
return retval
def Get1DHist(rootfile,hname,Debug=False):
ROOT.TH1F.AddDirectory( False )
f = ROOT.TFile.Open(rootfile)
if f==None:
print "TFile.Open returned empty pointer \n\t", rootfile," probably doesn\'t exist"
print "Exiting macro\n"
sys.exit(1)
f.cd()
if Debug:
f.ls()
h=ROOT.TH1F()
if hExist(f,hname):
h=f.Get(hname)
# h.SetDirectory(0) # retain histogram after closing file
# print h
if h==None:
print "Looks like a histogram with name: ",hname," does not exist on input file ",rootfile
if not Debug:
print "Add option debug to Get1DHist to see list of histograms"
f.Close()
return h
def Get2DHist(rootfile,hname,Debug=False):
ROOT.TH2F.AddDirectory( False )
# Debug=True
print "Input file: ",rootfile
print "Histogram: ",hname
f = ROOT.TFile.Open(rootfile)
if f==None:
print "TFile.Open returned empty pointer \n\t", rootfile," probably doesn\'t exist"
print "Exiting macro\n"
sys.exit(1)
f.cd()
if Debug:
f.ls()
# f.ls()
h=ROOT.TH2F()
if hExist(f,hname):
h=f.Get(hname)
if h==None:
print "Looks like a histogram with name: ",hname," does not exist on input file ",rootfile
if not Debug:
print "Add option debug to Get1DHist to see list of histograms"
# h.SetDirectory(0) # retain histogram after closing file
# print h
f.Close()
return h
def Get3DHist(rootfile,hname):
ROOT.TH3F.AddDirectory( False )
f = ROOT.TFile.Open(rootfile)
if f==None:
print "TFile.Open returned empty pointer \n\t", rootfile," probably doesn\'t exist"
print "Exiting macro\n"
sys.exit(1)
f.cd()
h=ROOT.TH3F()
if hExist(f,hname):
h=f.Get(hname)
if h==None:
print "Looks like a histogram with name: ",hname," does not exist on input file ",rootfile
if not Debug:
print "Add option debug to Get1DHist to see list of histograms"
# h.SetDirectory(0) # retain histogram after closing file
# print h
f.Close()
return h
def Book1D(cname,ctitle,nbins,xmin,xmax,doSumw2=True):
h=ROOT.TH1F(cname,ctitle,nbins,xmin,xmax)
if doSumw2: h.Sumw2()
return h
def DivideHistograms(hNum,hDen,c1=1.,c2=1.,WhichErrors="",newName=""):
hRat= hNum.Clone()
if len(newName) == 0:
hRat.SetName("Ratio")
else:
hRat.SetName(newName)
hRat.Divide(hNum,hDen,c1,c2,WhichErrors);
return hRat
def Proj2D_Y(h,xmin,xmax,hname="XXX",Debug=False):
# project 2D histogram into 1D along Y
imin=h.GetXaxis().FindBin(xmin+0.01)
imax=h.GetXaxis().FindBin(xmax-0.01)
if Debug:
nx= h.GetXaxis().GetNbins()
ny= h.GetYaxis().GetNbins()
print "Number of x bins: ",nx," Number of y bins: ",ny
print "Proj2D_Y: X-axis,low edge,upper edge:",\
h.GetXaxis().GetBinLowEdge(1),h.GetXaxis().GetBinUpEdge(nx)
print "Proj2D_Y: Y-axis,low edge,upper edge:",\
h.GetYaxis().GetBinLowEdge(1),h.GetYaxis().GetBinUpEdge(ny)
for i in range(1,nx):
print i,h.GetXaxis().GetBinLowEdge(i),h.GetXaxis().GetBinUpEdge(i)
print imin,xmin
print imax,xmax
if hname == "XXX":
orgname=h.GetName()
hname=orgname +"__projY_" + str(xmin) + "-" + str(xmax)
## hname="projY_" + str(xmin) + "-" + str(xmax)
proj_y=h.ProjectionY(hname, imin, imax)
ROOT.SetOwnership(proj_y,True)
if Debug:
n= proj_y.GetXaxis().GetNbins()
print hname
print "Proj_y low edge,upper edge:",\
proj_y.GetXaxis().GetBinLowEdge(1),proj_y.GetXaxis().GetBinUpEdge(n)
print proj_y.GetEntries()
return proj_y
def Proj2D_X(h,ymin,ymax,hname="XXX",Debug=False):
# project 2D histogram into 1D along Y
imin=h.GetYaxis().FindBin(ymin+0.01)
imax=h.GetYaxis().FindBin(ymax-0.01)
if Debug:
nx= h.GetXaxis().GetNbins()
ny= h.GetYaxis().GetNbins()
print "X-axis,low edge,upper edge:",\
h.GetXaxis().GetBinLowEdge(1),h.GetXaxis().GetBinUpEdge(nx)
print "Y-axis,low edge,upper edge:",\
h.GetYaxis().GetBinLowEdge(1),h.GetYaxis().GetBinUpEdge(ny)
print imin,ymin
print imax,ymax
if hname == "XXX":
orgname=h.GetName()
hname=orgname +"__projX_" + str(ymin) + "-" + str(ymax)
proj_x=h.ProjectionX(hname, imin, imax)
ROOT.SetOwnership(proj_x,True)
if Debug:
n= proj_x.GetXaxis().GetNbins()
print hname
print "Proj_y low edge,upper edge:",\
proj_x.GetXaxis().GetBinLowEdge(1),proj_x.GetXaxis().GetBinUpEdge(n)
print proj_x.GetEntries()
return proj_x
def Proj3D_Z(h,xmin,xmax,ymin,ymax,Debug=False):
# project 3D histogram into 1D along Z (x and y into Z)
imin=h.GetXaxis().FindBin(xmin+0.01)
imax=h.GetXaxis().FindBin(xmax-0.01)
jmin=h.GetYaxis().FindBin(ymin+0.01)
jmax=h.GetYaxis().FindBin(ymax-0.01)
if Debug:
n= h.GetXaxis().GetNbins()
print "X-axis,low edge,upper edge:",\
h.GetXaxis().GetBinLowEdge(1),h.GetXaxis().GetBinUpEdge(n)
print "Y-axis,low edge,upper edge:",\
h.GetYaxis().GetBinLowEdge(1),h.GetYaxis().GetBinUpEdge(n)
print imin,xmin
print imax,xmax
print jmin,ymin
print jmax,ymax
hname="projZ_" + str(xmin) + "-" + str(xmax) +"__"+ str(ymin) + "_" + str(ymax)
proj_z=h.ProjectionZ(hname, imin, imax, jmin, jmax)
ROOT.SetOwnership(proj_z,True)
if Debug:
n= proj_z.GetXaxis().GetNbins()
print hname
print "Proj_z low edge,upper edge:",\
proj_z.GetXaxis().GetBinLowEdge(1),proj_z.GetXaxis().GetBinUpEdge(n)
print proj_z.GetEntries()
return proj_z
def ZeroErrorBars(histo):
nbins=histo.GetNbinsX()
for ibin in range(1,nbins+1):
histo.SetBinError(ibin,0.0)
return
class XSFIT:
def __call__( self, x, par ):
value = par[0]/x[0]**(par[1])+par[2]/x[0]**(par[3]);
return value
class EFFFIT:
def __call__( self, x, par ):
value=par[0]/2.+par[0]/2.*ROOT.TMath.Erf((x[0]-par[1])/par[2]);
return value
class GFIT:
def __call__( self, x, par ):
# value1 = par[0]*math.exp(-0.5 * (x[0] - par[1])**2 / par[2]**2 );
# value2 = par[3]*math.exp(-0.5 * (x[0] - par[4])**2 / par[5]**2 );
value1 = par[0]*math.exp(-0.5 * ((x[0] - par[1])/par[2])**2 );
value2 = par[3]*math.exp(-0.5 * ((x[0] - par[4])/par[5])**2 );
return par[6]-(value1+value2)