Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 9018)
+++ trunk/MagicSoft/Mars/Changelog	(revision 9019)
@@ -53,4 +53,22 @@
    * mjobs/Makefile, mjobs/JobsLinkDef.h:
      - added MJMerpp
+
+   * datacenter/macros/checkfileavail.C, datacenter/macros/checkstardone.C:
+     - reduced queries to a single query
+     - take sequence number as argumenet instead of filename
+
+   * datacenter/scripts/checkstardone:
+     - adapted to changes in checkstardone.C
+
+   * mjobs/MSequence.[h,cc]:
+     - a lot of rework to the file-interface
+     - included subsystem (cc) files
+     - some renaming to some functions
+     - improved the interface to the database
+     - unified the Setup*Runs into GetRuns
+     - when inflating a sequence name allow to use [tel:]squence now
+
+   * mjobs/MSequenceSQL.[h,cc]:
+     - adapted to changes in MSequence
 
 
Index: trunk/MagicSoft/Mars/mjobs/MSequence.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequence.cc	(revision 9018)
+++ trunk/MagicSoft/Mars/mjobs/MSequence.cc	(revision 9019)
@@ -141,5 +141,5 @@
 //    seq.SetNight("2004-07-06");
 //    seq.AddFiles(31753, 0, 120);
-//    seq.SetupRuns(iter);
+//    seq.GetRuns(iter);
 //    seq.SetupPedRuns(iter, "/mypath", "[DPC]");
 //
@@ -386,5 +386,5 @@
 }
 
-TString MSequence::InflateRunPath(const MTime &night, Bool_t mc)
+TString MSequence::InflateFilePath(const MTime &night, Bool_t mc)
 {
     TString rc = GetStandardPath(mc);
@@ -394,7 +394,7 @@
 }
 
