#!/bin/bash
#datadir="/home/jum/wdir/ilcdaq/data/smalltpc/20250313zsup"
datadir="/home/jum/wdir/ilcdaq/data/smalltpc/"
rootfile="chamber-fec01-zsup-clk0-ped1-thr4-g0-s0-p1-600-8"
#datadir="/home/jum/wdir/ilcdaq/data/smalltpc/20251111"
#rootfile="chamber-fec01-debug-clk0-ped0-thr0-g0-s3-p1-1008-2"
#rootfile="chamber-fec01-debug-clk0-ped0-thr0-g0-s0-p1-1008-3"
#rootfile="chamber-fec01-debug-clk0-ped0-thr0-g0-s0-p1-1008-6"
#rootfile="chamber-fec01-zsup-clk0-ped1-thr5-g0-s0-p1-1008-3"
#rootfile=chamber-fec01-swtrig-clk0-ped0-thr0-g0-s3-p1-1008-4"
#rootfile=chamber-fec01-debug-clk0-ped0-thr0-g0-s0-p1-1008-2"
#rootfile="chamber-fec01-debug-clk0-ped0-thr0-g0-s0-p1-1008-4"
#rootfile="chamber-fec01-debug-clk0-ped0-thr0-g0-s0-p1-1008-1"
title="mcmx"
# 0 = allevents
events=0
# software threshold (set to 0 if zero suppression)
#thr=20
thr=0
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-mcmx.exe $thr $events $datadir $rootfile $title
#    ./plotevent-dev.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}-evt${events}-${title}.pdf
    rm ${rootfile}-evt${events}-${title}.pdf
    rm ${rootfile}-ped-evt${events}-${title}.pdf
    cp timesweeps.pdf ${datadir}/${rootfile}-timesweeps-evt${events}-${title}.pdf
    echo $events
done
