Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 9005)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 9006)
@@ -71,4 +71,18 @@
      - added telescope number to rules
 
+   * datacenter/macros/buildsequenceentries.C:
+     - the building is now based on the file-id (run*1000+file)
+       and it is only done for a single telescope
+     - added telescope number as srgument
+     - added fPriority to SequenceProcessStatus
+     - added fTelescopeNumber to the Sequence table
+     - the sequence number is still idetical to the first run-number
+     - changed reading of resource file such that the telescope
+       number can be given for each subsection
+
+   * datacenter/macros/doexclusions.C:
+     - many simplifications
+     - use telescope number as argument
+     - simplified and improved
 
 
Index: /trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C	(revision 9005)
+++ /trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C	(revision 9006)
@@ -159,4 +159,14 @@
     UInt_t fMax;
 
+    static UInt_t GetId(const TString &str)
+    {
+        const Ssiz_t dot = str.First('.');
+
+        const UInt_t run = str.Atoi();
+        const UInt_t sub = dot<0 ? 0 : atoi(str.Data()+dot+1);
+
+        return run*1000+sub;
+    }
+
     void Init(const TObjArray &arr, Int_t idx=0)
     {
@@ -179,9 +189,8 @@
             case 2: fRegexp1  = TPRegexp(Form("^%s$", str.Data())); break;
             case 3: fRegexp2  = TPRegexp(Form("^%s$", str.Data())); break;
-            case 4: fMin      = str.Atoi();               break;
-            case 5: fMax      = str.Atoi();               break;
+            case 4: fMin      = GetId(str);               break;
+            case 5: fMax      = GetId(str);               break;
             }
         }
-        //cout << endl;
     }
 
@@ -257,4 +266,6 @@
     TString fPathSequences;
 
+    Int_t fTelescopeNumber;
+
     TMap  fMap;
     TList fListRegexp;
@@ -286,10 +297,8 @@
         const TString runtime =
             "SUM(TIME_TO_SEC(TIMEDIFF(fRunStop,fRunStart))), ";
-            //"SUM(if(TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)<0,"
-            //" TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart)+24*60*60,"
-            //" TIME_TO_SEC(fRunStop)-TIME_TO_SEC(fRunStart))), ";
-
-        const TString where = Form("(fRunNumber BETWEEN %d AND %d) AND fExcludedFDAKEY=1",
-                                   from, to);
+
+        const TString where = Form("(fRunNumber*1000+fFileNumber BETWEEN %d AND %d) "
+                                   "AND fTelescopeNumber=%d AND fExcludedFDAKEY=1",
+                                   from, to, fTelescopeNumber);
 
         TString query;
@@ -359,9 +368,9 @@
         }
 
-        const TString set = Form("fSequenceFirst=%d ", from);
+        const TString set = Form("fSequenceFirst=%d, fTelescopeNumber=%d ", from/1000, fTelescopeNumber);
 
         TString query1;
         query1 += set;
-        query1 += Form(",fSequenceLast=%d,",                   to);
+        query1 += Form(",fSequenceLast=%d,",                   to/1000);
         query1 += Form(" fSourceKEY=%s,",                      (*row)[0]);
         query1 += Form(" fProjectKEY=%s,",                     (*row)[1]);
@@ -400,5 +409,7 @@
         }
 
