1 |
nmohr |
1.1 |
import ROOT
|
2 |
|
|
from copy import copy
|
3 |
|
|
|
4 |
|
|
class Rebinner:
|
5 |
|
|
def __init__(self,nBins,lowedgearray,active=True):
|
6 |
|
|
self.lowedgearray=lowedgearray
|
7 |
|
|
self.nBins=nBins
|
8 |
|
|
self.active=active
|
9 |
|
|
def rebin(self, histo):
|
10 |
|
|
if not self.active: return histo
|
11 |
|
|
#print 'rebinning'
|
12 |
|
|
#print histo.Integral()
|
13 |
|
|
ROOT.gDirectory.Delete('hnew')
|
14 |
|
|
histo.Rebin(self.nBins,'hnew',self.lowedgearray)
|
15 |
|
|
binhisto=ROOT.gDirectory.Get('hnew')
|
16 |
|
|
#print binhisto.Integral()
|
17 |
|
|
newhisto=ROOT.TH1F('new','new',self.nBins,self.lowedgearray[0],self.lowedgearray[-1])
|
18 |
|
|
newhisto.Sumw2()
|
19 |
|
|
for bin in range(1,self.nBins+1):
|
20 |
|
|
newhisto.SetBinContent(bin,binhisto.GetBinContent(bin))
|
21 |
|
|
newhisto.SetBinError(bin,binhisto.GetBinError(bin))
|
22 |
|
|
newhisto.SetName(binhisto.GetName())
|
23 |
|
|
newhisto.SetTitle(binhisto.GetTitle())
|
24 |
|
|
#print newhisto.Integral()
|
25 |
|
|
return copy(newhisto)
|
26 |
|
|
|
27 |
|
|
@staticmethod
|
28 |
|
|
def calculate_binning(hDummyRB,max_rel):
|
29 |
|
|
ErrorR=0
|
30 |
|
|
ErrorL=0
|
31 |
|
|
TotR=0
|
32 |
|
|
TotL=0
|
33 |
|
|
binR=nBinsRB
|
34 |
|
|
binL=1
|
35 |
|
|
rel=1.0
|
36 |
|
|
#---- from right
|
37 |
|
|
while rel > 0.35:
|
38 |
|
|
TotR+=hDummyRB.GetBinContent(binR)
|
39 |
|
|
ErrorR=sqrt(ErrorR**2+hDummyRB.GetBinError(binR)**2)
|
40 |
|
|
binR-=1
|
41 |
|
|
if not TotR == 0 and not ErrorR == 0:
|
42 |
|
|
rel=ErrorR/TotR
|
43 |
|
|
#print rel
|
44 |
|
|
#print 'upper bin is %s'%binR
|
45 |
|
|
|
46 |
|
|
#---- from left
|
47 |
|
|
rel=1.0
|
48 |
|
|
while rel > 0.35:
|
49 |
|
|
TotL+=hDummyRB.GetBinContent(binL)
|
50 |
|
|
ErrorL=sqrt(ErrorL**2+hDummyRB.GetBinError(binL)**2)
|
51 |
|
|
binL+=1
|
52 |
|
|
if not TotL == 0 and not ErrorL == 0:
|
53 |
|
|
rel=ErrorL/TotL
|
54 |
|
|
#print rel
|
55 |
|
|
#it's the lower edge
|
56 |
|
|
binL+=1
|
57 |
|
|
#print 'lower bin is %s'%binL
|
58 |
|
|
|
59 |
|
|
inbetween=binR-binL
|
60 |
|
|
stepsize=int(inbetween)/(int(nBins)-2)
|
61 |
|
|
modulo = int(inbetween)%(int(nBins)-2)
|
62 |
|
|
|
63 |
|
|
#print'stepsize %s'% stepsize
|
64 |
|
|
#print 'modulo %s'%modulo
|
65 |
|
|
|
66 |
|
|
binlist=[binL]
|
67 |
|
|
for i in range(0,int(nBins)-3):
|
68 |
|
|
binlist.append(binlist[-1]+stepsize)
|
69 |
|
|
binlist[-1]+=modulo
|
70 |
|
|
binlist.append(binR)
|
71 |
|
|
binlist.append(nBinsRB+1)
|
72 |
|
|
return binlist
|
73 |
|
|
|