#!/bin/tcsh -f

# This script is to be run by farm jobs. Suitable job
# scripts can be made using the mk_job_scripts.pl script.
# The intent here is to run a job at a single, discrete
# point in phase space.
#
# This script requires 1 explicit arguments from the command
# line and will use 1 implicit value from the environment.
# The command line arguments are shown in this example:
#
# run_one.csh ptype p theta jobname top_dir
#
# ptype    The particle type. This should be one of "piplus",
#          "piminus", or "proton".
#
# p        Total momentum.
#
# theta    Polar angle theta
#
# jobname  Name of job. Used as name of output directory 
#          relative to top_dir
#
# top_dir  This is the full path to the top directory on the
#          work disk where the simulation study is based. A
#          sub-directory will be created here to hold the
#          output files. It is also assumed that the script
#          "setenv.csh" exists in top_dir for setting up the
#          correct Hall-D environment. Also, the directory
#          $top_dir/farm should exist and contain the necessary
#          support files for the simulation.
#
# In addition, the JOB_ID environment variable
# is assumed to be set. If it is not, then a default of
# "1234" is used.
#


# Get command line arguments
if ( $#argv >= 4 ) then
	set nthreads=$argv[1];
	set ptype=$argv[2];
	set p=$argv[3];
	set theta=$argv[4];
	set jobname=$argv[5];
	set basename=$argv[6];
	set top_dir=$argv[7];
else
	echo "Too few arguments ("$#argv"). Should be 4!"
	exit
endif

# If running interactively from the command-line then
# JOB_ID won't be set. Check for this and set it if needed.
if ( ! $?JOB_ID ) then 
	setenv JOB_ID 1234
	mkdir -p tmp${JOB_ID}  # use subdirectory if running from command line
	cd tmp${JOB_ID}
endif
setenv JOB_ID `basename $JOB_ID .farmpbs.jlab.org`

# Append the JOB_ID to the basename (Ugh! long filenames!)
set basename=${basename}_JOB${JOB_ID}

# Setup environment and get directory holding executables
source ${top_dir}/setenv.csh
set bindir=${top_dir}/bin/${BMS_OSNAME}

# Work disk to copy outputs to 
set workdir=${top_dir}/$jobname;
mkdir -p $workdir
env > $workdir/${basename}.env

# Set geantid and mass hypotheses depending on particle type
if ( $ptype == "piplus" ) then
	set geantid=108;
	set mass_hypotheses_positive=0.13957
	set mass_hypotheses_negative
else if ($ptype == "piminus" ) then
	set geantid=109;
	set mass_hypotheses_positive
	set mass_hypotheses_negative=0.13957
else if ($ptype == "proton" ) then
	set geantid=114;
	set mass_hypotheses_positive=0.93827
	set mass_hypotheses_negative
else
	echo "Unknown ptype: "'"'$ptype'"'"!"
	exit;
endif
echo "geantid = $geantid";
echo "mass_hypotheses_positive = $mass_hypotheses_positive";
echo "mass_hypotheses_negative = $mass_hypotheses_negative";

# Create control.in file
rm -f control.in
cat ${top_dir}/farm/control.in.tmpl \
	| sed -e s/BASEFILENAME/${basename}/g \
	| sed -e s/PTYPE/${ptype}/g \
	| sed -e s/MOMENTUM/${p}/g \
	| sed -e s/THETA/${theta}/g \
	| sed -e s/GEANTID/${geantid}/g \
	| sed -e s/JOB_ID/${JOB_ID}/g \
	> ${workdir}/${basename}_control.in
ln -s ${workdir}/${basename}_control.in control.in

# Create JANA configuration files
rm -f jana_ALT1.conf
cat ${top_dir}/farm/jana_ALT1.conf.tmpl \
	| sed -e s/NUM_THREADS/${nthreads}/g \
	| sed -e s/MASS_POSITIVE/${mass_hypotheses_positive}/g \
	| sed -e s/MASS_NEGATIVE/${mass_hypotheses_negative}/g \
	> jana_ALT1.conf

rm -f jana_KalmanSIMD.conf
cat ${top_dir}/farm/jana_KalmanSIMD.conf.tmpl \
	| sed -e s/NUM_THREADS/${nthreads}/g \
	| sed -e s/MASS_POSITIVE/${mass_hypotheses_positive}/g \
	| sed -e s/MASS_NEGATIVE/${mass_hypotheses_negative}/g \
	> jana_KalmanSIMD.conf


####################### Run hdgeant ##########################
#
echo "hdgeant output redirected to log file: ${workdir}/${basename}_hdgeant.log"
${bindir}/hdgeant >& ${workdir}/${basename}_hdgeant.log

####################### Run mcsmear ##########################
#
echo "Running mcsmear ..."
${bindir}/mcsmear -n ${basename}.hddm
cp ${basename}_smeared.hddm $workdir

####################### Run hdroot ##########################
#
echo "hd_root output redirected to log file: ${workdir}/${basename}_ALT1.log"
time ${bindir}/hd_root --dumpconfig --config=jana_ALT1.conf ${basename}_smeared.hddm -o ${basename}_ALT1.root >& ${workdir}/${basename}_ALT1.log
cp ${basename}_ALT1.root $workdir
cp jana.config $workdir/${basename}_ALT1.config

echo "hd_root output redirected to log file: ${workdir}/${basename}_KalmanSIMD.log"
time ${bindir}/hd_root --dumpconfig --config=jana_KalmanSIMD.conf ${basename}_smeared.hddm -o ${basename}_KalmanSIMD.root >& ${workdir}/${basename}_KalmanSIMD.log
cp ${basename}_KalmanSIMD.root $workdir
cp jana.config $workdir/${basename}_KalmanSIMD.config