-        if (!Insert("SequenceProcessStatus", set))
+        TString prio = set;
+        prio += Form(", fPriority=%d ", from/1000);
+        if (!Insert("SequenceProcessStatus", prio))
         {
             cout << "ERROR - Could not insert Sequence into SequenceProcessStatus." << endl;
@@ -458,9 +469,9 @@
     Int_t CheckSequence(Int_t runstart, Int_t runstop)
     {
-        const char *fmt1 = "SELECT fRunNumber FROM RunData WHERE";
-        const char *fmt2 = "AND fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4) ORDER BY fRunNumber";
-
-        const TString query1 = Form("%s fSequenceFirst=%d %s",            fmt1, runstart, fmt2);
-        const TString query2 = Form("%s fRunNumber BETWEEN %d AND %d %s", fmt1, runstart, runstop, fmt2);
+        const char *fmt1 = "SELECT fRunNumber*1000+fFileNumber AS Id FROM RunData WHERE";
+        const char *fmt2 = "AND fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4) ORDER BY Id";
+
+        const TString query1 = Form("%s fTelescopeNumber=%d AND fSequenceFirst=%d %s",    fmt1, fTelescopeNumber, runstart/1000, fTelescopeNumber, fmt2);
+        const TString query2 = Form("%s fTelescopeNumber=%d AND fRunNumber*1000+fFileNumber BETWEEN %d AND %d %s", fmt1, fTelescopeNumber, runstart, runstop, fmt2);
 
         TSQLResult *res1 = Query(query1);
@@ -499,8 +510,8 @@
         TString query=
             Form("SELECT fSequenceFirst FROM RunData "
-                 " WHERE fRunNumber BETWEEN %d AND %d AND "
-                 " fSequenceFirst>0 AND "
+                 " WHERE fRunNumber*1000+fFileNumber BETWEEN %d AND %d AND "
+                 " fTelescopeNumber=%d AND fSequenceFirst>0 AND "
                  " fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4)"
-                 " GROUP BY fSequenceFirst", runstart, runstop);
+                 " GROUP BY fSequenceFirst", fTelescopeNumber, runstart, runstop);
 
         TSQLResult *res = Query(query);
@@ -554,5 +565,10 @@
     Bool_t ReadResources(const char *fname)
     {
-        TPRegexp regexp("^\\[.*\\]$");
+        // Check for the section header
+        TPRegexp regexp("^\\[(Transition|Regexp):?[0-9 ]*\\]$");
+        // Check if section header contains a number
+        TPRegexp regnum("[0-9]");
+        // Check if section header contains the telescope number
+        TPRegexp regtel(Form("[^0-9]0*%d[^0-9]", fTelescopeNumber));
 
         ifstream fin(fname);
@@ -579,14 +595,31 @@
                 continue;
 
-            if (txt[0]=='[' && section!=2)
+            if (txt[0]=='['/* && section!=2*/)
             {
-                //cout << txt << endl;
-                section = 0;
-                if (txt(regexp)=="[Transition]")
-                    section = 1;
-                if (txt(regexp)=="[Regexp]")
-                    section = 2;
-                continue;
+                TString sec = txt(regexp);
+                if (!sec.IsNull())
+                {
+                    section = 0;
+
+                    // Skip sections with the wrong telescope number
+                    if (!sec(regnum).IsNull() && !sec(regtel).IsNull())
+                        continue;
+
+                    // Check which section we are in
+                    if (sec.BeginsWith("[Transition"))
+                        section = 1;
+                    if (sec.BeginsWith("[Regexp]"))
+                        section = 2;
+                    continue;
+                }
+
+                if (section!=2)
+                {
+                    cout << "WARNING - Line starts with [ but we are not in the Regexp section." << endl;
+                    cout << txt << endl;
+                    continue;
+                }
             }
+
 
             TObjArray *arr = txt.Tokenize(" ");
@@ -705,7 +738,6 @@
     {
         return Form("SELECT RunData.f%sKEY, f%sName FROM RunData "
-                    "LEFT JOIN %s ON RunData.f%sKEY=%s.f%sKEY "
-                    "WHERE %s GROUP BY f%sName",
-                    col, col, col, col, col, col, cond, col);
+                    "LEFT JOIN %s USING (f%sKEY) WHERE %s GROUP BY f%sName",
+                    col, col, col, col, cond, col);
     }
 
@@ -864,9 +896,9 @@
         // Request data necessary to split block into sequences
         const TString query=
-            Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop"
+            Form("SELECT fRunNumber*1000+fFileNumber AS Id, fRunTypeKEY, fRunStart, fRunStop"
                  " FROM RunData "
-                 " WHERE fRunNumber BETWEEN %d AND %d AND "
+                 " WHERE fRunNumber*1000+fFileNumber BETWEEN %d AND %d AND "
                  " fExcludedFDAKEY=1 AND (fRunTypeKEY BETWEEN 2 AND 4)"
-                 " ORDER BY fRunNumber", runstart, runstop);
+                 " ORDER BY Id", runstart, runstop);
 
         // Send query
