#!/bin/sh
# \
  exec oagwish "$0" "$@"

set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSStandardSetup

# $Log: FPGABpmTurnHistory,v $
# Revision 1.14 2012/07/27  16:33:30 shang
# added status message for converting data.
#
# Revision 1.13 2012/07/27  16:26:01 shang
# added converting turn history data into headless position data, so that it is much easier and faster for matlab to load
# bpm position history data.
#
# Revision 1.11  2012/05/08 20:33:55  shang
# added ControlRamMode and TriggerMode parameters to output file.
#
# Revision 1.10  2012/04/16 14:35:54  shang
# fixed a spelling error and modified to load RAM only for selected bpms instead of all.
#
# Revision 1.9  2011/10/06 18:59:43  shang
# added missing $ before {bpm}/
#
# Revision 1.8  2011/09/22 14:56:00  shang
# added FPGA filter to obtain the FPGA bpm list, since S37H (narrowband) also locates in accelerator system now.
#
# Revision 1.7  2011/08/25 22:31:29  shang
# corrected the check button variable names since SR sector buttons are used.
#
# Revision 1.6  2011/08/25 22:14:48  shang
# changed the FPGA bpm selection widget to SR sector buttons which looks better and condenser.
#
# Revision 1.5  2011/05/31 23:18:54  shang
# added mps dump trigger selection.S
#
# Revision 1.4  2011/05/27 15:22:50  shang
# added option of collecting short or long turn history data and increased the GUI size so that all buttons can be seen.
#
# Revision 1.3  2011/03/01 21:02:23  shang
# added entry for post-trigger so that use can change it as needed.
#
# Revision 1.2  2010/12/14 22:56:05  shang
# fixed the bug that file index did not increase automatically.
#
# Revision 1.1  2010/12/01 17:39:49  shang
# for collecting FPGA turn history data.
#

set CVSRevisionAuthor "\$Revision: 1.11 $ \$Author: shang $"

proc SetStatus {text} {
    global fpgaBPMConfigStatus
    set fpgaBPMConfigStatus "[clock format [clock seconds] -format %H:%M:%S]  $text"
    update
}

proc GetSelectedIOCList {args} {
    global iocList outputDir X Y TmpRoot status processData selectedIOCList sectorList itemList lab
    eval global $iocList
    
    set selectedIOCList ""

    
    foreach sector $sectorList {
        foreach item {P1 P2} {
            set name fpgaBPM${sector}$item
            global $name
            if [set $name] {
                set sector1 [string index $sector end]
                if [expr $sector1 % 2]==0 {
                    lappend selectedIOCList $sector
                } else {
                    lappend selectedIOCList [string range $sector 0 end-1][expr $sector1 - 1]
                }
            }
        }
    }
    set selectedIOCList [lsort -unique $selectedIOCList]
    return
}

