#!/bin/csh -f


#********  run-line variables input by user ******** 

#we expect the DIST varible to be either 6,9,12,15, or 18
setenv DIST $1

# run number
setenv nrun $2

# define detector file  -don't modify this!
setenv DETNAME orca_115strings_20mhorizontal_18OMs_${DIST}.0mvertical
echo "detname is"


#************** variables which should be modified by user ************

# this is where all the data inputs should be stored - currently, this directory
setenv ORCA_ROOT `pwd`
echo $ORCA_ROOT


# point this to the mupage executable!
#setenv MUPAGE_EXEC /sps/km3net/users/cjames/grid_km3_mupage/v3r5_mupage/mupagev3r5.exe
setenv MUPAGE_EXEC v3r5_mupage/mupagev3r5.exe

#export CERN=/opt/exp_soft/km3net.org/km3/cern
#export CERN_ROOT=/opt/exp_soft/km3net.org/km3/cern
#export CERNLIB=$CERN/pro/lib
#export CERNBIN=$CERN/pro/bin
#export PATH=$PATH:$CERNBIN

setenv CERN /tmp/orca_files/km3/cern

setenv CERN_ROOT /tmp/orca_files/km3/cern
echo "1"
setenv CERNLIB ${CERN}/pro/lib
setenv CERNBIN ${CERN}/pro/bin
setenv PATH ${PATH}:${CERNBIN}
echo "2"
# point this to the km3 executable!
#setenv KM3_EXEC /sps/km3net/users/cjames/grid_km3_mupage/km3/Linux/km3mcv5r1.exe

setenv KM3_EXEC /tmp/orca_files/km3/km3_io/km3/Linux/km3mcv5r1.exe
#setenv KM3_THRONG /afs/in2p3.fr/throng/km3net
setenv KM3_THRONG afs/in2p3.fr/home/throng/km3net
setenv KM3_V v5r1

setenv KM3_DAT $KM3_THRONG/dat/km3/${KM3_V}_orca

# directories that hold detector, parameter etc data
setenv DETDIR ${ORCA_ROOT}/detectors
setenv PARAMDIR ${ORCA_ROOT}/parameters
setenv KM3_DAT ${ORCA_ROOT}/km3_data

#defines input parameters - esp can size
setenv PARAM_FILE ${PARAMDIR}/parameters_orca_115_${DIST}m.dat
  

#*************** variables which could be modified by user ************

# defines output file names
setenv PREFIX muatm
setenv MUPAGE_OUTFILE ${DETNAME}_${PREFIX}${nrun}.evt
setenv KM3_OUTFILE ${DETNAME}_${PREFIX}${nrun}.km3_v5r1.evt

# the program will cd to OUTDIR before execution, since km3 especially
# can produce a few files in the local run dir which can't be redirected
setenv OUTDIR `pwd`

#number of events to produce - output file size, and run time, scale linearly with this
setenv NEVENTS 1000

@ SEED = ${DIST}000 + $nrun
echo "seed -> ${SEED}"

#************************* MUPAGE *******************************

# move to output directory
echo "cd $OUTDIR"
cd $OUTDIR

setenv DETFILE ${DETNAME}.det
# give detector file to mupage
setenv GEOFIL "${DETDIR}/${DETFILE}  1 1"
echo "GEOFIL=${GEOFIL}"


$MUPAGE_EXEC -i $nrun -n $NEVENTS -s $SEED -o ${MUPAGE_OUTFILE} livetime.txt -p ${PARAM_FILE} -g $GEOFIL


# ***************************  km3  *********************************************************

# km3 input file
setenv EVIFIL ${MUPAGE_OUTFILE}
echo "EVIFIL=${EVIFIL}"
setenv EVOFIL ${KM3_OUTFILE}

# Does not abort on floating-point errors
setenv TRAPKM3FPE 0

# points to data files generated by 'hit'
setenv TAG orca_200m_
setenv LABEL v5r1

setenv hbmuon ${KM3_DAT}/km3-${TAG}muon-100gev_${LABEL}.hit
setenv hbele1 ${KM3_DAT}/km3-${TAG}elec-100mev_${LABEL}.hit
setenv hbele2 ${KM3_DAT}/km3-${TAG}elec-1gev_${LABEL}.hit
setenv hbele3 ${KM3_DAT}/km3-${TAG}elec-10gev_${LABEL}.hit
setenv hbele4 ${KM3_DAT}/km3-${TAG}elec-100gev_${LABEL}.hit

setenv demuon ${KM3_DAT}/km3-${TAG}muon-100gev_${LABEL}.desc
setenv deele1 ${KM3_DAT}/km3-${TAG}elec-100mev_${LABEL}.desc
setenv deele2 ${KM3_DAT}/km3-${TAG}elec-1gev_${LABEL}.desc
setenv deele3 ${KM3_DAT}/km3-${TAG}elec-10gev_${LABEL}.desc
setenv deele4 ${KM3_DAT}/km3-${TAG}elec-100gev_${LABEL}.desc

# music files. NOTE: no dispersion file is needed
setenv Meloss ${KM3_DAT}/km3-music-eloss.dat
setenv Mddxsections ${KM3_DAT}/km3-music-double-diff.dat
setenv Mxsections ${KM3_DAT}/km3-music-cross-sections.dat

#setenv GEOFIL ${DETFILE}
setenv GEOFIL ${DETDIR}/${DETFILE}


echo "Now KM3_DAT is ${KM3_DAT}"
echo " "
echo "Now Meloss is ${Meloss}"
echo "Now Mddxsections is ${Mddxsections}"
echo "Now Mxsections is ${Mxsections}"
 

# Run KM3 with all other user options

rm fort.68

echo "GEOFIL=${GEOFIL}"

$KM3_EXEC << EOD
IRAND 3 2000 0 0
INEVT ${NEVENTS}
XMERG 0.5
IEMPT 0
END
EOD

# IRAND    = random number generator seeds 
# INEVENT  = number of events
# IPT40    = K40 simulation
# XMERG    = merging
# IGAIN    = 0 -> no tts smearing
# XRAWH    = no raw hits
# INGRP    = Group velocity dispersion
# IEMPT    = ?


# I P T 4 0  60000 500  or 135000 for 15"
# Copy the output file back to the desired data directory

#ls -l -a .

bzip2 ${MUPAGE_OUTFILE}
bzip2 ${KM3_OUTFILE}

echo "rm fort.*"
rm fort.*
