Index: trunk/MagicSoft/Mars/datacenter/scripts/jobmanager
===================================================================
--- trunk/MagicSoft/Mars/datacenter/scripts/jobmanager	(revision 9562)
+++ trunk/MagicSoft/Mars/datacenter/scripts/jobmanager	(revision 9585)
@@ -37,5 +37,5 @@
 function nextscript()
 {
-   echo `date +%F\ %T`" sleeping \$$1 = $sleeptime seconds... " >> $jmscriptlog 2>&1
+   echo `date +%F\ %T`" sleeping \$$1 = $2 seconds... " >> $jmscriptlog 2>&1
    sleep $2
    echo "" >> $jmscriptlog 2>&1
@@ -88,5 +88,5 @@
 
       # check if there's something to do
-      column=${scriptscolname[$i]}
+      step=${scriptscolname[$i]}
       getstepinfo
       if [ "$noderestricted" = "yes" ]
@@ -126,5 +126,5 @@
       
       # check number of processes to be done
-      echo `date +%F\ %T`" Database: $numproc ${scripts[$i]} still to be done (incl. idle jobs) [DB/table/column $db/$table/$column]" >> $jmscriptlog 2>&1
+      echo `date +%F\ %T`" Database: $numproc ${scripts[$i]} still to be done (incl. idle jobs) [DB/step $db/$step]" >> $jmscriptlog 2>&1
       if [ "$numproc" = "0" ]
       then 
Index: trunk/MagicSoft/Mars/datacenter/scripts/runcorsika
===================================================================
--- trunk/MagicSoft/Mars/datacenter/scripts/runcorsika	(revision 9562)
+++ trunk/MagicSoft/Mars/datacenter/scripts/runcorsika	(revision 9585)
@@ -18,7 +18,7 @@
 #
 #
-#   Author(s): Daniela Dorner  05/2007 <mailto:dorner@astro.uni-wuerzburg.de>
-#
-#   Copyright: MAGIC Software Development, 2000-2008
+#   Author(s): Daniela Dorner  05/2007 <mailto:daniela.dorner@unige.ch>
+#
+#   Copyright: MAGIC Software Development, 2000-2010
 #
 #
@@ -31,5 +31,5 @@
 printprocesslog "INFO starting $0"
 program=corsika
-column=fCorsikaFileAvail
+step=Corsika
 
 set -C
@@ -41,5 +41,5 @@
 {
    # Run number
-   echo "RUNNR "$corsikarunno
+   echo "RUNNR "$file
    # Starting event number
    echo "EVTNR 1"
@@ -60,17 +60,28 @@
    echo "FIXCHI "$startalt
    # Observation level, magnetic field, atmosphere
-   echo "OBSLEV 2200.E2"
+   echo "OBSLEV "$obslev".E2"
    # From www.noaa.gov/geomagmodels roughly for January 2009. (was 29.5 and 23.0)
    # MAGNET BX BZ (BX is the horizontal component (muT) to the x-direction of
    # the detector, BZ is the vertical component (muT) downwards)
-   echo "MAGNET 30.3 24.1"
+   echo "MAGNET "$magnet
    # Rotation angle between array x-direction and magnetic north direction
    # positive if array x-direction points to the west
-   echo "ARRANG -7.0"
+   echo "ARRANG "$magnetrot
    # Atmosphere (model, true/false for atmospheric refraction of cer-photons)
    # Atmosphere read from atmprof11.dat
    # My current understanding is that only refraction (bending of light
    # but no absorption of photons is taken into account) setting T.
-   echo "ATMOSPHERE 11 T"
+   echo "ATMOSPHERE "$atm" T"
+
+   # Current boundaries in reflector program
+   # echo "ATMLAY 400000 1000000 4000000 10000000"
+
+   # Boundaries as determined by CORSIKA
+   # Until we can read the result from the corsika fit we fix the
+   # values which come out of the fit. This is not ideal but forces
+   # things to be consistent. Be careful is using a different model -
+   # the fit might not be ideal.
+   echo "ATMLAY 775000 1650000 5000000 10500000"
+
    # Other parameters
    echo "RADNKG 200.E2"
@@ -82,11 +93,25 @@
    # Enable muon multiple scattering
    echo "MUMULT T"
-   # Cherenkov telescope options
-   echo "CWAVLG 290. 600."
-   echo "CSCAT "$reuse" 0. "$impactmax
+   # Wavelength range (was originally 600)
+   echo "CWAVLG "$wavelengths
+   # MMCS
+   echo "CSCAT 1 0. "$impactmax
+   # CORSIKA
+   # echo "CSCAT "$reuse" "$impactmax" 0."
    echo "CERSIZ 1."
+   # Write Cherenkov photons to cer-file
    echo "CERFIL T"
+   # Number of telescopes
    echo "CERTEL 1"
-   echo " 0. 0. 0. 0. 0. "$diameter" 1700."
+   # MMCS
+   echo " 0. 0. 0. 0. 0. "$diameter" "$diameter
+   # CORSIKA (x, y, z, r)
+   # echo "TELESCOPE 0. 0. 0. "$diameter
+   # This keyword has no relevance but allows to store the 
+   # maximum simulated impact parameter. The "magic" is that
+   # ceres interpretes the third and fourth argument as
+   # maximum simulated impact if they are identical and
+   # the other four arguments are 1
+   # echo "CERARY 1 1 "$diameter" "$diameter" 1 1"
    # How the longitudinal shower development is sampled
    # echo "LONGI T 10. T F"
@@ -103,5 +128,9 @@
    # Output path
    echo "DIRECT "$outpath
-   # User and host (for convinience)
+   # DIRECT /dev/null
+   # TELFIL filename
+   # TELFIL | gzip
+   # TELESCOPE X Y Z R
+   # User and host (for convenience)
    echo "USER "`whoami`
    echo "HOST "`hostname`
@@ -114,74 +143,69 @@
 gettodo "1" 
 run=${primaries[0]}
-# get corsika runno
-query="SELECT fCorsikaRunNumber FROM MCRunData where fMCRunNumber="$run
-# to be changed later again, when additional tables are available
-#corsikarunno=`sendquery`
-corsikarunno=$run
+file=${primaries[1]}
 
 # lock sequ
-lockfile=$lockpath/lock-$table-$column-$corsikarunno.txt
+lockfile=$lockpath/lock-$step-$run.$file.txt
 checklock 
 
-printprocesslog "INFO starting $program for run $run"
+printprocesslog "INFO starting $program for run $run and file $file"
 
 setstatus "start" 
 
-printprocesslog "INFO run $program for run $run " 
-printprocesslog "INFO create input card... " 
+printprocesslog "INFO run $program for run $run  and file $file" 
+printprocesslog "INFO create input card for run $run  and file $file" 
 
 # get values for inputcard
-query="SELECT fParticleTypeKEY FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fParticleTypeKEY FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 primparticle=`sendquery`
-query="SELECT fEnergyMin, fEnergyMax FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fEnergyMin, fEnergyMax FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 erange=`sendquery`
-query="SELECT fNumEvents FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fNumEvents FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 numevts=`sendquery`
-query="SELECT fEnergySlope FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fEnergySlope FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 slope=`sendquery`
-query="SELECT fZenithDistanceMin, fZenithDistanceMax FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fZenithDistanceMin, fZenithDistanceMax FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 zdrange=`sendquery`
 # Change from the MARS definition (Az=90 -> East) to Corsika (Az=90 -> West)
-query="SELECT 180-fAzimuthMin, 180-fAzimuthMax FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT 180-fAzimuthMin, 180-fAzimuthMax FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 azrange=`sendquery`
-query="SELECT DATE_FORMAT(fRunStart, '%Y/%m/%d') FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
-date=`sendquery`
-outpath=$mcpath"/"$program"/"$date
-query="SELECT fCorsikaSeed FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fCorsikaSeed1 FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 seed1=`sendquery`
-query="SELECT fCorsikaSeed+1 FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fCorsikaSeed2 FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 seed2=`sendquery`
-query="SELECT fCorsikaSeed+2 FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fCorsikaSeed3 FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 seed3=`sendquery`
-query="SELECT fImpactMax FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fImpactMax FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 impactmax=`sendquery`
-query="SELECT fViewConeMax FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fViewConeMax FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 viewconemax=`sendquery`
-query="SELECT fNumReUseShower FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
-reuse=`sendquery`
-query="SELECT fStartingAltitude FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fStartingAltitude FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 startalt=`sendquery`
-query="SELECT fMirrorDiameter FROM MCCorsikaRunData where fCorsikaRunNumber="$corsikarunno
+query="SELECT fMirrorDiameter FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
 diameter=`sendquery`
-
-logfile=$outpath/$program"-"`printf %06d $corsikarunno`".log"
+query="SELECT fAtmosphericModelKEY FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
+atm=`sendquery`
+query="SELECT fObsLevel FROM CorsikaInfo LEFT JOIN Observatory USING(fObservatoryKEY) WHERE fRunNumber=$run AND fFileNumber=$file"
+obslev=`sendquery`
+query="SELECT fMagnetBX, fMagnetBZ FROM CorsikaInfo LEFT JOIN Observatory USING(fObservatoryKEY) WHERE fRunNumber=$run AND fFileNumber=$file"
+magnet=`sendquery`
+query="SELECT fMagnetRotation FROM CorsikaInfo LEFT JOIN Observatory USING(fObservatoryKEY) WHERE fRunNumber=$run AND fFileNumber=$file"
+magnetrot=`sendquery`
+query="SELECT fWavelengthMin, fWavelengthMax FROM CorsikaInfo WHERE fRunNumber=$run AND fFileNumber=$file"
+wavelengths=`sendquery`
+
+outpath=$mcpath"/"$program"/"`printf %08d $run | cut -c 0-4`/`printf %08d $run | cut -c 5-8`
 makedir $outpath 
+logfile=$outpath/cer000`printf %06d $file | cut -c 4-6`".log"
 
 cd $corsikapath
 
-echo "INPUTCARD:" >| $logfile 2>&1
-printinputcard >> $logfile 2>&1
-echo "" >> $logfile 2>&1
-echo "" >> $logfile 2>&1
-printinputcard | ./cc6501p-linux >> $logfile 2>&1
-
-rm -f $outpath/dat`printf %06d $corsikarunno`
+printinputcard | ./cc6501p-linux >| $logfile 2>&1
 
 check1=$?
-
 case $check1 in
-   0)   printprocesslog "INFO $program finished successfully for corsika run number $corsikarunno (check1=$check1)"
+   0)   printprocesslog "INFO $program finished successfully for run $run file $file (check1=$check1)"
         ;;
