diff --git a/README.md b/README.md index d56be5ad..2a2a68b8 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,38 @@ event.graph.render(filename="test", format="png", cleanup=True) event.graph.render(filename="test", format="pdf", cleanup=True) ``` +## Get started + +The example below provides a simple overview. +Full functionality can be inspected from the functions provided in the `pylhe` module. + +```python +import itertools + +# You can use LHE files from scikit-hep-testdata +from skhep_testdata import data_path + +import pylhe + +lhe_file = data_path("pylhe-testlhef3.lhe") +events = pylhe.read_lhe_with_attributes(lhe_file) +print(f"Number of events: {pylhe.read_num_events(lhe_file)}") + +# Get event 1 +event = next(itertools.islice(events, 1, 2)) + +# A DOT language graph of the event can be inspected as follows +print(event.graph.source) + +# The graph is nicely displayed as SVG in Jupyter notebooks +event + +# To save a DOT graph render the graph to a supported image format +# (refer to the Graphviz documentation for more) +event.graph.render(filename="test", format="png", cleanup=True) +event.graph.render(filename="test", format="pdf", cleanup=True) +``` + ## Citation The preferred BibTeX entry for citation of `pylhe` is diff --git a/src/pylhe/__init__.py b/src/pylhe/__init__.py index 6b83fd66..1bafcc5d 100644 --- a/src/pylhe/__init__.py +++ b/src/pylhe/__init__.py @@ -283,9 +283,10 @@ def read_lhe(filepath): with _extract_fileobj(filepath) as fileobj: for event, element in ET.iterparse(fileobj, events=["end"]): if element.tag == "event": - data = element.text.split("\n")[1:-1] + data = element.text.strip().split("\n") eventdata, particles = data[0], data[1:] eventinfo = LHEEventInfo.fromstring(eventdata) + particles = particles[: int(eventinfo.nparticles)] particle_objs = [LHEParticle.fromstring(p) for p in particles] yield LHEEvent(eventinfo, particle_objs) except ET.ParseError as excep: @@ -303,7 +304,7 @@ def read_lhe_with_attributes(filepath): for event, element in ET.iterparse(fileobj, events=["end"]): if element.tag == "event": eventdict = {} - data = element.text.split("\n")[1:-1] + data = element.text.strip().split("\n") eventdata, particles = data[0], data[1:] eventdict["eventinfo"] = LHEEventInfo.fromstring(eventdata) eventdict["particles"] = [] diff --git a/tests/test_lhe_reader.py b/tests/test_lhe_reader.py index 87f6baa1..0b022335 100644 --- a/tests/test_lhe_reader.py +++ b/tests/test_lhe_reader.py @@ -66,5 +66,25 @@ def test_lhe_init(testdata_gzip_file): def test_read_lhe(testdata_gzip_file): - assert pylhe.read_lhe(TEST_FILE) assert pylhe.read_lhe(testdata_gzip_file) + + +def test_read_lhe_internals(): + events = pylhe.read_lhe(TEST_FILE) + + assert events + for e in events: + assert e is not None + + +def test_issue_102(): + """ + Test a file containing lines starting with "#aMCatNLO". + """ + test_file = skhep_testdata.data_path("pylhe-testlhef3.lhe") + + assert pylhe.read_num_events(test_file) == 59 + assert ( + pylhe.read_lhe(test_file).__sizeof__() + == pylhe.read_lhe_with_attributes(test_file).__sizeof__() + )