@@ -1084,11 +1116,6 @@
 
 public:
-    SequenceBuild(TEnv &env) : MSQLMagic(env)
-    {
-        cout << "buildsequences" << endl;
-        cout << "--------------" << endl;
-        cout << endl;
-        cout << "Connected to " << GetName() << endl;
-
+    SequenceBuild(Int_t tel=1, const char *rc="sql.rc") : MSQLMagic(rc), fTelescopeNumber(tel)
+    {
         fListRegexp.SetOwner();
 
@@ -1097,5 +1124,14 @@
             return;
     }
-    SequenceBuild()
+
+    SequenceBuild(TEnv &env, Int_t tel=1) : MSQLMagic(env), fTelescopeNumber(tel)
+    {
+        fListRegexp.SetOwner();
+
+        // FIXME: THIS IS NOT YET HANDLED
+        if (ReadResources("resources/sequences.rc"))
+            return;
+    }
+    ~SequenceBuild()
     {
         fMap.DeleteAll();
@@ -1113,6 +1149,7 @@
         const TString cond =
             Form("(fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\") "
-                 "AND fExcludedFDAKEY=1 AND fRunTypeKEY BETWEEN 2 AND 4 ",
-                 day.Data(), day.Data());
+                 "AND fExcludedFDAKEY=1 AND fRunTypeKEY BETWEEN 2 AND 4 "
+                 "AND fTelescopeNumber=%d ",
+                 day.Data(), day.Data(), fTelescopeNumber);
 
         //query all sources observed in this night
@@ -1128,5 +1165,5 @@
         // Setup query to get all values from the database,
         // that are relevant for building sequences
-        TString query("SELECT fRunNumber, fRunTypeKEY, ");
+        TString query("SELECT fRunNumber*1000+fFileNumber AS Id, fRunTypeKEY, ");
         query += elts;
         query += ", ";
@@ -1142,5 +1179,5 @@
                 query += mapkey->GetName();
             }
-        query += Form(" FROM RunData WHERE %s ORDER BY fRunNumber", cond.Data());
+        query += Form(" FROM RunData WHERE %s ORDER BY Id", cond.Data());
 
         TSQLResult *res = Query(query);
@@ -1176,9 +1213,7 @@
 ClassImp(SequenceBuild);
 
