diff --git a/docs/changes/2696.feature.rst b/docs/changes/2696.feature.rst new file mode 100644 index 00000000000..e4856a4d7ef --- /dev/null +++ b/docs/changes/2696.feature.rst @@ -0,0 +1 @@ +Store also the SubarrayDescription in the camera monitoring data produced by the stats tool diff --git a/src/ctapipe/tools/calculate_pixel_stats.py b/src/ctapipe/tools/calculate_pixel_stats.py index 18aa07632eb..3e672d06332 100644 --- a/src/ctapipe/tools/calculate_pixel_stats.py +++ b/src/ctapipe/tools/calculate_pixel_stats.py @@ -84,8 +84,10 @@ class PixelStatisticsCalculatorTool(Tool): def setup(self): # Read the input data with the 'TableLoader' - self.input_data = TableLoader( - parent=self, + self.input_data = self.enter_context( + TableLoader( + parent=self, + ) ) # Check that the input and output files are not the same if self.input_data.input_url == self.output_path: @@ -100,18 +102,20 @@ def setup(self): self.input_data.dl1_images = True # Load the subarray description from the input file subarray = SubarrayDescription.from_hdf(self.input_data.input_url) - # Get the telescope ids from the input data or use the allowed_tels configuration - self.tel_ids = ( - subarray.tel_ids if self.allowed_tels is None else self.allowed_tels + # Select a new subarray if the allowed_tels configuration is used + self.subarray = ( + subarray + if self.allowed_tels is None + else subarray.select_subarray(self.allowed_tels) ) # Initialization of the statistics calculator self.stats_calculator = PixelStatisticsCalculator( - parent=self, subarray=subarray + parent=self, subarray=self.subarray ) def start(self): # Iterate over the telescope ids and calculate the statistics - for tel_id in self.tel_ids: + for tel_id in self.subarray.tel_ids: # Read the whole dl1 images for one particular telescope dl1_table = self.input_data.read_telescope_events_by_id( telescopes=[ @@ -171,13 +175,19 @@ def start(self): f"/dl1/monitoring/telescope/{self.output_table_name}/tel_{tel_id:03d}", overwrite=self.overwrite, ) - - def finish(self): self.log.info( "DL1 monitoring data was stored in '%s' under '%s'", self.output_path, f"/dl1/monitoring/telescope/{self.output_table_name}", ) + + def finish(self): + # Store the subarray description in the output file + self.subarray.to_hdf(self.output_path, overwrite=self.overwrite) + self.log.info( + "Subarray description was stored in '%s'", + self.output_path, + ) self.log.info("Tool is shutting down") diff --git a/src/ctapipe/tools/tests/test_calculate_pixel_stats.py b/src/ctapipe/tools/tests/test_calculate_pixel_stats.py index 1e0330fa40f..9e2e9674664 100644 --- a/src/ctapipe/tools/tests/test_calculate_pixel_stats.py +++ b/src/ctapipe/tools/tests/test_calculate_pixel_stats.py @@ -8,6 +8,7 @@ from ctapipe.core import run_tool from ctapipe.core.tool import ToolConfigurationError +from ctapipe.instrument import SubarrayDescription from ctapipe.io import read_table from ctapipe.tools.calculate_pixel_stats import PixelStatisticsCalculatorTool @@ -57,6 +58,10 @@ def test_calculate_pixel_stats_tool(tmp_path, dl1_image_file): )["mean"] is not None ) + # Read subarray description from the created monitoring file + subarray = SubarrayDescription.from_hdf(monitoring_file) + # Check for the selected telescope + assert subarray.tel_ids[0] == tel_id def test_tool_config_error(tmp_path, dl1_image_file):