#!/bin/tcsh -f

# additions to define local versions
setenv JANA_CALIB_URL file:///Users/elton/HallD/calib
alias bfield2root "../../Poisson_OSX/bfield2root"
alias wine "/Applications/Wine.app/Contents/MacOS/startwine"
setenv LANL /Users/elton/scratch/solenoid_replacement/calculations/Poisson_OSX/LANL


# Run the various poisson/superfish programs to generate a magnetic
# field map compatible with the GlueX code base.
#
# Inputs:
#
# gluex_sol_example1.am    Geometery configuration file
#
# gluex_sol_example1.in7   Output grid configuration file
#
#
# NOTE: It is assumed that "wine" is in your PATH and that the
# LANL environment variable is set prior to running this script

if (! $?LANL) then
	echo " "
	echo "You must set your LANL environment variable to point"
	echo "to your Poisson/Superfish installation directory!"
	echo " "
	exit(-1);
endif


# Run AUTOMESH.EXE
echo
echo "}}}}}}}}}}}}}}  Running AUTOMESH.EXE"
wine $LANL/AUTOMESH.EXE ./gluex_sol_*.am

# Run POISSON.EXE
echo
echo "}}}}}}}}}}}}}}  Running POISSON.EXE"
wine $LANL/POISSON.EXE ./GLUEX_SOL_*.T35

# Run SF7.EXE
echo
echo "}}}}}}}}}}}}}}  Running SF7"
wine $LANL/SF7 ./gluex_sol_*.in7

# Transform map into GlueX ASCII format
echo
echo "}}}}}}}}}}}}}}  Running poisson2calibDB.pl"
../poisson2calibDB.pl OUTSF7.TXT

# Copy ROOT file
set mapname=solenoid_1500_poisson_`basename $PWD`
mv solenoid_1500_poisson $mapname
cp $mapname ~/HallD/calib/Magnets/Solenoid

# Make ROOT file
bfield2root -PBFIELD_TYPE=CalibDB -PBFIELD_MAP=Magnets/Solenoid/$mapname

# Run ROOT scripts
#foreach s (../macros/*.C)
#	set cmd="root -q -b -l $s"\('"'$mapname'"'\)
#	echo $cmd
#	$cmd
#	#root -q -b -l $s\('"$mapname"'\)
#end

echo
echo
echo "To view a graphical representation of the map using WSFPLOT.EXE"
echo "do the following:"
echo
echo "wine $LANL/WSFPLOT.EXE ./GLUEX_SOL_EXAMPLE1.T35"
echo
echo
echo

