Skip to content
Closed
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
145 changes: 79 additions & 66 deletions src/pylhe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ def fromstring(cls, string: str) -> "LHEEventInfo":
"""
values = string.split()
return cls(
nparticles=int(float(values[0])),
pid=int(float(values[1])),
nparticles=int(values[0]),
pid=int(values[1]),
weight=float(values[2]),
scale=float(values[3]),
aqed=float(values[4]),
Expand Down Expand Up @@ -256,12 +256,12 @@ def fromstring(cls, string: str) -> "LHEParticle":
"""
values = string.split()
return cls(
id=int(float(values[0])),
status=int(float(values[1])),
mother1=int(float(values[2])),
mother2=int(float(values[3])),
color1=int(float(values[4])),
color2=int(float(values[5])),
id=int(values[0]),
status=int(values[1]),
mother1=int(values[2]),
mother2=int(values[3]),
color1=int(values[4]),
color2=int(values[5]),
px=float(values[6]),
py=float(values[7]),
pz=float(values[8]),
Expand Down Expand Up @@ -354,16 +354,16 @@ def fromstring(cls, string: str) -> "LHEInitInfo":
"""
values = string.split()
return cls(
beamA=int(float(values[0])),
beamB=int(float(values[1])),
beamA=int(values[0]),
beamB=int(values[1]),
energyA=float(values[2]),
energyB=float(values[3]),
PDFgroupA=int(float(values[4])),
PDFgroupB=int(float(values[5])),
PDFsetA=int(float(values[6])),
PDFsetB=int(float(values[7])),
weightingStrategy=int(float(values[8])),
numProcesses=int(float(values[9])),
PDFgroupA=int(values[4]),
PDFgroupB=int(values[5]),
PDFsetA=int(values[6]),
PDFsetB=int(values[7]),
weightingStrategy=int(values[8]),
numProcesses=int(values[9]),
)


Expand Down Expand Up @@ -399,7 +399,7 @@ def fromstring(cls, string: str) -> "LHEProcInfo":
xSection=float(values[0]),
error=float(values[1]),
unitWeight=float(values[2]),
procId=int(float(values[3])),
procId=int(values[3]),
)


Expand Down Expand Up @@ -640,58 +640,71 @@ def _fromcontext(
with_attributes: bool,
) -> Iterator["LHEEvent"]:
index_map = _get_index_to_id_map(lheinit) if with_attributes else {}
for event, element in context:
if event == "end" and element.tag == "event":
if element.text is None:
err = "<event> block has no text."
raise ValueError(err)
idx = 0

data = element.text.strip().split("\n")
eventdata_str, particles_str = data[0], data[1:]

eventinfo = LHEEventInfo.fromstring(eventdata_str)
particles = [
LHEParticle.fromstring(p)
for p in particles_str
if not p.strip().startswith("#")
]

if with_attributes:
weights = {}
attrib = element.attrib
optional = [
p.strip() for p in particles_str if p.strip().startswith("#")
try:
for event, element in context:
if event == "end" and element.tag == "event":
idx = idx + 1
if element.text is None:
err = "<event> block has no text."
raise ValueError(err)

data = element.text.strip().split("\n")
eventdata_str = data[0]
particles_str = [p.strip() for p in data[1:]]

eventinfo = LHEEventInfo.fromstring(eventdata_str)
particles = [
LHEParticle.fromstring(p)
for p in particles_str
if not p.startswith("#")
]

for sub in element:
if sub.tag == "weights":
if sub.text is None:
err = "<weights> block has no text."
raise ValueError(err)
for i, w in enumerate(sub.text.split()):
if w and index_map[i] not in weights:
weights[index_map[i]] = float(w)
elif sub.tag == "rwgt":
for r in sub:
if r.tag == "wgt":
if r.text is None:
err = "<wgt> block has no text."
raise ValueError(err)
weights[r.attrib["id"]] = float(r.text.strip())

yield LHEEvent(
eventinfo=eventinfo,
particles=particles,
weights=weights,
attributes=attrib,
optional=optional,
)
else:
yield LHEEvent(eventinfo, particles)

# Clear memory
element.clear()
root.clear()
if with_attributes:
weights = {}
attrib = element.attrib
optional = [p for p in particles_str if p.startswith("#")]

for sub in element:
if sub.tag == "weights":
if sub.text is None:
err = "<weights> block has no text."
raise ValueError(err)
weights.update(
{
index_map[i]: float(w)
for i, w in enumerate(
sub.text.split()[::-1]
) # reversed order to only keep the first occurrence in case of duplicates
if w and index_map[i] not in weights
}
)
elif sub.tag == "rwgt":
for r in sub:
if r.tag == "wgt":
if r.text is None:
err = "<wgt> block has no text."
raise ValueError(err)
weights[r.attrib["id"]] = float(r.text.strip())

yield LHEEvent(
eventinfo=eventinfo,
particles=particles,
weights=weights,
attributes=attrib,
optional=optional,
)
else:
yield LHEEvent(eventinfo, particles)

# Clear memory
element.clear()
# Clear the root every 100 elements
if idx % 100 == 0:
root.clear()
finally:
root.clear()

@property
def graph(self) -> graphviz.Digraph:
Expand Down