Exemple #1
0
def save_sample_traces(filelist,sample_trace_directory_name=None):
    for f in tqdm.tqdm(filelist):
        trc = lecroy.LecroyBinaryWaveform(f)
        time = trc.mat[:,0]
        signal = trc.mat[:,1]
        fname = f.strip('.trc').split('/')[-1]
        np.savetxt(sample_trace_directory_name+fname+'.txt', np.array(zip(time,signal)))
Exemple #2
0
def traces(directory_name, bg_points=1000, indexleft=0, indexright=0):
    """ from a trace stored in binary format
    creates a waveform object.
    It returns a list with useful statistics
    """

    # reads the files into waveform objects
    filelist = np.array(glob.glob(directory_name + '*.trc'))
    if indexright == 0:
        indexleft = 0
        indexright = len(filelist)

    trcs = [
        lecroy.LecroyBinaryWaveform(f) for f in filelist[indexleft:indexright]
    ]
    # uses the median as an estimator of the vertical offset of the trace
    bg = [np.median(t.mat[:bg_points, 1]) for t in trcs]

    # max height
    heights = [np.max(t.mat[:, 1] - b) for t, b in zip(trcs, bg)]

    # the area of the pulses after subtracting the vertical offset
    # dt = trcs[0].metadata['HORIZ_INTERVAL']
    areas_abs = [np.sum(np.abs(t.mat[:, 1] - b)) for t, b in zip(trcs, bg)]
    areas = [np.sum((t.mat[:, 1] - b)) for t, b in zip(trcs, bg)]

    return np.array(trcs), np.array(bg), np.array(heights),\
        np.array(areas_abs), np.array(areas)
Exemple #3
0
def param_extr(filename, t_initial=None, t_final=None, h_th=-10):
    """extract relevant parameters from a trace stored in a file
    """
    trc = lecroy.LecroyBinaryWaveform(filename)
    time = trc.mat[:, 0]
    signal = trc.mat[:, 1]
    idx_0 = 0
    idx_1 = -1
    if t_initial is not None:
        idx_0 = find_idx(time, t_initial)
    if t_final is not None:
        idx_1 = find_idx(time, t_final)
    time = time[idx_0:idx_1]
    signal = signal[idx_0:idx_1]

    # add check for length of bg_points against length of signal
    # bg_points = np.min([bg_points, len(signal)])
    # bg = np.median(signal[:bg_points])

    bg = find_bg(signal)
    signal = signal - bg
    height = np.max(signal)
    area_th = np.sum(signal[signal > h_th])
    area_abs = np.sum(np.abs(signal))

    return np.array([area_th, area_abs, height, bg])
Exemple #4
0
def trc2dat(filelist, out_folder=None):
    """batch conversion of lecroy binary traces to ASCII files"""
    for f in filelist:
        trc = lecroy.LecroyBinaryWaveform(f)
        time = trc.mat[:, 0]
        signal = trc.mat[:, 1]
        fname = f.strip('.trc').split('/')[-1]
        fname = out_folder + fname + '.dat'
        with open(fname, 'w') as f:
            f.write('#time\tvoltage\n')
            [f.write('{}\t{}\n'.format(t, v)) for t, v in zip(time, signal)]
Exemple #5
0
def save_sample_traces(filelist, sample_trace_directory_name=None):
    """
    Generates traces in the sample_pulse folder
    :param filelist: list of lecroy traces in the raw data directory
        specified in the README file.
    """

    for f in filelist:
        trc = lecroy.LecroyBinaryWaveform(f)
        time = trc.mat[:, 0]
        signal = trc.mat[:, 1]
        fname = f.strip('.trc').split('/')[-1]
        np.savetxt(sample_trace_directory_name + fname + '.txt',
                   np.array(zip(time, signal)))
Exemple #6
0
def pplot(filelist,
          height_th,
          t_initial=None,
          t_final=None,
          density=10,
          plot_every=5):
    """generates a lightweight plot of some sample traces
    WARNING: does not automatically remove trace dc offset
    :params density: plot every 'density' number of traces
    """
    plt.figure()
    for f in filelist[::density]:
        trc = lecroy.LecroyBinaryWaveform(f)
        time = trc.mat[:, 0]
        signal = trc.mat[:, 1]
        # plot_every = int(len(time)/100)
        plt.plot(time[::plot_every] * 1e6, signal[::plot_every], alpha=0.2)
        plt.xlabel('time(us)')