-   *)   printprocesslog "ERROR $program failed for corsika run number $corsikarunno (check1=$check1)"
+   *)   printprocesslog "ERROR $program failed for run $run file $file (check1=$check1)"
         com=$Fcorsika
         check=$check1
@@ -189,4 +213,7 @@
 esac
 
+# removing empty datnnnnnn file
+rm -f $outpath/dat`printf %06d $file`
+
 setstatus "stop" 
 
Index: trunk/MagicSoft/Mars/datacenter/scripts/setup.wue.fact.mc
===================================================================
--- trunk/MagicSoft/Mars/datacenter/scripts/setup.wue.fact.mc	(revision 9585)
+++ trunk/MagicSoft/Mars/datacenter/scripts/setup.wue.fact.mc	(revision 9585)
@@ -0,0 +1,123 @@
+#!/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  06/2010 <mailto:daniela.dorner@unige.ch>
+#
+#   Copyright: MAGIC Software Development, 2000-2010
+#
+#
+# ========================================================================
+#
+# This a resource file for the scripts, in which the standard paths and 
+# functions, which are needed more often are stored. 
+#
+
+rootsys=/opt/root_v5.12.00f
+if ! export | grep $rootsys  >|/dev/null
+then
+   export ROOTSYS=$rootsys
+   export PATH=$PATH:$ROOTSYS/bin
+   export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOTSYS/lib
+fi
+
+# queuing system
+queuesys=condor # wuerzburg data center
+
+# logging and setup
+logpath=/home/montecarlo/fact_db_test
+lockpath=/home/montecarlo/fact_db_test/locks
+setuppath=/home/montecarlo/fact_db_test/setup
+
+## paths of data
+#datapath=/magic/data
+#subsystempath=/magic/subsystemdata
+#sequpath=/magic/sequences
+#datasetpath=/magic/datasets
+
+## paths and setup for mc
+mcpath=/magic/simulated
+#mcrawpath=$mcpath/rawfiles
+#mcsequpath=$mcpath/sequences
+#corsikapath=/home/operator/Corsika/Mmcs6500/
+#detectordir=/home/msmeyer/MC/MagicSoft/Simulation/Detector
+#reflectorversion="reflector 0.7"
+#cameraversion="camera 0.8"
+#export FLUPRO=$corsikapath'/fluka2008_3'
+
+
+#webpath=/www/htdocs/datacenter
+
+# get paths for mars, macros and scripts
+mars=`dirname $0 | sed -e 's/\/datacenter\/scripts//'`
+macrospath=$mars/datacenter/macros
+scriptspath=$mars/datacenter/scripts
+# rcfiles
+# dependencies of steps
+steps=$mars/resources/steps_fact2.rc
+# file with db information
+sqlrc=$mars/sql.rc
+
+
+#addresses to which the errors are sent
+erradrs="daniela.dorner@unige.ch" 
+#addresses to which the changes are sent
+adrs="daniela.dorner@unige.ch" 
+
+
+#setup for jobmanager
+sleeptime=30 #30
+sleeptimelimit=360 #360
+errorsleeptimedefault=60 #60
+max=42 #maximum number of processes for one script in case there are more than one and the others do not have anything to do
+totalmax=40 #maximum number of processes (total) overwrites pnototal(we) in case it is smaller
+
+#
+# FIXME: 
+# max anz an prozessen setzen
+# besser Verhaeltnis zwischen den Prozessen und das am besten noch 
+# automatisch anpassen 
+# evtl noch maximale anz an callistos setzen
+# 
+
+#hour:            0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
+pnototal=(       42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 )
+pnototalwe=(     42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 )
+
+pnoceres=(        7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7 )
+pnocprun=(        7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7 )
+pnocallisto=(    32 32 32 32 32 20 20 18 18 12 12 12 12 12 12 12 12 12 12 18 18 20 20 32 )
+pnostar=(        10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 )
+
+pnocereswe=(      7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7 )
+pnocprunwe=(      7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7 )
+pnocallistowe=(  32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 )
+pnostarwe=(      10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 )
+
+## set variables for jobmanager 
+#scripts=( "runstar2" "runcallisto2" "cprun" "runceres" ) 
+#scriptscolname=( "Star" "Callisto" "CPRun" "Ceres" ) 
+#pnosweek=( ${pnostar[@]} ${pnocallisto[@]} ${pnocprun[@]} ${pnoceres[@]} ) 
+#pnoswe=( ${pnostarwe[@]} ${pnocallistowe[@]} ${pnocprunwe[@]} ${pnocereswe[@]} ) 
+
+# set variables for jobmanager 
+scripts=( "runstar2" "runcallisto2" "runceres" ) 
+scriptscolname=( "Star" "Callisto" "Ceres" ) 
+pnosweek=( ${pnostar[@]} ${pnocallisto[@]} ${pnoceres[@]} ) 
+pnoswe=( ${pnostarwe[@]} ${pnocallistowe[@]} ${pnocereswe[@]} ) 
+
Index: trunk/MagicSoft/Mars/datacenter/scripts/sourcefile
===================================================================
--- trunk/MagicSoft/Mars/datacenter/scripts/sourcefile	(revision 9562)
+++ trunk/MagicSoft/Mars/datacenter/scripts/sourcefile	(revision 9585)
@@ -20,5 +20,5 @@
 #   Author(s): Daniela Dorner  05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
 #
