#!/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): Daniel Hoehne  09/2007 <mailto:hoehne@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2007
#
#
# ========================================================================
#
# This script launches the inserting of mc parameters into the db by 
# starting fillcamera.C
# 

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

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

#mccampath=$mcpath/camera
mccampath=/magic/montecarlo/camera
cameradirs=`find $mccampath -maxdepth 1 -mindepth 1 -type d`
echo "camera dirs: "${cameradirs[@]}  >> $scriptlog 2>&1
echo "" >> $scriptlog 2>&1

cd $mars

# process directories
for cameradir in ${cameradirs[@]}
do
   printprocesslog "INFO inserting information for all root camera files in $cameradir"
   cam=`basename $cameradir`
   echo "dir: "$cameradir >> $scriptlog 2>&1
   fillcamerapath=$logpath/$program
   makedir $fillcamerapath >> $scriptlog 2>&1
   fillcameralog=$fillcamerapath/$program-$cam.log
   
   check0=`root -q -b $macrospath/fillcamera.C+\("\"$cameradir\""\,kFALSE\) | tee $fillcameralog | intgrep`
   case $check0 in 
      1)   echo "check0=$check0 -> everything ok " >> $scriptlog 2>&1 
           printprocesslog "INFO fillcamera run successfully for dir $cameradir"
           ;;
      0)   echo " check1=$check1 -> no connection to db -> continue..." >> $scriptlog 2>&1 
           printprocesslog "WARN connection to DB failed"
           check="no"
           ;;
      *)   echo "check0=$check0 -> ERROR " >> $scriptlog 2>&1 
           printprocesslog "ERROR $program.C failed for dir $cameradir"
           ;;
   esac
   
   printprocesslog "INFO linking cal and ped files"
   echo "linking cal and ped files" >> $scriptlog 2>&1
   
   epo=`basename $cameradir` #filename
   pedfile=`find $cameradir/Cal_and_Ped -name *_P_*.root`
   calfile=`find $cameradir/Cal_and_Ped -name *_C_*.root`
   
   echo "calfile for epoch $epo : " $calfile >> $scriptlog 2>&1
   echo "pedfile for epoch $epo : " $pedfile >> $scriptlog 2>&1
   
   #check number of files
   numfiles=`echo $pedfile $calfile | wc -w`
   
   if [ "$numfiles" != "2" ]
   then
      echo "too many files in the directory $epoch/Cal_and_Ped -> exit" >> $scriptlog 2>&1
      rm -v $lockfile >> $scriptlog 2>&1
      exit
      printprocesslog "ERROR too many ped and cal files found in $epoch/Cal_and_Ped"
      finish >> $scriptlog 2>&1
   fi
   

   user="test"
   db="TestMonteCarlo"
   pw="Ics+eaTD"
   cquery=" select fRunNumber from MCRunProcessStatus where fFileName=\"$calfile\" "
   pquery=" select fRunNumber from MCRunProcessStatus where fFileName=\"$pedfile\" "

   crun=`mysql -ss -u $user --password=$pw --host=vela $db -e " $cquery "`
   prun=`mysql -ss -u $user --password=$pw --host=vela $db -e " $pquery "`
   cnum=`printf %08d $crun`
   pnum=`printf %08d $prun`
   echo "calrun number: " $cnum >> $scriptlog 2>&1
   echo "pedrun number: " $pnum >> $scriptlog 2>&1
   #get all directories in the linked structure for the epoch
#   dirs=`find $mcrawpath/ -mindepth 3 -maxdepth 3 -type d`
   files=`find $mcrawpath/ -lname *$epo*.root`

   for file in ${files[@]}
   do 
      dir=`dirname $file`
      date=`echo $file | cut -c 28-31,33,34,36,37`
      
      if ls $dir | grep MonteCarlo | grep $pnum
      then 