-int buildsequenceentries(TString day, TString datapath, TString sequpath, Bool_t dummy=kTRUE)
+int buildsequenceentries(TString day, TString datapath, TString sequpath, Int_t tel=1, Bool_t dummy=kTRUE)
 {
-    TEnv env("sql.rc");
-
-    SequenceBuild serv(env);
+    SequenceBuild serv(tel, "sql.rc");
     if (!serv.IsConnected())
     {
@@ -1186,4 +1221,16 @@
         return 0;
     }
+
+    cout << "buildsequenceentries" << endl;
+    cout << "--------------------" << endl;
+    cout << endl;
+    cout << "Connected to " << serv.GetName() << endl;
+    if (!datapath.IsNull())
+        cout << "DataPath:  " << datapath << endl;
+    if (!sequpath.IsNull())
+        cout << "SeqPath:   " << sequpath << endl;
+    cout << "Day:       " << day      << endl;
+    cout << "Telescope: " << tel      << endl;
+    cout << endl;
 
     serv.SetIsDummy(dummy);
@@ -1196,9 +1243,7 @@
 // Build Sequences for all Nights
 //
-int buildsequenceentries(TString datapath, TString sequpath, Bool_t dummy=kTRUE)
+int buildsequenceentries(TString datapath, TString sequpath, Int_t tel=1, Bool_t dummy=kTRUE)
 {
-    TEnv env("sql.rc");
-
-    SequenceBuild serv(env);
+    SequenceBuild serv(tel, "sql.rc");
     if (!serv.IsConnected())
     {
@@ -1206,4 +1251,13 @@
         return 0;
     }
+
+    cout << "buildsequenceentries" << endl;
+    cout << "--------------------" << endl;
+    cout << endl;
+    cout << "Connected to " << serv.GetName() << endl;
+    cout << "DataPath:  " << datapath << endl;
+    cout << "SeqPath:   " << sequpath << endl;
+    cout << "Telescope: " << tel      << endl;
+    cout << endl;
 
     serv.SetIsDummy(dummy);
@@ -1213,11 +1267,11 @@
 }
 
-int buildsequenceentries(Bool_t dummy=kTRUE)
+int buildsequenceentries(Int_t tel=1, Bool_t dummy=kTRUE)
 {
-    return buildsequenceentries("", "", dummy);
+    return buildsequenceentries("", "", tel, dummy);
 }
 
-int buildsequenceentries(TString day, Bool_t dummy=kTRUE)
+int buildsequenceentries(TString day, Int_t tel=1, Bool_t dummy=kTRUE)
 {
-    return buildsequenceentries(day, "", "", dummy);
+    return buildsequenceentries(day, "", "", tel, dummy);
 }
Index: /trunk/MagicSoft/Mars/datacenter/macros/doexclusions.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/doexclusions.C	(revision 9005)
+++ /trunk/MagicSoft/Mars/datacenter/macros/doexclusions.C	(revision 9006)
@@ -59,62 +59,51 @@
 #include <iostream>
 #include <iomanip>
-#include <fstream>
-
-#include <MSQLServer.h>
+
+#include <TEnv.h>
+#include <TSystem.h>
+
 #include <TSQLRow.h>
 #include <TSQLResult.h>
 
-#include <TEnv.h>
-#include <TSystem.h>
+#include "MSQLMagic.h"
 
 using namespace std;
 
 //get minimum or maximum runnumber of the runs of a night
-int GetRunNumber(MSQLServer &serv, TString date, TString value)
+int GetRunNumber(MSQLServer &serv, Int_t tel, TString date, TString cmd)
 {
-    TString query(Form("SELECT %s(fRunNumber) FROM RunData ", value.Data()));
+    TString query;
+    query  = Form("SELECT %s(fRunNumber) FROM RunData ", cmd.Data());
+    query += Form("WHERE fTelescopeNumber=%d", tel);
 
     if (date!="NULL")
     {
         TString day=date+" 13:00:00";
-        query+=Form(" WHERE (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
-                    day.Data(), day.Data());
-    }
-
-    cout << "query: " << query << endl;
+        query += Form(" AND (fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\")",
+                      day.Data(), day.Data());
+    }
 
     TSQLResult *res = serv.Query(query);
     if (!res)
     {
-        cout << "Error - could not get run#" << endl;
+        cout << "ERROR - Could not get " << cmd << " fRunNumber." << endl;
         return -1;
     }
 
     TSQLRow *row =res->Next();
-    cout << (void*)row << endl;
     if (TString((*row)[0]).IsNull())
     {
-        cout << "No run available for this date" << endl;
+        cout << "No run available for " << date << endl;
         delete res;
         return 0;
     }
     delete res;
+
     return atoi((*row)[0]);
 }
 