proc SwitchInput {args} {
    global plane X Y
    if [catch {APSBoosterSwitchTuneInput -plane $plane -statusCallback SetStatus } result] {
        SetStatus "Error switch tune plane: $result"
        return
    }
}
set TmpRoot /tmp/[APSTmpString]
set oldSelectedIOCList ""
set fileIndex 0
set plotIndex 0
set first 1
proc CollectTurnHistory {args} {
    global iocList outputDir X Y TmpRoot status processData  selectedIOCList FPGABpmTurnHistory
    global showPlots oldSelectedIOCList fileIndex first configuration turns plotIndex plane
    eval global $iocList
    
    set plotIndex $fileIndex
    GetSelectedIOCList
    if ![llength $selectedIOCList] {
        SetStatus "Please select bpm/ioc first."
        return
    }
    set fileExist 0
    if ![file exists $outputDir/sectorFiles] {
        exec mkdir $outputDir/sectorFiles
    }
    set outDir $outputDir/sectorFiles
    foreach ioc $selectedIOCList  {
        set fileRoot $outDir/${ioc}-[format %03d $fileIndex]
        if [file exist $fileRoot.sdds] {
            set fileExist 1
            break
        }
    }
    if $fileExist {
        if ![APSYesNoPopUp "Some of fileIndex($fileIndex) files exist, overwrite them?"] {
            SetStatus "files of fileIndex($fileIndex) already exist, change the fileIndex before collecting data."
            return
        }
    }
    
    if {!$X && !$Y} {
        SetStatus "No plane selected for collecting."
        return
    }
    if {$X && $Y} {
        SetStatus "Can only collect one plane data at one time."
        return
    }
    if {0} {
        if [catch {SwitchInput} result] {
            SetStatus "Error switching tune plane: $result"
            return
        }
        if [catch {TurnOffTuneDrive} result] {
            SetStatus "Error turn off tune drive: $result"
            return
        }
    }
    SetStatus "Checking arm status..."
    if ![file exist $outputDir] {
        exec mkdir $outputDir
    }
  
    SetPostTrigger
    if [catch {APScavput -list=[join $selectedIOCList ,] \
                 -list=:turn:gtr:arm=0,:intervalAvg:AutoRestart=0 -pend=20 } result] {
        return -code error $result
    }
    
    after 1000
    
    if [catch {APScavput -list=[join $selectedIOCList ,] \
                 -list=:turn:gtr:arm=1 -pend=20 } result] {
        return -code error $result
    }

    after 1000
    set tmpRoot /tmp/[APSTmpString]
    #check if file exists
    
    foreach ioc $selectedIOCList  {
        set pvList ""
        set fileRoot $outDir/${ioc}-[format %03d $fileIndex]
        global ${ioc}bpmList
        foreach bpm [set ${ioc}(bpmList)] {
            global fpgaBPM${bpm}
            if [set fpgaBPM$bpm] {
                lappend pvList ${bpm}:turnHistory:${plane}position
                lappend pvList ${bpm}:turnHistory:${plane}sum
            }
        }
        if [catch {exec sddsmakedataset $tmpRoot.$ioc -col=ControlName,type=string \
                     -data=${ioc}:TurnHistoryTrigTimeSI,${ioc}:TurnHistoryTrigTurnLI } result] {
            return -code error "Error generating scalar pvs monitor file: $result"
        }
        if [file exist $fileRoot.sdds] {
            if ![APSYesNoPopUp "Warning< $fileRoot.sdds already exist, do you want to overwrite it?"] {
                return
            }
        }
        SetStatus "collecting data for $ioc ..."
        exec sddswmonitor -pvnames=[join $pvList ,] $fileRoot.sdds -steps=1 -scalars=$tmpRoot.$ioc -pendIOTime=50 -erase &
        lappend fileList $fileRoot.sdds
    }
    SetStatus "Data is collecting, please wait..."
    APSWaitWithUpdate -waitSeconds 5
    set done 0
    set tries 1
    while {!$done} {
        set done 1
        set missingFiles ""
        set emptyFiles ""
        foreach file $fileList {
            if ![file exist $file] {
                append missingFiles $file
                set done 0
            } else {
                if [catch {exec sdds2stream -rows=bar $file} rows] {
                    append missingFiles $file
                    set done 0
                } else {
                    if !$rows {
                        append emptyFiles $file
                        set done 0
                    }
                }
            }
        }
        if !$done {
	    incr tries
            if {$tries > 60} {
                if [string length $missingFiles] {
                    SetStatus "Unable to locate output files: $missingFiles"
                }
                if [string length $emptyFiles] {
                    SetStatus "empty output files: $emptyFiles"
                }
	        bell
                return
            }
            after 1000
            update
        }
    }
    incr fileIndex
    set lattice [file root [file readlink /home/helios/oagData/booster/orbitControllaw/lattices/default]]
    foreach file $fileList {
        if [catch {exec sddsprocess $file $file.1 \
                     "-redefine=col,Time,1.227e-3 Index *,units=ms" \
                     "-reprint=par,Lattice,$lattice" \
                     "-reprint=par,ControlRamFile,$FPGABpmTurnHistory(ShortFilename)" \
                     "-reprint=par,ControlRamDescription,[APSMakeSafeQualifierString $FPGABpmTurnHistory(Description)]" } result] {
            SetStatus "Error in define ControlRamMode for $file: $result"
         } else {
             exec mv $file.1 $file
             catch {exec rm ${file}~}
         }
    }
    if [catch {APScavput -list=[join $selectedIOCList ,] \
                 -list=:turn:gtr:arm=1,:intervalAvg:AutoRestart=1 -pend=20 } result] {
        return -code error $result
    }
    SetStatus " data collection done."
    SetStatus " post processing data..."
    foreach file $fileList {
        PostProcess -filename $file
    }
    
    SetStatus "done."
}

