|
| 1 | +import copy |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from ctapipe.containers import EventType |
| 5 | +from ctapipe.core.traits import Path |
| 6 | +from ctapipe_io_nectarcam.containers import NectarCAMDataContainer |
| 7 | + |
| 8 | +from ...data.container import ( |
| 9 | + PhotostatContainer, |
| 10 | + SPEfitContainer, |
| 11 | + merge_map_ArrayDataContainer, |
| 12 | +) |
| 13 | +from ...makers.component import ChargesComponent, GainNectarCAMComponent |
| 14 | +from ...utils import ComponentUtils, ContainerUtils |
| 15 | +from ...utils.constants import GAIN_DEFAULT, GAIN_LINEAR_RANGE, HILO_DEFAULT |
| 16 | + |
| 17 | +GAIN_CONTAINER_CLASSES = [PhotostatContainer, SPEfitContainer] |
| 18 | + |
| 19 | + |
| 20 | +class HiLoComponent(GainNectarCAMComponent): |
| 21 | + """ |
| 22 | + Component that computes the HiLo ratio. |
| 23 | + """ |
| 24 | + |
| 25 | + gain_file = Path( |
| 26 | + default_value=None, |
| 27 | + help="Path to h5 file with gain calibration coefficients", |
| 28 | + allow_none=True, |
| 29 | + ).tag(config=True) |
| 30 | + |
| 31 | + SubComponents = copy.deepcopy(GainNectarCAMComponent.SubComponents) |
| 32 | + SubComponents.default_value = [ |
| 33 | + "ChargesComponent", |
| 34 | + ] |
| 35 | + SubComponents.read_only = True |
| 36 | + |
| 37 | + def __init__(self, subarray, config=None, parent=None, *args, **kwargs): |
| 38 | + chargesComponent_kwargs = {} |
| 39 | + other_kwargs = {} |
| 40 | + chargesComponent_configurable_traits = ComponentUtils.get_configurable_traits( |
| 41 | + ChargesComponent |
| 42 | + ) |
| 43 | + for key in kwargs.keys(): |
| 44 | + if key in chargesComponent_configurable_traits.keys(): |
| 45 | + chargesComponent_kwargs[key] = kwargs[key] |
| 46 | + else: |
| 47 | + other_kwargs[key] = kwargs[key] |
| 48 | + |
| 49 | + super().__init__( |
| 50 | + subarray=subarray, config=config, parent=parent, *args, **other_kwargs |
| 51 | + ) |
| 52 | + |
| 53 | + self.chargesComponent = ChargesComponent( |
| 54 | + subarray=subarray, |
| 55 | + config=config, |
| 56 | + parent=parent, |
| 57 | + *args, |
| 58 | + **chargesComponent_kwargs, |
| 59 | + ) |
| 60 | + self._chargesContainer = None |
| 61 | + self.log.debug(f"{kwargs.keys()}") |
| 62 | + |
| 63 | + self._init_gain_container() |
| 64 | + |
| 65 | + def _init_gain_container(self): |
| 66 | + self.__gain_container = None |
| 67 | + |
| 68 | + if self.gain_file is None: |
| 69 | + raise ValueError("Need to provide a gain_file to compute HiLo ratio") |
| 70 | + |
| 71 | + try: |
| 72 | + self.__gain_container = ContainerUtils.get_container_from_hdf5( |
| 73 | + self.gain_file, |
| 74 | + GAIN_CONTAINER_CLASSES, |
| 75 | + ) |
| 76 | + ContainerUtils.add_missing_pixels_to_container( |
| 77 | + self.__gain_container, pad_value=GAIN_DEFAULT |
| 78 | + ) |
| 79 | + except Exception as e: |
| 80 | + raise RuntimeError( |
| 81 | + f"Failed to initialize gain container from {self.gain_file}" |
| 82 | + ) from e |
| 83 | + |
| 84 | + def __call__(self, event: NectarCAMDataContainer, *args, **kwargs): |
| 85 | + # For now only flat-field events, to be updated for e.g. white-target |
| 86 | + if event.trigger.event_type == EventType.FLATFIELD: |
| 87 | + self.chargesComponent(event=event, *args, **kwargs) |
| 88 | + |
| 89 | + def finish(self, *args, **kwargs): |
| 90 | + if self._chargesContainer is None: |
| 91 | + chargesContainers = self.chargesComponent.finish(*args, **kwargs) |
| 92 | + self._chargesContainer = merge_map_ArrayDataContainer(chargesContainers) |
| 93 | + |
| 94 | + self._compute_low_gain() |
| 95 | + |
| 96 | + return self.__gain_container |
| 97 | + |
| 98 | + def _compute_low_gain(self): |
| 99 | + ContainerUtils.add_missing_pixels_to_container(self._chargesContainer) |
| 100 | + charges_hg = self._chargesContainer["charges_hg"] |
| 101 | + charges_lg = self._chargesContainer["charges_lg"] |
| 102 | + high_gain = self.__gain_container["high_gain"][:, 0] |
| 103 | + |
| 104 | + # Mask the linear regime between low gain and high gain |
| 105 | + charges_hg_pe = charges_hg / high_gain |
| 106 | + mask_linearity = np.logical_and( |
| 107 | + charges_hg_pe > np.min(GAIN_LINEAR_RANGE), |
| 108 | + charges_hg_pe < np.max(GAIN_LINEAR_RANGE), |
| 109 | + ) |
| 110 | + |
| 111 | + # Add failsafe to not divide by 0 |
| 112 | + mask = np.logical_and(mask_linearity, charges_lg > 0) |
| 113 | + |
| 114 | + # Compute HiLo ratio (per pixel) for each event |
| 115 | + hilo_ratio_all_events = np.divide( |
| 116 | + charges_hg, |
| 117 | + charges_lg, |
| 118 | + where=mask, |
| 119 | + out=np.full_like(charges_hg, np.nan, dtype=float), |
| 120 | + ) |
| 121 | + |
| 122 | + # Compute HiLo ratio (per pixel) averaged over all events |
| 123 | + hilo_ratio = np.nanmean(hilo_ratio_all_events, axis=0) |
| 124 | + |
| 125 | + # Set default values if all events were masked |
| 126 | + hilo_ratio = np.where(np.isnan(hilo_ratio), HILO_DEFAULT, hilo_ratio) |
| 127 | + |
| 128 | + # Fill gain container |
| 129 | + self.__gain_container["low_gain"][:, 0] = high_gain / hilo_ratio |
0 commit comments