#!/bin/bash
#
# phonotree.sh - batch script for running a phonotree simulation
#
# usage: phonotree.sh <processId>
#          where <processId> is an unsigned negative integer less than 2^32
# author: richard.t.jones at uconn.edu
# version: february 4, 2017

if [[ $# -lt 1 ]]; then
    echo "Usage: phonotree.sh <processId> [<nevents>]"
    exit 9
fi

echo Running on `hostname -f`

BMS_OSNAME=Linux_CentOS6-x86_64-gcc4.9.2
ROOTSYS=/usr/local/root
source setup.sh

nevents=1000000
if [[ "$2" != "" ]]; then
    nevents=$2
fi

# This step just to compile the program
root -l -b -q loader.C 
cat <<EOI > load.C
#include <TROOT.h>
void load() {
   gROOT->ProcessLine(".L phonoCbrems.C+");
}
EOI

for n in a b c d; do
    echo "#include <TROOT.h>" > run${n}.C
    echo "#include <TString.h>" >> run${n}.C
    echo "int gentree(int nevents, TString outfile);" >> run${n}.C
    echo "void run${n}() {" >> run${n}.C
    outfile='"phonotree_'${1}${n}'.root"' 
    echo "   gentree($nevents, $outfile);" >> run${n}.C
    echo "}" >> run${n}.C
    root -l -b -q load.C run${n}.C &
done
wait
rm run*.C load.C
