Skip to content
Snippets Groups Projects
xgm.py 48.3 KiB
Newer Older
            period = 24 * step
        else:
            period = 1
    results = xr.DataArray(np.empty((data.trainId.shape[0], npulses)), coords=data[keyraw].coords,
                           dims=['trainId', 'peakId'.format(channel)])
    for i in range(npulses):
        a = intstart + period*i
        b = intstop + period*i
        bkga = bkgstart + period*i
        bkgb = bkgstop + period*i
        bg = np.outer(np.median(data[keyraw][:,bkga:bkgb], axis=1), np.ones(b-a))
        integ = np.trapz(data[keyraw][:,a:b] - bg, axis=1)
        results[:,i] = integ
    return results


def mergeFastAdcPeaks(data, channel, intstart, intstop, bkgstart, bkgstop, 
                      period=None, npulses=None, dim='lasPulseId'):
    ''' Calculates the peaks from Fast ADC raw traces with fastAdcPeaks()
        and merges the results in Dataset.
        Inputs:
            data: xr Dataset with 'FastADC[channel]raw' traces
            channel: Fast ADC channel
            intstart: trace index of integration start
            intstop: trace index of integration stop
            bkgstart: trace index of background start
            bkgstop: trace index of background stop
            period: Number of samples separation between two pulses. Needed
                if bunch pattern info is not available. If None, checks the 
                pulse pattern and determine the period assuming a resolution
                of 9.23 ns per sample which leads to 24 samples between
                two bunches @ 4.5 MHz. 
            npulses: number of pulses. If None, takes the maximum number of
                pulses according to the bunch patter (field 'npulses_sase3')
            dim: name of the xr dataset dimension along the peaks
            
    '''
    peaks = fastAdcPeaks(data, channel=channel, intstart=intstart, intstop=intstop,
                         bkgstart=bkgstart, bkgstop=bkgstop, period=period,
                         npulses=npulses)
    
    key = 'FastADC{}peaks'.format(channel) 
    if key in data:
        s = data.drop(key)
    else:
        s = data
    peaks = peaks.rename(key).rename({'peakId':dim})
    subset = xr.merge([s, peaks], join='inner')
    for k in data.attrs.keys():
        subset.attrs[k] = data.attrs[k]