Skip to content

Commit 9898908

Browse files
committed
root clear should be in the outer loop
1 parent 2d4503a commit 9898908

File tree

1 file changed

+55
-52
lines changed

1 file changed

+55
-52
lines changed

src/pylhe/__init__.py

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -641,59 +641,62 @@ def _fromcontext(
641641
) -> Iterator["LHEEvent"]:
642642
index_map = _get_index_to_id_map(lheinit) if with_attributes else {}
643643

644-
for idx, (event, element) in enumerate(context, 1):
645-
if event == "end" and element.tag == "event":
646-
if element.text is None:
647-
err = "<event> block has no text."
648-
raise ValueError(err)
649-
650-
data = element.text.strip().split("\n")
651-
eventdata_str, particles_str = data[0], data[1:]
652-
particles_str = [p.strip() for p in particles_str]
653-
654-
eventinfo = LHEEventInfo.fromstring(eventdata_str)
655-
particles = [
656-
LHEParticle.fromstring(p)
657-
for p in particles_str
658-
if not p.startswith("#")
659-
]
660-
661-
if with_attributes:
662-
weights = {}
663-
attrib = element.attrib
664-
optional = [p for p in particles_str if p.startswith("#")]
665-
666-
for sub in element:
667-
if sub.tag == "weights":
668-
if sub.text is None:
669-
err = "<weights> block has no text."
670-
raise ValueError(err)
671-
for i, w in enumerate(sub.text.split()):
672-
if w and index_map[i] not in weights:
673-
weights[index_map[i]] = float(w)
674-
elif sub.tag == "rwgt":
675-
for r in sub:
676-
if r.tag == "wgt":
677-
if r.text is None:
678-
err = "<wgt> block has no text."
679-
raise ValueError(err)
680-
weights[r.attrib["id"]] = float(r.text.strip())
681-
682-
yield LHEEvent(
683-
eventinfo=eventinfo,
684-
particles=particles,
685-
weights=weights,
686-
attributes=attrib,
687-
optional=optional,
688-
)
689-
else:
690-
yield LHEEvent(eventinfo, particles)
691-
692-
# Clear memory
693-
element.clear()
694-
# Clear the root every 10 elements
695-
if idx % 10 == 0:
644+
try:
645+
for idx, (event, element) in enumerate(context, 1):
646+
if event == "end" and element.tag == "event":
647+
if element.text is None:
648+
err = "<event> block has no text."
649+
raise ValueError(err)
650+
651+
data = element.text.strip().split("\n")
652+
eventdata_str, particles_str = data[0], data[1:]
653+
particles_str = [p.strip() for p in particles_str]
654+
655+
eventinfo = LHEEventInfo.fromstring(eventdata_str)
656+
particles = [
657+
LHEParticle.fromstring(p)
658+
for p in particles_str
659+
if not p.startswith("#")
660+
]
661+
662+
if with_attributes:
663+
weights = {}
664+
attrib = element.attrib
665+
optional = [p for p in particles_str if p.startswith("#")]
666+
667+
for sub in element:
668+
if sub.tag == "weights":
669+
if sub.text is None:
670+
err = "<weights> block has no text."
671+
raise ValueError(err)
672+
for i, w in enumerate(sub.text.split()):
673+
if w and index_map[i] not in weights:
674+
weights[index_map[i]] = float(w)
675+
elif sub.tag == "rwgt":
676+
for r in sub:
677+
if r.tag == "wgt":
678+
if r.text is None:
679+
err = "<wgt> block has no text."
680+
raise ValueError(err)
681+
weights[r.attrib["id"]] = float(r.text.strip())
682+
683+
yield LHEEvent(
684+
eventinfo=eventinfo,
685+
particles=particles,
686+
weights=weights,
687+
attributes=attrib,
688+
optional=optional,
689+
)
690+
else:
691+
yield LHEEvent(eventinfo, particles)
692+
693+
# Clear memory
694+
element.clear()
695+
# Clear the root every 100 elements
696+
if idx % 100 == 0:
696697
root.clear()
698+
finally:
699+
root.clear()
697700

698701
@property
699702
def graph(self) -> graphviz.Digraph:

0 commit comments

Comments
 (0)