예제 #1
0
    def processdata(self):
        for files in self.filelist:

            wavfile = files['wavfile']
            simfile = files['simfile']
            templfile = files['templfile']

            if not os.path.exists(simfile):
                directory, _ = os.path.split(simfile)
                os.makedirs(directory, exist_ok=True)

                data = readwav.readwav(wavfile)

                template = _template.Template.load(templfile)

                kw = dict(
                    batch=100,
                    pbar=True,
                    trigger=np.full(len(data),
                                    savetemplate.defaulttrigger(wavfile)),
                    filtlengths=[32, 64, 128, 256, 512, 1024, 2048],
                )
                sim = afterpulse.AfterPulse(data, template, **kw)

                print(f'save {simfile}...')
                sim.save(simfile)
예제 #2
0
def savenp():
    filename = 'darksidehd/merged_000886.root'
    channel = 'adc_W201_Ch00'
    # look at PDMadcCh.png to match the channel to the wav file
    print(f'reading {filename}, channel {channel}...')
    root = uproot.open(filename)
    tree = root['midas_data']
    noise = tree.array(channel)

    filename = 'darksidehd/nuvhd_lf_3x_tile57_77K_64V_6VoV_1.wav'
    data = readwav.readwav(filename, mmap=False)
    ignore = readwav.spurious_signals(data)
    print(f'ignore {np.count_nonzero(ignore)} events due to spurious signals')
    baseline = data[~ignore, 0, :8900]

    kw = dict()  # arguments for signal.periodogram()

    print('computing noise file spectrum...')
    counts = np.unique(noise.counts)
    assert len(counts) == 2 and counts[0] == 0
    noise_array = noise._content.reshape(-1, counts[-1])
    f1, s1s = signal.periodogram(noise_array, 125e6, **kw)
    s1 = np.median(s1s, axis=0)
    s1 *= (2 / 2**14)**2  # 2 Vpp, 14 bit

    print('computing signal file baseline spectrum...')
    f2, s2s = signal.periodogram(baseline, 1e9, **kw)
    s2 = np.median(s2s, axis=0)
    s2 *= (1 / 2**10)**2  # 1 Vpp, 10 bit

    print(f'save {npfile}')
    np.savez(npfile, f1=f1, f2=f2, s1=s1, s2=s2)
예제 #3
0
def savetemplate(source, plot='show'):
    dest = templatepath(source)
    destbase = templatebasepath(source)

    data = readwav.readwav(source)
    print(f'computing template...')
    if data.shape[1] == 1:
        trigger = defaulttrigger(source)
    else:
        trigger = None

    fig1 = plt.figure(num='savetemplate1', clear=True)
    templ = template.Template.from_lngs(data,
                                        7 * 512,
                                        trigger=trigger,
                                        fig=fig1)
    print(f'write {dest}...')
    templ.save(dest)

    fig2 = plt.figure(num='savetemplate2', clear=True, figsize=[10, 7])
    templateplot.templateplot(dest, fig=fig2)

    for i, fig in enumerate([fig1, fig2]):
        fig.tight_layout()
        if plot == 'show':
            fig.show()
        elif plot == 'save':
            destplot = f'{destbase}-{i + 1}.png'
            print(f'write {destplot}...')
            fig.savefig(destplot)
        else:
            raise KeyError(plot)
예제 #4
0
def apload(vov):
    filelist = vovdict[vov]['files']
    datalist = [
        readwav.readwav(files['wavfile'])
        for files in filelist
    ]
    simlist = [
        afterpulse.AfterPulse.load(files['simfile'])
        for files in filelist
    ]
    simcat = afterpulse.AfterPulse.concatenate(simlist)
    return datalist, simcat
예제 #5
0
    def __init__(self,
                 filename='darksidehd/nuvhd_lf_3x_tile57_77K_64V_6VoV_1.wav'):
        """
        The wav file is read at initialization.
        
        Parameters
        ----------
        filename : str
            The path of an LNGS wav. Default tile 57 6 VoV.
        """
        self.data = readwav.readwav(filename, mmap=False)
        self.ignore = readwav.spurious_signals(self.data)
        print(
            f'ignoring {np.sum(self.ignore)} events with signals in baseline zone'
        )

        _, name = os.path.split(filename)
        base, _ = os.path.splitext(name)
        templfile = 'templates/' + base + '-template.npz'
        print(f'read {templfile}...')
        self.template = _template.Template.load(templfile)
예제 #6
0
"""
Plot an event from an LNGS wav and the moving average filter output.
"""

import numpy as np
from matplotlib import pyplot as plt
import readwav
import fighelp