-TString MSequence::InflateRunPath(const MTime &night, UShort_t tel, Int_t run, Int_t file, Int_t type)
-{
-    return InflateRunPath(night)+InflateRunName(night, tel, run, file, type);
+TString MSequence::InflateFilePath(const MTime &night, UShort_t tel, Int_t run, Int_t file, Int_t type)
+{
+    return InflateFilePath(night)+InflateFileName(night, tel, run, file, type);
 }
 
@@ -411,15 +411,13 @@
     {
         d = GetStandardPath();
-        switch (type)
+        switch (type&kFileType)
         {
-        case kRawDat:  // rawdata
-        case kRawPed:
-        case kRawCal:
-        case kRawAll:
-        case kRootDat: // mcdata
-        case kRootPed:
-        case kRootCal:
-        case kRootAll:
+        case kRaw:  // rawdata
+        case kRoot: // mcdata
             d += "rawfiles/";
+            d += fNight.GetStringFmt("%Y/%m/%d");
+            break;
+        case kReport:
+            d += "../subsystemdata/cc/";
             d += fNight.GetStringFmt("%Y/%m/%d");
             break;
@@ -441,32 +439,40 @@
 }
 
-TString MSequence::InflateRunName(const MTime &night, UShort_t tel, Int_t run, Int_t file, Int_t type)
+// --------------------------------------------------------------------------
+//
+// Compile FileName according to night, tel, run, file and type.
+//
+TString MSequence::InflateFileName(const MTime &night, UShort_t tel, Int_t run, Int_t file, Int_t type)
 {
     const char *id="_";
-    switch (type)
-    {
-    case kRawDat:
-    case kRootDat:
+    switch ((RunType_t)(type&kRunType))
+    {
+    case kDat:
         id = "D";
         break;
-    case kRawPed:
-    case kRootPed:
+    case kPed:
         id = "P";
         break;
-    case kRawCal:
-    case kRootCal:
+    case kCal:
         id = "C";
         break;
-    case kRawAll:
-    case kRootAll:
+    case kAll:
         id = "[PCD]";
         break;
-    case kCalibrated:
+    case kCalib:
         id = "Y";
         break;
-    case kImages:
+    case kImage:
         id = "I";
         break;
-    }
+    case kExcl:
+        break;
+    }
+
+    // Is report file?
+    const Bool_t isrep = type&kReport;
+
+    // Calculate limit for transition from 5-digit to 8-digit numbers
+    const Int_t limit = isrep ? 39373 : 35487;
 
     // ------------- Create file name --------------
@@ -484,5 +490,5 @@
         // Changes to read the DAQ numbering format. Changes takes place
         // between runs 35487 and 00035488 (2004_08_30)
-        n += Form(run>35487 ? "%08d" : "%05d", run);
+        n += Form(run>limit ? "%08d" : "%05d", run);
 
         if (tel>0)
@@ -494,17 +500,18 @@
     n += "_*";
 
-    if (tel==0)
-        n += "_E";
-
-    switch (type)
-    {
-    case kRawDat:
-    case kRawPed:
-    case kRawCal:
-    case kRawAll:
-        n += ".raw.?g?z?";  // TPRegexp: (\.gz)?
+    if (tel==0 && run>=0)
+        n += isrep ? "_S" : "_E";
+
+    switch (type&kFileType)
+    {
+    case kRaw:       // MC     // DATA
+        n += run<0 ? ".root" : ".raw.?g?z?";  // TPRegexp: (\.gz)?
+        break;
+    case kReport:
+        n += ".rep";  // TPRegexp: (\.gz)?
         break;
     default:
         n += ".root";
+        break;
     }
 
@@ -519,7 +526,57 @@
 // the extension is defined by the type as well.
 //
-TString MSequence::GetFileName(UInt_t i, const TArrayI &arr, const TArrayI &sub, FileType_t type) const
-{
-    return InflateRunName(fNight, arr[i]>999999?fTelescope:0, fMonteCarlo?-arr[i]:arr[i], sub.GetSize()>0?sub[i]:0, type);
+TString MSequence::GetFileRegexp(UInt_t i, const TArrayI &arr, const TArrayI &sub, FileType_t type) const
+{
+    if (i>=(UInt_t)arr.GetSize())
+        return "";
+
+    return InflateFileName(fNight, arr[i]>999999?fTelescope:0, fMonteCarlo?-arr[i]:arr[i], sub.GetSize()>0?sub[i]:0, type);
+}
+
+// --------------------------------------------------------------------------
+//
+// Get the filename from the dirctory and the file-regexp.
+// return "" if file is not accessible or the regular expression is not
+// unambiguous.
+//
+TString MSequence::FindFile(const TString &d, const TString &f) const
+{
+    // Check existance and accessibility of file
+    MDirIter file(d, f, 0);
+
+    TString name = file();
+    gSystem->ExpandPathName(name);
+    if (gSystem->AccessPathName(name, kFileExists))
+    {
+        *fLog << err;
+        *fLog << "ERROR - File " << d << f << " not accessible!" << endl;
+        return "";
+    }
+    if (!file().IsNull())
+    {
+        *fLog << err;
+        *fLog << "ERROR - Searching for file " << d << f << " gave more than one result!" << endl;
+        return "";
+    }
+
+    return name;
+}
+
+// --------------------------------------------------------------------------
+//
+// Set the path accoring to type (if fDataPath=path=0), fDataPath
+// (if path=0) or path.
+// Returns accessability of the path.
+//
+Bool_t MSequence::FindPath(TString &path, FileType_t type) const
+{
+    if (path.IsNull())
+        path = fDataPath;
+
+    const Bool_t def = path.IsNull();
+
+    path = GetPathName(path, type);
+
+    return def;
 }
 
@@ -528,13 +585,7 @@
 // Add the entries from the arrays to the MDirIter
 //
-UInt_t MSequence::SetupRuns(MDirIter &iter, const TArrayI &arr, const TArrayI &sub, FileType_t type, const char *path) const
-{
-    TString d(path);
-    if (d.IsNull())
-        d = fDataPath;
-
-    const Bool_t def = d.IsNull();
-
-    d = GetPathName(d, type);
+UInt_t MSequence::GetRuns(MDirIter &iter, const TArrayI &arr, const TArrayI &sub, FileType_t type, TString path) const
+{
+    const Bool_t def = FindPath(path, type);
 
     // For this particular case we assume that the files are added one by
@@ -551,25 +602,11 @@
         }
 
-        const TString n = GetFileName(i, arr, sub, type);
-
-        // Check existance and accessibility of file
-        MDirIter file(d, n, 0);
-        TString name = file();
-        gSystem->ExpandPathName(name);
-        if (gSystem->AccessPathName(name, kFileExists))
-        {
-            *fLog << err;
-            *fLog << "ERROR - File " << d << n << " not accessible!" << endl;
-            return 0;
-        }
-        if (!file().IsNull())
-        {
-            *fLog << err;
-            *fLog << "ERROR - Searching for file " << d << n << " gave more than one result!" << endl;
-            return 0;
-        }
+        const TString n = GetFileRegexp(i, arr, sub, type);
+
+        if (FindFile(path, n).IsNull())
+            continue;
 
         // Add Path/File to TIter
-        iter.AddDirectory(d, n, 0);
+        iter.AddDirectory(path, n, 0);
     }
 
@@ -584,5 +621,5 @@
         *fLog << "ERROR - No input files for sequence #" << GetSequence() << endl;
         *fLog << "        read from " << GetBaseName() << endl;
-        *fLog << "        found in" << (def?" default-path ":" ") << d << endl;
+        *fLog << "        found in" << (def?" default-path ":" ") << path << endl;
         return 0;
     }
@@ -593,5 +630,5 @@
     *fLog << err;
     *fLog << "ERROR - " << n1 << " input files for sequence #" << GetSequence() << " found in" << endl;
-    *fLog << "        " << (def?" default-path ":" ") << d << endl;
+    *fLog << "        " << (def?" default-path ":" ") << path << endl;
     *fLog << "        but " << n2 << " files were defined in sequence file" << endl;
     *fLog << "        " << GetBaseName() << endl;
@@ -602,4 +639,170 @@
     iter.Print();
     return 0;
+}
+
+// --------------------------------------------------------------------------
+//
+// Returns "0" if file at i-th index is excluded. Get the access-path
+// defined by the default or data-path (if path=0) or path.
+// Get the full file-name by accessing the file.
+//
+TString MSequence::GetFileName(UInt_t i, const TArrayI &arr, const TArrayI &sub, FileType_t type, const char *path) const
+{
+    if (IsExcluded(arr[i], sub[i]))
+        return "0";
+
+    const TString d = FindPath(type, path);
+    const TString n = GetFileRegexp(i, arr, sub, type);
+
+    return FindFile(d, n);
+}
+
+// --------------------------------------------------------------------------
+//
+// Return the pointers to the arrays with the runs/files depending on the
+// given type.
+//
+void MSequence::GetArrays(const TArrayI* &r, const TArrayI* &f, Int_t type) const
+{
+    switch ((RunType_t)(type&kRunType))
+    {
+    case kPed:
+        r = &fPedRuns;
+        f = &fPedRunsSub;
+        break;
+    case kDat:
+    case kImage:
+    case kCalib:
+        r = &fDatRuns;
+        f = &fDatRunsSub;
+        break;
+    case kCal:
+        r = &fCalRuns;
+        f = &fCalRunsSub;
+        break;
+    case kExcl:
+        r = &fExclRuns;
+        f = &fExclRunsSub;
+        break;
+    case kAll:
+        r = &fRuns;
+        f = &fRunsSub;
+        break;
+    }
+}
+
+// --------------------------------------------------------------------------
+//
+// Add runs from the sequence to MDirIter. If path==0 fDataPath is
+// used instead. If it is also empty the standard path of the
+// data-center is assumed. If you have the runs locally use path="."
+//
+// Returns the number of files added.
+//
+// Runs which are in fExlRuns are ignored.
+//
+UInt_t MSequence::GetRuns(MDirIter &iter, FileType_t type, const char *path) const
+{
+    const TArrayI *r=0, *f=0;
+    GetArrays(r, f, type);
+    return GetRuns(iter, *r, *f, type, path);
+}
+
+// --------------------------------------------------------------------------
+//
+// Returns the num-th entry of FileType_t as regular expression.
+// If the file is excluded return "".
+// To get the number of valid entries use GetNumEntries(FileType_t)
+//
+//  e.g.
+//        if (GetNumEntries(MSequence::kDat))<5)
+//            GetFileRegexp(3, MSequence::kRawDat);
+//
+TString MSequence::GetFileRegexp(UInt_t num, FileType_t type) const
+{
+    const TArrayI *r=0, *f=0;
+    GetArrays(r, f, type);
+
+    TString rc = GetFileRegexp(num, *r, *f, type);
+    rc.ReplaceAll(".", "\\.");
+    rc.ReplaceAll("*", ".*");
+    return rc;
+}
+
+// --------------------------------------------------------------------------
+//
+// Returns the num-th entry of FileType_t as filename. To access the
+// path the default is used if path IsNull, path otherwise.
+// If the file could not be accessed "" is returned, if it is
+// excluded "0".
+// To get the number of valid entries use GetNumEntries(FileType_t)
+//
+//  e.g.
+//        if (GetNumEntries(MSequence::kDat))<5)
+//            GetFileName(3, MSequence::kRawDat);
+//
+TString MSequence::GetFileName(UInt_t num, FileType_t type, const char *path) const
+{
+    const TArrayI *r=0, *f=0;
+    GetArrays(r, f, type);
+    return GetFileName(num, *r, *f, type, path);
+}
+
+// --------------------------------------------------------------------------
+//
+// Returns the num-th entry of FileType_t as filename.
+// If the file could not be accessed or excluded return "".
+// To get the number of valid entries use GetNumEntries(RunType_t)
+//
+UInt_t MSequence::GetNumEntries(RunType_t type) const
+{
+    const TArrayI *r=0, *f=0;
+    GetArrays(r, f, type);
+    return r->GetSize();
+}
+
+// --------------------------------------------------------------------------
+//
+// Returns the num-th entry of RunType_t as run- and file-number.
+// Returns -1 if index is out of range, 0 if file is excluded and
+// 1 in case of success.
+//
+//  e.g.
+//        UInt_t run, file;
+//        GetFile(3, MSequence::kDat, run, file);
+//
+Int_t MSequence::GetFile(UInt_t i, RunType_t type, UInt_t &run, UInt_t &file) const
+{
+    const TArrayI *r=0, *f=0;
+    GetArrays(r, f, type);
+
+    if (i>=(UInt_t)r->GetSize())
+        return -1;
+
+    run  = (*r)[i];
+    file = i<(UInt_t)f->GetSize() ? (*f)[i] : 0;
+
+    if (IsExcluded(run, file))
+        return 0;
+
+    return 1;
+}
+
+// --------------------------------------------------------------------------
+//
+// Add a file (run/file) to the arrays defined by RunType_t
+//
+void MSequence::AddFile(UInt_t run, UInt_t file, RunType_t type)
+{
+    const TArrayI *rconst=0, *fconst=0;
+
+    GetArrays(rconst, fconst, type);
+
+    TArrayI &r=*const_cast<TArrayI*>(rconst);
+    TArrayI &f=*const_cast<TArrayI*>(fconst);
+
+    AddEntry(run, file, r, f);
+
+    SortArrays(r, f);
 }
 
@@ -631,31 +834,45 @@
 // --------------------------------------------------------------------------
 //
-// Read the file fname as setup file for the sequence.
-//
-MSequence::MSequence(const char *fname, const char *path, UInt_t seq)
-{
-    fName  = "MSequence";
-    fTitle = "Sequence file";
-
-    fFileName = fname;
+// Set the path to the data. If no path is given the data-center default
+// path is used to access the files.
+//
+// kTRUE is returned if the path is acessible, kFALSE otherwise.
+// (In case of the default path kTRUE is returned in any case)
+//
+Bool_t MSequence::SetDataPath(const char *path)
+{
     fDataPath = path;
 
+    gSystem->ExpandPathName(fDataPath);
+
+    const Bool_t rc = !fDataPath.IsNull() && gSystem->AccessPathName(fDataPath, kFileExists);
+    if (rc)
+        gLog << err << "ERROR - Directory '" << path << "' doesn't exist." << endl;
+
+    return !rc;
+}
+
+// --------------------------------------------------------------------------
+//
+// Read a sequence from a file. To set the location of the data (datapath)
+// use SetDataPath. The sequence number given is used if no sequence
+// number is found in the sequence file.
+//
+// If the file could not be found or accesed kFALSE is returned, kTRUE
+// otherwise.
+//
+Bool_t MSequence::ReadFile(const char *filename, UInt_t seq)
+{
+    fFileName = filename;
+
     gSystem->ExpandPathName(fFileName);
-    gSystem->ExpandPathName(fDataPath);
-
-    const Bool_t rc1 = gSystem->AccessPathName(fFileName, kFileExists);
-    const Bool_t rc2 = !fDataPath.IsNull() && gSystem->AccessPathName(fDataPath, kFileExists);
-
-    if (rc1)
-        gLog << err << "ERROR - Sequence file '" << fname << "' doesn't exist." << endl;
-    if (rc2)
-        gLog << err << "ERROR - Directory '" << path << "' doesn't exist." << endl;
+
+    const Bool_t rc = gSystem->AccessPathName(fFileName, kFileExists);
+    if (rc)
+        gLog << err << "ERROR - Sequence file '" << filename << "' doesn't exist." << endl;
 
     MEnv env(fFileName);
 
     fSequence = (UInt_t)env.GetValue("Sequence", (Int_t)seq);
-    if (rc1 || rc2)
-        fSequence = (UInt_t)-1;
-
 
     const TString prefix = Form("Sequence%08d", fSequence);
@@ -703,4 +920,77 @@
         env.PrintUntouched();
     }
+
+    return !rc;
+}
+
+// --------------------------------------------------------------------------
+//
+// Instantiate a file from either a file or the database.
+//
+// If not valid sequence could be created the sequence is invalidated,
+// which can be queried with IsValid.
+//
+//  see ReadFile, ReadDatabase and SetDataPath for more details.
+//
+MSequence::MSequence(const char *fname, const char *path, UInt_t seq)
+{
+    // regular expression to distinguish beween a mysql-path and
+    // a filename from where the sequence should be retrieved
+    TPRegexp regexp("mysql://[a-zA-Z0-9]+:.+@[a-zA-Z0-9]+/[a-zA-Z0-9]+/([0-9]+:)?[0-9]+");
+
+    const TString db = TString(fname)(regexp);
+
+    const Bool_t rc1 = db.IsNull() ? ReadFile(fname, seq) : ReadDatabase(fname, seq);
+    const Bool_t rc2 = SetDataPath(path);
+
+    fName  = "MSequence";
+    fTitle = "Sequence file";
+
+    if (!rc1 || !rc2)
+        fSequence = (UInt_t)-1;
+}
+
+// --------------------------------------------------------------------------
+//
+// Read a sequence from the database. Give the path to the database as:
+//    mysql://user:password@name/database
+// In this case you MUST give a sequence number.
+//
+// You can code the telescope and sequence number into the path and
+// omit the arguments:
+//    mysql://user:password@name/database/[tel:]sequence
+//
+// The third option is to give just the sql.rc as string in the argument.
+// In this case you MUST give a sequence number.
+//
+// If the telecope number is omitted as argument, i.e. ==0, or in the
+// database path, a sequence is read from the database if the sequence
+// number is unambiguous.
+//
+// kFALSE is returned if the final sequence is invalid, kTRUE in case of
+// success.
+//
+Bool_t MSequence::ReadDatabase(TString name, UInt_t seq, UShort_t tel)
+{
+    TPRegexp regexp("([0-9]+:)?[0-9]+$");
+
+    const TString telseq = name(regexp);
+
+    if (!telseq.IsNull())
+    {
+        const Ssiz_t p = telseq.First(':');
+
+        const TString strtel = p<0 ? TString("") : telseq(0, p);
+        const TString strseq = telseq(p+1, telseq.Length());
+
+        tel = strtel.Atoi();
+        seq = strseq.Atoi();
+
+        name = gSystem->DirName(name);
+    }
+
+    *this = MSequenceSQL(name, seq, tel);
+
+    return IsValid();
 }
 
@@ -715,9 +1005,18 @@
 //    MSequence seq(mysql, sequence, telescope);
 //
-// For more details see MSequenceSQL.
+// Is the telescope number is omitted (==0) the database is checked for
+// an unambiguous sequence.
+//
+// For more details see ReadDatabase and MSequenceSQL.
 //
 MSequence::MSequence(const char *fname, UInt_t seq, UShort_t tel)
 {
-    *this = MSequenceSQL(fname, seq, tel);
+    const Bool_t rc = ReadDatabase(fname, seq, tel);
+
+    fName  = "MSequence";
+    fTitle = "Sequence file";
+
+    if (!rc)
+        fSequence = (UInt_t)-1;
 }
 
@@ -1084,12 +1383,13 @@
     {
         // FIXME: This needs more check!
+        // FORBID the + syntax in "Runs"
         // 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);
-        /*str +=*/ PrintRuns(out, pre, "PedRuns:  ", fPedRuns,  fPedRunsSub);
-        /*str +=*/ PrintRuns(out, pre, "DataRuns: ", fDatRuns,  fDatRunsSub);
-        /*str +=*/ PrintRuns(out, pre, "Exclude:  ", fExclRuns, fExclRunsSub);
+        /*str +=*/ PrintRuns(out, pre, "Runs:     ", fRuns,     fRunsSub);
+        str += PrintRuns(out, pre, "CalRuns:  ", fCalRuns,  fCalRunsSub);
+        str += PrintRuns(out, pre, "PedRuns:  ", fPedRuns,  fPedRunsSub);
+        str += PrintRuns(out, pre, "DataRuns: ", fDatRuns,  fDatRunsSub);
+        str += PrintRuns(out, pre, "Exclude:  ", fExclRuns, fExclRunsSub);
     }
 
@@ -1144,84 +1444,4 @@
 // --------------------------------------------------------------------------
 //
-// Add all ped runs from the sequence to MDirIter.
-// If path==0 fDataPath is used instead. If it is also empty
-// the standard path of the data-center is assumed.
-// If you have the runs locally use path="."
-// Using raw=kTRUE you get correspodning raw-files setup.
-// Return the number of files added.
-//
-// Runs which are in fExlRuns are ignored.
-//
-UInt_t MSequence::SetupPedRuns(MDirIter &iter, const char *path, Bool_t raw) const
-{
-    return SetupRuns(iter, fPedRuns, fPedRunsSub, raw?kRawPed:kRootPed, path);
-}
-
-// --------------------------------------------------------------------------
-//
-// Add all data runs from the sequence to MDirIter.
-// If path==0 fDataPath is used instead. If it is also empty
-// the standard path of the data-center is assumed.
-// If you have the runs locally use path="."
-// Using raw=kTRUE you get correspodning raw-files setup.
-// Return the number of files added.
-//
-// Runs which are in fExlRuns are ignored.
-//
-UInt_t MSequence::SetupDatRuns(MDirIter &iter, const char *path, Bool_t raw) const
-{
-    return SetupRuns(iter, fDatRuns, fDatRunsSub, raw?kRawDat:kRootDat, path);
-}
-
-// --------------------------------------------------------------------------
-//
-// Add all runs from the sequence to MDirIter.
-// If path==0 fDataPath is used instead. If it is also empty
-// the standard path of the data-center is assumed.
-// If you have the runs locally use path="."
-// Using raw=kTRUE you get correspodning raw-files setup.
-// Return the number of files added.
-//
-// Runs which are in fExlRuns are ignored.
-//
-UInt_t MSequence::SetupAllRuns(MDirIter &iter, const char *path, Bool_t raw) const
-{
-    return SetupRuns(iter, fRuns, fRunsSub, raw?kRawAll:kRootAll, path);
-}
-
-// --------------------------------------------------------------------------
-//
-// Add all calibration runs from the sequence to MDirIter.
-// If path==0 fDataPath is used instead. If it is also empty
-// the standard path of the data-center is assumed.
-// If you have the runs locally use path="."
-// Using raw=kTRUE you get correspodning raw-files setup.
-// Return the number of files added.
-//
-// Runs which are in fExlRuns are ignored.
-//
-UInt_t MSequence::SetupCalRuns(MDirIter &iter, const char *path, Bool_t raw) const
-{
-    return SetupRuns(iter, fCalRuns, fCalRunsSub, raw?kRawCal:kRootCal, path);
-}
-
-// --------------------------------------------------------------------------
-//
-// Add all data runs from the sequence to MDirIter.
-// If path==0 fDataPath is used instead. If it is also empty
-// the standard path of the data-center is assumed.
-// If you have the runs locally use path="."
-// Using raw=kTRUE you get correspodning raw-files setup.
-// Return the number of files added.
-//
-// Runs which are in fExlRuns are ignored.
-//
-UInt_t MSequence::SetupDatRuns(MDirIter &iter, FileType_t type, const char *path) const
-{
-    return SetupRuns(iter, fDatRuns, fDatRunsSub, type, path);
-}
-
-// --------------------------------------------------------------------------
-//
 // check if the run/file is contained in the arrays.
 //
@@ -1242,40 +1462,4 @@
     }
     return kFALSE;
-}
-
-// --------------------------------------------------------------------------
-//
-// Add a file (run/file) to the arrays defined by type (P, C, D, X)
-//
-void MSequence::AddFile(UInt_t run, UInt_t file, char type)
-{
-    TArrayI *r=0, *f=0;
-    switch (type)
-    {
-    case 'P':
-        r = &fPedRuns;
-        f = &fPedRunsSub;
-        break;
-    case 'D':
-        r = &fDatRuns;
-        f = &fDatRunsSub;
-        break;
-    case 'C':
-        r = &fCalRuns;
-        f = &fCalRunsSub;
-        break;
-    case 'X':
-        r = &fExclRuns;
-        f = &fExclRunsSub;
-        break;
-    default:
-        r = &fRuns;
-        f = &fRunsSub;
-        break;
-    }
-
-    AddEntry(run, file, *r, *f);
-
-    SortArrays(*r, *f);
 }
 
@@ -1339,13 +1523,28 @@
 Bool_t MSequence::InflateSeq(TString &seq, Bool_t ismc)
 {
-    if (seq.IsDigit())
-    {
-        const Int_t numseq = seq.Atoi();
-        seq = "/magic/";
-        if (ismc)
+    TObjArray *arr = seq.Tokenize(':');
+    if (arr->GetEntries()<3)
+    {
+        TString num1 = (*arr)[0]->GetName();
+        TString num2 = arr->GetEntries()==2 ? (*arr)[1]->GetName() : "";
+
+        if (num1.IsDigit() && num2.IsDigit())
+        {
+            gLog << err << "ERROR - InflateSeq 'telescope:sequence' not yet implemented." << endl;
+            delete arr;
+            return kFALSE;
+        }
+
+        if (num1.IsDigit() && num2.IsNull())
+        {
+            const Int_t numseq = seq.Atoi();
+            seq = "/magic/";
+            if (ismc)
             seq += "montecarlo/";
-        seq += Form("sequences/%04d/sequence%08d.txt", numseq/10000, numseq);
-        gLog << inf << "Inflated sequence file: " << seq << endl;
-    }
+            seq += Form("sequences/%04d/sequence%08d.txt", numseq/10000, numseq);
+            gLog << inf << "Inflated sequence file: " << seq << endl;
+        }
+    }
+    delete arr;
 
     if (!gSystem->AccessPathName(seq, kFileExists))
Index: trunk/MagicSoft/Mars/mjobs/MSequence.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequence.h	(revision 9018)
+++ trunk/MagicSoft/Mars/mjobs/MSequence.h	(revision 9019)
@@ -15,10 +15,41 @@
 {
     friend class MSequenceSQL;
+
 public:
     enum LightCondition_t { kNA, kNoMoon, kTwilight, kMoon, kDay };
+
+    enum FileExt_t {
+        kRaw   =BIT(0),
+        kRoot  =BIT(1),
+        kReport=BIT(2),
+    };
+    enum RunType_t {
+        kPed   =BIT(3),
+        kCal   =BIT(4),
+        kDat   =BIT(5),
+        kExcl  =BIT(6),
+        kAll   =kPed|kDat|kCal,
+        kImage =BIT(7),
+        kCalib =BIT(8)
+    };
+
+    enum { kRunType  = kPed|kCal|kDat|kImage|kCalib };
+    enum { kFileType = kRaw|kRoot|kReport  };
+
     enum FileType_t {
-        kRawDat, kRawPed, kRawCal, kRawAll,
-        kRootDat, kRootPed, kRootCal, kRootAll,
-        kCalibrated, kImages
+        kRawDat     = kRaw   |kDat,
+        kRawPed     = kRaw   |kPed,
+        kRawCal     = kRaw   |kCal,
+        kRawAll     = kRaw   |kAll,
+        kRootDat    = kRoot  |kDat,
+        kRootPed    = kRoot  |kPed,
+        kRootCal    = kRoot  |kCal,
+        kRootAll    = kRoot  |kAll,
+        kReportPed  = kReport|kPed,
+        kReportCal  = kReport|kCal,
+        kReportDat  = kReport|kDat,
+        kReportAll  = kReport|kAll,
+        kCalibrated = kRoot  |kCalib,
+        kImages     = kRoot  |kImage,
     };
 
@@ -73,7 +104,12 @@
 
     // Helper for file setup
+    void    GetArrays(const TArrayI* &r, const TArrayI* &f, Int_t type) const;
+    Bool_t  FindPath(TString &path, FileType_t type) const;
+    TString FindPath(FileType_t type, TString path="") const { FindPath(path, type); return path; }
+    TString FindFile(const TString &d, const TString &f) const;
     TString GetPathName(TString d, FileType_t type) const;
-    TString GetFileName(UInt_t num, const TArrayI &arr, const TArrayI &sub, FileType_t type) const;
-    UInt_t  SetupRuns(MDirIter &iter, const TArrayI &arr, const TArrayI &file, FileType_t type, const char *path=0) const;
+    TString GetFileRegexp(UInt_t num, const TArrayI &arr, const TArrayI &sub, FileType_t type) const;
+    TString GetFileName(UInt_t i, const TArrayI &arr, const TArrayI &sub, FileType_t type, const char *path) const;
+    UInt_t  GetRuns(MDirIter &iter, const TArrayI &arr, const TArrayI &file, FileType_t type, TString path="") const;
 
     // Helper for Print()
@@ -121,6 +157,9 @@
 
     // I/O
+    Bool_t ReadDatabase(TString sql, UInt_t seq=0, UShort_t tel=0);
+    Bool_t ReadFile(const char *filename, UInt_t id=(UInt_t)-1);
     Bool_t WriteFile(const char *filename, const Option_t *o) const;
     Bool_t WriteFile(const char *filename) const { return WriteFile(filename,""); } //*MENU *ARGS={filename=>fBaseName}
+    Bool_t SetDataPath(const char *path="");
 
     // TObject
@@ -138,10 +177,10 @@
     void SetNight(const char*night);
 
-    void AddRun(UInt_t run, char type='*') { AddFile(run, 0, type); }
-    void AddRuns(UInt_t run1, UInt_t run2, char type='*') { for (UInt_t i=run1; i<=run2; i++) AddFile(i, 0, type); }
-    void AddFile(UInt_t run, UInt_t file, char type='*');
-    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 run, UInt_t file=0) { /*if (!force || !IsExcluded(run, file))*/ AddFile(run, file, 'X'); }
+    void AddRun(UInt_t run, RunType_t type=kAll) { AddFile(run, 0, type); }
+    void AddRuns(UInt_t run1, UInt_t run2, RunType_t type=kAll) { for (UInt_t i=run1; i<=run2; i++) AddFile(i, 0, type); }
+    void AddFile(UInt_t run, UInt_t file, RunType_t type=kAll);
+    void AddFiles(UInt_t run, UInt_t f1, UInt_t f2, RunType_t type=kAll) { for (UInt_t i=f1; i<=f2; i++) AddFile(run, i, type); }
+
+    void ExcludeFile(UInt_t run, UInt_t file=0) { /*if (!force || !IsExcluded(run, file))*/ AddFile(run, file, kExcl); }
     void ExcludeRuns(TString runs);
 
@@ -157,4 +196,6 @@
     Bool_t HasMoon() const { return fLightCondition==kMoon; }
 
+    Int_t  GetFile(UInt_t i, RunType_t type, UInt_t &run, UInt_t &file) const;
+
     LightCondition_t GetLightCondition() const { return fLightCondition; }
 
@@ -166,9 +207,8 @@
 
     // Filesystem interface
-    UInt_t SetupPedRuns(MDirIter &iter, const char *path=0, Bool_t raw=kFALSE) const;
-    UInt_t SetupDatRuns(MDirIter &iter, const char *path=0, Bool_t raw=kFALSE) const;
-    UInt_t SetupAllRuns(MDirIter &iter, const char *path=0, Bool_t raw=kFALSE) const;
-    UInt_t SetupCalRuns(MDirIter &iter, const char *path=0, Bool_t raw=kFALSE) const;
-    UInt_t SetupDatRuns(MDirIter &iter, FileType_t type, const char *path=0) const;
+    UInt_t  GetRuns(MDirIter &iter, FileType_t type, const char *path=0) const;
+    TString GetFileRegexp(UInt_t num, FileType_t type) const;
+    TString GetFileName(UInt_t i, FileType_t type, const char *path=0) const;
+    UInt_t  GetNumEntries(RunType_t type=kAll) const;
 
     // Filesystem getter
@@ -184,11 +224,11 @@
     static Bool_t  InflateSeq(TString &seq, /*FIXME: TELNUM*/ Bool_t ismc=kFALSE);
 
-    static TString InflateRunName(const MTime &night, UShort_t tel, Int_t run, Int_t file, Int_t type=kRawAll);
-    static TString InflateRunName(const MTime &night, Int_t run, Int_t type=kRawAll) { return InflateRunName(night, 1, run, 0, type); }
-
-    static TString InflateRunPath(const MTime &night, UShort_t tel, Int_t run, Int_t file=0, Int_t type=kRawAll);
-    static TString InflateRunPath(const MTime &night, Int_t run, Int_t type=kRawAll) { return InflateRunPath(night, 1, run, 0, type); }
-
-    static TString InflateRunPath(const MTime &night, Bool_t mc=kFALSE);
+    static TString InflateFileName(const MTime &night, UShort_t tel, Int_t run, Int_t file, Int_t type=kRawAll);
+    static TString InflateFileName(const MTime &night, Int_t run, Int_t type=kRawAll) { return InflateFileName(night, 1, run, 0, type); }
+
+    static TString InflateFilePath(const MTime &night, UShort_t tel, Int_t run, Int_t file=0, Int_t type=kRawAll);
+    static TString InflateFilePath(const MTime &night, Int_t run, Int_t type=kRawAll) { return InflateFilePath(night, 1, run, 0, type); }
+
+    static TString InflateFilePath(const MTime &night, Bool_t mc=kFALSE);
 
     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 9018)
+++ trunk/MagicSoft/Mars/mjobs/MSequenceSQL.cc	(revision 9019)
@@ -86,5 +86,5 @@
 // return kFALSE in case of error or if no events are in the files.
 //
-Bool_t MSequenceSQL::GetRuns(MSQLMagic &serv, TString query, char type, UInt_t nevts)
+Bool_t MSequenceSQL::GetRuns(MSQLMagic &serv, TString query, RunType_t type, UInt_t nevts)
 {
     TSQLResult *res = serv.Query(query);
@@ -211,11 +211,11 @@
 
     // get and setup runs
-    if (!GetRuns(serv, queryA))
-        return kFALSE;
-    if (!GetRuns(serv, queryC, 'C'))
-        return kFALSE;
-    if (!GetRuns(serv, queryP, 'P', 1000))
-        return kFALSE;
-    if (!GetRuns(serv, queryD, 'D'))
+    if (!GetRuns(serv, queryA, kAll))
+        return kFALSE;
+    if (!GetRuns(serv, queryC, kCal))
+        return kFALSE;
+    if (!GetRuns(serv, queryP, kPed, 1000))
+        return kFALSE;
+    if (!GetRuns(serv, queryD, kDat))
         return kFALSE;
 
Index: trunk/MagicSoft/Mars/mjobs/MSequenceSQL.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequenceSQL.h	(revision 9018)
+++ trunk/MagicSoft/Mars/mjobs/MSequenceSQL.h	(revision 9019)
@@ -14,5 +14,5 @@
 {
 private:
-    Bool_t  GetRuns(MSQLMagic &serv, TString query, char type='*', UInt_t nevts=(UInt_t)-1);
+    Bool_t  GetRuns(MSQLMagic &serv, TString query, RunType_t type, UInt_t nevts=(UInt_t)-1);
     TString GetTimeFromDatabase(MSQLMagic &serv, const TString &query);
     Bool_t  GetFromDatabase(MSQLMagic &serv, TSQLResult &res);
