#!/bin/bash
# 1 - chipnumber (as written on chip)
source=1
if [ $# == 0 ]; then
    chipnumber=0
elif [ $# == 1 ]; then
    chipnumber=$1
elif [ $# == 2 ]; then
    chipnumber=$1
    source=$2
else
    echo "Wrong number of arguments: " $#
    exit
fi
if [ $source == 0 ]; then
    source1=0
    source2=0
elif [ $source == 1 ]; then
    source1=1
    source2=0
elif [ $source == 2 ]; then
    source1=0
    source2=1
elif [ $source == 3 ]; then
    source1=1
    source2=1
else
    echo "Wrong source argument, must be 0-3 - Exiting"
    exit
fi
eventnb=1
#
# 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
#
# SRU and MCM general settings
#=============================
dcladdress=0
chipaddress=7
shaper=0
polarity=1
mode=0
threshold=(10 15 12 20)
post=$((7 << 14))
pre=$((3 << 17))
above=$((2 << 12))
events=500
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
#================================
# Init  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
#
# Set pretriggers=0, buffers=4
./saltrowrrdreg --fec $dcladdress --chip $chipaddress --register C --channel 0 --write 0 --verify
# Loop over gains
#================
declare -i clusters_min=events*12-10
declare -i clusters_max=events*12+10
if [ $source1 == 0 ]; then
    $vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER1"
fi
if [ $source2 == 0 ]; then
    $vxipath/vxi11_send_verify $IP "SOURCE2:FUNCTION USER1"
fi
for gain in {0..3}
do
    if [ ${gain} == 0 ]; then
	if [ $source1 == 1 ]; then
	    $vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER2"
	fi
	if [ $source2 == 1 ]; then
	    $vxipath/vxi11_send_verify $IP "SOURCE2:FUNCTION USER2"
	fi
	#
	# Create limit files
	cat <<EOF1 >plotchipzsuppeaks.txt
# peaktypes type min_mean max_mean max_rms
0.0 0.1 0.1
500.0 600.0 5.0
470.0 570.0 5.0
# cluster length min_mean max_mean max_rms min_clusters max_clusters
10.0 40.0 10.0 $clusters_min $clusters_max
EOF1
    elif [ ${gain} == 1 ]; then
	if [ $source1 == 1 ]; then
	    $vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER3"
	fi
	if [ $source2 == 1 ]; then
	    $vxipath/vxi11_send_verify $IP "SOURCE2:FUNCTION USER3"
	fi
	cat <<EOF1 >plotchipzsuppeaks.txt
# peaktypes type min_mean max_mean max_rms
0.0 0.1 0.1
500.0 600.0 5.0
470.0 570.0 5.0
# cluster length min_mean max_mean max_rms min_clusters max_clusters
10.0 40.0 10.0 $clusters_min $clusters_max
EOF1
    elif [ ${gain} == 2 ]; then
	if [ $source1 == 1 ]; then
	    $vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER2"
	fi
	if [ $source2 == 1 ]; then
	    $vxipath/vxi11_send_verify $IP "SOURCE2:FUNCTION USER2"
	fi
	cat <<EOF1 >plotchipzsuppeaks.txt
# peaktypes type min_mean max_mean max_rms
0.0 0.1 0.1
650.0 750.0 5.0
620.0 720.0 5.0
# cluster length min_mean max_mean max_rms min_clusters max_clusters
10.0 40.0 10.0 $clusters_min $clusters_max
EOF1
    elif [ ${gain} == 3 ]; then
	if [ $source1 == 1 ]; then
	    $vxipath/vxi11_send_verify $IP "SOURCE1:FUNCTION USER3"
	fi
	if [ $source2 == 1 ]; then
	    $vxipath/vxi11_send_verify $IP "SOURCE2:FUNCTION USER3"
	fi
	cat <<EOF1 >plotchipzsuppeaks.txt
# peaktypes type min_mean max_mean max_rms
0.0 0.1 0.1
720.0 820.0 8.0
690.0 790.0 8.0
# cluster length min_mean max_mean max_rms min_clusters max_clusters
10.0 40.0 10.0 $clusters_min $clusters_max
EOF1
    else
	echo "ERROR: no such gain - exiting" >&2
	exit
    fi
# Read pedestalvalues
    pedfile=`printf '%s/chip%d/saltro%d-mcm%d-c%d-g%d-s%d-p%d.ped' $datadir $chipnumber $chipnumber $dcladdress $chipaddress $gain $shaper $polarity`
    if [ ! -f "$pedfile" ]; then
	echo "ERROR: $pedfile does not exist - exiting" >&2
	exit
    fi
    lines=`cut -f 2 $pedfile`
    channel=0
# value is hex
    for line in $lines; do
	echo ${line}
	value=`printf '%x' $line`
	./saltrowrrdreg --fec $dcladdress --chip $chipaddress --register 13 --channel $channel --write $value --verify
	((channel++))
    done
    for ch in {0..15}; do
	./saltrowrrdreg --bcast --fec $dcladdress --chip $chipaddress --register 8 --channel $ch --write ${threshold[${gain}]} --verify
	echo "TH $ch ${threshold[${gain}]}"
    done

    datfile=`printf '%s/chip%d/saltro%d-zerosup-mcm%d-c%d-g%d-s%d-p%d-s%d.dat' $datadir $chipnumber $chipnumber $dcladdress $chipaddress $gain $shaper $polarity $source`
    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
    if [ ! -f "$datfile" ]; then
	echo "ERROR: $datfile does not exist - exiting" >&2
	exit
    fi
    
    ./sruunpack --data ${datfile} --root --dump > slask${gain}.out
    rootfile=`printf 'chip%d/saltro%d-zerosup-mcm%d-c%d-g%d-s%d-p%d-s%d' $chipnumber $chipnumber $dcladdress $chipaddress $gain $shaper $polarity $source`
    if [ ! -f "${datadir}/${rootfile}.root" ]; then
	echo "ERROR: $rootfile does not exist - exiting" >&2
	exit
    fi
    title=`printf 'g%d-s%d-p%d-s%d'  $gain $shaper $polarity $source`
    echo "FILE: ${datadir}/${rootfile}.root"
    ./plotchipzsuppeaks.exe $eventnb $chipnumber $dcladdress $chipaddress ${datadir} ${rootfile} ${title}
    value=$?
    if [ $value -ne 0 ] 
    then
	echo "ERROR: ./plotchipzsuppeaks.exe returned ${value} - exiting" >&2
	exit
    fi
    csvfile=${mydata}/${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
    else
	echo "TESTRUN - manage csvfile by hand"
    fi
    
done
echo $clusters_min $clusters_max
exit
