Skip to content

Commit

Permalink
Allow iterating over all events
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Sep 5, 2023
1 parent 9e596c3 commit c7aac42
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
21 changes: 18 additions & 3 deletions ctapipe/io/simteleventsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,14 @@ class SimTelEventSource(EventSource):
config=True
)

skip_non_triggered_events = Bool(
True,
help=(
"If False, also yield events where no telescope has triggered",
" meaning only shower information will be available.",
),
).tag(config=True)

focal_length_choice = UseEnum(
FocalLengthKind,
default_value=FocalLengthKind.EFFECTIVE,
Expand Down Expand Up @@ -499,6 +507,7 @@ def __init__(self, input_url=Undefined, config=None, parent=None, **kwargs):
self.input_url.expanduser(),
allowed_telescopes=self.allowed_tels,
skip_calibration=self.skip_calibration_events,
skip_non_triggered=self.skip_non_triggered_events,
)

self._subarray_info = self.prepare_subarray_info(
Expand Down Expand Up @@ -732,8 +741,9 @@ def _generate_events(self):
data.meta["input_url"] = self.input_url
data.meta["max_events"] = self.max_events

telescope_events = array_event["telescope_events"]
tracking_positions = array_event["tracking_positions"]
if trigger is None:
yield data
continue

photoelectron_sums = array_event.get("photoelectron_sums")
if photoelectron_sums is not None:
Expand All @@ -751,6 +761,9 @@ def _generate_events(self):
else:
impact_distances = np.full(len(self.subarray), np.nan) * u.m

telescope_events = array_event["telescope_events"]
tracking_positions = array_event["tracking_positions"]

for tel_id, telescope_event in telescope_events.items():
adc_samples = telescope_event.get("adc_samples")
if adc_samples is None:
Expand Down Expand Up @@ -891,7 +904,9 @@ def _fill_event_pointing(tracking_position):
return TelescopePointingContainer(azimuth=azimuth, altitude=altitude)

def _fill_trigger_info(self, array_event):
trigger = array_event["trigger_information"]
trigger = array_event.get("trigger_information")
if trigger is None:
return None

if array_event["type"] == "data":
event_type = EventType.SUBARRAY
Expand Down
23 changes: 23 additions & 0 deletions ctapipe/io/tests/test_simteleventsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,26 @@ def test_starting_grammage():
with SimTelEventSource(path, focal_length_choice="EQUIVALENT") as source:
e = next(iter(source))
assert e.simulation.shower.starting_grammage == 580 * u.g / u.cm**2


def test_all_events():
path = "dataset://gamma_prod5.simtel.zst"

with SimTelEventSource(path, skip_non_triggered_events=False) as source:
reuse = source.simulation_config[1].shower_reuse
n_showers = source.simulation_config[1].n_showers

shower = 0
event = 0
total = 0
for e in source:
if total % reuse == 0:
event = 0
shower += 1
else:
event += 1

assert e.index.event_id == shower * 100 + event
total += 1

assert total == n_showers * reuse

0 comments on commit c7aac42

Please sign in to comment.