#!/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  12/2005 <mailto:dorner@astro.uni-wuerzburg.de>
#   Author(s): Daniela Dorner  06/2008 <mailto:hoehne@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2008
#
#
# ========================================================================
#
# The script is building two sequences per directory and epoch. One for
# training and one for testing. The relevant parameters are taken from
# the MC database.
# 

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
program=mcsequences

set -C

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

# check if script is already running
lockfile=$lockpath/lock-$program.txt
checklock  >> $scriptlog 2>&1
                                              
# find montecarlo directories, build two sequences per directory and write sequence files
printprocesslog "INFO building two sequences per mcdirectory and epoch, one for training and one for testing"

#user="test"
#db="TestMonteCarlo"
#pw="Ics+eaTD"
user="operator"
db="MyMagicMonteCarlo"
pw="Ics+eMM!"

dirs=`find $mcrawpath -type d`
for dir in ${dirs[@]}
do 
   cont=`echo $dir | cut -d/ -f7`
   #cont=`echo $dir | cut -d/ -f10`
   if [ "$cont" == "" ]
   then
      continue
   fi
   echo $dir >> $scriptlog 2>&1
   
   epochs=`ls -l $mcpath/camera | cut -c 52-80`
   for epoch in ${epochs[@]}
   do
      echo $epoch >> $scriptlog 2>&1
      calfile=`find $dir -lname *${epoch}/*.root | grep "_C_"`
      pedfile=`find $dir -lname *${epoch}/*.root | grep "_P_"`
      
      calrun=`echo $calfile | cut -d_ -f2`
      pedrun=`echo $pedfile | cut -d_ -f2`
      
      modes=("Gamma" "GammaW1" "GammaW2" "GammaFW" "GammaDiff" "GammaHE" "GammaHEW1" "GammaHEW2" "GammaHEFW" "GammaHEDiff" "ProtonDiff" "ProtonHEDiff" "MuonDiff" "MuonHEDiff")
      for mode in ${modes[@]}
      do
         datruns=`find $dir -lname *${epoch}/*.root | grep ${mode}_ | grep "_D_" | cut -d_ -f2 | tr "\n" " "`
      	 if [ "$datruns" != "" ]
         then
            num=`echo $datruns | wc -w`
            train=`expr $num / 5`
            test=`expr $num - $train`
            last=`expr $num \* 9`
            train2=`expr $train \* 9`
      	    firstrun=`echo $datruns | cut -c 0-8`
            trainruns=`echo $datruns | cut -c 0-$train2`
            trainruns2=`echo $trainruns | sed -e 's/ /,/g'`
            testruns=`echo $datruns | cut -c $train2-$last`
            testruns2=`echo $testruns | sed -e 's/ /,/g'`
      	    firstruntrain=`echo $trainruns | cut -c 0-8`
      	    firstruntest=`echo $testruns | cut -c 0-8`

            date=`echo $dir | cut -c 28-37 | sed -e 's/\//-/g'`
            #date=`echo $dir | cut -c 57-66 | sed -e 's/\//-/g'`
            zbin=`echo $date | cut -c 3,4`


   	    #training runs
            for trainrun in ${trainruns[@]}
            do
               trainupdate=" update MCRunData set fSequenceFirst=\"$firstruntrain\" where fRunNumber=\"$trainrun\" "
   	       mysql -ss -u $user --password=$pw --host=vela $db -e " $trainupdate "
            done   

            trainquery1=" select fSpectrumKEY, fPSFKEY, fAmplFadcKEY, fAtmosphericModelKEY, fObservationModeKEY, fMCParticleKEY, fReflectorVersionKEY, fCorsikaVersionKEY, fCameraVersionKEY, fViewConeAngleOKEY, fAddSpotSizeKEY from MCRunData where fRunNumber=\"$firstruntrain\" "
            trainquery2=" select sum(fNumEvents), sum(fNumtriggers) from MCRunData where fRunNumber in ($trainruns2) "
            trainvalues=`mysql -ss -u $user --password=$pw --host=vela $db -e " $trainquery1 "`
            trainvalues2=`mysql -ss -u $user --password=$pw --host=vela $db -e " $trainquery2 "`
            trainspec=`echo $trainvalues | cut -d" " -f1`
            trainpsf=`echo $trainvalues | cut -d" " -f2`
            trainamplfadc=`echo $trainvalues | cut -d" " -f3`
            traintrainatmo=`echo $trainvalues | cut -d" " -f4`
            trainobs=`echo $trainvalues | cut -d" " -f5`
            trainpart=`echo $trainvalues | cut -d" " -f6`
            trainrefl=`echo $trainvalues | cut -d" " -f7`
            traincors=`echo $trainvalues | cut -d" " -f8`
            traincam=`echo $trainvalues | cut -d" " -f9`
            trainview=`echo $trainvalues | cut -d" " -f10`
            trainass=`echo $trainvalues | cut -d" " -f11`
            trainevents=`echo $trainvalues2 | cut -d" " -f1`
            traintriggers=`echo $trainvalues2 | cut -d" " -f2`
   	    trainquery3=" select fSequenceFirst from MCSequenceProcessStatus where fSequenceFirst=\"$firstruntrain\" "
   	    trainquery4=" select fSequenceFirst from MCSequences where fSequenceFirst=\"$firstruntrain\" "
   	    traincheck3=`mysql -ss -u $user --password=$pw --host=vela $db -e " $trainquery3 "`
   	    traincheck4=`mysql -ss -u $user --password=$pw --host=vela $db -e " $trainquery4 "`
   	    traininsert1=" insert MCSequenceProcessStatus set fSequenceFirst=\"$firstruntrain\", fSequenceFileWritten=Now() "
   	    traininsert2=" insert MCSequences set fSequenceFirst=\"$firstruntrain\", fRunStart=\"$date\", fNumEvents=\"$trainevents\", fNumTriggers=\"$traintriggers\", fZBin=\"$zbin\", fSpectrumKEY=\"$trainspec\", fPSFKEY=\"$trainpsf\", fAddSpotSizeKEY=\"$trainass\", fAmplFadcKEY=\"$trainamplfadc\", fAtmosphericModelKEY=\"$trainatmo\", fObservationModeKEY=\"$trainobs\", fMCParticleKEY=\"$trainpart\", fReflectorVersionKEY=\"$trainrefl\", fCorsikaVersionKEY=\"$traincors\", fCameraVersionKEY=\"$traincam\", fViewConeAngleOKEY=\"$trainview\", fTestTrainKEY=2 "
            trainupdate2=" update MCSequences set fRunStart=\"$date\", fNumEvents=\"$trainevents\", fNumTriggers=\"$traintriggers\", fZBin=\"$zbin\", fSpectrumKEY=\"$trainspec\", fPSFKEY=\"$trainpsf\", fAddSpotSizeKEY=\"$trainass\", fAmplFadcKEY=\"$trainamplfadc\", fAtmosphericModelKEY=\"$trainatmo\", fObservationModeKEY=\"$trainobs\", fMCParticleKEY=\"$trainpart\", fReflectorVersionKEY=\"$trainrefl\", fCorsikaVersionKEY=\"$traincors\", fCameraVersionKEY=\"$traincam\", fViewConeAngleOKEY=\"$trainview\", fTestTrainKEY=2 where fSequenceFirst=\"$firstruntrain\" "
            if [ "$traincheck3" == "" ]
            then
               mysql -ss -u $user --password=$pw --host=vela $db -e " $traininsert1 "
            fi
            if [ "$traincheck4" == "" ]
            then
   	       mysql -ss -u $user --password=$pw --host=vela $db -e " $traininsert2 "
            else
   	       mysql -ss -u $user --password=$pw --host=vela $db -e " $trainupdate2 "
            fi
            
            trainno=`echo $firstruntrain | cut -c 0-4`
            trainsequpath=$mcsequpath/$trainno
            makedir $trainsequpath >> $scriptlog 2>&1
            trainsequfile=$trainsequpath/sequence$firstruntrain.txt
            echo "writing train sequfile "$trainsequfile >> $scriptlog 2>&1
            printprocesslog "INFO writing train sequencefile $trainsequfile"
   
            echo "Sequence:        $firstruntrain" >| $trainsequfile
            echo "Night:           $date" >> $trainsequfile
            echo "Epoch:           $epoch" >> $trainsequfile
            echo "Mode:            $mode" >> $trainsequfile
            echo "" >> $trainsequfile
            echo "CalRuns:         $calrun" >> $trainsequfile
            echo "PedRuns:         $pedrun" >> $trainsequfile
            echo "DatRuns:         $trainruns" >> $trainsequfile
 	    echo "" >> $trainsequfile
            echo "MonteCarlo: Yes" >> $trainsequfile
   	    echo "Training sequence" >> $trainsequfile
   	    echo "" >> $trainsequfile


            #test runs
            for testrun in ${testruns[@]}
            do
               testupdate=" update MCRunData set fSequenceFirst=\"$firstruntest\" where fRunNumber=\"$testrun\" "
   	       mysql -ss -u $user --password=$pw --host=vela $db -e " $testupdate "
            done   

            testquery1=" select fSpectrumKEY, fPSFKEY, fAmplFadcKEY, fAtmosphericModelKEY, fObservationModeKEY, fMCParticleKEY, fReflectorVersionKEY, fCorsikaVersionKEY, fCameraVersionKEY, fViewConeAngleOKEY, fAddSpotSizeKEY from MCRunData where fRunNumber=\"$firstruntest\" "
            testquery2=" select sum(fNumEvents), sum(fNumtriggers) from MCRunData where fRunNumber in ($testruns2) "
            testvalues=`mysql -ss -u $user --password=$pw --host=vela $db -e " $testquery1 "`
            testvalues2=`mysql -ss -u $user --password=$pw --host=vela $db -e " $testquery2 "`
            testspec=`echo $testvalues | cut -d" " -f1`
            testpsf=`echo $testvalues | cut -d" " -f2`
            testamplfadc=`echo $testvalues | cut -d" " -f3`
            testatmo=`echo $testvalues | cut -d" " -f4`
            testobs=`echo $testvalues | cut -d" " -f5`
            testpart=`echo $testvalues | cut -d" " -f6`
            testrefl=`echo $testvalues | cut -d" " -f7`
            testcors=`echo $testvalues | cut -d" " -f8`
            testcam=`echo $testvalues | cut -d" " -f9`
            testview=`echo $testvalues | cut -d" " -f10`
            testass=`echo $testvalues | cut -d" " -f11`
            testevents=`echo $testvalues2 | cut -d" " -f1`
            testtriggers=`echo $testvalues2 | cut -d" " -f2`
   	    testquery3=" select fSequenceFirst from MCSequenceProcessStatus where fSequenceFirst=\"$firstruntest\" "
   	    testquery4=" select fSequenceFirst from MCSequences where fSequenceFirst=\"$firstruntest\" "
   	    testcheck3=`mysql -ss -u $user --password=$pw --host=vela $db -e " $testquery3 "`
   	    testcheck4=`mysql -ss -u $user --password=$pw --host=vela $db -e " $testquery4 "`
   	    testinsert1=" insert MCSequenceProcessStatus set fSequenceFirst=\"$firstruntest\", fSequenceFileWritten=Now() "
   	    testinsert2=" insert MCSequences set fSequenceFirst=\"$firstruntest\", fRunStart=\"$date\", fNumEvents=\"$testevents\", fNumTriggers=\"$testtriggers\", fZBin=\"$zbin\", fSpectrumKEY=\"$testspec\", fPSFKEY=\"$testpsf\", fAddSpotSizeKEY=\"$testass\", fAmplFadcKEY=\"$testamplfadc\", fAtmosphericModelKEY=\"$testatmo\", fObservationModeKEY=\"$testobs\", fMCParticleKEY=\"$testpart\", fReflectorVersionKEY=\"$testrefl\", fCorsikaVersionKEY=\"$testcors\", fCameraVersionKEY=\"$testcam\", fViewConeAngleOKEY=\"$testview\", fTestTrainKEY=3 "
            testupdate2=" update MCSequences set fRunStart=\"$date\", fNumEvents=\"$testevents\", fNumTriggers=\"$testtriggers\", fZBin=\"$zbin\", fSpectrumKEY=\"$testspec\", fPSFKEY=\"$testpsf\", fAddSpotSizeKEY=\"$testass\", fAmplFadcKEY=\"$testamplfadc\", fAtmosphericModelKEY=\"$testatmo\", fObservationModeKEY=\"$testobs\", fMCParticleKEY=\"$testpart\", fReflectorVersionKEY=\"$testrefl\", fCorsikaVersionKEY=\"$testcors\", fCameraVersionKEY=\"$testcam\", fViewConeAngleOKEY=\"$testview\", fTestTrainKEY=3 where fSequenceFirst=\"$firstruntest\" "
            if [ "$testcheck3" == "" ]
            then
               mysql -ss -u $user --password=$pw --host=vela $db -e " $testinsert1 "
            fi
            if [ "$testcheck4" == "" ]
            then
   	       mysql -ss -u $user --password=$pw --host=vela $db -e " $testinsert2 "
            else
   	       mysql -ss -u $user --password=$pw --host=vela $db -e " $testupdate2 "
            fi
            
            testno=`echo $firstruntest | cut -c 0-4`
            testsequpath=$mcsequpath/$testno
            makedir $testsequpath >> $scriptlog 2>&1
            testsequfile=$testsequpath/sequence$firstruntest.txt
            echo "writing test sequfile "$testsequfile >> $scriptlog 2>&1
            printprocesslog "INFO writing test sequencefile $testsequfile"
   
            echo "Sequence:        $firstruntest" >| $testsequfile
            echo "Night:           $date" >> $testsequfile
            echo "Epoch:           $epoch" >> $testsequfile
            echo "Mode:            $mode" >> $testsequfile
            echo "" >> $testsequfile
            echo "CalRuns:         $calrun" >> $testsequfile
            echo "PedRuns:         $pedrun" >> $testsequfile
            echo "DatRuns:         $testruns" >> $testsequfile
 	    echo "" >> $testsequfile
            echo "MonteCarlo: Yes" >> $testsequfile
   	    echo "Test sequence" >> $testsequfile
   	    echo "" >> $testsequfile
         fi
      done
   done
done

finish >> $scriptlog 2>&1

