
#
# $Log: not supported by cvs2svn $
# Revision 1.2  1996/10/02 03:30:58  borland
# Uses only one waveform, does decile ranges and statistics on 0/180 data.
#
#

# Look at SCDU's using beam histories to see what 0/180 offsets are
# and what digitization noise is.

proc APSSRAllSCDUCheckout {args} {
    #These PVs no longer exist
    return
    set fileName ""
    set statusCallback "puts stderr"
    APSStrictParseArguments {fileName statusCallback}
    if [string length $fileName]==0 {
        return -code error "APSSRAllSCDUCheckout: fileName is blank"
    }
    if [file exists $fileName] {
        return -code error "APSSRAllSCDUCheckout: $fileName already exists"
    }
    set fileList ""
    foreach family {A:P1 A:P2 A:P3 A:P4 B:P5 B:P4 B:P3 B:P2 B:P1} {
        if [string length $statusCallback] {
            eval $statusCallback {"Working on $family family."}
        }
        set file $fileName-$family
        lappend fileList $file
        if [catch {APSSRSCDUCheckout -fileName $file -family $family \
                     -statusCallback $statusCallback} result] {
            return -code error "APSSRAllSCDUCheckout: $result"
        }
    }
    if [catch {eval exec sddscombine $fileList -merge -pipe=out \
                 | sddssort -pipe=in $fileName \
                 -column=xyDRangeSum,decr} result] {
        return -code error "APSSRAllSCDUCheckout: $result"
    }
    catch {eval exec rm $fileList}
}

