def proc_data(**kwargs):

    DSNAME       = kwargs.get('dsname', '/reg/g/psdm/detector/data2_test/xtc/data-amox27716-r0100-acqiris-e000100.xtc2')
    DETNAME      = kwargs.get('detname','tmo_hexanode')
    NUM_CHANNELS = kwargs.get('numchs', 5)
    NUM_HITS     = kwargs.get('numhits', 16)
    EVSKIP       = kwargs.get('evskip', 0)
    EVENTS       = kwargs.get('events', 10) + EVSKIP
    EXP          = kwargs.get('exp', 'amox27716')
    RUN          = kwargs.get('run', 100)
    OFPREFIX     = kwargs.get('ofprefix','./')
    ofname       = '%s%s-r%04d-e%06d-single-node.h5' % (OFPREFIX, EXP, int(RUN), EVENTS)

    tname = sys.argv[1] if len(sys.argv) > 1 else '1'
    print('TEST %s' % tname)

    logger.info(str_kwargs(kwargs, title='Input parameters:'))

    peaks = WFPeaks(**kwargs)
    ofile = open_output_h5file(ofname, peaks, **kwargs)

    ds = DataSource(files=DSNAME)
    orun = next(ds.runs())
    det = orun.Detector(DETNAME)

    for nevt,evt in enumerate(orun.events()):
        if nevt<EVSKIP : continue
        if nevt>EVENTS : break
        print('Event %3d'%nevt)
        wts = det.raw.times(evt)
        wfs = det.raw.waveforms(evt)

        t0_sec = time()

        # ACCESS FOR TEST 1:
        if tname == '1' :
            nhits, pkinds, pkvals, pktns = peaks(wfs,wts)

        # ALTERNATIVE ACCESS:
        else :
            peaks.proc_waveforms(wfs, wts) 
            nhits  = peaks.number_of_hits(wfs, wts)
            pktns  = peaks.peak_times_ns(wfs, wts)
            pkvals = peaks.peak_values(wfs, wts)
            pkinds = peaks.peak_indexes(wfs, wts)

        print("  waveforms processing time = %.6f sec" % (time() - t0_sec))
        print_ndarr(wfs,   '  waveforms      : ', last=4)
        print_ndarr(nhits, '  number_of_hits : ')
        print_ndarr(wts,   '  times          : ', last=4)
        print_ndarr(pktns, '  peak_times_ns  : ', last=4)

        ofile.add_event_to_h5file()

    print('\n',usage())
    sys.exit('TEST %s EXIT' % tname)
Exemplo n.º 2
0
 def __init__(self, **kwargs):
     """
     """
     logger.debug(str_kwargs(kwargs, title='WFHDF5IO input parameters:'))
     self._of = None
     self._if = None
     self.RUN = kwargs.get('run', 0)
     self.EXP = kwargs.get('exp', 'test1234')
     self.TDC_RESOLUTION = kwargs.get('tdc_resolution', 0.250)  # ns
     self._size_increment = kwargs.get('size_increment', 4096)