-#   Copyright: MAGIC Software Development, 2000-2007
+#   Copyright: MAGIC Software Development, 2000-2010
 #
 #
@@ -209,59 +209,16 @@
 }
 
-function resetstatusvalues()
-{
-   statustime=NULL
-   starttime=NULL
-   returncode=NULL
-   programid=NULL
-   failedtime=NULL
-}
-
+# print the current status values
 function printstatusvalues()
 {
    echo "the current values are:"
-   echo " statustime=$statustime"
    echo " starttime=$starttime"
+   echo " stoptime=$stoptime"
    echo " returncode=$returncode"
-   echo " programid=$programid"
-   echo " failedtime=$failedtime"
    echo "-- check: -$check-"
    echo ""
 }
 
-# function evaluating the statusvalues
-function evalstatus()
-{
-   case $@ in
-      start)   printprocesslog "INFO setstatus start"
-               starttime="Now()"
-               ;;
-       stop)   case $check in
-                  ok)  printprocesslog "INFO setstatus stop - ok"
-                       statustime="Now()"
-                       ;;
-                  no)  printprocesslog "INFO setstatus stop - nothing new"
-                       check="ok"
-                       ;;
-                   *)  printprocesslog "INFO setstatus stop - failed"
-                       starttime=noreset
-                       if [ "$check" == "" ]
-                       then
-                          returncode=1
-                       else
-                          returncode=$check
-                       fi
-                       programid=$com
-                       failedtime="Now()"
-                       check="ok"
-                       ;;
-               esac
-               ;;
-          *)   printprocesslog "ERROR function evalstatus got wrong variable"
-               finish
-               ;;
-   esac
-}
-
+# get the db-setup from the sql.rc
 function getdbsetup()
 {
@@ -277,53 +234,125 @@
 }
 
