-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtest_lhe_reader.py
More file actions
295 lines (237 loc) · 9.43 KB
/
test_lhe_reader.py
File metadata and controls
295 lines (237 loc) · 9.43 KB
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
import gzip
import os
import shutil
from pathlib import Path
from tempfile import NamedTemporaryFile
import pytest
import skhep_testdata
import pylhe
from pylhe import LHEEvent
TEST_FILE_LHE_v1 = skhep_testdata.data_path("pylhe-testfile-pr29.lhe")
TEST_FILE_LHE_v3 = skhep_testdata.data_path("pylhe-testlhef3.lhe")
TEST_FILE_LHE_INITRWGT_WEIGHTS = skhep_testdata.data_path(
"pylhe-testfile-powheg-box-v2-hvq.lhe"
)
TEST_FILE_LHE_RWGT_WGT = skhep_testdata.data_path("pylhe-testfile-powheg-box-v2-W.lhe")
TEST_FILES_LHE_POWHEG = [
skhep_testdata.data_path(f"pylhe-testfile-powheg-box-v2-{proc}.lhe")
for proc in ["Z", "W", "Zj", "trijet", "directphoton", "hvq"]
]
TEST_FILES_LHE_MADGRAPH = [
skhep_testdata.data_path("pylhe-testfile-madgraph-2.0.0-wbj.lhe"),
skhep_testdata.data_path("pylhe-testfile-madgraph-2.2.1-Z-ckkwl.lhe.gz"),
skhep_testdata.data_path("pylhe-testfile-madgraph-2.2.1-Z-fxfx.lhe.gz"),
skhep_testdata.data_path("pylhe-testfile-madgraph-2.2.1-Z-mlm.lhe.gz"),
skhep_testdata.data_path("pylhe-testfile-madgraph5-3.5.8-pp_to_jj.lhe.gz"),
]
TEST_FILES_LHE_PYTHIA = [
skhep_testdata.data_path("pylhe-testfile-pythia-6.413-ttbar.lhe"),
skhep_testdata.data_path("pylhe-testfile-pythia-8.3.14-weakbosons.lhe"),
]
TEST_FILES_LHE_SHERPA = [
skhep_testdata.data_path("pylhe-testfile-sherpa-3.0.1-eejjj.lhe"),
]
TEST_FILES_LHE_WHIZARD = [
skhep_testdata.data_path("pylhe-testfile-whizard-3.1.4-eeWW.lhe"),
]
TEST_FILES_LHE_GENERATORS = [
*TEST_FILES_LHE_MADGRAPH,
*TEST_FILES_LHE_POWHEG,
*TEST_FILES_LHE_PYTHIA,
*TEST_FILES_LHE_SHERPA,
*TEST_FILES_LHE_WHIZARD,
]
@pytest.fixture(scope="session")
def testdata_gzip_file():
test_data = skhep_testdata.data_path("pylhe-testfile-pr29.lhe")
tmp_path = Path(NamedTemporaryFile().name)
# create what is basically pylhe-testfile-pr29.lhe.gz
with open(test_data, "rb") as readfile, gzip.open(tmp_path, "wb") as writefile:
shutil.copyfileobj(readfile, writefile)
yield tmp_path
# teardown
os.remove(tmp_path)
def test_gzip_open(testdata_gzip_file):
assert pylhe._extract_fileobj(TEST_FILE_LHE_v1)
assert pylhe._extract_fileobj(testdata_gzip_file)
# Needs path-like object, not a fileobj
with pytest.raises(TypeError), open(TEST_FILE_LHE_v1, "rb") as fileobj:
pylhe._extract_fileobj(fileobj)
with open(TEST_FILE_LHE_v1, "rb") as fileobj:
assert isinstance(pylhe._extract_fileobj(TEST_FILE_LHE_v1), type(fileobj))
assert isinstance(pylhe._extract_fileobj(Path(TEST_FILE_LHE_v1)), type(fileobj))
assert isinstance(pylhe._extract_fileobj(testdata_gzip_file), gzip.GzipFile)
assert isinstance(pylhe._extract_fileobj(Path(testdata_gzip_file)), gzip.GzipFile)
def test_read_num_events(testdata_gzip_file):
assert pylhe.read_num_events(TEST_FILE_LHE_v1) == 791
assert pylhe.read_num_events(TEST_FILE_LHE_v1) == pylhe.read_num_events(
testdata_gzip_file
)
def test_read_lhe_init_gzipped_file(testdata_gzip_file):
assert pylhe.read_lhe_init(TEST_FILE_LHE_v1) == pylhe.read_lhe_init(
testdata_gzip_file
)
def test_read_lhe_init_v1():
"""
Test method read_lhe_init() on a LesHouchesEvents version="1.0" file.
"""
init_data = pylhe.read_lhe_init(TEST_FILE_LHE_v1)
assert init_data["LHEVersion"] == pytest.approx(1.0)
init_info = init_data["initInfo"]
assert init_info["beamA"] == pytest.approx(1.0)
assert init_info["beamB"] == pytest.approx(2.0)
assert init_info["energyA"] == pytest.approx(1.234567)
assert init_info["energyB"] == pytest.approx(2.345678)
assert init_info["PDFgroupA"] == pytest.approx(3.0)
assert init_info["PDFgroupB"] == pytest.approx(4.0)
assert init_info["PDFsetA"] == pytest.approx(5.0)
assert init_info["PDFsetB"] == pytest.approx(6.0)
assert init_info["weightingStrategy"] == pytest.approx(7.0)
assert init_info["numProcesses"] == pytest.approx(8.0)
assert init_data["procInfo"] == []
def test_read_lhe_init_v3():
"""
Test method read_lhe_init() on a LesHouchesEvents version="3.0" file.
"""
init_data = pylhe.read_lhe_init(TEST_FILE_LHE_v3)
assert len(init_data["weightgroup"]) == 1
assert len(init_data["weightgroup"]["scale_variation"]["weights"]) == 9
def test_read_lhe_v1():
"""
Test method read_lhe() on a LesHouchesEvents version="1.0" file.
"""
events = pylhe.read_lhe(TEST_FILE_LHE_v1)
assert events
for e in events:
assert isinstance(e, LHEEvent)
def test_read_lhe_v3():
"""
Test method read_lhe() on a LesHouchesEvents version="3.0" file.
"""
events = pylhe.read_lhe(TEST_FILE_LHE_v3)
assert events
for e in events:
assert isinstance(e, LHEEvent)
def test_read_lhe_with_attributes_v1():
"""
Test method read_lhe_with_attributes() on a LesHouchesEvents version="1.0" file.
"""
events = pylhe.read_lhe_with_attributes(TEST_FILE_LHE_v1)
assert events
for e in events:
assert isinstance(e, LHEEvent)
def test_read_lhe_with_attributes_v3():
"""
Test method read_lhe_with_attributes() on a LesHouchesEvents version="3.0" file.
"""
events = pylhe.read_lhe_with_attributes(TEST_FILE_LHE_v3)
assert events
for e in events:
assert isinstance(e, LHEEvent)
@pytest.mark.parametrize("file", TEST_FILES_LHE_GENERATORS)
def test_read_lhe_generator(file):
"""
Test method read_lhe() on several types of LesHouchesEvents generator files.
"""
events = pylhe.read_lhe(file)
assert events
for e in events:
assert isinstance(e, LHEEvent)
@pytest.mark.parametrize("file", TEST_FILES_LHE_GENERATORS)
def test_read_lhe_with_attributes_generator(file):
"""
Test method read_lhe_with_attributes() on several types of LesHouchesEvents generator files.
"""
events = pylhe.read_lhe_with_attributes(file)
assert events
for e in events:
assert isinstance(e, LHEEvent)
@pytest.mark.parametrize(
"file", [TEST_FILE_LHE_INITRWGT_WEIGHTS, TEST_FILE_LHE_RWGT_WGT]
)
def test_read_lhe_file(file):
"""
Test that the read_lhe_file function works as the individual reads.
"""
lhefile = pylhe.read_lhe_file(file, with_attributes=False)
lheinit = pylhe.read_lhe_init(file)
lheevents = pylhe.read_lhe(file)
assert lheinit == lhefile.init
assert next(lheevents).tolhe() == next(lhefile.events).tolhe()
lhefile = pylhe.read_lhe_file(file, with_attributes=True)
lheevents = pylhe.read_lhe_with_attributes(file)
assert lheinit == lhefile.init
assert next(lheevents).tolhe() == next(lhefile.events).tolhe()
def test_read_lhe_initrwgt_weights():
"""
Test the weights from initrwgt with a weights list.
"""
events = pylhe.read_lhe_with_attributes(TEST_FILE_LHE_INITRWGT_WEIGHTS)
assert events
for e in events:
assert isinstance(e, LHEEvent)
assert len(e.weights) > 0
def test_read_lhe_rwgt_wgt():
"""
Test the weights from rwgt with a wgt list.
"""
events = pylhe.read_lhe_with_attributes(TEST_FILE_LHE_RWGT_WGT)
assert events
for e in events:
assert isinstance(e, LHEEvent)
assert len(e.weights) > 0
def test_issue_102():
"""
Test a file containing lines starting with "#aMCatNLO".
"""
assert pylhe.read_num_events(TEST_FILE_LHE_v3) == 59
assert len(list(pylhe.read_lhe(TEST_FILE_LHE_v3))) == len(
list(pylhe.read_lhe_with_attributes(TEST_FILE_LHE_v3))
)
def test_read_lhe_init_raises():
"""
Test that the <init> block raises AttributeErrors on faulty inputs.
"""
with pytest.raises(
AttributeError, match="weightgroup must have attribute 'type' or 'name'."
):
pylhe.LHEInit.fromstring(
"""<init>
2212 2212 4.0000000e+03 4.0000000e+03 -1 -1 21100 21100 -4 1
5.0109086e+01 8.9185414e-02 5.0109093e+01 66
<initrwgt>
<weightgroup combine="envelope">
<weight id="1001">muR=0.10000E+01 muF=0.10000E+01</weight>
<weight id="1002">muR=0.10000E+01 muF=0.20000E+01</weight>
<weight id="1003">muR=0.10000E+01 muF=0.50000E+00</weight>
<weight id="1004">muR=0.20000E+01 muF=0.10000E+01</weight>
<weight id="1005">muR=0.20000E+01 muF=0.20000E+01</weight>
<weight id="1006">muR=0.20000E+01 muF=0.50000E+00</weight>
<weight id="1007">muR=0.50000E+00 muF=0.10000E+01</weight>
<weight id="1008">muR=0.50000E+00 muF=0.20000E+01</weight>
<weight id="1009">muR=0.50000E+00 muF=0.50000E+00</weight>
</weightgroup>
</initrwgt>
</init>"""
)
with pytest.raises(AttributeError, match="weight must have attribute 'id'"):
pylhe.LHEInit.fromstring(
"""<init>
2212 2212 4.0000000e+03 4.0000000e+03 -1 -1 21100 21100 -4 1
5.0109086e+01 8.9185414e-02 5.0109093e+01 66
<initrwgt>
<weightgroup name="a fake name" combine="envelope">
<spam>muR=0.10000E+01 muF=0.10000E+01</spam>
<weight id="1001">muR=0.10000E+01 muF=0.10000E+01</weight>
<weight id="1002">muR=0.10000E+01 muF=0.20000E+01</weight>
<weight id="1003">muR=0.10000E+01 muF=0.50000E+00</weight>
<weight id="1004">muR=0.20000E+01 muF=0.10000E+01</weight>
<weight id="1005">muR=0.20000E+01 muF=0.20000E+01</weight>
<weight id="1006">muR=0.20000E+01 muF=0.50000E+00</weight>
<weight id="1007">muR=0.50000E+00 muF=0.10000E+01</weight>
<weight id="1008">muR=0.50000E+00 muF=0.20000E+01</weight>
<weight>muR=0.50000E+00 muF=0.50000E+00</weight>
</weightgroup>
</initrwgt>
</init>"""
)