ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/addingSamples.py
Revision: 1.2
Committed: Fri Nov 23 14:28:41 2012 UTC (12 years, 5 months ago) by bortigno
Content type: text/x-python
Branch: MAIN
Changes since 1.1: +214 -47 lines
Log Message:
HT bins and histo added

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 pt100 = []
37 ht200400 = []
38 ht400 = []
39
40 prefix='DiJetPt_noweight_'
41 fileList = [ [prefix+'DYJetsToLL_M-50_TuneZ2Star_8TeV-madgraph-tarball.root' , 2950.0, inc ] ,
42 [prefix+'DYJetsToLL_PtZ-50To70_TuneZ2star_8TeV-madgraph-tarball.root' , 93.8, pt5070 ],
43 [prefix+'DYJetsToLL_PtZ-70To100_TuneZ2star_8TeV-madgraph-tarball.root' , 52.31, pt70100 ],
44 [prefix+'DYJetsToLL_PtZ-100_TuneZ2star_8TeV-madgraph.root' , 34.1, pt100 ],
45 [prefix+'DY1JetsToLL_M-50_TuneZ2Star_8TeV-madgraph.root' , 561.0, j1 ] ,
46 [prefix+'DY2JetsToLL_M-50_TuneZ2Star_8TeV-madgraph.root' , 181.0, j2 ] ,
47 [prefix+'DY3JetsToLL_M-50_TuneZ2Star_8TeV-madgraph.root' , 51.1, j3 ] ,
48 [prefix+'DY4JetsToLL_M-50_TuneZ2Star_8TeV-madgraph.root' , 23.04, j4 ] ,
49 [prefix+'DYJetsToLL_HT-200To400_TuneZ2Star_8TeV-madgraph.root' , 19.73, ht200400 ],
50 [prefix+'DYJetsToLL_HT-400ToInf_TuneZ2Star_8TeV-madgraph.root' , 2.826, ht400 ] ]
51
52 #look here https://www.evernote.com/shard/s186/sh/8ffc289c-ede2-4e09-83ba-1e1981f13617/4d5aac2f42a9fd480dc66f9303c1c217
53
54 lheBin = [
55
56 'lheV_pt < 50 & lheNj == 0 & lheHT < 200',
57 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 0 & lheHT < 200',
58 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 0 & lheHT < 200',
59 'lheV_pt > 100 & lheNj == 0 & lheHT < 200',
60
61 'lheV_pt < 50 & lheNj == 1 & lheHT < 200',
62 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 1 & lheHT < 200',
63 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 1 & lheHT < 200',
64 'lheV_pt > 100 & lheNj == 1 & lheHT < 200',
65
66 'lheV_pt < 50 & lheNj == 2 & lheHT < 200',
67 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 2 & lheHT < 200',
68 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 2 & lheHT < 200',
69 'lheV_pt > 100 & lheNj == 2 & lheHT < 200',
70
71 'lheV_pt < 50 & lheNj == 3 & lheHT < 200',
72 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 3 & lheHT < 200',
73 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 3 & lheHT < 200',
74 'lheV_pt > 100 & lheNj == 3 & lheHT < 200',
75
76 'lheV_pt < 50 & lheNj == 4 & lheHT < 200',
77 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 4 & lheHT < 200',
78 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 4 & lheHT < 200',
79 'lheV_pt > 100 & lheNj == 4 & lheHT < 200',
80
81
82 'lheV_pt < 50 & lheNj == 0 & lheHT > 200 & lheHT < 400',
83 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 0 & lheHT > 200 & lheHT < 400',
84 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 0 & lheHT > 200 & lheHT < 400',
85 'lheV_pt > 100 & lheNj == 0 & lheHT > 200 & lheHT < 400',
86
87 'lheV_pt < 50 & lheNj == 1 & lheHT > 200 & lheHT < 400',
88 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 1 & lheHT > 200 & lheHT < 400',
89 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 1 & lheHT > 200 & lheHT < 400',
90 'lheV_pt > 100 & lheNj == 1 & lheHT > 200 & lheHT < 400',
91
92 'lheV_pt < 50 & lheNj == 2 & lheHT > 200 & lheHT < 400',
93 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 2 & lheHT > 200 & lheHT < 400',
94 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 2 & lheHT > 200 & lheHT < 400',
95 'lheV_pt > 100 & lheNj == 2 & lheHT > 200 & lheHT < 400',
96
97 'lheV_pt < 50 & lheNj == 3 & lheHT > 200 & lheHT < 400',
98 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 3 & lheHT > 200 & lheHT < 400',
99 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 3 & lheHT > 200 & lheHT < 400',
100 'lheV_pt > 100 & lheNj == 3 & lheHT > 200 & lheHT < 400',
101
102 'lheV_pt < 50 & lheNj == 4 & lheHT > 200 & lheHT < 400',
103 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 4 & lheHT > 200 & lheHT < 400',
104 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 4 & lheHT > 200 & lheHT < 400',
105 'lheV_pt > 100 & lheNj == 4 & lheHT > 200 & lheHT < 400',
106
107
108 'lheV_pt < 50 & lheNj == 0 & lheHT > 400',
109 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 0 & lheHT > 400',
110 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 0 & lheHT > 400',
111 'lheV_pt > 100 & lheNj == 0 & lheHT > 400',
112
113 'lheV_pt < 50 & lheNj == 1 & lheHT > 400',
114 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 1 & lheHT > 400',
115 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 1 & lheHT > 400',
116 'lheV_pt > 100 & lheNj == 1 & lheHT > 400',
117
118 'lheV_pt < 50 & lheNj == 2 & lheHT > 400',
119 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 2 & lheHT > 400',
120 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 2 & lheHT > 400',
121 'lheV_pt > 100 & lheNj == 2 & lheHT > 400',
122
123 'lheV_pt < 50 & lheNj == 3 & lheHT > 400',
124 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 3 & lheHT > 400',
125 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 3 & lheHT > 400',
126 'lheV_pt > 100 & lheNj == 3 & lheHT > 400',
127
128 'lheV_pt < 50 & lheNj == 4 & lheHT > 400',
129 'lheV_pt > 50 & lheV_pt < 70 & lheNj == 4 & lheHT > 400',
130 'lheV_pt > 70 & lheV_pt < 100 & lheNj == 4 & lheHT > 400',
131 'lheV_pt > 100 & lheNj == 4 & lheHT > 400'
132
133
134 ]
135
136 eventList = {}
137 num = []
138 for file in fileList:
139 print file
140 infile = ROOT.TFile(file[0],"READ")
141 tree = getObj(infile, 'tree')
142 for bin in lheBin:
143 print bin
144 file[2].append( 1.*tree.GetEntries(bin) )
145 # file[2] = updateEventArray( tree, lheBin, file[2] )
146 count = getObj( infile, 'CountWithPU' )
147 file.append( count.GetBinContent(1) )
148 print fileList
149
150 CountIncl = fileList[0][3]
151 print fileList
152
153
154 #print num[fileList[1]]
155
156 #total -> total numer of events in each lheBin
157 print 'Calculating total'
158 total = []
159 weight= []
160 for bin in range(0, len(lheBin) ):
161 total.append(getTotal(bin, fileList))
162 print bin
163 #to better stich we need the highest stat (that should correspond to the binned sample relative to the bin)
164 fileList.sort( key=lambda file: file[2][bin], reverse=True )
165 print 'After sorting'
166 print fileList
167 if total[bin] > 0.:
168 #the first is always the one with the highest N in the bin:
169 weight.append( (fileList[0][1]/fileList[0][3]) * (CountIncl/2950.0) * fileList[0][2][bin]/total[bin] )
170 print weight[bin]
171 else:
172 weight.append(1.)
173
174 print weight
175
176 #now add the branch with the weight normalized to the inclusive
177 for file in fileList:
178 infile = ROOT.TFile(file[0],"READ")
179 outfile = ROOT.TFile('lheWeight.'+file[0],'RECREATE')
180 histoInfile = ROOT.TFile(prefix+'DYJetsToLL_M-50_TuneZ2Star_8TeV-madgraph-tarball.root',"READ")
181 histoInfile.cd()
182 obj = ROOT.TObject
183 for key in ROOT.gDirectory.GetListOfKeys():
184 histoInfile.cd()
185 obj = key.ReadObj()
186 print obj.GetName()
187 if obj.GetName() == 'tree':
188 continue
189 outfile.cd()
190 print key.GetName()
191 obj.Write(key.GetName())
192
193 infile.cd()
194 tree = infile.Get('tree')
195 outfile.cd()
196 newtree = tree.CloneTree(0)
197 lheWeight = array('f',[0])
198 newtree.Branch('lheWeight',lheWeight,'lheWeight/F')
199
200 nEntries = tree.GetEntries()
201 for entry in range(0,nEntries):
202 tree.GetEntry(entry)
203
204 if tree.lheV_pt < 50 and tree.lheNj == 0 and tree.lheHT < 200:
205 lheWeight[0] = weight[0]
206 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 0 and tree.lheHT < 200:
207 lheWeight[0] = weight[1]
208 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 0 and tree.lheHT < 200:
209 lheWeight[0] = weight[2]
210 elif tree.lheV_pt > 100 and tree.lheNj == 0 and tree.lheHT < 200:
211 lheWeight[0] = weight[3]
212
213 elif tree.lheV_pt < 50 and tree.lheNj == 1 and tree.lheHT < 200:
214 lheWeight[0] = weight[4]
215 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 1 and tree.lheHT < 200:
216 lheWeight[0] = weight[5]
217 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 1 and tree.lheHT < 200:
218 lheWeight[0] = weight[6]
219 elif tree.lheV_pt > 100 and tree.lheNj == 1 and tree.lheHT < 200:
220 lheWeight[0] = weight[7]
221
222 elif tree.lheV_pt < 50 and tree.lheNj == 2 and tree.lheHT < 200:
223 lheWeight[0] = weight[8]
224 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 2 and tree.lheHT < 200:
225 lheWeight[0] = weight[9]
226 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 2 and tree.lheHT < 200:
227 lheWeight[0] = weight[10]
228 elif tree.lheV_pt > 100 and tree.lheNj == 2 and tree.lheHT < 200:
229 lheWeight[0] = weight[11]
230
231 elif tree.lheV_pt < 50 and tree.lheNj == 3 and tree.lheHT < 200:
232 lheWeight[0] = weight[12]
233 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 3 and tree.lheHT < 200:
234 lheWeight[0] = weight[13]
235 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 3 and tree.lheHT < 200:
236 lheWeight[0] = weight[14]
237 elif tree.lheV_pt > 100 and tree.lheNj == 3 and tree.lheHT < 200:
238 lheWeight[0] = weight[15]
239
240 elif tree.lheV_pt < 50 and tree.lheNj == 4 and tree.lheHT < 200:
241 lheWeight[0] = weight[16]
242 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 4 and tree.lheHT < 200:
243 lheWeight[0] = weight[17]
244 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 4 and tree.lheHT < 200:
245 lheWeight[0] = weight[18]
246 elif tree.lheV_pt > 100 and tree.lheNj == 4 and tree.lheHT < 200:
247 lheWeight[0] = weight[19]
248
249
250
251
252 elif tree.lheV_pt < 50 and tree.lheNj == 0 and tree.lheHT > 200 and tree.lheHT < 400:
253 lheWeight[0] = weight[20]
254 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 0 and tree.lheHT > 200 and tree.lheHT < 400:
255 lheWeight[0] = weight[21]
256 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 0 and tree.lheHT > 200 and tree.lheHT < 400:
257 lheWeight[0] = weight[22]
258 elif tree.lheV_pt > 100 and tree.lheNj == 0 and tree.lheHT > 200 and tree.lheHT < 400:
259 lheWeight[0] = weight[23]
260
261 elif tree.lheV_pt < 50 and tree.lheNj == 1 and tree.lheHT > 200 and tree.lheHT < 400:
262 lheWeight[0] = weight[24]
263 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 1 and tree.lheHT > 200 and tree.lheHT < 400:
264 lheWeight[0] = weight[25]
265 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 1 and tree.lheHT > 200 and tree.lheHT < 400:
266 lheWeight[0] = weight[26]
267 elif tree.lheV_pt > 100 and tree.lheNj == 1 and tree.lheHT > 200 and tree.lheHT < 400:
268 lheWeight[0] = weight[27]
269
270 elif tree.lheV_pt < 50 and tree.lheNj == 2 and tree.lheHT > 200 and tree.lheHT < 400:
271 lheWeight[0] = weight[28]
272 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 2 and tree.lheHT > 200 and tree.lheHT < 400:
273 lheWeight[0] = weight[29]
274 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 2 and tree.lheHT > 200 and tree.lheHT < 400:
275 lheWeight[0] = weight[30]
276 elif tree.lheV_pt > 100 and tree.lheNj == 2 and tree.lheHT > 200 and tree.lheHT < 400:
277 lheWeight[0] = weight[31]
278
279 elif tree.lheV_pt < 50 and tree.lheNj == 3 and tree.lheHT > 200 and tree.lheHT < 400:
280 lheWeight[0] = weight[32]
281 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 3 and tree.lheHT > 200 and tree.lheHT < 400:
282 lheWeight[0] = weight[33]
283 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 3 and tree.lheHT > 200 and tree.lheHT < 400:
284 lheWeight[0] = weight[34]
285 elif tree.lheV_pt > 100 and tree.lheNj == 3 and tree.lheHT > 200 and tree.lheHT < 400:
286 lheWeight[0] = weight[35]
287
288 elif tree.lheV_pt < 50 and tree.lheNj == 4 and tree.lheHT > 200 and tree.lheHT < 400:
289 lheWeight[0] = weight[36]
290 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 4 and tree.lheHT > 200 and tree.lheHT < 400:
291 lheWeight[0] = weight[37]
292 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 4 and tree.lheHT > 200 and tree.lheHT < 400:
293 lheWeight[0] = weight[38]
294 elif tree.lheV_pt > 100 and tree.lheNj == 4 and tree.lheHT > 200 and tree.lheHT < 400:
295 lheWeight[0] = weight[39]
296
297
298
299 elif tree.lheV_pt < 50 and tree.lheNj == 0 and tree.lheHT > 400:
300 lheWeight[0] = weight[40]
301 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 0 and tree.lheHT > 400:
302 lheWeight[0] = weight[41]
303 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 0 and tree.lheHT > 400:
304 lheWeight[0] = weight[42]
305 elif tree.lheV_pt > 100 and tree.lheNj == 0 and tree.lheHT > 400:
306 lheWeight[0] = weight[43]
307
308 elif tree.lheV_pt < 50 and tree.lheNj == 1 and tree.lheHT > 400:
309 lheWeight[0] = weight[44]
310 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 1 and tree.lheHT > 400:
311 lheWeight[0] = weight[45]
312 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 1 and tree.lheHT > 400:
313 lheWeight[0] = weight[46]
314 elif tree.lheV_pt > 100 and tree.lheNj == 1 and tree.lheHT > 400:
315 lheWeight[0] = weight[47]
316
317 elif tree.lheV_pt < 50 and tree.lheNj == 2 and tree.lheHT > 400:
318 lheWeight[0] = weight[48]
319 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 2 and tree.lheHT > 400:
320 lheWeight[0] = weight[49]
321 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 2 and tree.lheHT > 400:
322 lheWeight[0] = weight[50]
323 elif tree.lheV_pt > 100 and tree.lheNj == 2 and tree.lheHT > 400:
324 lheWeight[0] = weight[51]
325
326 elif tree.lheV_pt < 50 and tree.lheNj == 3 and tree.lheHT > 400:
327 lheWeight[0] = weight[52]
328 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 3 and tree.lheHT > 400:
329 lheWeight[0] = weight[53]
330 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 3 and tree.lheHT > 400:
331 lheWeight[0] = weight[54]
332 elif tree.lheV_pt > 100 and tree.lheNj == 3 and tree.lheHT > 400:
333 lheWeight[0] = weight[55]
334
335 elif tree.lheV_pt < 50 and tree.lheNj == 4 and tree.lheHT > 400:
336 lheWeight[0] = weight[56]
337 elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 4 and tree.lheHT > 400:
338 lheWeight[0] = weight[57]
339 elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 4 and tree.lheHT > 400:
340 lheWeight[0] = weight[58]
341 elif tree.lheV_pt > 100 and tree.lheNj == 4 and tree.lheHT > 400:
342 lheWeight[0] = weight[59]
343
344
345 else:
346 lheWeight[0] = 1.
347
348
349 newtree.Fill()
350
351 newtree.AutoSave()
352 outfile.Write()
353 outfile.Close()
354