+# function to get the needed information from the dependencies-file steps.rc
 function getstepinfo()
 {
    getdbsetup
-   table=`grep "$column:" $steps | sed -e "s/[.]$column://" -e 's/#//' -e 's/ //g'`
-   coltab=`grep "$column:" $steps | sed -e 's/://' -e 's/#//' -e 's/ //g'`
-   needs=`grep "$coltab[.]Needs:" $steps | sed -e "s/$coltab[.]Needs://"`
-   noderestricted=`grep "$coltab[.]NodeRestricted:" $steps | sed -e "s/$coltab[.]NodeRestricted://" -e 's/ //g'`
-   influences=`grep "$coltab[.]Influences:" $steps | sed -e "s/$coltab[.]Influences://"`
-   prims=( `grep "$table[.]Primary:" $steps | sed -e "s/$table[.]Primary://"` )
-#   echo " column $column - table $table - coltab $coltab"
+   needs=`grep "$step[.]Needs:" $steps | sed -e "s/$step[.]Needs://"`
+   noderestricted=`grep "$step[.]NodeRestricted:" $steps | sed -e "s/$step[.]NodeRestricted://" -e 's/ //g'`
+   prims=( `grep "$step[.]Primaries:" $steps | sed -e "s/$step[.]Primaries://"` )
 #   echo " needs: $needs"
-#   echo " influences: $influences"
 #   echo " noderestricted: $noderestricted"
 #   echo " prims: ${prims[@]}"
 }
 
