Index: trunk/FACT++/src/EventBuilderWrapper.h
===================================================================
--- trunk/FACT++/src/EventBuilderWrapper.h	(revision 10937)
+++ trunk/FACT++/src/EventBuilderWrapper.h	(revision 10938)
@@ -46,9 +46,27 @@
     MessageImp &fMsg;
 
+    enum
+    {
+        kCurrent = 0,
+        kTotal   = 1
+    };
+
+    bool fFitsFormat;
+
+    uint32_t fMaxRun;
+    uint32_t fNumEvts[2];
+
     DimDescribedService fDimFiles;
+    DimDescribedService fDimRuns;
+    DimDescribedService fDimEvents;
+    DimDescribedService fDimCurrentEvent;
 
 public:
-    EventBuilderWrapper(MessageImp &msg) : fMsg(msg),
-        fDimFiles("FAD_CONTROL/FILES", "X:1", "")
+    EventBuilderWrapper(MessageImp &msg) : fMsg(msg), 
+        fFitsFormat(false), fMaxRun(0),
+        fDimFiles ("FAD_CONTROL/FILES",         "X:1", ""),
+        fDimRuns  ("FAD_CONTROL/RUNS",          "I:1", ""),
+        fDimEvents("FAD_CONTROL/EVENTS",        "I:3", ""),
+        fDimCurrentEvent("FAD_CONTROL/CURRENT_EVENT", "I:1", "")
     {
         if (This)
@@ -57,5 +75,9 @@
         This = this;
 
-        fFitsFormat = false;
+        memset(fNumEvts, 0, sizeof(fNumEvts));
+
+        Update(fDimRuns,         uint32_t(0));
+        Update(fDimCurrentEvent, uint32_t(0));
+        Update(fDimEvents,       fNumEvts);
     }
     ~EventBuilderWrapper()
@@ -165,4 +187,7 @@
     size_t GetUsedMemory() const { return gi_usedMem; }
 
+    virtual int CloseOpenFiles() { CloseRunFile(0, 0); return 0; }
+
+
     /*
      struct OpenFileToDim
@@ -223,6 +248,4 @@
     vector<DataFileImp*> fFiles;
 
-    bool fFitsFormat;
-
     template<class T>
         void Update(DimDescribedService &svc, const T &data) const
@@ -236,5 +259,17 @@
     {
         // Check if file already exists...
-        cout << "OPEN_FILE #" << runid << endl;
+        DataFileImp *file = NULL;
+        try
+        {
+            file = fFitsFormat ?
+                static_cast<DataFileImp*>(new DataFileFits(runid)) :
+                static_cast<DataFileImp*>(new DataFileRaw(runid));
+        }
+        catch (const exception &e)
+        {
+            return 0;
+        }
+
+        cout << "OPEN_FILE #" << runid << " (" << file << ")" <<  endl;
         cout << " Ver= " << h->Version << endl;
         cout << " Typ= " << h->RunType << endl;
@@ -244,17 +279,15 @@
         cout << " roi= " << h->Nroi << endl;
 
-        DataFileImp *file = NULL;
-        try
-        {
-            file = fFitsFormat ?
-                static_cast<DataFileImp*>(new DataFileFits(runid)) :
-                static_cast<DataFileImp*>(new DataFileRaw(runid));
-        }
-        catch (const exception &e)
-        {
-            return 0;
-        }
-
         fFiles.push_back(file);
+
+        if (runid>fMaxRun)
+        {
+            fMaxRun = runid;
+            fNumEvts[kCurrent] = 0;
+
+            Update(fDimRuns,         fMaxRun);
+            Update(fDimEvents,       fNumEvts);
+            Update(fDimCurrentEvent, uint32_t(0));
+        }
 
         Update(fDimFiles, fFiles.size());
@@ -281,4 +314,13 @@
             return -1;
 
+        if (file->GetRunId()==fMaxRun)
+        {
+            Update(fDimCurrentEvent, e->EventNum);
+            fNumEvts[kCurrent]++;
+        }
+
+        fNumEvts[kTotal]++;
+        Update(fDimEvents, fNumEvts);
+
         // ===> SignalEvtWritten(runid);
         // Send num events written of newest file
@@ -295,7 +337,18 @@
         DataFileImp *file = reinterpret_cast<DataFileImp*>(handler);
 
-        cout << "CLOSE_RUN " << file->GetRunId() << endl;
-
-        fFiles.erase(find(fFiles.begin(), fFiles.end(), file));
+         const vector<DataFileImp*>::iterator it = find(fFiles.begin(), fFiles.end(), file);
+        if (it==fFiles.end())
+        {
+            ostringstream str;
+            str << "File handler (" << handler << ") requested to close by event builder doesn't exist.";
+            fMsg.Fatal(str);
+            return -1;
+        }
+
+        ostringstream str;
+        str << "CLOSE_RUN requested for " << file->GetRunId() << " (" << file << ")" <<endl;
+        fMsg.Debug(str);
+
+        fFiles.erase(it);
 
         Update(fDimFiles, fFiles.size());
