ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeSinglePlot.py
Revision: 1.10
Committed: Thu Mar 28 09:06:24 2013 UTC (12 years, 1 month ago) by wulsin
Content type: text/x-python
Branch: MAIN
Changes since 1.9: +8 -1 lines
Log Message:
add option to print pdf

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     ##set default plotting options
14     line_width = 2
15 lantonel 1.2 plotting_options = ""
16    
17 lantonel 1.3 parser = OptionParser()
18 lantonel 1.2 parser = set_commandline_arguments(parser)
19    
20 lantonel 1.3
21 lantonel 1.4 parser.add_option("--hist", action="store_true", dest="plot_hist", default=False,
22 lantonel 1.2 help="plot as hollow histograms instead of error bar crosses")
23 lantonel 1.4 parser.add_option("--line-width", dest="line_width",
24 lantonel 1.2 help="set line width (default is 2)")
25 wulsin 1.10 parser.add_option("--pdf", action="store_true", dest="plot_savePdf", default=False,
26     help="save plot as pdf in addition")
27 lantonel 1.2
28 lantonel 1.1
29 lantonel 1.3 (arguments, args) = parser.parse_args()
30 lantonel 1.1
31 lantonel 1.2 if arguments.localConfig:
32 lantonel 1.9 sys.path.insert(0,os.getcwd())
33 lantonel 1.2 exec("from " + arguments.localConfig.rstrip('.py') + " import *")
34 lantonel 1.9 print arguments.localConfig.rstrip('.py')
35 lantonel 1.1
36     outputFileName = "simple_plot.root"
37 lantonel 1.2 if arguments.outputFileName:
38     outputFileName = arguments.outputFileName
39 lantonel 1.1
40    
41 lantonel 1.2 condor_dir = set_condor_output_dir(arguments)
42 lantonel 1.1
43    
44 lantonel 1.2 if arguments.normalizeToData and arguments.normalizeToUnitArea:
45 lantonel 1.1 print "Conflicting normalizations requsted, will normalize to unit area"
46 lantonel 1.2 arguments.normalizeToData = False
47     if arguments.normalizeToData and arguments.noStack:
48 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"
49 lantonel 1.2 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 lantonel 1.1
68    
69     outputFile = TFile(outputFileName, "RECREATE")
70    
71     datasets_needed = []
72     for histogram in input_histograms:
73     if histogram['dataset'] not in datasets_needed:
74     datasets_needed.append(histogram['dataset'])
75    
76     weight = intLumi / 10000.0
77     for dataset in datasets_needed:
78 lantonel 1.9
79 lantonel 1.1 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     Histogram = inputFile.Get("OSUAnalysis/"+histogram['channel']+"/"+histogram['name']).Clone()
106     Histogram.SetDirectory(0)
107     inputFile.Close()
108    
109    
110     Histogram.SetTitle("")
111     Histogram.SetMarkerColor(histogram['color'])
112     Histogram.SetLineColor(histogram['color'])
113     Histogram.SetLineWidth(line_width)
114     Histogram.SetFillStyle(0)
115 lantonel 1.2 if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
116 lantonel 1.1 Histogram.Scale(1./Histogram.Integral())
117    
118     currentMax = Histogram.GetMaximum()
119     if currentMax > finalMax:
120     finalMax = currentMax
121    
122     Legend.AddEntry(Histogram,histogram['legend_entry'],"L")
123     Histograms.append(Histogram)
124    
125    
126     Canvas = TCanvas("plot")
127    
128     counter = 0
129     for Histogram in Histograms:
130     if counter is 0:
131     Histogram.SetMaximum(1.1*finalMax)
132 lantonel 1.8 Histogram.SetMinimum(0.0001)
133 lantonel 1.2 Histogram.Draw(plotting_options)
134 lantonel 1.1
135     else:
136 lantonel 1.2 Histogram.Draw(plotting_options+" SAME")
137 lantonel 1.1 counter = counter+1
138    
139    
140     Legend.Draw()
141 lantonel 1.2 if arguments.normalizeToUnitArea:
142 lantonel 1.1 NormLabel = TPaveLabel(0.1,0.75,0.35,0.85,"Scaled to unit area","NDC")
143     NormLabel.SetBorderSize(0)
144     NormLabel.SetFillColor(0)
145     NormLabel.SetFillStyle(0)
146     NormLabel.Draw()
147    
148     outputFile.cd()
149     Canvas.Write()
150 wulsin 1.10
151     if arguments.plot_savePdf:
152     pdfFileName = outputFileName.replace(".root", ".pdf")
153     Canvas.SaveAs(pdfFileName)
154     print "Saved file: " + pdfFileName
155 lantonel 1.1
156    
157     for dataset in datasets_needed:
158     dataset_file = "%s/%s.root_tmp" % (condor_dir,dataset)
159     os.remove(dataset_file)
160    
161     outputFile.Close()
162 wulsin 1.10 print "Saved plot in file: " + outputFileName