+# function to get the primaries of a step from the dependencies-file steps.rc
+function getprimary()
+{
+   getdbsetup
+   grep $@"[.]Primaries:" $steps | sed -e "s/$@[.]Primaries://"
+}
+
+# function to get the join of a step from the dependencies-file steps.rc
+function getjoin()
+{
+   getdbsetup
+   grep $@"[.]Join:" $steps | sed -e "s/$@[.]Join://"
+}
+
+# function to create the middle part of a query
+#  which is identical for the functions getstatus() and gettodo()
+function middlepartofquery()
+{
+   # add from which table the information is queried
+   query=$query" FROM "$step"Status "
+   # add the joins to the tables in which the status of the preceding steps is stored
+   for need in $needs
+   do
+      needprims=( `getprimary $need` )
+      if [ "`echo ${needprims[@]}`" == "`echo ${prims[@]}`" ]
+      then 
+         query=$query" LEFT JOIN "$need"Status USING (${prims[@]}) "
+      fi
+   done
+   # add condition
+   query=$query" WHERE "
+   # add condition for the status of the peceding steps
+   counter=0
+   for need in $needs
+   do
+      if [ $counter -gt 0 ]
+      then
+         query=$query" AND "
+      fi
+      needprims=( `getprimary $need` )
+      # in case the primaries of the tables agree
+      #   only the condition is given
+      # for tables with differing primaries a special query 
+      #   is needed
+      if [ "`echo ${needprims[@]}`" == "`echo ${prims[@]}`" ]
+      then 
+         query=$query" NOT ISNULL("$need"Status.fStartTime) AND "
+         query=$query" NOT ISNULL("$need"Status.fStopTime) AND "
+         query=$query" ISNULL("$need"Status.fReturnCode) "
+      else 
+         query=$query" (SELECT COUNT(*) FROM "$need"Status "
+         query=$query" "`getjoin $need`" "
+         query=$query" WHERE "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"="$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`
+         for (( j=1 ; j < ${#prims[@]} ; j++ ))
+         do
+            query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"="$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`
+         done
+         query=$query") = (SELECT COUNT(*) FROM "$need"Status "
+         query=$query" "`getjoin $need`" "
+         query=$query" WHERE "$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`"="$step"Status."`echo ${prims[0]} | sed -e 's/,//g'`
+         for (( j=1 ; j < ${#prims[@]} ; j++ ))
+         do
+            query=$query" AND "$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`"="$step"Status."`echo ${prims[$j]} | sed -e 's/,//g'`
+         done
+         query=$query" AND NOT ISNULL(fStartTime) "
+         query=$query" AND NOT ISNULL(fStopTime) "
+         query=$query" AND ISNULL(fReturnCode)) "
+      fi
+      counter=`echo $counter + 1 | bc -l`
+   done
+   # add condition for the status of the step itself
+   query=$query" AND ISNULL("$step"Status.fStartTime) "
+   query=$query" AND ISNULL("$step"Status.fStopTime) "
+   query=$query" AND ISNULL("$step"Status.fReturnCode) "
+}
+
 # function to get todolist
