Esempio n. 1
0
def gen_h5(source='xtc', pid=None):
    def test_callback(data_dict):
        CALLBACK_OUTPUT.append(data_dict['oneint'])

    if source == 'xtc':
        xtc_dir = os.path.join(os.environ.get('TEST_XTC_DIR', os.getcwd()),
                               '.tmp')
        ds = DataSource(exp='xpptut13',
                        run=1,
                        dir=xtc_dir,
                        filter=lambda x: True,
                        batch_size=2)
    elif source == 'shmem':
        ds = DataSource(shmem='shmem_test_' + pid)

    smd = ds.smalldata(filename='smalldata_test.h5',
                       batch_size=5,
                       callbacks=[test_callback])

    for run in ds.runs():
        # test that we can make a Detector, which is somewhat subtle
        # because SRV cores make dummy detectors using NullDataSource/NullRun
        dummydet = run.Detector('xppcspad')
        for i, evt in enumerate(run.events()):

            print('event:', i)

            smd.event(evt,
                      timestamp=evt.timestamp,
                      oneint=1,
                      twofloat=2.0,
                      arrint=np.ones(2, dtype=int),
                      arrfloat=np.ones(2, dtype=float)
                      # ragged_
                      )

            if evt.timestamp % 2 == 0:
                smd.event(
                    evt.timestamp,  # make sure passing int works
                    unaligned_int=3,
                    every_other_missing=2)

            if (rank % 2 == 0) and (smd._type == 'client'):
                smd.event(evt, missing_vds=1)
        # Todo: MONA put this in because with the new change in DgramManager,
        # it continues to look for new run in shmem mode. In this data file,
        # we have only one run so we needed to find a way to stop the process.
        # Future: We might need to test in case where where actually have multple
        # runs. For this, we may need an eof flag coming from the datasource.
        break

    if smd.summary:
        smd.save_summary({'summary_array': np.arange(3)}, summary_int=1)
    smd.done()

    return
Esempio n. 2
0
def gen_h5(source='xtc', pid=None):
    def test_callback(data_dict):
        CALLBACK_OUTPUT.append(data_dict['oneint'])

    if source == 'xtc':
        xtc_dir = os.path.join(os.environ.get('TEST_XTC_DIR', os.getcwd()),
                               '.tmp')
        ds = DataSource(exp='xpptut13',
                        run=1,
                        dir=xtc_dir,
                        filter=lambda x: True,
                        batch_size=2)
    elif source == 'shmem':
        ds = DataSource(shmem='shmem_test_' + pid)

    smd = ds.smalldata(filename='smalldata_test.h5',
                       batch_size=5,
                       callbacks=[test_callback])

    for run in ds.runs():
        # test that we can make a Detector, which is somewhat subtle
        # because SRV cores make dummy detectors using NullDataSource/NullRun
        dummydet = run.Detector('xppcspad')
        for i, evt in enumerate(run.events()):

            print('event:', i)

            smd.event(evt,
                      timestamp=evt.timestamp,
                      oneint=1,
                      twofloat=2.0,
                      arrint=np.ones(2, dtype=int),
                      arrfloat=np.ones(2, dtype=float)
                      # ragged_
                      )

            if evt.timestamp % 2 == 0:
                smd.event(
                    evt.timestamp,  # make sure passing int works
                    unaligned_int=3,
                    every_other_missing=2)

            if (rank % 2 == 0) and (smd._type == 'client'):
                smd.event(evt, missing_vds=1)

    if smd.summary:
        smd.save_summary({'summary_array': np.arange(3)}, summary_int=1)
    smd.done()

    return