#         echo "P run already there, do nothing" >> $scriptlog 2>&1
         continue
      else 
         echo "linking P run for epoch $epo" >> $scriptlog 2>&1
         newpedfile="${dir}/${date}_${pnum}_P_MonteCarlo_E.root"
         ln -sv $pedfile $newpedfile >> $scriptlog 2>&1
      fi
      
      if ls $dir | grep MonteCarlo | grep $cnum
      then 
#         echo "C run already there, do nothing" >> $scriptlog 2>&1
         continue
      else 
         echo "linking C run for epoch $epo" >> $scriptlog 2>&1
         newcalfile="${dir}/${date}_${cnum}_C_MonteCarlo_E.root"
         ln -sv $calfile $newcalfile >> $scriptlog 2>&1
      fi

   done

done

readme=$mcrawpath/README.txt #file in which the information about the properties of the files is redirected to have always an updated explanation

# observation epochs 
epochs=("" "After January 07 (new MUX)," "From April 06 to January 07," "Before April 06")

# print information and explanation of structure into README.txt 
date >| $readme 2>&1
echo "" >> $readme 2>&1
echo "Explanation for the structure in which the mc files are linked" >> $readme 2>&1
echo "--------------------------------------------------------------" >> $readme 2>&1
echo "" >> $readme 2>&1
echo "the files are linked in a YYYY/MM/DD structure like the data files" >> $readme 2>&1
echo "YYYY represents 19zbin" >> $readme 2>&1
echo "MM represents the epoch" >> $readme 2>&1
echo "DD represents the psf in mm" >> $readme 2>&1
echo "" >> $readme 2>&1
echo "explanation of the epochs" >> $readme 2>&1
echo "epochs: "${epochs[@]} >> $readme 2>&1
echo "" >> $readme 2>&1
for (( i=1 ; i <= 3 ; i++ )) 
do 
   if [ "${epochs[i]}" != "" ]
   then 
      numofepoch=`printf %02d $i`
      echo "epoch (MM) = $numofepoch means ${epochs[$i]}" >> $readme 2>&1
   fi
done
echo "" >> $readme 2>&1
echo "the epoch is indicating " >> $readme 2>&1
echo " - the time for which the MCs are produced" >> $readme 2>&1
echo "   the epoch is determined from the AmplFADCs value filled into the MC-DB" >> $readme 2>&1
echo "" >> $readme 2>&1
echo "----------------" >> $readme 2>&1
echo "" >> $readme 2>&1
echo "The names of the files characterize their properties. Structure:" >> $readme 2>&1
echo "/magic/montecarlo/rawfiles/YYYY/MM/DD/YYYYMMDD_<RunNumber>_<P/C/D>_<Particle><Spectrum><ObservationMode>_E.root" >> $readme 2>&1
echo "<P/C/D> indicates the run type" >> $readme 2>&1
echo "<Particle>: Gamma, Proton, Muon, MonteCarlo (for P,C runs)" >> $readme 2>&1
echo "<Spectrum>: empty or HE, depending on the simulated MC spectrum" >> $readme 2>&1
echo "<ObservationMode>: empty (On), W1/W2 (Wobble), FW1/FW2 (Fake Wobble), Diff (Diffuse)" >> $readme 2>&1
echo "" >> $readme 2>&1
echo "Run numbers for Cal and Ped files are linked depending on the epoch:" >> $readme 2>&1
echo "MC_old:           P=1, C=2" >> $readme 2>&1
echo "MC_up_to_April06: P=1, C=2" >> $readme 2>&1
echo "MC_April_May06:   P=3, C=4" >> $readme 2>&1
echo "MC_post_June06:   P=3, C=4" >> $readme 2>&1
echo "MC_MuxFADCs:      P=5, C=6" >> $readme 2>&1
echo "MC_MuxFADCs_NEW:  P=5, C=6" >> $readme 2>&1
echo "" >> $readme 2>&1


finish >> $scriptlog 2>&1

