ATLAS Offline Software
Loading...
Searching...
No Matches
plotmaker.py
Go to the documentation of this file.
1#!/usr/bin/env python
2
3# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
4
5import ROOT
6import optparse
7ROOT.gStyle.SetOptStat(0)
8ROOT.gStyle.SetTitleFont(42,"xy")
9ROOT.gStyle.SetTitleFont(42,"t")
10
11ScalarColor = ROOT.kPink+4
12DarkPhotonColor = ROOT.kAzure-3
13ElectronColor = ROOT.kRed-3
14MuonColor = ROOT.kBlue-3
15PionColor = ROOT.kGreen-3
16
17def make2DPdf(name, hist, prefix):
18 if not hist:
19 return
20 canvas = ROOT.TCanvas('whoCares'+name , 'name', 1600,1600)
21 canvas.cd(0)
22 hist.Draw('COLZ')
23 if prefix != '':
24 prefix += '_'
25 pass
26 canvas.SaveAs(prefix+name+'.pdf')
27
28def makePdf(name, hists, prefix):
29 if len(hists) == 0:
30 return
31 if prefix != '' and prefix[-1] != '_':
32 prefix += '_'
33 pass
34
35 if isinstance(hists, list):
36 maxY = -999
37 maxEntries = -999
38 for hist in hists:
39 hist.Scale( 1./float(hist.GetEntries()) )
40 hist.SetYTitle('Normalized to Unity')
41 if hist.GetMaximum() > maxY and ('Electron' in hist.GetName() or 'Pion' in hist.GetName() or 'Muon' in hist.GetName()):
42 maxY = hist.GetMaximum()
43 maxEntries = hist.GetEntries()
44 pass
45 hist.SetLineWidth(2)
46 pass
47
48 legend = ROOT.TLegend(.6, .65, .85, .89)
49 legend.SetFillColor(0)
50 legend.SetBorderSize(0)
51 legend.SetTextFont(102)
52 legend.SetTextSize(0.04)
53
54 canvas = ROOT.TCanvas('whoCares'+name , 'name', 1600,1200)
55 canvas.cd(0)
56 scalarhist = None
57 darkphotonhist = None
58 for index,hist in enumerate(hists):
59 #print hist.GetName()
60 drawOptions = 'HIST'
61 if index!=0:
62 drawOptions += 'SAME'
63 pass
64
65 if 'Electron' in hist.GetName():
66 legend.AddEntry(hist, '#splitline{Electrons}{'+str(int(hist.GetEntries()))+' events}', 'f')
67 hist.SetLineColor(ElectronColor)
68 hist.SetMaximum(1.2*maxY )
69 pass
70 elif 'Muon' in hist.GetName():
71 legend.AddEntry(hist, '#splitline{Muons}{'+str(int(hist.GetEntries()))+' events}', 'f')
72 hist.SetLineColor(MuonColor)
73 hist.SetMaximum(1.2*maxY )
74 pass
75 elif 'Pion' in hist.GetName():
76 legend.AddEntry(hist, '#splitline{Pions}{'+str(int(hist.GetEntries()))+' events}', 'f')
77 hist.SetLineColor(PionColor)
78 hist.SetMaximum(1.2*maxY )
79 pass
80 elif 'DarkPhoton' in hist.GetName() or 'darkPhoton' in hist.GetName():
81 hist.SetLineColor(DarkPhotonColor)
82 darkphotonhist = hist
83 continue
84 pass
85 elif 'Scalar' in hist.GetName() or 'scalar' in hist.GetName():
86 hist.SetLineColor(ScalarColor)
87 scalarhist = hist
88 continue
89 pass
90 hist.Draw(drawOptions)
91 pass
92 if legend.GetNRows():
93 legend.Draw()
94 pass
95 canvas.SaveAs(prefix+name+'.pdf')
96 if darkphotonhist:
97 if not 'darkPhoton' in name:
98 name = 'darkPhoton'+name
99 pass
100 makePdf(name, darkphotonhist, prefix)
101 pass
102 if scalarhist:
103 if not 'scalar' in name:
104 name = 'scalar'+name
105 pass
106 makePdf(name, scalarhist, prefix)
107 pass
108 else:
109 canvas = ROOT.TCanvas('whoCares2'+name , 'name', 1600,1200)
110 canvas.cd(0)
111 hists.Draw('HIST')
112 canvas.SaveAs(prefix+name+'.pdf')
113
114
115if __name__=='__main__':
116 ROOT.gROOT.SetBatch(1)
117 parser = optparse.OptionParser()
118 parser.add_option('-f', '--file', dest='hist_file_in', help='path to ROOT file with histograms' )
119 parser.add_option('-p', '--prefix', dest='prefix', default='', help='prefix to all output pdfs' )
120 #parser.add_argument('-b')
121
122 (options, args) = parser.parse_args()
123
124 if not options.hist_file_in:
125 print 'ERROR: input file (./plotmaker.py -f <path to ROOT file>) needed'
126 exit(0)
127 pass
128
129 hist_file = ROOT.TFile.Open(options.hist_file_in, 'r')
130 if not hist_file:
131 print 'Path provided to ROOT file is incorrect. Or something is wrong with the file.'
132 pass
133
134
135 hist1DDict = { 'darkPhotonPt': [], 'darkPhotonEta':[], 'darkPhotonPhi': [], 'Polarization': [],
136 'scalarPt': [], 'scalarEta':[], 'scalarPhi': [],
137 'decayR1D': [], 'decayZ1D':[],
138 'OpeningR':[], 'OpeningPhi':[], 'OpeningEta':[],
139 'ptBalance':[], 'subLeadingPt':[], 'leadingPt':[]}
140
141 hist2DDict = { 'decayZR2D': None, 'decayEtaR2D':None }
142
143 keys = hist_file.GetListOfKeys()
144 for key in keys:
145 keyName = key.GetName()
146 for histName in hist1DDict.keys():
147 if histName in keyName:
148 hist = ROOT.TH1F()
149 hist_file.GetObject(keyName, hist)
150 if hist.GetEntries() == 0:
151 continue
152 hist1DDict[histName].append(hist)
153 pass
154 continue
155 pass
156
157 for key in keys:
158 keyName = key.GetName()
159 for histName in hist2DDict.keys():
160 if histName in keyName:
161 hist = ROOT.TH2F()
162 hist_file.GetObject(keyName, hist)
163 if hist.GetEntries() == 0:
164 continue
165 hist2DDict[histName] = hist
166 pass
167 continue
168 pass
169
170 for name,hist in hist1DDict.iteritems():
171 makePdf(name, hist, options.prefix)
172 pass
173
174 for name,hist in hist2DDict.iteritems():
175 make2DPdf(name, hist, options.prefix)
make2DPdf(name, hist, prefix)
Definition plotmaker.py:17
makePdf(name, hists, prefix)
Definition plotmaker.py:28