Index: trunk/MagicSoft/Mars/datacenter/scripts/linkmc
===================================================================
--- trunk/MagicSoft/Mars/datacenter/scripts/linkmc	(revision 8482)
+++ trunk/MagicSoft/Mars/datacenter/scripts/linkmc	(revision 8602)
@@ -19,4 +19,5 @@
 #
 #   Author(s): Daniela Dorner  12/2005 <mailto:dorner@astro.uni-wuerzburg.de>
+#   Author(s): Daniel Hoehne   06/2007 <mailto:hoehne@astro.uni-wuerzburg.de>
 #
 #   Copyright: MAGIC Software Development, 2000-2007
@@ -28,4 +29,6 @@
 # to a structure from which the files can be processed more easily with the
 # automatic analysis. 
+# Furthermore the content of the files is filled in the database running
+# fillcamera.C.
 # This script is not yet running automatically. 
 #
@@ -38,7 +41,10 @@
 # /magic/montecarlo/rawfiles/README.txt
 #
-# First the data files are linked and then in each new directory also the 
-# pedestal and calibration file is linked. 
-#
+# First a file is linked and then fillcamera.C is executed. In case of an 
+# error in fillcamera.C the link is removed.
+# In each new directory also the pedestal and calibration file is linked. 
+#
+# ========================================================================
+
 
 source `dirname $0`/sourcefile
@@ -48,5 +54,5 @@
 set -C
 
-scriptlog=$runlogpath/$progam-`date +%F`.log
+scriptlog=$runlogpath/$program-`date +%F`.log
 date >> $scriptlog 2>&1
 
@@ -70,6 +76,6 @@
 
 # observation modes 
-modes=("" "Gammawobble+" "Gammanowobble0" "GammawobbleHE+" "GammanowobbleHE0" "Gammawobble0" "GammawobbleHE0" "Gammadiffuse0" "Protonnowobble0" )
-#be carful: 
+modes=("" "Gammawobble+" "Gammanowobble0" "GammawobbleHE+" "GammanowobbleHE0" "Gammawobble0" "GammawobbleHE0" "Gammadiffuse0" "Protonnowobble0" "ProtonnowobbleHE0")
+# be careful: 
 # w- not yet foreseen in this script
 
@@ -107,18 +113,23 @@
 
 
-#get runnumber
+
 runno=`cat $next`
 
 #get files, which have to be linked
-camfiles=`find $mccampath -type f | grep -v Cal_and_Ped`
+camfiles=`find $mccampath -type f | grep -v Cal_and_Ped | sort`
 
 printprocesslog "INFO linking new camerafiles starting with runno $runno"
 for camfile in ${camfiles[@]}
 do 
+
+
    #continue, if file is already linked 
-   if grep $camfile $processed >> $scriptlog 2>&1
+   if find $mcpath/* -lname '$mccampath*.root' -printf %l'\n' | grep $camfile >> $scriptlog 2>&1
+#   if grep $camfile $processed >> $scriptlog 2>&1
    then 
       continue
    fi
+
+
    printprocesslog "INFO linking $file"
    file=`basename $camfile` #filename
@@ -129,8 +140,8 @@
    wobble=`echo $file | cut -d_ -f6 | cut -c 2` #mode from filename
    particle=`echo $file | cut -d_ -f1` #particle type from filename
-   psf=`echo $camfile | cut -d/ -f5 | cut -c 6,8` #psf from path
-   
-   particledir=`echo $camfile | cut -d/ -f4` #particletype from path
-   wobbledir=`echo $camfile | cut -d/ -f6` #mode from path
+   psf=`echo $camfile | cut -d/ -f6 | cut -c 6,8` #psf from path
+   
+   particledir=`echo $camfile | cut -d/ -f5` #particletype from path
+   wobbledir=`echo $camfile | cut -d/ -f7` #mode from path
    
    #build mode name
@@ -167,8 +178,34 @@
    echo $runno >| $next 
 
+   linkmclogpath=$logpath/$program/$no2
+   makedir $linkmclogpath >> $scriptlog 2>&1
+   linkmclog=$linkmclogpath/$program-$no2.log
+
    #link file
+   echo "linking file $camfile"
    ln -sv $camfile $newfile >> $scriptlog 2>&1 
-   #add filename to processed file
-   echo $camfile >> $processed
+
+   # run fillcamera.C. In case of success the camfile is written to .processed. In case of an error the link is removed
+   echo "run fillcamera for mc run $no2" >> $scriptlog 2>&1
+   check2=`root -q -b $macrospath/fillcamera.C+\("\"$newfile\""\,kTRUE\) | tee $linkmclog | intgrep`
+   case $check2 in
+      1)   echo " check2=$check2 -> everything ok " >> $scriptlog 2>&1 
+           printprocesslog "INFO done fillcamera successfully for mc run $no2"
+           # add filename to .processed
+           echo $camfile >> $processed
+           ;;
+      0)   echo " check2=$check2 -> no connection to db -> continue..." >> $scriptlog 2>&1 
+           printprocesslog "WARN connection to DB failed, removing link"
+           check="no"
+           # remove link
+           rm $newfile >> $scriptlog 2>&1
+           ;;
+      *)   echo " check2=$check2 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
+           printprocesslog "ERROR fillcamera failed for mc run $no2, removing link"
+           check=$check2
+           # remove link
+           rm $newfile >> $scriptlog 2>&1
+           ;;
+   esac
 
 done
@@ -178,5 +215,9 @@
 #get files
 pedfile=`find $mccampath/Cal_and_Ped -name *_P_*.root`
+
+
 calfile=`find $mccampath/Cal_and_Ped -name *_C_*.root`
+
+
 echo "calfile"$calfile >> $scriptlog 2>&1
 echo "pedfile"$pedfile >> $scriptlog 2>&1
@@ -186,4 +227,8 @@
 then 
    "too many files in the directory $mccampath/Cal_and_Ped -> exit" >> $scriptlog 2>&1
+
+
+   rm -v $lockfile >> $scriptlog 2>&1
+   exit
    printprocesslog "ERROR too many ped and cal files found in $mccampath/Cal_and_Ped"
    finish >> $scriptlog 2>&1
@@ -196,5 +241,5 @@
 do 
    #continue, if directory has already linked C and P run
-   cont=`ls $dir/*_0000[12]_[CP]_MonteCarlo_E.root 2>/dev/null | wc -w`
+   cont=`ls $dir/*_0000000[12]_[CP]_MonteCarlo_E.root 2>/dev/null | wc -w`
    if [ "$cont" == "2" ] >> $scriptlog 2>&1
    then 
