#!/bin/sh
#
# ========================================================================
#
# *
# * This file is part of MARS, the MAGIC Analysis and Reconstruction
# * Software. It is distributed to you in the hope that it can be a useful
# * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
# * It is distributed WITHOUT ANY WARRANTY.
# *
# * Permission to use, copy, modify and distribute this software and its
# * documentation for any purpose is hereby granted without fee,
# * provided that the above copyright notice appear in all copies and
# * that both that copyright notice and this permission notice appear
# * in supporting documentation. It is provided "as is" without express
# * or implied warranty.
# *
#
#
#   Author(s): Daniela Dorner  05/2007 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2007
#
#
# ========================================================================
#
# This script runs the reflector.
#

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
program=camera
column=fCameraFileAvail
detectordir=/home/msmeyer/MC/MagicSoft/Simulation/Detector

set -C

# printing input card to stdout
function printinputcard()
{
   echo $cameraversion
   echo "ct_num 1"
   echo "ct_geom 1"
   echo "qe_file 0 $detectordir/Data/qe-emi-coat.RFL.dat"
   echo "input_file 0 "$mcpath"/reflector/"$date"/"$outfile
   echo "data_file $outpath/"`echo $outfile | sed -e 's/rfl/dat/'`
   echo "root_file $outpath/"`echo $outfile | sed -e 's/rfl/root/'`
   echo "trigger_prop 3.0 0.25 1.0 2.0"
   echo "trigger_single 0 4 4 2"
   echo "nsb_on"
   echo "nsb_mean 0.178 50"
   echo "fadc_noise 1.3 2.4 1."
   echo "sigma_xy_cm_spot "$spotsize
   echo "mirror_fraction 0 0.73"
   echo "seeds "$seeds
   echo "nsb_directory $detectordir/StarLight/inner/"
   echo "nsb_dir_outer $detectordir/StarLight/outer/"
   echo "fadc_prop 1 7.8 5.0"
   echo "fadc_outer 1 3.2 5.0"
   echo "write_McTrig"
   echo "write_McFadc"
   echo "end_file"
}


scriptlog=$runlogpath/run$program-$datetime.log
date >> $scriptlog 2>&1

# get sequence # 
gettodo >> $scriptlog 2>&1
run=$process

# lock sequ
lockfile=$lockpath/lock-$table-$column-$sequence.txt
checklock >> $scriptlog 2>&1

echo "run $program for run $run..." >> $scriptlog 2>&1
printprocesslog "INFO starting $program for run $run"

primvar=$run
setstatus "start" >> $scriptlog 2>&1

echo "run $program for run $run "
echo "create input card... "

# get values for inputcard
query="SELECT fMCRunNumber FROM MCRunData where fMCRunNumber="$run
reflectorrunno=`sendquery`
query="SELECT DATE_FORMAT(fRunStart, '%Y/%m/%d') FROM MCReflectorRunData where fReflectorRunNumber="$reflectorrunno
date=`sendquery`
query="SELECT fParticleTypeName FROM MCCorsikaRunData "
query=$query" LEFT JOIN ParticleType ON MCCorsikaRunData.fParticleTypeKEY=ParticleType.fParticleTypeKEY "
query=$query" LEFT JOIN MCRunData ON MCCorsikaRunData.fCorsikaRunNumber=MCRunData.fCorsikaRunNumber "
query=$query" WHERE fMCRunNumber="$run
particle=`sendquery`
outpath=$mcpath"/"$program"/"$date
date2=`echo $date | sed -e 's/\///g'`
outfile=$date2"_"`printf %06d $reflectorrunno`"_"$particle".rfl"
query="SELECT fAdditionalSpotSize FROM MCRunData where fMCRunNumber="$run
spotsize=`sendquery`
query="SELECT fCameraSeed, fCameraSeed+1 FROM MCRunData where fMCRunNumber="$run
seeds=`sendquery`

echo "" >> $scriptlog 2>&1
echo "INPUTCARD:" >> $scriptlog 2>&1
echo "----------" >> $scriptlog 2>&1
echo "" >> $scriptlog 2>&1
echo "" >> $scriptlog 2>&1
printinputcard >> $scriptlog 2>&1

logfile=$outpath/$program"-"`printf %06d $run`".log"
makedir $outpath >> $scriptlog 2>&1

cd $detectordir/Camera

echo "INPUTCARD:" >> $logfile 2>&1
printinputcard >> $logfile 2>&1
echo "" >> $logfile 2>&1
echo "" >> $logfile 2>&1
printinputcard | ./camera

check1=$?

case $check1 in
   0)   echo " check1=$check1 -> everything ok " >> $scriptlog 2>&1
        printprocesslog "INFO $program finished successfully for sequence $sequence"
        ;;
   *)   echo " check1=$check1 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
        printprocesslog "ERROR $program failed for sequence $sequence"
        com=$Freflector
        check=$check1
        ;;
esac

setstatus "stop" >> $scriptlog 2>&1

finish >> $scriptlog 2>&1