Esempio n. 3
0
    def Process(self):

        if self.params['DataSource']['max_evt'] == -1:
            ds = DataSource(exp=self.params['DataSource']['exp'],
                            run=self.params['DataSource']['run'])

        else:
            ds = DataSource(exp=self.params['DataSource']['exp'],
                            run=self.params['DataSource']['run'],
                            max_events=self.params['DataSource']['max_evt'])

        myrun = next(ds.runs())

        opal = myrun.Detector(self.params['opal']['detname'])
        hsd = myrun.Detector(self.params['Waveforms']['detname'])

        det_evt = myrun.Detector('timing')
        det_gmd = myrun.Detector('gmd')
        det_xgmd = myrun.Detector('xgmd')
        det_ebm = myrun.Detector('ebeam')

        if os.path.isfile(self.params['h5nm']):
            os.remove(self.params['h5nm'])

        smd = ds.smalldata(filename=self.params['h5nm'],
                           batch_size=self.refresh_num)

        evt_dict = {}
        sevt_dict = {}

        sinit = None

        for nevt, evt in enumerate(myrun.events()):

            try:

                # Machine data
                evt_dict['evt'] = np.array(det_evt.raw.eventcodes(evt),
                                           dtype=np.int)
                evt_dict['gmd'] = det_gmd.raw.energy(evt) * 1000
                evt_dict['xgmd'] = det_xgmd.raw.energy(evt) * 1000
                evt_dict['pho'] = det_ebm.raw.ebeamPhotonEnergy(evt)
                evt_dict['ebm_l3'] = det_ebm.raw.ebeamL3Energy(evt)
                evt_dict['evt70'] = evt_dict['evt'][70]

                ##Electron data
                img = opal.raw.image(evt)
                pks = self.algos.peak_finder_v4r3_d2(
                    img,
                    self.mask,
                    thr_low=self.params['opal']['thr_low'],
                    thr_high=self.params['opal']['thr_high'],
                    rank=self.params['opal']['rank'],
                    r0=self.params['opal']['r0'],
                    dr=self.params['opal']['dr'])
                coords = np.array([[pk.col, pk.row] for pk in pks])
                ele_hits = np.ones([self.params['opal']['maxnum'], 2]) * 1e6

                evt_dict['ele_nhts'] = min(len(coords),
                                           self.params['opal']['maxnum'])
                ele_hits[:evt_dict['ele_nhts']] = coords[:evt_dict[
                    'ele_nhts'], :]
                evt_dict['ele_hits'] = ele_hits

                ##Ion data
                wfs = hsd.raw.waveforms(evt)
                factor = 1
                if sinit == None:
                    ts = wfs[channels['mcp']]['times']
                    ts = rebin1dx(ts, factor) * 1e9
                    inds_dict = {}
                    ts_dict = {}
                    for ik, mk in enumerate(self.tof_dict.keys()):
                        inds_dict[mk] = ((ts > self.tof_dict[mk][0]) &
                                         (ts < self.tof_dict[mk][1]))
                        ts_dict[mk] = ts[inds_dict[mk]]

                    sevt_dict['tof'] = np.zeros_like(ts)
                    sinit = 1

                wf_mcp = wfs[channels['mcp']][0].astype(np.float)
                wf_mcp = rebin1d(wf_mcp, factor)
                wf_mcp = wf_mcp - np.mean(wf_mcp[-100:])

                wf_mcp = gaussian_filter1d(wf_mcp, 3)

                recov = deconv_f(ts[1] - ts[0], wf_mcp, y_resp, 5)
                wf_mcp = np.abs(recov)
                wf_mcp = wf_mcp[:min(len(ts), len(wf_mcp))]

                for ik, mk in enumerate(self.tof_dict.keys()):
                    evt_dict[mk + '_max'] = np.max(wf_mcp[inds_dict[mk]])
                    evt_dict[mk + '_sum'] = np.sum(wf_mcp[inds_dict[mk]])

                sevt_dict['tof'] += wf_mcp

                evt_dict['index'] = nevt
                smd.event(evt, evt_dict)

            except Exception as e:
                print(e)

            print('Event Num:', nevt, ', Max Event Num:',
                  self.params['DataSource']['max_evt'])

            if nevt == self.params['DataSource']['max_evt'] - 1:
                break

        if smd.summary:
            smd_dict = {}
            for k in sevt_dict.keys():
                smd_dict[k] = smd.sum(sevt_dict[k])
            smd.save_summary({'smd_dict': smd_dict})
        smd.done()