-//get part of a query (left join of tables)
-TString GetJoin(TString table)
+int doexclusions(Int_t startrun, Int_t stoprun, Int_t tel=1, TString date="NULL", Bool_t dummy=kTRUE)
 {
-    TString query(Form("left join %s ON RunData.f%sKEY=%s.f%sKEY ",
-                       table.Data(), table.Data(), table.Data(), table.Data()));
-    return query;
-}
-
-int doexclusions(Int_t startrun, Int_t stoprun, TString date="NULL")
-{
-    TEnv env("sql.rc");
-    TEnv rc("automatic-exclusions.rc");
-
-    MSQLServer serv(env);
+    MSQLMagic serv("sql.rc");
     if (!serv.IsConnected())
     {
@@ -123,41 +112,45 @@
     }
 
+    // Open rc files with exclusions
+    TEnv rc("resources/exclusions.rc");
+
+    // Some information for the user
     cout << "doexclusions" << endl;
     cout << "------------" << endl;
     cout << endl;
     cout << "Connected to " << serv.GetName() << endl;
-
-    //if neither start- nor stoprun is given, the minimum and maximum runnumber
-    // is queried from the database to do the exclusions for all runs
-    // if a night is given for all runs of this night
+    cout << "Date:      " << date << endl;
+
+    serv.SetIsDummy(dummy);
+
+    // if neither start- nor stoprun is given, the minimum and maximum
+    // runnumber is queried from the database to do the exclusions for
+    // all runs if a night is given for all runs of this night
     if (startrun==0 && stoprun==0)
     {
-        startrun=GetRunNumber(serv, date, "min");
-        stoprun=GetRunNumber(serv, date, "max");
-    }
+        startrun = GetRunNumber(serv, tel, date, "MIN");
+        stoprun  = GetRunNumber(serv, tel, date, "MAX");
+    }
+
+    cout << "Start Run: " << startrun << endl;
+    cout << "Stop  Run: " << stoprun  << endl;
+
     //check format of start- and stoprun
     if (startrun<0 || stoprun<0)
     {
-        cout << "wrong format of runno" << endl;
+        cout << "ERROR - Startrun<0 or stoprun<0." << endl;
         return 2;
     }
+
     //if no run for date is available, GetRunNumber() returns 0
     if (startrun==0 || stoprun==0)
         return 1;
 
-    //get the condition for the runnumber range
-    TString runcond(Form("AND fRunNumber BETWEEN %d AND %d ", startrun, stoprun));
-
-    //get exclusions-reasons (stored in the table ExcludedFDA) from the database
-    //the exclusions which can be done automatically are marked with the flag fExcludedFDAAutomatic='yes'
-    //and with an importance (one run may be excluded for several reasons,
-    //the reason is chosen according to the importance)
-    TString query="SELECT fExcludedFDAKEY from ExcludedFDA where fExcludedFDAAutomatic='yes'";
-    TSQLResult *res = serv.Query(query);
+    // Get exclusions-reasons (stored in the table ExcludedFDA) from the DB
+    TSQLResult *res = serv.Query("SELECT fExcludedFDAKEY, fExcludedFDAName "
+                                 "FROM ExcludedFDA "
+                                 "ORDER BY fExcludedFDAImportance ASC");
     if (!res)
-    {
-        cout << "Error - could not do any automatic excludes." << endl;
         return 2;
-    }
 
     //update the exclusion-reasons for all runs
