ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/OSUT3Analysis/Configuration/scripts/makeSinglePlot.py
Revision: 1.2
Committed: Tue Mar 12 21:10:44 2013 UTC (12 years, 1 month ago) by lantonel
Content type: text/x-python
Branch: MAIN
Changes since 1.1: +45 -27 lines
Log Message:
migrated from depricated optparse package to argparse.  All scripts now have -h,--help arguments which print all command-line options

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