Exemplo n.º 3
0
def proc_data(**kwargs):

    logger.info(str_kwargs(kwargs, title='Input parameters:'))

    DSNAME = kwargs.get(
        'dsname',
        '/reg/g/psdm/detector/data2_test/xtc/data-amox27716-r0100-acqiris-e000100.xtc2'
    )
    DETNAME = kwargs.get('detname', 'tmo_quadanode')
    EVSKIP = kwargs.get('evskip', 0)
    EVENTS = kwargs.get('events', 100) + EVSKIP
    OFPREFIX = kwargs.get('ofprefix', './')
    VERBOSE = kwargs.get('verbose', False)

    ds = DataSource(files=DSNAME)
    orun = next(ds.runs())
    det = orun.Detector(DETNAME)

    kwargs['consts'] = det.calibconst

    peaks = WFPeaks(**kwargs)
    proc = DLDProcessor(**kwargs)
    stats = DLDStatistics(proc, **kwargs)

    for nev, evt in enumerate(orun.events()):
        if nev < EVSKIP: continue
        if nev > EVENTS: break

        if do_print(nev): logger.info('Event %3d' % nev)
        t0_sec = time()

        wts = det.raw.times(evt)
        wfs = det.raw.waveforms(evt)

        nhits, pkinds, pkvals, pktsec = peaks(wfs, wts)

        if VERBOSE:
            print("  waveforms processing time = %.6f sec" % (time() - t0_sec))
            print_ndarr(wfs, '  waveforms     : ', last=4)
            print_ndarr(wts, '  times         : ', last=4)
            print_ndarr(nhits, '  number_of_hits: ')
            print_ndarr(pktsec, '  peak_times_sec: ', last=4)

        proc.event_proc(nev, nhits, pktsec)

        stats.fill_data(nhits, pktsec)

        if VERBOSE:
            for i, (x, y, r,
                    t) in enumerate(proc.xyrt_list(nev, nhits, pktsec)):
                print('    hit:%2d x:%7.3f y:%7.3f t:%10.5g r:%7.3f' %
                      (i, x, y, t, r))

    draw_plots(stats, prefix=OFPREFIX, do_save=True, hwin_x0y0=(0, 10))
def proc_data(**kwargs):

    logger.info(str_kwargs(kwargs, title='Input parameters:'))

    IFNAME = kwargs.get(
        'ifname',
        '/reg/g/psdm/detector/data_test/hdf5/amox27716-r0100-e060000-single-node.h5'
    )
    EVSKIP = kwargs.get('evskip', 0)
    EVENTS = kwargs.get('events', 10) + EVSKIP
    OFPREFIX = kwargs.get('ofprefix', './')
    VERBOSE = kwargs.get('verbose', False)

    proc = DLDProcessor(**kwargs)
    stats = DLDStatistics(proc, **kwargs)

    f = open_input_h5file(IFNAME, **kwargs)
    print('  file: %s\n  number of events in file %d' %
          (IFNAME, f.events_in_h5file()))

    t0_sec = time()
    nev = 0
    while f.next_event():
        nev = f.event_number()

        if nev < EVSKIP: continue
        if nev > EVENTS: break

        if do_print(nev): logger.info('Event %3d' % nev)

        nhits, pktsec = f.peak_arrays()

        if VERBOSE:
            print_ndarr(nhits, '  number_of_hits: ')
            print_ndarr(pktsec, '  peak_times_sec: ', last=4)

        proc.event_proc(nev, nhits, pktsec)

        stats.fill_data(nhits, pktsec)

        if VERBOSE:
            for i, (x, y, r,
                    t) in enumerate(proc.xyrt_list(nev, nhits, pktsec)):
                print('    hit:%2d x:%7.3f y:%7.3f t:%10.5g r:%7.3f' %
                      (i, x, y, t, r))

    dt = time() - t0_sec
    print('%d events processing time = %.3f sec or %.6f sec/event or %.3f Hz' %
          (nev, dt, dt / nev, nev / dt))

    draw_plots(stats, prefix=OFPREFIX, do_save=True, hwin_x0y0=(0, 10))
