ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/nowaf/RootFilesInUse/MakeRealTauEst_cff.py
Revision: 1.1.1.1 (vendor branch)
Committed: Tue Mar 20 13:12:08 2012 UTC (13 years, 1 month ago) by nowak
Content type: text/x-python
Branch: rootFilesInUse, MAIN
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
Error occurred while calculating annotation data.
Log Message:
t files in use

File Contents

# Content
1 import ROOT
2 import sys, re, math
3 sys.path.append("/afs/naf.desy.de/user/n/nowaf/UserCode/nowaf/PythonScripts/")
4 import definitions as Def
5 import styles as Style
6
7 class Eff:
8 def __init__( self, u , l ):
9 self.upper = u
10 self.lower = l
11 self.eff = 0.
12 if not self.lower == 0.:
13 self.eff = self.upper/self.lower
14 pass
15 self.errUp, self.errDwn = self.getErr()
16 pass
17
18 def getErr( self ):
19 h1 = ROOT.TH1F( "h1", "h1", 1, 0, 1 )
20 h2 = ROOT.TH1F( "h2", "h2", 1, 0, 1 )
21
22 for i in range( 0, int( self.upper ) ):
23 h1.Fill( 0.5 )
24 pass
25 for i in range( 0, int( self.lower ) ):
26 h2.Fill( 0.5 )
27 pass
28
29 g = ROOT.TGraphAsymmErrors()
30 g.BayesDivide( h1, h2 )
31 errUp = g.GetErrorYhigh( 0 )
32 errDwn = g.GetErrorYlow( 0 )
33 return errUp, errDwn
34
35 pass
36
37 class ReadIn:
38 """
39 Standard container class for reading in standard samples
40 """
41 def __init__( self, fileDir, fileName, lumi = 1000., rebinDict = {}, externalScalingFactor = 1. ):
42 self.sampleList = []
43 self.sampleList.append( "TTbar" )
44 self.sampleList.append( "GVJets" )
45 self.sampleList.append( "WWJets" )
46 self.sampleList.append( "Zinv" )
47 self.sampleList.append( "ZJets" )
48 self.sampleList.append( "WJets" )
49 self.sampleList.append( "QCDFlat" )
50 self.sampleList.append( "Data" )
51
52 self.fileDict = {}
53 self.fileDir = fileDir
54 self.fileName = fileName
55 #for sample in self.sampleList:
56 # self.fileDict[ sample ] = fileDir + sample + fileName + ".root"
57 # pass
58
59 #self.aliasDict = {}
60 #self.aliasDict[ "GVJets" ] = "GV+Jets"
61 #self.aliasDict[ "VVJets" ] = "VV+Jets"
62 #self.aliasDict[ "ZJets" ] = "Z+Jets"
63 #self.aliasDict[ "QCDFlat" ] = "Multijet"
64
65 self.histList = []
66 self.histList.append( "MHT" )
67
68 self.scalingFactors = {}
69 #self.scalingFactors[ "Multijet" ] = lumi/100.
70 #self.scalingFactors[ "ZJets" ] = lumi/100.
71 #self.scalingFactors[ "VVJets" ] = lumi/100.
72 #self.scalingFactors[ "WJets" ] = lumi * 31300. / ( 15110974 * 0.98 )
73 #self.scalingFactors[ "TTbar" ] = lumi * 165 / ( 1164208. * 0.92 )
74 #self.scalingFactors[ "GVJets" ] = lumi * 173 / ( 1102193. * 0.97 )
75 #self.scalingFactors[ "Zinv" ] = lumi * 5760 / ( 2165002. * 0.96 )
76 #self.scalingFactors[ "Data" ] = 1.
77
78 self.lumi = lumi
79 self.externalScalingFactor = externalScalingFactor
80
81 #### Summer11
82 self.scalingFactors[ "QCDFlat" ] = lumi/1000.
83 self.scalingFactors[ "ZJets" ] = lumi * 3048/( 36277961 *1. )
84 ##print "Warning: Wrong scaling Factor for: VVJets"
85 self.scalingFactors[ "VVJets" ] = lumi/100.
86 self.scalingFactors[ "WWJets" ] = lumi * 43. / ( 1197558. * 1. )
87 self.scalingFactors[ "WJets" ] = lumi * 31314. / ( 81352581 )
88
89 #self.scalingFactors[ "WJets" ] = lumi * 31314. / ( 81352581* 0.93 )
90
91 #self.scalingFactors[ "WJets" ] = lumi * 31300. / ( 81352581 * 451./483 )
92 self.scalingFactors[ "TTbar" ] = lumi * 165 / ( 3701947. * 1. )
93 self.scalingFactors[ "GVJets" ] = lumi * 173 / ( 1067879. * 1. )
94 ##print "Warning: Wrong scaling Factor for: Zinv"
95 self.scalingFactors[ "Zinv" ] = lumi * 32.92 / ( 3067017. )
96 self.scalingFactors[ "Data" ] = 1.
97 self.scalingFactors[ "LM13" ] = lumi * 6.899 / ( 77000. * 1. ) ### skim
98 #self.scalingFactors[ "LM13" ] = lumi * 6.899 / ( 517000 * 1 ) ### sie haben nachproduziert!!
99 self.scalingFactors[ "LM3" ] = lumi * 3.438 / ( 36475. * 1. )
100 self.scalingFactors[ "LM8" ] = lumi * 0.73 / ( 10595. * 1. )
101 #self.scalingFactors[ "LM2" ] = lumi * 0.6027 / ( 11000. * 1. ) ### skim
102 self.scalingFactors[ "LM2" ] = lumi * 0.6027 / ( 445060 * 1. ) ### the new, unpublished skim
103
104 #self.scalingFactors[ "Zinv" ] = lumi * 32.92 / ( 3067017. * 0.84 ) ### full_iii
105 #self.scalingFactors[ "WWJets" ] = lumi * 43. / ( 1197558. * 0.08 ) ### full_iii
106 #self.scalingFactors[ "LM13" ] = lumi * 6.899 / ( 517000 * 1 ) ### full_iii
107
108 #### overwrite for full_ii
109 #self.scalingFactors[ "LM13" ] = lumi * 6.899 / ( 517000 * 1 ) ### sie haben nachproduziert!!
110 #self.scalingFactors[ "LM2" ] = lumi * 0.6027 / ( 445060 * 1. )
111 #self.scalingFactors[ "WWJets" ] = lumi * 43. / ( 1197558. * 0.9 )
112 #self.scalingFactors[ "Zinv" ] = lumi * 32.92 / ( 3067017. * 0.26 )
113
114 self.doScaling = True
115 self.normalize = False
116
117 self.rebinDict = rebinDict
118
119 pass
120
121 def changeScalingFactor( self, sample, factor ):
122 self.scalingFactors[ sample ] = factor
123 pass
124
125 def dontScale( self ):
126 self.doScaling = False
127 pass
128
129 def doNormalization( self ):
130 self.normalize = True
131 pass
132
133 def getHists( self, histDir ):
134
135 for sample in self.sampleList:
136 if sample not in [ "LM2", "LM13" ]:
137 self.fileDict[ sample ] = self.fileDir + sample + self.fileName + ".root"
138 else:
139 self.fileDict[ sample ] = self.fileDir + sample + "_cxviii" + ".root"
140 pass
141
142 histDict = {}
143 print "..... reading in ....."
144 for hist in self.histList:
145 print hist, ":"
146 histDict[ hist ] = {}
147 for file in self.sampleList:
148 print " from sample ", file
149 rootfile = ROOT.TFile.Open( self.fileDict[ file ] )
150 histDict[ hist ][ file ] = rootfile.Get( histDir + hist )
151 histDict[ hist ][ file ].SetDirectory( 0 )
152 if hist in self.rebinDict.keys():
153 histDict[ hist ][ file ].Rebin( self.rebinDict[ hist ] )
154 pass
155 if self.doScaling:
156 histDict[ hist ][ file ].Scale( self.scalingFactors[ file ] )
157 histDict[ hist ][ file ].Scale( self.externalScalingFactor )
158 pass
159 if self.normalize:
160 if self.doScaling:
161 print "Warning! Hists will be normalized, scaling has no effect."
162 pass
163 norm = histDict[ hist ][ file ].Integral()
164 if not norm == 0:
165 histDict[ hist ][ file ].Scale( 1/norm )
166 else:
167 print "Not normalizing because norm == 0."
168 pass
169 pass
170 pass
171
172 return histDict
173
174 def addHists( self, histList ):
175 for h in histList:
176 if not h in self.histList:
177 self.histList.append( h )
178 pass
179 pass
180 pass
181
182 def removeHists( self, histList ):
183 for h in histList:
184 self.histList.pop( self.histList.index( h ) )
185 pass
186 pass
187
188 #def setAlias( self, sample, alias ):
189 # self.aliasDict[ sample ] = alias
190 # pass
191
192 #def removeAlias( self, sample ):
193 # del self.aliasDict[ sample ]
194 # pass
195
196 def removeSamples( self, removalList ):
197 for r in removalList:
198 self.sampleList.pop( self.sampleList.index( r ) )
199 pass
200 pass
201
202 def addSamples( self, addList ):
203 for a in addList:
204 self.sampleList.append( a )
205 pass
206 pass
207 pass
208
209 class AddUp:
210 def __init__( self, histDict ):
211 self.histDict = histDict
212 pass
213 def add( self ):
214 newHist = None
215 for sample in self.histDict.keys():
216 if not newHist == None:
217 newHist.Add( self.histDict[ sample ] )
218 else:
219 newHist = self.histDict[ sample ].Clone()
220 newHist.SetDirectory( 0 )
221 pass
222 pass
223 return newHist
224 pass
225
226 class StackErr:
227 def __init__( self, histDict, histDictUnscaled ):
228 self.histDict = histDict
229 self.histDictUnscaled = histDictUnscaled
230 #self.scalingFactors = scalingFactors
231 pass
232 def getStackErr( self ):
233 errDict = {}
234 errDictBins = {}
235 errDictBins[ "All" ] = {}
236 sum = 0.
237 for sample in self.histDict.keys():
238 #print self.histDict[ sample ].GetName()
239 #print self.histDict[ sample ].Integral()
240 binsum = 0
241 binsumUnscaled = 0
242 errDictBins[ sample ] = {}
243 for bin in range( 1, self.histDict[ sample ].GetNbinsX() + 1 ):
244 binsumUnscaled += self.histDictUnscaled[ sample ].GetBinContent( bin )
245 binsum += self.histDict[ sample ].GetBinContent( bin )
246 averageBinWeight = 1.
247 if not self.histDictUnscaled[ sample ].GetBinContent( bin ) == 0:
248 averageBinWeight = self.histDict[ sample ].GetBinContent( bin )/\
249 self.histDictUnscaled[ sample ].GetBinContent( bin )
250 pass
251 errDictBins[ sample ][ bin ] = math.sqrt( self.histDictUnscaled[ sample ].GetBinContent( bin ) ) *\
252 averageBinWeight
253 if not bin in errDictBins[ "All" ].keys():
254 errDictBins[ "All" ][ bin ] = 0.
255 pass
256 errDictBins[ "All" ][ bin ] += errDictBins[ sample ][ bin ]**2
257 pass
258
259 averageWeight = 1
260 if not binsumUnscaled == 0.:
261 averageWeight = binsum / binsumUnscaled
262
263 errDict[ sample ] = math.sqrt( binsumUnscaled ) * averageWeight
264 sum += errDict[ sample ]**2
265 pass
266
267 errDict[ "All" ] = math.sqrt( sum )
268
269 ### warning! errDictBins[ "All" ][ bin ] is squared!!
270 ### errDictBins[ sample ][ bin ] is not!
271 return errDict, errDictBins
272 def getStackErrQCD( self ):
273 errDict = {}
274 errDictBins = {}
275 errDictBins[ "All" ] = {}
276 sum = 0.
277 for sample in self.histDict.keys():
278 errDictBins[ sample ] = {}
279 err = ROOT.Double( 0 )
280 integral = self.histDict[ sample ].IntegralAndError( 0, self.histDict[ sample ].GetNbinsX() + 1,
281 err )
282 errDict[ sample ] = err
283 sum += err**2
284
285 for bin in range( 1, self.histDict[ sample ].GetNbinsX() + 1 ):
286 errDictBins[ sample ][ bin ] = self.histDict[ sample ].GetBinError( bin )
287 if not bin in errDictBins[ "All" ].keys():
288 errDictBins[ "All" ][ bin ] = 0
289 pass
290 errDictBins[ "All" ][ bin ] += errDictBins[ sample ][ bin ]**2
291 pass
292 pass
293
294 errDict[ "All" ] = math.sqrt( sum )
295
296 return errDict, errDictBins
297 pass
298
299
300 class RealTauPred:
301 def __init__( self,
302 histDict,
303 reco={},
304 prompt={},
305 notau={},
306 hadrBR = 1 - 0.352,
307 hadrBRErr = 0.,
308 eff={},
309 ):
310 self.histDict = histDict
311 #self.acc = acc
312 self.reco = reco
313 self.prompt = prompt
314 self.notau = notau
315 self.hadrBR = hadrBR
316 self.hadrBRErr = hadrBRErr
317 self.eff = eff
318 self.predDict = self.getPred()
319 self.errUpDict = self.getErrDirection( "Up" )
320 self.errDwnDict = self.getErrDirection( "Dwn" )
321 self.predHistDict = self.getPredHist()
322 #self.PredHistDictErrUp =
323 pass
324
325 def getPred( self, varyReco="No", varyPrompt="No", varyNotau="No", varyEff="No" ):
326 predDict = {}
327 for hist in self.histDict.keys():
328 predDict[ hist ] = {}
329 sum = 0.
330 for sample in self.histDict[ hist ].keys():
331
332 #acc = 1.
333 #if sample in self.acc.keys():
334 # acc = self.acc[ sample ].eff
335 # if varyAcc == "Up":
336 # acc += self.acc[ sample ].errUp
337 # elif varyAcc == "Dwn":
338 # acc -= self.acc[ sample ].errDwn
339 # pass
340 # pass
341 reco = 1.
342 if sample in self.reco.keys():
343 reco = self.reco[ sample ].eff
344 if varyReco == "Up":
345 reco += self.reco[ sample ].errUp
346 elif varyReco == "Dwn":
347 reco -= self.reco[ sample ].errDwn
348 pass
349 pass
350 prompt = 1.
351 if sample in self.prompt.keys():
352 prompt = self.prompt[ sample ].eff
353 if varyPrompt == "Up":
354 prompt += self.prompt[ sample ].errUp
355 elif varyPrompt == "Dwn":
356 prompt -= self.prompt[ sample ].errDwn
357 pass
358 pass
359 notau = 1.
360 if sample in self.notau.keys():
361 reco = self.notau[ sample ].eff
362 if varyNotau == "Up":
363 notau += self.notau[ sample ].errUp
364 elif varyNotau == "Dwn":
365 notau -= self.notau[ sample ].errDwn
366 pass
367 pass
368 eff = 1.
369 if sample in self.eff.keys():
370 eff = self.eff[ sample ].eff
371 if varyEff == "Up":
372 eff += self.eff[ sample ].errUp
373 elif varyEff == "Dwn":
374 eff -= self.eff[ sample ].errDwn
375 pass
376 pass
377
378 predDict[ hist ][ sample ] = self.histDict[ hist ][ sample ].Integral()
379
380 #if( varyPrompt == "No" and varyReco == "No" and varyNotau == "No" and varyEff == "No" ):
381 # print "Prediction ", sample, ": used acc=", acc, " eff=", eff, " hadrBR=", self.hadrBR
382 # print "used nEvts=",predDict[ hist ][ sample ]
383 # pass
384
385 if not reco == 0.:
386 predDict[ hist ][ sample ] *= 1/reco
387 pass
388 predDict[ hist ][ sample ] *= prompt
389 predDict[ hist ][ sample ] *= notau
390 predDict[ hist ][ sample ] *= self.hadrBR
391 predDict[ hist ][ sample ] *= eff
392
393
394 sum += predDict[ hist ][ sample ]
395 pass
396
397 predDict[ hist ][ "All" ] = sum
398 pass
399
400 return predDict
401
402 def getPredHist( self, varyReco="No", varyPrompt="No", varyNotau="No", varyEff="No" ):
403
404 predDict = {}
405 for hist in self.histDict.keys():
406 predDict[ hist ] = {}
407 for sample in self.histDict[ hist ].keys():
408
409 #acc = 1.
410 #if sample in self.acc.keys():
411 # acc = self.acc[ sample ].eff
412 # if varyAcc == "Up":
413 # acc += self.acc[ sample ].errUp
414 # elif varyAcc == "Dwn":
415 # acc -= self.acc[ sample ].errDwn
416 # pass
417 # pass
418 reco = 1.
419 if sample in self.reco.keys():
420 reco = self.reco[ sample ].eff
421 if varyReco == "Up":
422 reco += self.reco[ sample ].errUp
423 elif varyReco == "Dwn":
424 reco -= self.reco[ sample ].errDwn
425 pass
426 pass
427 prompt = 1.
428 if sample in self.prompt.keys():
429 prompt = self.prompt[ sample ].eff
430 if varyPrompt == "Up":
431 prompt += self.prompt[ sample ].errUp
432 elif varyPrompt == "Dwn":
433 prompt -= self.prompt[ sample ].errDwn
434 pass
435 pass
436 notau = 1.
437 if sample in self.notau.keys():
438 reco = self.notau[ sample ].eff
439 if varyNotau == "Up":
440 notau += self.notau[ sample ].errUp
441 elif varyNotau == "Dwn":
442 notau -= self.notau[ sample ].errDwn
443 pass
444 pass
445 eff = 1.
446 if sample in self.eff.keys():
447 eff = self.eff[ sample ].eff
448 if varyEff == "Up":
449 eff += self.eff[ sample ].errUp
450 elif varyEff == "Dwn":
451 eff -= self.eff[ sample ].errDwn
452 pass
453 pass
454
455 predDict[ hist ][ sample ] = self.histDict[ hist ][ sample ].Clone()
456 #if not acc == 0.:
457 # predDict[ hist ][ sample ].Scale( 1/acc )
458 # pass
459 if not reco == 0.:
460 predDict[ hist ][ sample ].Scale( 1./reco )
461 pass
462 predDict[ hist ][ sample ].Scale( prompt )
463 predDict[ hist ][ sample ].Scale( notau )
464 predDict[ hist ][ sample ].Scale( self.hadrBR )
465 predDict[ hist ][ sample ].Scale( eff )
466
467 #if( varyAcc == "No" and varyEff == "No" ):
468 # print "Prediction hist ", sample, ": used acc=", acc, " eff=", eff, " hadrBR=", self.hadrBR
469 # pass
470 pass
471 pass
472 return predDict
473
474 def getErrDirectionHist( self, direction ):
475
476 pass
477
478
479 def getErrDirection( self, direction ):
480 """
481 Get the prediction for varying the acceptans and the efficiency up
482 """
483 #predDictAccDir = self.getPred( varyAcc=direction )
484 predDictRecoDir = self.getPred( varyReco=direction )
485 predDictPromptDir = self.getPred( varyPrompt=direction )
486 predDictNotauDir = self.getPred( varyNotau=direction )
487 predDictEffDir = self.getPred( varyEff=direction )
488
489 errDirDict = {}
490 for hist in self.histDict.keys():
491 errDirDict[ hist ] = {}
492 for sample in self.histDict[ hist ].keys():
493 #errAccDir = abs( self.predDict[ hist ][ sample ] - predDictAccDir[ hist ][ sample ] )
494 errRecoDir = abs( self.predDict[ hist ][ sample ] - predDictRecoDir[ hist ][ sample ] )
495 errPromptDir = abs( self.predDict[ hist ][ sample ] - predDictPromptDir[ hist ][ sample ] )
496 errNotauDir = abs( self.predDict[ hist ][ sample ] - predDictNotauDir[ hist ][ sample ] )
497 errEffDir = abs( self.predDict[ hist ][ sample ] - predDictEffDir[ hist ][ sample ] )
498 ##errDir = math.sqrt( errAccDir**2 + errEffDir**2 )
499 errDir = math.sqrt( errRecoDir**2 + errPromptDir**2 + errNotauDir**2 + errEffDir**2 )
500 errDirDict[ hist ][ sample ] = errDir
501 pass
502 #errAccDir = abs( self.predDict[ hist ][ "All" ] - predDictAccDir[ hist ][ "All" ] )
503 errRecoDir = abs( self.predDict[ hist ][ "All" ] - predDictRecoDir[ hist ][ "All" ] )
504 errPromptDir = abs( self.predDict[ hist ][ "All" ] - predDictPromptDir[ hist ][ "All" ] )
505 errNotauDir = abs( self.predDict[ hist ][ "All" ] - predDictNotauDir[ hist ][ "All" ] )
506 errEffDir = abs( self.predDict[ hist ][ "All" ] - predDictEffDir[ hist ][ "All" ] )
507 #errDir = math.sqrt( errAccDir**2 + errEffDir**2 )
508 errDir = math.sqrt( errRecoDir**2 + errPromptDir**2 + errNotauDir**2 + errEffDir**2 )
509 errDirDict[ hist ][ "All" ] = errDir
510 pass
511 return errDirDict
512 pass
513
514 class RatioData:
515 def __init__( self, dataHist, fakeHist, realHist, max=2.48, min=0.02, mcHistErrDict={}, colorErr=1,
516 xmin=None, xmax=None, title=None, markerStyle=20 ):
517 self.allHist = fakeHist.Clone()
518 self.allHist.Add( realHist )
519
520 #if not xmin == None and not xmax == None:
521 # self.allHist.GetXaxis().SetRangeUser( xmin, xmax )
522 # dataHist.GetXaxis().SetRangeUser( xmin, xmax )
523 # pass
524
525 self.Ratio = Ratio( dataHist=dataHist, mcHist=self.allHist, max=max, min=min, mcHistErrDict=mcHistErrDict,
526 colorUp=colorErr, xmin=xmin, xmax=xmax, title=title, markerStyle=markerStyle )
527 pass
528
529 def drawRatio( self, pad ):
530 self.Ratio.drawRatio( pad )
531 pass
532
533 class Ratio:
534
535 def __init__( self, dataHist, mcHist,max=1.98, min=0.02, mcHistErrDict = {}, colorUp=ROOT.kCyan + 2,
536 xmax=None, xmin=None, title=None, markerStyle=22, xtitle=None ):
537 self.dataHist = dataHist
538 self.mcHist = mcHist
539 self.max = max
540 self.min = min
541 self.mcHistErrDict = mcHistErrDict
542 self.colorUp = colorUp
543 self.xmax = xmax
544 self.xmin = xmin
545 self.title = title
546 self.markerStyle = markerStyle
547 self.xtitle=xtitle
548 self.graph, self.histErrUp, self.histErrDwn = self.getRatioGraph( )
549 pass
550
551 def getRatioGraph( self ):
552
553 print self.dataHist.ClassName()
554
555 if self.dataHist.ClassName() == "TH1F":
556 return self.getRatioGraphHists()
557 elif self.dataHist.ClassName() == "TGraphAsymmErrors":
558 return self.getRatioGraphGraphs()
559 else:
560 print "Unknown histogram type.Doing nothing."
561 pass
562 return None
563
564
565 def getRatioGraphGraphs( self ):
566 #newHist = self.dataHist.Clone()
567
568 newHist = ROOT.TGraphAsymmErrors()
569 for point in range( 0, self.dataHist.GetN() ):
570 #print "-----",point
571 xdata = ROOT.Double( 0 )
572 ydata = ROOT.Double( 0 )
573 self.dataHist.GetPoint( point, xdata,ydata )
574 xmc = ROOT.Double( 0 )
575 ymc = ROOT.Double( 0 )
576 self.mcHist.GetPoint( point, xmc,ymc )
577 #print ymc
578 if not ymc == 0:
579 newHist.SetPoint( point, xdata, ydata/ymc )
580 ydataErrUp = self.dataHist.GetErrorYhigh( point )
581 ydataErrLow = self.dataHist.GetErrorYlow( point )
582 ymcErrUp = self.mcHist.GetErrorYhigh( point )
583 ymcErrLow = self.mcHist.GetErrorYlow( point )
584 xdataErrUp = self.dataHist.GetErrorXhigh( point )
585 xdataErrLow = self.dataHist.GetErrorXlow( point )
586 errUp = math.sqrt( ( ydataErrUp/ymc )**2 +
587 ( ( ymcErrUp * ydata )/ymc**2 )**2 )
588 errLow = math.sqrt( ( ydataErrLow/ymc )**2 +
589 ( ( ymcErrLow * ydata )/ymc**2 )**2 )
590
591 newHist.SetPointError( point, xdataErrLow, xdataErrUp, errLow, errUp )
592 pass
593 pass
594
595 newHist.GetYaxis().SetTitle( "data/mc" )
596 newHist.GetYaxis().CenterTitle()
597 newHist.UseCurrentStyle()
598 newHist.SetMarkerStyle( self.markerStyle )
599 newHist.GetXaxis().SetLabelSize( 0.10 )
600 newHist.GetYaxis().SetLabelSize( 0.06 )
601 newHist.GetXaxis().SetTitleSize( 0.1 )
602 newHist.GetYaxis().SetTitleSize( 0.06 )
603 newHist.GetYaxis().SetTitleOffset( 0.7 )
604 newHist.GetXaxis().SetTitle( self.xtitle )
605 newHist.SetMarkerColor( self.colorUp )
606 #newHist.Divide( self.mcHist )
607
608 histErrUp = None
609 histErrDwn = None
610 return newHist, histErrUp, histErrDwn
611
612 def getRatioGraphHists( self ):
613
614 newHist = self.dataHist.Clone()
615 newHist.GetYaxis().SetTitle( "data/mc" )
616 if not self.title == None:
617 newHist.GetYaxis().SetTitle( self.title )
618 pass
619 newHist.GetYaxis().CenterTitle()
620 newHist.Sumw2()
621 newHist.SetDirectory( 0 )
622 newHist.UseCurrentStyle()
623 newHist.SetMarkerStyle( self.markerStyle )
624 newHist.GetXaxis().SetLabelSize( 0.10 )
625 newHist.GetYaxis().SetLabelSize( 0.06 )
626 newHist.GetXaxis().SetTitleSize( 0.1 )
627 newHist.GetYaxis().SetTitleSize( 0.06 )
628 newHist.GetYaxis().SetTitleOffset( 0.7 )
629 newHist.Divide( self.mcHist )
630 #### error for "data"
631 for i in range( 1, newHist.GetNbinsX() + 1 ):
632 if not self.mcHist.GetBinContent( i ) == 0.:
633 newHist.SetBinError( i, self.dataHist.GetBinError( i ) / self.mcHist.GetBinContent( i ) )
634 else:
635 newHist.SetBinError( i, 0 )
636 pass
637 pass
638 ### error for "mc"
639 histErrUp = ROOT.TH1F( "errUp" + self.mcHist.GetName(),
640 "errUp" + self.mcHist.GetName(),
641 self.mcHist.GetNbinsX(), self.mcHist.GetXaxis().GetXmin(),\
642 self.mcHist.GetXaxis().GetXmax() )
643 histErrDwn = ROOT.TH1F( "errDwn" + self.mcHist.GetName(),
644 "errDwn" + self.mcHist.GetName(),
645 self.mcHist.GetNbinsX(), self.mcHist.GetXaxis().GetXmin(),\
646 self.mcHist.GetXaxis().GetXmax() )
647
648 histErrUp.GetYaxis().SetTitle( "data/mc" )
649 if not self.title == None:
650 histErrUp.GetYaxis().SetTitle( self.title )
651 pass
652 histErrUp.GetYaxis().CenterTitle()
653 histErrUp.SetDirectory( 0 )
654 histErrUp.UseCurrentStyle()
655 histErrUp.GetXaxis().SetLabelSize( 0.10 )
656 histErrUp.GetYaxis().SetLabelSize( 0.06 )
657 histErrUp.GetXaxis().SetTitleSize( 0.1 )
658 histErrUp.GetYaxis().SetTitleSize( 0.06 )
659 histErrUp.GetYaxis().SetTitleOffset( 0.7 )
660 histErrUp.GetXaxis().SetTitle( self.dataHist.GetXaxis().GetTitle() )
661 histErrUp.SetTitle( "" )
662 histErrDwn.GetYaxis().SetTitle( "data/mc" )
663 if not self.title == None:
664 histErrDwn.GetYaxis().SetTitle( self.title )
665 pass
666 histErrDwn.GetYaxis().CenterTitle()
667 histErrDwn.SetDirectory( 0 )
668 histErrDwn.UseCurrentStyle()
669 histErrDwn.GetXaxis().SetLabelSize( 0.10 )
670 histErrDwn.GetYaxis().SetLabelSize( 0.06 )
671 histErrDwn.GetXaxis().SetTitleSize( 0.1 )
672 histErrDwn.GetYaxis().SetTitleSize( 0.06 )
673 histErrDwn.GetYaxis().SetTitleOffset( 0.7 )
674 histErrDwn.GetXaxis().SetTitle( self.dataHist.GetXaxis().GetTitle() )
675 histErrDwn.SetTitle( "" )
676
677 print "mc hist nbins=", self.mcHist.GetNbinsX()
678
679 if not self.mcHistErrDict == {}:
680 for i in range( 1, self.mcHist.GetNbinsX() + 1 ):
681 #print "bin ", i, "histDict content= ", self.mcHistErrDict[ i ]
682 errmc = math.sqrt( self.mcHistErrDict[ i ] )
683 if not self.mcHist.GetBinContent( i ) == 0.:
684 err = errmc * self.dataHist.GetBinContent( i ) / ( self.mcHist.GetBinContent( i ) )**2
685 #err = errmc / ( self.mcHist.GetBinContent( i ) )**2
686 #print "bin ", i, " : ", err
687 histErrUp.SetBinContent( i, 1 + err )
688 histErrDwn.SetBinContent( i, 1 - err )
689 pass
690 else:
691 histErrDwn.SetBinContent( i, 1 )
692 pass
693 pass
694 pass
695
696 #histErrUp.UseCurrentStyle()
697 #histErrDwn.UseCurrentStyle()
698
699 newHist.GetYaxis().SetRangeUser( self.min, self.max )
700 histErrUp.GetYaxis().SetRangeUser( self.min, self.max )
701 histErrDwn.GetYaxis().SetRangeUser( self.min, self.max )
702 histErrUp.SetFillStyle( 3354 )
703 histErrUp.SetFillColor( self.colorUp )
704 histErrUp.SetLineColor( self.colorUp )
705 histErrDwn.SetFillColor( 10 )
706 histErrDwn.SetLineColor( self.colorUp )
707 histErrDwn.SetFillStyle( 1001 )
708
709 return newHist, histErrUp, histErrDwn
710
711 def getRatioGraphInvert( self ):
712
713 newHist = self.mcHist.Clone()
714 newHist.GetYaxis().SetTitle( "mc/data" )
715 if not self.title == None:
716 newHist.GetYaxis().SetTitle( self.title )
717 pass
718 newHist.GetYaxis().CenterTitle()
719 newHist.Sumw2()
720 newHist.SetDirectory( 0 )
721 newHist.UseCurrentStyle()
722 newHist.SetMarkerStyle( 22 )
723 newHist.GetXaxis().SetLabelSize( 0.10 )
724 newHist.GetYaxis().SetLabelSize( 0.06 )
725 newHist.GetXaxis().SetTitleSize( 0.1 )
726 newHist.GetYaxis().SetTitleSize( 0.06 )
727 newHist.GetYaxis().SetTitleOffset( 0.7 )
728 newHist.Divide( self.dataHist )
729 #for i in range( 1, newHist.GetNbinsX() + 1 ):
730 # if not self.mcHist.GetBinContent( i ) == 0.:
731 # newHist.SetBinError( i, self.dataHist.GetBinError( i ) / self.mcHist.GetBinContent( i ) )
732 # else:
733 # newHist.SetBinError( i, 0 )
734 # pass
735 # pass
736
737 newHist.GetYaxis().SetRangeUser( self.min, self.max )
738
739 return newHist
740
741
742 def drawRatio( self, pad ):
743 """
744 Adds a pad to the existing, where the ratio should be drawn
745 """
746
747 logx = pad.GetLogx()
748 left = pad.GetLeftMargin()
749 right = pad.GetRightMargin()
750
751 pad.SetBottomMargin( 0.36 )
752 pad.SetRightMargin(right)
753 pad.SetLeftMargin(left)
754 pad.SetBorderMode(0)
755 pad.SetBorderSize(0)
756
757 self.rPad = ROOT.TPad("rPad","",0,0,1,.361)
758 self.rPad.Draw()
759 self.rPad.cd()
760 self.rPad.SetLogy(0)
761 self.rPad.SetLogx(logx)
762 self.rPad.SetTicky(1)
763 self.rPad.SetTopMargin(0.01)
764 self.rPad.SetBottomMargin(0.45)
765 self.rPad.SetRightMargin(right)
766 self.rPad.SetLeftMargin(left)
767 self.rPad.SetBorderSize(0)
768 self.rPad.SetBorderMode(0)
769
770 #self.graphComb.Draw( "E2" )
771 option = ""
772 if not self.histErrUp == None:
773 if not self.xmax == None and not self.xmin == None:
774 self.histErrUp.GetXaxis().SetRangeUser( self.xmin, self.xmax )
775 self.histErrDwn.GetXaxis().SetRangeUser( self.xmin, self.xmax )
776 pass
777
778 self.histErrUp.GetYaxis().SetNdivisions( 505 )
779 self.histErrUp.GetYaxis().SetTickLength( ROOT.gStyle.GetTickLength("Y")/0.8 )
780 self.histErrUp.GetYaxis().SetLabelSize( 0.1 )
781 self.histErrUp.GetYaxis().SetTitleOffset( 0.6 )
782 self.histErrUp.GetYaxis().SetTitleSize( 0.1 )
783
784 if not self.mcHistErrDict == {}:
785 self.histErrUp.Draw( )
786 option = "same"
787 self.histErrDwn.Draw( option )
788 self.histErrUp.Draw( "axissame" )
789 #option = "same"
790 pass
791 pass
792 else:
793 option = "A"
794 self.graph.Draw( "ep" + option )
795 if not self.xmax == None and not self.xmin == None:
796 self.graph.GetXaxis().SetRangeUser( self.xmin, self.xmax )
797 pass
798
799 self.rPad.Update()
800 ### draw a line at 1:
801 #line = TLine( self.dataHist.GetXaxis().GetXmin(), self.dataHist.GetXaxis().GetXmax() ,1,1 )
802 self.line = ROOT.TLine( self.dataHist.GetXaxis().GetXmin(), 1, self.dataHist.GetXaxis().GetXmax(), 1 )
803 if not self.xmin == None and not self.xmax == None:
804 self.line = ROOT.TLine( self.xmin, 1, self.xmax, 1 )
805 self.line.SetLineColor( 1 )
806 self.line.Draw( "same" )
807 self.rPad.Update()
808
809 pass
810
811 pass
812
813 class OneBinHist:
814 def __init__( self, dict ):
815 self.histDict = dict
816 pass
817
818 def getOneBinHists( self ):
819 dict = {}
820 for key in self.histDict.keys():
821 nBins = self.histDict[ key ].GetNbinsX()
822 dict[ key ] = self.histDict[ key ].Clone()
823 dict[ key ].Rebin( nBins )
824 pass
825 return dict
826 pass
827
828 class StatErrorHists:
829 def __init__( self, histDict, histErrDictUp,histErrDictDwn, colorUp = 1, colorDwn = 0 ):
830 self.histDict = histDict
831 self.histErrDictUp = histErrDictUp
832 self.histErrDictDwn = histErrDictDwn
833 self.colorUp = colorUp
834 self.colorDwn = colorDwn
835 pass
836
837 def getErrHists( self, histname ):
838 histUpDict = {}
839 histDwnDict = {}
840 name = "All"
841 #for hist in self.histDict.keys():
842 #print "NbinsX=", self.histDict.GetNbinsX(), " min=", self.histDict.GetXaxis().GetXmin(), " max=", \
843 # self.histDict.GetXaxis().GetXmax()
844 histUpDict[ name ] = ROOT.TH1F( name + histname + "Up", name + histname + "Up", self.histDict.GetNbinsX(),
845 self.histDict.GetXaxis().GetXmin(), self.histDict.GetXaxis().GetXmax() )
846 histUpDict[ name ].SetDirectory( 0 )
847 histUpDict[ name ].UseCurrentStyle()
848 histUpDict[ name ].SetFillStyle( 3354 )
849 histUpDict[ name ].SetFillColor( self.colorUp )
850 histUpDict[ name ].SetLineColor( self.colorUp )
851 histDwnDict[ name ] = ROOT.TH1F( name + histname + "Dwn", name + histname + "Dwn", self.histDict.GetNbinsX(),
852 self.histDict.GetXaxis().GetXmin(), self.histDict.GetXaxis().GetXmax() )
853 histDwnDict[ name ].SetDirectory( 0 )
854 histDwnDict[ name ].UseCurrentStyle()
855 histDwnDict[ name ].SetFillStyle( 1001 )
856 histDwnDict[ name ].SetFillColor( self.colorDwn )
857 histDwnDict[ name ].SetLineColor( self.colorUp )
858 for bin in self.histErrDictUp[ name ].keys():
859 #print bin, " : ", self.histDict.GetBinCenter( int( bin ) ), "=", self.histDict.GetBinContent( int( bin ) )
860 #print self.histDict.GetBinCenter( int( bin ) )
861 #xval = self.histDict.GetBinCenter( int( bin ) )
862 yval = self.histDict.GetBinContent( int( bin ) )
863 #errxval = self.histDict.GetBinWidth( int( bin ) )
864 erryvalUp = math.sqrt( self.histErrDictUp[ name ][ int( bin ) ] )
865 erryvalDwn = math.sqrt( self.histErrDictDwn[ name ][ int( bin ) ] )
866 #print "err=", erryval, " Up=", yval + erryval, " Dwn=", yval - erryval
867 histUpDict[ name ].SetBinContent( bin, yval + erryvalUp )
868 histDwnDict[ name ].SetBinContent( bin, yval - erryvalDwn )
869 pass
870 return histUpDict, histDwnDict
871
872 pass
873
874 pass
875
876 class MakeErrGraph:
877 #def __init__( self, fakeDict, fakeDictU, realDict, realDictU, systDict={} ):
878 def __init__( self, fakeDict, realDict, errDict={} ):
879 self.fakeDict = fakeDict
880 self.realDict = realDict
881 #self.fakeDictU = fakeDictU
882 #self.realDictU = realDictU
883 #self.binErrDict, self.binMeanDict, self.binXDict, self.binWidthDict = self.getBinErrs()
884 self.binMeanDict, self.binXDict, self.binWidthDict = self.getBinErrs()
885 self.errDict = errDict
886 pass
887
888 def getBinErrs( self ):
889 #binErrDict = {}
890 binMeanDict = {}
891 binXDict = {}
892 binWidthDict = {}
893 for hist in self.fakeDict.keys():
894 #binErrDict[ hist ] = {}
895 binMeanDict[ hist ] = {}
896 binXDict[ hist ] = {}
897 binWidthDict[ hist ] = {}
898 for sample in self.fakeDict[ hist ].keys():
899 for bin in range( 1, self.fakeDict[ hist ][ sample ].GetNbinsX() + 1 ):
900 ### initialize bin error with 0
901 #if not bin in binErrDict[ hist ].keys():
902 # binErrDict[ hist ][ bin ] = 0
903 # pass
904
905 rName = hist
906 if hist == "JetPt":
907 rName = "Tau1Pt"
908 pass
909 elif hist == "JetPhi":
910 rName = "Tau1Phi"
911 pass
912 elif hist == "JetEta":
913 rName = "Tau1Eta"
914 pass
915 elif hist == "nJets":
916 rName = "NumberOfJets"
917 pass
918
919 #binContentFakeU = self.fakeDictU[ hist + "Unweighted" ][ sample ].GetBinContent( bin )
920 binContentFake = self.fakeDict[ hist ][ sample ].GetBinContent( bin )
921 #binContentRealU = self.realDictU[ rName + "Unscaled" ][ sample ].GetBinContent( bin )
922 binContentReal = self.realDict[ rName ][ sample ].GetBinContent( bin )
923
924 binMeanDict[ hist ][ bin ] = binContentFake + binContentReal
925 binXDict[ hist ][ bin ] = self.fakeDict[ hist ][ sample ].GetBinCenter( bin )
926 binWidthDict[ hist ][ bin ] = self.fakeDict[ hist ][ sample ].GetBinWidth( bin )
927
928 #binErrFake = 0
929 #if not binContentFakeU == 0:
930 # binErrFake = math.sqrt( binContentFakeU ) * binContentFake/binContentFakeU
931 # pass
932 #binErrReal = 0
933 #if not binContentRealU == 0:
934 # binErrReal = math.sqrt( binContentRealU ) * binContentReal/binContentRealU
935 # pass
936
937 #binErr = math.sqrt( binErrFake**2 + binErrReal**2 )
938 #binErrDict[ hist ][ bin ] = binErr
939 pass
940 pass
941 pass
942 #return binErrDict, binMeanDict, binXDict, binWidthDict
943 return binMeanDict, binXDict, binWidthDict
944
945 #def addSystErr( self ):
946 # for hist in self.fakeDict.keys():
947 # if hist in self.systDict.keys():
948 # for bin in self.binErrDict[ hist ].keys():
949 # self.binErrDict[ hist ][ bin ] = math.sqrt( self.binErrDict[ hist ][ bin ]**2 +\
950 # self.systDict[ hist ][ bin ] )
951 # pass
952 # pass
953 # pass
954
955
956 def getErrGraphs( self ):
957 graphDict = {}
958 #for hist in self.binErrDict.keys():
959 for hist in self.errDict[ "Up" ].keys():
960 graphDict[ hist ] = ROOT.TGraphAsymmErrors()
961 #graphDict[ hist ].SetDirectory( 0 )
962 #for bin in self.binErrDict[ hist ].keys():
963 rName = hist
964 if hist == "Tau1Pt":
965 rName = "JetPt"
966 pass
967 elif hist == "Tau1Phi":
968 rName = "JetPhi"
969 pass
970 elif hist == "Tau1Eta":
971 rName = "JetEta"
972 pass
973 elif hist == "NumberOfJets":
974 rName = "nJets"
975 pass
976 for bin in self.errDict[ "Up" ][ hist ][ "All" ].keys():
977 ### note: hist binning starts at 1,
978 ### graph binning at 0 !
979 graphDict[ hist ].SetPoint( bin - 1,
980 self.binXDict[ rName ][ bin ],
981 self.binMeanDict[ rName ][ bin ] )
982 graphDict[ hist ].SetPointError( bin - 1,
983 self.binWidthDict[ rName ][ bin ] / 2,
984 self.binWidthDict[ rName ][ bin ] / 2,
985 math.sqrt( self.errDict[ "Dwn" ][ hist ][ "All" ][ bin ] ),
986 math.sqrt( self.errDict[ "Up" ][ hist ][ "All" ][ bin ] )
987 )
988 pass
989 pass
990
991 return graphDict
992
993 pass
994
995 class SystErr:
996 def __init__( self, hist, theSystDict={} ):
997 self.hist = hist
998 self.theSystDict = theSystDict
999 pass
1000
1001 def getGlobalSystErr( self ):
1002 errUp2 = 0.
1003 errDwn2 = 0.
1004 entries = self.hist.Integral()
1005 for syst in self.theSystDict.keys():
1006 errUp2 += ( self.theSystDict[ syst ][ "Up" ] * entries )**2
1007 errDwn2 += ( self.theSystDict[ syst ][ "Dwn" ] * entries )**2
1008 pass
1009 return ( errUp2, errDwn2 )
1010
1011 def getBinSystErr( self ):
1012 err2Dict = {}
1013 for bin in range( 1, self.hist.GetNbinsX() + 1 ):
1014 err2Dict[ bin ] = {}
1015 err2Dict[ bin ][ "Up" ] = 0.
1016 err2Dict[ bin ][ "Dwn" ] = 0.
1017 for syst in self.theSystDict.keys():
1018 err2Dict[ bin ][ "Up" ] += ( self.theSystDict[ syst ][ "Up" ] *
1019 self.hist.GetBinContent( bin ) )**2
1020 err2Dict[ bin ][ "Dwn" ] += ( self.theSystDict[ syst ][ "Dwn" ] *
1021 self.hist.GetBinContent( bin ) )**2
1022 pass
1023 pass
1024 return err2Dict
1025
1026 def getSystErr( self ):
1027 return ( self.getGlobalSystErr(), self.getBinSystErr() )
1028 pass
1029
1030 class SystFromHist:
1031 def __init__( self, dict ):
1032 self.histDict = dict
1033 pass
1034
1035 def getSystDict( self ):
1036 systDict = {}
1037 for hist in self.histDict.keys():
1038 systDict[ hist ] = 0.
1039 firstKey = self.histDict[ hist ].keys()[ 0 ]
1040 for bin in range( 1, self.histDict[ hist ][ firstKey ].GetNbinsX() + 1 ):
1041 linEntry = 0.
1042 for sample in self.histDict[ hist ].keys():
1043 entry = self.histDict[ hist ][ sample ].GetBinContent( bin )
1044 linEntry += entry
1045 pass
1046 systDict[ hist ] += linEntry**2
1047 pass
1048 pass
1049 return systDict