ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeYieldsTables.py
Revision: 1.5
Committed: Thu Aug 22 12:05:35 2013 UTC (11 years, 8 months ago) by lantonel
Content type: text/x-python
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +12 -0 lines
Log Message:
it now makes the pdf for you if you use the standAlone option (-s)

File Contents

# Content
1 #!/usr/bin/env python
2 import sys
3 import os
4 import re
5 import math
6 from array import *
7 from decimal import *
8 from optparse import OptionParser
9 from OSUT3Analysis.Configuration.configurationOptions import *
10 from OSUT3Analysis.Configuration.processingUtilities import *
11 from OSUT3Analysis.Configuration.formattingUtilities import *
12
13
14
15 ### parse the command-line options
16
17 parser = OptionParser()
18 parser = set_commandline_arguments(parser)
19
20 parser.remove_option("-o")
21 parser.remove_option("-n")
22 parser.remove_option("-u")
23 parser.remove_option("-e")
24 parser.remove_option("-r")
25 parser.remove_option("-R")
26 parser.remove_option("-d")
27 parser.remove_option("-b")
28 parser.remove_option("--2D")
29 parser.remove_option("-y")
30 parser.remove_option("-p")
31
32 parser.add_option("-s", "--standAlone", action="store_true", dest="standAlone", default=False,
33 help="adds the necessary header to be able to compile it")
34
35
36 (arguments, args) = parser.parse_args()
37
38
39 if arguments.localConfig:
40 sys.path.append(os.getcwd())
41 exec("from " + arguments.localConfig.rstrip('.py') + " import *")
42
43 #set condor directory
44 condor_dir = set_condor_output_dir(arguments)
45
46
47 from ROOT import TFile, TH1F, gDirectory
48
49
50 hLine = "\\hline\n"
51 endLine = " \\\\ "
52 newLine = " \n"
53
54
55 #### check which input datasets have valid output files
56 processed_datasets = []
57 for dataset in datasets:
58 if types[dataset] is "signalMC": #only include bgMC and data yields
59 continue
60 fileName = condor_dir + "/" + dataset + ".root"
61 if not os.path.exists(fileName):
62 continue
63 testFile = TFile(fileName)
64 if not (testFile.IsZombie()):
65 processed_datasets.append(dataset)
66
67 #### exit if no datasets found
68 if len(processed_datasets) is 0:
69 sys.exit("Can't find any output root files for the given list of datasets")
70
71
72 #open the first ROOT file and get the list of channels
73 channels = []
74 dataset_file = "%s/%s.root" % (condor_dir,processed_datasets[0])
75 inputFile = TFile(dataset_file)
76 inputFile.cd("OSUAnalysis")
77
78 for key in gDirectory.GetListOfKeys():
79 if (key.GetClassName() != "TDirectoryFile"):
80 continue
81 channels.append(key.GetName())
82
83 #get and store the yields and errors for each dataset
84 yields = {}
85 errors = {}
86 bgMCSum = {}
87 bgMCErrSquared = {}
88 processed_datasets_channels = {}
89
90 for channel in channels:
91 bgMCSum[channel] = 0
92 bgMCErrSquared[channel] = 0
93 processed_datasets_channels[channel] = []
94
95 for sample in processed_datasets:
96 yields[sample] = {}
97 errors[sample] = {}
98 dataset_file = "%s/%s.root" % (condor_dir,sample)
99 inputFile = TFile(dataset_file)
100 for channel in channels:
101 cutFlowHistogram = inputFile.Get("OSUAnalysis/"+channel+"CutFlow")
102 if not cutFlowHistogram:
103 print "WARNING: didn't find cutflow for ", sample, "dataset in", channel, "channel"
104 continue
105 processed_datasets_channels[channel].append(sample)
106
107 yield_ = cutFlowHistogram.GetBinContent(cutFlowHistogram.GetNbinsX())
108 error_ = cutFlowHistogram.GetBinError(cutFlowHistogram.GetNbinsX())
109 yields[sample][channel] = formatNumber(str(round_sigfigs(yield_,3)).rstrip("0").rstrip("."))
110 errors[sample][channel] = formatNumber(str(round_sigfigs(error_,3)).rstrip("0").rstrip("."))
111
112 if types[sample] is "bgMC":
113 bgMCSum[channel] = bgMCSum[channel] + yield_
114 bgMCErrSquared[channel] = bgMCErrSquared[channel] + error_*error_
115
116 inputFile.Close()
117
118
119 #write a table for each channel to a separate tex file
120
121 for channel in channels:
122 outputFile = condor_dir + "/yields_" + plainTextString(channel) + ".tex"
123 fout = open (outputFile, "w")
124 if(arguments.standAlone):
125 fout.write("\\documentclass{article}"+newLine+"\\begin{document}"+newLine)
126 fout.write ("\\makebox[0pt]{\\renewcommand{\\arraystretch}{1.2}\\begin{tabular}{lr}"+newLine+hLine)
127
128 fout.write("Event Source & Event Yield $\pm$ 1$\sigma$ (stat.)"+endLine+newLine+hLine)
129
130 #write a line for each background sample
131 bgMCcounter = 0
132 for sample in processed_datasets_channels[channel]:
133 if types[sample] is not "bgMC":
134 continue
135 bgMCcounter = bgMCcounter + 1
136 rawlabel = "$" + labels[sample] + "$"
137 label = rawlabel.replace("#","\\").replace("\\rightarrow","{\\rightarrow}").replace(" ","\\ ")
138 fout.write(label + " & " + yields[sample][channel] + " $\pm$ " + errors[sample][channel] + endLine + newLine)
139
140 #write a line with the sum of the backgrounds
141 if bgMCcounter is not 0:
142
143 bgMCSum_ = formatNumber(str(round_sigfigs(bgMCSum[channel],3)).rstrip("0").rstrip("."))
144 bgMCErr_ = formatNumber(str(round_sigfigs(math.sqrt(bgMCErrSquared[channel]),3)).rstrip("0").rstrip("."))
145
146 fout.write(hLine+"background sum & " + bgMCSum_ + " $\pm$ " + bgMCErr_ + endLine + newLine + hLine)
147
148 #write a line for each data sample
149 for sample in processed_datasets_channels[channel]:
150 if types[sample] is not "data":
151 continue
152 rawlabel = "$" + labels[sample] + "$"
153 label = rawlabel.replace("#","\\").replace("\\rightarrow","{\\rightarrow}").replace(" ","\\ ")
154 fout.write(label + " & " + yields[sample][channel] + " $\pm$ " + errors[sample][channel] + endLine + newLine)
155
156 fout.write("\\end{tabular}}"+newLine)
157 if(arguments.standAlone):
158 fout.write("\\end{document}"+newLine)
159
160 fout.close()
161
162 if arguments.standAlone:
163 #process tex files to make pdf files
164 command = "pdflatex -interaction=batchmode -output-directory=./%s %s > /dev/null" % (condor_dir,outputFile)
165 os.system(command)
166 os.system(command)
167 #os.system("rm %s" % outputFile)
168 os.system("rm %saux" % (outputFile.rstrip("tex")))
169 os.system("rm %slog" % (outputFile.rstrip("tex")))
170 print "Finished writing cutFlow to " + outputFile + " and compiling pdf"
171
172
173