#!/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 mc sequences and datasets for a psf, observation mode 
# and zenith range chosen by the user.
#
# variables, that have to be set by the user:
# - dir        directory, where the mc sequence and dataset files are stored 
#               be careful: don't move the sequence files afterwards, as the 
#               paths are stored in the datasetfiles
# - mars       directory, where your Mars version is stored
#               only needed, if you want to create a rf-root-file for the 
#               energy estimation 
# - sourcefile directory, where your Mars version is stored
#               only needed, if you want to create a rf-root-file for the 
#               energy estimation 
#               be careful, you also have to set the mars path in sourcefile
# - zdmin      minimum zenith distance
# - zdmax      maximum zenith distance
# - psf        psf 
#               at the moment mc with psf 14 and 20 is available
# - modes      observation mode
#               the explanation for the modes can be found in 
#                /montecarlo/rawfiles/README.txt
# - numruns    num of runs, that are in the sequence file, which are used for
#              training (SequencesOn in $mcdataset)
#
# If you want to create a rf-root-file for the energy estimation, you have to 
# comment out the 'exit' before the last lines of the script. 
# 
##############################################################################

#function to create directory
function makedir()
{
   if [ ! -d $@ ]
   then
     mkdir -pv $@ 
     if [ ! -d $@ ]
     then 
        echo "could not make dir "$@
        exit
     fi
   fi
}



#to be set by the user 
dir=/home/operator/mc 
mars=/home/operator/Mars.cvs 
zdmin=9
zdmax=51
psf=14
modes=( "02" "04" ) # e.g. nowobble
numruns=5

makedir $dir

#needed if not set in .bashrc or when script is e.g. executed in cronjob
export ROOTSYS=/opt/root_v5.12.00f
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOTSYS/lib
export PATH=$PATH:$ROOTSYS/bin

mcdir=/magic/montecarlo/rawfiles
mcdataset=$dir/mcdataset-test-train.txt
mcdataset2=$dir/mcdataset-for-ganymed.txt

#calculation of the zbin
zbinmin=`echo "scale=2 ; 100*(1 - c($zdmin*3.14/180))+1" | bc -l`
zbinmax=`echo "scale=2 ; 100*(1 - c($zdmax*3.14/180))+1" | bc -l`
zbinmin=`echo $zbinmin | cut -d. -f1`
zbinmax=`echo $zbinmax | cut -d. -f1`
echo "zd: min: $zbinmin max: $zbinmax"
echo "numruns: $numruns"

j=0
for mode in ${modes[@]}
do 
   echo "mode: "$mode
   for (( i=$zbinmin ; i < $zbinmax ; i++ ))
   do 
      zbin=`printf %02d $i`
      echo "zbin: $zbin"
      path=$mcdir/19$zbin/$mode/$psf
      runsforfirst=

      runs=(`ls $path 2>/dev/null | grep Gamma | cut -d_ -f2 | sed -e 's/^0//g' -e 's/^0//g' -e 's/^0//g' -e 's/^0//g' | tr "\n" " "`)
      if [ "$runs" = "" ]
      then
         echo "  no runs found for zbin $i "
         continue
      fi

      firstrun=${runs[0]}
      secondrun=${runs[${numruns}]}
      firstrunno=`printf %08d $firstrun`
      secondrunno=`printf %08d $secondrun`
      unset runs[0]
      for (( k=1 ; k < $numruns ; k++ ))
      do 
         runsforfirst=$runsforfirst" ${runs[$k]}"
         unset runs[$k]
      done
      date=`echo $path | cut -c 22-31 | sed -e 's/\//-/g'`
      
      trainsequfile=$dir/sequence$firstrunno.txt
      trainsequences[$j]=$firstrunno
      echo "  writing train-sequfile "$trainsequfile 
      echo "Sequence:        $firstrun" > $trainsequfile
      echo "Night:           $date" >> $trainsequfile
      echo "" >> $trainsequfile
      echo "CalRuns:         1" >> $trainsequfile
      echo "PedRuns:         2" >> $trainsequfile
      echo "DatRuns:         $firstrun$runsforfirst" >> $trainsequfile
      echo "" >> $trainsequfile

      testsequfile=$dir/sequence$secondrunno.txt
      testsequences[$j]=$secondrunno
      echo "  writing test-sequfile "$testsequfile 
      echo "Sequence:        $secondrun" > $testsequfile
      echo "Night:           $date" >> $testsequfile
      echo "" >> $testsequfile
      echo "CalRuns:         1" >> $testsequfile
      echo "PedRuns:         2" >> $testsequfile
      echo "DatRuns:         ${runs[@]}" >> $testsequfile
      echo "" >> $testsequfile
      
      j=$j+1
   done
done

echo "# test sequences: ${#testsequences[@]}"
echo "# train sequences: ${#trainsequences[@]}"

echo "writing dataset files $mcdataset and $mcdataset2"

echo "AnalysisNumber: 1 " > $mcdataset
echo "" >> $mcdataset
echo "SequencesOn: ${trainsequences[@]}" >> $mcdataset
echo "" >> $mcdataset
echo "SequencesOff: ${testsequences[@]}" >> $mcdataset
echo "" >> $mcdataset
echo "" >> $mcdataset

echo "AnalysisNumber: 1 " > $mcdataset2
echo "" >> $mcdataset2
echo "SequencesOn: ${testsequences[@]}" >> $mcdataset2
echo "" >> $mcdataset2
echo "" >> $mcdataset2

for (( i=0 ; i < ${#testsequences[@]} ; i++ ))
do 
   numtrain=${trainsequences[$i]}
   notrain=`echo $numtrain | cut -c 0-4`
   echo "Sequence$numtrain.File: $dir/sequence$numtrain.txt" >> $mcdataset
   echo "Sequence$numtrain.Dir:  /montecarlo/star/$notrain/$numtrain" >> $mcdataset
   echo "Sequence$numtrain.File: $dir/sequence$numtrain.txt" >> $mcdataset2
   echo "Sequence$numtrain.Dir:  /montecarlo/star/$notrain/$numtrain" >> $mcdataset2

   numtest=${testsequences[$i]}
   notest=`echo $numtest | cut -c 0-4`
   echo "Sequence$numtest.File: $dir/sequence$numtest.txt" >> $mcdataset
   echo "Sequence$numtest.Dir:  /montecarlo/star/$notrain/$numtrain" >> $mcdataset
   echo "Sequence$numtest.File: $dir/sequence$numtest.txt" >> $mcdataset2
   echo "Sequence$numtest.Dir:  /montecarlo/star/$notrain/$numtrain" >> $mcdataset2
done

# put # before the 'exit' to create rf-root-file for energy-estimation
exit

echo "creating rf-root-file for energy-estimation..."
cd $mars
logfile=$dir/trainenergy.log

root -q -b $macrospath/trainenergy.C+\("\"$mcdataset\""\,"\"$dir/\""\) | tee $logfile

