1212
1313from typing import Optional , Collection , Any
1414
15+ from pkg_resources import require
16+
1517from PyLamarr import EventBatch
1618
1719@dataclass
@@ -28,8 +30,6 @@ def load(self):
2830 self ._hepmcloader .load (hepmc_file , run_number , event_number )
2931 logging .getLogger ("HepMC2EventBatch" ).debug ("Loaded." )
3032
31-
32-
3333class CompressedHepMCLoader :
3434 """
3535 Adapter to read HepMC2 files compressed in a tar file. Requires SQLamarr.
@@ -51,11 +51,9 @@ def __init__(self,
5151 self ._regexp_totEvents = regexp_totEvents
5252 self ._max_event = max_event
5353 self ._events_per_batch = events_per_batch
54+ self ._particle_gun_patched_events = 0
5455 self .logger = logging .getLogger ("CompressedHepMCLoad" )
5556
56-
57-
58-
5957 def __call__ (self , database ):
6058 import SQLamarr
6159 self ._db = database
@@ -71,7 +69,6 @@ def _get_run_number(self, filename) -> int:
7169
7270 return int (self ._batch_counter )
7371
74-
7572 def _get_evt_number (self , filename : str , default : int ) -> int :
7673 matches = re .findall (self ._regexp_evtNumber , filename )
7774 return int (matches [- 1 ]) if len (matches ) else default
@@ -100,6 +97,7 @@ def archive_mirror(self, filename: str):
10097
10198 def copy_and_maybe_patch_hepmc (self , filename ):
10299 "Apply patches to the HepMC2 file to avoid segmentation fault in HepMC3 ascii reader"
100+ requires_particle_gun_patch = False
103101 with open (filename ) as input_file :
104102 lines = []
105103 for line in input_file :
@@ -109,9 +107,22 @@ def copy_and_maybe_patch_hepmc(self, filename):
109107 # Documentation at https://hepmc.web.cern.ch/hepmc/releases/HepMC2_user_manual.pdf
110108 # Section 6.2
111109 if int (tokens [6 ]) == 1 : # For Particle Gun process
112- tokens [7 ] = "-10000" # disable signal vertex
113- self .logger .warning ("Applying a patch to the input HepMC2 file SPECIFIC FOR PARTICLE GUNS!" )
114- lines .append (" " .join (tokens ))
110+ self ._particle_gun_patched_events += 1
111+ n_vertices = int (tokens [8 ])
112+ tokens [8 ] = str (n_vertices + 1 )
113+ tokens [12 + int (tokens [11 ])] = str (1 )
114+ tokens += ["1.0" ]
115+ requires_particle_gun_patch = True
116+ lines += [" " .join (tokens ), 'N 1 "0"' ]
117+ else :
118+ lines .append (line )
119+ elif len (line ) > 0 and line [0 ] == 'V' and requires_particle_gun_patch : # First vertex
120+ # PGUN Patch:
121+ # HepMC3::HepMC2Reader does not tolerate a PV with no incoming particles,
122+ # so we create a fake vertex and a fake beam particle.
123+ vertex_id = line .split (" " )[1 ]
124+ lines += ["V -99999 0 0 0 0 0 0 0 0" , "P 0 0 0. 0. 0. 0. 0. 3 0 0 %s 0" % vertex_id , line ]
125+ requires_particle_gun_patch = False
115126 else :
116127 lines .append (line )
117128
@@ -182,6 +193,11 @@ def load(self, filename: str):
182193 batches ['run_numbers' ].append (run_number )
183194 batches ['event_numbers' ].append (event_number )
184195 event_counter += 1
185-
196+
197+ if self ._particle_gun_patched_events > 0 :
198+ self .logger .warning (
199+ f"{ self ._particle_gun_patched_events } / { event_counter } events were identified as generated with a "
200+ "Particle Gun and patched."
201+ )
186202
187203
0 commit comments