ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeSinglePlot.py
Revision: 1.6
Committed: Wed Mar 13 13:43:09 2013 UTC (12 years, 1 month ago) by qpython
Content type: text/x-python
Branch: MAIN
Changes since 1.5: +1 -1 lines
Log Message:
fixed bug Histogram.SetMaximum(0) -> Histogram.SetMinimum(0)

File Contents

# User Rev Content
1 lantonel 1.1 #!/usr/bin/env python
2     import sys
3     import os
4     import re
5 lantonel 1.3 from optparse import OptionParser
6 lantonel 1.1 from array import *
7     from decimal import *
8 lantonel 1.2
9 lantonel 1.1 from OSUT3Analysis.Configuration.configurationOptions import *
10     from OSUT3Analysis.Configuration.processingUtilities import *
11    
12    
13 lantonel 1.2
14 lantonel 1.1
15    
16    
17     ##set default plotting options
18     line_width = 2
19 lantonel 1.2 plotting_options = ""
20    
21 lantonel 1.3 parser = OptionParser()
22 lantonel 1.2 parser = set_commandline_arguments(parser)
23    
24 lantonel 1.3
25 lantonel 1.4 parser.add_option("--hist", action="store_true", dest="plot_hist", default=False,
26 lantonel 1.2 help="plot as hollow histograms instead of error bar crosses")
27 lantonel 1.4 parser.add_option("--line-width", dest="line_width",
28 lantonel 1.2 help="set line width (default is 2)")
29    
30 lantonel 1.1
31 lantonel 1.3 (arguments, args) = parser.parse_args()
32 lantonel 1.1
33 lantonel 1.2 if arguments.localConfig:
34 lantonel 1.1 sys.path.append(os.getcwd())
35 lantonel 1.2 exec("from " + arguments.localConfig.rstrip('.py') + " import *")
36 lantonel 1.1
37     outputFileName = "simple_plot.root"
38 lantonel 1.2 if arguments.outputFileName:
39     outputFileName = arguments.outputFileName
40 lantonel 1.1
41    
42 lantonel 1.2 condor_dir = set_condor_output_dir(arguments)
43 lantonel 1.1
44    
45 lantonel 1.2 if arguments.normalizeToData and arguments.normalizeToUnitArea:
46 lantonel 1.1 print "Conflicting normalizations requsted, will normalize to unit area"
47 lantonel 1.2 arguments.normalizeToData = False
48     if arguments.normalizeToData and arguments.noStack:
49 lantonel 1.1 print "You have asked to scale non-stacked backgrounds to data. This is a very strange request. Will normalize to unit area instead"
50 lantonel 1.2 arguments.normalizeToData = False
51     arguments.normalizeToUnitArea = True
52    
53     if arguments.plot_hist:
54     plotting_options = plotting_options + "HIST"
55    
56    
57    
58     from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TArrow, THStack, TIter, TKey, TPaveLabel
59     sys.argv = []
60     gROOT.SetBatch()
61     gStyle.SetOptStat(0)
62     gStyle.SetCanvasBorderMode(0)
63     gStyle.SetPadBorderMode(0)
64     gStyle.SetPadColor(0)
65     gStyle.SetCanvasColor(0)
66     gStyle.SetTextFont(42)
67     gROOT.ForceStyle()
68 lantonel 1.1
69    
70     outputFile = TFile(outputFileName, "RECREATE")
71    
72    
73     datasets_needed = []
74     for histogram in input_histograms:
75     if histogram['dataset'] not in datasets_needed:
76     datasets_needed.append(histogram['dataset'])
77    
78     weight = intLumi / 10000.0
79     for dataset in datasets_needed:
80     dataset_file = "%s/%s.root" % (condor_dir,dataset)
81     if types[dataset] != "data":
82     os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", weight))
83     else:
84     os.system("mergeTFileServiceHistograms -i %s -o %s -w %g" % (dataset_file, dataset_file + "_tmp", 1.0))
85    
86    
87     Legend = TLegend(0.70,0.65,0.9,0.9)
88     Legend.SetBorderSize(0)
89     Legend.SetFillColor(0)
90     Legend.SetFillStyle(0)
91    
92    
93    
94     finalMax = 0
95    
96     Histograms = []
97     for histogram in input_histograms:
98    
99     fileName = condor_dir + "/" + histogram['dataset'] + ".root"
100     if not os.path.exists(fileName):
101     continue
102     inputFile = TFile(fileName)
103     if inputFile.IsZombie() or not inputFile.GetNkeys():
104     continue
105    
106     print "OSUAnalysis/"+histogram['channel']+"/"+histogram['name']
107     Histogram = inputFile.Get("OSUAnalysis/"+histogram['channel']+"/"+histogram['name']).Clone()
108     Histogram.SetDirectory(0)
109     inputFile.Close()
110    
111    
112     Histogram.SetTitle("")
113     Histogram.SetMarkerColor(histogram['color'])
114     Histogram.SetLineColor(histogram['color'])
115     Histogram.SetLineWidth(line_width)
116     Histogram.SetFillStyle(0)
117 lantonel 1.2 if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
118 lantonel 1.1 Histogram.Scale(1./Histogram.Integral())
119    
120     currentMax = Histogram.GetMaximum()
121     if currentMax > finalMax:
122     finalMax = currentMax
123    
124     Legend.AddEntry(Histogram,histogram['legend_entry'],"L")
125     Histograms.append(Histogram)
126    
127    
128     Canvas = TCanvas("plot")
129    
130     counter = 0
131     for Histogram in Histograms:
132     if counter is 0:
133     Histogram.SetMaximum(1.1*finalMax)
134 qpython 1.6 Histogram.SetMinimum(0)
135 lantonel 1.2 Histogram.Draw(plotting_options)
136 lantonel 1.1
137     else:
138 lantonel 1.2 Histogram.Draw(plotting_options+" SAME")
139 lantonel 1.1 counter = counter+1
140    
141    
142     Legend.Draw()
143 lantonel 1.2 if arguments.normalizeToUnitArea:
144 lantonel 1.1 NormLabel = TPaveLabel(0.1,0.75,0.35,0.85,"Scaled to unit area","NDC")
145     NormLabel.SetBorderSize(0)
146     NormLabel.SetFillColor(0)
147     NormLabel.SetFillStyle(0)
148     NormLabel.Draw()
149    
150     outputFile.cd()
151     Canvas.Write()
152    
153    
154    
155     for dataset in datasets_needed:
156     dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
157     os.remove(dataset_file)
158    
159     outputFile.Close()