#!/bin/bash
#
# General settings
#===================
mydata=/home/jum/wdir/ilcdaq/data/
chipnumber=0 # set to number written on chip - replace by argument
gain=0
shaper=0
polarity=1
#
# VXI settings
#=============
vxipath=$HOME/ilc/ilcdaq/chiptests/vxi11/bin
IP="192.168.4.10"
#
# select channel of pulsegenerator
#=================================
#USER1 : DC level
#USER2 : pulse for 12 and 16mV/fc = gain0 and gain2
#USER3 : 60% (12/20) of USER2 suitable 20 and 27mV/fC = gain1 and gain3
#USER4 : reverse polarity suitable for 12 and 16 mV/fc = gain0 and gain2
if [ ${gain} == 0 ]; then
    $vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER2"
elif [ ${gain} == 1 ]; then
    $vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER3"
elif [ ${gain} == 2 ]; then
    $vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER2"
elif [ ${gain} == 3 ]; then
    $vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER3"
else
    echo "no such gain - exiting"
    exit
fi
$vxipath/vxi11_send_verify $IP "TRIGGER:SEQUENCE:SOURCE EXTERNAL" # external trigger
#
# SRU and MCM general settings
#=============================
chipnumber=0
dcladdress=0
chipaddress=7
mode=0
post=$((0 << 14))
pre=$((0 << 17))
above=$((0 << 12))
events=1000
samples=`printf '%x' 1000`
channels=0
for register in {10..17}
do
    ./mcmwrreg --fec $dcladddress --register $register --write --value FFFF
done
mcmregister=`printf '%x' $((0x10 + $chipaddress))`
./saltrowrrdreg --fec $dcladdress --chip $chipaddress --register A --channel 0 --write $samples --verify
./mcmwrreg --fec $dcladdress --register $mcmregister --write --value $channels
#
# set readout mode of SALTRO chip
#================================
# Load 0 into VFPED as pedestal value - to all channels and chip
./saltrowrrdreg --bcast --fec $dcladdress  --chip $chipaddress --register 13 --channel 0 --write 0 --verify
# Load 0 threshold to ZSTRH
./saltrowrrdreg --bcast --fec $dcladdress --chip $chipaddress --register 8 --channel 0 --write 0 --verify
# Load DPCFG with mode = f(t) - fpd = 10,  postsamples (0-7), presamples (0-3), above threshold (0-3)
# To set back to normal mode mode=0.
value=`printf '%x' $((mode + post +pre + above))`
./saltrowrrdreg --bcast --fec $dcladdress --chip $chipaddress --register B --channel 0 --write $value --verify
#
# DATA taking
#============
shaper=1
datfile=`printf 'chip%d/readout-pulser-c%04d-g%1d-s%1d-p%1d.dat' $chipnumber $chipnumber $gain $shaper $polarity`
echo "FILE= " $datfile
./mcmpca16 --fec $dcladdress --preamp 0 --gain $gain --shaper $shaper --shutdown 0 --polarity $polarity --write --verify
time ./srureadout --fec $dcladdress  --rate 0 --data ${mydata}${datfile} --event $events --softtrigger --sruif enp3s0
./sruunpack --data ${mydata}${datfile} --root > slask.out
rootfile=`printf 'chip%d/readout-pulser-c%04d-g%1d-s%1d-p%1d.root' $chipnumber $chipnumber $gain $shaper $polarity`
title=`printf 'c%04d-g%1d-s%1d-p%1d.root' $chipnumber $gain $shaper $polarity`
./protomcmpulsespedsub.exe $chipnumber $dcladdress $chipaddress ${mydata} ${rootfile} $title
#
shaper=3
$vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER2"
datfile=`printf 'chip%d/readout-pulser-c%04d-g%1d-s%1d-p%1d.dat' $chipnumber $chipnumber $gain $shaper $polarity`
echo "FILE= " $datfile
./mcmpca16 --fec $dcladdress --preamp 0 --gain $gain --shaper $shaper --shutdown 0 --polarity $polarity --write --verify
time ./srureadout --fec $dcladdress  --rate 0 --data ${mydata}${datfile} --event $events --softtrigger --sruif enp3s0
./sruunpack --data ${mydata}${datfile} --root > slask.out
rootfile=`printf 'chip%d/readout-pulser-c%04d-g%1d-s%1d-p%1d.root' $chipnumber $chipnumber $gain $shaper $polarity`
title=`printf 'c%04d-g%1d-s%1d-p%1d.root' $chipnumber $gain $shaper $polarity`
./protomcmpulsespedsub.exe $chipnumber $dcladdress $chipaddress ${mydata} ${rootfile} $title
#
shaper=7
datfile=`printf 'chip%d/readout-pulser-c%04d-g%1d-s%1d-p%1d.dat' $chipnumber $chipnumber $gain $shaper $polarity`
echo "FILE= " $datfile
./mcmpca16 --fec $dcladdress --preamp 0 --gain $gain --shaper $shaper --shutdown 0 --polarity $polarity --write --verify
time ./srureadout --fec $dcladdress  --rate 0 --data ${mydata}${datfile} --event $events --softtrigger --sruif enp3s0
./sruunpack --data ${mydata}${datfile} --root > slask.out
rootfile=`printf 'chip%d/readout-pulser-c%04d-g%1d-s%1d-p%1d.root' $chipnumber $chipnumber $gain $shaper $polarity`
title=`printf 'c%04d-g%1d-s%1d-p%1d.root' $chipnumber $gain $shaper $polarity`
./protomcmpulsespedsub.exe $chipnumber $dcladdress $chipaddress ${mydata} ${rootfile} $title

exit
