Skip to content

Commit

Permalink
Merge pull request #182 from jacanchaplais/feature/lhe-classmethod-177
Browse files Browse the repository at this point in the history
Added classmethod to read LHE event data #177
  • Loading branch information
jacanchaplais authored Jun 3, 2024
2 parents 13cf0a5 + e59cbb4 commit 9ecf4de
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
29 changes: 29 additions & 0 deletions graphicle/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@
DataType = ty.TypeVar("DataType")


class LheEventInterface(ty.Protocol):
"""Interface for a generic Les Houches event.
:group: base
.. versionadded:: 0.4.0
"""

@property
def pdg(self) -> IntVector:
...

@property
def pmu(self) -> AnyVector:
...

@property
def color(self) -> AnyVector:
...

@property
def helicity(self) -> HalfIntVector:
...

@property
def status(self) -> HalfIntVector:
...


class EventInterface(ty.Protocol):
"""Defines the interface for a generic event object expected by
graphicle's routines. Attributes are stored as numpy arrays, with
Expand Down
30 changes: 30 additions & 0 deletions graphicle/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2296,6 +2296,36 @@ def copy(self) -> "ParticleSet":
"""Copies the underlying data into a new ParticleSet instance."""
return _composite_copy(self)

@classmethod
def from_lhe_event(cls, event: base.LheEventInterface) -> "ParticleSet":
"""Creates a ParticleSet instance directly from a data structure
holding LHE data. This is useful when you want to study the hard
process, without needing to shower or hadronize.
.. versionadded:: 0.4.0
Parameters
----------
event : LheEventInterface
A data structure with attributes "pdg", "pmu", "color",
"helicity", and "status". These attributes provide access to
numpy arrays, which hold the underlying Les Houches event
data.
Returns
-------
ParticleSet
A composite object, wrapping the data provided in Graphicle
objects, and providing a unified interface to them.
"""
props = ("pdg", "pmu", "color", "helicity", "status")
pcls = cls.from_numpy(**{name: getattr(event, name) for name in props})
pcls.final = MaskArray(np.zeros(len(pcls), dtype=np.bool_))
status_map = {-1: -21, 1: -23, -2: -22, 2: -22, 3: -22, -9: -12}
for old_code, new_code in status_map.items():
pcls.status.data[pcls.status.data == old_code] = new_code
return pcls

@classmethod
def from_numpy(
cls,
Expand Down

0 comments on commit 9ecf4de

Please sign in to comment.