95 |
|
########################################################################################################################################## |
96 |
|
########################################################################################################################################## |
97 |
|
|
98 |
+ |
# some fancy-ass code from Andrzej Zuranski to merge bins in the ratio plot until the error goes below some threshold |
99 |
+ |
def ratioHistogram( dataHist, mcHist, relErrMax=0.10): |
100 |
+ |
|
101 |
+ |
def groupR(group): |
102 |
+ |
Data,MC = [float(sum(hist.GetBinContent(i) for i in group)) for hist in [dataHist,mcHist]] |
103 |
+ |
return (Data-MC)/MC if MC else 0 |
104 |
+ |
|
105 |
+ |
def groupErr(group): |
106 |
+ |
Data,MC = [float(sum(hist.GetBinContent(i) for i in group)) for hist in [dataHist,mcHist]] |
107 |
+ |
dataErr2,mcErr2 = [sum(hist.GetBinError(i)**2 for i in group) for hist in [dataHist,mcHist]] |
108 |
+ |
return abs(math.sqrt( (dataErr2+mcErr2)/(Data-MC)**2 + mcErr2/MC**2 ) * (Data-MC)/MC) if Data and MC else 0 |
109 |
+ |
|
110 |
+ |
def regroup(groups): |
111 |
+ |
err,iG = max( (groupErr(g),groups.index(g)) for g in groups ) |
112 |
+ |
if err < relErrMax or len(groups)<3 : return groups |
113 |
+ |
iH = max( [iG-1,iG+1], key = lambda i: groupErr(groups[i]) if 0<=i<len(groups) else -1 ) |
114 |
+ |
iLo,iHi = sorted([iG,iH]) |
115 |
+ |
return regroup(groups[:iLo] + [groups[iLo]+groups[iHi]] + groups[iHi+1:]) |
116 |
+ |
|
117 |
+ |
groups = regroup( [(i,) for i in range(1,1+dataHist.GetNbinsX())] ) |
118 |
+ |
ratio = TH1F("ratio","",len(groups), array('d', [dataHist.GetBinLowEdge(min(g)) for g in groups ] + [dataHist.GetXaxis().GetBinUpEdge(dataHist.GetNbinsX())]) ) |
119 |
+ |
for i,g in enumerate(groups) : |
120 |
+ |
ratio.SetBinContent(i+1,groupR(g)) |
121 |
+ |
ratio.SetBinError(i+1,groupErr(g)) |
122 |
+ |
ratio.SetLineColor(1) |
123 |
+ |
ratio.SetLineWidth(2) |
124 |
+ |
|
125 |
+ |
return ratio |
126 |
+ |
|
127 |
+ |
########################################################################################################################################## |
128 |
+ |
########################################################################################################################################## |
129 |
+ |
########################################################################################################################################## |
130 |
+ |
|
131 |
+ |
|
132 |
|
def MakeOneDHist(pathToDir,histogramName): |
133 |
|
|
134 |
|
|
466 |
|
if makeRatioPlots or makeDiffPlots: |
467 |
|
Canvas.cd(2) |
468 |
|
BgSum = Stack.GetStack().Last() |
469 |
< |
Comparison = DataHistograms[0].Clone() |
436 |
< |
Comparison.Add(BgSum,-1) |
437 |
< |
if not makeDiffPlots: |
438 |
< |
Comparison.Divide(BgSum) |
439 |
< |
Comparison.SetTitle("") |
469 |
> |
Comparison = ratioHistogram(DataHistograms[0],BgSum) |
470 |
|
Comparison.GetXaxis().SetTitle(xAxisLabel) |
471 |
|
if makeRatioPlots: |
472 |
|
Comparison.GetYaxis().SetTitle("#frac{Data-MC}{MC}") |