Esempio n. 4
0
        lastimg = data_dict['opal']
    mydeque.append(data_dict['opalsum'])
    if numevents % 100 == 0:  # update plots around 1Hz
        print('event:', numevents)
        myxyplot = XYPlot(numevents,
                          "Last 25 Sums",
                          np.arange(len(mydeque)),
                          np.array(mydeque),
                          formats='o')
        publish.send("OPALSUMS", myxyplot)
        if lastimg is not None:  # opal image is not sent all the time
            myimgplot = Image(numevents, "Opal Image", lastimg)
            publish.send("OPALIMG", myimgplot)


while 1:  # mpi worker processes
    ds = DataSource(shmem='rix')
    smd = ds.smalldata(batch_size=5, callbacks=[my_smalldata])
    for myrun in ds.runs():
        opal = myrun.Detector('atmopal')
        for nevt, evt in enumerate(myrun.events()):
            mydict = {}
            image = opal.raw.image(evt)
            if image is None: continue
            # do as much work as possible in the workers
            # don't send large data all the time, if possible
            if nevt % 10 == 0: mydict['opal'] = image
            mydict['opalsum'] = np.sum(image)
            smd.event(evt, mydict)
        smd.event(evt, {'endrun': 1})  # tells gatherer to reset plots
Esempio n. 5
0
from psana import DataSource
from psana.peakFinder.blobfinder import find_blobs_2d
import numpy as np
import os
os.environ['PS_SRV_NODES'] = '1'
ds = DataSource(exp='xpptut13', run=1, dir='/cds/home/c/cpo/git/psana_cpo/ued')
for myrun in ds.runs():
    det = myrun.Detector('epix10k2M')
    threshold = 2.0
    smallh5 = ds.smalldata(filename='blobs.h5')
    max_blobs = 50
    for nevt, evt in enumerate(myrun.events()):
        image = det.raw.image(evt)
        if image is None: continue  # check for missing data
        nblobs, x_blobs, y_blobs, energy_blobs = find_blobs_2d(
            image, threshold, 2.0)
        # currently only support fixed-length arrays in h5
        if nblobs > max_blobs:
            print('too many blobs')
            continue
        x = np.zeros((max_blobs), dtype=np.float32)
        y = np.zeros((max_blobs), dtype=np.float32)
        energy = np.zeros((max_blobs), dtype=np.float32)
        x[:nblobs] = x_blobs
        y[:nblobs] = y_blobs
        energy[:nblobs] = energy_blobs
        smallh5.event(evt, nblobs=nblobs, x=x, y=y, energy=energy)
        print('Event', nevt, 'found', nblobs, 'blobs')
    smallh5.done()
Esempio n. 6
0
    def Process(self):   

        if 'max_evt' in self.params.keys():
            ds = DataSource(exp=self.params['exp'],run=self.params['run_num'],max_events=self.params['max_evt']) 
        else:
            ds = DataSource(exp=self.params['exp'],run=self.params['run_num'])             

        myrun = next(ds.runs())
        hsd = myrun.Detector('hsd')   

        det_evt = myrun.Detector('timing')
        det_gmd = myrun.Detector('gmd')
        det_xgmd = myrun.Detector('xgmd')
        det_ebm = myrun.Detector('ebeam')


        if os.path.isfile(self.params['hdf5']):
            os.remove(self.params['hdf5'])



        smd = ds.smalldata(filename=self.params['hdf5'], batch_size=self.params['refresh_num'])

        evt_dict = {}
        sevt_dict = {}


        sinit=None

        for nevt,evt in enumerate(myrun.events()):
            
            try:

                # Machine data
                evt_dict['evt'] = np.array(det_evt.raw.eventcodes(evt),dtype=np.int)
                evt_dict['gmd'] = det_gmd.raw.energy(evt)*1000
                evt_dict['xgmd'] = det_xgmd.raw.energy(evt)*1000
                evt_dict['pho'] = det_ebm.raw.ebeamPhotonEnergy(evt)
                evt_dict['ebm_l3'] = det_ebm.raw.ebeamL3Energy(evt)

                ##Ion data
                wfs = hsd.raw.waveforms(evt)
                wf_mcp = wfs[0][0].astype(np.float)
                
                if sinit == None:
                    for nm in ['tof','tof_67','tof_68','tof_67n','tof_68n']:
                        sevt_dict[nm] = np.zeros_like(wf_mcp) 
                    sinit = 1

                
                if evt_dict['evt'][161]==0:
                    if evt_dict['evt'][67]==1:
                        sevt_dict['tof_67'] += wf_mcp
                        sevt_dict['tof_67n'] += wf_mcp/evt_dict['xgmd']
                    if evt_dict['evt'][68]==1:
                        sevt_dict['tof_68'] += wf_mcp
                        sevt_dict['tof_68n'] += wf_mcp/evt_dict['xgmd']
                
                smd.event(evt, evt_dict)                


            except Exception as e:
                print(e)

            print('Event Num:',nevt) 
   

        if smd.summary:
            smd_dict = {}
            for nm in ['tof','tof_67','tof_68','tof_67n','tof_68n']:
                smd_dict[nm] = smd.sum(sevt_dict[nm])
                
            smd.save_summary({'sig_sum':smd_dict})
        smd.done()
