Index: /trunk/FACT++/src/DataCalib.cc
===================================================================
--- /trunk/FACT++/src/DataCalib.cc	(revision 11892)
+++ /trunk/FACT++/src/DataCalib.cc	(revision 11893)
@@ -221,5 +221,5 @@
     file.AddColumn('F', "TriggerOffsetRms",  1024*1440, "V");
 
-    fFileName = FormFileName(GetRunId(), "drs.fits");
+    fFileName = FormFileName("drs.fits");
 
     if (!file.OpenFile(fFileName))
Index: /trunk/FACT++/src/DataCalib.h
===================================================================
--- /trunk/FACT++/src/DataCalib.h	(revision 11892)
+++ /trunk/FACT++/src/DataCalib.h	(revision 11893)
@@ -31,5 +31,5 @@
 
 public:
-    DataCalib(uint32_t id, DimDescribedService &dim, MessageImp &imp) : DataProcessorImp(id, imp), fDim(dim)
+    DataCalib(const std::string &path, uint32_t id, DimDescribedService &dim, MessageImp &imp) : DataProcessorImp(path, id, imp), fDim(dim)
     {
     }
Index: /trunk/FACT++/src/DataProcessorImp.cc
===================================================================
--- /trunk/FACT++/src/DataProcessorImp.cc	(revision 11892)
+++ /trunk/FACT++/src/DataProcessorImp.cc	(revision 11893)
@@ -2,4 +2,6 @@
 
 #include "FAD.h"
+
+#include "DimWriteStatistics.h" // weird dependency
 
 using namespace std;
@@ -13,8 +15,29 @@
 //! @param extension a string containing the extension to be appened to the file name
 //
-string DataProcessorImp::FormFileName(uint32_t runid, const string &extension)
+string DataProcessorImp::FormFileName(const string &path, uint32_t runid, const string &extension)
 {
     ostringstream name;
-    name << Time().NightAsInt() << '_' << setfill('0') << setw(3) << runid << '.' << extension;
+
+    if (!path.empty())
+    {
+        name << path;
+        if (path[path.length()-1] != '/')
+            name << '/';
+    }
+
+    const Time time;
+    name << time.GetAsStr("%Y/%m/%d/");
+
+    try
+    {
+        DimWriteStatistics::CreateDirectory(name.str());
+    }
+    catch (const runtime_error &)
+    {
+        // File creation will fail anyway
+        //Error(e.what());
+    }
+
+    name << time.NightAsInt() << '_' << setfill('0') << setw(3) << runid << '.' << extension;
     return name.str();
 }
Index: /trunk/FACT++/src/DataProcessorImp.h
===================================================================
--- /trunk/FACT++/src/DataProcessorImp.h	(revision 11892)
+++ /trunk/FACT++/src/DataProcessorImp.h	(revision 11893)
@@ -14,5 +14,6 @@
 class DataProcessorImp : public MessageImp
 {
-    uint32_t fRunId;
+    std::string fPath;
+    uint32_t    fRunId;
 
     int Write(const Time &time, const std::string &txt, int qos)
@@ -26,5 +27,5 @@
 
 public:
-    DataProcessorImp(uint32_t id, MessageImp &imp) : fRunId(id), fMsg(imp) { }
+    DataProcessorImp(const std::string &path, uint32_t id, MessageImp &imp) : fPath(path), fRunId(id), fMsg(imp) { }
     virtual ~DataProcessorImp() { }
 
@@ -35,7 +36,12 @@
     const std::string &GetFileName() const { return fFileName; }
 
-    uint32_t GetRunId() const { return fRunId; }
+    std::string GetPath() const { return fPath; }
+    uint32_t    GetRunId() const { return fRunId; }
 
-    static std::string FormFileName(uint32_t runid, const std::string &extension);
+    static std::string FormFileName(const std::string &path, uint32_t runid, const std::string &extension);
+    std::string FormFileName(const std::string &extension)
+    {
+        return FormFileName(fPath, fRunId, extension);
+    }
 };
 
@@ -47,5 +53,5 @@
 
 public:
-    DataDump(uint32_t id, MessageImp &imp) : DataProcessorImp(id, imp) { }
+    DataDump(const std::string &path, uint32_t id, MessageImp &imp) : DataProcessorImp(path, id, imp) { }
 
     bool Open(RUN_HEAD* h);
@@ -57,5 +63,5 @@
 {
 public:
-    DataDebug(uint32_t id, MessageImp &imp) : DataDump(id, imp) { }
+    DataDebug(const std::string &path, uint32_t id, MessageImp &imp) : DataDump(path, id, imp) { }
 
     bool WriteEvt(EVENT *e);
Index: /trunk/FACT++/src/DataWriteFits.cc
===================================================================
--- /trunk/FACT++/src/DataWriteFits.cc	(revision 11892)
+++ /trunk/FACT++/src/DataWriteFits.cc	(revision 11893)
@@ -66,5 +66,5 @@
 
     //Form filename, based on runid and run-type
-    fFileName = FormFileName(GetRunId(), "fits");
+    fFileName = FormFileName("fits");
 
     if (!fFile.OpenFile(fFileName))
Index: /trunk/FACT++/src/DataWriteFits.h
===================================================================
--- /trunk/FACT++/src/DataWriteFits.h	(revision 11892)
+++ /trunk/FACT++/src/DataWriteFits.h	(revision 11893)
@@ -19,6 +19,6 @@
 
 public:
-    DataWriteFits(uint32_t runid, MessageImp &imp) :
-        DataProcessorImp(runid, imp), fConv(0), fFile(imp)
+    DataWriteFits(const std::string path, uint32_t runid, MessageImp &imp) :
+        DataProcessorImp(path, runid, imp), fConv(0), fFile(imp)
     {
     }
Index: /trunk/FACT++/src/DataWriteRaw.cc
===================================================================
--- /trunk/FACT++/src/DataWriteRaw.cc	(revision 11892)
+++ /trunk/FACT++/src/DataWriteRaw.cc	(revision 11893)
@@ -20,5 +20,5 @@
 bool DataWriteRaw::Open(RUN_HEAD *h)
 {
-    const string name = FormFileName(GetRunId(), "bin");
+    const string name = FormFileName("bin");
     if (access(name.c_str(), F_OK)==0)
     {
Index: /trunk/FACT++/src/DataWriteRaw.h
===================================================================
--- /trunk/FACT++/src/DataWriteRaw.h	(revision 11892)
+++ /trunk/FACT++/src/DataWriteRaw.h	(revision 11893)
@@ -68,5 +68,5 @@
 
 public:
-    DataWriteRaw(uint32_t id, MessageImp &imp) : DataProcessorImp(id, imp), fPosTail(0) { }
+    DataWriteRaw(const std::string &path, uint32_t id, MessageImp &imp) : DataProcessorImp(path, id, imp), fPosTail(0) { }
     ~DataWriteRaw() { if (fOut.is_open()) Close(); }
 
Index: /trunk/FACT++/src/DimWriteStatistics.cc
===================================================================
--- /trunk/FACT++/src/DimWriteStatistics.cc	(revision 11892)
+++ /trunk/FACT++/src/DimWriteStatistics.cc	(revision 11893)
@@ -10,4 +10,6 @@
 #include <sys/statvfs.h> //for getting disk free space
 #include <sys/stat.h>    //for getting files sizes
+
+#include <boost/filesystem.hpp>
 
 #include "Time.h"
@@ -88,4 +90,78 @@
 
      return -1;
+}
+
+// --------------------------------------------------------------------------
+//
+//! Check if a given path exists
+//! @param path the path to be checked
+//! @return whether or not the given path exists
+//
+bool DimWriteStatistics::DoesPathExist(const string &path, MessageImp &log)
+{
+    namespace fs = boost::filesystem;
+
+    const fs::path fullPath = fs::system_complete(fs::path(path));
+
+    if (!fs::exists(fullPath))
+       return false;
+
+    if (!fs::is_directory(fullPath))
+    {
+        log.Error("Path given for checking '" + path + "' designate a file name. Please provide a path name only");
+        return false;
+    }
+
+    if (access(path.c_str(), R_OK|W_OK|X_OK) != 0)
+    {
+        log.Error("Missing read, write or execute permissions on directory '" + path + "'");
+        return false;
+    }
+
+    return true;
+}
+
+// --------------------------------------------------------------------------
+//
+//! Check if a given path exists
+//! @param path the path to be checked
+//! @return whether or not the creation has been successfull
+//
+void DimWriteStatistics::CreateDirectory(string path)
+{
+    namespace fs = boost::filesystem;
+
+    //remove last '/', if present
+    if (path[path.size()-1] == '/')
+        path = path.substr(0, path.size()-1);
+
+    //create boost path
+    const fs::path fullPath = fs::system_complete(fs::path(path));
+
+    //if path does not exist, check if upper levels exist already
+    if (fs::exists(fullPath))
+    {
+        //if path already exist, make sure it does not designate a file (filenames cannot be checked if they do not exist)
+        if (fs::is_directory(fullPath))
+            return;
+
+        throw runtime_error("Path to be created is a file  '" + path + "'");
+    }
+
+    // we're hitting "/", which SHOULD have existed...
+    if (path.size() <= 1)
+        throw runtime_error("Path to be created looks suspicious '"+path+"'");
+
+    CreateDirectory(path.substr(0, path.find_last_of('/')));
+
+    //path does not exist, and upper level have been created already by recusrion.
+    const mode_t rightsMask = S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH; //everybody read, owner writes
+
+    if (mkdir(path.c_str(), rightsMask)==0)
+        return;
+
+    ostringstream str;
+    str << "mkdir() failed for '" << path << "': " << strerror(errno) << " [errno=" << errno << "]";
+    throw runtime_error(str.str());
 }
 
Index: /trunk/FACT++/src/DimWriteStatistics.h
===================================================================
--- /trunk/FACT++/src/DimWriteStatistics.h	(revision 11892)
+++ /trunk/FACT++/src/DimWriteStatistics.h	(revision 11893)
@@ -80,4 +80,8 @@
     ///Returns the size on disk of a given file
     static int64_t GetFileSizeOnDisk(const std::string& file, MessageImp &imp);
+
+    static bool DoesPathExist(const std::string &path, MessageImp &log);
+
+    static void CreateDirectory(std::string path);
 };
 
Index: /trunk/FACT++/src/EventBuilderWrapper.h
===================================================================
--- /trunk/FACT++/src/EventBuilderWrapper.h	(revision 11892)
+++ /trunk/FACT++/src/EventBuilderWrapper.h	(revision 11893)
@@ -10,4 +10,5 @@
 #endif
 #include <boost/thread.hpp>
+#include <boost/filesystem.hpp>
 #include <boost/date_time/posix_time/posix_time_types.hpp>
 
@@ -25,4 +26,5 @@
 namespace ba = boost::asio;
 namespace bs = boost::system;
+namespace fs = boost::filesystem;
 
 using ba::ip::tcp;
@@ -112,16 +114,18 @@
     bool fDebugLog;
 
+    string   fPath;
     uint32_t fRunNumber;
 
-    void InitRunNumber()
-    {
-        // FIXME: Add a check that we are not too close to noon!
-        //const int night = Time().NightAsInt();
-
+protected:
+    int64_t InitRunNumber()
+    {
         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(fRunNumber, "");
+            const string name = DataProcessorImp::FormFileName(fPath, fRunNumber, "");
 
             if (access((name+"bin").c_str(), F_OK) == 0)
@@ -133,12 +137,34 @@
         }
 
+        if (check != Time().NightAsInt())
+            return InitRunNumber();
+
         fRunNumber++;
 
+        if (fRunNumber==1000)
+        {
+            fMsg.Error("You have a file with run-number 1000 in "+fPath);
+            return -1;
+        }
+
         ostringstream str;
-        str << "Starting with run number " << fRunNumber;
+        str << "Set next run-number to " << fRunNumber << " determined from " << fPath;
         fMsg.Message(str);
 
-        fMsg.Info(" ==> TODO: Run-number detection doesn't work when noon passes!");
         fMsg.Info(" ==> TODO: Crosscheck with database!");
+
+        return check;
+    }
+
+    int64_t InitRunNumber(const string &path)
+    {
+        if (!DimWriteStatistics::DoesPathExist(path, fMsg))
+            return -1;
+
+        //const fs::path fullPath = fs::system_complete(fs::path(path));
+
+        fPath = path;
+
+        return InitRunNumber();
     }
 
@@ -176,6 +202,4 @@
         for (size_t i=0; i<40; i++)
             ConnectSlot(i, tcp::endpoint());
-
-        InitRunNumber();
     }
     virtual ~EventBuilderWrapper()
@@ -499,9 +523,9 @@
         switch (fFileFormat)
         {
-        case kNone:  file = new DataDump(runid,  fMsg); break;
-        case kDebug: file = new DataDebug(runid, fMsg); break;
-        case kFits:  file = new DataWriteFits(runid,  fMsg); break;
-	case kRaw:   file = new DataWriteRaw(runid,   fMsg); break;
-	case kCalib: file = new DataCalib(runid, fDimDrsCalibration, fMsg); break;
+        case kNone:  file = new DataDump(fPath, runid,  fMsg); break;
+        case kDebug: file = new DataDebug(fPath, runid, fMsg); break;
+        case kFits:  file = new DataWriteFits(fPath, runid,  fMsg); break;
+	case kRaw:   file = new DataWriteRaw(fPath, runid,   fMsg); break;
+	case kCalib: file = new DataCalib(fPath, runid, fDimDrsCalibration, fMsg); break;
         }
 
Index: /trunk/FACT++/src/datalogger.cc
===================================================================
--- /trunk/FACT++/src/datalogger.cc	(revision 11892)
+++ /trunk/FACT++/src/datalogger.cc	(revision 11893)
@@ -414,42 +414,14 @@
 bool DataLogger::CreateDirectory(string path)
 {
-    //remove last '/', if present
-    if (path[path.size()-1] == '/')
-        path = path.substr(0, path.size()-1);
-
-    //create boost path
-    const boost::filesystem::path fullPath =  boost::filesystem::system_complete(boost::filesystem::path(path));
-
-    //if path does not exist, check if upper levels exist already
-    if (boost::filesystem::exists(fullPath))
-    {
-        //if path already exist, make sure it does not designate a file (filenames cannot be checked if they do not exist)
-        if (boost::filesystem::is_directory(fullPath))
-            return true;
-
-        Error("Path to be created contains a file name: '" + path + "'");
+    try
+    {
+        DimWriteStatistics::CreateDirectory(path);
+        return true;
+    }
+    catch (const runtime_error &e)
+    {
+        Error(e.what());
         return false;
     }
-
-    if (path.size() <= 1)
-    {//we're hitting "/", which SHOULD have existed...
-        Error("Something unexpected happened while creating a path");
-    }
-    CreateDirectory(path.substr(0, path.find_last_of('/')));
-
-    //path does not exist, and upper level have been created already by recusrion.
-    const mode_t rightsMask = S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH; //everybody read, owner writes
-
-    const int returnValue = mkdir(path.c_str(), rightsMask);
-
-    if (returnValue != 0)
-    {
-        ostringstream str;
-        str << "mkdir() failed for '" << path << "': " << strerror(errno) << " [errno=" << errno << "]";
-        Error(str.str());
-        return false;
-    }
-
-    return true;
 }
 // --------------------------------------------------------------------------
@@ -461,23 +433,7 @@
 bool DataLogger::DoesPathExist(string path)
 {
-    const boost::filesystem::path fullPath = boost::filesystem::system_complete(boost::filesystem::path(path));
-
-    if (!boost::filesystem::exists(fullPath))
-       return false;
-
-    if (!boost::filesystem::is_directory(fullPath))
-    {
-        Error("Path given for checking '" + path + "' designate a file name. Please provide a path name only");
-        return false;
-    }
-
-    if (access(path.c_str(), R_OK|W_OK|X_OK) != 0)
-    {
-        Error("Missing read, write or execute permissions on directory '" + path + "'");
-        return false;
-    }
-
-    return true;
-}
+    return DimWriteStatistics::DoesPathExist(path, *this);
+}
+
 // --------------------------------------------------------------------------
 //
Index: /trunk/FACT++/src/fadctrl.cc
===================================================================
--- /trunk/FACT++/src/fadctrl.cc	(revision 11892)
+++ /trunk/FACT++/src/fadctrl.cc	(revision 11893)
@@ -1021,4 +1021,16 @@
         }
 
+        if (fNightAsInt!=Time().NightAsInt())
+        {
+            ostringstream out;
+            out << "Night changed from " << fNightAsInt << " to " << Time().NightAsInt() << "... determining new run-number.";
+            T::Info(out);
+
+            // FIXME: What about an error state?
+            fNightAsInt = InitRunNumber();
+            if (fNightAsInt<0)
+                return FAD::kConnected;
+        }
+
         const uint32_t runno = StartNewRun(evt.Get<uint64_t>(), evt.Get<uint64_t>(8), *fTargetConfig);
 
@@ -1026,4 +1038,7 @@
         str << "Starting configuration for run " << runno << " (" << name << ")";
         T::Message(str.str());
+
+        if (runno>=1000)
+            T::Warn("Run number exceeds logical maximum of 999 - this is no problem for writing but might give raise to problems in the analysis.");
 
         const FAD::Configuration &conf = fTargetConfig->second;
@@ -1813,4 +1828,6 @@
     }
 
+    int64_t fNightAsInt;
+
     int EvalOptions(Configuration &conf)
     {
@@ -1823,4 +1840,8 @@
         // ---------- Setup event builder ---------
         SetMaxMemory(conf.Get<unsigned int>("max-mem"));
+
+        fNightAsInt = InitRunNumber(conf.Get<string>("destination-folder"));
+        if (fNightAsInt<0)
+            return 1;
 
         // ---------- Setup run types ---------
@@ -1838,5 +1859,5 @@
             {
                 T::Error("Run-type "+*it+" defined twice.");
-                return 1;
+                return 2;
             }
 
@@ -1846,5 +1867,5 @@
                 !CheckConfigVal<bool>(conf, true, "enable-dwrite.",            *it) ||
                 !CheckConfigVal<bool>(conf, true, "enable-continous-trigger.", *it))
-                return 2;
+                return 3;
 
             target.fDenable          = conf.GetDef<bool>("enable-drs.", *it);
@@ -1856,5 +1877,5 @@
             {
                 if (!CheckConfigVal<uint16_t>(conf, 0xffff, "trigger-rate.", *it))
-                    return 3;
+                    return 4;
 
                 target.fTriggerRate = conf.GetDef<uint16_t>("trigger-rate.", *it);
@@ -1868,5 +1889,5 @@
                 if (!CheckConfigVal<uint16_t>(conf, FAD::kMaxRoiValue, "roi.",    *it) &&
                     !CheckConfigVal<uint16_t>(conf, FAD::kMaxRoiValue, str.str(), *it))
-                    return 4;
+                    return 5;
 
                 target.fRoi[i] = conf.HasDef(str.str(), *it) ?
@@ -1882,5 +1903,5 @@
                 if (!CheckConfigVal<uint16_t>(conf, FAD::kMaxDacValue, "dac.",    *it) &&
                     !CheckConfigVal<uint16_t>(conf, FAD::kMaxDacValue, str.str(), *it))
-                    return 5;
+                    return 6;
 
                 target.fDac[i] = conf.HasDef(str.str(), *it) ?
@@ -1903,5 +1924,5 @@
             const tcp::endpoint endpoint = GetEndpoint(addr);
             if (endpoint==tcp::endpoint())
-                return 1;
+                return 7;
 
             for (int i=0; i<num; i++)
@@ -1923,5 +1944,5 @@
             const tcp::endpoint endpoint = GetEndpoint(base);
             if (endpoint==tcp::endpoint())
-                return 10;
+                return 8;
 
             const ba::ip::address_v4::bytes_type ip = endpoint.address().to_v4().to_bytes();
@@ -1930,5 +1951,5 @@
             {
                 T::Out() << kRed << "EvalConfiguration - IP address given by --base-addr out-of-range." << endl;
-                return 11;
+                return 9;
             }
 
@@ -1957,5 +1978,5 @@
                 const tcp::endpoint endpoint = GetEndpoint(*i);
                 if (endpoint==tcp::endpoint())
-                    return 12;
+                    return 10;
 
                 AddEndpoint(endpoint);
