Skip to content

Commit

Permalink
fix: limit number of particles using the eventinfo (#103)
Browse files Browse the repository at this point in the history
* take particles up to the eventinfo.nparticles. otherwise put into the optinal field

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* synchronize with the master branch

* Add test

* Please linting checks

* ci: Add semantic PR check GHA workflow (#123)

* As https://github.com/zeke/semantic-pull-requests is no longer maintained
and is down, use the suggested alternative of
https://github.com/amannn/action-semantic-pull-request to check that PR titles
follow the Conventional Commits spec.

* feat: Add IPython visualisation and switch to graphviz (#118)

* Add graphviz>=0.12.0 as a core dependency.
* Add graphviz.Digrap graph object as .graph property to LHEFile.
   - Add support for IPython visualization.
* Add test for LHEEvent_graph.

* refactor: Use absolute imports over explicit relative imports (#124)

* Change all explicit relative imports to absolute imports.
   - Provides clearer more explicit imports for new developers.
* Add absolufy-imports to pre-commit hooks

* feat: Use tbump over bump2version (#125)

* Add tbump to 'develop' extra and remove bump2version.
* Add tbump.toml to configure tbump.
   - Allow for valid versions to follow SemVer and also support release
candidates: <major>.<minor>.<patch>rc<candidate>
* Remove .bumpversion.cfg.

* Bump version: 0.2.1 → 0.3.0

* docs: Update dev team affiliations (#126)

* Lukas Heinrich is a professor at Technical University of Munich as of March 2022.
* Matthew Feickert is a postdoc at University of Wisconsin-Madison as of June 2022.

* docs: Add Eduardo Rodrigues to dev team (#127)

* Add Eduardo Rodrigues to development team listing.
   - Update Zenodo citation metadata, recommended citation, and setup.cfg.

* chore: [pre-commit.ci] pre-commit autoupdate (#129)

* Update pre-commit hooks:
   - github.com/asottile/pyupgrade: v2.32.0 → v2.32.1

* docs: Add a 'get started' example to the README (#130)

* Add example to README that shows how to open an LHE file and interact with
the events.

* chore: [pre-commit.ci] pre-commit autoupdate (#133)

* Update pre-commit hooks:
   - github.com/pre-commit/pre-commit-hooks: v4.2.0 → v4.3.0
   - github.com/asottile/pyupgrade: v2.31.1 → v2.34.0
   - github.com/psf/black: 22.3.0 → 22.6.0

* take particles up to the eventinfo.nparticles. otherwise put into the optinal field

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* synchronize with the master branch

* Cover lines added/changed

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: eduardo-rodrigues <[email protected]>
Co-authored-by: Matthew Feickert <[email protected]>
  • Loading branch information
4 people authored Aug 29, 2022
1 parent 48f528e commit d84e325
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/pylhe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"] = []
Expand Down
22 changes: 21 additions & 1 deletion tests/test_lhe_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
)

0 comments on commit d84e325

Please sign in to comment.