Skip to content

Commit f585506

Browse files
committed
Fix subarray event source to check for telescope source exhaustion
1 parent edfd6ca commit f585506

5 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/ctapipe_io_zfits/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ def get_module_and_pixel_id_map(n_modules, n_pixels_module, missing_modules=None
101101
},
102102
id="no_tel_ids_with_data",
103103
),
104+
pytest.param(
105+
{
106+
"all_chunks": True,
107+
"obs_start": Time("2025-02-04T20:45:31"),
108+
"sb_creator_id": 2,
109+
"sb_id": 126,
110+
"obs_id": 127,
111+
},
112+
id="all_chunks",
113+
),
104114
]
105115

106116

src/ctapipe_io_zfits/dl0.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ def _generator(self):
308308
for count, subarray_trigger in enumerate(
309309
self._subarray_trigger_file.SubarrayEvents
310310
):
311+
# stop processing when all telescope event sources are exhausted
312+
if all(f.exhausted for f in self._telescope_files.values()):
313+
return
314+
311315
array_event = ArrayEventContainer(
312316
count=count,
313317
index=EventIndexContainer(

src/ctapipe_io_zfits/multifile.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ def __init__(self, path, *args, **kwargs):
223223
self._events_headers = {}
224224
self.camera_config = None
225225
self.data_stream = None
226+
self._exhausted_sources = set()
226227

227228
for data_source in self.data_sources:
228229
self._load_next_chunk(data_source)
@@ -236,6 +237,11 @@ def _valid_filename_convention(self, proposal):
236237
raise TraitError(msg)
237238
return value
238239

240+
@property
241+
def exhausted(self):
242+
"""Whether all data sources / chunks available have been consumed."""
243+
return len(self._exhausted_sources) == len(self.data_sources)
244+
239245
@property
240246
def n_open_files(self):
241247
"""Number of currently open files."""
@@ -326,6 +332,8 @@ def __next__(self): # noqa: D105
326332
try:
327333
self._load_next_chunk(data_source)
328334
except FileNotFoundError:
329-
pass
335+
self._exhausted_sources.add(data_source)
336+
else:
337+
self._exhausted_sources.add(data_source)
330338

331339
return event

src/ctapipe_io_zfits/tests/test_dl0.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ctapipe.instrument import SubarrayDescription
66
from ctapipe.io import EventSource, TableLoader
77
from ctapipe.tools.process import ProcessorTool
8+
from traitlets.config import Config
89

910

1011
def test_is_compatible(dummy_dl0):
@@ -27,8 +28,10 @@ def test_subarray(dummy_dl0):
2728

2829
def test_subarray_events(dummy_dl0):
2930
time = dummy_dl0["obs_start"]
31+
all_chunks = dummy_dl0.get("all_chunks", False)
32+
config = Config({"MultiFiles": {"all_chunks": all_chunks}})
3033

31-
with EventSource(dummy_dl0["trigger_path"]) as source:
34+
with EventSource(dummy_dl0["trigger_path"], config=config) as source:
3235
n_read = 0
3336
for i, array_event in enumerate(source):
3437
assert array_event.count == i
@@ -46,7 +49,7 @@ def test_subarray_events(dummy_dl0):
4649
n_read += 1
4750
time = time + 0.001 * u.s
4851

49-
assert n_read == 100
52+
assert n_read == (100 if all_chunks else 40)
5053

5154

5255
def test_process(dummy_dl0, tmp_path):
@@ -130,7 +133,8 @@ def test_telescope_event_source_missing_ids(dummy_tel_file_no_ids):
130133
assert ProtozfitsDL0TelescopeEventSource.is_compatible(first_ff_file)
131134
assert ProtozfitsDL0TelescopeEventSource.is_compatible(first_ped_file)
132135

133-
with EventSource(first_ff_file) as source:
136+
config = Config({"MultiFiles": {"all_chunks": True}})
137+
with EventSource(first_ff_file, config=config) as source:
134138
assert isinstance(source, ProtozfitsDL0TelescopeEventSource)
135139

136140
n_read = 0
@@ -142,7 +146,7 @@ def test_telescope_event_source_missing_ids(dummy_tel_file_no_ids):
142146

143147
assert n_read == 50
144148

145-
with EventSource(first_ped_file) as source:
149+
with EventSource(first_ped_file, config=config) as source:
146150
assert isinstance(source, ProtozfitsDL0TelescopeEventSource)
147151

148152
n_read = 0

src/ctapipe_io_zfits/tests/test_multifiles.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def test_multifiles(all_chunks, dummy_tel_file, dl0_base):
2323
else:
2424
assert expected_event_id == 40
2525

26+
assert mf.exhausted
27+
2628
recorded_inputs = Provenance().current_activity.provenance["input"]
2729
# five chunks per stream
2830
if all_chunks:

0 commit comments

Comments
 (0)