#!/bin/bash
gain=0
if [ $# == 0 ]; then
    chipnumber=0
    gain=0
elif [ $# == 1 ]; then
    chipnumber=$1
    gain=0
elif [ $# == 2 ]; then
    chipnumber=$1
    gain=$2
else 
    echo "Wrong number of arguments: " $#
    exit
fi
eventnb=3
#
# General settings
#===================
datadir=/home/jum/wdir/ilcdaq/data
#
# 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
$vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER1"
$vxipath/vxi11_send_verify $IP "SOURCE2:FUNCTION USER1"
#
# SRU and MCM general settings
#=============================
dcladdress=0
chipaddress=7
polarity=1
mode=0
post=$((0 << 14))
pre=$((0 << 17))
above=$((0 << 12))
events=500
samples=`printf '%x' 1008`
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
#================================
# Init  0 into VFPED as pedestal value, Load 0 threshold to ZSTRH
for ch in {0..15}; do
    ./saltrowrrdreg --fec $dcladdress --chip $chipaddress --register 13 --channel $ch --write 0 --verify
    ./saltrowrrdreg --fec $dcladdress --chip $chipaddress --register 8 --channel $ch --write 0 --verify
done
# 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
#
# make limitfile
        cat <<EOF1 >plotchipped.txt
# one line per shaper:  ped_min_mean ped_max_mean ped_max_rms
50.0 120.0 2.0
30.0 100.0 4.0
40.0 110.0 3.0
10.0 80.0 5.0
EOF1
#
# Loop over shapers
#==================
for shaper in 0 1 3 7
do
    datfile=`printf '%s/chip%d/saltro%d-pedestal-mcm%d-c%d-g%1d-s%1d-p%1d-sh.dat' $datadir $chipnumber $chipnumber $dcladdress $chipaddress  $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 ${datfile} --event $events --softtrigger --sruif enp3s0
    if [ ! -f "$datfile" ]; then
        echo "ERROR: $datfile does not exist - exiting" >&2
        exit
    fi
    ./sruunpack --data ${datfile} --root > ped${gain}.out
    rootfile=`printf 'chip%d/saltro%d-pedestal-mcm%d-c%d-g%1d-s%1d-p%1d-sh' $chipnumber $chipnumber $dcladdress $chipaddress  $gain $shaper $polarity`
    if [ ! -f "${datadir}/${rootfile}.root" ]; then
        echo "ERROR: $rootfile does not exist - exiting" >&2
        exit
    fi
    title=`printf 'g%d-s%d-p%d-sh'  $gain $shaper $polarity`
    ./plotchipped.exe $eventnb $chipnumber $dcladdress $chipaddress $gain ${datadir} ${rootfile} $title
    value=$?
    if [ $value -ne 0 ] 
    then
        echo "ERROR: plotchipped.exe returned ${value}" >&2
    fi

    csvfile=${datadir}/${rootfile}.csv
    if [ $chipnumber != 0 ]; then
        if [ -f "${csvfile}" ]; then
            mydate=`date +%Y%m%d%H%M%S`
            echo "$csvfile exist" $mydate
            destfile=${mydata}/${rootfile}-${mydate}.csv
            mv $csvfile $destfile
        fi
    fi

done

if [ $chipnumber == 0 ]; then
    echo "TESTRUN - manage csvfile by hand"
fi

exit
