Index: trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C	(revision 8061)
+++ trunk/MagicSoft/Mars/datacenter/macros/buildsequenceentries.C	(revision 8065)
@@ -259,11 +259,4 @@
     TList fListRegexp;
 
-    /*
-    TString GetKeyName(TString col, TString key)
-    {
-        col(TRegexp("^Elt")) = "";
-        return QueryNameOfKey(col, key, kFALSE);
-    }*/
-
     Int_t CheckTransition(TSQLResult &res, const TString *keys, TSQLRow &row, Int_t i)
     {
@@ -330,11 +323,23 @@
         delete res;
 
+        const TString elts = GetELTSource(where);
+        if (elts.IsNull())
+            return kFALSE;
+
+        const TString eltp = GetELTProject(where);
+        if (eltp.IsNull())
+            return kFALSE;
+
         // ========== Request data of sequence ==========
-        query = Form("SELECT fSourceKEY, fProjectKEY, "
-                     " fL1TriggerTableKEY, fL2TriggerTableKEY,"
-                     " fHvSettingsKEY, fDiscriminatorThresholdTableKEY,"
-                     " fTriggerDelayTableKEY, fObservationModeKEY "
-                     " FROM RunData WHERE fRunTypeKEY=2 AND %s"
-                     " LIMIT 1", where.Data());
+        query  = "SELECT ";
+        query += elts;
+        query += ", ";
+        query += eltp;
+        query += ", fL1TriggerTableKEY, fL2TriggerTableKEY,"
+            " fHvSettingsKEY, fDiscriminatorThresholdTableKEY,"
+            " fTriggerDelayTableKEY, fObservationModeKEY "
+            " FROM RunData WHERE fRunTypeKEY=2 AND ";
+        query += where;
+        query += " LIMIT 1";
 
         res = Query(query);
@@ -692,4 +697,53 @@
     }
 
+    TString GetELTQuery(const char *col, const char *cond) const
+    {
+        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);
+    }
+
+    TString GetELTSource(const char *cond)
+    {
+        //query all sources observed in this night
+        TSQLResult *resx = Query(GetELTQuery("Source", cond));
+        if (!resx)
+            return "";
+
+        // In the case there is only a single source
+        // do not replace the source key by the ELT
+        if (resx->GetRowCount()==1)
+            return "fSourceKEY";
+
+        TString elts = GetELT("Source", resx, "\\-?W[1-9][ abc]?$");
+        delete resx;
+
+        return elts;
+    }
+
+    TString GetELTProject(const char *cond)
+    {
+        //query all project names observed in this night
+        TSQLResult *resx = Query(GetELTQuery("Project", cond));
+        if (!resx)
+            return "";
+
+        // In the case there is only a single project
+        // do not replace the project key by the ELT
+        if (resx->GetRowCount()==1)
+            return "fProjectKEY";
+
+        TList regexp;
+        regexp.Add(new TObjString("\\-?W[1-9][abc]?$"));
+        regexp.Add(new TObjString("\\-W[0-9]\\.[0-9][0-9]\\+[0-9][0-9][0-9]$"));
+
+        TString eltp2 = GetELT("Project", resx, regexp);
+        delete resx;
+        regexp.Delete();
+
+        return eltp2;
+    }
+
     Bool_t HasAtLeastOne(TString src, TString chk) const
     {
@@ -1042,10 +1096,5 @@
         fMap.DeleteAll();
     }
-    /*
-     SequenceBuild(SequenceBuild &sb) : MSQLServer(sb)
-     {
-        fPathRawData = sb.fPathRawData;
-        fPathSequences = sb.fPathSequences;
-    }*/
+
     void SetPathRawData(const char *path) { fPathRawData=path; }
     void SetPathSequences(const char *path) { fPathSequences=path; }
@@ -1059,34 +1108,16 @@
         const TString cond =
             Form("(fRunStart>ADDDATE(\"%s\", INTERVAL -1 DAY) AND fRunStart<\"%s\") "
-                 "AND fExcludedFDAKEY=1 AND fRunTypeKEY BETWEEN 2 AND 4 "
-                 "ORDER BY fRunNumber",
+                 "AND fExcludedFDAKEY=1 AND fRunTypeKEY BETWEEN 2 AND 4 ",
                  day.Data(), day.Data());
 
-
-
         //query all sources observed in this night
-        TString querys(Form("SELECT RunData.fSourceKEY, fSourceName FROM RunData "
-                            "LEFT JOIN Source ON RunData.fSourceKEY=Source.fSourceKEY "
-                            "WHERE %s", cond.Data()));
-        TSQLResult *resx = Query(querys);
-        if (!resx)
-            return 2;
-        TString elts = GetELT("Source", resx, "\\-?W[1-9][ abc]?$");
-        delete resx;
+        const TString elts = GetELTSource(cond);
+        if (elts.IsNull())
+            return 2;
 
         //query all project names observed in this night
-        TString queryp(Form("SELECT RunData.fProjectKEY, fProjectName FROM RunData "
-                            "LEFT JOIN Project ON RunData.fProjectKEY=Project.fProjectKEY "
-                            "WHERE %s", cond.Data()));
-        resx = Query(queryp);
-        if (!resx)
-            return 2;
-
-        TList regexp;
-        regexp.Add(new TObjString("\\-?W[1-9][abc]?$"));
-        regexp.Add(new TObjString("\\-W[0-9]\\.[0-9][0-9]\\+[0-9][0-9][0-9]$"));
-        TString eltp2 = GetELT("Project", resx, regexp);
-        delete resx;
-        regexp.Delete();
+        const TString eltp2 = GetELTProject(cond);
+        if (elts.IsNull())
+            return 2;
 
         // Setup query to get all values from the database,
@@ -1106,5 +1137,5 @@
                 query += mapkey->GetName();
             }
-        query += Form(" FROM RunData WHERE %s", cond.Data());
+        query += Form(" FROM RunData WHERE %s ORDER BY fRunNumber", cond.Data());
 
         TSQLResult *res = Query(query);
