ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeCutFlows.py
Revision: 1.17
Committed: Mon May 20 00:46:58 2013 UTC (11 years, 11 months ago) by ahart
Content type: text/x-python
Branch: MAIN
CVS Tags: V02-03-01, V02-03-00, V02-02-00, V02-01-01
Changes since 1.16: +31 -3 lines
Log Message:
Added an sToB option. Also, the ratio and diff options now cause a ratio or diff column, respectively, to be printed.

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