ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeCutFlows.py
Revision: 1.13
Committed: Wed Mar 13 09:27:11 2013 UTC (12 years, 1 month ago) by lantonel
Content type: text/x-python
Branch: MAIN
Changes since 1.12: +3 -4 lines
Log Message:
switched back to optparse, since CMSSW_5_X_X has an older version of python

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