diff --git a/src/ctapipe_io_lst/__init__.py b/src/ctapipe_io_lst/__init__.py index 393a6936..ee03bc1f 100644 --- a/src/ctapipe_io_lst/__init__.py +++ b/src/ctapipe_io_lst/__init__.py @@ -19,7 +19,7 @@ OpticsDescription, SizeType, ) -from astropy.time import Time +from astropy.time import Time, TimeDelta from ctapipe.io import EventSource, read_table from ctapipe.io.datalevels import DataLevel @@ -267,6 +267,12 @@ class LSTEventSource(EventSource): ) ).tag(config=True) + event_time_correction_s = Float( + default_value=None, + allow_none=True, + help='If given, this number of seconds is *added* to the original event time. This is intendend to be used to correct wrong event timestamps due to wrong time on the White Rabbit switch.' + ).tag(config=True) + classes = [PointingSource, EventTimeCalculator, LSTR0Corrections] @@ -378,12 +384,14 @@ def __init__(self, input_url=None, **kwargs): self.read_pedestal_ids() - - if self.use_flatfield_heuristic is None: self.use_flatfield_heuristic = self.run_start < NO_FF_HEURISTIC_DATE self.log.info(f"Changed `use_flatfield_heuristic` to {self.use_flatfield_heuristic}") + self._event_time_correction = None + if self.event_time_correction_s is not None: + self._event_time_correction = TimeDelta(self.event_time_correction_s * u.s) + @property def subarray(self): return self._subarray @@ -634,6 +642,12 @@ def _generator(self): self.fill_mon_container(array_event, zfits_event) + # apply correction before the rest, so corrected time is used e.g. for pointing + if self._event_time_correction is not None: + array_event.trigger.time += self._event_time_correction + for tel_trigger in array_event.trigger.tel.values(): + tel_trigger.time += self._event_time_correction + if self.pointing_information: self.fill_pointing_info(array_event) diff --git a/src/ctapipe_io_lst/tests/test_lsteventsource.py b/src/ctapipe_io_lst/tests/test_lsteventsource.py index 189c345e..85f5bc03 100644 --- a/src/ctapipe_io_lst/tests/test_lsteventsource.py +++ b/src/ctapipe_io_lst/tests/test_lsteventsource.py @@ -356,3 +356,28 @@ def test_reference_position(): assert u.isclose(position.lat, LST1_LOCATION.lat) assert u.isclose(position.lon, LST1_LOCATION.lon) assert u.isclose(position.height, LST1_LOCATION.height) + + + + +@pytest.mark.parametrize("timeshift", (-5, 70)) +def test_time_correction(timeshift): + from ctapipe_io_lst import LSTEventSource + config = { + 'LSTEventSource': { + 'input_url': test_r0_path_all_streams, + 'apply_drs4_corrections': False, + 'pointing_information': False, + 'max_events': 5, + }, + } + + original = LSTEventSource(config=Config(config)) + shifted = LSTEventSource(config=Config(config), event_time_correction_s=timeshift) + + with original, shifted: + for event, event_shifted in zip(original, shifted): + dt = event_shifted.trigger.time - event.trigger.time + dt_tel = event_shifted.trigger.tel[1].time - event.trigger.tel[1].time + assert u.isclose(dt.to_value(u.s), timeshift) + assert u.isclose(dt_tel.to_value(u.s), timeshift)