-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plotter.py
309 lines (248 loc) · 11.2 KB
/
Plotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/python
# -*- coding: utf-8 -*-
#this is Peter's from-scratch frankenpaste prototype plotter
#naming standard: thisThing
#this program takes many things that end with ".root" and outputs simulation and reconstruction plots into the folder "plots"
#the process name in the config file must be the file's name
#now available on github
from Histograms import *
from HistogramFiller import *
from numpy import *
import ROOT as r
import pdb
import copy
from array import array
from ROOT import gSystem
from optparse import OptionParser
gSystem.Load("libFramework.so") #this library is vital for it to run. It might be old though?
r.gROOT.SetBatch(1); #makes root not try to display plots in a new window
# rootColors=[1,2,4,28,7] #a presumably color-blind friendly color palette
# rootColors=[28,2,4] #a three-compare color-blind friendly color palette
# rootColors=[4,2] #a -v+ comparison
rootColors=[1,2,3,4,6,7,8,9] #Colorblind unfriendly for comparing many things
rootMarkers=[4,26,32] #this is getting out of hand
def unabbreviate(str):
if str == "rec": return "reconstruction"
elif str == "sim": return "simulation"
elif str == "e-0.5": return "500 MeV electrons"
elif str == "e+0.5": return "500 MeV positrons"
else: return str
def barName(id):
if id is False: return ''#"Machine"
if id <20: return "bar"+str(id) #it means it is TS
shortID = id-402654208
layer = int(shortID/1024)
bar = shortID%1024
return "layer"+str(layer)+"_bar"+str(bar)
def createCanvas():
return r.TCanvas( 'c1', 'Histogram Drawing Options',1000,1000 )
def createPad(plotDimension):
pad = r.TPad( 'pad', 'The pad with the histogram', 0,0,1,1 )
pad.Draw()
pad.cd()
pad.SetGridx()
pad.SetGridy()
pad.GetFrame().SetFillColor( 18 )
if plotDimension == 1:
pad.SetRightMargin(0.05)
pad.SetLeftMargin(0.1)
# pad.SetLogy()
elif plotDimension == 2:
pad.SetRightMargin(0.12)
pad.SetLeftMargin(0.14)
return pad
def createPad2(plotDimension):
pad = r.TPad( 'pad2', 'The pad with the other histogram', 0,0,1,1 )
pad.Draw()
pad.cd()
# pad.SetGridx()
# pad.SetGridy()
# pad.GetFrame().SetFillColor( 18 )
# if plotDimension == 1:
# pad.SetRightMargin(0.05)
# pad.SetLeftMargin(0.1)
# pad.SetLogy()
# elif plotDimension == 2:
# pad.SetRightMargin(0.12)
# pad.SetLeftMargin(0.14)
return pad
def createLegend():
# legend = r.TLegend(0.0,0.95,0.18,1)
legend = r.TLegend(0.0,0.9,0.18,1)
# legend = r.TLegend(0.0,0.9,0,1)
# legend.SetTextSize(0.025)
return legend
def createInfoBox():
#sets what the top right box should say. "" for nothing.
r.gStyle.SetOptStat("ne")
def createLabel(fwhm=None):
label = r.TLatex()
label.SetTextFont(42)
label.SetTextSize(0.03)
label.SetNDC()
if fwhm: label.DrawLatex(0, 0.005, "FWHM: "+str(round(fwhm,6)))
return label
def drawLine(plotDimension,line):
# hist.SetOption("")
if plotDimension == 1:
line.Draw("HIST SAME")
# lines[-1].Draw("SAME E")
if plotDimension == 2:
line.Draw("COLZ SAME")
def drawLines(plotDimension,lines,options=''):
# hist.SetOption("")
if plotDimension == 1:
lines[0].Draw(options)
for i in range(1,len(lines)):
lines[i].Draw(options+" SAME")
# lines[i].Draw("SAME E")
if plotDimension == 2:
lines[0].Draw("COLZ") #SAME?
def createHist(plotDict,plotVar,id):
if plotDict[plotVar]['dimension'] == 1:
histTitle = plotVar
histName = 'Test'#barName(id)
print("The name",histName)
binning = plotDict[plotVar]['binning']
if type(binning) == type({}):
hist = r.TH1F(histName,histTitle, binning['nBins'],binning['min'],binning['max']) #name, title, nbins, start, finish
elif type(binning) == type([]):
hist = r.TH1F(histName,histTitle, len(binning)-1, array('f',binning)) #name, title, nbins, binlayout
# hist.SetMinimum(0.5)
elif plotDict[plotVar]['dimension'] == 2:
histTitle = plotVar
histName = barName(id)
binningX = plotDict[plotVar]['binningX']
binningY = plotDict[plotVar]['binningY']
if type(binningX) == type({}):
hist = r.TH2F(histName,histTitle,binningX['nBins'],binningX['min'],binningX['max'] #name, title, nbins, start, finish
,binningY['nBins'],binningY['min'],binningY['max']) #nbins, start, finish
elif type(binningX) == type([]):
hist = r.TH2F(histName,histTitle, len(binningX)-1, array('f',binningX) #name, title, nbins, start, finish
, len(binningY)-1, array('f',binningY)) #nbins, start, finish
# elif plotDict[plotVar]['dimension'] == "bar":
# binLabelsEvtType = ["Nothing hard","1n","2n","#geq 3n","1#pi","2#pi", "1#pi_{0}", "1#pi 1N", "1p","2N","exotics","multi-body"]
# hist.GetXaxis().SetBinLabel(b+1, binLabelsEvtType[b])
hist.SetYTitle(plotDict[plotVar]['yaxis'])
hist.SetXTitle(plotDict[plotVar]['xaxis'])
# hist.SetLineColor(rootColors[len(lines)])
# hist.SetFillStyle(0);
# hist.SetMarkerStyle(rootMarkers[len(lines)])
# hist.SetMarkerColor(rootColors[len(lines)])
# hist.SetMarkerSize(3)
return hist
def loadData(fileName): #can't even make it into a function why is ROOT so awful?
#tried to make this more efficient by only running it once, but such methods are doomed to fail
inFile = r.TFile(fileName+".root","READ")
allData = inFile.Get("LDMX_Events")
# allData.Print("toponly")
print(allData)
return allData
def getPlotDimension(plotNumber):
plot = plotGroups[plotNumber]
line = plot[0]
plotType = line[0]
dimension = plotDict[plotType]['dimension']
return dimension
def getPlotBars(plotNumber):
plot = plotGroups[plotNumber]
line = plot[0]
plotType = line[0]
try: bars = plotDict[plotType]['bars']
except: bars = [False]
return bars
def normaliseHist():
if plotDimension == 1:
try:
hist.Scale(1./hist.Integral()) #normalises
hist.SetYTitle("Normalised entries")
hist.SetMaximum(1)
except: print("didnt normalise")
def getBeamEnergyFromFileName(fileName):
try:
energy=fileName[fileName.find('-')+1:fileName.find('GeV')]
return (float(energy)*1e3)
except:
print('Warning: beam energy could not be determined from file name')
return None
def decoratePlot(filledHist,plotDimension,legend):
if plotDimension == 1 or 2: legend.Draw();
# if hasattr(filledHist, 'fwhm'):
# createLabel(fwhm=filledHist.fwhm)
def main():
fwhmList=[]
for plotNumber in range(len(plotGroups)): #creates a plot
plotDimension = getPlotDimension(plotNumber)
barIDs = getPlotBars(plotNumber)
extractionName = ""
for id in barIDs:
canvas = createCanvas()
createInfoBox()
pad = createPad(plotDimension)
legend = createLegend()
lines=[]
for j in plotGroups[plotNumber]: #creates a line for each variable in the plot
plotVar = var = j[0]
extractionName = plotVar
fileName = j[1]
inFile = r.TFile(fileName+".root","READ")
allData = inFile.Get("LDMX_Events")
hist = createHist(plotDict,plotVar,id)
if plotVar == 'Energy as a function of the incoming particle angle':
angles = [angle for angle in (0,2,4,6,8,10,20,30,40)]
inFiles= [r.TFile('e-1GeV'+str(angle)+"deg.root","READ") for angle in angles ]
allDatas=[f.Get("LDMX_Events") for f in inFiles]
for i in range(len(angles)):
filledHist = fillHist(hist, plotVar, allDatas[i],angle=angles[i])
filledHist.hist.SetMinimum(0.5)
elif plotVar == 'energy response vs. energy (1 plot)':
energies = [energy for energy in (0.5,1,2,4,8)]
inFiles= [r.TFile("e-"+str(energy)+"GeV1k.root","READ") for energy in energies ]
allDatas=[f.Get("LDMX_Events") for f in inFiles]
for i in range(len(energies)):
filledHist = fillHist(hist, plotVar, allDatas[i],angle=energies[i])
else:
beamEnergy=getBeamEnergyFromFileName(fileName)
filledHist = fillHist(hist, plotVar, allData, barID=id, beamEnergy=beamEnergy)
filledHist.hist.SetLineColor(rootColors[len(lines)])
#normaliseHist(plotDimension)
lines.append(copy.deepcopy(filledHist.hist))
legend.AddEntry(lines[-1],fileName,"f")
try: fwhmList.append(filledHist.fwhm)
except:pass
drawLines(plotDimension,lines,options="HIST")
decoratePlot(filledHist,plotDimension,legend)
# canvas.SaveAs("plots/Plot"+str(plotNumber)+"__"+barName(id)+"_linear.png")
canvas.SaveAs("plots/Plot"+str(plotNumber)+"__"+barName(id)+"_linear.png")
file = r.TFile("extractions/"+extractionName+".root", "RECREATE")
for histos in lines:
# print(histos)
histos.SetDirectory(file)
histos.Write()
file.Close()
# if plotDimension==1:
# pad.SetLogy()
# drawLines(plotDimension,lines,options="E")
# decoratePlot(filledHist,plotDimension,legend)
# canvas.SaveAs("plots/Plot"+str(plotNumber)+"__"+barName(id)+"_logarithmic.png")
# canvas.SaveAs("plots/"+str(plotNumber)+".png")
canvas.Close() #memory leak killer
try:
# fwhmList.append(filledHist.fwhm)
del filledHist
except:pass
if len(fwhmList)>1:
import matplotlib.pyplot as plt
if len(fwhmList)==5:
plt.plot((8,4,2,1,0.5),fwhmList,"b*")
elif len(fwhmList)==10:
plt.plot((8,4,2,1,0.5),fwhmList[0:5],"r*",label='Pions')
plt.plot((8,4,2,1,0.5),fwhmList[5:10],"b*",label='Electrons')
plt.ylabel("FWHMs")
plt.xlabel("Energy [GeV]")
plt.title('FWHMs')
plt.legend()
# plt.xscale('log')
plt.grid(visible=True)
plt.savefig('plots/fwhms.png')
main()