@@ -165,87 +158,49 @@
     while ((row = res->Next()))
     {
-        //read in values from the resource file automatic-exclusions (explanation see above)
-        TString key=(*row)[0];
-        TString column=rc.GetValue("key"+key+".Column", "");
-        TString join1=rc.GetValue("key"+key+".Join1", "");
-        TString join2=rc.GetValue("key"+key+".Join2", "");
-        TString border=rc.GetValue("key"+key+".SpecialRunCond", "");
-
-        //get importance of exclusion-reason from database
-        TString query(Form("SELECT fExcludedFDAImportance from ExcludedFDA where fExcludedFDAKEY=%s ", key.Data()));
-        TSQLResult *res2 = serv.Query(query);
+        // check the key (NULL means "No exclusion")
+        const TString key = (*row)[0];
+        if (key.IsNull())
+            continue;
+
+        // Get the corresponding condition from the file
+        const TString cond = rc.GetValue("key"+key, "");
+        if (cond.IsNull())
+            continue;
+
+        // Get all files to be excluded
+        TString query2 = "SELECT fRunNumber, fFileNumber FROM RunData ";
+        query2 += serv.GetJoins("RunData", query2+cond);
+
+        query2 += Form("WHERE (%s) ", cond.Data());
+        query2 += Form("AND fRunNumber BETWEEN %d AND %d ", startrun, stoprun);
+        query2 += Form("AND fTelescopeNumber=%d", tel);
+
+        TSQLResult *res2 = serv.Query(query2);
         if (!res2)
-        {
-            cout << "Error - could not get importance." << endl;
             return 2;
-        }
-
-        TSQLRow *row2=res2->Next();
-        Int_t newimp=atoi((*row2)[0]);
-        delete res2;
-
-        //get current importance from database
-        //for the runs which match the exclusion-reason
-        query="SELECT fRunNumber, fExcludedFDAImportance ";
-        if (!column.IsNull())
-            query+=Form(", %s", column.Data());
-        if (!join1.IsNull())
-            query+=Form(", f%sName", join1.Data());
-        if (!join2.IsNull())
-            query+=Form(", f%sName", join2.Data());
-        query +=" FROM RunData ";
-        query +=GetJoin("ExcludedFDA");
-        if (!join1.IsNull())
-            query+=GetJoin(join1.Data());
-        if (!join2.IsNull())
-            query+=GetJoin(join2.Data());
-        query +=Form("WHERE (%s) ", rc.GetValue("key"+key+".Cond", ""));
-        if (!border.IsNull())
-            query+=Form(" AND fRunNumber BETWEEN IF(%s>%d, %d, %s) AND IF (%s<%d, %s, %d) ",
-                        border.Data(), startrun, startrun, border.Data(),
-                        border.Data(), stoprun, border.Data(), stoprun);
-        else
-            query +=runcond;
-
-        cout << query << endl;
-
-        res2 = serv.Query(query);
-        if (!res2)
-        {
-            cout << "Error - no runs to exclude" << endl;
-            return 2;
-        }
-
-        //compare new and old importance
-        //change or keep the exclusion-reason accordingly
+
+        // Update exlcusion for file
+        TSQLRow *row2=0;
         while ((row2 = res2->Next()))
         {
-            if (TString((*row2)[1]).IsNull() || atoi((*row2)[1])>newimp)
-            {
-                //change exclusion-reason
-                TString query(Form("UPDATE RunData SET fExcludedFDAKEY=%s WHERE fRunNumber=%s",
-                                   key.Data(), (*row2)[0]));
-                cout << "QU: " << query << endl;
-                TSQLResult *res3 = serv.Query(query);
-                if (!res3)
-                {
-                    cout << "Error - could not insert exclusion" << endl;
-                    return 2;
-                }
-                delete res3;
-                continue;
-            }
-            //keep exclusion-reason
-            cout << "run#: " << (*row2)[0] << " reason for exclusion is still the same" << endl;
+            TString vars(Form("fExcludedFDAKEY=%s", key.Data()));
+            TString where(Form("fRunNumber=%s AND fFileNumber=%s", (*row2)[0], (*row2)[1]));
+
+            if (serv.Update("RunData", vars, where)==kFALSE)
+                return 2;
+
+            cout << "File M" << tel << ":" << (*row2)[0] << "/" << setw(3) << setfill('0') << (*row2)[1] << " excluded due to: " << (*row)[1] << "." << endl;
         }
+
         delete res2;
     }
     delete res;
+
     return 1;
 }
 
 //run doexclusions for one night
-int doexclusions(TString date="NULL")
+int doexclusions(TString date="NULL", Int_t tel=1, Bool_t dummy=kTRUE)
 {
-    return doexclusions(0, 0, date);
+    return doexclusions(0, 0, tel, date, dummy);
 }
Index: /trunk/MagicSoft/Mars/datacenter/macros/filldotrun.C
===================================================================
--- /trunk/MagicSoft/Mars/datacenter/macros/filldotrun.C	(revision 9005)
+++ /trunk/MagicSoft/Mars/datacenter/macros/filldotrun.C	(revision 9006)
@@ -133,5 +133,25 @@
     }
 
