Skip to content

Commit

Permalink
Split on event type (#123)
Browse files Browse the repository at this point in the history
* Add a Tool that can split data based on delimiter trigger

* Improve documentation

* A bit of cleaning

* Refactored split_run, some coding style

---------

Co-authored-by: Jean-Philippe Lenain <[email protected]>
  • Loading branch information
vmarandon and jlenain authored Jul 31, 2024
1 parent 9c3f30f commit 4192ac7
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions src/nectarchain/makers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import datetime

import numpy as np
from ctapipe.containers import Container
from ctapipe.containers import Container, EventType
from ctapipe.core import Component, Tool
from ctapipe.core.container import FieldValidationError
from ctapipe.core.traits import (
Expand All @@ -32,7 +32,10 @@
log = logging.getLogger(__name__)
log.handlers = logging.getLogger("__main__").handlers

__all__ = ["EventsLoopNectarCAMCalibrationTool"]
__all__ = [
"EventsLoopNectarCAMCalibrationTool",
"DelimiterLoopNectarCAMCalibrationTool",
]

"""The code snippet is a part of a class hierarchy for data processing.
It includes the `BaseMaker` abstract class, the `EventsLoopMaker` and `ArrayDataMaker`
Expand Down Expand Up @@ -258,12 +261,12 @@ def _init_writer(self, sliced: bool = False, slice_index: int = 0, group_name=No
)
group_name = f"data_{slice_index}"
self.log.info(
f"initilization of writer in sliced mode (output written "
f"initialization of writer in sliced mode (output written "
f"to {group_name})"
)
mode = "a"
else:
self.log.info("initilization of writter in full mode")
self.log.info("initialization of writer in full mode")
if self.overwrite:
try:
log.info(
Expand Down Expand Up @@ -383,7 +386,9 @@ def start(
# self._init_trigger_type(_trigger_type)

if restart_from_begining:
self.log.debug("restart from begining : creation of the EventSource reader")
self.log.debug(
"restart from beginning : creation of the EventSource " "reader"
)
self._load_eventsource()

n_events_in_slice = 0
Expand All @@ -407,10 +412,8 @@ def start(
n_events_in_slice += 1
if self._n_traited_events >= n_events:
break
if (
not (self.events_per_slice is None)
and n_events_in_slice >= self.events_per_slice
):

if self.split_run(n_events_in_slice, event):
self.log.info(f"slice number {slice_index} is full, pulling buffer")
self._finish_components(*args, **kwargs)
self.writer.close()
Expand All @@ -419,6 +422,14 @@ def start(
self._setup_components()
n_events_in_slice = 0

def split_run(self, n_events_in_slice, event):
"""Method to decide if criteria to end a run slice are met"""
condition = (
self.events_per_slice is not None
and n_events_in_slice >= self.events_per_slice
)
return condition

def finish(self, return_output_component=False, *args, **kwargs):
self.log.info("finishing Tool")

Expand All @@ -431,7 +442,7 @@ def finish(self, return_output_component=False, *args, **kwargs):
return output

def _finish_components(self, *args, **kwargs):
self.log.info("finishing components and writting to output file")
self.log.info("finishing components and writing to output file")
output = []
for component in self.components:
output.append(component.finish(*args, **kwargs))
Expand Down Expand Up @@ -517,6 +528,23 @@ def pixels_id(self):
return copy.deepcopy(self.__pixels_id)


class DelimiterLoopNectarCAMCalibrationTool(EventsLoopNectarCAMCalibrationTool):
"""
Class that will split data based on the EventType UNKNOWN.
Each time this particular type is seen, it will trigger the change of slice.
Note that the UNKONWN event will be seen by the component, so it must be filtered
there.
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def split_run(self, n_events_in_slice, event):
"""Method to decide if criteria to end a run slice is met"""
condition = event.trigger.event_type == EventType.UNKNOWN
return condition


def main():
"""run the tool"""
tool = EventsLoopNectarCAMCalibrationTool()
Expand Down

0 comments on commit 4192ac7

Please sign in to comment.