proc ProcessData {args} {
    global outputDir fileIndex
    set files [glob -nocomplain $outputDir/sectorFiles/B?C?-[format %03d $fileIndex].sdds]
    if ![llength $files] {
        SetStatus "No IOC files found for fileIndex - $fileIndex!"
        return
    }
    foreach file $files {
        SetStatus "Processing $file..."
        PostProcess -filename $file
    }
    SetStatus "done."
}

proc PostProcess {args} {
    set filename ""
    APSParseArguments {filename}
    #the bpm data in above directory of sector files
    set dir [file dir [file dir $filename]]
    set bpmList [exec sddsquery -col $filename | grep B*position]
    set file [file tail $filename]
    set index [scan [string range $file 5 7] %ld]
    
    foreach col $bpmList {
        set bpm [string range $col 0 5]
        if [regexp {X} $col] {
            set plane X
        } elseif [regexp {Y} $col] {
            set plane Y
        }
        set bpmFile $dir/${bpm}-${plane}-[format %03d ${index}].sdds
        if [catch {exec sddsconvert $filename -pipe=out -retain=col,${bpm}* \
                     | sddsprocess -pipe -filter=col,${bpm}:turnHistory:${plane}sum,100,1000 \
                     | sddsprocess -pipe=in -reprint=par,BPMName,$bpm -retain=col,*position \
                     -redefine=col,Index,i_row,type=long $bpmFile } result] {
            SetStatus "Waring in porcessing data for $bpm: $result"
        } else {
            #special bpm turn adjust
            if {$bpm=="B4C8P2" || $bpm=="B4C9P1" || $bpm=="B4C9P2"} {
                if [catch {exec sddsprocess $bpmFile \
                             "-redefine=col,Index,Index 1 -,type=long" -pipe=out \
                             | sddsprocess -pipe=in $bpmFile.1 -filter=col,Index,0,1e6 } result] {
                    SetStatus "Error process $bpm: $result"
                } 
                catch {exec mv $bpmFile.1 $bpmFile}
            }
        }
    }

}
set showPlots 0
proc ProcessTurnHistory {args} {
    global outputDir iocList status X Y selectedIOCList fileSelection showPlots fileIndex
    eval global $iocList
    GetSelectedIOCList
    if ![llength $selectedIOCList] {
        SetStatus "No bpms selected."
        return
    }
    set oldDir [pwd]
    cd $outputDir
    foreach ioc $selectedIOCList {
        set files [glob -nocomplain ${ioc}*.sdds]
        if ![llength $files] {
            SetStatus "No data found for $ioc."
            continue
        }
        
        SetStatus "Processing data for $ioc ..."
        foreach file $files {
            if [catch {ProcessBPMHistory -filename $file -showPlots 0} result] {
                SetStatus "Error in processing $file data: $result"
            }
        }
    }
    cd $oldDir
    SetStatus "done."
}

proc ProcessBPMHistory {args} {
    set filename ""
    set showPlots 0
    APSParseArguments {filename showPlots }
    
    set records 2048
    set fileRoot [file root $filename]
    
    if [catch {exec sddsprocess $filename  -filter=col,Index,141953,1e6  -pipe=out \
        | sddsbreak -rowLimits=$records -pipe \
        | sddsprocess -redefine=col,Index,i_row -pipe \
        | sddsfft -pipe=in $fileRoot.fft  \
        -col=Index,*position -windows -suppressAverage -padwithzeroes } result] {
        return -code error "Error processing $filename: $result"
    }
    if $showPlots {
        #PlotBPMHistory -filename $filename
    }
}

set plotFFT 0
proc PlotFFTData {args} {
    global outputDir iocList status X Y selectedIOCList fileSelection showPlots fileIndex plotIndex
    eval global $iocList
    GetSelectedIOCList
    set plot 0
    cd $outputDir
    foreach ioc $selectedIOCList {
        set files [glob -nocomplain ${ioc}-[format %03d $plotIndex].sdds]
        if ![llength $files] {
            SetStatus "No files found for $ioc"
            continue
	}
        foreach file $files {
            set fileRoot [file root $file]
            if ![file exist $fileRoot.fft] {
                 ProcessBPMHistory -filename $file
            }
            set columns [exec sddsquery -col $fileRoot.fft | grep FFT]
            foreach col $columns {
                exec  sddsplot -same -unsupp=y -topline=@${ioc}:TurnHistoryTrigTimeSI \
                  $fileRoot.fft -col=f,$col -grap=dot,vary  -split=page  -filter=col,f,0.1,0.3 \
                  -stagger=yIncrement=0.05  &
            }
        }
        set plot 1
    }
    if !$plot {
	SetStatus "No bpms selected for plotting."
	return
    }
}

