ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/addingSamples.py
Revision: 1.3
Committed: Fri Jan 11 09:28:34 2013 UTC (12 years, 4 months ago) by nmohr
Content type: text/x-python
Branch: MAIN
CVS Tags: workingVersionAfterHCP
Changes since 1.2: +47 -159 lines
Log Message:
Adaption for new samples

File Contents

# Content
1 #!/afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_gcc434/bin/python2.6
2
3 import ROOT
4 from array import array
5
6
7 def getObj( infile, name ):
8 infile.cd()
9 for key in ROOT.gDirectory.GetListOfKeys():
10 obj = key.ReadObj()
11 print obj.GetName()
12 if obj.GetName() == name:
13 return obj;
14
15
16 def updateEventArray( tree, lheBin, N ):
17 for bin in lheBin:
18 print bin
19 N.append( (bin, 1.*tree.GetEntries(bin) ) )
20 return N
21
22 def getTotal( bin, fileList ):
23 total = 0.
24 for i in range(0,len(fileList)):
25 total = total + fileList[i][2][bin]
26 #print total[bin]
27 return total
28
29 inc = []
30 j1 = []
31 j2 = []
32 j3 = []
33 j4 = []
34 pt5070 = []
35 pt70100 = []
36 pt100180 = []
37 pt180 = []
38 ht200400 = []
39 ht400 = []
40
41 prefix='DiJetPt_noweight_'
42 fileList = [ [prefix+'DYJetsToLL_M-50_TuneZ2Star_8TeV-madgraph-tarball.root' , 2950.0, inc ] ,
43 [prefix+'DYJetsToLL_PtZ-50To70_TuneZ2star_8TeV-madgraph-tarball.root' , 93.8, pt5070 ],
44 [prefix+'DYJetsToLL_PtZ-70To100_TuneZ2star_8TeV-madgraph-tarball.root' , 52.31, pt70100 ],
45 [prefix+'DYJetsToLL_PtZ-100_TuneZ2star_8TeV-madgraph.root' , 34.1, pt100180 ],
46 [prefix+'DYJetsToLL_PtZ-180_TuneZ2star_8TeV-madgraph.root' , 4.56, pt180 ],
47 [prefix+'DY1JetsToLL_M-50_TuneZ2Star_8TeV-madgraph.root' , 561.0, j1 ] ,
48 [prefix+'DY2JetsToLL_M-50_TuneZ2Star_8TeV-madgraph.root' , 181.0, j2 ] ,
49 [prefix+'DY3JetsToLL_M-50_TuneZ2Star_8TeV-madgraph.root' , 51.1, j3 ] ,
50 [prefix+'DY4JetsToLL_M-50_TuneZ2Star_8TeV-madgraph.root' , 23.04, j4 ] ,
51 [prefix+'DYJetsToLL_HT-200To400_TuneZ2Star_8TeV-madgraph.root' , 19.73, ht200400 ],
52 [prefix+'DYJetsToLL_HT-400ToInf_TuneZ2Star_8TeV-madgraph.root' , 2.826, ht400 ] ]
53
54 #look here https://www.evernote.com/shard/s186/sh/8ffc289c-ede2-4e09-83ba-1e1981f13617/4d5aac2f42a9fd480dc66f9303c1c217
55
56 lheBin = [
57
58 'lheV_pt < 50 & lheNj == 0 & lheHT < 200',
59 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 0 & lheHT < 200',
60 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 0 & lheHT < 200',
61 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 0 & lheHT < 200',
62 'lheV_pt > 180 & lheNj == 0 & lheHT < 200',
63
64 'lheV_pt < 50 & lheNj == 1 & lheHT < 200',
65 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 1 & lheHT < 200',
66 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 1 & lheHT < 200',
67 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 1 & lheHT < 200',
68 'lheV_pt > 180 & lheNj == 1 & lheHT < 200',
69
70 'lheV_pt < 50 & lheNj == 2 & lheHT < 200',
71 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 2 & lheHT < 200',
72 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 2 & lheHT < 200',
73 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 2 & lheHT < 200',
74 'lheV_pt > 180 & lheNj == 2 & lheHT < 200',
75
76 'lheV_pt < 50 & lheNj == 3 & lheHT < 200',
77 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 3 & lheHT < 200',
78 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 3 & lheHT < 200',
79 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 3 & lheHT < 200',
80 'lheV_pt > 180 & lheNj == 3 & lheHT < 200',
81
82 'lheV_pt < 50 & lheNj == 4 & lheHT < 200',
83 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 4 & lheHT < 200',
84 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 4 & lheHT < 200',
85 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 4 & lheHT < 200',
86 'lheV_pt > 180 & lheNj == 4 & lheHT < 200',
87
88
89 'lheV_pt < 50 & lheNj == 0 & lheHT > 200 & lheHT < 400',
90 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 0 & lheHT > 200 & lheHT < 400',
91 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 0 & lheHT > 200 & lheHT < 400',
92 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 0 & lheHT > 200 & lheHT < 400',
93 'lheV_pt > 180 & lheNj == 0 & lheHT > 200 & lheHT < 400',
94
95 'lheV_pt < 50 & lheNj == 1 & lheHT > 200 & lheHT < 400',
96 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 1 & lheHT > 200 & lheHT < 400',
97 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 1 & lheHT > 200 & lheHT < 400',
98 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 1 & lheHT > 200 & lheHT < 400',
99 'lheV_pt > 180 & lheNj == 1 & lheHT > 200 & lheHT < 400',
100
101 'lheV_pt < 50 & lheNj == 2 & lheHT > 200 & lheHT < 400',
102 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 2 & lheHT > 200 & lheHT < 400',
103 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 2 & lheHT > 200 & lheHT < 400',
104 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 2 & lheHT > 200 & lheHT < 400',
105 'lheV_pt > 180 & lheNj == 2 & lheHT > 200 & lheHT < 400',
106
107 'lheV_pt < 50 & lheNj == 3 & lheHT > 200 & lheHT < 400',
108 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 3 & lheHT > 200 & lheHT < 400',
109 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 3 & lheHT > 200 & lheHT < 400',
110 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 3 & lheHT > 200 & lheHT < 400',
111 'lheV_pt > 180 & lheNj == 3 & lheHT > 200 & lheHT < 400',
112
113 'lheV_pt < 50 & lheNj == 4 & lheHT > 200 & lheHT < 400',
114 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 4 & lheHT > 200 & lheHT < 400',
115 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 4 & lheHT > 200 & lheHT < 400',
116 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 4 & lheHT > 200 & lheHT < 400',
117 'lheV_pt > 180 & lheNj == 4 & lheHT > 200 & lheHT < 400',
118
119
120 'lheV_pt < 50 & lheNj == 0 & lheHT > 400',
121 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 0 & lheHT > 400',
122 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 0 & lheHT > 400',
123 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 0 & lheHT > 400',
124 'lheV_pt > 180 & lheNj == 0 & lheHT > 400',
125
126 'lheV_pt < 50 & lheNj == 1 & lheHT > 400',
127 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 1 & lheHT > 400',
128 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 1 & lheHT > 400',
129 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 1 & lheHT > 400',
130 'lheV_pt > 180 & lheNj == 1 & lheHT > 400',
131
132 'lheV_pt < 50 & lheNj == 2 & lheHT > 400',
133 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 2 & lheHT > 400',
134 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 2 & lheHT > 400',
135 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 2 & lheHT > 400',
136 'lheV_pt > 180 & lheNj == 2 & lheHT > 400',
137
138 'lheV_pt < 50 & lheNj == 3 & lheHT > 400',
139 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 3 & lheHT > 400',
140 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 3 & lheHT > 400',
141 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 3 & lheHT > 400',
142 'lheV_pt > 180 & lheNj == 3 & lheHT > 400',
143
144 'lheV_pt < 50 & lheNj == 4 & lheHT > 400',
145 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 4 & lheHT > 400',
146 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 4 & lheHT > 400',
147 'lheV_pt > 100 & lheV_pt < 180 & lheNj == 4 & lheHT > 400',
148 'lheV_pt > 180 & lheNj == 4 & lheHT > 400'
149
150
151 ]
152
153 eventList = {}
154 num = []
155 for file in fileList:
156 #file.append( 1 )
157 #continue
158 print file
159 infile = ROOT.TFile(file[0],"READ")
160 tree = getObj(infile, 'tree')
161 for bin in lheBin:
162 print bin
163 file[2].append( 1.*tree.GetEntries(bin) )
164 # file[2] = updateEventArray( tree, lheBin, file[2] )
165 count = getObj( infile, 'CountWithPU' )
166 file.append( count.GetBinContent(1) )
167 print fileList
168
169 CountIncl = fileList[0][3]
170 print fileList
171
172
173 #print num[fileList[1]]
174
175 #total -> total numer of events in each lheBin
176 print 'Calculating total'
177 total = []
178 weight= []
179 for bin in range(0, len(lheBin) ):
180 #weight.append(1.)
181 #continue
182 total.append(getTotal(bin, fileList))
183 print bin
184 #to better stich we need the highest stat (that should correspond to the binned sample relative to the bin)
185 fileList.sort( key=lambda file: file[2][bin], reverse=True )
186 print 'After sorting'
187 print fileList
188 if total[bin] > 0.:
189 #the first is always the one with the highest N in the bin:
190 weight.append( (fileList[0][1]/fileList[0][3]) * (CountIncl/2950.0) * fileList[0][2][bin]/total[bin] )
191 print weight[bin]
192 else:
193 weight.append(1.)
194
195 print weight
196
197 #now add the branch with the weight normalized to the inclusive
198 for file in fileList:
199 infile = ROOT.TFile(file[0],"READ")
200 outfile = ROOT.TFile('lheWeight.'+file[0],'RECREATE')
201 histoInfile = ROOT.TFile(prefix+'DYJetsToLL_M-50_TuneZ2Star_8TeV-madgraph-tarball.root',"READ")
202 histoInfile.cd()
203 obj = ROOT.TObject
204 for key in ROOT.gDirectory.GetListOfKeys():
205 histoInfile.cd()
206 obj = key.ReadObj()
207 print obj.GetName()
208 if obj.GetName() == 'tree':
209 continue
210 outfile.cd()
211 print key.GetName()
212 obj.Write(key.GetName())
213
214 infile.cd()
215 tree = infile.Get('tree')
216 outfile.cd()
217 newtree = tree.CloneTree(0)
218 lheWeight = array('f',[0])
219 newtree.Branch('lheWeight',lheWeight,'lheWeight/F')
220
221 nEntries = tree.GetEntries()
222 theBinForms = []
223 for bin in range(0, len(lheBin) ):
224 theBinForms.append(ROOT.TTreeFormula("Bin_formula_%s"%(bin),lheBin[bin],tree))
225 for entry in range(0,nEntries):
226 tree.GetEntry(entry)
227 i = -1
228 for bin in range(0, len(lheBin) ):
229 if theBinForms[bin].EvalInstance() > 0.:
230 i = bin
231 if i > -1:
232 lheWeight[0] = weight[i]
233 else:
234 lheWeight[0] = 1.
235
236
237 newtree.Fill()
238
239 newtree.AutoSave()
240 outfile.Write()
241 outfile.Close()
242