ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/gethistofromtree.py
Revision: 1.21
Committed: Wed Dec 5 15:00:20 2012 UTC (12 years, 5 months ago) by peller
Content type: text/x-python
Branch: MAIN
Changes since 1.20: +4 -4 lines
Log Message:
individual BKGd trainings

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