ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeCutFlows.py
Revision: 1.18
Committed: Mon Jul 8 08:55:37 2013 UTC (11 years, 10 months ago) by wulsin
Content type: text/x-python
Branch: MAIN
Changes since 1.17: +27 -2 lines
Log Message:
Add option to specify output file name; remove unused options

File Contents

# User Rev Content
1 lantonel 1.1 #!/usr/bin/env python
2     import sys
3     import os
4     import fileinput
5     from array import *
6 lantonel 1.13 from optparse import OptionParser
7 lantonel 1.2 from OSUT3Analysis.Configuration.configurationOptions import *
8     from OSUT3Analysis.Configuration.processingUtilities import *
9 lantonel 1.1
10 lantonel 1.13 parser = OptionParser()
11 lantonel 1.1 parser = set_commandline_arguments(parser)
12 ahart 1.17
13     parser.remove_option("-d")
14 wulsin 1.18 parser.remove_option("-q")
15     parser.remove_option("-n")
16     parser.remove_option("-u")
17     parser.remove_option("-e")
18     parser.remove_option("-R")
19     parser.remove_option("-b")
20     parser.remove_option("-t")
21     parser.remove_option("-y")
22     parser.remove_option("-p")
23 ahart 1.17 parser.remove_option("-r")
24 wulsin 1.18 parser.remove_option("-o")
25     parser.remove_option("--2D")
26 ahart 1.17
27     parser.add_option("-d", "--diff", action="store_true", dest="makeDiffPlots", default=False,
28     help="add a column for data-MC to the tables")
29     parser.add_option("-r", "--ratio", action="store_true", dest="makeRatioPlots", default=False,
30     help="add a colunn for (data-MC)/MC to the tables")
31     parser.add_option("-s", "--sToB", action="store_true", dest="signalToBackground", default=False,
32     help="add a column for S/sqrt(S+B) to the tables")
33 wulsin 1.18 parser.add_option("-o", "--output-file", dest="outputFileName",
34     help="specify an output file base name for the cutflow table (suffix will be appended), default is 'cutFlow'")
35    
36 ahart 1.17
37 lantonel 1.13 (arguments, args) = parser.parse_args()
38 lantonel 1.1
39 lantonel 1.12 if arguments.localConfig:
40 lantonel 1.1 sys.path.append(os.getcwd())
41 lantonel 1.12 exec("from " + arguments.localConfig.rstrip('.py') + " import *")
42 lantonel 1.1
43 lantonel 1.12 condor_dir = set_condor_output_dir(arguments)
44 lantonel 1.1
45     from ROOT import TFile, gROOT, gStyle, gDirectory, TKey
46    
47     channels = []
48     processed_datasets = []
49 wulsin 1.18
50     outputFileName = "cutFlow"
51     if arguments.outputFileName:
52     outputFileName = arguments.outputFileName
53     outputFileName = outputFileName.partition(".")[0] # If the input filename contains a period, take only the part before the period.
54    
55     texfile = condor_dir + "/" + outputFileName + ".tex"
56 lantonel 1.1
57     replacements = {
58     ">":"$>$",
59     "<":"$<$",
60 lantonel 1.11 " eta ":" $\\eta$ ",
61     " abs(eta) ":" $|\\eta|$ ",
62 lantonel 1.1 "#":"Num",
63    
64     "\\rightarrow":"{\\rightarrow}",
65     "\\mu QCD":"\\mu$ $QCD",
66     "EM QCD":"EM$ $QCD",
67     "BCtoE QCD":"BCtoE$ $QCD",
68 ahart 1.3
69    
70 lantonel 1.11 " pt ":" $p_{T}$ ",
71     " ht ":"$H_{T}$ ",
72 ahart 1.3
73 lantonel 1.1 "d0":"$d_{0}$",
74    
75 lantonel 1.11 " MET ":" Met ",
76     " MEt ":" Met ",
77     " met ":" Met ",
78     " Met ":"$\\not\\!\\!{E}_{T}$ ",
79    
80     " MHT ":" Mht ",
81     " MHt ":" Mht ",
82     " mht ":" Mht ",
83     " Mht ":"$\\not\\!\\!{H}_{T}$ ",
84 lantonel 1.1
85     "M_Z" : "$M_{Z}$",
86     "M_mumu" : "$M_{\\mu\\mu}$",
87     "M_ee" : "$M_{ee}$",
88     "M_ll" : "$M_{ll}$",
89    
90     }
91    
92     secondary_replacements = {
93     "$$<$":"$<"
94 lantonel 1.11
95 lantonel 1.1 }
96    
97    
98     #### check which input datasets have valid output files
99     for dataset in datasets:
100     fileName = condor_dir + "/" + dataset + ".root"
101     if not os.path.exists(fileName):
102 lantonel 1.5 #print "Couldn't find output file for",dataset,"dataset"
103 lantonel 1.1 continue
104     testFile = TFile(fileName)
105     if not (testFile.IsZombie()):
106     processed_datasets.append(dataset)
107    
108     if len(processed_datasets) is 0:
109 lantonel 1.5 sys.exit("Can't find any output root files for the given list of datasets")
110 lantonel 1.1
111     #### open first input file and re-make its directory structure in the output file
112     testFile = TFile(condor_dir + "/" + processed_datasets[0] + ".root")
113     testFile.cd()
114     for key in testFile.GetListOfKeys():
115     if (key.GetClassName() != "TDirectoryFile"):
116     continue
117     rootDirectory = key.GetName()
118     testFile.cd(key.GetName())
119     for key2 in gDirectory.GetListOfKeys():
120     if (key2.GetClassName() != "TDirectoryFile"):
121     continue
122     channels.append(key2.GetName())
123    
124 ahart 1.3 fout = open (texfile, "w")
125 ahart 1.9 fout.write ("\\documentclass[a2paper,8pt]{article}\n\n")
126 ahart 1.3 fout.write ("\\usepackage[landscape,margin=0.15cm]{geometry}\n\n")
127     fout.write ("\\usepackage{multirow}\n\n")
128     fout.write ("\\begin{document}\n\n")
129     fout.write ("\\pagestyle{empty}\n\n")
130     fout.close ()
131    
132     firstChannel = True
133    
134 ahart 1.15 #weight = intLumi / 10000.0
135     #for dataset in processed_datasets:
136     # dataset_file = "%s/%s.root" % (condor_dir,dataset)
137     # fin = TFile (dataset_file)
138     # flags = fin.Get ("flags")
139     # noWeights = flags and flags.GetBinContent (1)
140     # fin.Close ()
141     # if types[dataset] != "data" and not noWeights:
142     # os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", weight))
143     # else:
144     # os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", 1.0))
145 ahart 1.4
146 wulsin 1.18
147 lantonel 1.1 for channel in channels: # loop over final states, which each have their own directory
148 lantonel 1.11 formatted_channel = channel.replace("_"," ")
149 ahart 1.6 cutFlowArgs = ""
150     selectionArgs = ""
151     minusOneArgs = ""
152 lantonel 1.1 #print hist
153 wulsin 1.18
154 lantonel 1.1 for dataset in processed_datasets:
155 ahart 1.15 dataset_file = "%s/%s.root" % (condor_dir,dataset)
156 lantonel 1.1 #print dataset_file
157 ahart 1.17 if arguments.makeDiffPlots or arguments.makeRatioPlots:
158     if types[dataset] == "data":
159     dataset_file = "\"<" + dataset_file + "\""
160     elif types[dataset] == "bgMC":
161     dataset_file = "\">" + dataset_file + "\""
162     if arguments.signalToBackground:
163     if types[dataset] == "signalMC":
164     dataset_file = "\"<" + dataset_file + "\""
165     elif types[dataset] == "bgMC":
166     dataset_file = "\">" + dataset_file + "\""
167 ahart 1.6 cutFlowArgs = cutFlowArgs + " " + dataset_file
168     selectionArgs = selectionArgs + " " + dataset_file
169     minusOneArgs = minusOneArgs + " " + dataset_file
170 lantonel 1.16 cutFlowArgs = cutFlowArgs + " '" + channel + "CutFlow'"
171     selectionArgs = selectionArgs + " '" + channel + "Selection'"
172     minusOneArgs = minusOneArgs + " '" + channel + "MinusOne'"
173 ahart 1.3
174 lantonel 1.1 rawlabel = "$" + labels[dataset] + "$"
175     label = rawlabel.replace("#","\\")
176     label = "'" + label + "'"
177     #print label
178 ahart 1.6 cutFlowArgs = cutFlowArgs + " " + label
179     selectionArgs = selectionArgs + " " + label
180     minusOneArgs = minusOneArgs + " " + label
181 lantonel 1.1
182     #make cutFlowTable objects
183 ahart 1.6 fout = open (texfile, "a")
184 ahart 1.8 if not firstChannel:
185     fout.write ("\\pagebreak\n\n")
186 ahart 1.17 cutFlowTable = "cutFlowTable"
187     if arguments.makeDiffPlots:
188     cutFlowTable += " -d \"Data - MC\""
189     if arguments.makeRatioPlots:
190     cutFlowTable += " -r \"(Data - MC) / MC\""
191     if arguments.signalToBackground:
192     cutFlowTable += " -s \"$\\mathrm{S} / \\sqrt{\\mathrm{S} + \\mathrm{B}}$\""
193 ahart 1.8 firstChannel = False
194 lantonel 1.11 fout.write ("\\section*{" + formatted_channel + " channel}\n\n")
195 ahart 1.8 fout.write ("\\subsection*{Cut flow}\n\n")
196 ahart 1.6 fout.close ()
197 ahart 1.17 os.system("%s -l %g -m %s >> %s" % (cutFlowTable, intLumi,cutFlowArgs,texfile))
198 wulsin 1.18 # print "Debug: running command: %s -l %g -m %s >> %s" % (cutFlowTable, intLumi,cutFlowArgs,texfile)
199 ahart 1.6 fout = open (texfile, "a")
200     fout.write ("\\pagebreak\n\n")
201 lantonel 1.11 fout.write ("\\section*{" + formatted_channel + " channel}\n\n")
202 ahart 1.8 fout.write ("\\subsection*{Individual selection}\n\n")
203 ahart 1.6 fout.close ()
204 ahart 1.17 os.system("%s -l %g %s >> %s" % (cutFlowTable, intLumi,selectionArgs,texfile))
205 ahart 1.6 fout = open (texfile, "a")
206     fout.write ("\\pagebreak\n\n")
207 lantonel 1.11 fout.write ("\\section*{" + formatted_channel + " channel}\n\n")
208 ahart 1.8 fout.write ("\\subsection*{Minus one}\n\n")
209 ahart 1.6 fout.close ()
210 ahart 1.17 os.system("%s -l %g %s >> %s" % (cutFlowTable, intLumi,minusOneArgs,texfile))
211 ahart 1.4
212 ahart 1.15 #for dataset in processed_datasets:
213     # dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
214     # os.remove(dataset_file)
215 ahart 1.3
216     #reformat tex files
217     for line in fileinput.FileInput(texfile,inplace=1):
218     for replacement in replacements.keys():
219     line = line.replace(replacement,replacements[replacement])
220     print line,
221    
222     for line in fileinput.FileInput(texfile,inplace=1):
223     for replacement in secondary_replacements.keys():
224     line = line.replace(replacement,secondary_replacements[replacement])
225     print line,
226    
227     fout = open (texfile, "a")
228     fout.write ("\\end{document}\n")
229     fout.close ()
230    
231     #process tex files to make pdf files
232     command = "pdflatex -interaction=batchmode -output-directory=./%s %s > /dev/null" % (condor_dir,texfile)
233     os.system(command)
234     os.system(command)
235     #os.system("rm %s" % texfile)
236     os.system("rm %saux" % (texfile.rstrip("tex")))
237     os.system("rm %slog" % (texfile.rstrip("tex")))
238 wulsin 1.18
239     print "Finished writing cutFlow to " + condor_dir + "/" + outputFileName + "{.tex,.pdf}"
240