Esempio n. 7
0
from psana import DataSource
import numpy as np
import sys
import os

exp=sys.argv[1]
runnum=int(sys.argv[2])
hutch=exp[:3]

os.environ['PS_SRV_NODES']='1'

ds = DataSource(exp=exp,run=runnum)
#outfile='/cds/data/psdm/'+hutch+'/exp/hdf5/run'+str(runnum)+'.h5'
outfile='/cds/data/psdm/xpp/xpptut15/scratch/cpo/run'+str(runnum)+'.h5'
smd = ds.smalldata(filename=outfile, batch_size=5)

def fill_dict(mydict, obj, evt, name, epics):
    mydict[name]={}
    localdict = mydict[name]
    for attrstr in dir(obj):
        if attrstr[0]=='_' or attrstr=='calibconst': continue
        attr = getattr(obj,attrstr)
        if callable(attr):
            if epics:
                val = attr()
            else:
                val = attr(evt)
            if val is not None:
                if type(val) is list: # for eventcodes
                    localdict[attrstr] = np.array(val)
Esempio n. 8
0
import numpy as np


def my_evt_filter(smalldata_evt):  # throw away bad events using small data
    return True


def my_realtime_monitor(
        user_data_dict):  # watch user-defined data from smd.event() below
    print(user_data_dict)


ds = DataSource(exp='tstx00517', run=49, filter=my_evt_filter)

smd = ds.smalldata(filename='run49.h5',
                   batch_size=5,
                   callbacks=[my_realtime_monitor])
run = next(ds.runs())
det = run.Detector('tmohsd')  # several high speed digitizers

mysum = 0.0
hsd_num = 0
chan_num = 0
for i, evt in enumerate(run.events()):
    waveforms = det.raw.waveforms(evt)
    peaks = det.raw.peaks(evt)
    if waveforms is None:
        continue
    mypeak = waveforms[hsd_num][chan_num][12:15]  # select "peak" in waveform 0
    myarea = np.sum(mypeak)  # integrate the peak area
    smd.event(evt, mypeak=mypeak, myarea=myarea)
Esempio n. 9
0
}
#print(params)
params = params
params['hdf5'] = params['hdf5'] + str(params['run_num']) + '.h5'

if os.path.isfile(params['hdf5']):
    os.remove(params['hdf5'])

if 'max_evt' in params.keys():
    ds = DataSource(exp=params['exp'],
                    run=params['run_num'],
                    max_events=params['max_evt'])
else:
    ds = DataSource(exp=params['exp'], run=params['run_num'])

smd = ds.smalldata(filename=params['hdf5'], batch_size=params['refresh_num'])

