# Runs GEMC using the gcard, on LUND file produced by generator
#
#
# N is set to to the number requested by the user, with a max of 10K set by the portal

def C_runGemc(scard, **kwargs):

	gemcInputOptions = ""

	if scard.genExecutable == 'gemc':
		gemcInputOptions = scard.genOptions
	else:
		gemcInputOptions = """ -INPUT_GEN_FILE="lund, {0}" """.format(scard.genOutput)

	torusField = scard.torus
	solenField = scard.solenoid

	runGemc = """
# Run GEMC
# --------

echo GEMC START:  `date +%s`

# copying the gcard to <conf>.gcard
cp /jlab/clas12Tags/$CLAS12TAG"/config/"{2}".gcard" {2}.gcard

echo
echo GEMC executable: `which gemc`

gemc -USE_GUI=0 -OUTPUT="evio, gemc.evio" -N={0} {1} {2}.gcard -SCALE_FIELD="TorusSymmetric, {3}" -SCALE_FIELD="clas12-newSolenoid, {4}"
if ($? != 0) then
	echo gemc failed
	echo removing data files and exiting
	rm -f *.hipo *.evio
	exit 204
endif
# removing generated events file
rm -f *.dat

echo
printf "GEMC Completed on: "; /bin/date
echo
echo "Directory Content After GEMC:"
ls -l
if ($? != 0) then
	echo ls failure
	echo removing data files and exiting
	rm -f *.hipo *.evio
	exit 211
endif
echo

echo GEMC END:  `date +%s`

# End of GEMC
# -----------

""".format(scard.nevents, gemcInputOptions, scard.configuration, torusField, solenField)

	return runGemc
