ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/addingSamples.py
Revision: 1.1
Committed: Wed Oct 17 16:12:59 2012 UTC (12 years, 6 months ago) by bortigno
Content type: text/x-python
Branch: MAIN
CVS Tags: hcpApproval, HCP_unblinding, hcpPreApp
Log Message:
add lheWeight information to DYJets(inclusive,Zpt,NJets) samples.

File Contents

# User Rev Content
1 bortigno 1.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_'
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+'DY4JetsToLL_M-50_TuneZ2Star_8TeV-madgraph.root' , 51.1, j3 ] , # not ready in 53X
48     [prefix+'DY4JetsToLL_M-50_TuneZ2Star_8TeV-madgraph.root' , 23.04, j4 ] ]
49    
50     #look here https://www.evernote.com/shard/s186/sh/8ffc289c-ede2-4e09-83ba-1e1981f13617/4d5aac2f42a9fd480dc66f9303c1c217
51    
52     lheBin = ['lheV_pt < 50 & lheNj == 0',
53     'lheV_pt > 50 & lheV_pt < 70 & lheNj == 0',
54     'lheV_pt > 70 & lheV_pt < 100 & lheNj == 0',
55     'lheV_pt > 100 & lheNj == 0',
56    
57     'lheV_pt < 50 & lheNj == 1',
58     'lheV_pt > 50 & lheV_pt < 70 & lheNj == 1',
59     'lheV_pt > 70 & lheV_pt < 100 & lheNj == 1',
60     'lheV_pt > 100 & lheNj == 1',
61    
62     'lheV_pt < 50 & lheNj == 2',
63     'lheV_pt > 50 & lheV_pt < 70 & lheNj == 2',
64     'lheV_pt > 70 & lheV_pt < 100 & lheNj == 2',
65     'lheV_pt > 100 & lheNj == 2',
66    
67     'lheV_pt < 50 & lheNj == 3',
68     'lheV_pt > 50 & lheV_pt < 70 & lheNj == 3',
69     'lheV_pt > 70 & lheV_pt < 100 & lheNj == 3',
70     'lheV_pt > 100 & lheNj == 3',
71    
72     'lheV_pt < 50 & lheNj == 4',
73     'lheV_pt > 50 & lheV_pt < 70 & lheNj == 4',
74     'lheV_pt > 70 & lheV_pt < 100 & lheNj == 4',
75     'lheV_pt > 100 & lheNj == 4' ]
76    
77     eventList = {}
78     num = []
79     for file in fileList:
80     print file
81     infile = ROOT.TFile(file[0],"READ")
82     tree = getObj(infile, 'tree')
83     for bin in lheBin:
84     print bin
85     file[2].append( 1.*tree.GetEntries(bin) )
86     # file[2] = updateEventArray( tree, lheBin, file[2] )
87     count = getObj( infile, 'CountWithPU' )
88     file.append( count.GetBinContent(1) )
89     print fileList
90    
91     CountIncl = fileList[0][3]
92     print fileList
93    
94    
95     #print num[fileList[1]]
96    
97     #total -> total numer of events in each lheBin
98     print 'Calculating total'
99     total = []
100     weight= []
101     for bin in range(0, len(lheBin) ):
102     total.append(getTotal(bin, fileList))
103     print bin
104     #to better stich we need the highest stat (that should correspond to the binned sample relative to the bin)
105     fileList.sort( key=lambda file: file[2][bin], reverse=True )
106     print 'After sorting'
107     print fileList
108     if total[bin] > 0.:
109     #the first is always the one with the highest N in the bin:
110     weight.append( (fileList[0][1]/fileList[0][3]) * (CountIncl/2950.0) * fileList[0][2][bin]/total[bin] )
111     print weight[bin]
112     else:
113     weight.append(1.)
114    
115     print weight
116    
117     #now add the branch with the weight normalized to the inclusive
118     for file in fileList:
119     infile = ROOT.TFile(file[0],"READ")
120     outfile = ROOT.TFile('lheWeight.'+file[0],'RECREATE')
121    
122     infile.cd()
123     tree = infile.Get('tree')
124     outfile.cd()
125     newtree = tree.CloneTree(0)
126     lheWeight = array('f',[0])
127     newtree.Branch('lheWeight',lheWeight,'lheWeight/F')
128    
129     nEntries = tree.GetEntries()
130     for entry in range(0,nEntries):
131     tree.GetEntry(entry)
132    
133     if tree.lheV_pt < 50 and tree.lheNj == 0:
134     lheWeight[0] = weight[0]
135     elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 0:
136     lheWeight[0] = weight[1]
137     elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 0:
138     lheWeight[0] = weight[2]
139     elif tree.lheV_pt > 100 and tree.lheNj == 0:
140     lheWeight[0] = weight[3]
141    
142     elif tree.lheV_pt < 50 and tree.lheNj == 1:
143     lheWeight[0] = weight[4]
144     elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 1:
145     lheWeight[0] = weight[5]
146     elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 1:
147     lheWeight[0] = weight[6]
148     elif tree.lheV_pt > 100 and tree.lheNj == 1:
149     lheWeight[0] = weight[7]
150    
151     elif tree.lheV_pt < 50 and tree.lheNj == 2:
152     lheWeight[0] = weight[8]
153     elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 2:
154     lheWeight[0] = weight[9]
155     elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 2:
156     lheWeight[0] = weight[10]
157     elif tree.lheV_pt > 100 and tree.lheNj == 2:
158     lheWeight[0] = weight[11]
159    
160     elif tree.lheV_pt < 50 and tree.lheNj == 3:
161     lheWeight[0] = weight[12]
162     elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 3:
163     lheWeight[0] = weight[13]
164     elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 3:
165     lheWeight[0] = weight[14]
166     elif tree.lheV_pt > 100 and tree.lheNj == 3:
167     lheWeight[0] = weight[15]
168    
169     elif tree.lheV_pt < 50 and tree.lheNj == 4:
170     lheWeight[0] = weight[16]
171     elif tree.lheV_pt > 50 and tree.lheV_pt < 70 and tree.lheNj == 4:
172     lheWeight[0] = weight[17]
173     elif tree.lheV_pt > 70 and tree.lheV_pt < 100 and tree.lheNj == 4:
174     lheWeight[0] = weight[18]
175     elif tree.lheV_pt > 100 and tree.lheNj == 4:
176     lheWeight[0] = weight[19]
177    
178     else:
179     lheWeight[0] = 1.
180    
181    
182     newtree.Fill()
183    
184     newtree.AutoSave()
185     outfile.Write()
186     outfile.Close()
187