Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 9014)
+++ trunk/MagicSoft/Mars/Changelog	(revision 9015)
@@ -19,9 +19,33 @@
                                                  -*-*- END OF LINE -*-*-
 
- 2008/07/18 Thomas Bretz
+ 2008/07/19 Thomas Bretz
 
    * datacenter/macros/buildsequenceentries.C:
      - Use the GetId also in the constructor of Rule
      - fixed reading of the sections in the sequences.rc
+
+   * datacenter/macros/writesequencefile.C:
+     - included iostream
+
+   * mjobs/MSequence.[h,cc]:
+     - updates some comments
+     - simplified the comparison functions
+     - calculate fPeriod automatically in any case 
+       (do not read from file)
+     - added X for excluded to AddFile
+     - use AddFile in ExludeFile
+     - removed incorrect calles to MJob::SortArray
+     - call SortArrays correctly in AddFile
+     - changed default for telescope from 1 to 0 in SQL constructor
+
+   * mjobs/MSequenceSQL.[h,cc]:
+     - if telescope number is 0 check whether an unambiguous
+       sequence with this number exists. If existing set the
+       telescope number accordingly
+     - changed default for telescope in constructor to 0 accordingly
+
+
+
+ 2008/07/18 Thomas Bretz
 
    * mbase/MTime.cc:
Index: trunk/MagicSoft/Mars/datacenter/macros/writesequencefile.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/writesequencefile.C	(revision 9014)
+++ trunk/MagicSoft/Mars/datacenter/macros/writesequencefile.C	(revision 9015)
@@ -47,6 +47,8 @@
 //
 /////////////////////////////////////////////////////////////////////////////
-#include "MSequence.h"
+#include <iostream>
+
 #include "MSQLMagic.h"
+#include "MSequenceSQL.h"
 
 using namespace std;
Index: trunk/MagicSoft/Mars/mjobs/MSequence.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequence.cc	(revision 9014)
+++ trunk/MagicSoft/Mars/mjobs/MSequence.cc	(revision 9015)
@@ -47,6 +47,6 @@
 //   Sequence:     31015
 //
-//   # Observation Period (not needed)
-//   Period:       18
+//   # Observation Period (not needed, calculated from Night)
+//   # Period:       18
 //
 //   # Date of sunrise of the observation night - necessary if the path
@@ -74,4 +74,7 @@
 //   # Total number of data-events in sequence (not needed)
 //   NumEvents:    250914
+//
+//   # Brightest light condition of the contained runs (not needed)
+//   LightCondition: Twilight
 //
 //   # Whether this is MC data or not (necessary in case of MCs if
@@ -661,8 +664,7 @@
     fLastRun   = GetEnvValue2(env, prefix, "LastRun",   -1);
     fNumEvents = GetEnvValue2(env, prefix, "NumEvents", -1);
-    fPeriod    = GetEnvValue2(env, prefix, "Period",    -1);
 
     TString str;
-    str = GetEnvValue2(env, prefix, "LightConditions", "n/a");
+    str = GetEnvValue2(env, prefix, "LightCondition", "n/a");
     fLightCondition = GetLightCondition(str);
 
@@ -673,4 +675,6 @@
     str += " 00:00:00";
     fNight.SetSqlDateTime(str);
+
+    fPeriod = fNight.GetMagicPeriod();
 
     fProject      = GetEnvValue2(env, prefix, "Project", "");
@@ -688,4 +692,5 @@
 
     // Dummies:
+    env.Touch("Period");
     env.Touch("ZdMin");
     env.Touch("ZdMax");
@@ -700,4 +705,16 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Create a sequence from the database, e.g.
+//
+//    TString mysql     = "mysql://MAGIC:password@vela/MyMagic";
+//    Int_t   sequence  = 100002;
+//    Int_t   telescope = 1;
+//
+//    MSequence seq(mysql, sequence, telescope);
+//
+// For more details see MSequenceSQL.
+//
 MSequence::MSequence(const char *fname, UInt_t seq, UShort_t tel)
 {
@@ -705,54 +722,102 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Some helper functions for the comparison functions
+//
 static int operator==(const TArrayI &a, const TArrayI &b)
 {
-    return a.GetSize()==b.GetSize() ?
-        memcmp(a.GetArray(), b.GetArray(), a.GetSize()*sizeof(Int_t))==0 :
-        false;
-}
-
-static int IsNull(const TArrayI &a)
-{
-    if (a.GetSize()==0)
-        return true;
-
-    return a.GetSize()==0 ? true : a==TArrayI(a.GetSize());
+    return a.GetSize()==b.GetSize() &&
+        memcmp(a.GetArray(), b.GetArray(), a.GetSize()*sizeof(Int_t));
 }
 
 static int IsNull(const TArrayI &a, const TArrayI &b)
 {
-    return IsNull(a) && IsNull(b);
-}
-
-Bool_t MSequence::IsSimilar(const MSequence &s) const
+    return a==TArrayI(a.GetSize()) && b==TArrayI(b.GetSize());
+}
+
+static int Check(const TArrayI &a, const TArrayI &b)
+{
+    return a==b || IsNull(a, b);
+}
+
+// --------------------------------------------------------------------------
+//
+// Here we test all things mandatory for a sequence:
+//   fTelescope
+//   fSequence
+//   fNight
+//   fMonteCarlo
+//   fCalRuns
+//   fCalRunsSub
+//   fPedRuns
+//   fPedRunsSub
+//   fDatRuns
+//   fDatRunsSub
+//   fExclRuns
+//   fExclRunsSub
+//
+Bool_t MSequence::IsCompatible(const MSequence &s) const
 {
     return // Mandatory
-        fTelescope==s.fTelescope && fSequence==s.fSequence &&
-        fNight==s.fNight
-        && fLightCondition==s.fLightCondition &&
-
-        fMonteCarlo==s.fMonteCarlo &&
-
-        (fRunsSub    ==s.fRunsSub     || IsNull(fRunsSub,     s.fRunsSub))     &&
-        (fCalRunsSub ==s.fCalRunsSub  || IsNull(fCalRunsSub,  s.fCalRunsSub))  &&
-        (fPedRunsSub ==s.fPedRunsSub  || IsNull(fPedRunsSub,  s.fPedRunsSub))  &&
-        (fDatRunsSub ==s.fDatRunsSub  || IsNull(fDatRunsSub,  s.fDatRunsSub))  &&
-        (fExclRunsSub==s.fExclRunsSub || IsNull(fExclRunsSub, s.fExclRunsSub))
-        ;
-}
-
+        fTelescope   == s.fTelescope   &&
+        fSequence    == s.fSequence    &&
+        fNight       == s.fNight       &&
+
+        fMonteCarlo  == s.fMonteCarlo  &&
+
+        fCalRuns     == s.fCalRuns     &&
+        fPedRuns     == s.fPedRuns     &&
+        fDatRuns     == s.fDatRuns     &&
+        fExclRuns    == s.fExclRuns    &&
+
+        Check(fCalRunsSub,  s.fCalRunsSub) &&
+        Check(fPedRunsSub,  s.fPedRunsSub) &&
+        Check(fDatRunsSub,  s.fDatRunsSub) &&
+        Check(fExclRunsSub, s.fExclRunsSub);
+}
+
+// --------------------------------------------------------------------------
+//
+// Here we test whether all values of a sequence are identical
+//   IsCompaticle(s)
+//   fStart
+//   fLastRun
+//   fNumEvents
+//   fPeriod
+//   fProject
+//   fSource
+//   fRuns
+//   fHvSettings
+//   fTriggerTable
+//   fLightCondition
+//   fRuns
+//   fRunsSub
+//
+Bool_t MSequence::operator==(const MSequence &s) const
+{
+    return IsCompatible(s) &&
+        // Unnecessary
+        fStart          == s.fStart          &&
+        fLastRun        == s.fLastRun        &&
+        fNumEvents      == s.fNumEvents      &&
+        fPeriod         == s.fPeriod         &&
+        fProject        == s.fProject        &&
+        fSource         == s.fSource         &&
+        fRuns           == s.fRuns           &&
+        fHvSettings     == s.fHvSettings     &&
+        fTriggerTable   == s.fTriggerTable   &&
+        fLightCondition == s.fLightCondition &&
+        Check(fRunsSub, s.fRunsSub);
+}
+
+// --------------------------------------------------------------------------
+//
+// Check whether the sequence has also identical source, i.e.
+// the sequence filepath and the datapath in addition to operator==
+//
 Bool_t MSequence::IsIdentical(const MSequence &s) const
 {
-    return IsSimilar(s) &&
-        // Unnecessary
-        fStart==s.fStart         && fLastRun==s.fLastRun   &&
-        fNumEvents==s.fNumEvents && fPeriod==s.fPeriod     &&
-        fProject==s.fProject     && fSource==s.fSource     &&
-        /*fTriggerTable==s.fTriggerTable &&*/ fHvSettings==s.fHvSettings;
-}
-
-Bool_t MSequence::operator==(const MSequence &s) const
-{
-    return IsIdentical(s) &&
+    return *this==s &&
         // Obsolete
         fDataPath==s.fDataPath && fFileName==s.fFileName;
@@ -981,5 +1046,5 @@
         out << pre << "Night:          " << fNight.GetStringFmt("%Y-%m-%d") << endl;
     out << endl;
-    out << pre << "LightConditions: ";
+    out << pre << "LightCondition: ";
     switch (fLightCondition)
     {
@@ -1018,4 +1083,8 @@
     else
     {
+        // FIXME: This needs more check!
+        // Could we make "Runs" an automatic summary of all runs?
+        // Should we add a check forbidding the same run in
+        // Cal/Ped/Data? What about the Exclude?
         str += PrintRuns(out, pre, "Runs:     ", fRuns,     fRunsSub);
         /*str +=*/ PrintRuns(out, pre, "CalRuns:  ", fCalRuns,  fCalRunsSub);
@@ -1196,4 +1265,8 @@
         f = &fCalRunsSub;
         break;
+    case 'X':
+        r = &fExclRuns;
+        f = &fExclRunsSub;
+        break;
     default:
         r = &fRuns;
@@ -1204,19 +1277,5 @@
     AddEntry(run, file, *r, *f);
 
-    MJob::SortArray(fExclRuns);
-}
-
-// --------------------------------------------------------------------------
-//
-// Exclude this run (i.e. add it to fExclRuns)
-//
-void MSequence::ExcludeFile(UInt_t run, UInt_t file/*, Bool_t force*/)
-{
-//    if (force && IsExcluded(run, file))
-//        return;
-
-    AddEntry(run, file, fExclRuns, fExclRunsSub);
-
-    MJob::SortArray(fExclRuns);
+    SortArrays(*r, *f);
 }
 
@@ -1268,5 +1327,5 @@
     fNight.SetSqlDateTime(night);
 
-    fPeriod = MAstro::GetMagicPeriod(fNight.GetMjd());
+    fPeriod = fNight.GetMagicPeriod();
 }
 
Index: trunk/MagicSoft/Mars/mjobs/MSequence.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequence.h	(revision 9014)
+++ trunk/MagicSoft/Mars/mjobs/MSequence.h	(revision 9015)
@@ -101,5 +101,5 @@
     }
     MSequence(const char *fname, const char *path="", UInt_t id=(UInt_t)-1);