proc PlotBPMHistory {args} {
    set filename ""
    APSParseArguments {bpm plane filename}
    global plotFFT
    
    set fileRoot [file root $filename]
    set ioc [lindex [split [file tail $fileRoot] -] 0]
    set cols [exec sddsquery -col $filename]
    
    if ![file exist $filename] {
        SetStatus "$filename does not exist!"
        return
    }
    if ![file exist $fileRoot.fft] {
        SetStatus "$fileRoot.fft does not exist, processing ..."
        ProcessBPMHistory -filename $filename 
    }
    exec sddsplot -sep -title=@${ioc}:TurnHistoryTrigTimeSI -topTitle \
      ${fileRoot}.sdds -col=Time,B* -ylabel=use=name,edit=%/:turnHistory//%/position//%/Small//   &

}

proc PlotTurnHistory {args} {
    global outputDir iocList status X Y selectedIOCList fileIndex plotIndex sectorList itemList fileIndex
    eval global $iocList
    
    set XplotOpt ""
    set YplotOpt ""
    GetSelectedIOCList
    cd $outputDir
    if ![llength $selectedIOCList] {
        SetStatus "No BPMs selected."
        return
    }
    cd $outputDir
    set fileList ""
    foreach sector $sectorList {
        foreach item {P1 P2} {
            set name fpgaBPM${sector}$item
            global $name
            if [set $name] {
                set bpm ${sector}$item
                set pattern ${bpm}*-[format %03d $plotIndex].sdds
                set files [glob -nocomplain $pattern]
                if [llength $files]==0 {
                    SetStatus "No files found for $bpm for index $fileIndex."
                } else {
                    lappend fileList $files
                }
            }
        }
    }
    if ![llength $fileList] {
        SetStatus "No files found for selected bpms!"
        return
    }
    eval exec sddsplot -grap=li,vary -sep -leg=edit=%/:turnHistory://%/position// -col=Index,*pos* $fileList &
}

proc TurnOffTuneDrive {args} {
    if [catch {exec cavput -pend=30 \
                 -list=BOOTUNE:XAXIS:MATRE200SetVoltage=0,BOOTUNE:YAXIS:MATRE200SetVoltage=0 } result] {
        return -code error "Error turning off booster tune drive: $result"
    }
}
set fpgaBPMConfigStatus ""

set tmpFile /tmp/[APSTmpString].config
if [catch {exec sddsprocess /home/helios/oagData/SCR/requestFiles/SBPMWaveform.config $tmpFile \
             -match=par,Location=Accel,BPMType=Booster,&  } result] {
    puts stderr "$result"
    exit 1
}

if [catch {sdds load $tmpFile fpgaData} result] {
    puts stderr "Unabled to read FPGA config file: $result"
    exit 1
}
set iocList $fpgaData(Parameter.IOCName)
set page 0
foreach name $iocList {
    set ${name}(bpmList) [lindex $fpgaData(Column.BPM) $page]
    set defaultList [lindex $fpgaData(Column.Default) $page]
        foreach bpm [set ${name}(bpmList)] default $defaultList {
            set ${name}($bpm) $default
            lappend ${name}(varList) ${name}($bpm)
        }
    incr page
}
set outputDir [APSGoToDailyDirectory -subdirectory boosterFPGA]


set X 1
set Y 0

APSApplication . -name "BoosterFPGABpmTurnHistory" \
  -version $CVSRevisionAuthor \
  -overview "Booster FPGA bpm turn history collection and reviewer."
APSScrolledStatus .status -parent .userFrame -textVariable fpgaBPMConfigStatus -width 110 \
    -height 8 -packOption "-fill both -expand true"



