#!/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/2006 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2006
#
#
# ========================================================================
#
#
#
##############################################################################
#
# This script creates the ganymed.rc files with different parameter sets (the 
# parameters 1 and 3 are changed in steps). Then the script runs ganymed and 
# sponde for this different parameter sets. 
#
# variables, that have to be set by the user:
#
# - path          directory, where the files are stored 
# - mars          directory, where your Mars version is stored
# - dataset       dataset file, which shall be used (filename without path)
#                  the file has to be stored in the dataset directory in the 
#                  path
# - par*step      step width for changing the parameter * 
# - par*numminus  number of steps going to smaller values of the parameter * 
# - par*num       number of steps for changing the parameter * 
# - spondenum     number of sponde rc file (also of the root and log file)
# - ganymednum    number of ganymed root file
#
# Please make sure, that you provide the following files:
# - $path/setup/ganymed$ganymednum.rc
# - $path/setup/sponde$spondenum.rc
# - $path/datasets/mcdataset-for-sponde.txt (if you use the script preparemc, 
#    this file is produced automatically)
# - $path/datasets/$datasetfilename (you give the datasetfilename below, but 
#    you have to make sure, that it is in the directory $path/datasets
#
# To re-run ganymed (e.g. with new Software-Version), you have to delete 
# the ganymed*.root files or set a new ganymednum. 
# 
##############################################################################


# to be set by the user
path=/home/dorner/crabspectrum
mars=/home/dorner/mars.cvs
datasetfilename=datasetcrab2005-2.txt
spondenum=0
ganymednum=1
# settings for parameter 3
par3step=0.005
par3numminus=10
par3num=13
# settings for parameter 1
par1step=0.005
par1numminus=1 
par1num=7


function printsetup()
{
   echo "you set the following parameters: "
   echo " path:       $path "
   echo " ganymedrc:  $ganymedrc"
   echo " sponderc:   $sponderc"
   echo " dataset:    $dataset"
   echo " mcdataset:  $mcdataset"
   echo " spondenum:  $spondenum"
   echo " ganymednum: $ganymednum"
   echo " par1:       -$par1numminus to $par1num in $par1step steps"
   echo " par3:       -$par3numminus to $par3num in $par3step steps"
}

#general variables
datasetpath=$path/datasets
setuppath=$path/setup
#resultpath=$path/ganyspec
resultpath=$path/ganyspec`echo $datasetfilename | sed -e 's/[.]txt//'`
dataset=$datasetpath/$datasetfilename
mcdataset=$datasetpath/mcdataset-for-sponde.txt
ganymedrc=$setuppath/ganymed$ganymednum.rc
sponderc=$setuppath/sponde$spondenum.rc
setuplog=$setuppath/setup`date +%F-%H-%M-%S`.log

if ! ls $ganymedrc >/dev/null 2>&1
then
   echo "couldn't find $ganymedrc."
   echo "please provide the file $ganymedrc and re-run the script"
   exit
fi

if ! ls $sponderc >/dev/null 2>&1
then
   echo "couldn't find $sponderc."
   echo "do you want to continue anyhow? (ganymed will be done)"
   echo "please insert y, if you want continue"
   echo "              n, if you want quit"

   answer=`head -n 1`
   case $answer in
      y) echo "continue creating rc files and running ganymed"
         ;;
      n) echo "please provide the file $ganymedrc and re-run the script"
         exit
         ;;
      *) echo "your answer is not clear -> exit"
         exit
         ;;
   esac
   
fi

#start of the script
printsetup
printsetup > $setuplog

mkdir -pv $resultpath

echo "generating the ganymed.rc files with different parameter sets"
# generating the ganymed.rc files with different parameter sets
par3=`cat $ganymedrc | grep 'Cut1.Param3' | cut -d: -f2`
par1=`cat $ganymedrc | grep 'Cut1.Param1' | cut -d: -f2`

echo "parameter: 3 $par3 ,  1 $par1"

start3=`echo $par3 - \( $par3numminus*$par3step \) | bc `
start1=`echo $par1 - \( $par1numminus*$par1step \) | bc `
echo "start: 1: $start1 3: $start3"

val1=$start1
val3=$start3

for (( i=0 ; i < $par3num ; i++ ))
do 
  for (( j=0 ; j < $par1num ; j++ ))
  do 
#     echo "i: $i - j: $j ------- par1: $val1 and par3: $val3"
     dir=$resultpath/set${val1}_${val3}
     if ! ls $dir >/dev/null 2>&1
     then 
        mkdir -pv $dir
     fi
#     rcfile=$dir/ganymed_${val1}_${val3}_onoff.rc
     rcfile=$dir/`basename $ganymedrc` 
     if ! ls $rcfile >/dev/null 2>&1
     then 
        cat $ganymedrc | sed -e s/"${par1}"/"   ${val1}"/ -e s/"${par3}"/"  ${val3}"/ > $rcfile
     else
        echo "rcfile $rcfile already exists"
     fi
     val1=`echo $val1 + $par1step | bc`
  done
  val3=`echo $val3 + $par3step | bc`
  val1=$start1
done


# running ganymed and sponde for different parameter sets

# function running ganymed and sponde for a set of cuts
function dospec()
{
   cd $mars

   name=$set/ganymed`printf %08d $ganymednum`
   ganymedfile=$name.root
   if ! ls $ganymedfile >/dev/null 2>&1
   then 
      cd $mars
#      rc=`ls $set/ganymed_*onoff.rc`
      ganymedlog=$name.log
      echo "starting ganymed for set $set ..."
      if ! condor_run ./ganymed -b -f --n=$ganymednum --log=$ganymedlog --out=$set --config=$set/`basename $ganymedrc` $dataset >/dev/null
      then 
         echo "condor is not working " 
         return
      fi
   else
      echo "ganymed for set $set already done => continue with sponde"
   fi

   if ! ls $sponderc >/dev/null 2>&1
   then 
      echo "ERROR: spondercfile $sponderc is missing"
      return
   fi
   spondefile=$set/sponde$spondenum-g$ganymednum.root
   spondelog=$set/sponde$spondenum-g$ganymednum.log
   if ! ls $spondefile >/dev/null 2>&1
   then
      echo "starting sponde for set $set ..."
      if ! condor_run ./sponde -b -f --log=$spondelog --config=$sponderc -f $ganymedfile $mcdataset $spondefile >/dev/null
      then
         echo "condor is not working " 
         return
      fi
      echo "finished sponde for set $set ..."
   else
      echo "sponde $spondenum for set $set already done => please remove sponde files or give new spondenum"
   fi
}


sets=`find $resultpath -type d`
for set in ${sets[@]}
do 
#   echo "set: "$set
   if [ "$set" == "$resultpath" ]
   then
      continue
   fi
   dospec &
   sleep 3
done


