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

# Command line
# /home/helios/BXYANG/operations/s35apps/cherenkov/setupCherenkovOneSector -sector S36a

# set appsDir  /home/helios/BXYANG/operations/s35apps/cherenkov/Develop
set appsDir  /home/helios/BXYANG/operations/s35apps/cherenkov
set inputDir /home/helios/BXYANG/operations/s35apps/inputFiles

# Substitute command line args
proc setStrArg {var default} {
  global argv
  set value $default
  set nPos  [lsearch $argv "-$var"]
  if { $nPos >= 0 } { set value [lindex $argv [expr $nPos + 1] ] }
  uplevel "set $var $value"
  return $value
}
proc setNumArg {var default} { uplevel "set $var [expr [setStrArg $var $default] ]" }

# define default parameters
# For delayTime <= 0.15, no integrated charge is seen!
setStrArg sector     		"S02"
setNumArg maxGate    		6554
setNumArg maxDelay   		6554
setNumArg gateTime0  		3000
setNumArg gateTime1  		3000
setNumArg delayTime0 		1
setNumArg delayTime1 		1
setNumArg integratorScale0    	51
setNumArg integratorScale1    	1024
setNumArg integratorOffset0   	-595
setNumArg integratorOffset1   	-460
setNumArg HVSCALE      		1250
setNumArg maxPMTHV      	1000
setNumArg pmtHV	  		900
setNumArg maxDisc            	5.0
setNumArg discriminator 	0.5
setNumArg clearRate		1.0
setNumArg CountFactor 		0.0018
setNumArg Int0Factor 		1.00
setNumArg Int1Factor 		1.00
# The width of the averager is the same as period of data logger (8-sec)
setNumArg IntWidth 		80
setNumArg CountWidth 		8

puts "Setting up Sector ${sector} Cerenkov detector board gated integrators."

# Integrator width time scaling
exec caput ${sector}-CBLM:CDET:Delay0C.DRVH $maxDelay
exec caput ${sector}-CBLM:CDET:Delay0M.EGUF $maxDelay
exec caput ${sector}-CBLM:CDET:Delay0C.EGUF $maxDelay
exec caput ${sector}-CBLM:CDET:Delay0M.HOPR $maxDelay
exec caput ${sector}-CBLM:CDET:Delay0C.HOPR $maxDelay
exec caput ${sector}-CBLM:CDET:Delay0M.HIHI $maxDelay
exec caput ${sector}-CBLM:CDET:Delay0C.HIHI $maxDelay
exec caput ${sector}-CBLM:CDET:Delay0M.HIGH $maxDelay
exec caput ${sector}-CBLM:CDET:Delay0C.HIGH $maxDelay
exec caput ${sector}-CBLM:CDET:Delay1C.DRVH $maxDelay
exec caput ${sector}-CBLM:CDET:Delay1M.EGUF $maxDelay
exec caput ${sector}-CBLM:CDET:Delay1C.EGUF $maxDelay
exec caput ${sector}-CBLM:CDET:Delay1M.HOPR $maxDelay
exec caput ${sector}-CBLM:CDET:Delay1C.HOPR $maxDelay
exec caput ${sector}-CBLM:CDET:Delay1M.HIHI $maxDelay
exec caput ${sector}-CBLM:CDET:Delay1C.HIHI $maxDelay
exec caput ${sector}-CBLM:CDET:Delay1M.HIGH $maxDelay
exec caput ${sector}-CBLM:CDET:Delay1C.HIGH $maxDelay

# Integrator gate time scaling
exec caput ${sector}-CBLM:CDET:Width0C.DRVH $maxGate
exec caput ${sector}-CBLM:CDET:Width0M.EGUF $maxGate
exec caput ${sector}-CBLM:CDET:Width0C.EGUF $maxGate
exec caput ${sector}-CBLM:CDET:Width0M.HOPR $maxGate
exec caput ${sector}-CBLM:CDET:Width0C.HOPR $maxGate
exec caput ${sector}-CBLM:CDET:Width0M.HIHI $maxGate
exec caput ${sector}-CBLM:CDET:Width0C.HIHI $maxGate
exec caput ${sector}-CBLM:CDET:Width0M.HIGH $maxGate
exec caput ${sector}-CBLM:CDET:Width0C.HIGH $maxGate
exec caput ${sector}-CBLM:CDET:Width1C.DRVH $maxGate
exec caput ${sector}-CBLM:CDET:Width1M.EGUF $maxGate
exec caput ${sector}-CBLM:CDET:Width1C.EGUF $maxGate
exec caput ${sector}-CBLM:CDET:Width1M.HOPR $maxGate
exec caput ${sector}-CBLM:CDET:Width1C.HOPR $maxGate
exec caput ${sector}-CBLM:CDET:Width1M.HIHI $maxGate
exec caput ${sector}-CBLM:CDET:Width1C.HIHI $maxGate
exec caput ${sector}-CBLM:CDET:Width1M.HIGH $maxGate
exec caput ${sector}-CBLM:CDET:Width1C.HIGH $maxGate

