Index: /trunk/FACT++/src/EventBuilderWrapper.h
===================================================================
--- /trunk/FACT++/src/EventBuilderWrapper.h	(revision 14701)
+++ /trunk/FACT++/src/EventBuilderWrapper.h	(revision 14702)
@@ -110,17 +110,35 @@
 
     string   fPath;
+    uint64_t fNightAsInt;
     uint32_t fRunNumber;
 
 protected:
-    int64_t InitRunNumber()
-    {
+    bool InitRunNumber(const string &path="")
+    {
+        if (!path.empty())
+        {
+            if (!DimWriteStatistics::DoesPathExist(path, fMsg))
+            {
+                fMsg.Error("Data path "+path+" does not exist!");
+                return false;
+            }
+
+            fPath = path;
+            fDimWriteStats.SetCurrentFolder(fPath);
+
+            fMsg.Info("Data path set to "+path+".");
+        }
+
+        // Get current night
+        const uint64_t night = Time().NightAsInt();
+        if (night==fNightAsInt)
+            return true;
+
+        // Check for run numbers
         fRunNumber = 1000;
 
-        // Ensure that the night doesn't change during our check
-        const int check = Time().NightAsInt();
-
         while (--fRunNumber>0)
         {
-            const string name = DataProcessorImp::FormFileName(fPath, fRunNumber, "");
+            const string name = DataProcessorImp::FormFileName(fPath, night, fRunNumber, "");
 
             if (access((name+"bin").c_str(), F_OK) == 0)
@@ -130,42 +148,27 @@
             if (access((name+"drs.fits").c_str(), F_OK) == 0)
                 break;
-
-        }
-
-        if (check != Time().NightAsInt())
-            return InitRunNumber();
-
+        }
+
+        // This is now the first file which does not exist
         fRunNumber++;
 
+        // Check if we have exceeded the maximum
         if (fRunNumber==1000)
         {
-            fMsg.Error("You have a file with run-number 1000 in "+fPath);
-            return -1;
+            fMsg.Error("You have a file with run-number 1000 in "+fPath+" ["+to_string(night)+"]");
+            return false;
         }
 
         ostringstream str;
-        str << "Set next run-number to " << fRunNumber << " determined from '" << (fPath.empty()?".":fPath) << "'";
+        if (fNightAsInt==0)
+            str << "First night...";
+        else
+            str << "Night has changd from " << fNightAsInt << "... new";
+        str << " run-number is " << night << "-" << setfill('0') << setw(3) << fRunNumber << " [" << (fPath.empty()?".":fPath) << "]";
         fMsg.Message(str);
 
-        //fMsg.Info(" ==> TODO: Crosscheck with database!");
-
-        return check;
-    }
-
-    int64_t InitRunNumber(const string &path)
-    {
-        if (!DimWriteStatistics::DoesPathExist(path, fMsg))
-        {
-            fMsg.Error("Data path "+path+" does not exist!");
-            return -1;
-        }
-
-        //const fs::path fullPath = fs::system_complete(fs::path(path));
-
-        fPath = path;
-
-        fDimWriteStats.SetCurrentFolder(fPath);
-
-        return InitRunNumber();
+        fNightAsInt = night;
+
+        return true;
     }
 
@@ -255,5 +258,5 @@
                                                            "|numConn[int]:number of sockets successfully opened per board"),
         fDimFileFormat  ("FAD_CONTROL/FILE_FORMAT",        "S:1",  ""),
-        fDebugStream(false), fDebugRead(false), fDebugLog(false)
+        fDebugStream(false), fDebugRead(false), fDebugLog(false), fNightAsInt(0)
     {
         if (This)
@@ -298,4 +301,7 @@
             ref.second,
         };
+
+        if (!InitRunNumber())
+            return 0;
 
         // FIMXE: Maybe reset an event counter so that the mcp can count events?
@@ -630,10 +636,10 @@
         switch (fFileFormat)
         {
-        case FAD::kNone:    file = new DataDump(fPath, runid,  fMsg); break;
-        case FAD::kDebug:   file = new DataDebug(fPath, runid, fMsg); break;
-        case FAD::kCfitsio: file = new DataWriteFits(fPath, runid,  fMsg); break;
-        case FAD::kFits:    file = new DataWriteFits2(fPath, runid,  fMsg); break;
-	case FAD::kRaw:     file = new DataWriteRaw(fPath, runid,   fMsg); break;
-	case FAD::kCalib:   file = new DataCalib(fPath, runid, fDimDrsCalibration, fDimDrsRuns, fMsg); break;
+        case FAD::kNone:    file = new DataDump(fPath, fNightAsInt, runid,  fMsg); break;
+        case FAD::kDebug:   file = new DataDebug(fPath, fNightAsInt, runid, fMsg); break;
+        case FAD::kCfitsio: file = new DataWriteFits(fPath, fNightAsInt, runid,  fMsg); break;
+        case FAD::kFits:    file = new DataWriteFits2(fPath, fNightAsInt, runid,  fMsg); break;
+	case FAD::kRaw:     file = new DataWriteRaw(fPath, fNightAsInt, runid,   fMsg); break;
+	case FAD::kCalib:   file = new DataCalib(fPath, fNightAsInt, runid, fDimDrsCalibration, fDimDrsRuns, fMsg); break;
         }
 
@@ -1050,8 +1056,22 @@
     }
 
-    void IncreaseRunNumber(uint32_t run)
-    {
-        if (run>fRunNumber)
-            fRunNumber = run;
+    bool IncreaseRunNumber(uint32_t run)
+    {
+        if (!InitRunNumber())
+            return false;
+
+        if (run<fRunNumber)
+        {
+            ostringstream msg;
+            msg <<
+                "Run number " << run << " smaller than next available "
+                "run number " << fRunNumber << " in " << fPath << " [" << fNightAsInt << "]";
+            fMsg.Error(msg);
+            return false;
+        }
+
+        fRunNumber = run;
+
+        return true;
     }
 
