Skip to content

Commit

Permalink
Merge branch 'track_lineage_changes' into report_pr_changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JYangQi00 authored Nov 28, 2023
2 parents 94cd8bd + 29c0717 commit 4dfabf5
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 18 deletions.
7 changes: 7 additions & 0 deletions straxen/plugins/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@

from . import multi_scatter
from .multi_scatter import *

#Only for testing!
from . import event_s2_fwhm
from .event_s2_fwhm import *

from . import event_nans
from .event_nans import *
15 changes: 9 additions & 6 deletions straxen/plugins/events/event_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ class EventBasics(strax.Plugin):
"""

__version__ = "1.3.3"

depends_on = ("events", "peak_basics", "peak_positions", "peak_proximity")
provides = "event_basics"
data_kind = "events"
loop_over = "events"
#Depends on event_nans, but doesn't actually affect anything
depends_on = ('events',
'event_nans',
'peak_basics',
'peak_positions',
'peak_proximity')
provides = 'event_basics'
data_kind = 'events'
loop_over = 'events'

electron_drift_velocity = straxen.URLConfig(
default="cmt://electron_drift_velocity?version=ONLINE&run_id=plugin.run_id",
Expand Down
34 changes: 34 additions & 0 deletions straxen/plugins/events/event_nans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import strax
import numpy as np
import straxen

export, __all__ = strax.exporter()

@export
class GiveNans(strax.Plugin):
"""
A test plugin that just gives nans
"""

__version__ = '0.0.1'

depends_on = ('events',)
provides = 'event_nans'
data_kind = 'events'

dtype = [(('Start time since unix epoch [ns]', 'time'), np.int64),
(('Exclusive end time since unix epoch [ns]', 'endtime'), np.int64),
(('Just nans for approximately half of the events', 'event_nans'), np.float64),
(('Just -1 for approximately half of the events', 'event_negs'), np.int64)]

def compute(self, events):
result = np.zeros(len(events), self.dtype)
result['time'] = events['time']
result['endtime'] = events['endtime']

if len(events)>0:
halflen = int(len(events)/2)
result['event_nans'][:halflen] = np.nan
result['event_negs'][:halflen] = -1

return result
21 changes: 9 additions & 12 deletions straxen/plugins/events/event_positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EventPositions(strax.Plugin):
"""

depends_on = "event_basics"
depends_on = ('event_basics','event_nans')

__version__ = "0.3.0"

Expand Down Expand Up @@ -121,18 +121,15 @@ def infer_dtype(self):
]
dtype += naive_pos + fdc_pos
for s_i in [1, 2]:
dtype += [
(
f"alt_s{s_i}_theta",
np.float32,
(
f"Alternative S{s_i} (rel. main S{3 - s_i}) interaction angular position"
" (radians)"
),
)
]

dtype += [("theta", np.float32, f"Main interaction angular position (radians)")]
dtype += [(f'alt_s{s_i}_theta',
np.float32,
f'Alternative S{s_i} (rel. main S{3 - s_i}) interaction angular position (radians)')]

dtype += [('theta', np.float32, f'Main interaction angular position (radians)')]

#Add that random nan field just for testing, but don't do anything with it
dtype += [('nans', np.float64, f'The nans from event_nans')]
return dtype + strax.time_fields

def setup(self):
Expand Down
79 changes: 79 additions & 0 deletions straxen/plugins/events/event_s2_fwhm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import strax
import numpy as np
import straxen
import numba

export, __all__ = strax.exporter()

@export
class S2FWHM(strax.Plugin):
"""
This is a default plugin, like used by many plugins in straxen. It
finds the full-width half-maximum of the main and alternate S2
peak for each event
"""
__version__ = '0.0.1'

depends_on = ('event_basics', 'peaks')
provides = 's2_fwhm'
data_kind = 'events'

smoothing = straxen.URLConfig(default=False, type=bool, track = True,
help="Flag for whether or not the"
" waveform is smoothed or not.")
averaging_samples = straxen.URLConfig(default=3, type=int, track=True,
help="Number of samples to average over.")

# We really could just type the dtype, but for demonstration purposes
# we can use infer_dtype
def infer_dtype(self):
dtype = [(('Start time since unix epoch [ns]', 'time'), np.int64),
(('Exclusive end time since unix epoch [ns]', 'endtime'), np.int64),
(('FWHM for s2 S2 in the event', 's2_fwhm'), np.float32),
(('FWHM for alt_s2 S2 in the event', 'alt_s2_fwhm'), np.float32),
(('An extra field just to detect something', 'random_stuff'), np.float32)]

return dtype

def compute(self, events, peaks):
result = np.zeros(len(events), self.dtype)
result['time'] = events['time']
result['endtime'] = events['endtime']

if len(events)>0:
pks_per_ev = strax.split_by_containment(peaks, events)
pk_buffer = {t: -999*np.ones((len(events),len(peaks['data'][0]))) for t in ['s2', 'alt_s2']}
pk_dt = {t: 10*np.ones(len(events)) for t in ['s2', 'alt_s2']}

for t in ['s2', 'alt_s2']:
for i, (ev, pk_ev) in enumerate(zip(events, pks_per_ev)):
pk_buffer[t][i] = pk_ev[ev[f'{t}_index']]['data']
pk_dt[t][i] = pk_ev[ev[f'{t}_index']]['dt']


for t in ['s2', 'alt_s2']:
if self.smoothing:
pk_buffer[t] = smooth(pk_buffer[t], self.averaging_samples)
result[f'{t}_fwhm'] = fwhm(pk_buffer[t], pk_dt[t])

return result

@numba.njit()
def fwhm(wfs, dts):
result = -np.ones(len(wfs))
for i, (w, dt) in enumerate(zip(wfs, dts)):
if w[0] == -999:
result[i] = -1
else:
above_half = np.where(w>=0.5*np.max(w))[0]
result[i] = (above_half[-1]-above_half[0])*dt
return result

@numba.njit()
def smooth(wfs, smooth_samps ):
smooth_wfs = -999*np.ones((len(wfs),len(wfs[0])+smooth_samps-1))
for wf_i, wf in enumerate(wfs):
if wf[0]!=-999:
smooth_wfs[wf_i] = np.convolve(wf, np.ones(smooth_samps)/smooth_samps)

return smooth_wfs

0 comments on commit 4dfabf5

Please sign in to comment.