#!/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): Daniel Hoehne-Moench  01/2009 <mailto:hoehne@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2009
#
#
# ========================================================================
#
# 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

# check if script is already running
lockfile=$lockpath/lock-$program.txt
checklock 
                                              
# 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"

getdbsetup

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
   printprocesslog "INFO dir: "$dir 
   
   epochs=`ls -l $mcpath/camera | cut -c 52-80`
   for epoch in ${epochs[@]}
   do
      printprocesslog "INFO epoch: "$epoch 
      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" "GammaSUM" "GammaW1SUM" "GammaW2SUM" "GammaFWSUM" "GammaDiffSUM" "GammaHESUM" "GammaHEW1SUM" "GammaHEW2SUM" "GammaHEFWSUM" "GammaHEDiffSUM" "ProtonDiffSUM" "ProtonHEDiffSUM" "MuonDiffSUM" "MuonHEDiffSUM")
      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 $us --password=$pw --host=$ho $db -e " $trainupdate "
            done   

            trainquery1=" select fSpectrumKEY, fPSFKEY, fAmplFadcKEY, fAtmosphericModelKEY, fObservationModeKEY, fMCParticleKEY, fReflectorVersionKEY, fCorsikaVersionKEY, fCameraVersionKEY, fViewConeAngleOKEY, fAddSpotSizeKEY, fTriggerFlagKEY from MCRunData where fRunNumber=\"$firstruntrain\" "
            trainquery2=" select sum(fNumEvents), sum(fNumTriggers), sum(fNumSumTriggers) from MCRunData where fRunNumber in ($trainruns2) "
            trainvalues=`mysql -ss -u $us --password=$pw --host=$ho $db -e " $trainquery1 "`
            trainvalues2=`mysql -ss -u $us --password=$pw --host=$ho $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`
            traintrig=`echo $trainvalues | cut -d" " -f12`
            trainevents=`echo $trainvalues2 | cut -d" " -f1`
            traintriggers=`echo $trainvalues2 | cut -d" " -f2`
            trainsumtriggers=`echo $trainvalues2 | cut -d" " -f3`
   	    trainquery3=" select fSequenceFirst from MCSequenceProcessStatus where fSequenceFirst=\"$firstruntrain\" "
   	    trainquery4=" select fSequenceFirst from MCSequences where fSequenceFirst=\"$firstruntrain\" "
   	    traincheck3=`mysql -ss -u $us --password=$pw --host=$ho $db -e " $trainquery3 "`
   	    traincheck4=`mysql -ss -u $us --password=$pw --host=$ho $db -e " $trainquery4 "`
   	    traininsert1=" insert MCSequenceProcessStatus set fSequenceFirst=\"$firstruntrain\", fPriority=\"$firstruntrain\", fSequenceFileWritten=Now() "
   	    traininsert2=" insert MCSequences set fSequenceFirst=\"$firstruntrain\", fRunStart=\"$date\", fNumEvents=\"$trainevents\", fNumTriggers=\"$traintriggers\", fNumSumTriggers=\"$trainsumtriggers\", fZBin=\"$zbin\", fSpectrumKEY=\"$trainspec\", fPSFKEY=\"$trainpsf\", fAddSpotSizeKEY=\"$trainass\", fAmplFadcKEY=\"$trainamplfadc\", fAtmosphericModelKEY=\"$trainatmo\", fObservationModeKEY=\"$trainobs\", fMCParticleKEY=\"$trainpart\", fReflectorVersionKEY=\"$trainrefl\", fCorsikaVersionKEY=\"$traincors\", fCameraVersionKEY=\"$traincam\", fViewConeAngleOKEY=\"$trainview\", fTriggerFlagKEY=\"$traintrig\", fTestTrainKEY=2 "
            trainupdate2=" update MCSequences set fRunStart=\"$date\", fNumEvents=\"$trainevents\", fNumTriggers=\"$traintriggers\", fNumSumTriggers=\"$trainsumtriggers\", fZBin=\"$zbin\", fSpectrumKEY=\"$trainspec\", fPSFKEY=\"$trainpsf\", fAddSpotSizeKEY=\"$trainass\", fAmplFadcKEY=\"$trainamplfadc\", fAtmosphericModelKEY=\"$trainatmo\", fObservationModeKEY=\"$trainobs\", fMCParticleKEY=\"$trainpart\", fReflectorVersionKEY=\"$trainrefl\", fCorsikaVersionKEY=\"$traincors\", fCameraVersionKEY=\"$traincam\", fViewConeAngleOKEY=\"$trainview\", fTriggerFlagKEY=\"$traintrig\", fTestTrainKEY=2 where fSequenceFirst=\"$firstruntrain\" "
            if [ "$traincheck3" == "" ]
            then
               mysql -ss -u $us --password=$pw --host=$ho $db -e " $traininsert1 "
            fi
            if [ "$traincheck4" == "" ]
            then
   	       mysql -ss -u $us --password=$pw --host=$ho $db -e " $traininsert2 "
            else
   	       mysql -ss -u $us --password=$pw --host=$ho $db -e " $trainupdate2 "
            fi
            
            trainno=`echo $firstruntrain | cut -c 0-4`
            trainsequpath=$mcsequpath/$trainno
            makedir $trainsequpath 
            trainsequfile=$trainsequpath/sequence$firstruntrain.txt
            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 $us --password=$pw --host=$ho $db -e " $testupdate "
            done   

            testquery1=" select fSpectrumKEY, fPSFKEY, fAmplFadcKEY, fAtmosphericModelKEY, fObservationModeKEY, fMCParticleKEY, fReflectorVersionKEY, fCorsikaVersionKEY, fCameraVersionKEY, fViewConeAngleOKEY, fAddSpotSizeKEY, fTriggerFlagKEY from MCRunData where fRunNumber=\"$firstruntest\" "
            testquery2=" select sum(fNumEvents), sum(fNumTriggers), sum(fNumSumTriggers) from MCRunData where fRunNumber in ($testruns2) "
            testvalues=`mysql -ss -u $us --password=$pw --host=$ho $db -e " $testquery1 "`
            testvalues2=`mysql -ss -u $us --password=$pw --host=$ho $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`
            testtrig=`echo $testvalues | cut -d" " -f12`
            testevents=`echo $testvalues2 | cut -d" " -f1`
            testtriggers=`echo $testvalues2 | cut -d" " -f2`
            testsumtriggers=`echo $testvalues2 | cut -d" " -f3`
   	    testquery3=" select fSequenceFirst from MCSequenceProcessStatus where fSequenceFirst=\"$firstruntest\" "
   	    testquery4=" select fSequenceFirst from MCSequences where fSequenceFirst=\"$firstruntest\" "
   	    testcheck3=`mysql -ss -u $us --password=$pw --host=$ho $db -e " $testquery3 "`
   	    testcheck4=`mysql -ss -u $us --password=$pw --host=$ho $db -e " $testquery4 "`
   	    testinsert1=" insert MCSequenceProcessStatus set fSequenceFirst=\"$firstruntest\", fPriority=\"$firstruntest\", fSequenceFileWritten=Now() "
   	    testinsert2=" insert MCSequences set fSequenceFirst=\"$firstruntest\", fRunStart=\"$date\", fNumEvents=\"$testevents\", fNumTriggers=\"$testtriggers\", fNumSumTriggers=\"$testsumtriggers\", fZBin=\"$zbin\", fSpectrumKEY=\"$testspec\", fPSFKEY=\"$testpsf\", fAddSpotSizeKEY=\"$testass\", fAmplFadcKEY=\"$testamplfadc\", fAtmosphericModelKEY=\"$testatmo\", fObservationModeKEY=\"$testobs\", fMCParticleKEY=\"$testpart\", fReflectorVersionKEY=\"$testrefl\", fCorsikaVersionKEY=\"$testcors\", fCameraVersionKEY=\"$testcam\", fViewConeAngleOKEY=\"$testview\", fTriggerFlagKEY=\"$testtrig\", fTestTrainKEY=3 "
            testupdate2=" update MCSequences set fRunStart=\"$date\", fNumEvents=\"$testevents\", fNumTriggers=\"$testtriggers\", fNumSumTriggers=\"$testsumtriggers\", fZBin=\"$zbin\", fSpectrumKEY=\"$testspec\", fPSFKEY=\"$testpsf\", fAddSpotSizeKEY=\"$testass\", fAmplFadcKEY=\"$testamplfadc\", fAtmosphericModelKEY=\"$testatmo\", fObservationModeKEY=\"$testobs\", fMCParticleKEY=\"$testpart\", fReflectorVersionKEY=\"$testrefl\", fCorsikaVersionKEY=\"$testcors\", fCameraVersionKEY=\"$testcam\", fViewConeAngleOKEY=\"$testview\", fTriggerFlagKEY=\"$testtrig\", fTestTrainKEY=3 where fSequenceFirst=\"$firstruntest\" "
            if [ "$testcheck3" == "" ]
            then
               mysql -ss -u $us --password=$pw --host=$ho $db -e " $testinsert1 "
            fi
            if [ "$testcheck4" == "" ]
            then
   	       mysql -ss -u $us --password=$pw --host=$ho $db -e " $testinsert2 "
            else
   	       mysql -ss -u $us --password=$pw --host=$ho $db -e " $testupdate2 "
            fi
            
            testno=`echo $firstruntest | cut -c 0-4`
            testsequpath=$mcsequpath/$testno
            makedir $testsequpath 
            testsequfile=$testsequpath/sequence$firstruntest.txt
            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 

