Index: trunk/MagicSoft/Mars/mjobs/MSequence.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequence.cc	(revision 8999)
+++ trunk/MagicSoft/Mars/mjobs/MSequence.cc	(revision 9002)
@@ -170,4 +170,8 @@
 //   + fExclRunsSub
 //
+//  Class Version 6:
+//  ----------------
+//   + fTelescope
+//
 /////////////////////////////////////////////////////////////////////////////
 #include "MSequenceSQL.h"
@@ -514,6 +518,5 @@
 TString MSequence::GetFileName(UInt_t i, const TArrayI &arr, const TArrayI &sub, FileType_t type) const
 {
-    const Int_t tel = 1;
-    return InflateRunName(fNight, arr[i]>999999?tel:0, fMonteCarlo?-arr[i]:arr[i], sub.GetSize()>0?sub[i]:0, type);
+    return InflateRunName(fNight, arr[i]>999999?fTelescope:0, fMonteCarlo?-arr[i]:arr[i], sub.GetSize()>0?sub[i]:0, type);
 }
 
@@ -652,6 +655,8 @@
         fSequence = (UInt_t)-1;
 
+
     const TString prefix = Form("Sequence%08d", fSequence);
 
+    fTelescope = GetEnvValue2(env, prefix, "Telescope",  1);
     fLastRun   = GetEnvValue2(env, prefix, "LastRun",   -1);
     fNumEvents = GetEnvValue2(env, prefix, "NumEvents", -1);
@@ -695,7 +700,7 @@
 }
 
-MSequence::MSequence(const char *fname, UInt_t seq)
-{
-    *this = MSequenceSQL(fname, seq);
+MSequence::MSequence(const char *fname, UInt_t seq, UShort_t tel)
+{
+    *this = MSequenceSQL(fname, seq, tel);
 }
 
@@ -913,4 +918,6 @@
     if (pre.IsNull())
         out << "Sequence:       " << fSequence << endl;
+    if (fTelescope!=1)
+        out << "Telescope:      " << fTelescope << endl;
     if (fMonteCarlo)
         out << pre << "MonteCarlo:     Yes" << endl;
Index: trunk/MagicSoft/Mars/mjobs/MSequence.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequence.h	(revision 8999)
+++ trunk/MagicSoft/Mars/mjobs/MSequence.h	(revision 9002)
@@ -26,7 +26,6 @@
     TString fDataPath;         // Path to data files
 
-    UInt_t fSequence;          // Sequence number
-
-    // FIXME: UShort_t fTelescopeNumber ???
+    UShort_t fTelescope;       // Telescope number
+    UInt_t   fSequence;        // Sequence number
 
     MTime  fStart;             // Start time of sequence
@@ -94,5 +93,5 @@
 
 public:
-    MSequence() : fSequence((UInt_t)-1), fLastRun((UInt_t)-1),
+    MSequence() : fTelescope(0), fSequence((UInt_t)-1), fLastRun((UInt_t)-1),
         fNumEvents((UInt_t)-1), fPeriod((UInt_t)-1), fLightCondition(kNA), fMonteCarlo(kFALSE)
     {
@@ -101,7 +100,8 @@
     }
     MSequence(const char *fname, const char *path="", UInt_t id=(UInt_t)-1);
-    MSequence(const char *fname, UInt_t seq);
+    MSequence(const char *fname, UInt_t seq, UShort_t tel=1);
     MSequence(const MSequence &s) : MParContainer(s),
         fFileName(s.fFileName), fDataPath(s.fDataPath),
+        fTelescope(s.fTelescope),
         fSequence(s.fSequence), fStart(s.fStart),
         fLastRun(s.fLastRun), fNumEvents(s.fNumEvents), fPeriod(s.fPeriod),
@@ -125,5 +125,5 @@
 
     // Genaral interface
-    Bool_t IsValid() const { return fSequence!=(UInt_t)-1; }
+    Bool_t IsValid() const { return fTelescope>0 && fSequence!=(UInt_t)-1; }
     Bool_t IsMonteCarlo() const { return fMonteCarlo; }
     Bool_t IsExcluded(UInt_t run, UInt_t file) const { return IsContained(fExclRuns, fExclRunsSub, run, file); }
@@ -146,4 +146,5 @@
     UInt_t GetNumExclRuns() const { return fExclRuns.GetSize(); }
 
+    UShort_t GetTelescope() const { return fTelescope; }
     UInt_t GetSequence() const { return fSequence; }
     UInt_t GetLastRun() const  { return fLastRun; }
@@ -186,5 +187,5 @@
     static TString InflateRunPath(const MTime &night, Bool_t mc=kFALSE);
 
-    ClassDef(MSequence, 5) // Describes a sequences, reads and writes sequence files
+    ClassDef(MSequence, 6) // Describes a sequences, reads and writes sequence files
 };
 
Index: trunk/MagicSoft/Mars/mjobs/MSequenceSQL.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequenceSQL.cc	(revision 8999)
+++ trunk/MagicSoft/Mars/mjobs/MSequenceSQL.cc	(revision 9002)
@@ -91,6 +91,6 @@
     while ((row = res->Next()) && evts<nevts)
     {
-        AddFile(atoi((*row)[0]), 0, type);
-        evts += atoi((*row)[1]);
+        AddFile(atoi((*row)[0]), atoi((*row)[1]), type);
+        evts += atoi((*row)[2]);
         cnt++;
     }
