Skip to content
Snippets Groups Projects
xgm.py 49.5 KiB
Newer Older
            plt.axvline(intstart+i*period, ls='--', color='g')
            plt.axvline(intstop+i*period, ls='--', color='r')
            plt.axvline(bkgstart+i*period, ls='--', color='lightgrey')
            plt.axvline(bkgstop+i*period, ls='--', color='grey')
        plt.title(f'Fast ADC {channel} trace')
        plt.xlim(bkgstart-10, intstop + 50)
    return fAdcPeaks

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 ADC samples 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 = 24 samples between two pulses @ 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]