Exemplo n.º 5
0
def proc_data(**kwargs):

    logger.info(str_kwargs(kwargs, title='Input parameters:'))

    DSNAME = kwargs.get(
        'dsname',
        '/reg/g/psdm/detector/data2_test/xtc/data-amox27716-r0100-acqiris-e001000.xtc2'
    )
    DETNAME = kwargs.get('detname', 'tmo_quadanode')
    EVSKIP = kwargs.get('evskip', 0)
    EVENTS = kwargs.get('events', 10) + EVSKIP
    EXP = kwargs.get('exp', 'amox27716')
    RUN = kwargs.get('run', 100)
    VERBOSE = kwargs.get('verbose', False)
    OFPREFIX = kwargs.get('ofprefix', './')
    ofname = '%s%s-r%04d-e%06d-ex-22_lowerthresh.h5' % (OFPREFIX, EXP,
                                                        int(RUN), EVENTS)

    peaks = WFPeaks(**kwargs)
    ofile = open_output_h5file(ofname, peaks, **kwargs)

    ds = DataSource(files=DSNAME)
    orun = next(ds.runs())
    det = orun.Detector(DETNAME)

    for nev, evt in enumerate(orun.events()):

        if nev < EVSKIP: continue
        if nev > EVENTS: break

        if do_print(nev): logger.info('Event %4d' % nev)
        t0_sec = time()

        wts = det.raw.times(evt)
        wfs = det.raw.waveforms(evt)

        nhits, pkinds, pkvals, pktsec = peaks(wfs, wts)  # ACCESS TO PEAK INFO

        if VERBOSE:
            print("  ev:%4d waveforms processing time = %.6f sec" %
                  (nev, time() - t0_sec))
            print_ndarr(wfs, '    waveforms      : ', last=4)
            print_ndarr(wts, '    times          : ', last=4)
            print_ndarr(nhits, '    number_of_hits : ')
            print_ndarr(pktsec, '    peak_times_sec : ', last=4)

        ofile.add_event_to_h5file()
Exemplo n.º 6
0
def proc_data(**kwargs):

    logger.info(str_kwargs(kwargs, title='Input parameters:'))

    DSNAME = kwargs.get(
        'dsname',
        '/reg/g/psdm/detector/data2_test/xtc/data-amox27716-r0100-acqiris-e000100.xtc2'
    )
    DETNAME = kwargs.get('detname', 'tmo_quadanode')
    EVSKIP = kwargs.get('evskip', 0)
    EVENTS = kwargs.get('events', 10) + EVSKIP
    VERBOSE = kwargs.get('verbose', False)

    ds = DataSource(files=DSNAME)
    orun = next(ds.runs())

    print('\nruninfo expt: %s  runnum: %d' % (orun.expt, orun.runnum))

    det = orun.Detector(DETNAME)
    #kwargs['detobj'] = det
    kwargs['consts'] = det.calibconst

    peaks = WFPeaks(**kwargs)
    proc = DLDProcessor(**kwargs)  #detobj=det to get cfg/calib constants

    for nev, evt in enumerate(orun.events()):
        if nev < EVSKIP: continue
        if nev > EVENTS: break

        if do_print(nev): logger.info('Event %3d' % nev)
        t0_sec = time()

        wts = det.raw.times(evt)
        wfs = det.raw.waveforms(evt)

        nhits, pkinds, pkvals, pktsec = peaks(wfs, wts)  # ACCESS TO PEAK INFO

        if VERBOSE:
            print("  waveforms processing time = %.6f sec" % (time() - t0_sec))
            print_ndarr(wfs, '  waveforms      : ', last=4)
            print_ndarr(wts, '  times          : ', last=4)
            print_ndarr(nhits, '  number_of_hits : ')
            print_ndarr(pktsec, '  peak_times_sec : ', last=4)

        for i, (x, y, r, t) in enumerate(proc.xyrt_list(nev, nhits, pktsec)):
            print('    hit:%2d x:%7.3f y:%7.3f t:%10.5g r:%7.3f' %
                  (i, x, y, t, r))
Exemplo n.º 7
0
else:
    kwargs.update({
        'version': 1,
        'cfd_base': 0,
        'cfd_thr': -0.05,
        'cfd_cfr': 0.85,
        'cfd_deadtime': 10.0,
        'cfd_leadingedge': True,
        'cfd_ioffsetbeg': 1000,
        'cfd_ioffsetend': 2000,
        'cfd_wfbinbeg': binmin,
        'cfd_wfbinend': binmax,
    })

from psana.pyalgos.generic.Utils import str_kwargs
print(str_kwargs(kwargs, title='Input parameters:'))

# algorithm initialization in global scope
peaks = WFPeaks(**kwargs)

# global parameters for graphics

tbin_ns = 0.25 * 1e-9
#time_range_sec=(0.0000014,0.0000056)
time_range_sec = (binmin * tbin_ns, binmax * tbin_ns)
#time_range_sec=(0.0000000,0.0000111) # entire wf duration in this experiment