proc APSSRSCDUCheckout {args} {
    #These PVs no longer exist
    return
    set fileName ""
    set family A:P1
    set sectorList ""
    set statusCallback "puts stderr"
    APSStrictParseArguments {fileName family sectorList statusCallback}
    
    set input0 /home/helios/oagData/sr/sddsmonitorFiles/beamHistories.wmon

    if [string length $fileName]==0 {
        return -code error "APSSRSCDUCheckout: no fileName supplied"
    }
    if [file exists $fileName] {
        return -code error "APSSRSCDUCheckout: $fileName exists already."
    }

    set input /tmp/[APSTmpString]
    if [catch {exec sddsprocess $input0 $input \
                 -match=column,BPMName=*${family} \
                 -define=parameter,BPMNames,n_rows,type=long} result] {
        return -code error "APSSRSCDUCheckout: sddsprocess error: $result"
    }
    if [catch {APSGetSDDSParameter -fileName $input -parameter BPMNames} result] {
        return -code error "APSSRSCDUCheckout: $result"
    }
    if !$result {
        return -code error "APSSRSCDUCheckout: No BPMs chosen (check family name)"
    }

    if [llength $sectorList] {
        set sectorFilter ""
        set index 0
        foreach sector $sectorList {
            if $index {
                lappend sectorFilter "Sector,$sector,$sector,|"
            } else {
                lappend sectorFilter "Sector,$sector,$sector"
            }
            incr index
        }
        set input0 $input
        set input /tmp/[APSTmpString]
        if [catch {exec sddsprocess $input0 $input \
                     -filter=column,[join $sectorFilter ,] \
                     -define=parameter,BPMNames1,n_rows,type=long} result] {
            return -code error "APSSRSCDUCheckout: $result"
        }
        catch {exec rm $input0}
        if [catch {APSGetSDDSParameter -fileName $input -parameter BPMNames1} result] {
            return -code error "APSSRSCDUCheckout: $result"
        }
        if !$result {
            return -code error "APSSRSCDUCheckout: No BPMs chosen (check sector list)"
        }
        set sectorListArg -list=[join $sectorList ,]
    } else {
        set sectorListArg -range=begin=1,end=40
    }

    set dataFile /tmp/[APSTmpString]

    if [string length $statusCallback] {
        eval $statusCallback {"Taking data..."}
    }

    # configure beam histories
    # read enable, single shot, disable resync, stop on full
    if [catch \
          {exec cavput -pend=15 -list=S $sectorListArg -list=$family \
             -list=:bh. \
             "-list=BRDE=Enable,BRDM=Single Shot,FIR0=Disable,FIM0=Stop on Full" \
         } result] {
        catch {close $monFID}
        return -code error "APSSRSCDUCheckout: $result"
    }

    # enable reading of beam history waveform
    after 1000
    if [catch \
          {exec cavput -pend=15 -list=S $sectorListArg -list=$family \
             -list=:bh:wfDisable_bo=Enable} result] {
        catch {close $monFID}
        return -code error "APSSRSCDUCheckout: $result"
    }

    for {set waveform -1} {$waveform<=1} {incr waveform} {
        # capture 2 waveforms to flush out fifo
        if [catch {exec cavput -pend=15 -list=S $sectorListArg \
                     -list=$family:bh.SSEN=1,$family:bh.FRST=1} result] {
            catch {close $monFID}
            return -code error "APSSRSCDUCheckout: $result"
        }
        after 500
    }
    if [catch {exec sddswmonitor $input $dataFile -steps=1 \
                 -scalars=/home/helios/oagData/sr/sddsmonitorFiles/DCCT.mon} result] {
        return -code error "APSSRSCDUCheckout: $result"
    }

    # put histories back in read disable and continuous fill
    if [catch \
          {exec cavput -pend=15 -list=S $sectorListArg -list=$family \
             -list=:bh. "-list=BRDE=Disable,FIM0=Continuous Fill" \
         } result] {
        return -code error "APSSRSCDUCheckout: $result"
    }

    if [string length $statusCallback] {
        eval $statusCallback {"Processing..."}
    }

    # split off 0 samples
    if [catch \
          {exec sddsprocess $dataFile -pipe=out -sparse=2,0  \
             | sddsconvert -pipe=in $dataFile.0 -edit=column,*:*\[xy\],ei/0/} result] {
        return -code error "APSSRSCDUCheckout: $result"
    }

    # split off 180 samples, merge in 0 samples, then process paired differences
    if [catch \
          {exec sddsprocess $dataFile -pipe=out -sparse=2,1  \
             | sddsconvert -pipe -edit=column,*:*\[xy\],ei/1/ \
             | sddsxref -pipe $dataFile.0 -take=*0 \
             | sddsprocess -pipe \
             "-define=column,%sDelta,%s1 %s0 -,select=*:*\[xy\]0,edit=ebd" \
             | sddsprocess -pipe \
             -process=*Delta,average,%sMean -process=*Delta,sigma,%sSigma \
             -process=*Delta,drange,%sDRange \
             -process=*0,sigma,%sSigma -process=*1,sigma,%sSigma \
             | tee $fileName.raw \
             | sddscollapse -pipe \
             | sddscollect -pipe \
             -collect=suffix=xDeltaMean -collect=suffix=xDeltaSigma \
             -collect=suffix=yDeltaMean -collect=suffix=yDeltaSigma \
             -collect=suffix=xDeltaDRange -collect=suffix=yDeltaDRange \
             -collect=suffix=x0Sigma -collect=suffix=y0Sigma \
             -collect=suffix=x1Sigma -collect=suffix=y1Sigma \
             | sddsprocess -pipe \
             "-define=column,xyDRangeSum,xDeltaDRange yDeltaDRange +" \
             "-define=column,xDeltaMeanNorm,xDeltaMean xDeltaSigma 1e-16 + / abs" \
             "-define=column,yDeltaMeanNorm,yDeltaMean yDeltaSigma 1e-16 + / abs" \
             | sddssort -pipe=in $fileName -column=xyDRangeSum,decr } result] {
        return -code error "APSSRSCDUCheckout: $result"
    }

    catch {exec rm $dataFile $dataFile.0}
}