# Integrator gate time
exec caput ${sector}-CBLM:CDET:Width0C $gateTime0
exec caput ${sector}-CBLM:CDET:Width1C $gateTime0

# Integrator Delay time
exec caput ${sector}-CBLM:CDET:Delay0C $delayTime0
exec caput ${sector}-CBLM:CDET:Delay1C $delayTime1

# Integrator boxcar
exec caput ${sector}-CBLM:CDET:Int0WidthC $IntWidth
exec caput ${sector}-CBLM:CDET:Int1WidthC $IntWidth

# Set default scale for ADC
exec caput ${sector}-CBLM:CDET:Adc0M.EGUF $integratorScale0
exec caput ${sector}-CBLM:CDET:Adc1M.EGUF $integratorScale1

# Set ADC plot lmit
exec caput ${sector}-CBLM:CDET:Adc0M.LOPR 0
exec caput ${sector}-CBLM:CDET:Adc0M.HOPR $integratorScale0
exec caput ${sector}-CBLM:CDET:Adc1M.LOPR 0
exec caput ${sector}-CBLM:CDET:Adc1M.HOPR $integratorScale1

# Set default offset for ADC
exec caput ${sector}-CBLM:CDET:Adc0M.ROFF $integratorOffset0
exec caput ${sector}-CBLM:CDET:Adc1M.ROFF $integratorOffset1

puts "Setting up Sector ${sector} Cerenkov detector board high-voltage power supply."

# Set PMT-HV
exec caput ${sector}-CBLM:CDET:HighVoltageM.EGUF $HVSCALE
exec caput ${sector}-CBLM:CDET:HighVoltageC.EGUF $HVSCALE
exec caput ${sector}-CBLM:CDET:HighVoltageC.DRVH $maxPMTHV
exec caput ${sector}-CBLM:CDET:HighVoltageC.HOPR $maxPMTHV
exec caput ${sector}-CBLM:CDET:HighVoltageC   	$pmtHV

puts "Setting up Sector ${sector} Cerenkov detector board counter / discriminator."

# Set default discriminator scale / maximum for ADC
exec caput ${sector}-CBLM:CDET:DacC.DOL   $maxDisc
exec caput ${sector}-CBLM:CDET:DacC.DRVH  $maxDisc
exec caput ${sector}-CBLM:CDET:DacC.EGUF  $maxDisc
exec caput ${sector}-CBLM:CDET:DacC.HOPR  $maxDisc
exec caput ${sector}-CBLM:CDET:DacC.HIHI  $maxDisc
exec caput ${sector}-CBLM:CDET:DacC.HIGH  $maxDisc
exec caput ${sector}-CBLM:CDET:DacM.EGUF  $maxDisc
exec caput ${sector}-CBLM:CDET:DacM.HOPR  $maxDisc
exec caput ${sector}-CBLM:CDET:DacM.HIHI  $maxDisc
exec caput ${sector}-CBLM:CDET:DacM.HIGH  $maxDisc

# Set default discriminator level
exec caput ${sector}-CBLM:CDET:DacC $discriminator

# Set count time
exec caput ${sector}-CBLM:CDET:ClearRateC $clearRate

# Set count boxcar width
exec caput ${sector}-CBLM:CDET:CountWidthC $CountWidth

# Set counter calibration factor
exec caput ${sector}-CBLM:CDET:CountFactorC.PREC  4
exec caput ${sector}-CBLM:CDET:CountFactorC	  $CountFactor

# Set gated integrator calibration factor
exec caput ${sector}-CBLM:CDET:Int0FactorC	  $Int0Factor
exec caput ${sector}-CBLM:CDET:Int1FactorC	  $Int1Factor

# Set counter plot lmit
exec caput ${sector}-CBLM:CDET:CountLogM.LOPR	  -0.1
exec caput ${sector}-CBLM:CDET:CountLogM.HOPR	  5

puts "Setting up Sector ${sector} Cerenkov detector board: Done."
exit

# Local Variables:
# mode: tcl
# End:
