Index: /trunk/FACT++/src/dataLogger.cc
===================================================================
--- /trunk/FACT++/src/dataLogger.cc	(revision 10828)
+++ /trunk/FACT++/src/dataLogger.cc	(revision 10829)
@@ -125,4 +125,5 @@
 #endif
         runNumber = -1;
+        //give it a meaningless, 0 time to distinguish with actual, valid times
         time = Time(0,0);
         numCopies = new int(1);
@@ -453,6 +454,250 @@
     ///checks with fServiceList whether or not the services got updated
     bool CheckForServicesUpdate();
+    ///retrieves the size of a file
+    off_t GetFileSize(string&);
+    ///Form the name of the nightly text files (logs and reports)
+    void FormNightlyTextFileName(string& name, bool isReport);
+    ///Form the name of the run text files (logs and reports)
+    void FormRunTextFileName(string& name, bool isReport, const int runNumber);
+    ///Form the name of the nightly Fits files
+    void FormNightlyFitsFileName(string& fileName, string& fileNameWithPath, const string& serviceName);
+    ///Form the name of the run Fits files
+    void FormRunFitsFileName(string& fileName, string& fileNameWithPath, const string& serviceName, const int runNumber);
+    ///Form the name of the grouped fits file (i.e. the file where several services are written)
+    void FormGroupedRunFitsFileName(string& fileName, string& fileNameWithPath, const int runNumber);
+    ///Form the name of the grouping fits file (i.e. the file that is created after a run ends)
+    void FormFitsGroupingFileName(string& fileNameWithPath, const int runNumber);
+    ///Check whether service is in black and/or white list
+    bool ShouldSubscribe(const string& server, const string& service);
+    ///Subscribe to a given server and service
+    DimStampedInfo* SubscribeToPlease(const string& server, const string& service);
+    ///Open a text file and checks for ofstream status
+    void OpenTextFilePlease(ofstream& stream, const string& name);
+    ///Check if a dir is . and returns the actual string corresponding to .
+    string CheckIfDirIsDot(const string& dir);
+    ///Remembers the size of newly opened files. for statistic purposes
+    bool RememberFileOrigSizePlease(string& fileName, bool nightly);
 }; //DataLogger
 
