ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py
(Generate patch)

Comparing UserCode/OSUT3Analysis/Configuration/scripts/makePlots.py (file contents):
Revision 1.35 by biliu, Mon May 27 20:34:12 2013 UTC vs.
Revision 1.36 by lantonel, Thu May 30 14:46:27 2013 UTC

# Line 10 | Line 10 | from OSUT3Analysis.Configuration.configu
10   from OSUT3Analysis.Configuration.processingUtilities import *
11  
12  
13 +
14 + ### parse the command-line options
15 +
16   parser = OptionParser()
17   parser = set_commandline_arguments(parser)
18   (arguments, args) = parser.parse_args()
# Line 19 | Line 22 | if arguments.localConfig:
22      exec("from " + arguments.localConfig.rstrip('.py') + " import *")
23  
24  
22 outputFileName = "stacked_histograms.root"
23 if arguments.outputFileName:
24    outputFileName = arguments.outputFileName
25
26 condor_dir = set_condor_output_dir(arguments)
27
28
29
25   #### deal with conflicting arguments
26   if arguments.normalizeToData and arguments.normalizeToUnitArea:
27      print "Conflicting normalizations requsted, will normalize to unit area"
# Line 39 | Line 34 | if arguments.makeRatioPlots and argument
34      print "You have requested both ratio and difference plots.  Will make just ratio plots instead"
35      arguments.makeRatioPlots = False
36  
37 < from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TLegendEntry, TArrow, THStack, TIter, TKey, TPaveLabel, gPad
37 >
38 > from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TLegendEntry, THStack, TIter, TKey, TPaveLabel, gPad
39 >
40 >
41 > ### setting ROOT options so our plots will look awesome and everyone will love us
42  
43   gROOT.SetBatch()
44   gStyle.SetOptStat(0)
# Line 48 | Line 47 | gStyle.SetPadBorderMode(0)
47   gStyle.SetPadColor(0)
48   gStyle.SetCanvasColor(0)
49   gStyle.SetTextFont(42)
50 + gStyle.SetCanvasDefH(600)
51 + gStyle.SetCanvasDefW(600)
52 + gStyle.SetCanvasDefX(0)
53 + gStyle.SetCanvasDefY(0)
54 + gStyle.SetPadTopMargin(0.05)
55 + gStyle.SetPadBottomMargin(0.13)
56 + gStyle.SetPadLeftMargin(0.15)
57 + gStyle.SetPadRightMargin(0.05)
58 + gStyle.SetTitleColor(1, "XYZ")
59 + gStyle.SetTitleFont(42, "XYZ")
60 + gStyle.SetTitleSize(0.05, "XYZ")
61 + gStyle.SetTitleXOffset(0.95)
62 + gStyle.SetTitleYOffset(1.25)
63 + gStyle.SetTextAlign(22)
64 + gStyle.SetLabelColor(1, "XYZ")
65 + gStyle.SetLabelFont(42, "XYZ")
66 + gStyle.SetLabelOffset(0.007, "XYZ")
67 + gStyle.SetLabelSize(0.05, "XYZ")
68 + gStyle.SetAxisColor(1, "XYZ")
69 + gStyle.SetStripDecimals(True)
70 + gStyle.SetTickLength(0.03, "XYZ")
71 + gStyle.SetNdivisions(510, "XYZ")
72 + gStyle.SetPadTickX(1)
73 + gStyle.SetPadTickY(1)
74   gROOT.ForceStyle()
52 outputFile = TFile(condor_dir + "/" + outputFileName, "RECREATE")
75  
76  
77 < channels = {}
78 < processed_datasets = []
79 < #### check which input datasets have valid output files
80 < for sample in datasets:
81 <    fileName = condor_dir + "/" + sample + ".root"
82 <    if not os.path.exists(fileName):
83 <        continue
62 <    testFile = TFile(fileName)
63 <    if testFile.IsZombie() or not testFile.GetNkeys():
64 <        continue
65 <    processed_datasets.append(sample)
77 > #set the text for the luminosity label
78 > if(intLumi < 1000.):
79 >    LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
80 >    LumiText = "L_{int} = " + str.format('{0:.1f}', LumiInPb) + " pb^{-1}"
81 > else:
82 >    LumiInFb = intLumi/1000.
83 >    LumiText = "L_{int} = " + str.format('{0:.1f}', LumiInFb) + " fb^{-1}"
84  
85 < if len(processed_datasets) is 0:
86 <    sys.exit("No datasets have been processed")
85 >    
86 > #bestest place for lumi. label, in top left corner
87 > topLeft_x_left    = 0.1375839
88 > topLeft_x_right   = 0.4580537
89 > topLeft_y_bottom  = 0.8479021
90 > topLeft_y_top     = 0.9475524
91 > topLeft_y_offset  = 0.035
92 >                                                                        
93 >                                                                            
94 > ##########################################################################################################################################
95 > ##########################################################################################################################################
96 > ##########################################################################################################################################
97  
98 < #### open first input file and re-make its directory structure in the output file
71 < testFile = TFile(condor_dir + "/" + processed_datasets[0] + ".root")
72 < testFile.cd()
73 <
74 < #To find whether there are a third layer in directories.
75 < GetPlotsAfterEachCut = True
76 <
77 < for key in testFile.GetListOfKeys():
78 <        if (key.GetClassName() != "TDirectoryFile"):
79 <            continue
80 <        testFile.cd(key.GetName())
81 <        for key2 in gDirectory.GetListOfKeys():
82 <            if (key2.GetClassName() != "TDirectoryFile"):
83 <                continue
84 <            testFile.cd(key.GetName()+"/"+key2.GetName())
85 <            for key3 in gDirectory.GetListOfKeys():
86 <                if (key3.GetClassName() != "TDirectoryFile"):
87 <                    GetPlotsAfterEachCut = False
98 > def MakeOneDHist(pathToDir,histogramName):
99  
100 < if GetPlotsAfterEachCut:
101 <    for key in testFile.GetListOfKeys():
102 <        if (key.GetClassName() != "TDirectoryFile"):
103 <            continue
104 <        outputFile.cd()
105 <        outputFile.mkdir(key.GetName())
95 <        rootDirectory = key.GetName()
96 <    
97 <        testFile.cd(key.GetName())
98 <        for key2 in gDirectory.GetListOfKeys():
99 <            if (key2.GetClassName() != "TDirectoryFile"):
100 <                continue
101 <            outputFile.cd(key.GetName())
102 <            gDirectory.mkdir(key2.GetName())
103 <            channels[key2.GetName()] = []
104 <            testFile.cd(key.GetName()+"/"+key2.GetName())
105 <            for key3 in gDirectory.GetListOfKeys():
106 <                if (key3.GetClassName() != "TDirectoryFile"):
107 <                    continue
108 <                outputFile.cd(key.GetName()+"/"+key2.GetName())
109 <                gDirectory.mkdir(key3.GetName())
110 <                channels[key2.GetName()].append("/"+key3.GetName())
111 < else:
112 <    for key in testFile.GetListOfKeys():
113 <        if (key.GetClassName() != "TDirectoryFile"):
114 <            continue
115 <        outputFile.cd()
116 <        outputFile.mkdir(key.GetName())
117 <        rootDirectory = key.GetName()
118 <
119 <        testFile.cd(key.GetName())
120 <        for key2 in gDirectory.GetListOfKeys():
121 <            if (key2.GetClassName() != "TDirectoryFile"):
122 <                continue
123 <            outputFile.cd(key.GetName())
124 <            gDirectory.mkdir(key2.GetName())
125 <            channels[key2.GetName()] = []
126 <            
127 < ## weight = intLumi / 10000.0
128 < ## for dataset in processed_datasets:
129 < ##     dataset_file = "%s/%s.root" % (condor_dir,dataset)
130 < ##     fin = TFile (dataset_file)
131 < ##     flags = fin.Get ("flags")
132 < ##     noWeights = flags and flags.GetBinContent (1)
133 < ##     fin.Close ()
134 <
135 < ##     if types[dataset] != "data" and not noWeights:
136 < ##         os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", weight))
137 < ##     else:
138 < ##         os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", 1.0))
139 < def MakeOneDHist(histogramname,channel,cut):
140 <            numBgMCSamples = 0
141 <            numDataSamples = 0
142 <            numSignalSamples = 0
100 >    
101 >    numBgMCSamples = 0
102 >    numDataSamples = 0
103 >    numSignalSamples = 0
104 >    
105 >    Stack = THStack("stack",histogramName)
106  
107 <            Stack = THStack("stack",histogramName)
107 >    LumiLabel = TPaveLabel(topLeft_x_left,topLeft_y_bottom,topLeft_x_right,topLeft_y_top,LumiText,"NDC")
108 >    LumiLabel.SetBorderSize(0)
109 >    LumiLabel.SetTextSize(0.32)
110 >    LumiLabel.SetFillColor(0)
111 >    LumiLabel.SetFillStyle(0)
112 >    
113 >    NormLabel = TPaveLabel()
114 >    NormLabel.SetDrawOption("NDC")
115 >    NormLabel.SetX1NDC(topLeft_x_left)
116 >    NormLabel.SetX2NDC(topLeft_x_right)
117 >    
118 >    NormLabel.SetBorderSize(0)
119 >    NormLabel.SetTextSize(0.32)
120 >    NormLabel.SetFillColor(0)
121 >    NormLabel.SetFillStyle(0)
122 >    
123 >    NormText = ""
124 >    if arguments.normalizeToUnitArea:
125 >        NormText = "Scaled to unit area"
126 >    elif arguments.normalizeToData:
127 >        NormText = "MC scaled to data"
128 >        NormLabel.SetLabel(NormText)
129          
130 <            if(intLumi < 1000.):
131 <                LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
132 <            else:
133 <                getcontext().prec = 2
134 <                LumiInFb = intLumi/1000.
135 <                LumiText = "L_{int} = " + str(LumiInFb) + " fb^{-1}"
130 >        
131 >    BgMCLegend = TLegend()
132 >    BgTitle = BgMCLegend.AddEntry(0, "Data & Bkgd. MC", "H")
133 >    BgTitle.SetTextAlign(22)
134 >    BgTitle.SetTextFont(62)
135 >    BgMCLegend.SetBorderSize(0)
136 >    BgMCLegend.SetFillColor(0)
137 >    BgMCLegend.SetFillStyle(0)
138 >    SignalMCLegend = TLegend()
139 >    SignalTitle = SignalMCLegend.AddEntry(0, "Signal MC", "H")
140 >    SignalTitle.SetTextAlign(22)
141 >    SignalTitle.SetTextFont(62)
142 >    SignalMCLegend.SetBorderSize(0)
143 >    SignalMCLegend.SetFillColor(0)
144 >    SignalMCLegend.SetFillStyle(0)
145 >    
146 >    outputFile.cd(pathToDir)
147 >    Canvas = TCanvas(histogramName)
148 >    BgMCHistograms = []
149 >    BgMCLegendEntries = []
150 >    SignalMCHistograms = []
151 >    SignalMCLegendEntries = []
152 >    DataHistograms = []
153 >    DataLegendEntries = []
154 >    
155 >    
156 >    backgroundIntegral = 0
157 >    dataIntegral = 0
158 >    scaleFactor = 1
159 >    
160 >    for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
161 >        dataset_file = "%s/%s.root" % (condor_dir,sample)
162 >        inputFile = TFile(dataset_file)
163 >        Histogram = inputFile.Get(pathToDir+"/"+histogramName).Clone()
164 >        Histogram.SetDirectory(0)
165 >        inputFile.Close()
166 >        if arguments.rebinFactor:
167 >            RebinFactor = int(arguments.rebinFactor)
168 >            #don't rebin histograms which will have less than 5 bins or any gen-matching histograms
169 >            if Histogram.GetNbinsX() >= RebinFactor*5 and Histogram.GetTitle.find("GenMatch") is -1:
170 >                Histogram.Rebin(RebinFactor)
171 >
172 >        xAxisLabel = Histogram.GetXaxis().GetTitle()
173 >        histoTitle = Histogram.GetTitle()
174 >
175 >        legLabel = labels[sample]
176 >        if (arguments.printYields):
177 >            yieldHist = Histogram.Integral()
178 >            legLabel = legLabel + " (%.1f)" % yieldHist
179  
180 <            LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
154 <            LumiLabel.SetBorderSize(0)
155 <            LumiLabel.SetFillColor(0)
156 <            LumiLabel.SetFillStyle(0)
157 <            
158 <            BgMCLegend = TLegend()
159 <            BgTitle = BgMCLegend.AddEntry(0, "Data & Bkgd. MC", "H")
160 <            BgTitle.SetTextAlign(21)
161 <            BgTitle.SetTextFont(62)
162 <            BgMCLegend.SetBorderSize(0)
163 <            BgMCLegend.SetFillColor(0)
164 <            BgMCLegend.SetFillStyle(0)
165 <            SignalMCLegend = TLegend()
166 <            SignalTitle = SignalMCLegend.AddEntry(0, "Signal MC", "H")
167 <            SignalTitle.SetTextAlign(21)
168 <            SignalTitle.SetTextFont(62)
169 <            SignalMCLegend.SetBorderSize(0)
170 <            SignalMCLegend.SetFillColor(0)
171 <            SignalMCLegend.SetFillStyle(0)
172 <            
173 <            outputFile.cd(rootDirectory+"/"+channel+cut)
174 <            Canvas = TCanvas(histogramName)
175 <            BgMCHistograms = []
176 <            BgMCLegendEntries = []
177 <            SignalMCHistograms = []
178 <            SignalMCLegendEntries = []
179 <            DataHistograms = []
180 <            DataLegendEntries = []
180 >        if( types[sample] == "bgMC"):
181              
182 +            numBgMCSamples += 1
183 +            backgroundIntegral += Histogram.Integral()
184              
185 <            backgroundIntegral = 0
186 <            dataIntegral = 0
187 <            scaleFactor = 1
188 <            
189 <            for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
190 <                dataset_file = "%s/%s.root" % (condor_dir,sample)
191 <                inputFile = TFile(dataset_file)
192 <                Histogram = inputFile.Get(rootDirectory+"/"+channel+cut+"/"+histogramName).Clone()
193 <                Histogram.SetDirectory(0)
194 <                if arguments.rebinFactor:
193 <                    RebinFactor = int(arguments.rebinFactor)
194 <                    if Histogram.GetNbinsX() >= RebinFactor*10:
195 <                        Histogram.Rebin(RebinFactor)
196 <                inputFile.Close()
197 <                xAxisLabel = Histogram.GetXaxis().GetTitle()
198 <                histoTitle = Histogram.GetTitle()
199 <
200 <                legLabel = labels[sample]
201 <                if (arguments.printYields):
202 <                    yieldHist = Histogram.Integral()
203 <                    legLabel = legLabel + " (%.1f)" % yieldHist
204 <
205 <                if( types[sample] == "bgMC"):
206 <                    
207 <                    numBgMCSamples += 1
208 <                    backgroundIntegral += Histogram.Integral()
209 <
210 <                    Histogram.SetLineStyle(1)
211 <                    if(arguments.noStack):
212 <                        Histogram.SetFillStyle(0)
213 <                        Histogram.SetLineColor(colors[sample])
214 <                        Histogram.SetLineWidth(2)
215 <                    else:
216 <                        Histogram.SetFillStyle(1001)
217 <                        Histogram.SetFillColor(colors[sample])
218 <                        Histogram.SetLineColor(1)
219 <                        Histogram.SetLineWidth(1)
185 >            Histogram.SetLineStyle(1)
186 >            if(arguments.noStack):
187 >                Histogram.SetFillStyle(0)
188 >                Histogram.SetLineColor(colors[sample])
189 >                Histogram.SetLineWidth(2)
190 >            else:
191 >                Histogram.SetFillStyle(1001)
192 >                Histogram.SetFillColor(colors[sample])
193 >                Histogram.SetLineColor(1)
194 >                Histogram.SetLineWidth(1)
195  
196 <                    BgMCLegendEntries.append(legLabel)
197 <                    BgMCHistograms.append(Histogram)
196 >                BgMCLegendEntries.append(legLabel)
197 >                BgMCHistograms.append(Histogram)
198  
199                      
200 <                elif( types[sample] == "signalMC"):
200 >        elif( types[sample] == "signalMC"):
201 >            
202 >            numSignalSamples += 1
203 >            
204 >            Histogram.SetFillStyle(0)
205 >            Histogram.SetLineColor(colors[sample])
206 >            Histogram.SetLineStyle(1)
207 >            Histogram.SetLineWidth(2)
208 >            if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
209 >                Histogram.Scale(1./Histogram.Integral())
210 >                
211 >            SignalMCLegendEntries.append(legLabel)
212 >            SignalMCHistograms.append(Histogram)
213                      
214 <                    numSignalSamples += 1
214 >        elif( types[sample] == "data"):
215  
216 <                    Histogram.SetFillStyle(0)
217 <                    Histogram.SetLineColor(colors[sample])
218 <                    Histogram.SetLineStyle(1)
219 <                    Histogram.SetLineWidth(2)
220 <                    if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
221 <                        Histogram.Scale(1./Histogram.Integral())
216 >            numDataSamples += 1
217 >            dataIntegral += Histogram.Integral()
218 >            
219 >            Histogram.SetFillStyle(0)
220 >            Histogram.SetLineColor(colors[sample])
221 >            Histogram.SetLineStyle(1)
222 >            Histogram.SetLineWidth(2)
223 >            if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
224 >                Histogram.Scale(1./Histogram.Integral())
225  
226 <                    SignalMCLegendEntries.append(legLabel)
227 <                    SignalMCHistograms.append(Histogram)
226 >            DataLegendEntries.append(legLabel)
227 >            DataHistograms.append(Histogram)
228                      
229 <                elif( types[sample] == "data"):
229 >    #scaling histograms as per user's specifications
230 >    if dataIntegral > 0 and backgroundIntegral > 0:
231 >        scaleFactor = dataIntegral/backgroundIntegral
232 >    for bgMCHist in BgMCHistograms:
233 >        if arguments.normalizeToData:
234 >            bgMCHist.Scale(scaleFactor)
235 >
236 >        if arguments.normalizeToUnitArea and not arguments.noStack and backgroundIntegral > 0:
237 >            bgMCHist.Scale(1./backgroundIntegral)
238 >        elif arguments.normalizeToUnitArea and arguments.noStack and bgMCHist.Integral() > 0:
239 >            bgMCHist.Scale(1./bgMCHist.Integral())
240  
241 <                    numDataSamples += 1
242 <                    dataIntegral += Histogram.Integral()
243 <
244 <                    Histogram.SetFillStyle(0)
245 <                    Histogram.SetLineColor(colors[sample])
246 <                    Histogram.SetLineStyle(1)
247 <                    Histogram.SetLineWidth(2)
248 <                    if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
249 <                        Histogram.Scale(1./Histogram.Integral())
250 <
251 <                    DataLegendEntries.append(legLabel)
252 <                    DataHistograms.append(Histogram)
253 <                    
254 <            if dataIntegral > 0 and backgroundIntegral > 0:
255 <                scaleFactor = dataIntegral/backgroundIntegral
256 <            for bgMCHist in BgMCHistograms:
257 <                if arguments.normalizeToData:
258 <                    bgMCHist.Scale(scaleFactor)
259 <                if arguments.normalizeToUnitArea and not arguments.noStack and backgroundIntegral > 0:
260 <                    bgMCHist.Scale(1./backgroundIntegral)
261 <                elif arguments.normalizeToUnitArea and arguments.noStack and bgMCHist.Integral() > 0:
262 <                    bgMCHist.Scale(1./bgMCHist.Integral())
263 <                if not arguments.noStack:
264 <                    Stack.Add(bgMCHist)
241 >        if not arguments.noStack:
242 >            Stack.Add(bgMCHist)
243  
244                  
245  
246 +    ### formatting data histograms and adding to legend
247 +    legendIndex = 0
248 +    for Histogram in DataHistograms:
249 +        BgMCLegend.AddEntry(Histogram,DataLegendEntries[legendIndex],"LEP").SetTextFont (42)
250 +        legendIndex = legendIndex+1
251 +
252 +
253 +    ### creating the histogram to represent the statistical errors on the stack
254 +    if numBgMCSamples is not 0 and not arguments.noStack:
255 +        ErrorHisto = BgMCHistograms[0].Clone("errors")
256 +        ErrorHisto.SetFillStyle(3001)
257 +        ErrorHisto.SetFillColor(13)
258 +        ErrorHisto.SetLineWidth(0)
259 +        BgMCLegend.AddEntry(ErrorHisto,"Stat. Errors","F").SetTextFont (42)
260 +        for Histogram in BgMCHistograms:
261 +            if Histogram is not BgMCHistograms[0]:
262 +                ErrorHisto.Add(Histogram)
263 +
264 +
265 +    ### formatting bgMC histograms and adding to legend
266 +    legendIndex = numBgMCSamples-1
267 +    for Histogram in reversed(BgMCHistograms):
268 +        if(arguments.noStack):
269 +            BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[legendIndex],"L").SetTextFont (42)
270 +        else:
271 +            BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[legendIndex],"F").SetTextFont (42)
272 +        legendIndex = legendIndex-1
273 +
274 +
275 +    ### formatting signalMC histograms and adding to legend
276 +    legendIndex = 0
277 +    for Histogram in SignalMCHistograms:
278 +        SignalMCLegend.AddEntry(Histogram,SignalMCLegendEntries[legendIndex],"L").SetTextFont (42)
279 +        legendIndex = legendIndex+1
280 +
281 +
282 +    ### finding the maximum value of anything going on the canvas, so we know how to set the y-axis
283 +    finalMax = 0
284 +    if numBgMCSamples is not 0 and not arguments.noStack:
285 +        finalMax = ErrorHisto.GetMaximum() + ErrorHisto.GetBinError(ErrorHisto.GetMaximumBin())
286 +    else:
287 +        for bgMCHist in BgMCHistograms:
288 +            if(bgMCHist.GetMaximum() > finalMax):
289 +                finalMax = bgMCHist.GetMaximum()
290 +    for signalMCHist in SignalMCHistograms:
291 +        if(signalMCHist.GetMaximum() > finalMax):
292 +            finalMax = signalMCHist.GetMaximum()
293 +    for dataHist in DataHistograms:
294 +        if(dataHist.GetMaximum() > finalMax):
295 +            finalMax = dataHist.GetMaximum() + dataHist.GetBinError(dataHist.GetMaximumBin())
296 +    finalMax = 1.15*finalMax
297  
298  
299 <            ### formatting data histograms and adding to legend
271 <            counter = 0
272 <            for Histogram in DataHistograms:
273 <                    BgMCLegend.AddEntry(Histogram,DataLegendEntries[counter],"LEP").SetTextFont (42)
274 <                    counter = counter+1
275 <
276 <
277 <            ### creating the histogram to represent the statistical errors on the stack
278 <            if numBgMCSamples is not 0 and not arguments.noStack:
279 <                ErrorHisto = BgMCHistograms[0].Clone("errors")
280 <                ErrorHisto.SetFillStyle(3001)
281 <                ErrorHisto.SetFillColor(13)
282 <                ErrorHisto.SetLineWidth(0)
283 <                BgMCLegend.AddEntry(ErrorHisto,"Stat. Errors","F").SetTextFont (42)
284 <                for Histogram in BgMCHistograms:
285 <                    if Histogram is not BgMCHistograms[0]:
286 <                        ErrorHisto.Add(Histogram)
287 <
288 <
289 <            ### formatting bgMC histograms and adding to legend
290 <            counter = numBgMCSamples-1
291 <            for Histogram in reversed(BgMCHistograms):
292 <                    if(arguments.noStack):
293 <                        BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[counter],"L").SetTextFont (42)
294 <                    else:
295 <                        BgMCLegend.AddEntry(Histogram,BgMCLegendEntries[counter],"F").SetTextFont (42)
296 <                    counter = counter-1
297 <
298 <            ### formatting signalMC histograms and adding to legend
299 <            counter = 0
300 <            for Histogram in SignalMCHistograms:
301 <                    SignalMCLegend.AddEntry(Histogram,SignalMCLegendEntries[counter],"L").SetTextFont (42)
302 <                    counter = counter+1
303 <
304 <
305 <
306 <            ### finding the maximum value of anything going on the canvas, and know how to set the y-axis
307 <            finalMax = 0
308 <            if numBgMCSamples is not 0 and not arguments.noStack:
309 <                finalMax = ErrorHisto.GetMaximum() + ErrorHisto.GetBinError(ErrorHisto.GetMaximumBin())
310 <            else:
311 <                for bgMCHist in BgMCHistograms:
312 <                    if(bgMCHist.GetMaximum() > finalMax):
313 <                        finalMax = bgMCHist.GetMaximum()
314 <            for signalMCHist in SignalMCHistograms:
315 <                if(signalMCHist.GetMaximum() > finalMax):
316 <                    finalMax = signalMCHist.GetMaximum()
317 <            for dataHist in DataHistograms:
318 <                if(dataHist.GetMaximum() > finalMax):
319 <                    finalMax = dataHist.GetMaximum() + dataHist.GetBinError(dataHist.GetMaximumBin())
320 <            finalMax = 1.1*finalMax
321 <
322 <
323 <            ### Drawing histograms to canvas
299 >    ### Drawing histograms to canvas
300  
301 +    outputFile.cd(pathToDir)        
302 +        
303 +    makeRatioPlots = arguments.makeRatioPlots
304 +    makeDiffPlots = arguments.makeDiffPlots
305  
306 <            outputFile.cd(rootDirectory+"/"+channel+cut)        
306 >    if numBgMCSamples is 0 or numDataSamples is not 1:
307 >        makeRatioPlots = False
308 >        makeDiffPlots = False
309 >    if makeRatioPlots or makeDiffPlots:
310 >        Canvas.SetFillStyle(0)
311 >        Canvas.Divide(1,2)
312 >        Canvas.cd(1)
313 >        gPad.SetPad(0.01,0.25,0.99,0.99)
314 >        gPad.SetMargin(0.1,0.05,0.02,0.07)
315 >        gPad.SetFillStyle(0)
316 >        gPad.Update()
317 >        gPad.Draw()
318 >        Canvas.cd(2)
319 >        gPad.SetPad(0.01,0.01,0.99,0.25)
320 >        #format: gPad.SetMargin(l,r,b,t)
321 >        gPad.SetMargin(0.1,0.05,0.4,0.02)
322 >        gPad.SetFillStyle(0)
323 >        gPad.SetGridy(1)
324 >        gPad.Update()
325 >        gPad.Draw()
326 >        
327 >        Canvas.cd(1)
328  
329 <            makeRatioPlots = arguments.makeRatioPlots
329 <            makeDiffPlots = arguments.makeDiffPlots
329 >    if numBgMCSamples is not 0: # the first thing to draw to the canvas is a bgMC sample
330  
331 <            if numBgMCSamples is 0 or numDataSamples is not 1:
332 <                makeRatioPlots = False
333 <                makeDiffPlots = False
331 >        
332 >        if not arguments.noStack: # draw unstacked background samples
333 >            Stack.SetTitle(histoTitle)
334 >            Stack.Draw("HIST")
335 >            Stack.GetXaxis().SetTitle(xAxisLabel)
336 >            Stack.SetMaximum(finalMax)
337 >            Stack.SetMinimum(0.0001)
338              if makeRatioPlots or makeDiffPlots:
339 <                Canvas.SetFillStyle(0)
340 <                Canvas.Divide(1,2)
341 <                Canvas.cd(1)
342 <                gPad.SetPad(0.01,0.25,0.99,0.99)
343 <                gPad.SetMargin(0.1,0.05,0.02,0.07)
344 <                gPad.SetFillStyle(0)
345 <                gPad.Update()
346 <                gPad.Draw()
347 <                Canvas.cd(2)
348 <                gPad.SetPad(0.01,0.01,0.99,0.25)
349 <                #format: gPad.SetMargin(l,r,b,t)
350 <                gPad.SetMargin(0.1,0.05,0.4,0.02)
351 <                gPad.SetFillStyle(0)
352 <                gPad.SetGridy(1)
353 <                gPad.Update()
354 <                gPad.Draw()
355 <
352 <                Canvas.cd(1)
353 <
354 <            if numBgMCSamples is not 0:
355 <
356 <                if not arguments.noStack:
357 <                    Stack.SetTitle(histoTitle)
358 <                    Stack.Draw("HIST")
359 <                    Stack.GetXaxis().SetTitle(xAxisLabel)
360 <                    Stack.SetMaximum(finalMax)
361 <                    Stack.SetMinimum(0.0001)
362 <                    if makeRatioPlots or makeDiffPlots:
363 <                        Stack.GetHistogram().GetXaxis().SetLabelSize(0)
364 <                    #draw shaded error bands
365 <                    ErrorHisto.Draw("A E2 SAME")
339 >                Stack.GetHistogram().GetXaxis().SetLabelSize(0)
340 >            #draw shaded error bands
341 >            ErrorHisto.Draw("A E2 SAME")
342 >                
343 >        else: #draw the stacked backgrounds
344 >            BgMCHistograms[0].SetTitle(histoTitle)
345 >            BgMCHistograms[0].Draw("HIST")
346 >            BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
347 >            BgMCHistograms[0].SetMaximum(finalMax)
348 >            BgMCHistograms[0].SetMinimum(0.0001)
349 >            for bgMCHist in BgMCHistograms:
350 >                bgMCHist.Draw("A HIST SAME")
351 >
352 >        for signalMCHist in SignalMCHistograms:
353 >            signalMCHist.Draw("A HIST SAME")
354 >        for dataHist in DataHistograms:
355 >            dataHist.Draw("A E SAME")
356  
367                    
368                else:
369                    BgMCHistograms[0].SetTitle(histoTitle)
370                    BgMCHistograms[0].Draw("HIST")
371                    BgMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
372                    BgMCHistograms[0].SetMaximum(finalMax)
373                    BgMCHistograms[0].SetMinimum(0.0001)
374                    for bgMCHist in BgMCHistograms:
375                        bgMCHist.Draw("A HIST SAME")
376                for signalMCHist in SignalMCHistograms:
377                    signalMCHist.Draw("A HIST SAME")
378                for dataHist in DataHistograms:
379                    dataHist.Draw("A E SAME")
357                                  
358 <            elif numSignalSamples is not 0:
359 <                SignalMCHistograms[0].SetTitle(histoTitle)
360 <                SignalMCHistograms[0].Draw("HIST")
361 <                SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
362 <                SignalMCHistograms[0].SetMaximum(finalMax)
363 <                SignalMCHistograms[0].SetMinimum(0.0001)
364 <                for signalMCHist in SignalMCHistograms:
365 <                    if(signalMCHist is not SignalMCHistograms[0]):
366 <                        signalMCHist.Draw("A HIST SAME")
367 <                for dataHist in DataHistograms:
368 <                    dataHist.Draw("A E SAME")
358 >    elif numSignalSamples is not 0: # the first thing to draw to the canvas is a signalMC sample
359 >        SignalMCHistograms[0].SetTitle(histoTitle)
360 >        SignalMCHistograms[0].Draw("HIST")
361 >        SignalMCHistograms[0].GetXaxis().SetTitle(xAxisLabel)
362 >        SignalMCHistograms[0].SetMaximum(finalMax)
363 >        SignalMCHistograms[0].SetMinimum(0.0001)
364 >
365 >        for signalMCHist in SignalMCHistograms:
366 >            if(signalMCHist is not SignalMCHistograms[0]):
367 >                signalMCHist.Draw("A HIST SAME")
368 >        for dataHist in DataHistograms:
369 >            dataHist.Draw("A E SAME")
370 >
371 >
372 >    elif(numDataSamples is not 0): # the first thing to draw to the canvas is a data sample
373 >        DataHistograms[0].SetTitle(histoTitle)
374 >        DataHistograms[0].Draw("E")
375 >        DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
376 >        DataHistograms[0].SetMaximum(finalMax)
377 >        DataHistograms[0].SetMinimum(0.0001)
378 >        for dataHist in DataHistograms:
379 >            if(dataHist is not DataHistograms[0]):
380 >                dataHist.Draw("A E SAME")
381  
393            elif(numDataSamples is not 0):
394                DataHistograms[0].SetTitle(histoTitle)
395                DataHistograms[0].Draw("E")
396                DataHistograms[0].GetXaxis().SetTitle(xAxisLabel)
397                DataHistograms[0].SetMaximum(finalMax)
398                DataHistograms[0].SetMinimum(0.0001)
399                for dataHist in DataHistograms:
400                    if(dataHist is not DataHistograms[0]):
401                        dataHist.Draw("A E SAME")
382  
383 +
384 +    #legend coordinates, empirically determined :-)
385 +    x_left = 0.6761745
386 +    x_right = 0.9328859
387 +    x_width = x_right - x_left
388 +    y_max = 0.9335664
389 +    entry_height = 0.05
390 +
391 +    if(numBgMCSamples is not 0 or numDataSamples is not 0): #then draw the data & bgMC legend
392 +
393 +        numExtraEntries = 1 # count the legend title
394 +        BgMCLegend.SetX1NDC(x_left)
395 +        if numBgMCSamples > 0:
396 +            numExtraEntries = numExtraEntries + 1 # count the stat. errors entry
397              
398 <            if(numBgMCSamples is not 0 or numDataSamples is not 0):
399 <                BgMCLegend.SetX1NDC(0.75)
400 <                if numBgMCSamples is 0:
401 <                    BgMCLegend.SetY1NDC(0.9-0.05*(1+numDataSamples))
398 >        BgMCLegend.SetY1NDC(y_max-entry_height*(numExtraEntries+numBgMCSamples+numDataSamples))
399 >        BgMCLegend.SetX2NDC(x_right)
400 >        BgMCLegend.SetY2NDC(y_max)
401 >        BgMCLegend.Draw()
402 >
403 >        if(numSignalSamples is not 0): #then draw the signalMC legend to the left of the other one
404 >            SignalMCLegend.SetX1NDC(x_left-x_width)
405 >            SignalMCLegend.SetY1NDC(y_max-entry_height*(1+numSignalSamples)) # add one for the title
406 >            SignalMCLegend.SetX2NDC(x_left)
407 >            SignalMCLegend.SetY2NDC(y_max)
408 >            SignalMCLegend.Draw()
409 >
410 >    elif numSignalSamples is not 0: #draw the signalMC legend in the upper right corner
411 >        SignalMCLegend.SetX1NDC(x_left)
412 >        SignalMCLegend.SetY1NDC(y_max-entry_height*(1+numSignalSamples)) # add one for the title
413 >        SignalMCLegend.SetX2NDC(x_right)
414 >        SignalMCLegend.SetY2NDC(y_max)
415 >        SignalMCLegend.Draw()
416 >
417 >
418 >    if not arguments.normalizeToUnitArea or numDataSamples > 0: #don't draw the lumi label if there's no data and it's scaled to unit area
419 >        LumiLabel.Draw()
420 >        if arguments.normalizeToUnitArea or arguments.normalizeToData:
421 >            #move the normalization label down before drawing if we drew the lumi. label
422 >            NormLabel.SetY1NDC(topLeft_y_bottom-topLeft_y_offset)
423 >            NormLabel.SetY2NDC(topLeft_y_top-topLeft_y_offset)
424 >            NormLabel.Draw()
425 >
426 >    elif arguments.normalizeToUnitArea or arguments.normalizeToData:
427 >        NormLabel.SetY1NDC(topLeft_y_bottom)
428 >        NormLabel.SetY2NDC(topLeft_y_top)
429 >        NormLabel.Draw()
430 >
431 >
432 >    if makeRatioPlots or makeDiffPlots:
433 >        Canvas.cd(2)
434 >        BgSum = Stack.GetStack().Last()
435 >        Comparison = DataHistograms[0].Clone()
436 >        Comparison.Add(BgSum,-1)
437 >        if not makeDiffPlots:        
438 >            Comparison.Divide(BgSum)
439 >        Comparison.SetTitle("")
440 >        Comparison.GetXaxis().SetTitle(xAxisLabel)
441 >        if makeRatioPlots:
442 >            Comparison.GetYaxis().SetTitle("#frac{Data-MC}{MC}")
443 >        elif makeDiffPlots:
444 >            Comparison.GetYaxis().SetTitle("Data-MC")
445 >        Comparison.GetYaxis().CenterTitle()
446 >        Comparison.GetYaxis().SetTitleSize(0.1)
447 >        Comparison.GetYaxis().SetTitleOffset(0.35)                
448 >        Comparison.GetXaxis().SetTitleSize(0.15)
449 >        Comparison.GetYaxis().SetLabelSize(0.1)
450 >        Comparison.GetXaxis().SetLabelSize(0.15)
451 >        if makeRatioPlots:
452 >            Comparison.GetYaxis().SetRangeUser(-1.15,1.15)
453 >        elif makeDiffPlots:
454 >            YMax = Comparison.GetMaximum()
455 >            YMin = Comparison.GetMinimum()
456 >            if YMax <= 0 and YMin <= 0:
457 >                Comparison.GetYaxis().SetRangeUser(-1.2*YMin,0)
458 >            elif YMax >= 0 and YMin >= 0:
459 >                Comparison.GetYaxis().SetRangeUser(0,1.2*YMax)
460 >            else: #axis crosses y=0
461 >                if abs(YMax) > abs(YMin):
462 >                    Comparison.GetYaxis().SetRangeUser(-1.2*YMax,1.2*YMax)
463                  else:
464 <                    BgMCLegend.SetY1NDC(0.9-0.05*(2+numBgMCSamples+numDataSamples))
465 <                BgMCLegend.SetX2NDC(0.9)
466 <                BgMCLegend.SetY2NDC(0.9)
467 <                BgMCLegend.Draw()
413 <                if(numSignalSamples is not 0):
414 <                    SignalMCLegend.SetX1NDC(0.6)
415 <                    SignalMCLegend.SetY1NDC(0.9-0.05*(1+numSignalSamples))
416 <                    SignalMCLegend.SetX2NDC(0.75)
417 <                    SignalMCLegend.SetY2NDC(0.9)
418 <                    SignalMCLegend.Draw()
419 <            elif numSignalSamples is not 0:
420 <                SignalMCLegend.SetX1NDC(0.75)
421 <                SignalMCLegend.SetY1NDC(0.9-0.05*(1+numSignalSamples))
422 <                SignalMCLegend.SetX2NDC(0.9)
423 <                SignalMCLegend.SetY2NDC(0.9)
424 <                SignalMCLegend.Draw()
464 >                    Comparison.GetYaxis().SetRangeUser(-1.2*YMin,1.2*YMin)
465 >                            
466 >        Comparison.GetYaxis().SetNdivisions(205)
467 >        Comparison.Draw()
468  
469 <            if not arguments.normalizeToUnitArea or numDataSamples > 0:
470 <                LumiLabel.Draw()
471 <            if arguments.normalizeToData and numBgMCSamples > 0 and numDataSamples > 0:
472 <                if numSignalSamples is 0:
430 <                    NormLabel = TPaveLabel(0.6,0.85,0.75,0.9,"MC scaled to data","NDC")
431 <                else:
432 <                    NormLabel = TPaveLabel(0.45,0.85,0.6,0.9,"MC scaled to data","NDC")
433 <                NormLabel.SetBorderSize(0)
434 <                NormLabel.SetFillColor(0)
435 <                NormLabel.SetFillStyle(0)
436 <                NormLabel.Draw()
437 <            elif arguments.normalizeToUnitArea:
438 <                if (numBgMCSamples is not 0 or numDataSamples is not 0) and numSignalSamples is 0:
439 <                    NormLabel = TPaveLabel(0.6,0.85,0.75,0.9,"Scaled to unit area","NDC")
440 <                else:
441 <                    NormLabel = TPaveLabel(0.45,0.85,0.6,0.9,"Scaled to unit area","NDC")
442 <                NormLabel.SetBorderSize(0)
443 <                NormLabel.SetFillColor(0)
444 <                NormLabel.SetFillStyle(0)
445 <                NormLabel.Draw()
469 >    Canvas.Write()
470 >    if arguments.savePDFs:
471 >        pathToDirString = pathToDir.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')
472 >        Canvas.SaveAs(condor_dir+"/stacked_histograms_pdfs/"+pathToDirString+"/"+histogramName+".pdf")
473  
474  
475 <            if makeRatioPlots or makeDiffPlots:
476 <                Canvas.cd(2)
477 <                BgSum = Stack.GetStack().Last()
451 <                Comparison = DataHistograms[0].Clone()
452 <                Comparison.Add(BgSum,-1)
453 <                if not makeDiffPlots:        
454 <                      Comparison.Divide(BgSum)
455 <                Comparison.SetTitle("")
456 <                Comparison.GetXaxis().SetTitle(xAxisLabel)
457 <                if makeRatioPlots:
458 <                    Comparison.GetYaxis().SetTitle("#frac{Data-MC}{MC}")
459 <                elif makeDiffPlots:
460 <                    Comparison.GetYaxis().SetTitle("Data-MC")
461 <                Comparison.GetYaxis().CenterTitle()
462 <                Comparison.GetYaxis().SetTitleSize(0.1)
463 <                Comparison.GetYaxis().SetTitleOffset(0.35)                
464 <                Comparison.GetXaxis().SetTitleSize(0.15)
465 <                Comparison.GetYaxis().SetLabelSize(0.1)
466 <                Comparison.GetXaxis().SetLabelSize(0.15)
467 <                if makeRatioPlots:
468 <                    Comparison.GetYaxis().SetRangeUser(-1.15,1.15)
469 <                elif makeDiffPlots:
470 <                    YMax = Comparison.GetMaximum()
471 <                    YMin = Comparison.GetMinimum()
472 <                    if YMax <= 0 and YMin <= 0:
473 <                        Comparison.GetYaxis().SetRangeUser(-1.2*YMin,0)
474 <                    elif YMax >= 0 and YMin >= 0:
475 <                        Comparison.GetYaxis().SetRangeUser(0,1.2*YMax)
476 <                    else: #axis crosses y=0
477 <                        if abs(YMax) > abs(YMin):
478 <                            Comparison.GetYaxis().SetRangeUser(-1.2*YMax,1.2*YMax)
479 <                        else:
480 <                            Comparison.GetYaxis().SetRangeUser(-1.2*YMin,1.2*YMin)
481 <                            
482 <                Comparison.GetYaxis().SetNdivisions(205)
483 <                Comparison.Draw()
484 <            Canvas.Write()
475 > ##########################################################################################################################################
476 > ##########################################################################################################################################
477 > ##########################################################################################################################################
478  
479 < def MakeTwoDHist(histogramname,channel,cut):
479 > def MakeTwoDHist(pathToDir,histogramName):
480              numBgMCSamples = 0
481              numDataSamples = 0
482              numSignalSamples = 0
483  
491            if(intLumi < 1000.):
492                LumiText = "L_{int} = " + str(intLumi) + " pb^{-1}"
493            else:
494                getcontext().prec = 2
495                LumiInFb = intLumi/1000.
496                LumiText = "L_{int} = " + str(LumiInFb) + " fb^{-1}"
497
484              LumiLabel = TPaveLabel(0.1,0.8,0.34,0.9,LumiText,"NDC")
485              LumiLabel.SetBorderSize(0)
486              LumiLabel.SetFillColor(0)
# Line 511 | Line 497 | def MakeTwoDHist(histogramname,channel,c
497              SignalMCLegend.SetFillColor(0)
498              SignalMCLegend.SetFillStyle(0)
499  
500 <            outputFile.cd(rootDirectory+"/"+channel+cut)
500 >            outputFile.cd(pathToDir)
501              Canvas = TCanvas(histogramName)
502              Canvas.SetRightMargin(0.2413793);
503              BgMCHistograms = []
# Line 521 | Line 507 | def MakeTwoDHist(histogramname,channel,c
507              for sample in processed_datasets: # loop over different samples as listed in configurationOptions.py
508                  dataset_file = "%s/%s.root" % (condor_dir,sample)
509                  inputFile = TFile(dataset_file)
510 <                Histogram = inputFile.Get(rootDirectory+"/"+channel+"/"+cut+"/"+histogramName).Clone()
510 >                Histogram = inputFile.Get(pathToDir+"/"+histogramName).Clone()
511                  Histogram.SetDirectory(0)
512                  RebinFactor = int(arguments.rebinFactor)
513                  if arguments.rebinFactor and Histogram.GetNbinsX() >= RebinFactor*10 and Histogram.GetNbinsY() >= RebinFactor*10:
# Line 556 | Line 542 | def MakeTwoDHist(histogramname,channel,c
542                      DataHistograms.append(Histogram)
543  
544  
545 <            outputFile.cd(rootDirectory+"/"+channel+cut)        
545 >            outputFile.cd(pathToDir)        
546  
547              if(numBgMCSamples is not 0):
548                  BgMCHistograms[0].SetTitle(histoTitle)
# Line 598 | Line 584 | def MakeTwoDHist(histogramname,channel,c
584  
585              Canvas.Write()
586  
601 for channel in channels: # loop over final states, which each have their own directory
602    if GetPlotsAfterEachCut:
603        for cut in channels[channel]:
604            testFile.cd(rootDirectory+"/"+channel+cut)
587  
606            for key in gDirectory.GetListOfKeys(): # loop over histograms in the current directory
607                histogramName = key.GetName()
608        
609                if re.match ('TH1', key.GetClassName()): # plot a 1-D histogram
610                    MakeOneDHist(histogramName,channel,cut)
611                if re.match ('TH2', key.GetClassName()) and arguments.draw2DPlots: # plot a 2-D histogram        
612                    MakeTwoDHist(histogramName,channel,cut)
613    else:
614        testFile.cd(rootDirectory+"/"+channel)
588  
616        for key in gDirectory.GetListOfKeys(): # loop over histograms in the current directory
617            histogramName = key.GetName()
589  
590 <            if re.match ('TH1', key.GetClassName()): # plot a 1-D histogram
591 <                MakeOneDHist(histogramName,channel,"")
592 <            if re.match ('TH2', key.GetClassName()) and arguments.draw2DPlots: # plot a 2-D histogram        
593 <                MakeTwoDHist(histogramName,channel,"")
594 <
595 < #for dataset in processed_datasets:
596 < #    dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
597 < #    os.remove(dataset_file)
590 > ##########################################################################################################################################
591 > ##########################################################################################################################################
592 > ##########################################################################################################################################
593 >
594 > processed_datasets = []
595 >
596 > condor_dir = set_condor_output_dir(arguments)
597 >
598 > #### check which input datasets have valid output files
599 > for sample in datasets:
600 >    fileName = condor_dir + "/" + sample + ".root"
601 >    if not os.path.exists(fileName):
602 >        continue
603 >    testFile = TFile(fileName)
604 >    if testFile.IsZombie() or not testFile.GetNkeys():
605 >        continue
606 >    processed_datasets.append(sample)
607 >
608 > if len(processed_datasets) is 0:
609 >    sys.exit("No datasets have been processed")
610 >
611 >
612 > #### make output file
613 > outputFileName = "stacked_histograms.root"
614 > if arguments.outputFileName:
615 >    outputFileName = arguments.outputFileName
616 >
617 > outputFile = TFile(condor_dir + "/" + outputFileName, "RECREATE")
618 >
619 >
620 >
621 > #### use the first input file as a template and make stacked versions of all its histograms
622 > inputFile = TFile(condor_dir + "/" + processed_datasets[0] + ".root")
623 > inputFile.cd()
624 > outputFile.cd()
625 >
626 > if arguments.savePDFs:
627 >    os.system("rm -r %s/stacked_histograms_pdfs" % (condor_dir))
628 >    os.system("mkdir %s/stacked_histograms_pdfs" % (condor_dir))
629 >    
630 >
631 > #get root directory in the first layer, generally "OSUAnalysis"
632 > for key in inputFile.GetListOfKeys():
633 >    if (key.GetClassName() != "TDirectoryFile"):
634 >        continue
635 >    rootDirectory = key.GetName()
636 >    outputFile.mkdir(rootDirectory)
637 >    if arguments.savePDFs:
638 >        os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,rootDirectory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
639 >
640 >    #cd to root directory and look for histograms
641 >    inputFile.cd(rootDirectory)
642 >    for key2 in gDirectory.GetListOfKeys():
643 >
644 >        if re.match ('TH1', key2.GetClassName()): # found a 1-D histogram
645 >            MakeOneDHist(rootDirectory,key2.GetName())
646 >        elif re.match ('TH2', key2.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram        
647 >            MakeTwoDHist(rootDirectory,key2.GetName())
648 >
649 >        elif (key2.GetClassName() == "TDirectoryFile"): # found a directory, cd there and look for histograms
650 >            level2Directory = rootDirectory+"/"+key2.GetName()
651 >
652 >            #make a corresponding directory in the output file
653 >            outputFile.cd(rootDirectory)
654 >            gDirectory.mkdir(key2.GetName())
655 >            if arguments.savePDFs:
656 >                os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,level2Directory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
657 >
658 >            #####################################################
659 >            ###  This layer is typically the "channels" layer ###
660 >            #####################################################
661 >
662 >            inputFile.cd(level2Directory)
663 >            for key3 in gDirectory.GetListOfKeys():
664 >                if re.match ('TH1', key3.GetClassName()): # found a 1-D histogram
665 >                    MakeOneDHist(level2Directory,key3.GetName())
666 >                elif re.match ('TH2', key3.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram        
667 >                    MakeTwoDHist(level2Directory,key3.GetName())
668 >
669 >                elif (key3.GetClassName() == "TDirectoryFile"): # found a directory, cd there and look for histograms
670 >                    level3Directory = level2Directory+"/"+key3.GetName()
671 >                    
672 >                    #make a corresponding directory in the output file
673 >                    outputFile.cd(level2Directory)
674 >                    gDirectory.mkdir(key3.GetName())
675 >                    if arguments.savePDFs:
676 >                        os.system("mkdir %s/stacked_histograms_pdfs/%s" % (condor_dir,level3Directory.replace(' ','_').replace('<','lt').replace('>','gt').replace('(','').replace(')','').replace('=','eq')))
677 >
678 >                    #################################################
679 >                    ###  This layer is typically the "cuts" layer ###
680 >                    #################################################
681 >
682 >                    inputFile.cd(level3Directory)
683 >                    for key3 in gDirectory.GetListOfKeys():
684 >                        if re.match ('TH1', key3.GetClassName()): # found a 1-D histogram
685 >                            MakeOneDHist(level3Directory,key3.GetName())
686 >                        elif re.match ('TH2', key3.GetClassName()) and arguments.draw2DPlots: # found a 2-D histogram        
687 >                            MakeTwoDHist(level3Directory,key3.GetName())
688 >
689  
690   outputFile.Close()        

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines