Index: trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C	(revision 7629)
+++ trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C	(revision 7813)
@@ -92,4 +92,5 @@
 #include <TSystem.h>
 #include <TObjString.h>
+#include <TObjArray.h>
 
 #include <MTime.h>
@@ -258,7 +259,7 @@
         //       without the check for manuallychanged, all manually
         //       changed sequences would be deleted
-        cout << "found no sequence in Sequ-DB -> check other tables" << endl;
-        cout << " deleting every sequence found in Calibration, Star or SequenceProcessStatus between "
-            << from << " and " << to << endl;
+        cout << "   + found no sequence in Sequ-DB -> check other tables" << endl;
+        cout << "     deleting every sequence found in Calibration, Star or" << endl;
+        cout << "     SequenceProcessStatus between " << from << " and " << to << endl;
 
         //calibration table
@@ -315,5 +316,5 @@
 
     case 1:
-        cout << "found 1 sequence: " << sequences.At(0) << " -> check sequ# " << endl;
+        cout << "   + found one sequence: " << sequences.At(0) << " -> check sequence." << endl;
         if (sequences.At(0)!=from)
         {
@@ -324,5 +325,5 @@
         else
         {
-            cout << "sequence# is the same -> checking the runs " << endl;
+            cout << "     * sequence is the same -> checking the runs." << endl;
 
             //getting olf runs
@@ -370,5 +371,5 @@
             else
             {
-                cout << " number of runs is the same -> checking the single runnumbers " << endl;
+                cout << "       · number of runs is the same -> checking the single runnumbers." << endl;
 
                 for (Int_t i=0;i<newruns.GetSize();i++)
@@ -378,22 +379,20 @@
                         continue;
 
-                    cout << i << ". run is not the same ( " << oldruns.At(i) << " -- " << newruns.At(i)
+                    cout << "       · " << i << ". run is not the same ( " << oldruns.At(i) << " -- " << newruns.At(i)
                         << ") -> deleting sequence " << sequences.At(0) << endl;
                     if (!DeleteSequence(serv, datapath, sequpath, sequences.At(0), dummy))
                         return -1;
-                    rc=1;
-                    break;
+                    return 1;
                 }
             }
         }
-        rc=2;
-        break;
+        return 2;
 
     default:
-        cout << "found " << numsequ << " sequences -> deleting them " << endl;
+        cout << " - found " << numsequ << " sequences -> deleting them " << endl;
 
         for (Int_t i=0;i<sequences.GetSize();i++)
         {
-                cout << "deleting sequence " << sequences.At(i) << "... <" << i << ">" << endl;
+                cout << "   + deleting sequence " << sequences.At(i) << "... <" << i << ">" << endl;
                 if (!DeleteSequence(serv, datapath, sequpath, sequences.At(i), dummy))
                     return -1;
@@ -407,6 +406,5 @@
 Bool_t InsertSequence(MSQLServer &serv, Int_t from, Int_t to)
 {
-
-    cout << "Inserting sequence " << from << " ... " << endl;
+    cout << "       · Inserting sequence " << from << " ... " << endl;
 
     // ========== Request number of events ==========
@@ -481,22 +479,23 @@
 
     TString query1("INSERT Sequences SET");
-    query1+=Form(" fSequenceFirst=%d,  fSequenceLast=%d,", from, to);
-    query1+=Form(" fSourceKEY=%s,", (*row)[0]);
-    query1+=Form(" fProjectKEY=%s,", (*row)[1]);
-    query1+=Form(" fNumEvents=%s,", nevts.Data());
-    query1+=Form(" fRunTime=%s,", secs.Data());
-    query1+=Form(" fRunStart=\"%s\",", start.Data());
-    query1+=Form(" fZenithDistanceMin=%s,", zdmin.Data());
-    query1+=Form(" fZenithDistanceMax=%s,", zdmax.Data());
-    query1+=Form(" fAzimuthMin=%s,", azmin.Data());
-    query1+=Form(" fAzimuthMax=%s,", azmax.Data());
-    query1+=Form(" fL1TriggerTableKEY=%s,", (*row)[2]);
-    query1+=Form(" fL2TriggerTableKEY=%s,", (*row)[3]);
-    query1+=Form(" fHvSettingsKEY=%s,", (*row)[4]);
-    query1+=Form(" fDiscriminatorThresholdTableKEY=%s,", (*row)[5]);
-    query1+=Form(" fTriggerDelayTableKEY=%s,", (*row)[6]);
-    query1+=Form(" fLightConditionsKEY=%s,", (*row)[7]);
-    query1+=Form(" fTestFlagKEY=%s, ", (*row)[8]);
-    query1+=Form(" fObservationModeKEY=%s, fManuallyChangedKEY=1", (*row)[9]);
+    query1 += Form(" fSequenceFirst=%d,  fSequenceLast=%d,", from, to);
+    query1 += Form(" fSourceKEY=%s,",                      (*row)[0]);
+    query1 += Form(" fProjectKEY=%s,",                     (*row)[1]);
+    query1 += Form(" fNumEvents=%s,",                      nevts.Data());
+    query1 += Form(" fRunTime=%s,",                        secs.Data());
+    query1 += Form(" fRunStart=\"%s\",",                   start.Data());
+    query1 += Form(" fZenithDistanceMin=%s,",              zdmin.Data());
+    query1 += Form(" fZenithDistanceMax=%s,",              zdmax.Data());
+    query1 += Form(" fAzimuthMin=%s,",                     azmin.Data());
+    query1 += Form(" fAzimuthMax=%s,",                     azmax.Data());
+    query1 += Form(" fL1TriggerTableKEY=%s,",              (*row)[2]);
+    query1 += Form(" fL2TriggerTableKEY=%s,",              (*row)[3]);
+    query1 += Form(" fHvSettingsKEY=%s,",                  (*row)[4]);
+    query1 += Form(" fDiscriminatorThresholdTableKEY=%s,", (*row)[5]);
+    query1 += Form(" fTriggerDelayTableKEY=%s,",           (*row)[6]);
+    query1 += Form(" fLightConditionsKEY=%s,",             (*row)[7]);
+    query1 += Form(" fTestFlagKEY=%s,",                    (*row)[8]);
+    query1 += Form(" fObservationModeKEY=%s, ",            (*row)[9]);
+    query1+="fManuallyChangedKEY=1";
 
 
@@ -511,7 +510,9 @@
     delete res;
 
-    cout << "q1: " << query1 << endl;
-    cout << "q2: " << query2 << endl;
-    cout << "q3: " << query3 << endl;
+    /*
+     cout << "q1: " << query1 << endl;
+     cout << "q2: " << query2 << endl;
+     cout << "q3: " << query3 << endl;
+     */
 
     res = serv.Query(query1);
@@ -547,5 +548,5 @@
 Bool_t NewSequence(MSQLServer &serv, TString datapath, TString sequpath, Int_t from, Int_t to, TList &sequlist, Bool_t dummy)
 {
-    cout << "Found Sequence (" << from << ", " << to << ") ... checking runs..." << flush;
+    cout << " - Found Sequence (" << from << ", " << to << ") ... checking runs..." << flush;
 
     if (!CheckRuns(serv, from, to, 2))
@@ -567,5 +568,5 @@
 
 
-    cout << "checking Sequence..." << endl;
+    cout << "   + checking sequence..." << endl;
 
     TObject *sequ;
@@ -574,5 +575,5 @@
     {
     case 0:
-        cout << " sequence not found -> inserting " << from << flush ;
+        cout << "       · sequence not found -> inserting " << from << flush;
         if (dummy)
         {
@@ -586,5 +587,5 @@
 
     case 1:
-        cout << " deleting successfully finished -> inserting sequence " << from << flush; 
+        cout << "       · deleting successfully finished -> inserting sequence " << from << flush;
         if (dummy)
         {
@@ -598,5 +599,5 @@
 
     case 2:
-        cout << " sequence " << from << " is already existing -> inserting not necessary" << endl;
+        cout << "       · sequence " << from << " is already existing -> inserting not necessary" << endl;
         if ((sequ=sequlist.Remove(sequlist.FindObject(Form("%d", from)))))
             delete sequ;
@@ -604,5 +605,5 @@
 
     case -1:
-        cout << " deleting went wrong " << endl;
+        cout << "       · deleting went wrong " << endl;
         return kFALSE;
     }
@@ -784,5 +785,58 @@
 }
 
-
+void buildblocks(TSQLResult *res, TExMap &blocks)
+{
+    //build blocks of runs, which have the same values
+    //for each block the first and the last run are stored in a TExMap
+    //the values are checked with the help of an array of TStrings
+    TString keys[7];
+
+    Long_t runstart = -1;
+    Long_t runstop  = -1;
+
+    // "Result set closed" means "Result set empty"
+
+    // Loop over runs
+    TSQLRow *row=0;
+    while ((row=res->Next()))
+    {
+        // Check whether all keys for this run fit
+        for (Int_t i=1; i<8; i++)
+        {
+            // Do not check project key for this runs
+            // (this condition is never true for the first run)
+            if (i==2 && runstart>20100 && runstop<45100)
+                continue;
+
+            // Check whether key has changed for this run
+            // (this condition is never true for the first run)
+            if (keys[i-1] == (*row)[i])
+                continue;
+
+            // Found one block with unique keys, fill values into TExMap
+            // (except if this is the first run processed)
+            if (runstart>0)
+                blocks.Add((ULong_t)blocks.GetSize(), runstart, runstop);
+
+            // This is the runnumber of the first run in the new block
+            runstart=atoi((*row)[0]);
+
+            // These are the keys corresponding to the first run in the new block
+            for (int i=1; i<8; i++)
+                keys[i-1] = (*row)[i];
+
+            break;
+        }
+
+        // This is the new runnumber of the last run in this block
+        runstop=atoi((*row)[0]);
+    }
+
+    if (runstart!=runstop)
+    {
+        //fill values into TExMap (last value)
+        blocks.Add((ULong_t)blocks.GetSize(), runstart, runstop);
+    }
+}
 
 //
@@ -811,6 +865,15 @@
                             day.Data(), day.Data()));
 
+    TString elts  = "fSourceKEY";
+    TString eltp2 = "fProjectKEY";
+
     //get all values from the database, that are relevant for building sequences
-    TString query(Form("SELECT fRunNumber, fSourceKEY, fProjectKEY, fHvSettingsKEY,  fLightConditionsKEY, fDiscriminatorThresholdTableKEY, fTriggerDelayTableKEY, fObservationModeKEY FROM RunData WHERE %s AND fExcludedFDAKEY=1 order by fRunNumber", cond.Data()));
+    TString query("SELECT fRunNumber, ");
+    query += elts;
+    query += ", ";
+    query += eltp2;
+    query += Form(", fHvSettingsKEY, fLightConditionsKEY, fDiscriminatorThresholdTableKEY, "
+                  "fTriggerDelayTableKEY, fObservationModeKEY FROM RunData "
+                  "WHERE %s AND fExcludedFDAKEY=1 order by fRunNumber", cond.Data());
 
     TSQLResult *res = serv.Query(query);
@@ -818,55 +881,12 @@
         return 2;
 
-    //build blocks of runs, which have the same values
-    //for each block the first and the last run are stored in a TExMap
-    //the values are checked with the help of an array of TStrings
-    TString keys[7]= { "NULL", "NULL", "NULL", "NULL", "NULL", "NULL" , "NULL" };
-    TString stop     = "NULL";
-    TString runstart = "NULL";
-    TString runstop  = "NULL";
-    Int_t count = 0;
     TExMap blocks;
-    Int_t runbegin;
-    Int_t runend;
-
-    TSQLRow *row=0;
-    while ((row=res->Next()))
-    {
-        if (count==0)
-        {
-            for (Int_t i=1 ; i<8 ; i++)
-                keys[i-1]=(*row)[i];
-            runstart=(*row)[0];
-        }
-
-        for (Int_t i=1 ; i<8 ; i++)
-        {
-            runbegin=atoi(runstart.Data());
-            runend=atoi(runstop.Data());
-            if (i==2 && runbegin>20100 && runend<45100)
-                continue;
-
-            TString value=(*row)[i];
-            TString key=keys[i-1];
-            if (!value.CompareTo(key)) 
-                continue;
-
-            keys[i-1]=value;
-            //fill values into TExMap
-            blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
-            runstart=(*row)[0];
-            for (Int_t i=1 ; i<8 ; i++)
-                keys[i-1]=(*row)[i];
-            break;
-        }
-        runstop=(*row)[0];
-        count++;
-    }
-
-    //fill values into TExMap (last value)
-    runbegin=atoi(runstart.Data());
-    runend=atoi(runstop.Data());
-    blocks.Add((ULong_t)blocks.GetSize(), (Long_t)runbegin, (Long_t)runend);
-
+    buildblocks(res, blocks);
+    delete res;
+
+    // ---
+    // Now the blocks containing the same observation conditions
+    // are created...
+    // ---
 
     //get list of current sequence of this night from the database and store it in sequlist
@@ -874,5 +894,4 @@
     query=Form("SELECT fSequenceFirst FROM Sequences WHERE fManuallyChangedKEY=1 AND %s order by fSequenceFirst",
                cond.Data());
-//    cout << "Q: " << query << endl;
 
     res = serv.Query(query);
@@ -880,5 +899,6 @@
         return 2;
 
-    cout << "old sequences: " << flush;
+    cout << "Old sequences: " << flush;
+    TSQLRow *row=0;
     while ((row=res->Next()))
     {
@@ -888,4 +908,6 @@
     }
     cout << endl;
+
+    delete res;
 
     Bool_t rc = kTRUE;
@@ -896,9 +918,10 @@
     while (nblocks.Next(key, val))
     {
-        Int_t runstart2 = (Int_t)key;
-        Int_t runstop2 = (Int_t)val;
-        cout << endl << "datablock from " << runstart2 << " to " << runstop2 << endl;
-
-        if (!Process(serv, datapath, sequpath, runstart2, runstop2, sequlist, dummy))
+        const Int_t runstart = (Int_t)key;
+        const Int_t runstop  = (Int_t)val;
+
+        cout << endl << "Datablock from " << runstart << " to " << runstop << ":" << endl;
+
+        if (!Process(serv, datapath, sequpath, runstart, runstop, sequlist, dummy))
             rc = kFALSE;
 
@@ -912,5 +935,5 @@
     while ((obj=Next()))
     {
-        cout << "sequ: " << obj->GetName() << " deleting... " << endl;
+        cout << " - Sequence " << obj->GetName() << " deleting... " << endl;
         if (!DeleteSequence(serv, datapath, sequpath, atoi(obj->GetName()), dummy))
             return 2;
@@ -947,7 +970,8 @@
     {
         date=(*row)[0];
-        cout << "date: " << date << endl;
         buildsequenceentries(date, datapath, sequpath, dummy);
     }
+
+    delete res;
 
     return 1;