proc MakeCollectionWidget {args} {
    set parent ""
    APSParseArguments {parent}
    global iocList X Y showPlots fileIndex postTrigger   lab
    eval global $iocList
    APSFrame .f1 -parent $parent -packOption "-side top"
  
    set postTrigger 262144
    APSLabeledEntry .dir -parent $parent -label "Output directory:" -textVariable outputDir -width 70 \
        -contextHelp "directory for writing files to or processing, viewing files from." \
        -fileSelectDirectory 1 -fileSelectButton 1 
    APSFrameGrid .grid1 -parent $parent -xList {x1 x2}
    set w1 $parent.grid1.x1
    set w2 $parent.grid1.x2
    APSLabeledEntry .index -parent $w1 -label "File index:" -textVariable fileIndex -width 40 \
	-contextHelp "the filename will be named by file index, and it increases by 1 after each data collection"
    APSLabeledEntry .posttrig -parent $w1 -label "Post Trigger:" -textVariable postTrigger -width 40 \
      -contextHelp "provide the post trigger of FPGA bpms."
    bind $w1.posttrig.entry <Return> "SetPostTrigger"
    
    APSRadioButtonFrame .plot -parent $w2 -label "Show plots" -buttonList {Yes No} \
      -valueList {1 0} -variable showPlots -orientation horizontal \
      -contextHelp "choose whether to show plots when process turn history button is clicked."
   
   # APSLabeledEntry .trig -parent $w2 -label "Post trigger:" -textVariable postTrigger -width 40 \
#	-contextHelp "provide the post trigger of FPGA bpms."
  #  bind $w2.trig.entry <Return> "SetPostTrigger"
   
  #  APSCheckButtonFrame .plane -parent $w1 -label "Plane:" -buttonList {X Y} -variableList {X Y} \
        -allNone 1 -contextHelp "Select bpm plane." -orientation horizontal
    global plane
    set plane X
    APSRadioButtonFrame .plane -parent $w1 -label "Plane:" -buttonList {X Y} -variable plane -orientation horizontal \
      -valueList {X Y} -commandList {"set X 1; set Y 0;set plane X" "set X 0; set Y 1;set plane Y"}
    global plotFFT 
    
   # APSRadioButtonFrame .fftplot -parent $w2 -label "Plot FFT?" -buttonList {Yes No} -valueList {1 0} \
   #   -orientation horizontal -variable plotFFT

    APSLabeledEntry .plotIndex -parent $w2 -label "Plot File Index:" -textVariable plotIndex -width 40
    global sectorList itemList itemLabelList
   
    APSSRSectorButtons .bpmcorr -parent $parent.f1.frame -rootname fpgaBPM  \
      -label "FPGA bpms" -sectorList $sectorList  \
      -itemList $itemList \
      -itemLabelList $itemLabelList \
      -description "FPGA bpms" \
      -orientation horizontal -sectorControl 1 -packOption "-side top"   
    
    APSFrame .f -parent $parent -packOption "-side top"
    $parent.f.frame configure -bd 0
    APSFrame .b1 -parent $parent -packOption "-side top"
    $parent.b1.frame configure -bd 0
    set f0 $parent.f.frame
    set f1 $parent.b1.frame
    APSButton .col -parent $f0 -text "Collect turn History" -command "CollectTurnHistory"
    APSButton .proc1 -parent $f0 -text "Process Data" -command "ProcessData"
    APSButton .tune -parent $f1 -text "Switch Booster tune input" -command "SwitchInput"
    APSButton .tune1 -parent $f1 -text "Turn off tune drive" -command "TurnOffTuneDrive"
  #  APSButton .proc -parent $f1 -text "Process FFT" -command "ProcessTurnHistory"
    APSButton .view -parent $f1 -text "Plot turn History" -command "PlotTurnHistory"
  #  APSButton .plot1 -parent $f1 -text "Plot FFT Data" -command "PlotFFTData"
    APSButton .arm -parent $f0 -text "ReArm ALL" -command "ReArmAll" \
        -contextHelp "rearm bpms and enable averages."
    APSButton .disarm -parent $f0 -text "DisArm ALL" -command "DisArmAll" \
        -contextHelp "DisArm bpms and disable averages."
}



if [catch {sdds load /home/helios/oagData/SCR/preferredChoices/BBPMWaveform.sdds refData} result] {
    puts stderr $result
    exit 1
}
set referenceList [lindex $refData(Column.ChoiceName) 0]
set suffixList [lindex $refData(Column.Suffix) 0]
set SCRdir /home/helios/oagData/SCR/snapshots/BBPMWaveform

