#!/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 
# - 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)
# - trainenergy  if set to 'yes', the rf for energy estimation is trained
#
# 
##############################################################################



#to be set by the user 
dir=/home/dorner/crabspectrum 
mars=/home/dorner/mars.cvs 
zdmin=5
zdmax=35
psf=14
modes=( "02" "04" ) # e.g. nowobble
numruns=5
# set here if you want to train a rf-engery-estimator
#trainenergy=
trainenergy="yes"


# begin of script
datasetdir=$dir/datasets
sequencedir=$dir/sequences
setupdir=$dir/setup
mcdir=/magic/montecarlo
mcrawdir=$mcdir/rawfiles
mcstardir=$mcdir/star
mcdataset=$datasetdir/mcdataset-test-train.txt
mcdataset2=$datasetdir/mcdataset-for-sponde.txt

# make directories 
mkdir -pv $dir
mkdir -v $sequencedir
mkdir -v $datasetdir

#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=$mcrawdir/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
      mcrawdir2=`echo $mcrawdir | sed -e 's/\//\\\\\//g'` 
      date=`echo $path | sed -e "s/$mcrawdir2\///" -e 's/\//-/g'` 
#      date=`echo $path | cut -c 22-31 | sed -e 's/\//-/g'`
      
      trainsequfile=$sequencedir/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=$sequencedir/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: $sequencedir/sequence$numtrain.txt" >> $mcdataset
   echo "Sequence$numtrain.Dir:  $mcstardir/$notrain/$numtrain" >> $mcdataset
   echo "Sequence$numtrain.File: $sequencedir/sequence$numtrain.txt" >> $mcdataset2
   echo "Sequence$numtrain.Dir:  $mcstardir/$notrain/$numtrain" >> $mcdataset2

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


if [ "$trainenergy" == "yes" ]
then 
   echo "creating rf-root-file for energy-estimation..."
   cd $mars
   logfile=$setupdir/trainenergy.log

   root -q -b $mars/datacenter/macros/trainenergy.C+\("\"$mcdataset\""\,"\"$setupdir/\""\) | tee $logfile
fi