filename = 'darksidehd/nuvhd_lf_3x_tile57_77K_64V_6VoV_1.wav'
data = readwav.readwav(filename, maxevents=1, mmap=False)


def moving_average(x, n):
    s = np.concatenate([[0], np.cumsum(x)])
    return (s[n:] - s[:-n]) / n


signal = data[0, 0]
trigger = data[0, 1]
ma = moving_average(signal, 1000)

fig = fighelp.figwithsize([8.21, 5.09], resetfigcount=True)

ax = fig.subplots(1, 1)

ax.plot(signal, '-', color='red', label='signal', linewidth=1)
ax.plot(trigger, '-', color='blue', label='trigger', linewidth=1)
ax.plot(np.arange(999, len(signal)),
        ma,
        '-',
예제 #7
0
import numpy as np
from matplotlib import pyplot as plt

import toy
import figlatex
import readwav
import template as _template

prefix = 'nuvhd_lf_3x_tile57_77K_64V_6VoV_1'
length = 1024

###########################

data = readwav.readwav('darksidehd/' + prefix + '.wav',
                       maxevents=1,
                       mmap=False)
signal = data[:, 0]

template = _template.Template.load('templates/' + prefix + '-template.npz')
templ, offset = template.matched_filter_template(length,
                                                 timebase=1,
                                                 aligned='trigger')

filt = toy.Filter(signal, template.baseline)
fsignal = filt.all(templ)[:, 0]

print(f'filter length = {length} ns')

fig, ax = plt.subplots(num='figfilters', clear=True, figsize=[6.4, 3.5])

ax.plot(fsignal[0], color='#f55', linewidth=1, label=toy.Filter.name(0))
예제 #8
0
import os

import numpy as np
from scipy import stats

import afterpulse
import readwav
import template as _template

prefix = 'nuvhd_lf_3x_tile57_77K_64V_6VoV_1'

data = readwav.readwav('darksidehd/' + prefix + '.wav')
template = _template.Template.load(f'templates/{prefix}-template.npz')

savedir = 'afterpulse_tile57'
os.makedirs(savedir, exist_ok=True)

simfile = f'{savedir}/sim.npz'
if not os.path.exists(simfile):
    sim = afterpulse.AfterPulse(data, template, batch=100, pbar=True)
    sim.save(simfile)
sim = afterpulse.AfterPulse.load(simfile)

cond = 'good&(mainpos>=0)&(length==512)&(npe>0)&(npe<8)'
fig = sim.hist('mainheight/npe', cond)
fig.savefig(f'{savedir}/fig1.png')
m = sim.getexpr('median(mainheight/npe)', cond)
print(f'fig1 median = {m}')

cond = 'good&(mainpos>=0)&(length==512)&(npe==1)'
fig = sim.scatter('minorpos-mainpos', 'minorheight', cond)
예제 #9
0
plot_mf_template
plot_signals
plot_filters
plot_localization
"""

import numpy as np
from matplotlib import pyplot as plt

import toy
import readwav

generator = np.random.default_rng(202011111750)

filename = 'nuvhd_lf_3x_tile57_77K_64V_6VoV_1.wav'
data = readwav.readwav(filename, mmap=False)
ignore = readwav.spurious_signals(data)
print(f'ignoring {np.sum(ignore)} events with signals in baseline zone')


def plot_mf_template():
    """
    BROKEN
    
    Plot a cross correlation filter template.
    """
    template = toy.Template()
    template.make(data, 4096, ~ignore)

    fig = plt.figure('runtoy-mf-template')
    fig.clf()
예제 #10
0
def apload(vov):
    things = vovdict[vov]
    data = readwav.readwav(things['wavfile'])
    sim = afterpulse.AfterPulse.load(things['simfile'])
    return data, sim
예제 #11
0
    path, name = os.path.split(wavfile)
    prefix = name.replace('.wav', '')
    if '0VoV' in wavfile:
        templfile = 'templates/LF_TILE15_77K_59V_2VoV_1-template.npz'
    else:
        templfile = f'templates/{prefix}-template.npz'
    simfile = f'{savedir}/{prefix}.npz'

    vov, = re.search(r'(\d)VoV', wavfile).groups()
    vov = int(vov)
    vovdict[vov] = dict(simfile=simfile, wavfile=wavfile, templfile=templfile)

    if not os.path.exists(simfile):

        data = readwav.readwav(wavfile)

        template = _template.Template.load(templfile)

        filtlength = 2048
        kw = dict(batch=100,
                  pbar=True,
                  filtlengths=filtlength,
                  ptlength=filtlength)
        sim = afterpulse.AfterPulse(data, template, **kw)

        print(f'save {simfile}...')
        sim.save(simfile)


def apload(vov):
예제 #12
0
 def datalist(self):
     """
     List of memory mapped source files.
     """
     return [readwav.readwav(files['wavfile']) for files in self.filelist]
예제 #13
0
import numpy as np
from matplotlib import pyplot as plt

import figlatex
import readwav
import firstbelowthreshold

archive = 'figthesis/figtriggerhist.npz'
if not os.path.exists(archive):

    files = list(sorted(glob.glob('darksidehd/*.wav')))

    outfiles = []
    hists = []
    for file in files:
        data = readwav.readwav(file)
        if data.shape[1] != 2:
            continue
        trigger = firstbelowthreshold.firstbelowthreshold(data[:, 1], 600)
        hists.append(np.bincount(trigger))
        outfiles.append(file)

    length = max(len(h) for h in hists)
    hists = [np.pad(h, (0, length - len(h))) for h in hists]

    print(f'save {archive}...')
    np.savez_compressed(archive, files=outfiles, hists=hists)

print(f'load {archive}...')
with np.load(archive) as arch:
    files = arch['files']
예제 #14
0
import runsliced

filename = sys.argv[1]
maxevents = 1000
length = 2000
start = 0
cmap = None
try:
    maxevents = int(sys.argv[2])
    length = int(sys.argv[3])
    start = int(sys.argv[4])
    cmap = sys.argv[5]
except IndexError:
    pass

data = readwav.readwav(filename, mmap=True)

nevents = min(len(data), maxevents)

h = np.zeros((length, 1024), int)

@numba.njit(cache=True)
def accumhist(hist, data, start, length):
    for event in data:
        signal = event[0]
        trigger = event[1]
        for itrig, sample in enumerate(trigger):
            if sample < 600:
                break
        for i in range(length):
            isample = itrig + start + i
예제 #15
0
def read(filespec, maxevents=None, quiet=False, mmap=True, swapch='auto', return_trigger=True, firstevent=None):
    """
    Read an LNGS laser wav or a Proto0 root.
    
    Parameters
    ----------
    filespec : str
        The file path. For root files, there must be an additional channel
        specification separated by a column. Examples:
        'XXX.root:adc_W200_Ch00', 'XXX.root:57'.
    maxevents : int
        The maximum number of events read from the file.
    quiet : bool
        If True, do not print 'reading <file>...'.
    mmap : bool
        (wav-specific) return a memmap to the file.
    swapch : str, bool
        (wav-specific) If True the signal and trigger channels are swapped in
        the file. 'auto' (default) tries to deduce it from the file name.
    return_trigger : bool
        If True (default) return the trigger position for each event.
    firstevent : int, optional
        The first event to read.
    
    Return
    ------
    array : int array (nevents, nsamples)
        The array with the signal waveforms.
    trigger : array (nevents,) or None
        The trigger position for each event in samples. Returned only if
        `return_trigger` is True. `None` if there's no trigger information. For
        Proto0 files the trigger position is constant, read from metadata, and
        not reliable.
    freq : scalar
        The sampling frequency in samples per second.
    ndigit : int
        The number of ADC values.
    """
    path, last = os.path.split(filespec)
    comp = last.split(':')
    if len(comp) == 1 or comp[1] == '':
        name = comp[0]
        ch = None
    else:
        name = ':'.join(comp[:-1])
        ch = comp[-1]
    
    if name.endswith('.wav'):
        data = readwav.readwav(filespec, quiet=quiet, mmap=True, swapch=swapch)
        
        if firstevent is not None:
            data = data[firstevent:]
        if maxevents is not None:
            data = data[:maxevents]
        
        array = data[:, 0]
        if not mmap:
            array = np.copy(array)
        
        trigger = None
        if return_trigger and data.shape[1] == 2:
            trigger = readwav.firstbelowthreshold(data[:, 1], 600)
        freq = 1e9
        ndigit = 2 ** 10
    
    elif name.endswith('.root'):
        if ch is None:
            raise ValueError(f'missing channel for file {name}')
        if not ch.startswith('adc'):
            ch = int(ch)
        if path != '':
            path += '/'
        array, trigger, freq = readroot.readroot(path + name, ch, maxevents=maxevents, quiet=quiet, firstevent=firstevent)
        if return_trigger:
            trigger = np.full(array.shape[0], trigger)
        ndigit = 2 ** 14
    
    else:
        raise ValueError(f'unrecognized extension for file {name}')
    
    if return_trigger:
        return array, trigger, freq, ndigit
    else:
        return array, freq, ndigit