ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeCutFlows.py
Revision: 1.19
Committed: Tue Jul 16 09:05:34 2013 UTC (11 years, 9 months ago) by wulsin
Content type: text/x-python
Branch: MAIN
CVS Tags: V02-03-02, HEAD
Changes since 1.18: +1 -3 lines
Log Message:
Do not remove -t, -q options (they no longer exist previously)

File Contents

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