@@ -178,14 +178,16 @@
 
     // Now prepare queries to request the runs from the database
-    TString where(Form(" FROM RunData WHERE fSequenceFirst=%d AND fExcludedFDAKEY=1"
-                       " AND fRunTypeKEY%%s", fSequence));
-
-    const TString query1(Form("SELECT fRunNumber, fNumEvents %s", where.Data()));
+    TString where(Form(" FROM RunData WHERE"
+                       " fTelescopeTelescope=%d AND fSequenceFirst=%d AND"
+                       " fExcludedFDAKEY=1 AND fRunTypeKEY%%s",
+                       fTelescope, fSequence));
+
+    const TString query1(Form("SELECT fRunNumber, fFileNumber, fNumEvents %s", where.Data()));
     const TString query2(Form("SELECT fRunStart, fRunStop %s", where.Data()));
 
-    const TString queryA(Form(query1.Data(), " BETWEEN 2 AND 4 ORDER BY fRunNumber"));
-    const TString queryC(Form(query1.Data(), "=4 ORDER BY fRunNumber"));
-    const TString queryD(Form(query1.Data(), "=2 ORDER BY fRunNumber"));
-    const TString queryT(Form(query2.Data(), "=4 ORDER BY fRunNumber LIMIT 1"));
+    const TString queryA(Form(query1.Data(), " BETWEEN 2 AND 4 ORDER BY fRunNumber*1000+fFileNumber"));
+    const TString queryC(Form(query1.Data(), "=4 ORDER BY fRunNumber*1000+fFileNumber"));
+    const TString queryD(Form(query1.Data(), "=2 ORDER BY fRunNumber*1000+fFileNumber"));
+    const TString queryT(Form(query2.Data(), "=4 ORDER BY fRunNumber*1000+fFileNumber LIMIT 1"));
 
     // Try to get a valid time for the first calibration run
@@ -266,5 +268,5 @@
 //  kTrue in case of success.
 //
-Bool_t MSequenceSQL::GetFromDatabase(MSQLMagic &serv, Int_t sequno)
+Bool_t MSequenceSQL::GetFromDatabase(MSQLMagic &serv, Int_t sequno, Int_t tel)
 {
     // Check if we are connected to the sql server
@@ -275,5 +277,12 @@
     }
 
-    // check if any sequence number os avlid
+    // check if any telescope number is avlid
+    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)
     {
@@ -284,4 +293,6 @@
     // set "filename" and sequence number
     fFileName = serv.GetName();
+    if (tel>0)
+        fTelescope = tel;
     if (sequno>=0)
         fSequence = sequno;
@@ -292,6 +303,7 @@
                   " fLightConditionsKEY, fRunStart, fNumEvents, "
                   " fZenithDistanceMin, fZenithDistanceMax "
-                  " FROM Sequences WHERE fSequenceFirst=");
-    query += fSequence;
+                  " FROM Sequences WHERE ");
+    query += Form("fTelescopeNumber=%d AND fSequenceFirst=%d",
+                  fTelescope, fSequence);
 
     // Request information from database
@@ -302,5 +314,8 @@
     // invalidate sequence if retrieval failed
     if (!rc)
-        fSequence = (UInt_t)-1;
+    {
+        fTelescope = 0;
+        fSequence  = (UInt_t)-1;
+    }
 
     // return result
@@ -330,7 +345,7 @@
 //  kTrue in case of success.
 //
-Bool_t MSequenceSQL::GetFromDatabase(const char *rc, Int_t sequno)
+Bool_t MSequenceSQL::GetFromDatabase(const char *rc, Int_t sequno, Int_t tel)
 {
     MSQLMagic serv(rc);
-    return GetFromDatabase(serv, sequno);
-}
+    return GetFromDatabase(serv, sequno, tel);
+}
Index: trunk/MagicSoft/Mars/mjobs/MSequenceSQL.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequenceSQL.h	(revision 8999)
+++ trunk/MagicSoft/Mars/mjobs/MSequenceSQL.h	(revision 9002)
@@ -20,11 +20,11 @@
 
 public:
-    MSequenceSQL(MSQLMagic &serv, Int_t seqno=-1)  { GetFromDatabase(serv, seqno); }
-    MSequenceSQL(const char *rc, Int_t seqno=-1)   { GetFromDatabase(rc, seqno); }
-    MSequenceSQL(Int_t seqno=-1)                   { GetFromDatabase(seqno); }
+    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); }
 
-    Bool_t GetFromDatabase(MSQLMagic &serv, Int_t seqno=-1);
-    Bool_t GetFromDatabase(const char *rc, Int_t seqno=-1);
-    Bool_t GetFromDatabase(Int_t seqno=-1) { return GetFromDatabase("sql.rc", seqno); }
+    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); }
 
     ClassDef(MSequenceSQL, 0) // Extension of MSequence to get a sequence from a database
