ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeSinglePlot.py
Revision: 1.5
Committed: Wed Mar 13 10:41:49 2013 UTC (12 years, 1 month ago) by lantonel
Content type: text/x-python
Branch: MAIN
Changes since 1.4: +1 -0 lines
Log Message:
set histogram minimum to zero

File Contents

# Content
1 #!/usr/bin/env python
2 import sys
3 import os
4 import re
5 from optparse import OptionParser
6 from array import *
7 from decimal import *
8
9 from OSUT3Analysis.Configuration.configurationOptions import *
10 from OSUT3Analysis.Configuration.processingUtilities import *
11
12
13
14
15
16
17 ##set default plotting options
18 line_width = 2
19 plotting_options = ""
20
21 parser = OptionParser()
22 parser = set_commandline_arguments(parser)
23
24
25 parser.add_option("--hist", action="store_true", dest="plot_hist", default=False,
26 help="plot as hollow histograms instead of error bar crosses")
27 parser.add_option("--line-width", dest="line_width",
28 help="set line width (default is 2)")
29
30
31 (arguments, args) = parser.parse_args()
32
33 if arguments.localConfig:
34 sys.path.append(os.getcwd())
35 exec("from " + arguments.localConfig.rstrip('.py') + " import *")
36
37 outputFileName = "simple_plot.root"
38 if arguments.outputFileName:
39 outputFileName = arguments.outputFileName
40
41
42 condor_dir = set_condor_output_dir(arguments)
43
44
45 if arguments.normalizeToData and arguments.normalizeToUnitArea:
46 print "Conflicting normalizations requsted, will normalize to unit area"
47 arguments.normalizeToData = False
48 if arguments.normalizeToData and arguments.noStack:
49 print "You have asked to scale non-stacked backgrounds to data. This is a very strange request. Will normalize to unit area instead"
50 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
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 if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
118 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 Histogram.SetMaximum(0)
135 Histogram.Draw(plotting_options)
136
137 else:
138 Histogram.Draw(plotting_options+" SAME")
139 counter = counter+1
140
141
142 Legend.Draw()
143 if arguments.normalizeToUnitArea:
144 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()