-    cout << "  V" << version << " " << flush;
+    if (version >= 200805190)
+    {
+        strng.ReadLine(fin);
+        if (!strng.BeginsWith("Telescope M"))
+        {
+            cout << "WARNING - Line 3 doesn't start with 'Telescope M'." << endl;
+            cout << strng << endl;
+        }
+    }
+
+    if (version >= 200411130)
+    {
+        strng.ReadLine(fin);
+        if (strng[0]!='#')
+        {
+            cout << "WARNING - '#' expected." << endl;
+            cout << strng << endl;
+        }
+    }
+
+    cout << " * V" << version << " " << endl;
 
     Int_t cnt=0;
@@ -146,5 +166,5 @@
             if (strng[0]!='M')
             {
-                cout << "First character is not an M." << endl;
+                cout << "WARNING - First character is not an M." << endl;
                 cout << strng << endl;
                 strng.ReadLine(fin);
@@ -153,5 +173,5 @@
             if (strng[1]!='1')
             {
-                cout << "Only MAGIC 1 implemented so far." << endl;
+                cout << "WARNING - Only MAGIC 1 implemented so far." << endl;
                 cout << strng << endl;
                 strng.ReadLine(fin);
@@ -176,5 +196,6 @@
         {
             strng.ReadLine(fin);
-            cout << "Runnumber == 0" << endl;
+            cout << "WARNING - Runnumber == 0" << endl;
+            cout << strng << endl;
             continue;
         }
@@ -187,11 +208,10 @@
         }
 
-        TString where = Form("((fTelescopeNumber=%d AND fFileNumber=%d) OR"
-                             "(ISNULL(fTelescopeNumber) AND ISNULL(fFileNumber)))",
+        TString where = Form("fTelescopeNumber=%d AND fFileNumber=%d",
                              telnumber, filenumber);
-        if (serv.ExistStr("fRunNumber", "RunData", strng, where))
+        if (serv.ExistStr("fRunNumber", "RunData", Form("%d", runnumber), where))
         {
             // FIXME: Maybe we can implement a switch to update mode?
-            cout << "Entry M" << telnumber << ":" << runnumber << "/" << filenumber << " already existing... skipped." << endl;
+            cout << "WARNING - Entry M" << telnumber << ":" << runnumber << "/" << filenumber << " already existing... skipped." << endl;
             strng.ReadLine(fin);
             continue;
@@ -569,6 +589,7 @@
             return -1;
 
-        TString query2=Form("fRunNumber=%d, fTimingCorrection='1970-01-01 00:00:00', fCompmux='1970-01-01 00:00:00'",
-                            runnumber);
+        TString query2=Form("fTelescopeNumber=%d, fRunNumber=%d, fFileNumber=%d, "
+                            "fPriority=%d, fTimingCorrection='1970-01-01 00:00:00', fCompmux='1970-01-01 00:00:00'",
+                            telnumber, runnumber, filenumber, runnumber);
         if (testflagkey==3)
             query2+=" , fDataCheckDone='1970-01-01 00:00:00'";
@@ -580,9 +601,8 @@
 
     return cnt;
-
 }
 
 // This tool will work from Period017 (2004_05_17) on...
-int filldotrun(const TString path="/data/MAGIC/Period018/ccdata", Bool_t dummy=kTRUE)
+int filldotrun(const TString path="/home/lapalma/transfer/ccdata", Bool_t dummy=kTRUE)
 {
     MSQLMagic serv("sql.rc");
@@ -592,4 +612,5 @@
         return 0;
     }
+
     cout << "filldotrun" << endl;
     cout << "----------" << endl;
@@ -617,7 +638,7 @@
             break;
 
-        cout << name(TRegexp("CC_.*.run", kFALSE)) << flush;
+        cout << " * " << name(TRegexp("CC_.*.run", kFALSE)) << endl;
         Int_t n = insert(serv, dummy, name);
-        cout << " <" << n << "> " << (dummy?"DUMMY":"") << endl;
+        cout << "   <" << n << "> " << (dummy?"DUMMY":"") << endl;
 
         if (n<0)
