Skip to content

Commit

Permalink
Implement reading of simulated event distribution in HDF5EventSource, f…
Browse files Browse the repository at this point in the history
…ixes #2632
  • Loading branch information
maxnoe committed Oct 30, 2024
1 parent d2bf61b commit 1a26426
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/ctapipe/io/hdf5eventsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
SchedulingBlockContainer,
SimulatedEventContainer,
SimulatedShowerContainer,
SimulatedShowerDistribution,
SimulationConfigContainer,
TelescopeImpactParameterContainer,
TelescopePointingContainer,
Expand Down Expand Up @@ -231,6 +232,24 @@ def __init__(self, input_url=None, config=None, parent=None, **kwargs):
table.add_index("obs_id")
self._constant_telescope_pointing[tel_id] = table

self._simulated_shower_distributions = (
self._read_simulated_shower_distributions()
)

def _read_simulated_shower_distributions(self):
key = "/simulation/service/shower_distribution"
if key not in self.file_.root:
return {}

reader = HDF5TableReader(self.file_).read(
key, containers=SimulatedShowerDistribution
)
return {dist.obs_id: dist for dist in reader}

@property
def simulated_shower_distributions(self):
return self._simulated_shower_distributions

def __exit__(self, exc_type, exc_val, exc_tb):
self.close()

Expand Down
8 changes: 8 additions & 0 deletions src/ctapipe/io/tests/test_hdf5eventsource.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,11 @@ def test_interpolate_pointing(dl1_mon_pointing_file):
for pointing in e.pointing.tel.values():
assert not np.isnan(pointing.azimuth)
assert not np.isnan(pointing.altitude)


def test_simulated_events_distribution(dl1_file):
with HDF5EventSource(dl1_file) as source:
assert len(source.simulated_shower_distributions) == 1
dist = source.simulated_shower_distributions[1]
assert dist["n_entries"] == 1000
assert dist["histogram"].sum() == 1000.0

0 comments on commit 1a26426

Please sign in to comment.