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

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