Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Extend coverage of classes and add cover of read_lhe #143

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions tests/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ def test_LHEEventInfo_no_default_init():
_ = LHEEventInfo()


def test_LHEEventInfo_fromstring():
"""
Data taken from the first <event> block of scikit-hep-testdata's package
"pylhe-testlhef3.lhe" file.
"""
data = "5 66 0.50109093E+02 0.14137688E+03 0.75563862E-02 0.12114027E+00"
event_info = LHEEventInfo.fromstring(data)

assert event_info.nparticles == 5
assert event_info.pid == 66
assert event_info.weight == pytest.approx(0.50109093e02)
assert event_info.scale == pytest.approx(0.14137688e03)
assert event_info.aqed == pytest.approx(0.75563862e-02)
assert event_info.aqcd == pytest.approx(0.12114027e00)


def test_LHEFile_default_init():
assert LHEFile() is not None

Expand All @@ -33,10 +49,100 @@ def test_LHEInit_default_init():
assert LHEInit() is not None


def test_LHEInit_fromstring():
"""
Data taken from the <init> block of scikit-hep-testdata's package
"pylhe-testlhef3.lhe" file.
"""
data = "2212 2212 0.40000000E+04 0.40000000E+04 -1 -1 21100 21100 -4 1"
result = {
"beamA": 2212.0,
"beamB": 2212.0,
"energyA": 4000.0,
"energyB": 4000.0,
"PDFgroupA": -1.0,
"PDFgroupB": -1.0,
"PDFsetA": 21100.0,
"PDFsetB": 21100.0,
"weightingStrategy": -4.0,
"numProcesses": 1.0,
}
assert LHEInit.fromstring(data) == result


def test_LHEParticle_no_default_init():
with pytest.raises(RuntimeError):
_ = LHEParticle()


def test_LHEParticle_fromstring():
"""
Data taken from the first <event> block of scikit-hep-testdata's package
"pylhe-testlhef3.lhe" file.
"""
particles = [
" 5 -1 0 0 501 0 0.00000000E+00 0.00000000E+00 0.14322906E+03 0.14330946E+03 0.48000000E+01 0.0000E+00 0.0000E+00",
" 2 -1 0 0 502 0 0.00000000E+00 0.00000000E+00 -.93544317E+03 0.93544323E+03 0.33000000E+00 0.0000E+00 0.0000E+00",
" 24 1 1 2 0 0 -.84258804E+02 -.15708566E+03 -.10629600E+03 0.22257162E+03 0.80398000E+02 0.0000E+00 0.0000E+00",
" 5 1 1 2 501 0 -.13668073E+03 -.36307424E+02 -.40614473E+02 0.14721558E+03 0.48000000E+01 0.0000E+00 0.0000E+00",
" 1 1 1 2 502 0 0.22093954E+03 0.19339308E+03 -.64530364E+03 0.70896548E+03 0.33000000E+00 0.0000E+00 0.0000E+00",
]

particle_objs = [LHEParticle.fromstring(p) for p in particles]

assert [p.id for p in particle_objs] == [5, 2, 24, 5, 1]
assert [p.status for p in particle_objs] == [-1.0, -1.0, 1.0, 1.0, 1.0]
assert [p.mother1 for p in particle_objs] == [0.0, 0.0, 1.0, 1.0, 1.0]
assert [p.mother2 for p in particle_objs] == [0.0, 0.0, 2.0, 2.0, 2.0]
assert [p.color1 for p in particle_objs] == [501.0, 502.0, 0.0, 501.0, 502.0]
assert [p.color2 for p in particle_objs] == [0.0, 0.0, 0.0, 0.0, 0.0]
assert [p.px for p in particle_objs] == [
0.0,
0.0,
-84.258804,
-136.68073,
220.93954,
]
assert [p.py for p in particle_objs] == [
0.0,
0.0,
-157.08566,
-36.307424,
193.39308,
]
assert [p.pz for p in particle_objs] == [
143.22906,
-935.44317,
-106.296,
-40.614473,
-645.30364,
]
assert [p.e for p in particle_objs] == [
143.30946,
935.44323,
222.57162,
147.21558,
708.96548,
]
assert [p.m for p in particle_objs] == [4.8, 0.33, 80.398, 4.8, 0.33]
assert [p.lifetime for p in particle_objs] == [0.0, 0.0, 0.0, 0.0, 0.0]
assert [p.spin for p in particle_objs] == [0.0, 0.0, 0.0, 0.0, 0.0]


def test_LHEProcInfo_default_init():
assert LHEProcInfo() is not None


def test_LHEProcInfo_fromstring():
"""
Data taken from the <init> block of scikit-hep-testdata's package
"pylhe-testlhef3.lhe" file.
"""
data = "0.50109086E+02 0.89185414E-01 0.50109093E+02 66"
result = {
"xSection": 50.109086,
"error": 0.089185414,
"unitWeight": 50.109093,
"procId": 66.0,
}
assert LHEProcInfo.fromstring(data) == result
60 changes: 55 additions & 5 deletions tests/test_lhe_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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")
Expand Down Expand Up @@ -51,11 +52,16 @@ def test_read_num_events(testdata_gzip_file):
)


def test_read_lhe_init(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)
Expand All @@ -72,17 +78,61 @@ def test_read_lhe_init(testdata_gzip_file):
assert init_info["weightingStrategy"] == pytest.approx(7.0)
assert init_info["numProcesses"] == pytest.approx(8.0)

assert init_data["procInfo"] == []


def test_read_lhe(testdata_gzip_file):
assert pylhe.read_lhe(testdata_gzip_file)
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_internals():

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 e is not None
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)


def test_issue_102():
Expand Down