-
Notifications
You must be signed in to change notification settings - Fork 0
/
LHEReader.py
365 lines (323 loc) · 12.8 KB
/
LHEReader.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import sys
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors
from matplotlib.backends.backend_pdf import PdfPages
import math
from tqdm import tqdm
import matplotlib.gridspec as gridspec # more plotting
import gzip
##########################
# FUNCTIONS
##########################
# Functions to handle colors in matplotlib plots:
# choose the next colour -- for plotting
ccount = 0
def next_color():
global ccount
colors = ['green', 'orange', 'red', 'blue', 'black', 'cyan', 'magenta', 'brown', 'violet'] # 9 colours
color_chosen = colors[ccount]
if ccount < 8:
ccount = ccount + 1
else:
ccount = 0
return color_chosen
# do not increment colour in this case:
def same_color():
global ccount
colors = ['green', 'orange', 'red', 'blue', 'black', 'cyan', 'magenta', 'brown', 'violet'] # 9 colours
color_chosen = colors[ccount-1]
return color_chosen
# reset the color counter:
def reset_color():
global ccount
ccount = 0
# function to plot histograms: including the cross section normalization and STACKED
# DATA_array contains ARRAYS of data for each event. Each array represents a different type of input (e.g. a run with different parameters, etc.).
# CrossSection_array contains ARRAYS of cross sections
# plot_type is simply the main name of the plot
# plotnames_multi has to be an array of equal size to DATA_array
# custom_bins can be provided for the desired observable
def histogram_multi_xsec_stacked(DATA_array, CrossSection_array, plot_type, plotnames_multi, xlabel='', ylabel='fraction/bin', nbins=50, title='', custom_bins=[], ylogbool=False, xlogbool=False, outputfiletag=''):
print('---')
print('plotting', plot_type)
# plot settings ########
ylab = ylabel # the ylabel
xlab = xlabel # the x label
# log scale?
ylog = ylogbool # whether to plot y in log scale
xlog = xlogbool # whether to plot x in log scale
# construct the axes for the plot
# no need to modify this if you just need one plot
gs = gridspec.GridSpec(4, 4)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid(False)
# loop over the DATA in the DATA_array
# get the errors per bin and normalize so that we obtain fraction of events/bin
dd = 0
X = []
Y = []
binstot = []
for DATA in DATA_array:
if len(custom_bins) == 0:
bins, edges = np.histogram(np.array(DATA), bins=nbins)
else:
bins, edges = np.histogram(np.array(DATA), bins=custom_bins)
errors = np.divide(np.sqrt(bins), bins, out=np.zeros_like(np.sqrt(bins)), where=bins!=0.)
bins = bins/float(len(DATA))
errors = bins*errors
#print(bins)
#print(errors)
left,right = edges[:-1],edges[1:]
X = np.array([left,right]).T.flatten()
if len(Y) == 0:
Y = np.multiply(np.array([bins,bins]).T.flatten(),CrossSection_array[dd])
binstot = bins/float(len(DATA))*CrossSection_array[dd]
else:
Y = Y + np.multiply(np.array([bins,bins]).T.flatten(),CrossSection_array[dd])
binstot = binstot + bins/float(len(DATA))*CrossSection_array[dd]
dd = dd+1
center = (edges[:-1] + edges[1:]) / 2
plt.plot(X,Y, label='total', color=next_color(), lw=1)
#plt.errorbar(X, Y, yerr=., color=same_color(), lw=0, elinewidth=1, capsize=1)
# set the ticks, labels and limits etc.
ax.set_ylabel(ylab, fontsize=20)
ax.set_xlabel(xlab, fontsize=20)
# choose x and y log scales
if ylog:
ax.set_yscale('log')
else:
ax.set_yscale('linear')
if xlog:
ax.set_xscale('log')
else:
ax.set_xscale('linear')
# set the limits on the x and y axes if required below:
# (this is not implemented automatically yet)
#xmin = 0.
#xmax = 1500.
#ymin = 0.
#ymax = 0.09
#plt.xlim([0,400])
#plt.ylim([0.06,0.15])
# create legend and plot/font size
ax.legend()
ax.legend(loc="upper right", numpoints=1, frameon=False, prop={'size':8})
# set the title of the figure
if title != '':
plt.title(title)
# save the figure
print('saving the figure')
# save the figure in PDF format
if outputfiletag != '':
infile = plot_type + '-' + outputfiletag + '_stacked.dat'
else:
infile = plot_type + '_stacked.dat'
print('output in', infile.replace('.dat','.pdf'))
plt.savefig(infile.replace('.dat','.pdf'), bbox_inches='tight')
#plt.close(fig)
plt.show()
# function to plot histograms: including the cross section normalization
# CrossSection_array contains ARRAYS of cross sections
# DATA_array contains ARRAYS of data for each event. Each array represents a different type of input (e.g. a run with different parameters, etc.).
# plot_type is simply the main name of the plot
# plotnames_multi has to be an array of equal size to DATA_array
# custom_bins can be provided for the desired observable
def histogram_multi_xsec(DATA_array, CrossSection_array, plot_type, plotnames_multi, xlabel='', ylabel='fraction/bin', nbins=50, title='', custom_bins=[], ylogbool=False, xlogbool=False, outputfiletag=''):
print('---')
print('plotting', plot_type)
# plot settings ########
ylab = ylabel # the ylabel
xlab = xlabel # the x label
# log scale?
ylog = ylogbool # whether to plot y in log scale
xlog = xlogbool # whether to plot x in log scale
# construct the axes for the plot
# no need to modify this if you just need one plot
gs = gridspec.GridSpec(4, 4)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid(False)
# loop over the DATA in the DATA_array
# get the errors per bin and normalize so that we obtain fraction of events/bin
dd = 0
for DATA in DATA_array:
if len(custom_bins) == 0:
bins, edges = np.histogram(np.array(DATA), bins=nbins)
else:
bins, edges = np.histogram(np.array(DATA), bins=custom_bins)
errors = np.divide(np.sqrt(bins), bins, out=np.zeros_like(np.sqrt(bins)), where=bins!=0.)
bins = bins/float(len(DATA))
errors = bins*errors
#print(bins)
#print(errors)
left,right = edges[:-1],edges[1:]
X = np.array([left,right]).T.flatten()
Y = np.multiply(np.array([bins,bins]).T.flatten(),CrossSection_array[dd])
plt.plot(X,Y, label=plotnames_multi[dd], color=next_color(), lw=1)
#center = (edges[:-1] + edges[1:]) / 2
#plt.errorbar(center, bins, yerr=errors, color=same_color(), lw=0, elinewidth=1, capsize=1)
dd = dd+1
# set the ticks, labels and limits etc.
ax.set_ylabel(ylab, fontsize=20)
ax.set_xlabel(xlab, fontsize=20)
# choose x and y log scales
if ylog:
ax.set_yscale('log')
else:
ax.set_yscale('linear')
if xlog:
ax.set_xscale('log')
else:
ax.set_xscale('linear')
# set the limits on the x and y axes if required below:
# (this is not implemented automatically yet)
#xmin = 0.
#xmax = 1500.
#ymin = 0.
#ymax = 0.09
#plt.xlim([0,400])
#plt.ylim([0.06,0.15])
# create legend and plot/font size
ax.legend()
ax.legend(loc="upper right", numpoints=1, frameon=False, prop={'size':8})
# set the title of the figure
if title != '':
plt.title(title)
# save the figure
print('saving the figure')
# save the figure in PDF format
if outputfiletag != '':
infile = plot_type + '-' + outputfiletag + '.dat'
else:
infile = plot_type + '.dat'
print('output in', infile.replace('.dat','.pdf'))
plt.savefig(infile.replace('.dat','.pdf'), bbox_inches='tight')
#plt.close(fig)
plt.show()
# function to plot histograms
# DATA_array contains ARRAYS of data for each event. Each array represents a different type of input (e.g. a run with different parameters, etc.).
# plot_type is simply the main name of the plot
# plotnames_multi has to be an array of equal size to DATA_array
# custom_bins can be provided for the desired observable
def histogram_multi(DATA_array, plot_type, plotnames_multi, xlabel='', ylabel='fraction/bin', nbins=50, title='', custom_bins=[], ylogbool=False, xlogbool=False, outputfiletag=''):
print('---')
print('plotting', plot_type)
# plot settings ########
ylab = ylabel # the ylabel
xlab = xlabel # the x label
# log scale?
ylog = ylogbool # whether to plot y in log scale
xlog = xlogbool # whether to plot x in log scale
# construct the axes for the plot
# no need to modify this if you just need one plot
gs = gridspec.GridSpec(4, 4)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid(False)
# loop over the DATA in the DATA_array
# get the errors per bin and normalize so that we obtain fraction of events/bin
dd = 0
for DATA in DATA_array:
if len(custom_bins) == 0:
bins, edges = np.histogram(np.array(DATA), bins=nbins)
else:
bins, edges = np.histogram(np.array(DATA), bins=custom_bins)
errors = np.divide(np.sqrt(bins), bins, out=np.zeros_like(np.sqrt(bins)), where=bins!=0.)
bins = bins/float(len(DATA))
errors = bins*errors
#print(bins)
#print(errors)
left,right = edges[:-1],edges[1:]
X = np.array([left,right]).T.flatten()
Y = np.array([bins,bins]).T.flatten()
plt.plot(X,Y, label=plotnames_multi[dd], color=next_color(), lw=1)
center = (edges[:-1] + edges[1:]) / 2
plt.errorbar(center, bins, yerr=errors, color=same_color(), lw=0, elinewidth=1, capsize=1)
dd = dd+1
# set the ticks, labels and limits etc.
ax.set_ylabel(ylab, fontsize=20)
ax.set_xlabel(xlab, fontsize=20)
# choose x and y log scales
if ylog:
ax.set_yscale('log')
else:
ax.set_yscale('linear')
if xlog:
ax.set_xscale('log')
else:
ax.set_xscale('linear')
# set the limits on the x and y axes if required below:
# (this is not implemented automatically yet)
#xmin = 0.
#xmax = 1500.
#ymin = 0.
#ymax = 0.09
#plt.xlim([0,400])
#plt.ylim([0.06,0.15])
# create legend and plot/font size
ax.legend()
ax.legend(loc="upper right", numpoints=1, frameon=False, prop={'size':8})
# set the title of the figure
if title != '':
plt.title(title)
# save the figure
print('saving the figure')
# save the figure in PDF format
if outputfiletag != '':
infile = plot_type + '-' + outputfiletag + '.dat'
else:
infile = plot_type + '.dat'
print('output in', infile.replace('.dat','.pdf'))
plt.savefig(infile.replace('.dat','.pdf'), bbox_inches='tight')
plt.show()
# function to read lhe files in and grab the particle momenta for each event
# it also grabs the weight of each event, as well as the multiple weights, if present
# the return variables are: events, in which each entry is a set of particle 4-momenta in the format:
# [id, status, px, py, pz, e, m] -> id is the PDG id, status is the LHE status (i.e. incoming: -1, final: 1)
# weights contains the weight of each event
# multiweights contains the multiple weights of each event
def readlhefile(infile):
if infile.endswith('.gz'):
my_open = gzip.open
else:
my_open = open
infile_read = my_open(infile, 'rt')
numevents = 0
reading_event = False
events = []
weights = []
multiweights = []
for line in infile_read:
if '<event>' in line:
particles = []
multiweight = {}
#print('reading new event')
numevents = numevents + 1
reading_event = True
if reading_event is True:
if '</event>' in line:
reading_event = False
events.append(particles)
weights.append(weight)
multiweights.append(multiweight)
#print(line, len(line.split()))
if len(line.split()) == 6:
weight = float(line.split()[2])
if len(line.split()) == 13:
particles.append(read_momenta(line))
if len(line.split()) == 4:
multiweight[line.split()[1].replace('id=', '').replace('>', '').replace("'", '')] = float(line.split()[2])
#print('multiweight[', line.split()[1].replace('id=', '').replace('>', '').replace("'", ''), ']=', line.split()[2])
return events, weights, multiweights
# read the particle information for the given particle line in the LHE file
def read_momenta(inputline):
id = int(inputline.split()[0])
status = int(inputline.split()[1])
px = float(inputline.split()[6])
py = float(inputline.split()[7])
pz = float(inputline.split()[8])
e = float(inputline.split()[9])
m = float(inputline.split()[10])
return [id, status, px, py, pz, e, m]