Exemple #7
0
def trace_extr(filename, t_initial=None, t_final=None):
    """extract relevant parameters from a trace stored in a file
    """
    trc = lecroy.LecroyBinaryWaveform(filename)
    time = trc.mat[:, 0]
    signal = trc.mat[:, 1]
    idx_0 = 0
    idx_1 = -1
    if t_initial is not None:
        idx_0 = find_idx(time, t_initial)
    if t_final is not None:
        idx_1 = find_idx(time, t_final)
    time = time[idx_0:idx_1]
    signal = signal[idx_0:idx_1]

    # add check for length of bg_points against length of signal
    # bg_points = np.min([bg_points, len(signal)])
    # bg = np.median(signal[:bg_points])
    bg = find_bg(signal)
    signal = signal - bg

    return np.array(time), np.array(signal)
Exemple #8
0
def trace_extr(filename, h_th, t_initial=None, t_final=None, zero=True):
    """extract relevant parameters from a trace stored in a file
    """
    trc = lecroy.LecroyBinaryWaveform(filename)
    time = trc.mat[:, 0]
    signal = trc.mat[:, 1]
    # _,signal = butter_lowpass_filter(trc,1000e3)
    idx_0 = 0
    idx_1 = -1
    if t_initial is not None:
        idx_0 = find_idx(time, t_initial)
    if t_final is not None:
        idx_1 = find_idx(time, t_final)
    time = time[idx_0:idx_1]
    signal = signal[idx_0:idx_1]

    bg = tp.find_bg(signal)
    # bg = np.median(signal[signal<h_th])
    signal = signal - bg

    if zero:
        time = time - np.mean(time)

    return np.array(time), np.array(signal)
Exemple #9
0
def time_vector(filename):
    trc = lecroy.LecroyBinaryWaveform(filename)
    time = trc.mat[:, 0]
    return time