set turns 1
proc LoadRAM {args} {
    global SCRdir configuration postTrigger selectedIOCList FPGABpmTurnHistory turns
    GetSelectedIOCList
    if ![llength $selectedIOCList] {
	SetStatus "No bpms selected."
	return
    }
    set refFile $FPGABpmTurnHistory(ShortFilename)
    SetStatus "loading $FPGABpmTurnHistory(Description) ..."
    if [catch {APSSCRLogAction -file $refFile -action restore-start} result] {
        SetStatus "$result"
    }
    set logFile /tmp/[APSTmpString]
    set filename $SCRdir/$refFile
    set restoreError 0
    set option1 ""
    set opption2 ""
    foreach ioc $selectedIOCList {
	if ![string length $option1] {
	    set option1 -match=col,ControlName=${ioc}*
	    set option2 -match=par,WaveformPV=${ioc}*
	} else {
	    append option1 ,ControlName=${ioc}*,|
	    append option2 ,WaveformPV=${ioc}*,|
	}
    }
    if [catch {exec sddsprocess $filename $logFile.pv $option1 "-match=col,ControlName=*WF,!" 
	exec sddsprocess [file root $filename].waveform.gz $logFile.waveform $option2} result] {
	return -code error ":Error in sddsprocess: $result"
    }
    if [catch {exec sddscasr $logFile.pv -restore -pend=60
	exec sddswput $logFile.waveform -pend=60 } result] {
	return -code error "Error in restoring RAM: $result"
    }
    if [regexp "12 turns" $FPGABpmTurnHistory(Description)] {
        set turns 12
    } else {
        set turns 1
    }
    SetStatus "done."
    return
}

set FPGABpmTurnHistory(ShortFilename) ""
proc MakeControlWidget {args} {
    set parent ""
    APSParseArguments {parent}
    global FPGABpmTurnHistory
   
    APSAddSCRDialog .scr -parent $parent -system BBPMWaveform \
              -arrayName FPGABpmTurnHistory \
              -defaultFile ""
    APSButton .load -parent $parent -text "RESTORE" -command "LoadRAM" -contextHelp "load the selected SCR config into IOCs."
}



proc DisArmAll {args} {
    global iocList
    if [catch {exec cavput -list=[join $iocList ,] \
                 -list=:intervalAvg:AutoRestart=0,:turn:gtr:arm=0 -pend=30} result] {
        return -code error "ReArmAll: $result"    
    }
}

proc ReArmAll {args} {
    global iocList
    if [catch {exec cavput -list=[join $iocList ,] \
                 -list=:intervalAvg:AutoRestart=1,:turn:gtr:arm=1 -pend=30} result] {
        return -code error "ReArmAll: $result"    
    }
}


proc SetPostTrigger {args} {
    global postTrigger  selectedIOCList 
    GetSelectedIOCList
    if ![llength $selectedIOCList] {
	SetStatus "No bpms selected."
	return
    }
    if [catch {exec cavput -list=[join $selectedIOCList ,] -list=:turn:gtr:numberPTS=$postTrigger -pend=30} result] {
	return -code error "SetPostTrigger: $result" 
    }
}



set iocList [join [exec sddsprocess /home/helios/oagData/SCR/requestFiles/SBPMWaveform.config \
                     -pipe=out -match=par,Location=Accel -match=par,BPMType=Booster \
                     | sdds2stream -pipe=in -par=IOCName]]

set sectorList ""
foreach ioc $iocList {
    lappend sectorList $ioc
    set sector [string index $ioc end]
    lappend sectorList [string range $ioc 0 end-1][expr $sector +1]
}


set itemList {P1 P2}
set itemLabelList {P1 P2}

set env(EPICS_CA_MAX_ARRAY_BYTES)  "3000000"

set widgetList [APSTabFrame .tab -parent .userFrame -label ""  \
    -labelList {Acquisition Control} -width  1100 -height 550]

MakeCollectionWidget -parent [lindex $widgetList 0]

MakeControlWidget -parent [lindex $widgetList 1]


APSSetSRSectorButtons -mode all-off -rootname fpgaBPM -sectorList $sectorList \
  -sectorCount [llength $sectorList] -itemList $itemList

