ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/tree_stack.py
(Generate patch)

Comparing UserCode/VHbb/python/tree_stack.py (file contents):
Revision 1.30 by nmohr, Tue Oct 16 12:28:03 2012 UTC vs.
Revision 1.31 by nmohr, Thu Oct 18 13:44:02 2012 UTC

# Line 3 | Line 3 | from samplesclass import sample
3   from printcolor import printc
4   import pickle
5   import ROOT
6 from ROOT import TFile, TTree
7 import ROOT
6   from array import array
7   from BetterConfigParser import BetterConfigParser
8   import sys, os
9   from mvainfos import mvainfo
10   #from gethistofromtree import getHistoFromTree, orderandadd
13 from Ratio import getRatio
11   from optparse import OptionParser
12   from HistoMaker import HistoMaker, orderandadd
16 import TdrStyles
13   from copy import deepcopy
14 + from StackMaker import StackMaker
15  
16   #CONFIGURE
17   argv = sys.argv
# Line 31 | Line 28 | if opts.config =="":
28   print opts.config
29   config = BetterConfigParser()
30   config.read(opts.config)
34 anaTag = config.get("Analysis","tag")
35
31  
32   path = opts.path
33   region = opts.region
34  
40 plotConfig = BetterConfigParser()
41 plotConfig.read('vhbbPlotDef.ini')
42
35   #get locations:
36   Wdir=config.get('Directories','Wdir')
37   samplesinfo=config.get('Directories','samplesinfo')
38  
39   section='Plot:%s'%region
40  
49 Normalize = eval(config.get(section,'Normalize'))
50 logPlot = eval(config.get(section,'log'))
51 blind = eval(config.get(section,'blind'))
52 rebin = 1
53 nBinsCfg = False
54 minCfg = False
55 maxCfg = False
56 if config.has_option(section,'rebin'):
57    rebin = eval(config.get(section,'rebin'))
58 if config.has_option(section,'nBins'):
59    nBinsCfg = eval(config.get(section,'nBins'))
60 if config.has_option(section,'min'):
61    minCfg = eval(config.get(section,'min'))
62 if config.has_option(section,'max'):
63    maxCfg = eval(config.get(section,'max'))
64
41   infofile = open(samplesinfo,'r')
42   info = pickle.load(infofile)
43   infofile.close()
44  
45   #options = plot.split(',')
46  
71 mass = config.get(section,'Signal')
72
47   vars = (config.get(section, 'vars')).split(',')
48  
75 names = [plotConfig.get('plotDef:%s'%x,'relPath') for x in vars]
76 xAxis = [plotConfig.get('plotDef:%s'%x,'xAxis') for x in vars]
77 nBins = []
78 xMin = []
79 xMax = []
80 log = []
81 for x in vars:
82    if logPlot:
83        log.append(True)
84    elif plotConfig.has_option('plotDef:%s'%x,'log'):
85        log.append(eval(plotConfig.get('plotDef:%s'%x,'log')))
86    else:
87        log.append(False)
88    if nBinsCfg:
89        nBins.append(nBinsCfg)
90    else:
91        nBins.append(int(eval(plotConfig.get('plotDef:%s'%x,'nBins'))/rebin))
92    if minCfg:
93        xMin.append(minCfg)
94    else:
95        xMin.append(eval(plotConfig.get('plotDef:%s'%x,'min')))
96    if maxCfg:
97        xMax.append(maxCfg)
98    else:
99        xMax.append(eval(plotConfig.get('plotDef:%s'%x,'max')))
100
101 for p in range(0,len(names)):
102    if '<mass>' in names[p]:
103        newp= names[p].replace('<mass>',mass)
104        names[p]=newp
105    print names[p]
106
49   if 'ZLight' in region or 'TTbar' in region or 'Zbb' in region: SignalRegion = False
50   else:
51      SignalRegion = True
52      print 'You are in the Signal Region!'
53  
54   data = config.get(section,'Datas')
113 if config.has_option(section, 'Datacut'):
114    datacut=config.get(section, 'Datacut')
115 else:
116    datacut = region
117
118 options=[]
119
120 if blind: blindopt='blind'
121 else: blindopt = 'noblind'
122
123 for i in range(0,len(vars)):
124    options.append([names[i],'',xAxis[i],nBins[i],xMin[i],xMax[i],'%s_%s.pdf'%(region,vars[i]),region,datacut,mass,data,blindopt])
125
126
127 setup=config.get('Plot_general','setup')
128 if logPlot:
129    setup=config.get('Plot_general','setupLog')
130 setup=setup.split(',')
55  
56   samples=config.get('Plot_general','samples')
57   samples=samples.split(',')
58  
135 colorDict=eval(config.get('Plot_general','colorDict'))
136 typLegendDict=eval(config.get('Plot_general','typLegendDict'))
137 #color=color.split(',')
138
139
59   weightF=config.get('Weights','weightF')
60   Group = eval(config.get('Plot_general','Group'))
61  
62  
63   #GETALL AT ONCE
64 + options = []
65 + Stacks = []
66 + for i in range(len(vars)):
67 +    Stacks.append(StackMaker(config,vars[i],region,SignalRegion))
68 +    options.append(Stacks[i].options)
69  
70   Plotter=HistoMaker(path,config,region,options)
71  
# Line 152 | Line 76 | Ldatas = [[] for _ in range(0,len(vars))
76   Ldatatyps = [[] for _ in range(0,len(vars))]
77   Ldatanames = [[] for _ in range(0,len(vars))]
78  
155 def myText(txt="CMS Preliminary",ndcX=0,ndcY=0,size=0.8):
156    ROOT.gPad.Update()
157    text = ROOT.TLatex()
158    text.SetNDC()
159    text.SetTextColor(ROOT.kBlack)
160    text.SetTextSize(text.GetTextSize()*size)
161    text.DrawLatex(ndcX,ndcY,txt)
162    return text
163
79  
80   #Find out Lumi:
81   lumicounter=0.
# Line 174 | Line 89 | if lumicounter > 0:
89      lumi=lumi/lumicounter
90  
91   Plotter.lumi=lumi
92 + mass = Stacks[0].mass
93 +
94  
95   for job in info:
96      if eval(job.active):
# Line 213 | Line 130 | for job in info:
130                      Ldatatyps[v].append(typ[v])
131                      Ldatanames[v].append(job.name)
132  
216 if not SignalRegion: setup.remove('ZH')
217
218 print setup
133  
134   for v in range(0,len(vars)):
135  
136      histos = Lhistos[v]
137      typs = Ltyps[v]
138 <    datas = Ldatas[v]
139 <    datatyps = Ldatatyps[v]
140 <    datanames= Ldatanames[v]
141 <
142 <    Overlay = Overlaylist[v]
143 <
144 <    TdrStyles.tdrStyle()
231 <    
232 <    c = ROOT.TCanvas(vars[v],'', 600, 600)
233 <    c.SetFillStyle(4000)
234 <    c.SetFrameFillStyle(1000)
235 <    c.SetFrameFillColor(0)
236 <
237 <    oben = ROOT.TPad('oben','oben',0,0.3 ,1.0,1.0)
238 <    oben.SetBottomMargin(0)
239 <    oben.SetFillStyle(4000)
240 <    oben.SetFrameFillStyle(1000)
241 <    oben.SetFrameFillColor(0)
242 <    unten = ROOT.TPad('unten','unten',0,0.0,1.0,0.3)
243 <    unten.SetTopMargin(0.)
244 <    unten.SetBottomMargin(0.35)
245 <    unten.SetFillStyle(4000)
246 <    unten.SetFrameFillStyle(1000)
247 <    unten.SetFrameFillColor(0)
248 <
249 <    oben.Draw()
250 <    unten.Draw()
251 <
252 <    oben.cd()
253 <
254 <
255 <    
256 <    allStack = ROOT.THStack(vars[v],'')    
257 <    l = ROOT.TLegend(0.63, 0.60,0.92,0.92)
258 <    l.SetLineWidth(2)
259 <    l.SetBorderSize(0)
260 <    l.SetFillColor(0)
261 <    l.SetFillStyle(4000)
262 <    l.SetTextFont(62)
263 <    l.SetTextSize(0.035)
264 <    MC_integral=0
265 <    MC_entries=0
266 <
267 <    for histo in histos:
268 <        MC_integral+=histo.Integral()
269 <        #MC_entries+=histo.GetEntries()
270 <    print "\033[1;32m\n\tMC integral = %s\033[1;m"%MC_integral
271 <
272 <    #ORDER AND ADD TOGETHER
273 <    #print typs
274 <    #print setup
275 <    histos, typs = orderandadd(histos,typs,setup)
276 <
277 <
278 <    k=len(histos)
138 >    Stacks[v].histos = Lhistos[v]
139 >    Stacks[v].typs = Ltyps[v]
140 >    Stacks[v].datas = Ldatas[v]
141 >    Stacks[v].datatyps = Ldatatyps[v]
142 >    Stacks[v].datanames= Ldatanames[v]
143 >    Stacks[v].overlay = Overlaylist[v]
144 >    Stacks[v].lumi = lumi
145      
146 <    for j in range(0,k):
281 <        #print histos[j].GetBinContent(1)
282 <        i=k-j-1
283 <        histos[i].SetFillColor(int(colorDict[setup[i]]))
284 <        histos[i].SetLineColor(1)
285 <        allStack.Add(histos[i])
286 <
287 <    d1 = ROOT.TH1F('noData','noData',nBins[v],xMin[v],xMax[v])
288 <    datatitle='Data'
289 <    addFlag = ''
290 <    if 'Zee' in datanames and 'Zmm' in datanames:
291 <        addFlag = 'Z(l^{-}l^{+})H(b#bar{b})'
292 <    elif 'Zee' in datanames:
293 <        addFlag = 'Z(e^{-}e^{+})H(b#bar{b})'
294 <    elif 'Zmm' in datanames:
295 <        addFlag = 'Z(#mu^{-}#mu^{+})H(b#bar{b})'
296 <    for i in range(0,len(datas)):
297 <        d1.Add(datas[i],1)
298 <    print "\033[1;32m\n\tDATA integral = %s\033[1;m"%d1.Integral()
299 <    flow = d1.GetEntries()-d1.Integral()
300 <    if flow > 0:
301 <        print "\033[1;31m\tU/O flow: %s\033[1;m"%flow
302 <
303 <    Overlay.SetLineColor(2)
304 <    Overlay.SetLineWidth(2)
305 <    Overlay.SetFillColor(0)
306 <    Overlay.SetFillStyle(4000)
307 <    Overlay.SetNameTitle('Overlay','Overlay')
308 <
309 <    l.AddEntry(d1,datatitle,'P')
310 <    for j in range(0,k):
311 <        l.AddEntry(histos[j],typLegendDict[typs[j]],'F')
312 <    l.AddEntry(Overlay,typLegendDict['Overlay'],'L')
313 <    
314 <    if Normalize:
315 <        if MC_integral != 0:    stackscale=d1.Integral()/MC_integral
316 <        Overlay.Scale(stackscale)
317 <        stackhists=allStack.GetHists()
318 <        for blabla in stackhists:
319 <                if MC_integral != 0: blabla.Scale(stackscale)
320 <    
321 <    allMC=allStack.GetStack().Last().Clone()
322 <
323 <    allStack.SetTitle()
324 <    allStack.Draw("hist")
325 <    allStack.GetXaxis().SetTitle('')
326 <    yTitle = 'Entries'
327 <    if not '/' in yTitle:
328 <            yAppend = '%s' %(allStack.GetXaxis().GetBinWidth(1))
329 <            yTitle = '%s / %s' %(yTitle, yAppend)
330 <    allStack.GetYaxis().SetTitle(yTitle)
331 <    allStack.GetXaxis().SetRangeUser(xMin[v],xMax[v])
332 <    allStack.GetYaxis().SetRangeUser(0,20000)
333 <    theErrorGraph = ROOT.TGraphErrors(allMC)
334 <    theErrorGraph.SetFillColor(ROOT.kGray+3)
335 <    theErrorGraph.SetFillStyle(3013)
336 <    theErrorGraph.Draw('SAME2')
337 <    l.AddEntry(theErrorGraph,"MC uncert. (stat.)","fl")
338 <    Ymax = max(allStack.GetMaximum(),d1.GetMaximum())*1.7
339 <    if log[v]:
340 <        allStack.SetMinimum(0.05)
341 <        Ymax = Ymax*ROOT.TMath.Power(10,1.6*(ROOT.TMath.Log(1.6*(Ymax/0.1))/ROOT.TMath.Log(10)))*(0.6*0.1)
342 <        ROOT.gPad.SetLogy()
343 <    allStack.SetMaximum(Ymax)
344 <    c.Update()
345 <    ROOT.gPad.SetTicks(1,1)
346 <    #allStack.Draw("hist")
347 <    d1.Draw("E,same")
348 <    l.SetFillColor(0)
349 <    l.SetBorderSize(0)
350 <    l.Draw()
351 <
352 <    Overlay.Draw('hist,same')
353 <
354 <    tPrel = myText("CMS Preliminary",0.17,0.88,1.04)
355 <    tLumi = myText("#sqrt{s} =  %s, L = %s fb^{-1}"%(anaTag,(float(lumi)/1000.)),0.17,0.83)
356 <    tAddFlag = myText(addFlag,0.17,0.78)
357 <
358 <    unten.cd()
359 <    ROOT.gPad.SetTicks(1,1)
360 <
361 <    ratio, error = getRatio(d1,allMC,xMin[v],xMax[v])
362 <    ksScore = d1.KolmogorovTest( allMC )
363 <    chiScore = d1.Chi2Test( allMC , "UWCHI2/NDF")
364 <    print ksScore
365 <    print chiScore
366 <    ratio.SetStats(0)
367 <    ratio.GetXaxis().SetTitle(xAxis[v])
368 <    ratioError = ROOT.TGraphErrors(error)
369 <    ratioError.SetFillColor(ROOT.kGray+3)
370 <    ratioError.SetFillStyle(3013)
371 <    ratio.Draw("E1")
372 <    ratioError.Draw('SAME2')
373 <    ratio.Draw("E1SAME")
374 <    ratio.SetTitle("")
375 <    m_one_line = ROOT.TLine(xMin[v],1,xMax[v],1)
376 <    m_one_line.SetLineStyle(ROOT.kDashed)
377 <    m_one_line.Draw("Same")
378 <
379 <    if not blind:
380 <        tKsChi = myText("#chi_{#nu}^{2} = %.3f K_{s} = %.3f"%(chiScore,ksScore),0.17,0.9,1.5)
381 <    t0 = ROOT.TText()
382 <    t0.SetTextSize(ROOT.gStyle.GetLabelSize()*2.4)
383 <    t0.SetTextFont(ROOT.gStyle.GetLabelFont())
384 <    if not log[v]:
385 <        t0.DrawTextNDC(0.1059,0.96, "0")
146 >    Stacks[v].doPlot()
147  
387    name = '%s/%s' %(config.get('Directories','plotpath'),options[v][6])
388    c.Print(name)
148  
149      print 'i am done!\n'
150  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines