Index: /trunk/FACT++/src/EventBuilderWrapper.h
===================================================================
--- /trunk/FACT++/src/EventBuilderWrapper.h	(revision 11380)
+++ /trunk/FACT++/src/EventBuilderWrapper.h	(revision 11381)
@@ -28,18 +28,23 @@
 using namespace std;
 
-class DataFileImp
+class DataFileImp : public MessageImp
 {
     uint32_t fRunId;
 
+    int Write(const Time &time, const std::string &txt, int qos)
+    {
+        return fMsg.Write(time, txt, qos);
+    }
+
 protected:
-    MessageImp &fLog;
+    MessageImp &fMsg;
     string fFileName;
 
 public:
-    DataFileImp(uint32_t id, MessageImp &imp) : fRunId(id), fLog(imp) { }
+    DataFileImp(uint32_t id, MessageImp &imp) : fRunId(id), fMsg(imp) { }
     virtual ~DataFileImp() { }
 
     virtual bool OpenFile(RUN_HEAD* h) = 0;
-    virtual bool Write(EVENT *) = 0;
+    virtual bool WriteEvt(EVENT *) = 0;
     virtual bool Close(RUN_TAIL * = 0) = 0;
 
@@ -55,38 +60,9 @@
     //! @param extension a string containing the extension to be appened to the file name
     //
-    string FormFileName(uint32_t runType, string extension)
-    {
-        //TODO where am I supposed to get the base directory from ?
-        //TODO also, for creating subsequent directories, should I use the functions from the dataLogger ?
-        string baseDirectory = "./Run";
-
-        ostringstream result;
-//        result << baseDirectory;
-//        result << Time::fmt("/%Y/%m/%d/") << (Time() - boost::posix_time::time_duration(12,0,0));
-        result << setfill('0') << setw(8) << fRunId;
-        result << ".001_";
-        switch (runType)
-        {
-        case -1:
-            result << 'T';
-            break;
-        case 0:
-            result << 'D';
-            break;
-        case 1:
-            result << 'P';
-            break;
-        case 2:
-            result << 'C';
-            break;
-        case 3:
-            result << 'N';
-            break;
-        default:
-            result << runType;
-        };
-        result << "." << extension;
-
-        return result.str();
+    static string FormFileName(uint32_t runid, string extension)
+    {
+        ostringstream name;
+        name << Time().NightAsInt() << '.' << setfill('0') << setw(3) << runid << '.' << extension;
+        return name.str();
     }
 };
@@ -97,24 +73,47 @@
     DataFileNone(uint32_t id, MessageImp &imp) : DataFileImp(id, imp) { }
 
+    Time fTime;
+
     bool OpenFile(RUN_HEAD* h)
     {
         fFileName = "/dev/null";
-        cout << "OPEN_FILE #" << GetRunId() << " (" << this << ")" <<  endl;
-        cout << " Ver= " << h->Version << endl;
-        cout << " Typ= " << h->RunType << endl;
-        cout << " Nb = " << h->NBoard << endl;
-        cout << " Np = " << h->NPix << endl;
-        cout << " NTm= " << h->NTm << endl;
-        cout << " roi= " << h->Nroi << endl;
+
+        ostringstream str;
+        str << this << " - "
+            << "OPEN_FILE #" << GetRunId() << ":"
+            << " Ver=" << h->Version
+            << " Typ=" << h->RunType
+            << " Nb="  << h->NBoard
+            << " Np="  << h->NPix
+            << " NTm=" << h->NTm
+            << " roi=" << h->Nroi;
+
+        Debug(str);
+
+        fTime = Time();
 
         return true;
     }
-    bool Write(EVENT *)
-    {
+    bool WriteEvt(EVENT *e)
+    {
+        const Time now;
+        if (now-fTime<boost::posix_time::seconds(5))
+            return true;
+
+        fTime = now;
+
+        ostringstream str;
+        str << this << " - EVENT #" << e->EventNum;
+        Debug(str);
+
         return true;
     }
     bool Close(RUN_TAIL * = 0)
     {
-        cout << "CLOSE FILE #" << GetRunId() << " (" << this << ")" << endl;
+        ostringstream str;
+        str << this << " - CLOSE FILE #" << GetRunId();
+
+        Debug(str);
+
         return true;
     }
@@ -126,5 +125,5 @@
     DataFileDebug(uint32_t id, MessageImp &imp) : DataFileNone(id, imp) { }
 
-    bool Write(EVENT *e)
+    bool WriteEvt(EVENT *e)
     {
         cout << "WRITE_EVENT #" << GetRunId() << " (" << e->EventNum << ")" << endl;
@@ -226,8 +225,8 @@
     bool OpenFile(RUN_HEAD *h)
     {
-        const string name = FormFileName(h->RunType, "bin");
+        const string name = FormFileName(GetRunId(), "bin");
         if (access(name.c_str(), F_OK)==0)
         {
-            fLog.Error("File '"+name+"' already exists.");
+            Error("File '"+name+"' already exists.");
             return false;
         }
@@ -241,5 +240,5 @@
             ostringstream str;
             str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";
-            fLog.Error(str);
+            Error(str);
 
             return false;
@@ -276,5 +275,5 @@
             ostringstream str;
             str << "Open file " << name << ": " << strerror(errno) << " (errno=" << errno << ")";
-            fLog.Error(str);
+            Error(str);
 
             return false;
@@ -283,5 +282,5 @@
         return true;
     }
-    bool Write(EVENT *evt)
+    bool WriteEvt(EVENT *evt)
     {
         const int sh = sizeof(EVENT)-2 + NPIX*evt->Roi*2;
@@ -307,5 +306,5 @@
             ostringstream str;
             str << " Writing footer: " << strerror(errno) << " (errno=" << errno << ")";
-            fLog.Error(str);
+            Error(str);
 
             return false;
@@ -318,5 +317,5 @@
             ostringstream str;
             str << "Closing file: " << strerror(errno) << " (errno=" << errno << ")";
-            fLog.Error(str);
+            Error(str);
 
             return false;
@@ -390,5 +389,5 @@
             ostringstream str;
             str << "Could not add header key " << name;
-            fLog.Error(str);
+            Error(str);
         }
     }
@@ -412,8 +411,8 @@
     {
         //Form filename, based on runid and run-type
-        const string fileName = FormFileName(h->RunType, "fits");
+        const string fileName = FormFileName(GetRunId(), "fits");
         if (access(fileName.c_str(), F_OK)==0)
         {
-            fLog.Error("File '"+fileName+"' already exists.");
+            Error("File '"+fileName+"' already exists.");
             return false;
         }
@@ -443,5 +442,5 @@
             ostringstream str;
             str << "Could not open FITS file " << fileName << ": " << e.message();
-            fLog.Error(str);
+            Error(str);
             return false;
         }
@@ -476,5 +475,5 @@
             ostringstream str;
             str << "Could not create FITS table 'Events' in file " << fileName << " reason: " << e.message();
-            fLog.Error(str);
+            Error(str);
             return false;
         }
@@ -482,5 +481,5 @@
         if (fTable->rows() != 0)
         {
-            fLog.Error("FITS table created on the fly looks non-empty.");
+            Error("FITS table created on the fly looks non-empty.");
             return false;
         }
@@ -588,5 +587,5 @@
             ostringstream str;
             str << "Writing FITS row " << fNumRows << ": " << text << " (file_write_tblbytes, rc=" << status << ")";
-            fLog.Error(str);
+            Error(str);
         }
 
