#!/bin/bash
#
# hd_root_evio.sh - script for running hd_root on raw data files in pnfs
# author: alex.barnes at uconn.edu
# version: march 5, 2016
#
# usage: ./hd_root_evio.sh $1 $2 $3 $4 [ $5 ] [ $6 ]
#  where $1 = plugin name
#        $2 = number of threads
#        $3 = run number
#        $4 = run sub file
#        $5 = options
#        $6 = events to keep

source /home/halld/setup.sh
export ROOTSYS=/usr/local/root
export PATH=$PATH:$ROOTSYS/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOTSYS/lib
exe="hd_root"
plugins="-PPLUGINS=$1"
threads="-PNTHREADS=$2"
eviofile="hd_rawdata_$3_$4.evio"
eviosource="/pnfs4/phys.uconn.edu/data/Gluex/commissioning/rawdata/Run$3/hd_rawdata_$3_$4.evio"
output="hd_rawdata_$3_$4.root"
options="-PPLUGINS=TAGM_tracer -PEVIO:VERBOSE=0 $5"
events="-PEVENTS_TO_KEEP=$6"

echo Processing ready to begin on node `hostname` at `date`

if echo $eviosource | grep -q ^/pnfs4; then
   eviofile=$eviosource
elif dccp $eviosource $eviofile; then
   echo Successfully staged input file $eviosource on local disk
   ls -l
else
   echo Error - Unable to stage input file $eviosource on local disk, cannot continue!
   exit 1
fi

if [ ! -z "$6" ]; then
   command=( $exe $plugins $threads $events $options $eviofile)
else
   command=( $exe $plugins $threads $options $eviofile)
fi

${command[@]}

if [[ -r hd_root.root ]]; then
   mv hd_root.root $output
else
   echo Error - no output hd_root.root file found, processing failed!
   exit 2
fi
