#! /bin/tcsh -f
#
# Scan Chi2 motor and do a curve fit to find the maximum Iend
# or: steer beam from side to side to put it back through the pinhole
#
#
set chi2 = ""
set Iend = ""
if(! $?plot) set plot = ""
if(! $?beamline) then
# default to beamline this computer belongs to
setenv beamline `beamline.com`
if($status) then
echo "ERROR: $beamline"
exit 9
endif
endif
#if(! $?beamline) setenv beamline 831
# lower limit before beam is considered "off" (nA)
set min_Izero = 0.001
set min_Iend = 50
set min_Iend = 20
# user-specified starting point?
set manual = `echo "$1" | awk '$1+0>0.2 && $1+0<1.5{print $1+0}'`
if("$1" == "debug") set debug
if("$1" == "slow") set SLOW
# got multilayers?
if( `blmotor.com Mono Type |& awk '{print $NF+0}' | tail -1` ) then
# multilayers are engaged
set deltas = ( -0.015 -0.01 -0.005 -0.002 -0.001 0 0.001 0.002 0.005 0.01 0.015 )
else
set deltas = ( -0.005 -0.002 -0.001 -0.0006 -0.0005 -0.0004 -0.0003 -0.0002 -0.0001 0 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.001 0.002 0.005 )
set deltas = ( -0.001 -0.0006 -0.0005 -0.0004 -0.0003 -0.0002 -0.0001 0 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.001 )
endif
# find a writable temporary file name
if(! $?home) set home
foreach tempfile ( ./tune_chi2_tmp ~/tune_chi2_tmp /tmp/tune_chi2_tmp$$ )
touch ${tempfile} >& /dev/null
if(! $status) break
end
rm -f ${tempfile}
# decide how to print output
test -t 1
if($status) set AUTO
if($?CGI) set AUTO
diode.com in
shutter.com open
if("$manual" != "") then
# use a user-specified value as the "center" of the scan
set Chi2 = "$manual"
unset use_local
blmotor.com Chi2 $Chi2 > /dev/null
else
set use_local
endif
try_again:
##########################################################################################################
autoscale.com
if($status) then
set BAD = "no beam"
goto finish
endif
set Chi2 = `blmotor.com Chi2 | awk '! /unknown/{print $NF+0}'`
set Iend = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
set Izero = `blmotor.com Izero | awk '! /unknown/{print $NF+0}'`
set lastgood = `tac /data/log/change.log | awk '/Chi2/ && $NF>3000{print $(NF-2)}' | head -1`
set origChi2 = $Chi2
if("$Iend" == "") goto try_again
echo "Chi2: $Chi2 Iend = $Iend"
echo ""
# make sure beam goes into the hutch!
set weak = `echo $Izero $min_Izero | awk '{print ($1<$2)}'`
if(("$weak")&&(! $?force_Izero)) then
echo "Flux into hutch is insignificant... "
echo ""
echo "Please push the PHOTONS ON button on the hutch."
if($?AUTO) then
echo "and run this again."
exit
endif
echo 'press "Enter" when you have done this:'
set nutn = ( $< )
set force_Izero
goto try_again
endif
# make sure the PIN diode is in the beam!
set weak = `echo $Iend $min_Iend | awk '{print ($1<$2)}'`
if(($weak) && (! $?force_Iend)) then
# check if Chi2 is just WAY off
if ( `echo $Chi2 $lastgood | awk '{print (sqrt(($1-$2)^2) > 0.01)}'` ) then
echo "resetting Chi2 to $lastgood"
set Chi2 = $lastgood
blmotor.com Chi2 $Chi2
set Iend = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
set weak = `echo $Iend $min_Iend | awk '{print ($1<$2)}'`
endif
endif
if(("$weak")&&(! $?force_Iend)) then
echo "Flux into diode is insignificant... "
echo ""
echo "Please insert the PIN diode and open all shutters"
if($?AUTO) then
echo "and run this again."
exit
endif
echo 'press "Enter" when you have done this:'
set nutn = ( $< )
set force_Iend
goto try_again
endif
# make sure no autoscales occur during run
set test = `echo "autooff\r" | sock_exchange.tcl beamline 10001 1 | grep Local`
if("$test" != "") then
echo "unable to control beamline."
echo 'please switch the beamline control computer (bl'${beamline}'b) to "Remote Control"'
exit 9
endif
# register this with the change log
change.com "Chi2 $Chi2 Iend: $Iend"
set origChi2 = $Chi2
if($?use_local) then
echo -n "doing Ed's tuneup"
autoscale.com on > /dev/null
if($status) then
set BAD = "no beam"
goto finish
endif
blmotor.com Tune Chi2 0
autoscale.com > /dev/null
if($status) then
set BAD = "no beam"
goto finish
endif
echo ""
sleep 1
set Chi2 = `blmotor.com Chi2 | awk '! /unknown/{print $NF+0}'`
set Iend = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
echo "Chi2: $Chi2 Iend = $Iend"
echo ""
endif
if($?HURRY) goto finish
echo -n "" >! ${tempfile}.plot
# back up first
#set goal = `echo "$origChi2 -0.005" | awk '{print $1+$2}'`
#blmotor.com Chi2 $goal > /dev/null
#set goal = `echo "$origChi2 -0.0007" | awk '{print $1+$2}'`
#blmotor.com Chi2 $goal > /dev/null
#foreach delta ( -0.001 -0.0006 -0.0005 -0.0004 -0.0003 -0.0002 -0.0001 0 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.001 )
foreach delta ( $deltas )
set goal = `echo "$Chi2 $delta" | awk '{print $1+$2}'`
echo -n "$goal " | tee -a ${tempfile}.plot
blmotor.com Chi2 $goal > /dev/null
sleep 1
if($?SLOW) sleep 2
set result = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
echo "$result" | tee -a ${tempfile}.plot
if("$result" == "") goto try_again
end
# go back to original position
blmotor.com Chi2 $origChi2 > /dev/null
# reasonable starting value for fit
set Iend = `sort -n +1 ${tempfile}.plot | awk '{print $2}' | tail -1`
set x0 = `sort -n +1 ${tempfile}.plot | awk '{print $1}' | tail -1`
set w = `awk -v Chi2=$Chi2 -v Iend=$Iend '{++n; dT += ($1-Chi2)^2; dI += ($2-Iend)^2} END{if(n) print 1/(sqrt(dT/n) * sqrt(dI/n))}' ${tempfile}.plot`
cat ${tempfile}.plot |\
awk '{print $0, 1}' |\
cat >! ${tempfile}.splot
cat << EOF | gnuplot -persist >&! ${tempfile}.log
f(x) = w*(x-x0)**2 + c
w=-$w;x0=$Chi2;c=$Iend;
fit f(x) '${tempfile}.plot' using 1:2:(1) via w
fit f(x) '${tempfile}.plot' using 1:2:(1) via x0
fit f(x) '${tempfile}.plot' using 1:2:(1) via c
fit f(x) '${tempfile}.plot' using 1:2:(1) via x0
fit f(x) '${tempfile}.plot' using 1:2:(1) via w
fit f(x) '${tempfile}.plot' using 1:2:(1) via x0
fit f(x) '${tempfile}.plot' using 1:2:(1) via w,x0,c
set title "Flux vs horizontal beam position"
set timestamp
set nokey
${plot}plot '${tempfile}.plot' using 1:2, f(x)
#set terminal png small color
#set output "flux_vs_chi2.png"
#plot '${tempfile}.plot' using 1:2, f(x)
EOF
if($?CGI) echo '
'
date
if($?CGI) echo '
'
rm -f fit.log >& /dev/null
set newChi2 = `awk '/^x0/ && $4=="+/-"{print $3}' ${tempfile}.log | tail -1`
echo "new Chi2 = $newChi2"
set toofar = `echo $newChi2 $Chi2 $deltas | awk '{print (sqrt(($1-$2)^2) > $NF*0.75 )}'`
set curvature = `awk '/^w / && $4=="+/-"{print $3/sqrt($3*$3)}' ${tempfile}.log | tail -1`
set quality = `awk '/^final sum of squares of residuals/{printf "%50d\n", $NF/1e6}' ${tempfile}.log | tail -1`
echo "curvature=$curvature quality=$quality"
#if(("$delta" != "")&&($curvature == -1)&&($quality < 10000000)) then
#if(( ! $toofar )&&($curvature == -1)) then
if(( ! $toofar )&&($curvature == -1)&&($quality < 10)) then
set Chi2 = $newChi2
else
# just pick highest value seen
set Chi2 = `sort -n +1 ${tempfile}.plot | awk '{print $1}' | tail -1`
set BAD_FIT
endif
# move to beginning of scan first (backlash?)
set goal = `echo "$origChi2 -0.005" | awk '{print $1+$2}'`
blmotor.com Chi2 $goal > /dev/null
#set goal = `echo "$origChi2 -0.0007" | awk '{print $1+$2}'`
#blmotor.com Chi2 $goal > /dev/null
# go to new position
if($?debug) set Chi2 = $origChi2
blmotor.com Chi2 $Chi2 > /dev/null
usleep 500000
set Iend = `blmotor.com Iend | awk '! /unknown/{print $NF+0}'`
if("$Iend" == "") set BAD_FIT
echo ""
echo "new Chi2: $Chi2 Iend = $Iend"
if(! $?trials) set trials = 1
if(($?BAD_FIT)&&($trials < 5)) then
unset BAD_FIT
@ trials = ( $trials + 1 )
echo "lets try that again..."
goto try_again
endif
if($?BAD_FIT) then
set BAD = "bad fit"
endif
finish:
# did something bad happen?
if($?BAD) then
echo "putting Chi2 back to $origChi2"
blmotor.com Chi2 $origChi2
echo "ERROR: $BAD"
exit 9
endif
# register this with the change log
change.com "Chi2: $Chi2 Iend: $Iend"
# clean up
if(! $?debug) rm -f ${tempfile}* >& /dev/null