From 96d76b08a75049c94d8ff8789be5bcb4f69dd2bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20N=C3=B6the?= Date: Thu, 14 Jul 2022 10:31:14 +0200 Subject: [PATCH 1/2] Add method to read the simulation config to table loader --- ctapipe/io/tableloader.py | 7 +++++++ ctapipe/io/tests/test_table_loader.py | 17 +++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ctapipe/io/tableloader.py b/ctapipe/io/tableloader.py index 5d724cf1647..e3c9489ff82 100644 --- a/ctapipe/io/tableloader.py +++ b/ctapipe/io/tableloader.py @@ -28,6 +28,7 @@ TRUE_IMAGES_GROUP = "/simulation/event/telescope/images" TRUE_PARAMETERS_GROUP = "/simulation/event/telescope/parameters" TRUE_IMPACT_GROUP = "/simulation/event/telescope/impact" +SIMULATION_CONFIG_TABLE = "/configuration/simulation/run" DL2_SUBARRAY_GROUP = "/dl2/event/subarray" DL2_TELESCOPE_GROUP = "/dl2/event/telescope" @@ -299,6 +300,12 @@ def _add_index_if_needed(table): if "__index__" not in table.colnames: table["__index__"] = np.arange(len(table)) + def read_simulation_configuration(self): + """ + Read the simulation configuration table + """ + return read_table(self.h5file, SIMULATION_CONFIG_TABLE) + def read_subarray_events(self, start=None, stop=None, keep_order=True): """Read subarray-based event information. diff --git a/ctapipe/io/tests/test_table_loader.py b/ctapipe/io/tests/test_table_loader.py index fb0e32ecf6a..cd3626e253e 100644 --- a/ctapipe/io/tests/test_table_loader.py +++ b/ctapipe/io/tests/test_table_loader.py @@ -1,8 +1,10 @@ +import astropy.units as u +import numpy as np import pytest import tables -import numpy as np from astropy.table import Table +from ctapipe.instrument.subarray import SubarrayDescription from ctapipe.io.astropy_helpers import read_table @@ -63,9 +65,6 @@ def test_check_order(): ) -from ctapipe.instrument.subarray import SubarrayDescription - - @pytest.fixture(params=["by_type", "by_id"]) def test_file(request, dl1_file, dl1_by_type_file): """Test dl1 files in both structures""" @@ -343,3 +342,13 @@ def test_chunked(dl2_shower_geometry_file): assert n_events_by_type == len(tel_events) assert n_read == n_events + + +def test_read_simulation_config(dl2_merged_file): + from ctapipe.io import TableLoader + + with TableLoader(dl2_merged_file) as table_loader: + runs = table_loader.read_simulation_configuration() + assert len(runs) == 2 + assert np.all(runs["obs_id"] == [4, 1]) + assert u.allclose(runs["energy_range_min"].quantity, [0.004, 0.003] * u.TeV) From 2e60e911649ee1a0fdc0092b85883114be0df8bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20N=C3=B6the?= Date: Thu, 14 Jul 2022 10:37:18 +0200 Subject: [PATCH 2/2] Add method to read the shower distributions to table loader --- ctapipe/io/tableloader.py | 7 +++++++ ctapipe/io/tests/test_table_loader.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/ctapipe/io/tableloader.py b/ctapipe/io/tableloader.py index e3c9489ff82..f52e1758a54 100644 --- a/ctapipe/io/tableloader.py +++ b/ctapipe/io/tableloader.py @@ -29,6 +29,7 @@ TRUE_PARAMETERS_GROUP = "/simulation/event/telescope/parameters" TRUE_IMPACT_GROUP = "/simulation/event/telescope/impact" SIMULATION_CONFIG_TABLE = "/configuration/simulation/run" +SHOWER_DISTRIBUTION_TABLE = "/simulation/service/shower_distribution" DL2_SUBARRAY_GROUP = "/dl2/event/subarray" DL2_TELESCOPE_GROUP = "/dl2/event/telescope" @@ -306,6 +307,12 @@ def read_simulation_configuration(self): """ return read_table(self.h5file, SIMULATION_CONFIG_TABLE) + def read_shower_distribution(self): + """ + Read the simulated shower distribution histograms + """ + return read_table(self.h5file, SHOWER_DISTRIBUTION_TABLE) + def read_subarray_events(self, start=None, stop=None, keep_order=True): """Read subarray-based event information. diff --git a/ctapipe/io/tests/test_table_loader.py b/ctapipe/io/tests/test_table_loader.py index cd3626e253e..727593efb60 100644 --- a/ctapipe/io/tests/test_table_loader.py +++ b/ctapipe/io/tests/test_table_loader.py @@ -352,3 +352,14 @@ def test_read_simulation_config(dl2_merged_file): assert len(runs) == 2 assert np.all(runs["obs_id"] == [4, 1]) assert u.allclose(runs["energy_range_min"].quantity, [0.004, 0.003] * u.TeV) + + +def test_read_shower_distributions(dl2_merged_file): + from ctapipe.io import TableLoader + + with TableLoader(dl2_merged_file) as table_loader: + histograms = table_loader.read_shower_distribution() + assert len(histograms) == 2 + assert np.all(histograms["obs_id"] == [4, 1]) + assert np.all(histograms["num_entries"] == [2000, 1000]) + assert np.all(histograms["histogram"].sum(axis=(1, 2)) == [2000, 1000])