Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store subarray description in the output file of the stats tool #2696

Merged
merged 7 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/2696.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Store also the SubarrayDescription in the camera monitoring data produced by the stats tool
28 changes: 19 additions & 9 deletions src/ctapipe/tools/calculate_pixel_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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=[
Expand Down Expand Up @@ -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")


Expand Down
5 changes: 5 additions & 0 deletions src/ctapipe/tools/tests/test_calculate_pixel_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down