Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 8051)
+++ trunk/MagicSoft/Mars/Changelog	(revision 8052)
@@ -59,4 +59,8 @@
    * mbase/MLogHtml.cc:
      - fixed a bug which caused the output to be totally corrupted
+
+   * datacenter/macros/writesequencefile.C:
+     - implemented again that the 1000 pedestal events are taken which
+       are next to the calibration run
 
 
Index: trunk/MagicSoft/Mars/datacenter/macros/writesequencefile.C
===================================================================
--- trunk/MagicSoft/Mars/datacenter/macros/writesequencefile.C	(revision 8051)
+++ trunk/MagicSoft/Mars/datacenter/macros/writesequencefile.C	(revision 8052)
@@ -57,9 +57,9 @@
 #include "MDirIter.h"
 
-#include "MSQLServer.h"
+#include "MSQLMagic.h"
 
 using namespace std;
 
-Bool_t GetRuns(MSQLServer &serv, ofstream &fout, TString query, TString name)
+Bool_t GetRuns(MSQLServer &serv, ofstream &fout, TString query, TString name, UInt_t nevts=(UInt_t)-1)
 {
     TSQLResult *res = serv.Query(query);
@@ -67,11 +67,13 @@
         return kFALSE;
 
-    Int_t cnt=0;
+    UInt_t cnt=0;
+    UInt_t evts=0;
 
     fout << name << ":";
     TSQLRow *row=0;
-    while ((row = res->Next()))
+    while ((row = res->Next()) && evts<nevts)
     {
         fout << " " << (*row)[0];
+        evts += atoi((*row)[1]);
         cnt++;
     }
@@ -89,27 +91,30 @@
 }
 
-TString GetName(MSQLServer &serv, const char *col, const char *n)
-{
-    TString query(Form("SELECT f%sName FROM %s WHERE f%sKEY=%s",
-                       col, col, col, n));
-    
-    TSQLResult *res = serv.Query(query);
+TString GetTime(const char *type)
+{
+    return Form("if(TIME_TO_SEC(fRun%s)<12*60*60,"
+                " TIME_TO_SEC(fRun%s)+24*60*60,"
+                " TIME_TO_SEC(fRun%s))", type, type, type);
+}
+
+Int_t GetTimeC(MSQLServer &serv, const TString &str)
+{
+    TSQLResult *res = serv.Query(str);
     if (!res)
-    {
-        cout << "ERROR - Resolving " << col << " failed! " << endl;
-        return "";
-    }
-
-    TSQLRow *row = res->Next();
+        return -1;
+
+    TSQLRow *row=res->Next();
     if (!row)
     {
-        cout << "ERROR - No name in " << col << " for key " << n << endl;
-        return "";
-    }
-
-    return (*row)[0];
-}
-
-Bool_t GetSequence(MSQLServer &serv, TSQLRow &data, TString sequpath)
+        delete res;
+        return -1;
+    }
+
+    Int_t rc = (*row)[0] ? atoi((*row)[0]) : -1;
+    delete res;
+    return rc;
+}
+
+Bool_t GetSequence(MSQLMagic &serv, TSQLRow &data, TString sequpath)
 {
     UShort_t y;
@@ -125,10 +130,10 @@
 
     TString str[6];
-    str[0] = GetName(serv, "Project",         data[2]);
-    str[1] = GetName(serv, "Source",          data[3]);
-    str[2] = GetName(serv, "L1TriggerTable",  data[4]);
-    str[3] = GetName(serv, "L2TriggerTable",  data[5]);
-    str[4] = GetName(serv, "HvSettings",      data[6]);
-    str[5] = GetName(serv, "LightConditions", data[7]);
+    str[0] = serv.QueryNameOfKey("Project",         data[2]);
+    str[1] = serv.QueryNameOfKey("Source",          data[3]);
+    str[2] = serv.QueryNameOfKey("L1TriggerTable",  data[4]);
+    str[3] = serv.QueryNameOfKey("L2TriggerTable",  data[5]);
+    str[4] = serv.QueryNameOfKey("HvSettings",      data[6]);
+    str[5] = serv.QueryNameOfKey("LightConditions", data[7]);
 
     if (str[0].IsNull() || str[1].IsNull() || str[2].IsNull() || str[3].IsNull() || str[4].IsNull() || str[5].IsNull())
@@ -164,14 +169,31 @@
     fout << endl;
 
-    TString query(Form("SELECT fRunNumber, fRunTypeKEY, fRunStart, fRunStop, fNumEvents"
-                       " FROM RunData WHERE fSequenceFirst=%s AND fExcludedFDAKEY=1"
-                       " AND fRunTypeKEY %%s ORDER BY fRunNumber",
-                       data[0]));
-
-
-    TString queryA(Form(query.Data(), "BETWEEN 2 AND 4"));
-    TString queryC(Form(query.Data(), "BETWEEN 4 AND 4"));
-    TString queryD(Form(query.Data(), "BETWEEN 2 AND 2"));
-    TString queryP(Form(query.Data(), "BETWEEN 3 AND 3"));
+    TString where(Form(" FROM RunData WHERE fSequenceFirst=%s AND fExcludedFDAKEY=1"
+                       " AND fRunTypeKEY%%s", data[0]));
+
+    TString query1(Form("SELECT fRunNumber, fNumEvents %s", where.Data()));
+    TString query2(Form("SELECT %s %s", GetTime("Start").Data(), where.Data()));
+    TString query3(Form("SELECT %s %s", GetTime("Stop").Data(),  where.Data()));
+
+    TString queryA(Form(query1.Data(), " BETWEEN 2 AND 4 ORDER BY fRunNumber"));
+    TString queryC(Form(query1.Data(), "=4 ORDER BY fRunNumber"));
+    TString queryD(Form(query1.Data(), "=2 ORDER BY fRunNumber"));
+    TString queryT(Form(query2.Data(), "=4 ORDER BY fRunNumber LIMIT 1"));
+
+    Int_t timec = GetTimeC(serv, queryT);
+    if (timec<0)
+    {
+        cout << "WARNING - Requesting start time of first calibration run failed." << endl;
+        queryT = Form(query3.Data(), "=4 ORDER BY fRunNumber LIMIT 1");
+        timec = GetTimeC(serv, queryT);
+        if (timec<0)
+        {
+            cout << "ERROR - Neither start nor stop time of first calibration could be requested." << endl;
+            return kFALSE;
+        }
+    }
+
+    TString query4(Form("=3 ORDER BY ABS(%s-%d) ASC", GetTime("Stop").Data(), timec));
+    TString queryP(Form(query1.Data(), query4.Data()));
 
     //write runs into sequence file
@@ -183,5 +205,5 @@
     if (!GetRuns(serv, fout, queryC, "CalRuns"))
         return kFALSE;
-    if (!GetRuns(serv, fout, queryP, "PedRuns"))
+    if (!GetRuns(serv, fout, queryP, "PedRuns", 1000))
         return kFALSE;
     if (!GetRuns(serv, fout, queryD, "DatRuns"))
@@ -200,5 +222,5 @@
     TEnv env("sql.rc");
 
-    MSQLServer serv(env);
+    MSQLMagic serv(env);
     if (!serv.IsConnected())
     {
