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("-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 |
ahart |
1.17 |
parser.remove_option("-r")
|
22 |
wulsin |
1.18 |
parser.remove_option("-o")
|
23 |
|
|
parser.remove_option("--2D")
|
24 |
ahart |
1.17 |
|
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 |
wulsin |
1.18 |
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 |
ahart |
1.17 |
|
35 |
lantonel |
1.13 |
(arguments, args) = parser.parse_args()
|
36 |
lantonel |
1.1 |
|
37 |
lantonel |
1.12 |
if arguments.localConfig:
|
38 |
lantonel |
1.1 |
sys.path.append(os.getcwd())
|
39 |
lantonel |
1.12 |
exec("from " + arguments.localConfig.rstrip('.py') + " import *")
|
40 |
lantonel |
1.1 |
|
41 |
lantonel |
1.12 |
condor_dir = set_condor_output_dir(arguments)
|
42 |
lantonel |
1.1 |
|
43 |
|
|
from ROOT import TFile, gROOT, gStyle, gDirectory, TKey
|
44 |
|
|
|
45 |
|
|
channels = []
|
46 |
|
|
processed_datasets = []
|
47 |
wulsin |
1.18 |
|
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 |
lantonel |
1.1 |
|
55 |
|
|
replacements = {
|
56 |
|
|
">":"$>$",
|
57 |
|
|
"<":"$<$",
|
58 |
lantonel |
1.11 |
" eta ":" $\\eta$ ",
|
59 |
|
|
" abs(eta) ":" $|\\eta|$ ",
|
60 |
lantonel |
1.1 |
"#":"Num",
|
61 |
|
|
|
62 |
|
|
"\\rightarrow":"{\\rightarrow}",
|
63 |
|
|
"\\mu QCD":"\\mu$ $QCD",
|
64 |
|
|
"EM QCD":"EM$ $QCD",
|
65 |
|
|
"BCtoE QCD":"BCtoE$ $QCD",
|
66 |
ahart |
1.3 |
|
67 |
|
|
|
68 |
lantonel |
1.11 |
" pt ":" $p_{T}$ ",
|
69 |
|
|
" ht ":"$H_{T}$ ",
|
70 |
ahart |
1.3 |
|
71 |
lantonel |
1.1 |
"d0":"$d_{0}$",
|
72 |
|
|
|
73 |
lantonel |
1.11 |
" 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 |
lantonel |
1.1 |
|
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 |
lantonel |
1.11 |
|
93 |
lantonel |
1.1 |
}
|
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 |
lantonel |
1.5 |
#print "Couldn't find output file for",dataset,"dataset"
|
101 |
lantonel |
1.1 |
continue
|
102 |
|
|
testFile = TFile(fileName)
|
103 |
|
|
if not (testFile.IsZombie()):
|
104 |
|
|
processed_datasets.append(dataset)
|
105 |
|
|
|
106 |
|
|
if len(processed_datasets) is 0:
|
107 |
lantonel |
1.5 |
sys.exit("Can't find any output root files for the given list of datasets")
|
108 |
lantonel |
1.1 |
|
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 |
ahart |
1.3 |
fout = open (texfile, "w")
|
123 |
ahart |
1.9 |
fout.write ("\\documentclass[a2paper,8pt]{article}\n\n")
|
124 |
ahart |
1.3 |
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 |
ahart |
1.15 |
#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 |
ahart |
1.4 |
|
144 |
wulsin |
1.18 |
|
145 |
lantonel |
1.1 |
for channel in channels: # loop over final states, which each have their own directory
|
146 |
lantonel |
1.11 |
formatted_channel = channel.replace("_"," ")
|
147 |
ahart |
1.6 |
cutFlowArgs = ""
|
148 |
|
|
selectionArgs = ""
|
149 |
|
|
minusOneArgs = ""
|
150 |
lantonel |
1.1 |
#print hist
|
151 |
wulsin |
1.18 |
|
152 |
lantonel |
1.1 |
for dataset in processed_datasets:
|
153 |
ahart |
1.15 |
dataset_file = "%s/%s.root" % (condor_dir,dataset)
|
154 |
lantonel |
1.1 |
#print dataset_file
|
155 |
ahart |
1.17 |
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 |
ahart |
1.6 |
cutFlowArgs = cutFlowArgs + " " + dataset_file
|
166 |
|
|
selectionArgs = selectionArgs + " " + dataset_file
|
167 |
|
|
minusOneArgs = minusOneArgs + " " + dataset_file
|
168 |
lantonel |
1.16 |
cutFlowArgs = cutFlowArgs + " '" + channel + "CutFlow'"
|
169 |
|
|
selectionArgs = selectionArgs + " '" + channel + "Selection'"
|
170 |
|
|
minusOneArgs = minusOneArgs + " '" + channel + "MinusOne'"
|
171 |
ahart |
1.3 |
|
172 |
lantonel |
1.1 |
rawlabel = "$" + labels[dataset] + "$"
|
173 |
|
|
label = rawlabel.replace("#","\\")
|
174 |
|
|
label = "'" + label + "'"
|
175 |
|
|
#print label
|
176 |
ahart |
1.6 |
cutFlowArgs = cutFlowArgs + " " + label
|
177 |
|
|
selectionArgs = selectionArgs + " " + label
|
178 |
|
|
minusOneArgs = minusOneArgs + " " + label
|
179 |
lantonel |
1.1 |
|
180 |
|
|
#make cutFlowTable objects
|
181 |
ahart |
1.6 |
fout = open (texfile, "a")
|
182 |
ahart |
1.8 |
if not firstChannel:
|
183 |
|
|
fout.write ("\\pagebreak\n\n")
|
184 |
ahart |
1.17 |
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 |
ahart |
1.8 |
firstChannel = False
|
192 |
lantonel |
1.11 |
fout.write ("\\section*{" + formatted_channel + " channel}\n\n")
|
193 |
ahart |
1.8 |
fout.write ("\\subsection*{Cut flow}\n\n")
|
194 |
ahart |
1.6 |
fout.close ()
|
195 |
wulsin |
1.19 |
# print "Debug: running command: %s -l %g -m %s >> %s" % (cutFlowTable, intLumi,cutFlowArgs,texfile)
|
196 |
ahart |
1.17 |
os.system("%s -l %g -m %s >> %s" % (cutFlowTable, intLumi,cutFlowArgs,texfile))
|
197 |
ahart |
1.6 |
fout = open (texfile, "a")
|
198 |
|
|
fout.write ("\\pagebreak\n\n")
|
199 |
lantonel |
1.11 |
fout.write ("\\section*{" + formatted_channel + " channel}\n\n")
|
200 |
ahart |
1.8 |
fout.write ("\\subsection*{Individual selection}\n\n")
|
201 |
ahart |
1.6 |
fout.close ()
|
202 |
ahart |
1.17 |
os.system("%s -l %g %s >> %s" % (cutFlowTable, intLumi,selectionArgs,texfile))
|
203 |
ahart |
1.6 |
fout = open (texfile, "a")
|
204 |
|
|
fout.write ("\\pagebreak\n\n")
|
205 |
lantonel |
1.11 |
fout.write ("\\section*{" + formatted_channel + " channel}\n\n")
|
206 |
ahart |
1.8 |
fout.write ("\\subsection*{Minus one}\n\n")
|
207 |
ahart |
1.6 |
fout.close ()
|
208 |
ahart |
1.17 |
os.system("%s -l %g %s >> %s" % (cutFlowTable, intLumi,minusOneArgs,texfile))
|
209 |
ahart |
1.4 |
|
210 |
ahart |
1.15 |
#for dataset in processed_datasets:
|
211 |
|
|
# dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
|
212 |
|
|
# os.remove(dataset_file)
|
213 |
ahart |
1.3 |
|
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 |
wulsin |
1.18 |
|
237 |
|
|
print "Finished writing cutFlow to " + condor_dir + "/" + outputFileName + "{.tex,.pdf}"
|
238 |
|
|
|