#!/bin/tcsh -f

# This script is to be run by farm jobs. It will produce a single,
# simulated data set using hdgeant, but then smear it two different
# times according to two BCAL segmentation schemes. The smeared files
# will be reconstructed using the reprective segmentation schemes
#
# The JOB_ID environment variable
# is assumed to be set. If it is not, then a default of
# "1234" is used.
#

set workdir=/work/halld/home/davidl/2011.06.03.bcal_calib


# 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`

if ( ! $?JOBNAME ) then 
	setenv JOBNAME OUTPUT.2
endif


# Create unique prefix for filenames associated with this job
set prefix=JOB${JOB_ID}

# Setup environment and pointers to specific executables
source ${workdir}/setenv.csh
set hdgeant=${HALLD_HOME}/bin/${BMS_OSNAME}/hdgeant
set mcsmear_fine=${workdir}/fine/bin/${BMS_OSNAME}/mcsmear
set mcsmear_course=${workdir}/course/bin/${BMS_OSNAME}/mcsmear
set hd_root_fine=${workdir}/fine/bin/${BMS_OSNAME}/hd_root
set hd_root_course=${workdir}/course/bin/${BMS_OSNAME}/hd_root

# Output disk to copy files to 
set outdir=${workdir}/${JOBNAME};
mkdir -p $outdir
env > $outdir/${prefix}.env


# Create control.in file
rm -f control.in
cat ${workdir}/farm/control.in.tmpl \
	| sed -e s/JOB_ID/${JOB_ID}/g \
	> ${outdir}/${prefix}_control.in
ln -s ${outdir}/${prefix}_control.in control.in


####################### Run hdgeant ##################################
#
echo "hdgeant output redirected to log file: ${outdir}/${prefix}_hdgeant.log"
${hdgeant} >& ${outdir}/${prefix}_hdgeant.log



####################### Run mcsmear(fine) ############################
#
echo "Running mcsmear (fine) ..."
${mcsmear_fine} sim_photon_${JOB_ID}.hddm >& ${outdir}/${prefix}_mcsmear_fine.log
set smeared_fine=${prefix}_sim_photon_${JOB_ID}_smeared_fine.hddm
mv sim_photon_${JOB_ID}_smeared.hddm ${smeared_fine}
cp ${smeared_fine} $outdir

####################### Run hdroot (fine) ############################
#
echo "hd_root output redirected to log file: ${outdir}/${prefix}_hd_root_fine.log"
time ${hd_root_fine} ${smeared_fine} -PPLUGINS=bcal_calib,mcthrown_hists -o ${prefix}_fine.root >& ${outdir}/${prefix}_hd_root_fine.log
cp ${prefix}_fine.root $outdir


####################### Run mcsmear(course) #########################
#
echo "Running mcsmear (course) ..."
${mcsmear_course} sim_photon_${JOB_ID}.hddm >& ${outdir}/${prefix}_mcsmear_course.log
set smeared_course=${prefix}_sim_photon_${JOB_ID}_smeared_course.hddm
mv sim_photon_${JOB_ID}_smeared.hddm ${smeared_course}
cp ${smeared_course} $outdir

####################### Run hdroot (course) #########################
#
echo "hd_root output redirected to log file: ${outdir}/${prefix}_hd_root_course.log"
time ${hd_root_course} ${smeared_course} -PPLUGINS=bcal_calib,mcthrown_hists -o ${prefix}_course.root >& ${outdir}/${prefix}_hd_root_course.log
cp ${prefix}_course.root $outdir


