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

Protect rebin #687

Merged
merged 7 commits into from
Dec 31, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
33 changes: 31 additions & 2 deletions invisible_cities/cities/irene_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
from .. io.run_and_event_io import read_run_and_event
from .. evm.ic_containers import S12Params as S12P

from .. database import load_db
from .. database.load_db import DetDB
from .. database import load_db
from .. database.load_db import DetDB
from .. io .pmaps_io import load_pmaps

from . irene import irene

Expand Down Expand Up @@ -386,3 +387,31 @@ def test_irene_empty_input_file(config_tmpdir, ICDATADIR):
file_out = PATH_OUT))

irene(**conf)


def test_irene_sequential_times(config_tmpdir, ICDATADIR):

PATH_IN = os.path.join(ICDATADIR , 'single_evt_nonseqtime_rwf.h5')
PATH_OUT = os.path.join(config_tmpdir, 'test_pmaps.h5')

conf = configure('dummy invisible_cities/config/irene.conf'.split())
conf.update(dict(files_in = PATH_IN ,
file_out = PATH_OUT ,
run_number = 6351 ,
n_baseline = 48000 ,
thr_sipm = 1 * units.pes,
s1_tmin = 0 * units.mus,
s1_tmax = 640 * units.mus,
s1_lmin = 5 ,
s1_lmax = 30 ,
s2_tmin = 645 * units.mus,
s2_tmax = 1300 * units.mus,
s2_lmin = 80 ,
s2_lmax = 200000 ,
thr_sipm_s2 = 5 * units.pes))

irene(**conf)

pmaps_out = load_pmaps(PATH_OUT)

assert np.all(np.diff(pmaps_out[3348].s2s[1].times) > 0)
Git LFS file not shown
3 changes: 3 additions & 0 deletions invisible_cities/database/test_data/pmaps_negative_bins.h5
Git LFS file not shown
Git LFS file not shown
9 changes: 5 additions & 4 deletions invisible_cities/reco/peak_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def rebin_times_and_waveforms(times, widths, waveforms,
rebin_stride=2, slices=None):
if rebin_stride < 2: return times, widths, waveforms

if not slices:
if slices is None:
n_bins = int(np.ceil(len(times) / rebin_stride))
reb = rebin_stride
slices = [slice(reb * i, reb * (i + 1)) for i in range(n_bins)]
Expand All @@ -153,9 +153,10 @@ def rebin_times_and_waveforms(times, widths, waveforms,
rebinned_wfs = np.zeros((n_sensors, len(slices)))

for i, s in enumerate(slices):
t = times [ s]
e = waveforms[:, s]
w = np.sum(e, axis=0) if np.any(e) else None
t = times [ s]
e = waveforms[:, s]
w = np.sum(e, axis=0).clip(0) if np.any(e) else None
if not np.any(w): w = None
rebinned_times [ i] = np.average(t, weights=w)
rebinned_widths[ i] = np.sum ( widths[s])
rebinned_wfs [:, i] = np.sum (e, axis=1)
Expand Down
12 changes: 12 additions & 0 deletions invisible_cities/reco/peak_functions_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import numpy as np

from pytest import approx
Expand All @@ -22,6 +24,7 @@
from ..evm .pmaps import S1
from ..evm .pmaps import S2
from ..evm .pmaps import PMap
from ..io .pmaps_io import load_pmaps
from ..types.ic_types_c import minmax
from . import peak_functions as pf

Expand Down Expand Up @@ -597,3 +600,12 @@ def test_rebin_times_and_waveforms_times_are_consistent(t_and_wf, stride):
np.ones_like(wfs), stride)

assert np.sum(rb_times) * stride == approx(np.sum(times))


def test_rebin_times_and_waveforms_negative_bins(ICDATADIR):
pmap_file = os.path.join(ICDATADIR, 'pmaps_negative_bins.h5')
pmaps = load_pmaps(pmap_file)
s2 = pmaps[48490].s2s[0]
rebinned_info = pf.rebin_times_and_waveforms(s2.times ,
s2.bin_widths ,
s2.pmts.all_waveforms)