ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/gethistofromtree.py
Revision: 1.18
Committed: Fri Oct 19 15:44:07 2012 UTC (12 years, 6 months ago) by peller
Content type: text/x-python
Branch: MAIN
CVS Tags: hcpApproval, HCP_unblinding
Changes since 1.17: +5 -3 lines
Log Message:
BDT plots from TH files

File Contents

# Content
1 from samplesclass import sample
2 from printcolor import printc
3 import pickle
4 import ROOT
5 from ROOT import TFile, TTree
6 import ROOT
7 from array import array
8 from BetterConfigParser import BetterConfigParser
9 import sys
10
11
12 def getScale(job,path,config,rescale,subsample=-1):
13 anaTag=config.get('Analysis','tag')
14 input = TFile.Open(path+'/'+job.getpath())
15 CountWithPU = input.Get("CountWithPU")
16 CountWithPU2011B = input.Get("CountWithPU2011B")
17 #print lumi*xsecs[i]/hist.GetBinContent(1)
18
19 if subsample>-1:
20 if type(job.xsec[subsample]) == str: xsec=float(eval(job.xsec[subsample]))
21 else: xsec=float(job.xsec[subsample])
22 sf=float(job.sf[subsample])
23 else:
24 if type(job.xsec) == str: xsec=float(eval(job.xsec))
25 else: xsec=float(job.xsec)
26 sf=float(job.sf)
27
28
29 theScale = 1.
30 if anaTag == '7TeV':
31 theScale = float(job.lumi)*xsec*sf/(0.46502*CountWithPU.GetBinContent(1)+0.53498*CountWithPU2011B.GetBinContent(1))*rescale/float(job.split)
32 elif anaTag == '8TeV':
33 theScale = float(job.lumi)*xsec*sf/(CountWithPU.GetBinContent(1))*rescale/float(job.split)
34 return theScale
35
36 def getHistoFromTree(job,path,config,options,rescale=1,subsample=-1,which_weightF='weightF'):
37
38 #print job.getpath()
39 #print options
40 treeVar=options[0]
41 if subsample>-1:
42 name=job.subnames[subsample]
43 group=job.group[subsample]
44 else:
45 name=job.name
46 group=job.group
47
48 #title=job.plotname()
49 nBins=int(options[3])
50 xMin=float(options[4])
51 xMax=float(options[5])
52 #addOverFlow=eval(config.get('Plot_general','addOverFlow'))
53 addOverFlow = False
54
55 TrainFlag = eval(config.get('Analysis','TrainFlag'))
56 if TrainFlag: traincut = " & EventForTraining == 0"
57 if not TrainFlag: traincut=""
58
59 if job.type != 'DATA':
60
61 if type(options[7])==str:
62 cutcut=config.get('Cuts',options[7])
63 elif type(options[7])==list:
64 cutcut=config.get('Cuts',options[7][0])
65 cutcut=cutcut.replace(options[7][1],options[7][2])
66 print cutcut
67 if subsample>-1:
68 treeCut='%s & %s%s'%(cutcut,job.subcuts[subsample],traincut)
69 else:
70 treeCut='%s%s'%(cutcut,traincut)
71
72 elif job.type == 'DATA':
73 cutcut=config.get('Cuts',options[8])
74 treeCut='%s'%(cutcut)
75
76
77 input = TFile.Open(path+'/'+job.getpath(),'read')
78
79 Tree = input.Get(job.tree)
80 #Tree=tmpTree.CloneTree()
81 #Tree.SetDirectory(0)
82
83 #Tree=tmpTree.Clone()
84 weightF=config.get('Weights',which_weightF)
85 #hTree = ROOT.TH1F('%s'%name,'%s'%title,nBins,xMin,xMax)
86 #hTree.SetDirectory(0)
87 #hTree.Sumw2()
88 #print 'drawing...'
89 if job.type != 'DATA':
90 #print treeCut
91 #print job.name
92 if Tree.GetEntries():
93 Tree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),'(%s)*(%s)' %(treeCut,weightF), "goff,e")
94 full=True
95 else:
96 full=False
97 elif job.type == 'DATA':
98
99 if len(options)>10:
100 if options[11] == 'blind':
101 treeCut = treeCut + '&'+treeVar+'<0'
102
103
104 Tree.Draw('%s>>%s(%s,%s,%s)' %(treeVar,name,nBins,xMin,xMax),treeCut, "goff,e")
105 full = True
106 if full:
107 hTree = ROOT.gDirectory.Get(name)
108 else:
109 hTree = ROOT.TH1F('%s'%name,'%s'%name,nBins,xMin,xMax)
110 hTree.Sumw2()
111 #print job.name + ' Sumw2', hTree.GetEntries()
112
113 if job.type != 'DATA':
114 ScaleFactor = getScale(job,path,config,rescale,subsample)
115 if ScaleFactor != 0:
116 hTree.Scale(ScaleFactor)
117
118 if addOverFlow:
119 print 'Adding overflow'
120 uFlow = hTree.GetBinContent(0)+hTree.GetBinContent(1)
121 oFlow = hTree.GetBinContent(hTree.GetNbinsX()+1)+hTree.GetBinContent(hTree.GetNbinsX())
122 uFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(0),2)+ROOT.TMath.Power(hTree.GetBinError(1),2))
123 oFlowErr = ROOT.TMath.Sqrt(ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()),2)+ROOT.TMath.Power(hTree.GetBinError(hTree.GetNbinsX()+1),2))
124 hTree.SetBinContent(1,uFlow)
125 hTree.SetBinContent(hTree.GetNbinsX(),oFlow)
126 hTree.SetBinError(1,uFlowErr)
127 hTree.SetBinError(hTree.GetNbinsX(),oFlowErr)
128
129
130 print '\t-->import %s\t Integral: %s'%(job.name,hTree.Integral())
131
132 hTree.SetDirectory(0)
133 input.Close()
134
135 return hTree, group
136
137
138 ######################
139
140
141
142 def orderandadd(histos,typs,setup):
143 #ORDER AND ADD TOGETHER
144
145 ordnung=[]
146 ordnungtyp=[]
147 num=[0]*len(setup)
148 for i in range(0,len(setup)):
149 for j in range(0,len(histos)):
150 if typs[j] in setup[i]:
151 num[i]+=1
152 ordnung.append(histos[j])
153 ordnungtyp.append(typs[j])
154
155 del histos
156 del typs
157
158 histos=ordnung
159 typs=ordnungtyp
160
161 print typs
162
163 for k in range(0,len(num)):
164 for m in range(0,num[k]):
165 if m > 0:
166
167 #add
168 histos[k].Add(histos[k+1],1)
169 printc('magenta','','\t--> added %s to %s'%(typs[k],typs[k+1]))
170 del histos[k+1]
171 del typs[k+1]
172
173 del histos[len(setup):]
174 del typs[len(setup):]
175
176 return histos, typs
177
178