-    MSequence(const char *fname, UInt_t seq, UShort_t tel=1);
+    MSequence(const char *fname, UInt_t seq, UShort_t tel=0);
     MSequence(const MSequence &s) : MParContainer(s),
         fFileName(s.fFileName), fDataPath(s.fDataPath),
@@ -116,5 +116,5 @@
         fExclRunsSub(s.fExclRunsSub), fMonteCarlo(s.fMonteCarlo) { }
 
-    Bool_t IsSimilar(const MSequence &s) const;
+    Bool_t IsCompatible(const MSequence &s) const;
     Bool_t IsIdentical(const MSequence &s) const;
     Bool_t operator==(const MSequence &s) const;
@@ -143,5 +143,5 @@
     void AddFiles(UInt_t run, UInt_t f1, UInt_t f2, char type='*') { for (UInt_t i=f1; i<=f2; i++) AddFile(run, i, type); }
 
-    void ExcludeFile(UInt_t num, UInt_t file=0);
+    void ExcludeFile(UInt_t run, UInt_t file=0) { /*if (!force || !IsExcluded(run, file))*/ AddFile(run, file, 'X'); }
     void ExcludeRuns(TString runs);
 
Index: trunk/MagicSoft/Mars/mjobs/MSequenceSQL.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequenceSQL.cc	(revision 9014)
+++ trunk/MagicSoft/Mars/mjobs/MSequenceSQL.cc	(revision 9015)
@@ -55,4 +55,11 @@
 //
 //
+// A telscope number of -1 means: Take fTelescope as telescope number
+// to request. A telescope number of 0 tries to get the sequence from
+// the db including the telescope number. This will fail if more than
+// one sequence with the same number exist in the db for several
+// telescopes.
+//
+//
 // This tool will work from Period017 (2004_05_17) on...
 //
@@ -169,4 +176,5 @@
     fLastRun       = atoi(data[0]);
     fNumEvents     = atoi(data[8]);
+    fTelescope     = atoi(data[11]);
     fProject       = str[0];
     fSource        = str[1];
@@ -175,11 +183,13 @@
     fLightCondition = GetLightCondition(str[5]);
 
-    // MISSING: ---> ZdMin, ZdMax, fComment, fMonteCarlo <---
+    // FIXME: ZdMin (str[9]) ZdMax(str[10]) --> Comment
+    // FIXME: fMonteCarlo
+    // FIXME: fComment?
 
     // Now prepare queries to request the runs from the database
-    TString where(Form(" FROM RunData WHERE"
-                       " fTelescopeNumber=%d AND fSequenceFirst=%d AND"
-                       " fExcludedFDAKEY=1 AND fRunTypeKEY%%s",
-                       fTelescope, fSequence));
+    const TString where(Form(" FROM RunData WHERE"
+                             " fTelescopeNumber=%d AND fSequenceFirst=%d AND"
+                             " fExcludedFDAKEY=1 AND fRunTypeKEY%%s",
+                             fTelescope, fSequence));
 
     const TString query1(Form("SELECT fRunNumber, fFileNumber, fNumEvents %s", where.Data()));
@@ -229,9 +239,9 @@
     if (res.GetRowCount()>1)
     {
-        *fLog << err << "ERROR - Database request returned unexpected number of rows." << endl;
-        return kFALSE;
-    }
-
-    if (res.GetFieldCount()!=11)
+        *fLog << err << "ERROR - Database request returned morethan one sequence." << endl;
+        return kFALSE;
+    }
+
+    if (res.GetFieldCount()!=12)
     {
         *fLog << err << "ERROR - Database request returned unexpected number of rows." << endl;
@@ -268,5 +278,5 @@
 //  kTrue in case of success.
 //
-Bool_t MSequenceSQL::GetFromDatabase(MSQLMagic &serv, Int_t sequno, Int_t tel)
+Bool_t MSequenceSQL::GetFromDatabase(MSQLMagic &serv, UInt_t sequno, Int_t tel)
 {
     // Check if we are connected to the sql server
@@ -277,13 +287,15 @@
     }
 
-    // check if any telescope number is avlid
-    if (tel<=0 && fTelescope<=0)
+    /*
+    // check if any telescope number is valid
+    if (tel<0 && fTelescope==0)
     {
         *fLog << err << "ERROR - No telescope number given in GetSeqFromDatabase." << endl;
         return kFALSE;
     }
-
-    // check if any sequence number is avlid
-    if (sequno<0 && fSequence<0)
+    */
+
+    // check if any sequence number is valid
+    if (sequno==(UInt_t)-1 && fSequence==(UInt_t)-1)
     {
         *fLog << err << "ERROR - No sequence number given in GetSeqFromDatabase." << endl;
@@ -292,8 +304,8 @@
 
     // set "filename" and sequence number
-    fFileName = serv.GetName();
-    if (tel>0)
+    fFileName  = serv.GetName();
+    if (tel>=0)
         fTelescope = tel;
-    if (sequno>=0)
+    if (sequno!=(UInt_t)-1)
         fSequence = sequno;
 
@@ -302,8 +314,10 @@
                   " fL1TriggerTableKEY, fL2TriggerTableKEY, fHvSettingsKEY, "
                   " fLightConditionsKEY, fRunStart, fNumEvents, "
-                  " fZenithDistanceMin, fZenithDistanceMax "
+                  " fZenithDistanceMin, fZenithDistanceMax, fTelescopeNumber "
                   " FROM Sequences WHERE ");
-    query += Form("fTelescopeNumber=%d AND fSequenceFirst=%d",
-                  fTelescope, fSequence);
+    query += Form("fSequenceFirst=%d", fSequence);
+
+    if (tel>0)
+        query += Form(" AND fTelescopeNumber=%d", fTelescope);
 
     // Request information from database
@@ -348,5 +362,5 @@
 //  kTrue in case of success.
 //
-Bool_t MSequenceSQL::GetFromDatabase(const char *rc, Int_t sequno, Int_t tel)
+Bool_t MSequenceSQL::GetFromDatabase(const char *rc, UInt_t sequno, Int_t tel)
 {
     MSQLMagic serv(rc);
Index: trunk/MagicSoft/Mars/mjobs/MSequenceSQL.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequenceSQL.h	(revision 9014)
+++ trunk/MagicSoft/Mars/mjobs/MSequenceSQL.h	(revision 9015)
@@ -20,11 +20,11 @@
 
 public:
-    MSequenceSQL(MSQLMagic &serv, Int_t seqno=-1, Int_t tel=-1)  { GetFromDatabase(serv, seqno, tel); }
-    MSequenceSQL(const char *rc, Int_t seqno=-1, Int_t tel=-1)   { GetFromDatabase(rc, seqno, tel); }
-    MSequenceSQL(Int_t seqno=-1, Int_t tel=-1)                   { GetFromDatabase(seqno, tel); }
+    MSequenceSQL(MSQLMagic &serv, UInt_t seqno, Int_t tel=0) { GetFromDatabase(serv, seqno, tel); }
+    MSequenceSQL(const char *rc, UInt_t seqno, Int_t tel=0)  { GetFromDatabase(rc, seqno, tel); }
+    MSequenceSQL(UInt_t seqno, Int_t tel=0)                  { GetFromDatabase(seqno, tel); }
 
-    Bool_t GetFromDatabase(MSQLMagic &serv, Int_t seqno=-1, Int_t tel=-1);
-    Bool_t GetFromDatabase(const char *rc, Int_t seqno=-1, Int_t tel=-1);
-    Bool_t GetFromDatabase(Int_t seqno=-1, Int_t tel=-1) { return GetFromDatabase("sql.rc", seqno, tel); }
+    Bool_t GetFromDatabase(MSQLMagic &serv, UInt_t seqno=(UInt_t)-1, Int_t tel=-1);
+    Bool_t GetFromDatabase(const char *rc, UInt_t seqno=(UInt_t)-1, Int_t tel=-1);
+    Bool_t GetFromDatabase(UInt_t seqno=(UInt_t)-1, Int_t tel=-1) { return GetFromDatabase("sql.rc", seqno, tel); }
 
     ClassDef(MSequenceSQL, 0) // Extension of MSequence to get a sequence from a database
