ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/algomez/FourTop/TreeAnalyzer/test/mkCutFlowTables.py
Revision: 1.2
Committed: Thu Mar 1 17:29:31 2012 UTC (13 years, 2 months ago) by algomez
Content type: text/x-python
Branch: MAIN
Changes since 1.1: +53 -35 lines
Log Message:
new version, new Gh samples and new cuts

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 algomez 1.2 kFactor = 1.0
14 algomez 1.1 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 algomez 1.2 file['ttbar'] = path+'/results_ttbar.root'
46     file['QCD'] = path+'/results_QCD.root'
47 algomez 1.1 if splitWjets:
48 algomez 1.2 file['Wbb'] = path+'/results_Wbb.root'
49     file['Wcc']= path+'/results_Wcc.root'
50     file['Wqq']= path+'/results_Wqq.root'
51 algomez 1.1 else:
52 algomez 1.2 file['Wjets'] = path+'/results_WJets.root'
53     file['Zjets'] = path+'/results_ZJets.root'
54     file['tch'] = path+'/results_STtch.root'
55     file['tWch'] = path+'/results_STtWch.root'
56     file['sch'] = path+'/results_STsch.root'
57     file['tch_bar'] = path+'/results_STtch_bar.root'
58     file['tWch_bar'] = path+'/results_STtWch_bar.root'
59     file['sch_bar'] = path+'/results_STsch_bar.root'
60     file['WW'] = path+'/results_WW.root'
61     file['WZ'] = path+'/results_WZ.root'
62     file['4TopSM'] = path+'/results_4TopSM.root'
63    
64     file['Gh500'] = path+'/results_4Top500.root'
65     file['Gh700'] = path+'/results_4Top700.root'
66     file['Gh900'] = path+'/results_4Top900.root'
67     file['Gh1000'] = path+'/results_4Top1000.root'
68     file['Gh1100'] = path+'/results_4Top1100.root'
69 algomez 1.1
70     xsec['ttbar'] = 163.0 #157.5
71     xsec['QCD'] = 84679.3
72     xsec['Wjets'] = 31314.0
73    
74     xsec['Wbb'] = 31314.0 * 1.21
75     xsec['Wcc'] = 31314.0 * 1.66
76     xsec['Wqq'] = 31314.0 #* 0.58
77    
78     xsec['Zjets'] = 3048.0
79     xsec['tch'] = 41.92
80     xsec['tWch'] = 7.87
81     xsec['sch'] = 3.19
82     xsec['tch_bar'] = 22.65
83     xsec['tWch_bar'] = 7.87
84     xsec['sch_bar'] = 1.44
85     xsec['WW'] = 43.0
86     xsec['WZ'] = 18.0
87 algomez 1.2 xsec['4TopSM'] = 0.0004746
88     xsec['Gh500'] = 0.18182*kFactor
89     xsec['Gh700'] = 0.012131*kFactor
90     xsec['Gh900'] = 0.0011484*kFactor
91     xsec['Gh1000'] = 0.00038029*kFactor
92     xsec['Gh1100'] = 0.00012830*kFactor
93 algomez 1.1
94     Nevents['ttbar'] = 3701947.0
95     Nevents['Wjets'] = 77105816.0
96    
97     Nevents['Wbb'] = 77105816.0
98     Nevents['Wcc'] = 77105816.0
99     Nevents['Wqq'] = 77105816.0
100    
101     Nevents['Zjets'] = 36277961.0
102     Nevents['WW'] = 4225916.0
103     Nevents['WZ'] = 4265243.0
104     Nevents['QCD'] = 25080241.0
105     Nevents['sch'] = 259971.0
106     Nevents['sch_bar'] = 137980.0
107     Nevents['tch'] = 3900171.0
108     Nevents['tch_bar'] = 1944826.0
109     Nevents['tWch'] = 814390.0
110     Nevents['tWch_bar'] = 809984.0
111 algomez 1.2 Nevents['4TopSM'] = 3057.0
112 algomez 1.1
113     Nevents['Gh500'] = 3368.0
114 algomez 1.2 Nevents['Gh700'] = 3757.0
115     Nevents['Gh900'] = 3949.0
116 algomez 1.1 Nevents['Gh1000'] = 3977.0
117 algomez 1.2 Nevents['Gh1100'] = 4041.0
118 algomez 1.1
119     label['ttbar'] = '$t\\overline{t}$'
120     label['QCD'] = 'QCD'
121     label['Wjets'] = '$W\\rightarrow l\\nu$'
122    
123     label['Wbb'] = '$Wbb$'
124     label['Wcc'] = '$Wcc$'
125     label['Wqq'] = '$Wqq$'
126    
127     label['Zjets'] = '$Z\\rightarrow l^{+}l^{-}$'
128     label['tch'] = 'top t-ch'
129     label['tWch'] = 'top tW-ch'
130     label['sch'] = 'top s-ch'
131     label['tch_bar'] = 'anti-top t-ch'
132     label['tWch_bar'] = 'anti-top tW-ch'
133     label['sch_bar'] = 'anti-top s-ch'
134     label['WW'] = 'WW'
135     label['WZ'] = 'WZ'
136    
137 algomez 1.2 label['4TopSM'] = 'FourTop SM'
138    
139 algomez 1.1 label['Gh500'] = "Gh 0.5 TeV"
140 algomez 1.2 label['Gh700'] = "Gh 0.7 TeV"
141     label['Gh900'] = "Gh 0.9 TeV"
142 algomez 1.1 label['Gh1000'] = "Gh 1.0 TeV"
143 algomez 1.2 label['Gh1100'] = "Gh 1.1 TeV"
144 algomez 1.1
145     label['Total'] = 'Total MC'
146     else:
147     # data files
148     #file['Jun14'] = ''
149     #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'
150     #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'
151     #file['Prompt']= '/uscms_data/d3/ttmuj/Documents/NtupleMaker/Data/2.88pb-1/ttmuj_Prompt_Sep3.root'
152    
153     #label['Jun14'] = 'Jun 14th Mu'
154     #label['MB'] = 'Jun 14th MinimumBias'
155     #label['Jul16'] = 'Jul 16th'
156     #label['Prompt'] = 'PromptReco'
157    
158     #if os.path.isfile(path+'/JPT/cutflow_JPT_data.txt'):
159     # file['dataJPT'] = path+'/cutflow_JPT_data.txt'
160     # label['dataJPT'] = 'JPT Ref. Sel.'
161     #if os.path.isfile(path+'/calo/cutflow_calo_data.txt'):
162     # file['datacalo'] = path+'/cutflow_calo_data.txt'
163     # label['datacalo'] = 'Calo Ref. Sel.'
164     #if os.path.isfile(path+'/PF/cutflow_PF_data.txt'):
165     # file['dataPF'] = path+'/cutflow_PF_data.txt'
166     # label['dataPF'] = 'PF Ref. Sel.'
167    
168 algomez 1.2 file['data'] = path+'/results_data.root';
169 algomez 1.1 label['data'] = "Data";
170    
171    
172     keys = file.keys()
173     cutflow = {}
174     cutflowerr = {}
175     cutlabel = {}
176     cutlabel['Processed'] = 'Processed'
177     cutlabel['CleanFilters'] = 'CleanFilters'
178     cutlabel['HLT'] = 'HLT'
179     cutlabel['GoodPV'] = 'Skim'
180     cutlabel['OneIsoMu'] = 'One Iso $\mu$'
181     cutlabel['LooseMuVeto'] = 'Loose $\mu$ veto'
182     cutlabel['ElectronVeto'] = 'Electron veto'
183     cutlabel['1Jet'] = 'jets $\geq 1$'
184     cutlabel['2Jet'] = 'jets $\geq 2$'
185     cutlabel['3Jet'] = 'jets $\geq 3$'
186 algomez 1.2 cutlabel['4Jet'] = 'jets $\geq 4$'
187     cutlabel['Ht'] = '$H_t \geq 300$'
188     cutlabel['4Jet1b'] = 'btags $\geq 1$'
189     cutlabel['4JetCut'] = '$p_t^{jet4} > 40$ '
190     cutlabel['Ht2'] = '$H_t \geq 500$'
191     cutlabel['Stjet'] = '$S_t^{jet} \geq 500$'
192 algomez 1.1
193 algomez 1.2 cutlabelvector = [ 'GoodPV', 'OneIsoMu', 'LooseMuVeto', 'ElectronVeto', '4Jet','Ht', '4Jet1b', '4JetCut', 'Stjet']
194     SKIPCUTS = []#'4JetCut']#'1Jet','3Jet','4Jet']
195 algomez 1.1
196     allmap = {}
197     allmaperr = {}
198    
199     weightmap = {}
200    
201 algomez 1.2 tablelist = ['ttbar','Wjets','Zjets','QCD','tch','tch_bar','tWch','tWch_bar','sch','sch_bar','WW','WZ','4TopSM','Total']
202 algomez 1.1
203     if splitWjets:
204 algomez 1.2 tablelist = ['ttbar','Wbb','Wcc','Wqq','Zjets','QCD','tch','tch_bar','tWch','tWch_bar','sch','sch_bar','WW','WZ','4TopSM','Total']
205 algomez 1.1
206     if showGh:
207 algomez 1.2 tablelist = ['Gh500','Gh700','Gh900','Gh1000','Gh1100']
208 algomez 1.1
209     if Lumi<=0:
210 algomez 1.2 tablelist = ['ttbar','Wjets','Zjets','QCD','tch','tch_bar','tWch','tWch_bar','sch','sch_bar','WW','WZ','4TopSM']
211 algomez 1.1
212     if not IsMC:
213     tablelist = ['data']
214    
215     for sample in tablelist:
216    
217     if sample=="Total": continue
218     print " processing " + sample
219    
220     cutmap = {}
221     cutmaperr = {}
222     #txtfile = open(file[sample])
223     roofile = TFile(file[sample])
224     hcutflow = gDirectory.Get("cutflow")
225     print " entries in cutflow histogram: " + str(hcutflow.GetEntries())
226    
227     for ibin in xrange(1, hcutflow.GetNbinsX() +1 ):
228    
229     skipthiscut = False
230     for skipcut in SKIPCUTS:
231     if skipcut == cutlabelvector[ibin-1]: skipthiscut = True
232     if skipthiscut:
233     print "skip counting cut name: "+cutlabelvector[ibin-1]
234     continue
235     cutname = cutlabelvector[ibin-1]
236     acut = hcutflow.GetBinContent(ibin)
237     #print cutname
238     #print acut
239     #if sample=="data":
240     if sample.find("data")!=-1:
241     cutmap[ cutname ] = int(float(acut))
242     cutmaperr[ cutname ] = math.sqrt( cutmap[cutname] )
243     else:
244     cutmap[ cutname ] = Decimal(str(acut))
245     cutmaperr[ cutname ] = cutmap[cutname].sqrt() #math.sqrt( cutmap[cutname] )
246    
247     roofile.Close()
248    
249     scale = 1.
250     if IsMC and Lumi>0:
251     scale = Decimal( str( Lumi * xsec[ sample ] / Nevents[sample] ))
252     print "sample weight = "+ str( xsec[ sample ] / Nevents[sample] )
253     weightmap[sample] = xsec[ sample ] / Nevents[sample]
254     ilabel = 0
255     for key in cutmap.keys():
256    
257     cutmap[ key ] = Decimal(str(scale)) * cutmap[ key]
258     cutmaperr[key]= Decimal(str(scale)) * Decimal(str(cutmaperr[key])) * Decimal( str(scale) ) * Decimal(str(cutmaperr[key]))
259    
260     print " cut "+str(key) + " ("+cutlabel[key]+") "+" = "+str( round(cutmap[key],Ndecimals) )
261    
262     if allmap.has_key(key):
263     allmap[ key ] += cutmap[ key ]
264     allmaperr[ key ] += cutmaperr[key]
265     else:
266     allmap[ key ] = cutmap[ key ]
267     allmaperr[ key ] = cutmap[key]
268     ilabel += 1
269     cutflow[ sample ] = cutmap
270     cutflowerr[ sample ] = cutmaperr
271    
272     print " TOTAL"
273     ilabel=0
274     for key in allmap.keys():
275     print " cut "+str(key) + " ("+cutlabel[key]+") "+" = "+str( round(allmap[key],Ndecimals) )
276     ilabel += 1
277    
278     cutflow["Total"] = allmap
279     cutflowerr["Total"] = allmaperr
280    
281     # write latex
282     #sortedcutlist = ['CleanFilters','HLT','GoodPV','OneIsoMu','LooseMuVeto','ElectronVeto','MET','1Jet','2Jet','3Jet','4Jet']
283 algomez 1.2 sortedcutlist = ['GoodPV','OneIsoMu','LooseMuVeto','ElectronVeto']
284     sortedcutlist2= ['4Jet', 'Ht', '4Jet1b', '4JetCut', 'Stjet']
285 algomez 1.1
286     if IsMC:
287     cutlabel['CleanFilters'] = 'Processed'
288    
289     texname = "cutflow_"+JetType+"_data.tex"
290    
291     if IsMC:
292     texname = "cutflow_"+JetType+"_MC.tex"
293     if showGh:
294     texname = "cutflow_"+JetType+"_MC_Gh"+WprimeType+".tex"
295    
296     outtex = open(texname,"w")
297    
298     sss = " & "
299    
300     # Write Header
301     outtex.write('''
302     \\begin{table}[h]
303     \\begin{centering}
304     ''')
305     if IsMC:
306     aline = '\\begin{tabular}{|l|'+'c|'*(len(sortedcutlist)+1) +'} \hline \n'
307     if Lumi<=0:
308     aline = '\\begin{tabular}{|l|'+'c|'*len(sortedcutlist) +'} \hline \n'
309     outtex.write(aline)
310     #outtex.write('Cut & ttbar & Wjets & Zjets & QCD & STtch \hline')
311     line = "Sample"
312     for icut in sortedcutlist:
313     line += sss+cutlabel[icut]
314     outtex.write(line+" \\\ \hline \n")
315     else:
316     aline = '\\begin{tabular}{l|r|r|r|} \hline \n'
317    
318     outtex.write(aline)
319     line = "Sample"
320     for icut in sortedcutlist:
321     line += sss+cutlabel[icut]
322     outtex.write(line+" \\\ \hline \n")
323    
324     ilabel = 0
325     #for key in allmap.keys():
326     # Write content
327     for sample in tablelist:
328    
329     line = label[sample]
330    
331     for key in sortedcutlist:
332     cutmap = cutflow[sample]
333     cutmaperr = cutflowerr[sample]
334     acut = cutmap[key]
335     acuterr = cutmaperr[key]
336     stracut = str(int(acut))
337     stracuterr = str(round(math.sqrt(acuterr),Ndecimals))
338     #stracut = stracut.strip('.0')
339     if IsMC:
340     acut = round(cutmap[key],Ndecimals)
341     stracut = str(acut)
342     line += sss + stracut + " $\\pm$ " + stracuterr
343     else:
344     line += sss + stracut
345     if sample=="Total":line = '\\hline \n'+line
346     outtex.write(line+" \\\ \n")
347     ilabel += 1
348    
349     outtex.write(''' \hline
350     \end{tabular}
351     %\caption{MC cutflow}\label{tab:tab}
352     \end{centering}
353     \end{table}
354     ''')
355    
356    
357     if len(sortedcutlist2) > 0:
358    
359    
360     # Write Header
361     outtex.write('''
362     \\begin{table}[h]
363     \\begin{centering}
364     ''')
365     if IsMC:
366     aline = '\\begin{tabular}{|l|'+'c|'*(len(sortedcutlist2)+1) +'} \hline \n'
367     if Lumi<=0:
368     aline = '\\begin{tabular}{|l|'+'c|'*len(sortedcutlist2) +'} \hline \n'
369     outtex.write(aline)
370     #outtex.write('Cut & ttbar & Wjets & Zjets & QCD & STtch \hline')
371     line = "Sample"
372     for icut in sortedcutlist2:
373     line += sss+cutlabel[icut]
374     outtex.write(line+" \\\ \hline \n")
375     else:
376     aline = '\\begin{tabular}{|l|r|r|r|} \hline \n'
377    
378     outtex.write(aline)
379     line = "Sample"
380     for icut in sortedcutlist2:
381     line += sss+cutlabel[icut]
382     outtex.write(line+" \\\ \hline \n")
383    
384     ilabel = 0
385     #for key in allmap.keys():
386     # Write content
387     for sample in tablelist:
388    
389     line = label[sample]
390    
391     for key in sortedcutlist2:
392     cutmap = cutflow[sample]
393     cutmaperr = cutflowerr[sample]
394     acut = cutmap[key]
395     acuterr = cutmaperr[key]
396     stracut = str(int(acut))
397     stracuterr = str(round(math.sqrt(acuterr),Ndecimals))
398     #stracut = stracut.strip('.0')
399     if IsMC:
400     acut = round(cutmap[key],Ndecimals)
401     stracut = str(acut)
402     line += sss + stracut + " $\\pm$ " + stracuterr
403     else:
404     line += sss + stracut
405     if sample=="Total":line = '\\hline \n'+line
406     outtex.write(line+" \\\ \n")
407     ilabel += 1
408    
409     outtex.write(''' \hline
410     \end{tabular}
411     %\caption{MC cutflow}\label{tab:tab}
412     \end{centering}
413     \end{table}
414     ''')
415    
416     print "file "+texname+ " has been written."
417    
418     if IsMC:
419    
420     print "\n MC Weights"
421     tmplistsamples = weightmap.keys()
422     tmplistsamples.sort()
423     for sample in tmplistsamples:
424    
425     print sample+" "+str(weightmap[sample])
426    
427    
428    
429