Index: trunk/FACT++/src/EventBuilderWrapper.h
===================================================================
--- trunk/FACT++/src/EventBuilderWrapper.h	(revision 12480)
+++ trunk/FACT++/src/EventBuilderWrapper.h	(revision 12481)
@@ -194,5 +194,5 @@
         fDimRoi         ("FAD_CONTROL/REGION_OF_INTEREST", "S:2",  ""),
         fDimDac         ("FAD_CONTROL/DAC",                "S:336", ""),
-        fDimDrsCalibration("FAD_CONTROL/DRS_CALIBRATION",  "I:3;I:1;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:163840;F:163840", ""),
+        fDimDrsCalibration("FAD_CONTROL/DRS_CALIBRATION",  "I:1;I:3;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:1474560;F:163840;F:163840", ""),
         fDimStatistics1 ("FAD_CONTROL/STATISTICS1",        "I:3;I:5;X:4;I:3;I:3;I:40;I:1;I:2;C:40;I:40;I:40;X:40", ""),
         fDimStatistics2 ("FAD_CONTROL/STATISTICS2",        "I:1;I:280;X:40;I:40;I:4;I:4;I:2;I:2;I:3;C:40",  ""),
@@ -223,17 +223,6 @@
     }
 
-    struct RunDescription
-    {
-        uint32_t maxtime;
-        uint32_t maxevt;
-
-        string name;
-
-        FAD::Configuration reference;
-
-        bool started;
-    };
-
-    map<uint32_t, RunDescription> fExpectedRuns;
+    set<uint32_t> fIsRunStarted;
+    map<uint32_t, FAD::RunDescription> fExpectedRuns;
 
     uint32_t StartNewRun(int64_t maxtime, int64_t maxevt, const pair<string, FAD::Configuration> &ref)
@@ -244,5 +233,5 @@
             maxevt  = INT32_MAX;
 
-        const RunDescription descr =
+        const FAD::RunDescription descr =
         {
             uint32_t(maxtime),
@@ -250,5 +239,4 @@
             ref.first,
             ref.second,
-            false
         };
 
@@ -258,4 +246,5 @@
 
         fExpectedRuns[fRunNumber] = descr;
+        fIsRunStarted.insert(fRunNumber);
         return fRunNumber++;
     }
@@ -535,4 +524,30 @@
         fMsg.Info(" ==> TODO: Write information from fTargetConfig to header!");
 
+        map<uint32_t,FAD::RunDescription>::iterator it = fExpectedRuns.begin();
+        while (1)
+        {
+            if (it->first<runid)
+            {
+                ostringstream str;
+                str << "runOpen - Missed run " << runid << "." << endl;
+                fMsg.Info(str);
+
+                fExpectedRuns.erase(it);
+            }
+            if (it->first==runid)
+                break;
+            it++;
+        }
+        if (it==fExpectedRuns.end())
+        {
+            ostringstream str;
+            str << "runOpen - Run " << runid << " wasn't expected." << endl;
+            fMsg.Warn(str);
+        }
+
+        const FAD::RunDescription desc = it->second;
+
+        fExpectedRuns.erase(it);
+
         // Check if file already exists...
         DataProcessorImp *file = 0;
@@ -548,5 +563,5 @@
         try
         {
-            if (!file->Open(h))
+            if (!file->Open(h, desc))
                 return 0;
         }
@@ -923,6 +938,6 @@
     bool IsRunStarted() const
     {
-        const map<uint32_t,RunDescription>::const_iterator it = fExpectedRuns.find(fRunNumber-1);
-        return it==fExpectedRuns.end();// ? true : it->second.started;
+        const set<uint32_t>::const_iterator it = fIsRunStarted.find(fRunNumber-1);
+        return it==fIsRunStarted.end();// ? true : it->second.started;
     }
 
@@ -938,16 +953,40 @@
     }
 
-    void gotNewRun(int runnr, PEVNT_HEADER */*headers*/)
+    void gotNewRun(uint32_t runnr, PEVNT_HEADER */*headers*/)
     {
         // This function is called even when writing is switched off
-        const map<uint32_t,RunDescription>::iterator it = fExpectedRuns.find(runnr);
-        if (it==fExpectedRuns.end())
+        set<uint32_t>::iterator it = fIsRunStarted.begin();
+        while (1)
+        {
+            if (*it<runnr)
+            {
+                ostringstream str;
+                str << "gotNewRun - Missed run " << runnr << "." << endl;
+                fMsg.Info(str);
+
+                fIsRunStarted.erase(it);
+            }
+            if (*it==runnr)
+                break;
+            it++;
+        }
+        if (it==fIsRunStarted.end())
+        {
+            ostringstream str;
+            str << "gotNewRun - Not waiting for run " << runnr << "." << endl;
+            fMsg.Warn(str);
+            return;
+        }
+
+        map<uint32_t,FAD::RunDescription>::iterator i2 = fExpectedRuns.find(runnr);
+        if (i2==fExpectedRuns.end())
         {
             ostringstream str;
             str << "gotNewRun - Run " << runnr << " wasn't expected." << endl;
-            return;
-        }
-
-        CloseRunFile(runnr, time(NULL)+it->second.maxtime, it->second.maxevt);
+            fMsg.Warn(str);
+            return;
+        }
+
+        CloseRunFile(runnr, time(NULL)+i2->second.maxtime, i2->second.maxevt);
         // return: 0=close scheduled / >0 already closed / <0 does not exist
 
@@ -955,7 +994,5 @@
         //        be opened/closed
 
-        it->second.started = true;
-
-        fExpectedRuns.erase(it);
+        fIsRunStarted.erase(it);
     }
 
@@ -1320,5 +1357,5 @@
     }
 
-    void gotNewRun(int runnr, PEVNT_HEADER *headers)
+    void gotNewRun(uint32_t runnr, PEVNT_HEADER *headers)
     {
         return EventBuilderWrapper::This->gotNewRun(runnr, headers);
