ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Jeng/scripts/make_table.py
Revision: 1.2
Committed: Fri Oct 30 22:08:56 2009 UTC (15 years, 6 months ago) by jengbou
Content type: text/x-python
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +28 -9 lines
Log Message:
update

File Contents

# User Rev Content
1 jengbou 1.1 from ROOT import *
2    
3     import sys,os
4    
5    
6     #________________________________________________________________
7     def get_list_files(directory,pattern = ""):
8    
9     dir = []
10    
11     dir = os.listdir(directory)
12    
13     lfiles = []
14     for f in dir:
15    
16     if f.find(pattern) != -1 and f.find("match") == -1 and f.endswith("root"):
17    
18     lfiles.append(directory+"/"+f)
19    
20     lfiles.sort()
21     return lfiles
22    
23    
24     def main():
25 jengbou 1.2 format = 'all' ## 'html' or 'all' for html+twiki
26 jengbou 1.1
27     if len(sys.argv) < 2:
28     print "[usage] make_table.py <dir> <weight=true/false> or"
29     print "[usage] make_table.py <dir> <weight=true/false> <sampleName> <weight>"
30     print "e.g. make_table.py /Users/jengbou/Desktop/TopWork/Analysis/Validation/7TeV/ true"
31     sys.exit()
32    
33     path = sys.argv[1]
34     applyWeight = sys.argv[2]
35    
36     files = []
37     files.append(path+'TTbar_mcatnlo_Summer09_2_all.root')
38     files.append(path+'TTbar_madgraph_Summer09_2_all.root')
39 jengbou 1.2 files.append(path+'TTbar_pythia_Summer09_all.root')
40     # files.append(path+'TTbar_Summer09_all.root') ## 10 TeV
41 jengbou 1.1 files.append(path+'WJets_madgraph_Summer09_2_all.root')
42     files.append(path+'Zjets_madgraph_Summer09_2_all.root')
43     files.append(path+'WcJets_madgraph_Summer09_2_all.root')
44 jengbou 1.2 files.append(path+'AstarJets_madgraph_Summer09_3_all.root')
45 jengbou 1.1
46 jengbou 1.2 sampleNames = ['ttbar mc@nlo','ttbar madgraph','ttbar pythia', 'W+Jets','Z+Jets','Wc+Jets','Astar']
47 jengbou 1.1 weightList = [0.0321, 0.0169, 0.006, 0.1817, 0.1012, 0.0026, 0.0277]
48 jengbou 1.2
49     ## sampleNames = ['ttbar mc@nlo','ttbar madgraph','ttbar pythia', 'ttbar pythia(10 TeV)', 'W+Jets','Z+Jets','Wc+Jets','Astar']
50     ## weightList = [0.0321, 0.0169, 0.006, 0.0156, 0.1817, 0.1012, 0.0026, 0.0277]
51 jengbou 1.1 ## print len(sys.argv)
52    
53     if len(sys.argv) == 5: ## actually 4 arguments
54     del sampleNames[:]
55     del weightList[:]
56     del files[:]
57     sampleNames.append(sys.argv[3])
58     weightList = [float(sys.argv[4])]
59     ## print sampleNames
60     ## print sys.argv[3]
61     ## print weightList
62     files = get_list_files(path, sys.argv[3])
63     ## print files
64    
65 jengbou 1.2 nfiles = 0
66 jengbou 1.1 hlist = []
67    
68    
69     for f in files:
70    
71     print "\n Open File: " + f
72    
73     tf = TFile(f);
74     nfiles += 1
75     h = ROOT.gDirectory.Get("DQMData/muonJetsChecker/Selection")
76     h.SetDirectory(0)
77     #h.Print("all")
78     hlist.append(h)
79    
80    
81     select = ['All events','HLT: HLT_Mu9','>= 1 muon','>= 1 good muon','>= 1 isolated muon','= 1 isolated muon','Veto on electron','>= 1 jet','>= 2 jets','>= 3 jets','>= 4 jets','Veto Loose muon','Veto Loose electron (M3)','M3 prime']
82    
83     selectLabel = ['All events','HLT: HLT_Mu9','>= 1 muon','>= 1 good muon','>= 1 isolated muon','= 1 isolated muon','Veto on electron','>= 1 jet','>= 2 jets','>= 3 jets','>= 4 jets','Veto Loose muon','Veto Loose electron (M3)','M3 prime']
84    
85     outName = "Summary_cut_flow"
86     if len(sys.argv) == 5:
87     outName = outName + "_" + sys.argv[3]
88     outName += ".txt"
89     outfile = open(outName, 'w')
90    
91     outfile.write("<STYLE type=\"text/css\">\n")
92     outfile.write("td.alr {text-align: right}\n")
93     outfile.write("</STYLE>\n")
94     outfile.write("<table border=\"1\">\n")
95     outfile.write("<tr>")
96     outfile.write("<th>Selection</th>")
97 jengbou 1.2
98     if format == 'all':
99     outfile1 = open('TWiki_cut_flow.txt', 'w')
100     outfile1.write("| *Selection step* |")
101    
102 jengbou 1.1 i_sample = 0
103     for s in sampleNames:
104     if i_sample < nfiles:
105 jengbou 1.2 outfile.write("<th>"+s+"</th>")
106     if format == 'all':
107     outfile1.write(" *"+s+"* |")
108    
109 jengbou 1.1 i_sample += 1
110     outfile.write("</tr>\n")
111 jengbou 1.2 if format == 'all':
112     outfile1.write("\n")
113 jengbou 1.1
114     for cut in select:
115    
116     events = []
117     i_hlist = 0
118    
119     for h in hlist:
120     weight = 1.
121    
122     if applyWeight =="true":
123     weight = weightList[i_hlist]
124     i_hlist += 1
125     labels = h.GetXaxis().GetLabels()
126     labelsize = len(labels)
127     ibin = 1
128     for il in labels:
129     if il == cut:
130     events.append(weight*h.GetBinContent(ibin))
131    
132     ibin = ibin + 1
133    
134     line = "<tr><th>" + cut + "</th>"
135 jengbou 1.2 if format == 'all':
136     line1 = "| " + cut + " |"
137 jengbou 1.1
138     for ev in events:
139     line = line + "<td class=\"alr\">" + str(int(round(ev,0))) + "</td>"
140 jengbou 1.2 if format == 'all':
141     line1 = line1 + " " + str(int(round(ev,0))) + " |"
142 jengbou 1.1
143     outfile.write(line+"</tr>\n")
144 jengbou 1.2 if format == 'all':
145     outfile1.write(line1+"\n")
146 jengbou 1.1
147     outfile.write("</table>")
148     ## print line
149    
150     outfile.close()
151 jengbou 1.2 if format == 'all':
152     outfile1.close()
153 jengbou 1.1
154     if __name__ =='__main__':
155     sys.exit(main())
156