diff --git a/eventio/simtel/simtelfile.py b/eventio/simtel/simtelfile.py index f4b9adbe..cf3635f2 100644 --- a/eventio/simtel/simtelfile.py +++ b/eventio/simtel/simtelfile.py @@ -83,7 +83,7 @@ def __init__(self, path, allowed_telescopes=None, skip_calibration=False): self.mc_run_headers = [] self.corsika_inputcards = [] self.header = None - self.n_telescopes = 0 + self.n_telescopes = None self.telescope_descriptions = defaultdict(dict) self.camera_monitorings = defaultdict(dict) self.laser_calibrations = defaultdict(dict) @@ -98,9 +98,12 @@ def __init__(self, path, allowed_telescopes=None, skip_calibration=False): # read the header: # assumption: the header is done when # any of the objects in check is not None anymore + # and we found the telescope_descriptions of all telescopes check = [] - while not any(o for o in check): + found_all_telescopes = False + while not (any(o for o in check) and found_all_telescopes): self.next_low_level() + check = [ self.current_mc_shower, self.current_array_event, @@ -109,7 +112,13 @@ def __init__(self, path, allowed_telescopes=None, skip_calibration=False): self.camera_monitorings, ] - self._first_event_byte = self.tell() + # check if we found all the descriptions of all telescopes + if self.n_telescopes is not None: + found = sum( + len(t) == len(telescope_descriptions_types) + for t in self.telescope_descriptions.values() + ) + found_all_telescopes = found == self.n_telescopes def __iter__(self): return self.iter_array_events() @@ -173,7 +182,6 @@ def next_low_level(self): ) def iter_mc_events(self): - self._next_header_pos = self._first_event_byte while True: try: next_event = self.try_build_mc_event() @@ -194,7 +202,6 @@ def try_build_mc_event(self): self.next_low_level() def iter_array_events(self): - self._next_header_pos = self._first_event_byte while True: next_event = self.try_build_event() diff --git a/tests/resources/gamma_merged.simtel.gz b/tests/resources/gamma_merged.simtel.gz new file mode 100644 index 00000000..8b273d94 Binary files /dev/null and b/tests/resources/gamma_merged.simtel.gz differ diff --git a/tests/simtel/test_simtelfile.py b/tests/simtel/test_simtelfile.py index 683dc357..d31b122a 100644 --- a/tests/simtel/test_simtelfile.py +++ b/tests/simtel/test_simtelfile.py @@ -10,6 +10,7 @@ # a file prod4_zst_path = 'tests/resources/gamma_20deg_0deg_run102___cta-prod4-sst-1m_desert-2150m-Paranal-sst-1m.simtel.zst' calib_path = 'tests/resources/calib_events.simtel.gz' +frankenstein_path = 'tests/resources/gamma_merged.simtel.gz' test_paths = [prod2_path, prod3_path, prod4_path] @@ -176,3 +177,8 @@ def test_skip_calibration_events(): if event['type'] == 'calibration': i += 1 assert i == 0 + + +def test_frankenstein(): + with SimTelFile(frankenstein_path) as f: + assert len(f.telescope_descriptions) == f.n_telescopes