ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeYieldsTables.py
Revision: 1.1
Committed: Thu Jun 27 14:58:55 2013 UTC (11 years, 10 months ago) by lantonel
Content type: text/x-python
Branch: MAIN
Log Message:
first commit of a script that produces a tex file with yields for each channel in condor output directory

File Contents

# User Rev Content
1 lantonel 1.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("-t")
22     parser.remove_option("-q")
23     parser.remove_option("-n")
24     parser.remove_option("-u")
25     parser.remove_option("-e")
26     parser.remove_option("-r")
27     parser.remove_option("-R")
28     parser.remove_option("-d")
29     parser.remove_option("-b")
30     parser.remove_option("--2D")
31     parser.remove_option("-y")
32     parser.remove_option("-p")
33    
34    
35     (arguments, args) = parser.parse_args()
36    
37    
38     if arguments.localConfig:
39     sys.path.append(os.getcwd())
40     exec("from " + arguments.localConfig.rstrip('.py') + " import *")
41    
42     #set condor directory
43     condor_dir = set_condor_output_dir(arguments)
44    
45    
46     from ROOT import TFile, TH1F, gDirectory
47    
48    
49     hLine = "\\hline\n"
50     endLine = " \\\\ "
51     newLine = " \n"
52    
53    
54     #### check which input datasets have valid output files
55     processed_datasets = []
56     for dataset in datasets:
57     if types[dataset] is "signalMC": #only include bgMC and data yields
58     continue
59     fileName = condor_dir + "/" + dataset + ".root"
60     if not os.path.exists(fileName):
61     continue
62     testFile = TFile(fileName)
63     if not (testFile.IsZombie()):
64     processed_datasets.append(dataset)
65    
66     #### exit if no datasets found
67     if len(processed_datasets) is 0:
68     sys.exit("Can't find any output root files for the given list of datasets")
69    
70    
71     #open the first ROOT file and get the list of channels
72     channels = []
73     dataset_file = "%s/%s.root" % (condor_dir,processed_datasets[0])
74     inputFile = TFile(dataset_file)
75     inputFile.cd("OSUAnalysis")
76    
77     for key in gDirectory.GetListOfKeys():
78     if (key.GetClassName() != "TDirectoryFile"):
79     continue
80     channels.append(key.GetName())
81    
82     #get and store the yields and errors for each dataset
83     yields = {}
84     errors = {}
85     bgMCSum = {}
86     bgMCErrSquared = {}
87    
88     for channel in channels:
89     bgMCSum[channel] = 0
90     bgMCErrSquared[channel] = 0
91    
92     for sample in processed_datasets:
93     yields[sample] = {}
94     errors[sample] = {}
95     dataset_file = "%s/%s.root" % (condor_dir,sample)
96     inputFile = TFile(dataset_file)
97     for channel in channels:
98     cutFlowHistogram = inputFile.Get("OSUAnalysis/"+channel+"CutFlow")
99    
100     yield_ = cutFlowHistogram.GetBinContent(cutFlowHistogram.GetNbinsX())
101     error_ = cutFlowHistogram.GetBinError(cutFlowHistogram.GetNbinsX())
102     yields[sample][channel] = formatNumber(str(round_sigfigs(yield_,3)).rstrip("0").rstrip("."))
103     errors[sample][channel] = formatNumber(str(round_sigfigs(error_,3)).rstrip("0").rstrip("."))
104    
105     if types[sample] is "bgMC":
106     bgMCSum[channel] = bgMCSum[channel] + yield_
107     bgMCErrSquared[channel] = bgMCErrSquared[channel] + error_*error_
108    
109     inputFile.Close()
110    
111    
112     #write a table for each channel to a separate tex file
113    
114     for channel in channels:
115     outputFile = condor_dir + "/yields_" + plainTextString(channel) + ".tex"
116     fout = open (outputFile, "w")
117     fout.write ("\\makebox[0pt]{\\renewcommand{\\arraystretch}{1.2}\\begin{tabular}{lr}"+newLine+hLine)
118    
119     fout.write("Event Source & Event Yield $\pm$ 1$\sigma$"+endLine+newLine+hLine)
120    
121     #write a line for each background sample
122     bgMCcounter = 0
123     for sample in processed_datasets:
124     if types[sample] is not "bgMC":
125     continue
126     bgMCcounter = bgMCcounter + 1
127     rawlabel = "$" + labels[sample] + "$"
128     label = rawlabel.replace("#","\\").replace("\\rightarrow","{\\rightarrow}").replace(" ","\\ ")
129     fout.write(label + " & " + yields[sample][channel] + " $\pm$ " + errors[sample][channel] + endLine + newLine)
130    
131     #write a line with the sum of the backgrounds
132     if bgMCcounter is not 0:
133     bgMCSum = formatNumber(str(round_sigfigs(bgMCSum[channel],3)).rstrip("0").rstrip("."))
134     bgMCErr = formatNumber(str(round_sigfigs(math.sqrt(bgMCErrSquared[channel]),3)).rstrip("0").rstrip("."))
135     fout.write(hLine+"background sum & " + bgMCSum + " $\pm$ " + bgMCErr + endLine + newLine + hLine)
136    
137     #write a line for each data sample
138     for sample in processed_datasets:
139     if types[sample] is not "data":
140     continue
141     rawlabel = "$" + labels[sample] + "$"
142     label = rawlabel.replace("#","\\").replace("\\rightarrow","{\\rightarrow}").replace(" ","\\ ")
143     fout.write(label + " & " + yields[sample][channel] + " $\pm$ " + errors[sample][channel] + endLine + newLine)
144    
145     fout.write("\\end{tabular}}")
146     fout.close()
147