Skip to content

Commit 79033a7

Browse files
tibaldojlenain
andcommitted
Fix bug in ChargeDistributionFilter for pedestal maker (cta-observatory#213)
* Only fill charges container if waveforms are available * Cleanup commented stuff from some previous PR * Pin `persistent` (dependency for `ZODB`) version to fix CI --------- Co-authored-by: Jean-Philippe Lenain <[email protected]> # Conflicts: # src/nectarchain/makers/component/pedestal_component.py
1 parent 7eb6329 commit 79033a7

File tree

1 file changed

+37
-52
lines changed

1 file changed

+37
-52
lines changed

src/nectarchain/makers/component/pedestal_component.py

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -202,50 +202,38 @@ def __init__(self, subarray, config=None, parent=None, *args, **kwargs):
202202
)
203203

204204
@staticmethod
205-
def calculate_stats(container, mask, statistics):
205+
def calculate_stats(waveformsContainers, wfs_mask, statistics):
206206
"""Calculate statistics for the pedestals from a waveforms container.
207207
208208
Parameters
209209
----------
210-
containers : `~nectarchain.data.container`
211-
Waveforms or Charges container
212-
mask : `numpy.ndarray`
213-
Mask to apply to exclude outliers with shape (n_events,n_pixels,n_samples)
210+
waveformsContainers : `~nectarchain.data.container.WaveformsContainer`
211+
Waveforms container
212+
wfs_mask : `numpy.ndarray`
213+
Mask to apply to exclude outliers with shape (n_pixels,n_samples)
214214
statistics : `list`
215215
Names of the statistics (numpy.ma attributes) to compute
216216
217217
Returns
218218
-------
219219
ped_stats : `dict`
220-
A dictionary containing 3D (n_chan,n_pixels,n_samples)
221-
or 2D (n_chan,n_pixels) arrays for each statistic.
220+
A dictionary containing 3D (n_chan,n_pixels,n_samples) arrays for each
221+
statistic.
222222
"""
223223

224-
# Detect container type by attribute name
225-
if hasattr(container, "wfs_hg"):
226-
data_hg = container.wfs_hg
227-
data_lg = container.wfs_lg
228-
is_waveform = True
229-
else:
230-
data_hg = container.charges_hg
231-
data_lg = container.charges_lg
232-
is_waveform = False
233-
234-
# Adapt mask shape
235-
# Waveforms: (n_events, n_pixels, n_samples)
236-
# Charges: (n_events, n_pixels)
237-
if not is_waveform:
238-
mask = mask[:, :, 0]
239-
240224
ped_stats = {}
241225

242226
for stat in statistics:
243227
# Calculate the statistic along axis = 0, that is over events
244-
ped_stat_hg = getattr(ma, stat)(ma.masked_array(data_hg, mask), axis=0)
245-
ped_stat_lg = getattr(ma, stat)(ma.masked_array(data_lg, mask), axis=0)
228+
ped_stat_hg = getattr(ma, stat)(
229+
ma.masked_array(waveformsContainers.wfs_hg, wfs_mask), axis=0
230+
)
231+
ped_stat_lg = getattr(ma, stat)(
232+
ma.masked_array(waveformsContainers.wfs_lg, wfs_mask), axis=0
233+
)
246234

247235
# Create a 3D array for the statistic
248-
array_shape = np.append([N_GAINS], np.shape(data_hg[0]))
236+
array_shape = np.append([N_GAINS], np.shape(waveformsContainers.wfs_hg[0]))
249237
ped_stat = np.zeros(array_shape)
250238
ped_stat[HIGH_GAIN] = ped_stat_hg
251239
ped_stat[LOW_GAIN] = ped_stat_lg
@@ -511,6 +499,29 @@ def finish(self, *args, **kwargs):
511499
# log.info('JPL: waveformsContainers=',waveformsContainers.containers[
512500
# EventType.SKY_PEDESTAL].nsamples)
513501

502+
# If we want to filter based on charges distribution
503+
# make sure that the charge distribution container is filled
504+
if (
505+
self.filter_method == "ChargeDistributionFilter"
506+
and self._chargesContainers is None
507+
):
508+
log.debug("Compute charges from waveforms")
509+
chargesComponent_kwargs = {}
510+
chargesComponent_configurable_traits = (
511+
ComponentUtils.get_configurable_traits(ChargesComponent)
512+
)
513+
for key in kwargs.keys():
514+
if key in chargesComponent_configurable_traits.keys():
515+
chargesComponent_kwargs[key] = kwargs[key]
516+
self._chargesContainers = ChargesComponent.create_from_waveforms(
517+
waveformsContainer=self._waveformsContainers,
518+
subarray=self.subarray,
519+
config=self.config,
520+
parent=self.parent,
521+
*args,
522+
**chargesComponent_kwargs,
523+
)
524+
514525
# Check if waveforms container is empty
515526
if self._waveformsContainers is None:
516527
log.warning("Waveforms container is none, pedestals cannot be evaluated")
@@ -524,25 +535,6 @@ def finish(self, *args, **kwargs):
524535
# container with no results
525536
return None
526537
else:
527-
# Make sure that the charge distribution container is filled
528-
if self._chargesContainers is None:
529-
log.debug("Compute charges from waveforms")
530-
chargesComponent_kwargs = {}
531-
chargesComponent_configurable_traits = (
532-
ComponentUtils.get_configurable_traits(ChargesComponent)
533-
)
534-
for key in kwargs.keys():
535-
if key in chargesComponent_configurable_traits.keys():
536-
chargesComponent_kwargs[key] = kwargs[key]
537-
self._chargesContainers = ChargesComponent.create_from_waveforms(
538-
waveformsContainer=self._waveformsContainers,
539-
subarray=self.subarray,
540-
config=self.config,
541-
parent=self.parent,
542-
*args,
543-
**chargesComponent_kwargs,
544-
)
545-
546538
# Build mask to filter the waveforms
547539
# Mask based on the high gain channel that is most sensitive to signals
548540
# Initialize empty mask
@@ -585,9 +577,6 @@ def finish(self, *args, **kwargs):
585577
self._ped_stats = self.calculate_stats(
586578
self._waveformsContainers, self._wfs_mask, statistics
587579
)
588-
self._charge_stats = self.calculate_stats(
589-
self._chargesContainers, self._wfs_mask, statistics
590-
)
591580

592581
# calculate the number of events per pixel used to compute the quantitites
593582
# start wit total number of events
@@ -612,10 +601,6 @@ def finish(self, *args, **kwargs):
612601
pedestal_mean_lg=self._ped_stats["mean"][LOW_GAIN],
613602
pedestal_std_hg=self._ped_stats["std"][HIGH_GAIN],
614603
pedestal_std_lg=self._ped_stats["std"][LOW_GAIN],
615-
pedestal_charge_mean_hg=self._charge_stats["mean"][HIGH_GAIN],
616-
pedestal_charge_mean_lg=self._charge_stats["mean"][LOW_GAIN],
617-
pedestal_charge_std_hg=self._charge_stats["std"][HIGH_GAIN],
618-
pedestal_charge_std_lg=self._charge_stats["std"][LOW_GAIN],
619604
pixel_mask=pixel_mask,
620605
)
621606

0 commit comments

Comments
 (0)