ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/algomez/FourTop/TreeAnalyzer/test/mkCutFlowTables.py
Revision: 1.1
Committed: Thu Jan 26 17:42:36 2012 UTC (13 years, 3 months ago) by algomez
Content type: text/x-python
Branch: MAIN
Log Message:
calculate cutflow and write a latex table

File Contents

# User Rev Content
1 algomez 1.1 #! /usr/bin/env python
2    
3     from ROOT import *
4     from decimal import Decimal
5     import sys,os, math
6    
7     path = "./"
8     Ndecimals = 2
9     IsMC = False
10     Lumi = 10.93 # in pb-1
11     JetType = "PF" #calo
12     showGh = False
13     kFactor = 1.2
14     WprimeType = "RH"
15     splitWjets = False
16    
17     if len(sys.argv) < 2:
18     print "usage: mkCutFlowTables.py <path to files> <Lumi for MC> < 1 =will produce table for signal Gh>"
19     sys.exit()
20    
21     if len(sys.argv) > 1:
22    
23     path = sys.argv[1]
24     print " path: "+ path
25     if len(sys.argv)>2:
26     IsMC = True
27     Lumi = float(sys.argv[2])
28     print " Luminosity for MC samples: "+str(Lumi)
29    
30     if len(sys.argv)>3:
31    
32     if sys.argv[3] == "Wbb":
33     splitWjets = True
34     else:
35     showGh = True
36     GhType = sys.argv[3]
37     print "checking Gh files of type: " + GhType
38    
39     Nevents = {}
40     xsec = {} # xsection in pb
41     file = {}
42     label = {}
43     # MC files
44     if IsMC:
45     file['ttbar'] = path+'/preresults_ttbar.root'
46     file['QCD'] = path+'/preresults_QCD.root'
47     if splitWjets:
48     file['Wbb'] = path+'/preresults_Wbb.root'
49     file['Wcc']= path+'/preresults_Wcc.root'
50     file['Wqq']= path+'/preresults_Wqq.root'
51     else:
52     file['Wjets'] = path+'/preresults_WJets.root'
53     file['Zjets'] = path+'/preresults_ZJets.root'
54     file['tch'] = path+'/preresults_STtch.root'
55     file['tWch'] = path+'/preresults_STtWch.root'
56     file['sch'] = path+'/preresults_STsch.root'
57     file['tch_bar'] = path+'/preresults_STtch_bar.root'
58     file['tWch_bar'] = path+'/preresults_STtWch_bar.root'
59     file['sch_bar'] = path+'/preresults_STsch_bar.root'
60     file['WW'] = path+'/preresults_WW.root'
61     file['WZ'] = path+'/preresults_WZ.root'
62    
63     file['Gh500'] = path+'/preresults_4Top_500.root'
64     file['Gh1000'] = path+'/preresults_4Top_1000.root'
65    
66     xsec['ttbar'] = 163.0 #157.5
67     xsec['QCD'] = 84679.3
68     xsec['Wjets'] = 31314.0
69    
70     xsec['Wbb'] = 31314.0 * 1.21
71     xsec['Wcc'] = 31314.0 * 1.66
72     xsec['Wqq'] = 31314.0 #* 0.58
73    
74     xsec['Zjets'] = 3048.0
75     xsec['tch'] = 41.92
76     xsec['tWch'] = 7.87
77     xsec['sch'] = 3.19
78     xsec['tch_bar'] = 22.65
79     xsec['tWch_bar'] = 7.87
80     xsec['sch_bar'] = 1.44
81     xsec['WW'] = 43.0
82     xsec['WZ'] = 18.0
83     xsec['Gh500'] = 0.00025442*kFactor
84     xsec['Gh1000'] = 0.0000098699*kFactor
85    
86     Nevents['ttbar'] = 3701947.0
87     Nevents['Wjets'] = 77105816.0
88    
89     Nevents['Wbb'] = 77105816.0
90     Nevents['Wcc'] = 77105816.0
91     Nevents['Wqq'] = 77105816.0
92    
93     Nevents['Zjets'] = 36277961.0
94     Nevents['WW'] = 4225916.0
95     Nevents['WZ'] = 4265243.0
96     Nevents['QCD'] = 25080241.0
97     Nevents['sch'] = 259971.0
98     Nevents['sch_bar'] = 137980.0
99     Nevents['tch'] = 3900171.0
100     Nevents['tch_bar'] = 1944826.0
101     Nevents['tWch'] = 814390.0
102     Nevents['tWch_bar'] = 809984.0
103    
104     Nevents['Gh500'] = 3368.0
105     Nevents['Gh1000'] = 3977.0
106    
107     label['ttbar'] = '$t\\overline{t}$'
108     label['QCD'] = 'QCD'
109     label['Wjets'] = '$W\\rightarrow l\\nu$'
110    
111     label['Wbb'] = '$Wbb$'
112     label['Wcc'] = '$Wcc$'
113     label['Wqq'] = '$Wqq$'
114    
115     label['Zjets'] = '$Z\\rightarrow l^{+}l^{-}$'
116     label['tch'] = 'top t-ch'
117     label['tWch'] = 'top tW-ch'
118     label['sch'] = 'top s-ch'
119     label['tch_bar'] = 'anti-top t-ch'
120     label['tWch_bar'] = 'anti-top tW-ch'
121     label['sch_bar'] = 'anti-top s-ch'
122     label['WW'] = 'WW'
123     label['WZ'] = 'WZ'
124    
125     label['Gh500'] = "Gh 0.5 TeV"
126     label['Gh1000'] = "Gh 1.0 TeV"
127    
128     label['Total'] = 'Total MC'
129     else:
130     # data files
131     #file['Jun14'] = ''
132     #file['MB'] = '/uscmst1b_scratch/lpc1/cmsroc/yumiceva/top_prod_Sep22/Jun14/ttmuj_Jun14_MB_PSep23.root'#'/uscms_data/d3/ttmuj/Documents/NtupleMaker/Data/2.88pb-1/ttmuj_MB_Sep3.root'
133     #file['Jul16'] = '/uscmst1b_scratch/lpc1/cmsroc/yumiceva/top_prod_Sep22/Jul16/ttmuj_Jul16_PSep23.root'#'/uscms_data/d3/ttmuj/Documents/NtupleMaker/Data/2.88pb-1/ttmuj_Jul16_Sep3.root'
134     #file['Prompt']= '/uscms_data/d3/ttmuj/Documents/NtupleMaker/Data/2.88pb-1/ttmuj_Prompt_Sep3.root'
135    
136     #label['Jun14'] = 'Jun 14th Mu'
137     #label['MB'] = 'Jun 14th MinimumBias'
138     #label['Jul16'] = 'Jul 16th'
139     #label['Prompt'] = 'PromptReco'
140    
141     #if os.path.isfile(path+'/JPT/cutflow_JPT_data.txt'):
142     # file['dataJPT'] = path+'/cutflow_JPT_data.txt'
143     # label['dataJPT'] = 'JPT Ref. Sel.'
144     #if os.path.isfile(path+'/calo/cutflow_calo_data.txt'):
145     # file['datacalo'] = path+'/cutflow_calo_data.txt'
146     # label['datacalo'] = 'Calo Ref. Sel.'
147     #if os.path.isfile(path+'/PF/cutflow_PF_data.txt'):
148     # file['dataPF'] = path+'/cutflow_PF_data.txt'
149     # label['dataPF'] = 'PF Ref. Sel.'
150    
151     file['data'] = path+'/preresults_data.root';
152     label['data'] = "Data";
153    
154    
155     keys = file.keys()
156     cutflow = {}
157     cutflowerr = {}
158     cutlabel = {}
159     cutlabel['Processed'] = 'Processed'
160     cutlabel['CleanFilters'] = 'CleanFilters'
161     cutlabel['HLT'] = 'HLT'
162     cutlabel['GoodPV'] = 'Skim'
163     cutlabel['OneIsoMu'] = 'One Iso $\mu$'
164     cutlabel['LooseMuVeto'] = 'Loose $\mu$ veto'
165     cutlabel['ElectronVeto'] = 'Electron veto'
166     cutlabel['1Jet'] = 'jets $\geq 1$'
167     cutlabel['2Jet'] = 'jets $\geq 2$'
168     cutlabel['3Jet'] = 'jets $\geq 3$'
169     cutlabel['5Jet'] = 'jets $\geq 4$'
170     cutlabel['Ht'] = '$H_t$ > 300'
171     cutlabel['5Jet1b'] = 'btags $\geq 1$'
172     #cutlabel['2Jet1b'] = 'btags $\geq 1$'
173    
174     cutlabelvector = [ 'GoodPV', 'OneIsoMu', 'LooseMuVeto', 'ElectronVeto', '5Jet','Ht', '5Jet1b']
175     SKIPCUTS = []#'1Jet','3Jet','4Jet']
176    
177     allmap = {}
178     allmaperr = {}
179    
180     weightmap = {}
181    
182     tablelist = ['ttbar','Wjets','Zjets','QCD','tch','tch_bar','tWch','tWch_bar','sch','sch_bar','WW','WZ','Total']
183    
184     if splitWjets:
185     tablelist = ['ttbar','Wbb','Wcc','Wqq','Zjets','QCD','tch','tch_bar','tWch','tWch_bar','sch','sch_bar','WW','WZ','Total']
186    
187     if showGh:
188     tablelist = ['Gh500','Gh1000']
189    
190     if Lumi<=0:
191     tablelist = ['ttbar','Wjets','Zjets','QCD','tch','tch_bar','tWch','tWch_bar','sch','sch_bar','WW','WZ']
192    
193     if not IsMC:
194     tablelist = ['data']
195    
196     for sample in tablelist:
197    
198     if sample=="Total": continue
199     print " processing " + sample
200    
201     cutmap = {}
202     cutmaperr = {}
203     #txtfile = open(file[sample])
204     roofile = TFile(file[sample])
205     hcutflow = gDirectory.Get("cutflow")
206     print " entries in cutflow histogram: " + str(hcutflow.GetEntries())
207    
208     for ibin in xrange(1, hcutflow.GetNbinsX() +1 ):
209    
210     skipthiscut = False
211     for skipcut in SKIPCUTS:
212     if skipcut == cutlabelvector[ibin-1]: skipthiscut = True
213     if skipthiscut:
214     print "skip counting cut name: "+cutlabelvector[ibin-1]
215     continue
216     cutname = cutlabelvector[ibin-1]
217     acut = hcutflow.GetBinContent(ibin)
218     #print cutname
219     #print acut
220     #if sample=="data":
221     if sample.find("data")!=-1:
222     cutmap[ cutname ] = int(float(acut))
223     cutmaperr[ cutname ] = math.sqrt( cutmap[cutname] )
224     else:
225     cutmap[ cutname ] = Decimal(str(acut))
226     cutmaperr[ cutname ] = cutmap[cutname].sqrt() #math.sqrt( cutmap[cutname] )
227    
228     roofile.Close()
229    
230     scale = 1.
231     if IsMC and Lumi>0:
232     scale = Decimal( str( Lumi * xsec[ sample ] / Nevents[sample] ))
233     print "sample weight = "+ str( xsec[ sample ] / Nevents[sample] )
234     weightmap[sample] = xsec[ sample ] / Nevents[sample]
235     ilabel = 0
236     for key in cutmap.keys():
237    
238     cutmap[ key ] = Decimal(str(scale)) * cutmap[ key]
239     cutmaperr[key]= Decimal(str(scale)) * Decimal(str(cutmaperr[key])) * Decimal( str(scale) ) * Decimal(str(cutmaperr[key]))
240    
241     print " cut "+str(key) + " ("+cutlabel[key]+") "+" = "+str( round(cutmap[key],Ndecimals) )
242    
243     if allmap.has_key(key):
244     allmap[ key ] += cutmap[ key ]
245     allmaperr[ key ] += cutmaperr[key]
246     else:
247     allmap[ key ] = cutmap[ key ]
248     allmaperr[ key ] = cutmap[key]
249     ilabel += 1
250     cutflow[ sample ] = cutmap
251     cutflowerr[ sample ] = cutmaperr
252    
253     print " TOTAL"
254     ilabel=0
255     for key in allmap.keys():
256     print " cut "+str(key) + " ("+cutlabel[key]+") "+" = "+str( round(allmap[key],Ndecimals) )
257     ilabel += 1
258    
259     cutflow["Total"] = allmap
260     cutflowerr["Total"] = allmaperr
261    
262     # write latex
263     #sortedcutlist = ['CleanFilters','HLT','GoodPV','OneIsoMu','LooseMuVeto','ElectronVeto','MET','1Jet','2Jet','3Jet','4Jet']
264     sortedcutlist = ['GoodPV','OneIsoMu','LooseMuVeto','ElectronVeto']#,'MET','1Jet','2Jet','2Jet1b']
265     sortedcutlist2= ['5Jet', 'Ht', '5Jet1b'] #'MET','2Jet0b','2JetOnly1b','2Jet2b','2Jet1b','j1j2pt','toppt','topmass']
266    
267     if IsMC:
268     cutlabel['CleanFilters'] = 'Processed'
269    
270     texname = "cutflow_"+JetType+"_data.tex"
271    
272     if IsMC:
273     texname = "cutflow_"+JetType+"_MC.tex"
274     if showGh:
275     texname = "cutflow_"+JetType+"_MC_Gh"+WprimeType+".tex"
276    
277     outtex = open(texname,"w")
278    
279     sss = " & "
280    
281     # Write Header
282     outtex.write('''
283     \\begin{table}[h]
284     \\begin{centering}
285     ''')
286     if IsMC:
287     aline = '\\begin{tabular}{|l|'+'c|'*(len(sortedcutlist)+1) +'} \hline \n'
288     if Lumi<=0:
289     aline = '\\begin{tabular}{|l|'+'c|'*len(sortedcutlist) +'} \hline \n'
290     outtex.write(aline)
291     #outtex.write('Cut & ttbar & Wjets & Zjets & QCD & STtch \hline')
292     line = "Sample"
293     for icut in sortedcutlist:
294     line += sss+cutlabel[icut]
295     outtex.write(line+" \\\ \hline \n")
296     else:
297     aline = '\\begin{tabular}{l|r|r|r|} \hline \n'
298    
299     outtex.write(aline)
300     line = "Sample"
301     for icut in sortedcutlist:
302     line += sss+cutlabel[icut]
303     outtex.write(line+" \\\ \hline \n")
304    
305     ilabel = 0
306     #for key in allmap.keys():
307     # Write content
308     for sample in tablelist:
309    
310     line = label[sample]
311    
312     for key in sortedcutlist:
313     cutmap = cutflow[sample]
314     cutmaperr = cutflowerr[sample]
315     acut = cutmap[key]
316     acuterr = cutmaperr[key]
317     stracut = str(int(acut))
318     stracuterr = str(round(math.sqrt(acuterr),Ndecimals))
319     #stracut = stracut.strip('.0')
320     if IsMC:
321     acut = round(cutmap[key],Ndecimals)
322     stracut = str(acut)
323     line += sss + stracut + " $\\pm$ " + stracuterr
324     else:
325     line += sss + stracut
326     if sample=="Total":line = '\\hline \n'+line
327     outtex.write(line+" \\\ \n")
328     ilabel += 1
329    
330     outtex.write(''' \hline
331     \end{tabular}
332     %\caption{MC cutflow}\label{tab:tab}
333     \end{centering}
334     \end{table}
335     ''')
336    
337    
338     if len(sortedcutlist2) > 0:
339    
340    
341     # Write Header
342     outtex.write('''
343     \\begin{table}[h]
344     \\begin{centering}
345     ''')
346     if IsMC:
347     aline = '\\begin{tabular}{|l|'+'c|'*(len(sortedcutlist2)+1) +'} \hline \n'
348     if Lumi<=0:
349     aline = '\\begin{tabular}{|l|'+'c|'*len(sortedcutlist2) +'} \hline \n'
350     outtex.write(aline)
351     #outtex.write('Cut & ttbar & Wjets & Zjets & QCD & STtch \hline')
352     line = "Sample"
353     for icut in sortedcutlist2:
354     line += sss+cutlabel[icut]
355     outtex.write(line+" \\\ \hline \n")
356     else:
357     aline = '\\begin{tabular}{|l|r|r|r|} \hline \n'
358    
359     outtex.write(aline)
360     line = "Sample"
361     for icut in sortedcutlist2:
362     line += sss+cutlabel[icut]
363     outtex.write(line+" \\\ \hline \n")
364    
365     ilabel = 0
366     #for key in allmap.keys():
367     # Write content
368     for sample in tablelist:
369    
370     line = label[sample]
371    
372     for key in sortedcutlist2:
373     cutmap = cutflow[sample]
374     cutmaperr = cutflowerr[sample]
375     acut = cutmap[key]
376     acuterr = cutmaperr[key]
377     stracut = str(int(acut))
378     stracuterr = str(round(math.sqrt(acuterr),Ndecimals))
379     #stracut = stracut.strip('.0')
380     if IsMC:
381     acut = round(cutmap[key],Ndecimals)
382     stracut = str(acut)
383     line += sss + stracut + " $\\pm$ " + stracuterr
384     else:
385     line += sss + stracut
386     if sample=="Total":line = '\\hline \n'+line
387     outtex.write(line+" \\\ \n")
388     ilabel += 1
389    
390     outtex.write(''' \hline
391     \end{tabular}
392     %\caption{MC cutflow}\label{tab:tab}
393     \end{centering}
394     \end{table}
395     ''')
396    
397    
398     print "file "+texname+ " has been written."
399    
400     if IsMC:
401    
402     print "\n MC Weights"
403     tmplistsamples = weightmap.keys()
404     tmplistsamples.sort()
405     for sample in tmplistsamples:
406    
407     print sample+" "+str(weightmap[sample])
408    
409    
410    
411