@@ -600,5 +599,5 @@
     //! @param e the pointer to the EVENT
     //
-    virtual bool Write(EVENT *e)
+    virtual bool WriteEvt(EVENT *e)
     {
         //FIXME As discussed earlier, we do not swap the bytes yet.
@@ -614,5 +613,5 @@
             ostringstream str;
             str << "Inserting row " << fNumRows << " into " << fFileName << ": " << text << " (fits_insert_rows, rc=" << status << ")";
-            fLog.Error(str);
+            Error(str);
 
             return false;
@@ -779,14 +778,9 @@
     bool fDebugLog;
 
-    int Write(const Time &time, const std::string &txt, int qos)
-    {
-        return fMsg.Write(time, txt, qos);
-    }
-
-
     uint32_t fRunNumber;
 
     void InitRunNumber()
     {
+        // FIXME: Add a check that we are not too close to noon!
         const int night = Time().NightAsInt();
 
@@ -795,10 +789,9 @@
         while (--fRunNumber>0)
         {
-            ostringstream name;
-            name << night << '.' << setfill('0') << setw(3) << fRunNumber;
-
-            if (access((name.str()+".bin").c_str(), F_OK) == 0)
+            const string name = DataFileImp::FormFileName(fRunNumber, "");
+
+            if (access((name+"bin").c_str(), F_OK) == 0)
                 break;
-            if (access((name.str()+".fits").c_str(), F_OK) == 0)
+            if (access((name+"fits").c_str(), F_OK) == 0)
                 break;
         }
@@ -806,5 +799,10 @@
         fRunNumber++;
 
-        cout << "FOUND: " << night << '.' << setfill('0') << setw(3) << fRunNumber << endl;
+        ostringstream str;
+        str << "Starting with run number " << fRunNumber;
+        fMsg.Message(str);
+
+        fMsg.Fatal("Run-number detection doesn't work when noon passes!");
+        fMsg.Error("Crosscheck with database!");
     }
 
@@ -858,4 +856,5 @@
     }
 
+    uint32_t GetRunNumber() const { return fRunNumber; }
 
     bool IsThreadRunning()
@@ -1148,5 +1147,5 @@
         DataFileImp *file = reinterpret_cast<DataFileImp*>(handler);
 
-        if (!file->Write(e))
+        if (!file->WriteEvt(e))
             return -1;
 
