@@ -640,60 +640,65 @@ def _fromcontext(
640640 with_attributes : bool ,
641641 ) -> Iterator ["LHEEvent" ]:
642642 index_map = _get_index_to_id_map (lheinit ) if with_attributes else {}
643+ idx = 0
643644
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 :
696- root .clear ()
645+ try :
646+ for event , element in context :
647+ if event == "end" and element .tag == "event" :
648+ idx = idx + 1
649+ if element .text is None :
650+ err = "<event> block has no text."
651+ raise ValueError (err )
652+
653+ data = element .text .strip ().split ("\n " )
654+ eventdata_str , particles_str = data [0 ], data [1 :]
655+ particles_str = [p .strip () for p in particles_str ]
656+
657+ eventinfo = LHEEventInfo .fromstring (eventdata_str )
658+ particles = [
659+ LHEParticle .fromstring (p )
660+ for p in particles_str
661+ if not p .startswith ("#" )
662+ ]
663+
664+ if with_attributes :
665+ weights = {}
666+ attrib = element .attrib
667+ optional = [p for p in particles_str if p .startswith ("#" )]
668+
669+ for sub in element :
670+ if sub .tag == "weights" :
671+ if sub .text is None :
672+ err = "<weights> block has no text."
673+ raise ValueError (err )
674+ for i , w in enumerate (sub .text .split ()):
675+ if w and index_map [i ] not in weights :
676+ weights [index_map [i ]] = float (w )
677+ elif sub .tag == "rwgt" :
678+ for r in sub :
679+ if r .tag == "wgt" :
680+ if r .text is None :
681+ err = "<wgt> block has no text."
682+ raise ValueError (err )
683+ weights [r .attrib ["id" ]] = float (r .text .strip ())
684+
685+ yield LHEEvent (
686+ eventinfo = eventinfo ,
687+ particles = particles ,
688+ weights = weights ,
689+ attributes = attrib ,
690+ optional = optional ,
691+ )
692+ else :
693+ yield LHEEvent (eventinfo , particles )
694+
695+ # Clear memory
696+ element .clear ()
697+ # Clear the root every 100 elements
698+ if idx % 100 == 0 :
699+ root .clear ()
700+ finally :
701+ root .clear ()
697702
698703 @property
699704 def graph (self ) -> graphviz .Digraph :
0 commit comments