sanitize_hepmc3 fails to add END_EVENT_LISTING after discarding partial event
If the input hepmc3 file ends with a partial event, the final partial event is dropped but contrary to log messages, no
END_EVENT_LISTING is appended.
WARNING: File does not end with END_EVENT_LISTING, appending WARNING: Invalid particle count for event: E 305 8 20 @ -7.4609364157173369e-02 -7.7988299819229476e-03 9.6855910264790026e+00 -7.2251504307264325e+00 --> skipping event WARNING: Skipped invalid event E 305 8 20 @ -7.4609364157173369e-02 -7.7988299819229476e-03 9.6855910264790026e+00 -7.2251504307264325e+00
At the end of the file, when going into flush_buffer, the buffer has the partial event with the
if not end_reached: warn("File does not end with END_EVENT_LISTING, appending") buffer.append('HepMC::Asciiv3-END_EVENT_LISTING\n') # final buffer flush at the end flush_buffer(header, buffer)
However, flush_buffer drops the entire buffer, including the
END_EVENT_LISTING and returns:
def flush_buffer(header, buffer): if header: event_record = header.get_record() if not event_record: warn('Skipped invalid event', header.raw) return sys.stdout.write(header.get_record()) for line in buffer: sys.stdout.write(line)
A likely fix may be to flush the buffer before
if not end_reached:, then append the
END_EVENT_LISTING again, and flush the buffer again.
That leaves as a case that will result in a missing
END_EVENT_LISTING an input file that ends with
END_EVENT_LISTING after an actual incomplete event, in which case the final buffer includes the
END_EVENT_LISTING, is dropped, and doesn't trigger the
if not end_reached:.
Maybe we can also just always drop END_EVENT_LISTING from the input, and always end by writing
END_EVENT_LISTING to the output.