#!/bin/csh
#
# This is a command file for GEANT job submissions at WG through the script, tbsub.pl.  
# In tbsub.pl, the script is used as:  e614.com <ffcard file> 
# 
# Input:

# FFCARD: GEANT input commands
setenv FFCARD $1

# Output files will be written WRT E614GEANT
# On Westgrid, "." is the /data/twist/e614/tbsdata/gen<xxx>/run<yyy>
setenv E614GEANT .

# Mandatory files - always read
#   CHGEOM      Detector geometry
#   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
#   ==> for msor == 4
#   MU_BEAM     Muon beam properties
#   E_BEAM      Electron beam properties
#   ==> for msor == 5
#   MDC_X_VS_Y  Muon beam properties
#   MDC_X_VS_DX  //    //    // 
#   MDC_Y_VS_DY  //    //    //
#   EDC_X_VS_Y  Electron beam properties
#   EDC_X_VS_DX    //     //    //   
#   EDC_Y_VS_DY    //    //    // 
#   ==> for msor == 6
#   MTECXVSY    Muon     beam properties
#   ETECXVSY    Electron  //  // 
#
# Output:
#
#   Output files are written to local nodes on WG
#   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)
#
#
setenv CHGEOM $CAL_DB/dt_geo.00053
#
# 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.00023
    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 CORRUVDC  $CAL_DB/dc_ppc.00010
setenv CORRROTDC $CAL_DB/dc_prc.00007
setenv CORRUVPC  $CAL_DB/pc_ppc.00001
setenv CORRROTPC $CAL_DB/pc_prc.00001
setenv CORRTEC   $CAL_DB/tc_rpc.00001
#
# setenv FIELDMAP $CAL_DB/field_map.0002
#
# Uncomment OPERAMAP below to activate the OPERA field map
#
setenv OPERAMAP $CAL_DB/bfld_map.00012
#
# 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 $CAL_DB/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 $CAL_DB/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 $CAL_DB/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
#
# Below is latest beam input file:
#
# Check for the first particle (MUON)#
@ msor=`grep "^MSOR" $FFCARD | grep -v C |awk '{print $2}'`
if ($msor == 4) then
    setenv MU_BEAM  $CAL_DB/surface_tune_03.dat
else if ($msor == 5) then
    setenv MDC_X_VS_Y /home/drgill/e614/experiment/analysis/run_mofia/entrance/data/run20558/old/xyMID_20558LC_tcap.dat
    setenv MDC_X_VS_DX /home/drgill/e614/experiment/analysis/run_mofia/entrance/data/run20558/old/xdxMID_20558LC_tcap.dat
    setenv MDC_Y_VS_DY /home/drgill/e614/experiment/analysis/run_mofia/entrance/data/run20558/old/ydyMID_20558LC_tcap.dat
else if ($msor == 6) then
    setenv MTECXVSY $CAL_DB/msor6_set32_33_36_39.dat
endif
#
# Check for the second particle (POSITRON)
#
@ msor=`grep "^MSOR" $FFCARD | grep -v C |awk '{print $3}'`
#
if ($msor == 4) then
    setenv E_BEAM  $CAL_DB/surface_tune_03.dat
else if ($msor == 5) then
    # These are from Anthony Hillairet's analysis of 2005 data,
    # using DCs for the positron profiles.
    setenv EDC_X_VS_Y  $CAL_DB/beame2004_msor6_YvsX_Ant4Mar2006.dat
    setenv EDC_X_VS_DX $CAL_DB/beame2004_msor6_AXvsX_Ant4Mar2006.dat
    setenv EDC_Y_VS_DY $CAL_DB/beame2004_msor6_AYvsY_Ant4Mar2006.dat
else if ($msor == 6) then
    setenv ETECXVSY $CAL_DB/msor6_set32_33_36_39.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 the local nodes"
echo "For links, see: /global/home/e614/tbsroot/gen<xxx>"
#
# Now run the binary
#
echo "GEANT Executable $EXECUTABLE is set in the Env Var file."
$EXECUTABLE
exit