+// --------------------------------------------------------------------------
+//
+//!
+//
+bool DataLogger::RememberFileOrigSizePlease(string& fileName, bool nightly)
+{
+    //get the size of the file we're about to open
+    if (fFileSizesMap.find(fileName) == fFileSizesMap.end())
+    {
+        if (nightly)
+            fBaseSizeNightly += GetFileSize(fileName);
+        else
+            fBaseSizeRun += GetFileSize(fileName);
+        fFileSizesMap[fileName] = 0;
+        return true;
+    }
+    return false;
+}
+// --------------------------------------------------------------------------
+//
+//!
+//
+string DataLogger::CheckIfDirIsDot(const string& dir)
+{
+    if (dir == ".")
+    {
+        char currentPath[FILENAME_MAX];
+        if (!getcwd(currentPath, sizeof(currentPath)))
+        {
+            if (errno != 0)
+            {
+                ostringstream str;
+                str << "Unable to retrieve the current path" << ". Reason: " << strerror(errno) << " [" << errno << "]";
+                Error(str);
+            }
+        }
+        return string(currentPath);
+    }
+    else
+    {
+        return string(dir);
+    }
+}
+// --------------------------------------------------------------------------
+//
+//!
+//
+void DataLogger::OpenTextFilePlease(ofstream& stream, const string& name)
+{
+    stream.open(name.c_str(), ios_base::out | ios_base::app);
+    if (errno != 0)
+    {
+        ostringstream str;
+        str << "Unable to open file: " << name << ". Reason: " << strerror(errno) << " [" << errno << "]";
+        Error(str);
+    }
+}
+// --------------------------------------------------------------------------
+//
+//! Create a new dim subscription to a given server and service
+//! @param server the server name
+//! @param service the service name
+//
+DimStampedInfo* DataLogger::SubscribeToPlease(const string& server, const string& service)
+{
+    if (fDebugIsOn)
+    {
+        ostringstream str;
+        str << "Subscribing to service " << server << "/" << service;
+        Debug(str);
+    }
+    return new DimStampedInfo((server + "/" + service).c_str(), const_cast<char*>(""), this);
+}
+// --------------------------------------------------------------------------
+//
+//! Check whether a service should be subscribed to, based on the black/white list entries
+//! @param server the server name associated with the service being checked
+//! @param service the service name associated with the service being checked
+//
+bool DataLogger::ShouldSubscribe(const string& server, const string& service)
+{
+    if (service == "SERVICE_LIST")
+        return false;
+    if (fHasWhiteList && (fWhiteList.find(server + "/") == fWhiteList.end()) &&
+                         (fWhiteList.find(server + "/" + service) == fWhiteList.end()) &&
+                         (fWhiteList.find("/" + service) == fWhiteList.end()))
+        return false;
+    if (fHasBlackList && ((fBlackList.find(server + "/") != fBlackList.end()) ||
+                          (fBlackList.find(server + "/" + service) != fBlackList.end()) ||
+                          (fBlackList.find("/" + service) != fBlackList.end())))
+        return false;
+
+    return true;
+}
+// --------------------------------------------------------------------------
+//
+//!Form the name of the grouped fits file (i.e. the file where several services are written)
+//! @param fileName only the file name itself (no path)
+//! @param fileNameWithPath fileName, augmented with the prefix path
+//! @param runNumber the current run number for which the file should be opened.
+//
+void DataLogger::FormGroupedRunFitsFileName(string& fileName, string& fileNameWithPath, const int runNumber)
+{
+    ostringstream sRun;
+    sRun << runNumber;
+    fileName = sRun.str() + "_group.fits";
+    fileNameWithPath = fRunFilePath + '/' + fileName;
+}
+// --------------------------------------------------------------------------
+//
+//! Form the name of the grouping fits file (i.e. the file that is created after a run ends)
+//! This file is meant to link the various fits files related to a particular run from a single file
+//! @param fileNameWithPath the full filename
+//! @param runNumber the current run number for which the file should be opened. -1 means nightly grouping
+//
+void DataLogger::FormFitsGroupingFileName(string& fileNameWithPath, const int runNumber)
+{
+    ostringstream groupName;
+    if (runNumber != -1)
+    {
+        groupName << fRunFilePath << '/' << runNumber << ".fits";
+    }
+    else
+    {
+        Time time;
+        ostringstream sTime;
+        sTime << time.Y() << "_" << time.M() << "_" << time.D();
+        groupName << fNightlyFilePath << '/' << sTime.str() << ".fits";
+    }
+    fileNameWithPath = groupName.str();
+
+}
+// --------------------------------------------------------------------------
+//
+//! Form the name of the nightly Fits files
+//! @param fileName the file name only
+//! @param fileNameWithPath the fileName augmented by the prefix path
+//! @param serviceName the service for which this file should be opened
+//
+void DataLogger::FormNightlyFitsFileName(string& fileName, string& fileNameWithPath, const string& serviceName)
+{
+    Time time;
+    ostringstream sTime;
+    sTime << time.Y() << "_" << time.M() << "_" << time.D();
+    fileName = sTime.str() + '_' + serviceName + ".fits";
+    fileNameWithPath = fNightlyFilePath + '/' + fileName;
+}
+// --------------------------------------------------------------------------
+//
+//! Form the name of the run Fits files
+//! @param fileName the file name only
+//! @param fileNameWithPath the fileName augmented by the prefix path
+//! @param serviceName the service for which this file should be opened
+//! @param runNumber the current run number for which this file should be opened
+//
+void DataLogger::FormRunFitsFileName(string& fileName, string& fileNameWithPath, const string& serviceName, const int runNumber)
+{
+    ostringstream sRun;
+    sRun << runNumber;
+    fileName = sRun.str() + '_' + serviceName + ".fits";
+    fileNameWithPath = fRunFilePath + '/' + fileName;
+}
+// --------------------------------------------------------------------------
+//
+//! Form the name of the run text files (logs and reports)
+//! @param name the full file name
+//! @param isReport whether a log or report name should be formed
+//! @param runNumber the current run number for which this file should be opened
+//
+void DataLogger::FormRunTextFileName(string& name, bool isReport, const int runNumber)
+{
+    ostringstream sRun;
+    sRun << runNumber;
+
+    name = fRunFilePath + '/' + sRun.str();
+
+    if (isReport)
+        name += ".rep";
+    else
+        name += ".log";
+}
+// --------------------------------------------------------------------------
+//
+//! Form the name of the nightly text files (logs and reports)
+//! @param name the full file name
+//! @param isReport whether a log or report name should be formed
+//
+void DataLogger::FormNightlyTextFileName(string& name, bool isReport)
+{
+    Time time;
+    ostringstream sTime;
+    sTime << time.Y() << "_" << time.M() << "_" << time.D();
+
+    name = fNightlyFilePath + '/' + sTime.str();
+    if (isReport)
+        name += ".rep";
+    else
+        name += ".log";
+}
+// --------------------------------------------------------------------------
+//
+//!retrieves the size on disk of a file
+//! @param fileName the full file name for which the size on disk should be retrieved
+//! @return the size of the file on disk, in bytes. 0 if the file does not exist or if an error occured
+//
+off_t DataLogger::GetFileSize(string& fileName)
+{
+    struct stat st;
+    if (!stat(fileName.c_str(), &st))
+        return st.st_size;
+
+    if (errno != 0)
+    {
+        ostringstream str;
+        str << "Unable to stat " << fileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
+        Error(str);
+    }
+
+    return 0;
+}
 // --------------------------------------------------------------------------
 //
@@ -532,28 +777,15 @@
         Message("FITS output disabled at compilation");
 #endif
-    struct stat st;
     //gather log and report files sizes on disk
     if (fNightlyLogFile.is_open())
-    {
-        stat(fFullNightlyLogFileName.c_str(), &st);
-        fFileSizesMap[fFullNightlyLogFileName] = st.st_size;
-    }
+        fFileSizesMap[fFullNightlyLogFileName] = GetFileSize(fFullNightlyLogFileName);
     if (fNightlyReportFile.is_open())
-    {
-        stat(fFullNightlyReportFileName.c_str(), &st);
-        fFileSizesMap[fFullNightlyReportFileName] = st.st_size;
-    }
+        fFileSizesMap[fFullNightlyReportFileName] = GetFileSize(fFullNightlyReportFileName);
     for (list<RunNumberType>::iterator it = fRunNumber.begin(); it != fRunNumber.end(); it++)
     {
         if (it->reportFile->is_open())
-        {
-            stat(it->reportName.c_str(), &st);
-            fFileSizesMap[it->reportName] = st.st_size;
-        }
+            fFileSizesMap[it->reportName] = GetFileSize(it->reportName);
         if (it->logFile->is_open())
-        {
-            stat(it->logName.c_str(), &st);
-            fFileSizesMap[it->logName] = st.st_size;
-        }
+            fFileSizesMap[it->logName] = GetFileSize(it->logName);
     }
     struct statvfs vfs;
@@ -831,4 +1063,5 @@
         //skip the two de-fact excluded services
         //Dim crashes if the publisher subscribes to its own service. This sounds weird, I agree.
+        //I agree: hard coded values are bad, but in this case these two entries should always be here otherwise Dim crashes.
         if ((i->find("DIS_DNS") != string::npos) ||
             (i->find("DATA_LOGGER") != string::npos))
