#!/bin/csh 
#
# This is a command file for running GEANT.  Type
#          e614.com > e614.log &
# to run this command file in the background (ie.
# non-interactively) and to stream output to a LOG
# file.  Input and output files are assigned to
# environment variables (names in upper case) as follows:
#
# Input:
#
# Mandatory files - always read
#   CHGEOM      Detector geometry
#   FFCARD      GEANT input commands
#   ISOMAP1     Map of drift times in DC cell (isochrones)
#   ISOMAP2     Map of drift times in PC cell (isochrones)
#   ISOMAP3     Map of drift times in TC cell (isochrones)
#   TDCMAP1     Map of TDC channels (1st bank)
#   TDCMAP2     Map of TDC channels (2nd bank)
# Optional files - depend on FFCARDS settings
#   FIELDMAP    Field map file
#   OPERAMAP    Opera field map file
#   RAYFILE     REVMOC or M13GEANT rays
#   DCEFF       Plane efficiency for each DC plane
#   PCEFF       Plane efficiency for each PC plane
#   MUBEAM      Muon beam properties
#   EBEAM       Electron beam properties
#
# Output:
#
# Note: in general geant output files are large and
#       should not be written into the user home
#       directory. This is controlled by the E614GEANT
#       environement variable:
#
#   E614GEANT   directory to store output files
#
#   File names are automatically constructed based on run number
#   from the RUNG ffcard.
#
#   Always created:
#     runxxxxxx.hbook --> HBOOK Histograms
#   Created if ISWITCH(10) = 1 or 3
#     runxxxxxx.dat   --> Digitized data (YBOS format)
#   Created if ISWITCH(10) = 2 or 3
#     hitsxxxxxx.dat  --> Un-digitized data (ASCII format)
#
if (! $?FFCARD) setenv FFCARD e614.ffcards
#
setenv CHGEOM $CAL_DB/dt_geo.00050
#
# Check the Bfield is off or on
# 
@ bfld=`grep "^BFLD" $FFCARD | grep -v C |awk '{print $2}'`
#
if ($bfld == 0) then
    echo "BField is set OFF...\!"
    setenv ISOMAP1 $CAL_DB/dc_str.00018
    setenv ISOMAP2 $CAL_DB/pc_str.00005
    setenv ISOMAP3 $CAL_DB/tc_str.00008
else
    echo "BField is set ON...\!"
    setenv ISOMAP1 $CAL_DB/dc_str.00022
    setenv ISOMAP2 $CAL_DB/pc_str.00001
    setenv ISOMAP3 $CAL_DB/tc_str.00009
endif
#
setenv TDCMAP1 $CAL_DB/fbc1_map.00039
setenv TDCMAP2 $CAL_DB/fbc2_map.00039
setenv CORRUV  $CAL_DB/dc_ppc.00010
setenv CORRROT $CAL_DB/dc_prc.00007
setenv MU_BEAM $CAL_DB/surface_tune_03.dat
#
# setenv FIELDMAP $CAL_DB/field_map.0002
#
# Uncomment OPERAMAP below to activate the OPERA field map
#
setenv OPERAMAP $CAL_DB/bfld_map.00012
#
# location of all output files
#
if (! $?E614GEANT) setenv E614GEANT results
if (! -d $E614GEANT) then
  echo "Creating directory $E614GEANT\n"
  mkdir $E614GEANT
endif
#
# DC efficiencies
#
# ignored  if CALC_DC_EFFS is false or omitted
# used     if CALC_DC_EFFS is true
# setenv CALC_DC_EFFS false
setenv DCEFF $E614GEANT/dc_effs.dat
#
# PC efficiencies
#
# ignored  if CALC_PC_EFFS is false or omitted
# used     if CALC_PC_EFFS is true
# setenv CALC_PC_EFFS false
setenv PCEFF $E614GEANT/pc_effs.dat
#
# TC efficiencies
#
# ignored  if CALC_TC_EFFS is false or omitted
# used     if CALC_TC_EFFS is true
# setenv CALC_TC_EFFS false
setenv TCEFF $E614GEANT/tc_effs.dat
#
# Input ray file
#
# testbeam.dat is a REVMOC ray file
# real.out is a M13GEANT ray file
# setenv RAYFILE testbeam.dat
setenv RAYFILE real.out
#
@ msor=`grep "^MSOR" $FFCARD | grep -v C |awk '{print $2}'`
#
#if ($msor == 5) then
# This is dave's tune file method... need to
# get files from Dave to use this
# setenv TEC_X_VS_Y
# setenv TEC_X_VS_DX
# setenv TEC_Y_VS_DY
#endif
#
# Below is latest beam input file:
#
if ($msor == 6) then
  setenv TEC_X_VS_Y $CAL_DB/msor6_20558_all.dat
endif
#
#Findout what run number is selected in the FFCARD
#
set runn = `grep -i "^RUNG " $FFCARD|awk '{print $2}'`
#
# Dump some run information to logfile
#
echo "Using FFCARD file: $FFCARD, run number: $runn"
echo "Output files are written to directory: $E614GEANT"
echo "Log file is $E614GEANT/run$runn.log"
#
#Set location of geant executable
#
if (! $?GEANT_BIN) setenv GEANT_BIN /global/home/e614/rundb-dev/exe
if (! $?EXECUTABLE) setenv EXECUTABLE $GEANT_BIN/wg_g3_12
if (! -f $EXECUTABLE) then
 echo " Executable $EXECUTABLE not found. Try exe directory"
 setenv EXECUTABLE $GEANT_BIN/wg_g3_12
endif
#
# Now run the binary
#
$EXECUTABLE
exit