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 |
parser.remove_option("-c")
|
21 |
parser.remove_option("-n")
|
22 |
parser.remove_option("-e")
|
23 |
parser.remove_option("-r")
|
24 |
parser.remove_option("-d")
|
25 |
parser.remove_option("--2D")
|
26 |
|
27 |
|
28 |
parser.add_option("--hist", action="store_true", dest="plot_hist", default=False,
|
29 |
help="plot as hollow histograms instead of error bar crosses")
|
30 |
parser.add_option("--line-width", dest="line_width",
|
31 |
help="set line width (default is 2)")
|
32 |
parser.add_option("--pdf", action="store_true", dest="plot_savePdf", default=False,
|
33 |
help="save plot as pdf in addition")
|
34 |
|
35 |
|
36 |
(arguments, args) = parser.parse_args()
|
37 |
|
38 |
if arguments.localConfig:
|
39 |
sys.path.insert(0,os.getcwd())
|
40 |
exec("from " + arguments.localConfig.rstrip('.py') + " import *")
|
41 |
|
42 |
|
43 |
outputFileName = "simple_plot.root"
|
44 |
if arguments.outputFileName:
|
45 |
outputFileName = arguments.outputFileName
|
46 |
|
47 |
|
48 |
if arguments.plot_hist:
|
49 |
plotting_options = plotting_options + "HIST"
|
50 |
|
51 |
|
52 |
from ROOT import TFile, gROOT, gStyle, gDirectory, TStyle, THStack, TH1F, TCanvas, TString, TLegend, TArrow, THStack, TIter, TKey, TPaveLabel
|
53 |
sys.argv = []
|
54 |
gROOT.SetBatch()
|
55 |
gStyle.SetOptStat(0)
|
56 |
gStyle.SetCanvasBorderMode(0)
|
57 |
gStyle.SetPadBorderMode(0)
|
58 |
gStyle.SetPadColor(0)
|
59 |
gStyle.SetCanvasColor(0)
|
60 |
gStyle.SetTextFont(42)
|
61 |
gROOT.ForceStyle()
|
62 |
|
63 |
|
64 |
outputFile = TFile(outputFileName, "RECREATE")
|
65 |
|
66 |
datasets_needed = []
|
67 |
for histogram in input_histograms:
|
68 |
if histogram['dataset'] not in datasets_needed:
|
69 |
datasets_needed.append(histogram['dataset'])
|
70 |
|
71 |
|
72 |
Legend = TLegend(0.70,0.65,0.9,0.9)
|
73 |
Legend.SetBorderSize(0)
|
74 |
Legend.SetFillColor(0)
|
75 |
Legend.SetFillStyle(0)
|
76 |
|
77 |
|
78 |
|
79 |
finalMax = 0
|
80 |
|
81 |
Histograms = []
|
82 |
for histogram in input_histograms:
|
83 |
|
84 |
fileName = "condor/" + histogram['condor_dir'] + "/" + histogram['dataset'] + ".root"
|
85 |
if not os.path.exists(fileName):
|
86 |
continue
|
87 |
inputFile = TFile(fileName)
|
88 |
if inputFile.IsZombie() or not inputFile.GetNkeys():
|
89 |
continue
|
90 |
|
91 |
Histogram = inputFile.Get("OSUAnalysis/"+histogram['channel']+"/"+histogram['name']).Clone()
|
92 |
Histogram.SetDirectory(0)
|
93 |
inputFile.Close()
|
94 |
|
95 |
|
96 |
fullTitle = Histogram.GetTitle()
|
97 |
splitTitle = fullTitle.split(":")
|
98 |
# print splitTitle
|
99 |
Histogram.SetTitle(splitTitle[1].lstrip(" "))
|
100 |
|
101 |
Histogram.SetMarkerColor(histogram['color'])
|
102 |
Histogram.SetLineColor(histogram['color'])
|
103 |
Histogram.SetLineWidth(line_width)
|
104 |
Histogram.SetFillStyle(0)
|
105 |
if(arguments.normalizeToUnitArea and Histogram.Integral() > 0):
|
106 |
Histogram.Scale(1./Histogram.Integral())
|
107 |
if arguments.rebinFactor:
|
108 |
RebinFactor = int(arguments.rebinFactor)
|
109 |
if Histogram.GetNbinsX() >= RebinFactor*10:
|
110 |
Histogram.Rebin(RebinFactor)
|
111 |
|
112 |
|
113 |
currentMax = Histogram.GetMaximum()
|
114 |
if currentMax > finalMax:
|
115 |
finalMax = currentMax
|
116 |
|
117 |
Legend.AddEntry(Histogram,histogram['legend_entry'],"L")
|
118 |
Histograms.append(Histogram)
|
119 |
|
120 |
|
121 |
Canvas = TCanvas(outputFileName.rstrip('.root'))
|
122 |
|
123 |
counter = 0
|
124 |
for Histogram in Histograms:
|
125 |
if counter is 0:
|
126 |
Histogram.SetMaximum(1.1*finalMax)
|
127 |
Histogram.SetMinimum(0.0001)
|
128 |
Histogram.Draw(plotting_options)
|
129 |
|
130 |
else:
|
131 |
Histogram.Draw(plotting_options+" SAME")
|
132 |
counter = counter+1
|
133 |
|
134 |
|
135 |
Legend.Draw()
|
136 |
if arguments.normalizeToUnitArea:
|
137 |
NormLabel = TPaveLabel(0.1,0.75,0.35,0.85,"Scaled to unit area","NDC")
|
138 |
NormLabel.SetBorderSize(0)
|
139 |
NormLabel.SetFillColor(0)
|
140 |
NormLabel.SetFillStyle(0)
|
141 |
NormLabel.Draw()
|
142 |
|
143 |
outputFile.cd()
|
144 |
Canvas.Write()
|
145 |
|
146 |
if arguments.plot_savePdf:
|
147 |
pdfFileName = outputFileName.replace(".root", ".pdf")
|
148 |
Canvas.SaveAs(pdfFileName)
|
149 |
print "Saved file: " + pdfFileName
|
150 |
|
151 |
|
152 |
outputFile.Close()
|
153 |
print "Saved plot in file: " + outputFileName
|