#!/bin/bash
datadir="/home/jum/work/ilc/readout/data/mcmtests/smalltpc/20240311"
rootfile="chamber-fec01-debug-clk0-ped0-thr0-g0-s0-p1-1008-4"
title="0"
events=0
thr=30
echo "n=next event"
echo "p=previous event"
echo "<value>=plot event"
echo "q=quit"
while true; do 
    read -p "Event (n,p,<value>):" myinput
    echo $myinput
    re='^[0-9]+$'
    if [ "$myinput" = "n" ]; then
	events=$(($events + 1)) 
    elif [ "$myinput" = "p" ]; then
	if [ $events -gt 1 ]; then
	    events=$(($events - 1))
	fi
    elif [ "$myinput" = "q" ]; then
	exit
    else
	if ! [[ $myinput =~ $re ]] ; then
	    echo "error: Not a number" >&2
	else
	    events=$myinput
	fi
    fi
    ./plotevent.exe $thr $events $datadir $rootfile $title
    cp ${rootfile}-evt${events}-${title}.pdf plotevent.pdf
    cp ${rootfile}-ped-evt${events}-${title}.pdf plotped.pdf
    cp ${rootfile}-evt${events}-${title}.pdf ${datadir}/${rootfile}-thr${thr}-evt${events}-${title}.pdf
    cp ${rootfile}-ped-evt${events}-${title}.pdf ${datadir}/.
    rm ${rootfile}-evt${events}-${title}.pdf 
    rm ${rootfile}-ped-evt${events}-${title}.pdf
    echo $events
done
