ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeSinglePlot.py
Revision: 1.9
Committed: Tue Mar 26 14:03:02 2013 UTC (12 years, 1 month ago) by lantonel
Content type: text/x-python
Branch: MAIN
Changes since 1.8: +3 -3 lines
Log Message:
fixed weird error where wrong localOptions file was being imported

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