+#   returns the next or the list of next steps
 function gettodo()
 {
+   # reset the variable for the number of the next step 
    process=
    printprocesslog "INFO getting todo..."
    getstepinfo
    # get query
-   query=" SELECT "${prims[0]}
-   for (( i=1 ; i < ${#prims[@]} ; i++ ))
+   query=" SELECT "`echo ${prims[0]} | sed -e 's/,//g'`
+   for (( j=1 ; j < ${#prims[@]} ; j++ ))
    do
-      query=$query", ${prims[$i]}"
+      query=$query", "`echo ${prims[$j]} | sed -e 's/,//g'`
    done
-   query=$query" FROM $table WHERE "
-   if ! echo $needs | grep '#' > /dev/null
-   then
-      for need in $needs
-      do
-         query=$query" NOT ISNULL($need) AND"
-      done
-   fi
-   query=$query" ISNULL($column) "
+   # get middle par of query 
+   middlepartofquery
+   # add requirement for production host in case it is needed
    if [ "$2 " != " " ]
    then 
       query=$query" AND fProductionHostKEY=$2 "
    fi
-   query=$query" AND ISNULL(fStartTime) AND ISNULL(fFailedTime) AND ISNULL(fProgramId) AND ISNULL(fReturnCode) "
-   query=$query" ORDER BY fPriority desc "
+   # order by priority to the the number of the next step to be done
+   query=$query" ORDER BY "$step"Status.fPriority desc "
+   # add limitation in case only one or a limited number of 
+   #  processes should be executed
    if [ "$1 " != " " ]
    then 
       query=$query" limit 0, $1 "
    fi
-#   echo " QUERY: "$query
+   # print query 
+   echo " gettodo QUERY: "$query
    printprocesslog "INFO gettodo QUERY: "$query
+   # execute query 
    if ! process=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
    then
@@ -331,5 +360,5 @@
       finish
    fi
-
+   # get numbers of next step from mysql result
    if [ "$process" = "" ]
    then
@@ -342,29 +371,26 @@
 }
 
-
 # function to get the number of processes which still have to be done
 function getstatus()
 {
+   # reset the variable for the number of steps to be done
    numproc=
    getstepinfo
    # get query
-   query=" SELECT COUNT(*) FROM $table WHERE "
-   if ! echo $needs | grep '#' > /dev/null
-   then
-      for need in $needs
-      do
-         query=$query" NOT ISNULL($need) AND"
-      done
-   fi
-   query=$query" ISNULL($column) "
+   query=" SELECT COUNT(*), 1 " # the 1 is just for grouping
+   # get middle part of query 
+   middlepartofquery
+   # add requirement for production host in case it is needed
    if [ "$1 " != " " ]
    then 
       query=$query" AND fProductionHostKEY=$1 "
    fi
-   query=$query" AND ISNULL(fStartTime) AND ISNULL(fFailedTime) AND ISNULL(fProgramId) AND ISNULL(fReturnCode) "
-   query=$query" GROUP BY $column "
-#   echo "QUERY: "$query
+   # group by an 'artifical' column to get the number of lines
+   query=$query" GROUP BY 2 "
+   # printing query
+   echo " getstatus QUERY: "$query
    printprocesslog "INFO getstatus QUERY: "$query
-   if ! numproc=`mysql -s -u $us --password=$pw --host=$ho $db -e " $query "`
+   # execute query
+   if ! numprocs=( `mysql -s -u $us --password=$pw --host=$ho $db -e " $query "` )
    then
       printprocesslog "ERROR could not query number of processes from db (program: $program, function getstatus)"
@@ -372,7 +398,10 @@
       continue
    fi
-   if [ "$numproc" = "" ]
+   # get number of processes from mysql result
+   if [ "${numprocs[1]}" = "" ]
    then
       numproc=0
+   else
+      numproc=${numprocs[0]}
    fi
 }
@@ -381,43 +410,95 @@
 function setstatus()
 {
-   if [ "$column" = "no" ]
+   # remark:
+   # this function does not include the 'Default' flag 
+   # for resetting steps
+
+   # for dowebplots (there are steps which have no entry in the DB)
+   if [ "$step" = "no" ]
    then
       return
    fi
-   resetstatusvalues
-   evalstatus $@
+   
+   # reset status values
+   starttime=NULL
+   stoptime=NULL
+   returncode=NULL
+   # evaluate the status values
+   case $@ in
+      start)   printprocesslog "INFO setstatus start"
+               starttime="Now()"
+               ;;
+       stop)   case $check in
+                  ok)  printprocesslog "INFO setstatus stop - ok"
+                       starttime=noreset
+                       stoptime="Now()"
+                       ;;
+                  no)  printprocesslog "INFO setstatus stop - nothing new"
+                       check="ok"
+                       ;;
+                   *)  printprocesslog "INFO setstatus stop - failed"
+                       starttime=noreset
+                       stoptime="Now()"
+                       if [ "$check" == "" ]
+                       then
+                          returncode=1
+                       else
+                          returncode=$check
+                       fi
+                       check="ok"
+                       ;;
+               esac
+               ;;
+          *)   printprocesslog "ERROR function setstatus got wrong variable"
+               finish
+               ;;
+   esac
+   # get 
    getstepinfo
+   
+   # get the influences from the steps.rc by evaluating the needs of all steps
+   influences=`grep $step $rc | grep "Needs" | grep -v "$step[.]Needs" | cut -d'.' -f1`
+   
    # get query
-   reset=`grep "$coltab[.]Reset:" $steps | sed -e "s/$coltab[.]Reset://" -e 's/ //g'`
-   if [ "$reset" = "no" ]
-   then
-      printprocesslog "ERROR You cannot reset $column for ${primaries[$s+$s]}"
-      finish
-   fi
-   query=" update $table set $column=$statustime"
-   if ! echo $influences | grep '#' > /dev/null
-   then
-      for influence in $influences
-      do
-         query=$query", $influence=NULL"
-      done
-   fi
+   query=" UPDATE "$step"Status "
+   # add joins to the influenced tables
+   for influence in $influences
+   do
+      query=$query" LEFT JOIN $influence USING("`getprimary $influence`") "
+      specialjoin=`getjoin $influence`
+      if ! [ "$specialjoin" = "" ]
+      then 
+         query=$query$specialjoin
+      fi
+   done
+   # set the status values according to the new status of the step
+   query=$query" SET "
    if ! [ "$starttime" = "noreset" ]
    then
-      query=$query", fStartTime=$starttime"
-   fi
-   query=$query", fFailedTime=$failedtime, fProgramId=$programid, fReturnCode=$returncode "
-   query=$query" where "
+      query=$query" "$step"Status.fStartTime=$starttime, "
+   fi
+   query=$query" "$step"Status.fStopTime=$stoptime, "$step"Status.fReturnCode=$returncode "
+   # set also the status values of the influenced steps
+   for influence in $influences
+   do
+      query=$query", "$influence"Status.fStartTime=NULL "
+      query=$query", "$influence"Status.fStopTime=NULL "
+      query=$query", "$influence"Status.fReturnCode=NULL "
+   done
+   # give the condition for which step the status values have to be set
+   query=$query" WHERE "
    if [ "$s" = "" ]
    then
       s=0
    fi
-   query=$query" ${prims[0]}='${primaries[$s*${#prims[@]}]}'"
-   for (( i=1 ; i < ${#prims[@]} ; i++ ))
+   query=$query" "`echo ${prims[0]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}]}'"
+   for (( j=1 ; j < ${#prims[@]} ; j++ ))
    do
-      query=$query" AND ${prims[$i]}='${primaries[$s*${#prims[@]}+$i]}' "
+      query=$query" AND "`echo ${prims[$j]} | sed -e 's/,//g'`"='${primaries[$s*${#prims[@]}+$j]}' "
    done   
-#   echo " QUERY: "$query
+   # print query
+   echo " setstatus QUERY: "$query
    printprocesslog "INFO setstatus QUERY: "$query
+   # execute query
    if ! mysql -s -u $us --password=$pw --host=$ho $db -e " $query "
    then
@@ -425,5 +506,4 @@
       finish
    fi
-
 }
 