naxes = 5  # 5 for quad- or 7 for hex-anode
# assumes that lcls2 detector data returns channels
# in desired order for u1, u2, v1, v2, [w1, w2,] mcp
Exemplo n.º 8
0
def proc_data(**kwargs):

    logger.info(str_kwargs(kwargs, title='Input parameters:'))

    DSNAME = kwargs.get(
        'dsname',
        '/reg/g/psdm/detector/data2_test/xtc/data-amox27716-r0100-acqiris-e001000.xtc2'
    )
    DETNAME = kwargs.get('detname', 'tmo_quadanode')
    EVSKIP = kwargs.get('evskip', 0)
    EVENTS = kwargs.get('events', 10) + EVSKIP
    EXP = kwargs.get('exp', 'amox27716')
    RUN = kwargs.get('run', 85)
    VERBOSE = kwargs.get('verbose', True)
    PLOT = kwargs.get('plot', False)
    OFPREFIX = kwargs.get('ofprefix', './')
    PARAMSCFD = kwargs.get('paramsCFD')
    NUMCHS = kwargs.get('numchs', 5)
    NUMHITS = kwargs.get('numhits', 16)

    ds = DataSource(files=DSNAME)
    orun = next(ds.runs())
    det = orun.Detector(DETNAME)
    dldpars = {'consts': det.calibconst}

    # Update calibcfg and calibtab
    kwargs.update(dldpars)

    tb_sec = time()
    nev = 0

    # Counts no. of channels with different no. peaks from CFD(raw) and CFD(win)
    cn_match = 0
    cn_unmatch = 0
    cn_match_dld = 0
    cn_unmatch_dld = 0

    # Summing differences between each peak from CFD(raw) and CFD(win)
    sum_err = 0
    sum_pks = 0

    # Summing differences between x,y,r,t (output from Roentdek)
    sum_err_dld = np.zeros([EVENTS, 4], dtype=np.float64)

    # Initialize PyCFD per channel
    cfds = {}
    for i_chan in range(NUMCHS):
        CFD_params = PARAMSCFD[i_chan]
        cfds[i_chan] = PyCFD(CFD_params)

    # This is used for calculate Roentdek algorithm (_c is for checking
    # peaks from simulated fex data).
    proc = DLDProcessor(**kwargs)
    proc_c = DLDProcessor(**kwargs)

    # Initialize dgrampy and generate starting transitions for xtc2
    out_i_chan = 4
    out_xtcfname = f"data-r0085-s{str(out_i_chan).zfill(3)}-c000.xtc2"
    segment_id = 4
    ts = 0
    data_names, datadef = dgrampy_start(EXP, RUN, out_xtcfname, segment_id, ts)

    ts += 4  # Configure, BeginRun, BeginStep, Enable
    for nev, evt in enumerate(orun.events()):

        if nev < EVSKIP: continue
        if nev >= EVENTS: break

        if do_print(nev): logger.info('Event %4d' % nev)
        t0_sec = time()

        wts = det.raw.times(evt)
        wfs = det.raw.waveforms(evt)

        # Function peaks returns `pktsec` for each channel. We need to locate
        # index of these peaks in wts. The indices will be used to identify
        # windows of waveform wfs and startpos in wts.
        NUMCHS = wfs.shape[0]
        window_size = 8
        nhits = np.zeros(NUMCHS, dtype=np.int64)
        pktsec = np.zeros([NUMCHS, NUMHITS], dtype=wts.dtype)

        nhits_fex = np.zeros(NUMCHS, dtype=np.int64)
        pktsec_fex = np.zeros([NUMCHS, NUMHITS], dtype=wts.dtype)

        for i_chan in range(NUMCHS):
            # Find peaks using CFD
            CFD_params = PARAMSCFD[i_chan]
            CFD = cfds[i_chan]

            pktsec_chan = CFD.CFD(wfs[i_chan, :], wts[i_chan, :])
            nhits_chan = min(len(pktsec_chan), NUMHITS)
            nhits[i_chan] = nhits_chan

            pktsec[i_chan, :nhits_chan] = pktsec_chan[:nhits_chan]

            # Calculate sample interval
            sample_intervals = wts[i_chan, 1:] - wts[i_chan, :-1]

            # Find peak indices
            peak_ind = np.searchsorted(wts[i_chan, :], pktsec_chan)

            if False:
                plt.plot(wts[i_chan, :], wfs[i_chan, :], label='waveform')
                # Get peak values from found indices
                pktval = wfs[i_chan, peak_ind]
                plt.scatter(pktsec_chan,
                            pktval,
                            marker='o',
                            c='r',
                            label=f'CFD peaks #{nhits[i_chan]}')
                plt.scatter(
                    wts[i_chan, peak_ind],
                    pktval,
                    marker='x',
                    c='g',
                    label=f'ts from found indices #{len(wts[i_chan, peak_ind])}'
                )
                plt.legend()
                plt.show()

            # Find peak windows
            pkwin_list, startpos_list, result_peaks = get_window_from_peaks(
                wfs[i_chan, :],
                wts[i_chan, :],
                peak_ind,
                window_size,
                plot=PLOT,
                CFD=CFD,
                sample_period=CFD_params['sample_interval'],
                threshold=CFD_params['threshold'])

            # Save hits and peaks from fex data
            nhits_chan_fex = min(len(result_peaks), NUMHITS)
            nhits_fex[i_chan] = nhits_chan_fex
            pktsec_fex[i_chan, :nhits_chan_fex] = result_peaks[:nhits_chan_fex]

            # Write out one channel at a time (change out_i_chan to switch file)
            if i_chan == out_i_chan:
                dgrampy_adddata(data_names, datadef, startpos_list, pkwin_list,
                                ts)
                ts += 1

            # Calculate the differences betwen CFD(raw) and CFD(windows)
            if len(result_peaks) == nhits[i_chan]:
                sum_err += np.sum(
                    np.absolute(
                        np.asarray(pktsec_chan) - np.asarray(result_peaks)))
                sum_pks += nhits[i_chan]
                cn_match += 1
            else:
                cn_unmatch += 1

        # end for i_chan...

        # Test RoenDek algorithm
        proc.event_proc(nev, nhits, pktsec)
        proc_c.event_proc(nev, nhits_fex, pktsec_fex)
        xyrt = proc.xyrt_list(nev, nhits, pktsec)
        xyrt_c = proc_c.xyrt_list(nev, nhits_fex, pktsec_fex)
        if len(xyrt) == len(xyrt_c):
            for i, ((x, y, r, t), (x_c, y_c, r_c,
                                   t_c)) in enumerate(zip(xyrt, xyrt_c)):
                dx = abs(x - x_c)
                dy = abs(y - y_c)
                dr = abs(r - r_c)
                dt = abs(t - t_c)
                sum_err_dld[nev, :] = [dx, dy, dr, dt]
                print('  ev:%4d hit:%2d dx:%7.3f dy:%7.3f dt:%10.5g dr:%7.3f' %
                      (nev, i, dx, dy, dt, dr))
            cn_match_dld += 1
        else:
            cn_unmatch_dld += 1

        if VERBOSE:
            print("  ev:%4d waveforms processing time = %.6f sec" %
                  (nev, time() - t0_sec))
            print_ndarr(wfs, '    waveforms      : ', last=4)
            print_ndarr(wts, '    times          : ', last=4)
            print_ndarr(nhits, '    number_of_hits : ')

    # end for nev, evt in...
    dgrampy_done(ts)

    print("Summary ev:%4d processing time = %.6f sec" % (nev, time() - tb_sec))
    print(
        f"average err per channel: {sum_err/sum_pks} #unmatch: {cn_unmatch} ({(cn_unmatch/(5*nev))*100:.2f} %)"
    )
    print(
        f"average err(x,y,r,t) per evt:  {np.sum(sum_err_dld, axis=0)/nev} #unmatch: {cn_unmatch_dld} ({(cn_unmatch_dld/nev)*100:.2f} %)"
    )