#!/bin/tcsh

# This job will use genr8, genr8_2_hddm, and hdgeant to generate
# 10K events of p_pi+pi-pi0 data.

set NEVENTS=10000

# Setup working directory on Hall-D work disk
cd /work/halld/NightlyTests
rm -rf Test1.previous
if( -e Test1 ) then
	mv Test1 Test1.previous
endif
mkdir Test1
cd Test1
cp -r /group/halld/Software/scripts/NightlyTest1/* .


# Create output.log file
echo "Output for" `date` > output.log
echo " " >> output.log
hostname >> output.log
cat /proc/cpuinfo > cpuinfo.log


# Dump environment 
env > env.log

# Confirm required executables exist
set GENR8=${HALLD_HOME}/bin/${OSNAME}/genr8
set GENR82HDDM=${HALLD_HOME}/bin/${OSNAME}/genr8_2_hddm
set HDGEANT=${HALLD_HOME}/bin/${OSNAME}/hdgeant
set HDROOT=${HALLD_HOME}/bin/${OSNAME}/hd_root
if( ! -e ${GENR8} ) then
	echo "${GENR8} does not exist \!\!" >> output.log
	exit
endif
if( ! -e ${GENR82HDDM} ) then
	echo "${GENR82HDDM} does not exist \!\!" >> output.log
	exit
endif
if( ! -e ${HDGEANT} ) then
	echo "${HDGEANT} does not exist \!\!" >> output.log
	exit
endif
if( ! -e ${HDROOT} ) then
	echo "${HDROOT} does not exist \!\!" >> output.log
	exit
endif

# Run genr8
set cmd="${GENR8} -l1000 -M${NEVENTS} -Ap_pi+pi-pi0.ascii"
echo "Running genr8 ..." >> output.log
echo "$cmd < p_pi+pi-pi0.input" >> output.log
echo " " >> output.log
$cmd < p_pi+pi-pi0.input >& genr8.log

# Run genr8_2_hddm
set cmd="${GENR82HDDM} p_pi+pi-pi0.ascii"
echo "Running genr8_2_hddm ..." >> output.log
echo "$cmd" >> output.log
echo " " >> output.log
$cmd >& genr8_2_hddm.log

# Run hdgeant
set cmd="${HDGEANT}"
echo "Running hdgeant ..." >> output.log
echo "$cmd" >> output.log
echo " " >> output.log
$cmd >& hdgeant.log

# Run HDROOT
unsetenv JANA_PLUGIN_DIR
setenv JANA_PLUGIN_PATH ${HALLD_HOME}/lib/${OSNAME}
set cmd="${HDROOT} --plugin=trackeff_hists2 hdgeant.hddm"
echo "Running hd_root ..." >> output.log
echo "$cmd" >> output.log
echo " " >> output.log
ln -s ${HALLD_HOME}/src/programs/Simulation/HDParSim/hd_res_charged.root .
$cmd >& hd_root.log

# Run ROOT macro to project out histos from hd_root.root file
set cmd="root -b -q efficiency.C"
echo "Running root ..." >> output.log
echo "$cmd" >> output.log
echo " " >> output.log
$cmd >& root.log

# Run root2email
set cmd="root2email -if efficiency.root -r davidl@jlab.org -H pt_pull -H theta_pull -H pull -H chisq_corrected -H eff_vs_pt_vs_theta"
echo "Running root2email ..." >> output.log
echo "$cmd" >> output.log
echo " " >> output.log
$cmd >& root2email.log

echo " Done." >> output.log
