ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/VHbb/python/addingSamples.py
Revision: 1.4
Committed: Fri Mar 1 09:47:11 2013 UTC (12 years, 2 months ago) by nmohr
Content type: text/x-python
Branch: MAIN
Changes since 1.3: +149 -222 lines
Log Message:
New lheWeights

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 sys,ROOT
4 ROOT.gROOT.SetBatch(True)
5 from array import array
6 from optparse import OptionParser
7
8 def getTotal( bin, fileList ):
9 total = 0.
10 for i in range(0,len(fileList)):
11 total = total + fileList[i][2][bin]
12 return total
13
14
15 argv = sys.argv
16 parser = OptionParser()
17
18 parser.add_option("-C", "--config", dest="config", default=[], action="append",
19 help="configuration defining the plots to make")
20 (opts, args) = parser.parse_args(argv)
21 if opts.config =="":
22 opts.config = "config"
23
24 from myutils import BetterConfigParser
25
26 print opts.config
27 config = BetterConfigParser()
28 config.read(opts.config)
29 prefix = config.get('LHEWeights','prefix')
30 newprefix = config.get('LHEWeights','newprefix')
31 inclusive = config.get('LHEWeights','inclusive')
32 files = eval(config.get('LHEWeights','fileList'))
33 lheBin = eval(config.get('LHEWeights','lheBin'))
34 fileList = []
35 for file in files:
36 new_entry = [prefix+file,files[file],[]]
37 if file == inclusive:
38 fileList.insert(0, new_entry)
39 else:
40 fileList.append(new_entry)
41 print fileList
42 print lheBin
43
44 def get_weights(fileList,lheBin):
45 for file in fileList:
46 #file.append( 1 )
47 #continue
48 print file
49 infile = ROOT.TFile(file[0],"READ")
50 tree = infile.Get('tree')
51 for bin in lheBin:
52 file[2].append( float(tree.GetEntries(bin)) )
53 count = infile.Get('CountWithPU')
54 file.append( count.GetBinContent(1) )
55 infile.Close()
56 del tree
57
58 CountIncl = fileList[0][3]
59 print fileList
60
61 #total -> total numer of events in each lheBin
62 print 'Calculating total'
63 total = []
64 weight= []
65 for bin in range(0, len(lheBin) ):
66 #weight.append(1.)
67 #continue
68 total.append(getTotal(bin, fileList))
69 #print lheBin[bin]
70 #to better stich we need the highest stat (that should correspond to the binned sample relative to the bin)
71 fileList.sort( key=lambda file: file[2][bin], reverse=True )
72 #print 'After sorting'
73 #print fileList
74 if total[bin] > 0.:
75 #the first is always the one with the highest N in the bin:
76 weight.append( (fileList[0][1]/fileList[0][3]) * (CountIncl/2950.0) * fileList[0][2][bin]/total[bin] )
77 else:
78 weight.append(1.)
79
80 weight_map = {}
81 for bin in range(0, len(lheBin) ):
82 print '%s: %.2f' %(lheBin[bin], weight[bin])
83 weight_map[lheBin[bin]] = weight[bin]
84 return weight_map
85
86
87 def apply_weights(fileList,weight_map,inclusive,newprefix):
88 #now add the branch with the weight normalized to the inclusive
89 for file in fileList:
90 outfile = ROOT.TFile(newprefix+file[0],'RECREATE')
91 infile = ROOT.TFile(file[0],"READ")
92 histoInfile = ROOT.TFile(inclusive,"READ")
93 histoInfile.cd()
94 obj = ROOT.TObject
95 for key in ROOT.gDirectory.GetListOfKeys():
96 histoInfile.cd()
97 obj = key.ReadObj()
98 #print obj.GetName()
99 if obj.GetName() == 'tree':
100 continue
101 outfile.cd()
102 #print key.GetName()
103 obj.Write(key.GetName())
104
105 infile.cd()
106 tree = infile.Get('tree')
107 outfile.cd()
108 newtree = tree.CloneTree(0)
109 lheWeight = array('f',[0])
110 newtree.Branch('lheWeight',lheWeight,'lheWeight/F')
111
112 nEntries = tree.GetEntries()
113 theBinForms = {}
114 for bin in weight_map:
115 theBinForms[bin] = ROOT.TTreeFormula("Bin_formula_%s"%(bin),bin,tree)
116 for entry in range(0,nEntries):
117 tree.GetEntry(entry)
118 match_bin = None
119 for bin in weight_map:
120 if theBinForms[bin].EvalInstance() > 0.:
121 match_bin = bin
122 if match_bin:
123 lheWeight[0] = weight_map[match_bin]
124 else:
125 lheWeight[0] = 1.
126 newtree.Fill()
127
128 newtree.AutoSave()
129 outfile.Write()
130 outfile.Close()
131 infile.Close()
132 del tree
133 del newtree
134
135 def do_validation(fileList,inclusive,newprefix):
136 histo = ROOT.TH1F("lheV_pt","lheV_pt",300,0,300)
137 histo1 = ROOT.TH1F("lheV_ptInc","lheV_ptInc",300,0,300)
138
139 for file in fileList:
140 h_name=((newprefix+file[0]).split("."))[-2]
141 print h_name
142 tfile = ROOT.TFile.Open(newprefix+file[0],"read")
143 tree = tfile.Get("tree")
144 h_tmp = ROOT.TH1F(h_name,h_name,300,0,300)
145 ROOT.gDirectory.ls()
146 tree.Draw("lheV_pt>>"+str(h_name),"(lheWeight)*(lheV_pt < 300.)","goff")
147 if inclusive in file[0]:
148 h_tmp1 = ROOT.TH1F(h_name+'Inc',h_name+'Inc',300,0,300)
149 tree.Draw("lheV_pt>>"+str(h_name+'Inc'),"(lheV_pt < 300.)","goff")
150 histo1.Add(h_tmp1)
151
152 histo.Add(h_tmp)
153
154 canvas = ROOT.TCanvas("lheV_pt","lheV_pt",600,600)
155 ROOT.gPad.SetLogy()
156 histo.Draw()
157 histo1.Draw('SAME')
158 print histo.Integral(180,300)
159 print histo1.Integral(180,300)
160 histo1.SetLineColor(ROOT.kRed)
161 canvas.Print("validation_lheV_pt.pdf","pdf")
162
163 weight_map = get_weights(fileList,lheBin)
164 config.set('LHEWeights', 'weights_per_bin', '%s' %weight_map)
165 f = open('lheWeights', 'w')
166 config.write(f)
167 f.close()
168 apply_weights(fileList,weight_map,prefix+inclusive,newprefix)
169 do_validation(fileList,inclusive,newprefix)