for myrun in ds.runs():
    hsd = myrun.Detector('hsd')

    det_evt = myrun.Detector('timing')
    det_gmd = myrun.Detector('gmd')
    det_xgmd = myrun.Detector('xgmd')
    det_ebm = myrun.Detector('ebeam')

    det_las_t = myrun.Detector('las_fs14_target_time')
    det_las_d = myrun.Detector('las_fs14_target_time_dial')

    las_t = []
    las_d = []
Esempio n. 10
0
def proc_data(**kwargs):
    XTCDIR       = kwargs.get('xtcdir', '/cds/home/m/monarin/psana-nersc/psana2/dgrampy/amox27716')
    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)
    TESTMODE     = kwargs.get('testmode', 1)
    ROENTDEK     = kwargs.get('roentdek', 'dldproc')
    MONITOR      = kwargs.get('monitor', False)
    DETECTORS    = kwargs.get('detectors', 1)

    # Open datasource
    ds = DataSource(exp=EXP, 
                    run=RUN, 
                    dir=XTCDIR, 
                    max_events=EVENTS,
                    monitor=MONITOR,
                   )
    run = next(ds.runs())
    det = run.Detector("tmo_quadanode")
    
    # Hidden-rank problem
    if det:
        # Update calibration constants
        dldpars = {'consts':det.calibconst}
        kwargs.update(dldpars)
        
        # Initialize PyCFD per channel
        cfds = {}
        for i_chan in range(NUMCHS):
            cfds[i_chan] = PyCFD(PARAMSCFD[i_chan])
        
        # Intitialize Roentdek wrapper and Xiang's hitfinder
        proc  = DLDProcessor(**kwargs)
        HF = HitFinder(kwargs)
        if PLOT: stats = DLDStatistics(proc,**kwargs)

        # Collect batch rate with t0 and t1
        t0 = time.monotonic()

        # Find peaks for each channel and saves them in a fixed
        # size array for running Roentdek algorithms
        cn_no_peaks_events = 0
        cn_peaks = np.zeros(EVENTS, dtype=np.int64)
        dt_pks = np.zeros(EVENTS, dtype=np.float64)
        dt_rtks = np.zeros(EVENTS, dtype=np.float64)
        
        # Aux arrays for passing N-channels data to Roentdek
        nhits_fex = np.zeros(NUMCHS * DETECTORS, dtype=np.int64)
        pktsec_fex = np.zeros([NUMCHS * DETECTORS, NUMHITS], dtype=np.float64)

    # Setup srv nodes if writing is requested
    if TESTMODE == 4:
        smd_batch_size = 1000
        smd = ds.smalldata(filename='/cds/data/drpsrcf/users/monarin/amox27716/out/mysmallh5.h5', 
                           batch_size=smd_batch_size, 
                          )
    
    for i_ev, evt in enumerate(run.events()):
        if VERBOSE: print(f'EVENT: {i_ev}')
        
        # Get list of waveforms and startpos by segment id
        waveforms = det.fex.waveforms(evt, n_dets=DETECTORS)
        times = det.fex.times(evt, n_dets=DETECTORS)

        # Run peakfinder
        # For > 1 detector, we get new detector at every NUMCHS streams.
        # Note that we reuse PyCFD for all detectors (for testing).
        t0_pk = time.monotonic()
        if TESTMODE in (2, 3, 4):
            n_result_peaks = np.zeros(NUMCHS * DETECTORS, dtype=np.int64)
            for i_det in range(DETECTORS):
                for i_chan in range(NUMCHS):
                    seg_id = int(i_det * NUMCHS + i_chan)
                    # TODO: Eliminate sample_interval
                    result_peaks = find_peaks(waveforms[seg_id], 
                                              times[seg_id], 
                                              cfds[i_chan], 
                                             )
                    
                    n_result_peaks[seg_id] = len(result_peaks)
                    if VERBOSE: print(f'  det: {i_det} chan:{i_chan} #peaks:{len(result_peaks)}')
                    
                    # Save hits and peaks from fex data
                    nhits_chan_fex = min(len(result_peaks), NUMHITS)
                    nhits_fex[seg_id] = nhits_chan_fex
                    pktsec_fex[seg_id,:nhits_chan_fex] = result_peaks[:nhits_chan_fex]
                    pktsec_fex[seg_id,nhits_chan_fex:] = 0
                
            if np.all(n_result_peaks==0): cn_no_peaks_events += 1
            cn_peaks[i_ev] = np.prod(n_result_peaks + 1)
        t1_pk = time.monotonic()
        dt_pks[i_ev] = t1_pk - t0_pk

        # Run Roentdek
        t0_rtk = time.monotonic()
        rt_hits = np.zeros([DETECTORS, NUMHITS, 4], dtype=np.float64)
        if TESTMODE in (3, 4) and np.sum(n_result_peaks) > 0:
            for i_det in range(DETECTORS):
                st = int(i_det * NUMCHS)
                en = st + NUMCHS

                if ROENTDEK == 'dldproc':
                    # Roentdek wrapper
                    # TODO: With Mikhail - eliminate i_ev
                    proc.event_proc(i_ev, nhits_fex[st:en], pktsec_fex[st:en, :])
                    for i,(x,y,r,t) in enumerate(proc.xyrt_list(i_ev, nhits_fex[st:en], pktsec_fex[st:en,:])):
                        # Allow saving upto NUMHITS
                        if i == NUMHITS: break
                        rt_hits[i_det, i, :] = [x,y,r,t]
                        if VERBOSE: print('    hit:%2d x:%7.3f y:%7.3f t:%10.5g r:%7.3f' % (i,x,y,t,r))
                else: 
                    # Xiang's peakfinding
                    HF.FindHits(pktsec_fex[4,:nhits_fex[4]],
                            pktsec_fex[0,:nhits_fex[0]],
                            pktsec_fex[1,:nhits_fex[1]],
                            pktsec_fex[2,:nhits_fex[2]],
                            pktsec_fex[3,:nhits_fex[3]])
                    xs1,ys1,ts1 = HF.GetXYT()
                    if VERBOSE: print(f'xs1={xs1} ys1={ys1} ts1={ts1}')

                if PLOT: stats.fill_data(nhits_fex[st:en], pktsec_fex[st:en, :])

        t1_rtk = time.monotonic()
        dt_rtks[i_ev] = t1_rtk - t0_rtk

        # Calculate batch rate
        if i_ev % 1000 == 0 and i_ev > 0:
            t1 = time.monotonic()
            print(f'RANK: {rank} RATE: {(1000/(t1-t0))*1e-3:.2f} kHz')
            t0 = time.monotonic()

        if TESTMODE == 4:
            smd.event(evt, peaks=pktsec_fex, hits=rt_hits)

    # end for i_ev in...
    if TESTMODE == 4:
        smd.done()

    if PLOT:
        draw_plots(stats, prefix=OFPREFIX+EXP+f'-r{str(RUN).zfill(4)}', do_save=True, hwin_x0y0=(0,10))

    # Plot correlation between calc. time and #peaks and histogram of calc.time
    #plt.subplot(2,2,1)
    #plt.hist(dt_pks*1e3)
    #plt.title('Histogram of peak finding time (ms)')
    #plt.subplot(2,2,2)
    #plt.scatter(cn_peaks, dt_pks*1e3)
    #plt.title('CC of #peaks and peakfinding time (ms)')
    #plt.subplot(2,2,3)
    #plt.hist(dt_rtks*1e3)
    #plt.title('Histogram of Roentdek calc. time (ms)')
    #plt.subplot(2,2,4)
    #plt.scatter(cn_peaks, dt_rtks*1e3)
    #plt.title('CC of #np.prod(peaks+1) and Roentdek calc. time (ms)')
    #plt.show()

    #ind = cn_peaks == 0
    #plt.subplot(2,1,1)
    #plt.plot(dt_rtks[ind])
    #plt.title('Roentdek Calc. Time (s.) for Zero-peak events')
    #plt.subplot(2,1,2)
    #plt.plot(cn_peaks[ind])
    #plt.show()
    
    if VERBOSE: print(f'EVENTS={EVENTS} NO PEAK EVENTS={cn_no_peaks_events}')