Exemple #10
0
def convert_traces_to_hdf5(indir,
                           outdir,
                           outfile=None,
                           tag_trace="C4",
                           test_trace=None):
    g_tags = []

    flist = os.listdir(indir)
    # get traces from filename list
    traces = list(set([x[:2] for x in flist]))
    # tag trace goes first
    traces.remove(tag_trace)
    traces.insert(0, tag_trace)

    print("Found this traces: ", traces)

    buffer_size = 100  # images
    default_value = 0

    if outfile is None:
        outfile = indir.strip("/").split("/")[-1] + ".h5"

    fout = h5py.File(outdir + outfile, "w")
    groups = {}
    bi = 0
    li = 0

    #fnumbers = sorted(set(map(lambda x: x[7:12], flist)))
    fnumbers = None
    t_flists = {}
    t_fnumbers = {}

    # running only on events with all traces
    for trace in traces:
        t_flists[trace] = [x for x in flist if x.find(trace) != -1]
        t_fnumbers[trace] = sorted(set([x[7:12] for x in t_flists[trace]]))
        if fnumbers is None:
            fnumbers = t_fnumbers[trace]
        else:
            fnumbers = set(fnumbers) & set(t_fnumbers[trace])

    fnumbers = list(sorted(fnumbers))
    for trace in traces:
        count = 0
        t_flist = t_flists[trace]
        # first, tags... skipping events with no tag

        groups[trace] = fout.create_group(trace)

        t_size = None
        d_size = None
        t = None

        #for fi, f in enumerate(t_flist):

        if fnumbers == [] or fnumbers is None:
            print("No tags found, cannot continue")
            break
        if t_flist == []:
            print("No files found")
            break

        for fi, n in enumerate(fnumbers):
            fname = indir + trace + "Trace" + str(n).zfill(5) + ".trc"
            try:
                bwf = lecroy.LecroyBinaryWaveform(fname)
                t = bwf.WAVE_ARRAY_1_time
                d = bwf.WAVE_ARRAY_1.ravel()

                #t, d = lecroy.read_timetrace(indir + trace + "Trace" + str(n).zfill(5) + ".trc")
            except:
                print("[ERROR] file %s" % fname)
                print(sys.exc_info()[1])
                # at least the first n should exist for all traces
                if t is None:
                    continue
                else:
                    d = np.zeros((d_size, ), dtype=np.float32)
                    #print d
                    d[:] = default_value

            if t_size is None:
                t_size = t.shape[0]
                d_size = d.shape[0]
                dset_t = groups[trace].create_dataset("time", (t_size, ),
                                                      compression="gzip",
                                                      dtype=np.float64)
                dset_d = groups[trace].create_dataset("data",
                                                      (buffer_size, d_size),
                                                      maxshape=(None, d_size),
                                                      compression="gzip",
                                                      dtype=np.float32,
                                                      shuffle=False,
                                                      compression_opts=6,
                                                      chunks=(1, d_size))
                dset_n = groups[trace].create_dataset("filenumber",
                                                      (buffer_size, ),
                                                      maxshape=(None, ),
                                                      dtype=np.int,
                                                      chunks=True)
                if trace == tag_trace:
                    dset_tag = fout.create_dataset("tags", (buffer_size, ),
                                                   maxshape=(None, ),
                                                   dtype=np.int64,
                                                   chunks=True)

            if fi == 0:
                spectra_buffer = np.ndarray((buffer_size, d_size),
                                            dtype=np.float32)
                n_buffer = np.ndarray((buffer_size, ), dtype=np.int)
                if trace == tag_trace:
                    tag_buffer = np.ndarray((buffer_size, ), dtype=np.int64)
                bi = 0
                li = 0

            if bi < buffer_size:
                spectra_buffer[bi] = d
                if trace == tag_trace:
                    #print d, bwf.HORIZ_INTERVAL
                    tag_buffer[bi] = read_serial(d, bwf.HORIZ_INTERVAL)[0]
                    g_tags.append(tag_buffer[bi])
                    #print tag_buffer[bi]
                n_buffer[bi] = n
                bi += 1
            else:
                try:
                    dset_d[li:li + bi] = spectra_buffer
                    dset_n[li:li + bi] = n_buffer
                    dset_d.resize(dset_d.shape[0] + buffer_size, axis=0)
                    dset_n.resize(dset_n.shape[0] + buffer_size, axis=0)

                    if trace == tag_trace:
                        dset_tag[li:li + bi] = tag_buffer
                        dset_tag.resize(dset_tag.shape[0] + buffer_size,
                                        axis=0)

                    li += bi
                    bi = 0
                except:
                    print(sys.exc_info())
                    #print li, bi, spectra_buffer.shape
            count += 1

        if t_size is None:
            print("No files found! Are tag traces missing?")
            continue
        print(count)
        dset_t[:] = t
        if bi != 0:
            try:
                dset_d[li:li + bi] = spectra_buffer[:bi]
                dset_n[li:li + bi] = n_buffer[:bi]
                #dset_d.resize(dset_d.shape[0] + buffer_size, axis=0)
                #dset_n.resize(dset_n.shape[0] + buffer_size, axis=0)

                if trace == tag_trace:
                    dset_tag[li:li + bi] = tag_buffer[:bi]
                    #dset_tag.resize(dset_tag.shape[0] + buffer_size, axis=0)
                li += bi
                count = li

                bi = 0
            except:
                print(sys.exc_info())

        if dset_t.shape[0] >= count:
            dset_d.resize(count, axis=0)
            dset_n.resize(count, axis=0)
            dset_tag.resize(count, axis=0)

    fout.close()
    print("Output file %s created" % (outdir + outfile))
    return g_tags, outdir + outfile
Exemple #11
0
def param_extr(filename, t_initial=None, t_final=None, h_th=0.0075, t0=.56e-6):
    """extract relevant parameters from a trace stored in a file
    """
    trc = lecroy.LecroyBinaryWaveform(filename)
Exemple #12
0
                     heightattime, 
                     bg),
        dtype=[('area_win','float64'),
        ('area_abs','float64'),
        ('area','float64'),
        ('height','float64'),
        ('height_clamped','float64'),
        ('heightattime','float64'),
        ('bg','float64')]
        )

def trace_extr(filename, h_th, t_initial=None, t_final=None, zero=True):
>>>>>>> 2a32507f24526850f2f413729b6adedbf5ac41fc
    """extract relevant parameters from a trace stored in a file
    """
    trc = lecroy.LecroyBinaryWaveform(filename)
    time = trc.mat[:, 0]
    signal = trc.mat[:, 1]
    # _,signal = butter_lowpass_filter(trc,1000e3)
<<<<<<< HEAD

=======
    
>>>>>>> 2a32507f24526850f2f413729b6adedbf5ac41fc
    idx_0 = 0
    idx_1 = -1
    if t_initial is not None:
        idx_0 = find_idx(time, t_initial)
    if t_final is not None:
        idx_1 = find_idx(time, t_final)
    time = time[idx_0:idx_1]