@@ -840,5 +1073,5 @@
         if (cSubs != fServiceSubscriptions.end())//if the current server already is in our subscriptions
         {                                         //then check and update our list of subscriptions
-            //first, remove the services that may have dissapeared.
+            //first, remove the services that may have disapeared.
             map<string, SubscriptionType>::iterator serverSubs;
             vector<string>::const_iterator givenSubs;
@@ -860,26 +1093,10 @@
             for (givenSubs = cServicesList.begin(); givenSubs != cServicesList.end(); givenSubs++)
             {
-                if (*givenSubs == "SERVICE_LIST")
+                if (!ShouldSubscribe(*i, *givenSubs))
                     continue;
-
-                if (fHasWhiteList && (fWhiteList.find(*i + "/") == fWhiteList.end()) &&
-                                     (fWhiteList.find(*i + "/" + *givenSubs) == fWhiteList.end()) &&
-                                     (fWhiteList.find("/" + *givenSubs) == fWhiteList.end()))
-                    continue;
-                if (fHasBlackList && ((fBlackList.find(*i + "/") != fBlackList.end()) ||
-                                      (fBlackList.find(*i + "/" + *givenSubs) != fBlackList.end()) ||
-                                      (fBlackList.find("/" + *givenSubs) != fBlackList.end())))
-                    continue;
-
                 if (cSubs->second.find(*givenSubs) == cSubs->second.end())
                 {//service not found. Add it
-                    cSubs->second[*givenSubs].dimInfo = new DimStampedInfo(((*i) + "/" + *givenSubs).c_str(), const_cast<char*>(""), this);
+                    cSubs->second[*givenSubs].dimInfo = SubscribeToPlease(*i, *givenSubs);
                     serviceUpdated = true;
-                    if(fDebugIsOn)
-                    {
-                        ostringstream str;
-                        str << "Subscribing to service " << *i << "/" << *givenSubs;
-                        Debug(str);
-                    }
                 }    
             }
@@ -891,23 +1108,8 @@
             for (vector<string>::const_iterator j = cServicesList.begin(); j!= cServicesList.end(); j++)
             {
-                if (*j == "SERVICE_LIST")
+                if (!ShouldSubscribe(*i, *j))
                     continue;
-                if (fHasWhiteList && (fWhiteList.find(*i + "/") == fWhiteList.end()) &&
-                                     (fWhiteList.find(*i + "/" + *j) == fWhiteList.end()) &&
-                                     (fWhiteList.find("/" + *j) == fWhiteList.end()))
-                    continue;
-                if (fHasBlackList && ((fBlackList.find(*i + "/") != fBlackList.end()) ||
-                                      (fBlackList.find(*i + "/" + *j) != fBlackList.end()) ||
-                                      (fBlackList.find("/" + *j) != fBlackList.end())))
-                    continue;
-
-                liste[*j].dimInfo = new DimStampedInfo(((*i) + "/" + (*j)).c_str(), const_cast<char*>(""), this);
+                liste[*j].dimInfo = SubscribeToPlease(*i, *j);
                 serviceUpdated = true;
-                if(fDebugIsOn)
-                {
-                    ostringstream str;
-                    str << "Subscribing to service " << *i << "/" << *j;
-                    Debug(str);
-                }
             }
         }    
@@ -934,10 +1136,5 @@
 
     fMonitoringThread.join();
-    //close the files
-    if (fNightlyLogFile.is_open())
-        fNightlyLogFile.close();
-    if (fNightlyReportFile.is_open())
-        fNightlyReportFile.close();
-    //check if some run number entries can be deleted
+    //clear any remaining run number (should remain only one)
      while (fRunNumber.size() > 0)
      {
@@ -1026,7 +1223,4 @@
 int DataLogger::OpenRunFile(RunNumberType& run)
 {
-    ostringstream sRun;
-    sRun << run.runNumber;
-    run.logName = fRunFilePath + '/' + sRun.str() + ".log";
     if (run.logFile->is_open())
     {
@@ -1036,5 +1230,5 @@
         return -1;
     }
-
+    FormRunTextFileName(run.logName, false, run.runNumber);
     run.logFile->open(run.logName.c_str(), ios_base::out | ios_base::app);
     if (errno != 0)
@@ -1045,5 +1239,5 @@
     }
     //open report file
-    run.reportName = fRunFilePath + '/' + sRun.str() + ".rep";
+    FormRunTextFileName(run.reportName, true, run.runNumber);
     if (run.reportFile->is_open())
     {
@@ -1069,53 +1263,11 @@
     }
     //get the size of the newly opened file.
-    struct stat st;
-    fBaseSizeRun = 0;
-    if (fFileSizesMap.find(run.logName) == fFileSizesMap.end())
-    {
-        stat(run.logName.c_str(), &st);
-        if (errno != 0)
-        {
-            ostringstream str;
-            str << "Unable to stat " << run.logName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-            Error(str);
-        }
-        else
-            fBaseSizeRun += st.st_size;
-        fFileSizesMap[run.logName] = 0;
-    }
-    if (fFileSizesMap.find(run.reportName) == fFileSizesMap.end())
-    {
-        stat(run.reportName.c_str(), &st);
-        if (errno != 0)
-        {
-            ostringstream str;
-            str << "Unable to stat " << run.reportName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-            Error(str);
-        }
-        else
-            fBaseSizeRun += st.st_size;
-        fFileSizesMap[run.reportName] = 0;
-    }
-    string actualTargetDir;
-    if (fRunFilePath == ".")
-    {
-        char currentPath[FILENAME_MAX];
-        if (!getcwd(currentPath, sizeof(currentPath)))
-        {
-            if (errno != 0)
-            {
-                ostringstream str;
-                str << "Unable to retrieve the current path" << ". Reason: " << strerror(errno) << " [" << errno << "]";
-                Error(str);
-            }
-        }
-        actualTargetDir = currentPath;
-    }
-    else
-    {
-        actualTargetDir = fRunFilePath;
-    }
-//TODO this notification scheme might be messed up now.... fix it !
-    NotifyOpenedFile(actualTargetDir + '/' + sRun.str(), 3, fOpenedRunFiles);
+    RememberFileOrigSizePlease(run.logName, false);
+    RememberFileOrigSizePlease(run.reportName, false);
+
+    //TODO this notification scheme might be messed up now.... fix it !
+    ostringstream sRun;
+    sRun << run.runNumber;
+    NotifyOpenedFile(CheckIfDirIsDot(fRunFilePath) + '/' + sRun.str(), 3, fOpenedRunFiles);
     run.openedFits.clear();
     return 0;
@@ -1273,10 +1425,4 @@
         if (fNightlyReportFile.is_open())
         {
-            if (fDebugIsOn)
-            {
-                ostringstream str;
-                str << "Writing: \"" << header.str() << text << "\" to Nightly report file";
-                Debug(str);
-            }
             fNightlyReportFile << header.str() << text << endl;
             //check if either eof, bailbit or batbit are set
@@ -1291,10 +1437,4 @@
         if (targetRunFile && targetRunFile->is_open())
         {
-            if (fDebugIsOn)
-            {
-                ostringstream str;
-                str << "Writing: \"" << header.str() << text << "\" to Run report file";
-                Debug(str);
-            }
             *targetRunFile << header.str() << text << endl;
             if (!targetRunFile->good())
@@ -1314,10 +1454,4 @@
         if (fNightlyLogFile.is_open())
         {
-            if (fDebugIsOn)
-            {
-                ostringstream str;
-                str << "Writing: \"" << msg.str() << "\" to Nightly log file";
-                Debug(str);
-            }
             MessageImp nightlyMess(fNightlyLogFile);
             nightlyMess.Write(cTime, msg.str().c_str(), fQuality);
@@ -1331,10 +1465,4 @@
         if (targetRunFile && targetRunFile->is_open())
         {
-            if (fDebugIsOn)
-            {
-                ostringstream str;
-                str << "Writing: \"" << msg.str() << "\" to Run log file";
-                Debug(str);
-            }
             MessageImp runMess(*targetRunFile);
             runMess.Write(cTime, msg.str().c_str(), fQuality);
@@ -1471,25 +1599,10 @@
     Message("-----------------------------------------");
     //print the path configuration
-    string actualTargetDir;
-    if (fNightlyFilePath == ".")
-    {
-        char currentPath[FILENAME_MAX];
-        if (getcwd(currentPath, sizeof(currentPath)))
-            actualTargetDir = currentPath;
-    }
-    else
-        actualTargetDir = fNightlyFilePath;
-    Message("Nightly Path: " + actualTargetDir);
-    if (fRunFilePath == ".")
-    {
-        char currentPath[FILENAME_MAX];
-        if (getcwd(currentPath, sizeof(currentPath)))
-            actualTargetDir = currentPath;
-    }
-    else
-        actualTargetDir = fRunFilePath;
-    Message("Run Path: " + actualTargetDir);
+    Message("Nightly Path: " + CheckIfDirIsDot(fNightlyFilePath));
+    Message("Run Path: " + CheckIfDirIsDot(fRunFilePath));
+
+    //print active run numbers
     ostringstream str;
-    str << "Active Run Numbers: ";//<< fRunNumber;
+    str << "Active Run Numbers: ";
     for (list<RunNumberType>::iterator it=fRunNumber.begin(); it!=fRunNumber.end(); it++)
         str << "\n" << it->runNumber;
@@ -1610,5 +1723,5 @@
         return GetCurrentState();    
     }
-    if (fStatsPeriodDuration != fStatsPeriodDuration)
+    if (!finite(fStatsPeriodDuration))// != fStatsPeriodDuration)
     {
         Error("Provided duration does not appear to be a valid float. discarding it.");
@@ -1772,24 +1885,10 @@
         Debug("Starting...");    
     }
-    Time time;
-    ostringstream sTime;
-    sTime << time.Y() << "_" << time.M() << "_" << time.D();
-
-    fFullNightlyLogFileName = fNightlyFilePath + '/' + sTime.str() + ".log";
-    fNightlyLogFile.open(fFullNightlyLogFileName.c_str(), ios_base::out | ios_base::app);
-    if (errno != 0)
-    {
-        ostringstream str;
-        str << "Unable to open Nightly Log " << fFullNightlyLogFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-        Error(str);    
-    }
-    fFullNightlyReportFileName = fNightlyFilePath + '/' + sTime.str() + ".rep";
-    fNightlyReportFile.open(fFullNightlyReportFileName.c_str(), ios_base::out | ios_base::app);
-    if (errno != 0)
-    {
-        ostringstream str;
-        str << "Unable to open Nightly Report " << fFullNightlyReportFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-        Error(str);    
-    }
+
+    FormNightlyTextFileName(fFullNightlyLogFileName, false);
+    OpenTextFilePlease(fNightlyLogFile, fFullNightlyLogFileName);
+
+    FormNightlyTextFileName(fFullNightlyReportFileName, true);
+    OpenTextFilePlease(fNightlyReportFile, fFullNightlyReportFileName);
 
     if (!fNightlyLogFile.is_open() || !fNightlyReportFile.is_open())
@@ -1801,34 +1900,15 @@
     }
     //get the size of the newly opened file.
-    struct stat st;
-    stat(fFullNightlyLogFileName.c_str(), &st);
-    fBaseSizeNightly = st.st_size;    
-    stat(fFullNightlyReportFileName.c_str(), &st);
-    fBaseSizeNightly += st.st_size;
+    fBaseSizeNightly = GetFileSize(fFullNightlyLogFileName);
+    fBaseSizeNightly += GetFileSize(fFullNightlyReportFileName);
     fFileSizesMap.clear();
     fBaseSizeRun = 0;
     fPreviousSize = 0;
-    //notify that files were opened
-    string actualTargetDir;
-    if (fNightlyFilePath == ".")
-    {
-        char currentPath[FILENAME_MAX];
-        if (!getcwd(currentPath, sizeof(currentPath)))
-        {
-            if (errno != 0)
-            {
-                ostringstream str;
-                str << "Unable retrieve current path" << ". Reason: " << strerror(errno) << " [" << errno << "]";
-                Error(str);
-            }
-        }
-        actualTargetDir = currentPath;
-    }
-    else
-    {
-        actualTargetDir = fNightlyFilePath;
-    }
+
     //notify that a new file has been opened.
-    NotifyOpenedFile(actualTargetDir + '/' + sTime.str(), 3, fOpenedNightlyFiles);
+    Time time;
+    ostringstream sTime;
+    sTime << time.Y() << "_" << time.M() << "_" << time.D();
+    NotifyOpenedFile(CheckIfDirIsDot(fNightlyFilePath) + '/' + sTime.str(), 3, fOpenedNightlyFiles);
 
     fOpenedNightlyFits.clear();
@@ -1850,7 +1930,5 @@
      {
          if (fDebugIsOn)
-         {
              Debug("Run number changed. Closing " + sub.runFile.fFileName);
-         }
          sub.runFile.Close();
      }
@@ -1877,38 +1955,21 @@
         }    
     }
-    Time time;
-    ostringstream sTime;
-    sTime << time.Y() << "_" << time.M() << "_" << time.D();
     //we open the NightlyFile anyway, otherwise this function shouldn't have been called.
     if (!sub.nightlyFile.IsOpen())
     {
-        string fileNameOnly = sTime.str() + '_' + serviceName + ".fits";
-        string partialName = fNightlyFilePath + '/' + fileNameOnly;
+        string fileNameOnly, partialName;
+        FormNightlyFitsFileName(fileNameOnly, partialName, serviceName);
         AllocateFITSBuffers(sub);
         //get the size of the file we're about to open
-        if (fFileSizesMap.find(partialName) == fFileSizesMap.end())
-        {
-            struct stat st;
-            if (!stat(partialName.c_str(), &st))
-                fBaseSizeNightly += st.st_size;
-            fFileSizesMap[partialName] = 0;
-            //remember that this file was opened.
+        if (RememberFileOrigSizePlease(partialName, true))//and remember that the file was opened (i.e. not an update)
             fOpenedNightlyFits[fileNameOnly].push_back(serviceName);
-        }
+
         sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, -1);//Out());
 
         //notify the opening
-        string actualTargetDir;
-        if (fNightlyFilePath == ".")
-        {
-            char currentPath[FILENAME_MAX];
-            if (getcwd(currentPath, sizeof(currentPath)))
-                actualTargetDir = currentPath;
-        }
-        else
-        {
-            actualTargetDir = fNightlyFilePath;
-        }        
-        NotifyOpenedFile(actualTargetDir + '/' + sTime.str(), 7, fOpenedNightlyFiles);
+        Time time;
+        ostringstream sTime;
+        sTime << time.Y() << "_" << time.M() << "_" << time.D();
+        NotifyOpenedFile(CheckIfDirIsDot(fNightlyFilePath) + '/' + sTime.str(), 7, fOpenedNightlyFiles);
         if (fNumSubAndFitsIsOn)
             fNumSubAndFits->updateService();
@@ -1920,9 +1981,7 @@
         }
     }
-    //to the actual file open
+    //do the actual file open
     if (!sub.runFile.IsOpen() && (GetCurrentState() == kSM_Logging) && sub.runNumber != -1)
     {//buffer for the run file have already been allocated when doing the Nightly file
-        ostringstream sRun;
-        sRun << sub.runNumber;
         string fileNameOnly;
         string partialName;
@@ -1930,23 +1989,13 @@
         if (hasGrouping)
         {
-            fileNameOnly = sRun.str() + "_group.fits";
-            partialName = fRunFilePath + '/' + fileNameOnly;
+            FormGroupedRunFitsFileName(fileNameOnly, partialName, sub.runNumber);
         }
         else
         {
-            fileNameOnly = sRun.str() + '_' + serviceName + ".fits";
-            partialName = fRunFilePath + '/' + fileNameOnly;
+            FormRunFitsFileName(fileNameOnly, partialName, serviceName, sub.runNumber);
         }
         //get the size of the file we're about to open
-        if (fFileSizesMap.find(partialName) == fFileSizesMap.end())
-        {
-            struct stat st;
-            if (!stat(partialName.c_str(), &st))
-                fBaseSizeRun += st.st_size;
-            else
-                fBaseSizeRun = 0;
-            fFileSizesMap[partialName] = 0;
+        if (RememberFileOrigSizePlease(partialName, false))//and remember that the file was opened (i.e. not an update)
             cRunNumber->openedFits[fileNameOnly].push_back(serviceName);
-        }
         else
             if (hasGrouping)
@@ -1979,16 +2028,7 @@
             }
 
-        string actualTargetDir;
-        if (fRunFilePath == ".")
-        {
-            char currentPath[FILENAME_MAX];
-            if (getcwd(currentPath, sizeof(currentPath)))
-                actualTargetDir = currentPath;
-        }
-        else
-        {
-           actualTargetDir = fRunFilePath;
-        }
-        NotifyOpenedFile(actualTargetDir + '/' + sRun.str(), 7, fOpenedRunFiles);// + '_' + serviceName, 4);
+        ostringstream sRun;
+        sRun << sub.runNumber;
+        NotifyOpenedFile(CheckIfDirIsDot(fRunFilePath) + '/' + sRun.str(), 7, fOpenedRunFiles);// + '_' + serviceName, 4);
         if (hasGrouping)
             sub.runFile.Open(partialName, serviceName, cRunNumber->runFitsFile, &fNumSubAndFitsData.numOpenFits, this, sub.runNumber);//Out());
@@ -2169,22 +2209,11 @@
         return;
     }
-    ostringstream groupName;
-    if (runNumber != -1)
-    {
-        groupName << fRunFilePath << '/' << runNumber << ".fits";
-    }
-    else
-    {
-        Time time;
-        ostringstream sTime;
-        sTime << time.Y() << "_" << time.M() << "_" << time.D();
-
-        groupName << fNightlyFilePath << '/' << sTime.str() << ".fits";
-    }
+    string groupName;
+    FormFitsGroupingFileName(groupName, runNumber);
     CCfits::Table* groupTable;
     int maxCharLength = 50;//FILENAME_MAX;
     try
     {
-        groupFile = new CCfits::FITS(groupName.str(), CCfits::RWmode::Write);
+        groupFile = new CCfits::FITS(groupName, CCfits::RWmode::Write);
         //setup the column names
         ostringstream pathTypeName;
@@ -2207,5 +2236,5 @@
      {
          ostringstream str;
-         str << "Could not open or create FITS table GROUPING in  file " << groupName.str() << " reason: " << e.message();
+         str << "Could not open or create FITS table GROUPING in  file " << groupName << " reason: " << e.message();
          Error(str);
          return;
@@ -2374,4 +2403,8 @@
                 Debug("                   " + *it);
         }
+//Adding entries that should ALWAYS be ignored, because of Dim: otherwise the DataLogger would crash
+        fBlackList.insert("DATA_LOGGER/");
+        fBlackList.insert("/SERVICE_LIST");
+        fBlackList.insert("DIS_DNS/");
     }
     if (conf.Has("allow"))
@@ -2555,4 +2588,11 @@
 int main(int argc, const char* argv[])
 {
+
+    float salut1 = 1.0f/0.0f;
+    if (salut1 != salut1)
+        cout << "NaN !";
+        else
+            cout << "regular number";
+
     Configuration conf(argv[0]);
     conf.SetPrintUsage(PrintUsage);
