Index: /trunk/FACT++/src/Fits.cc
===================================================================
--- /trunk/FACT++/src/Fits.cc	(revision 10813)
+++ /trunk/FACT++/src/Fits.cc	(revision 10814)
@@ -82,10 +82,12 @@
 //! @param fitsCounter a pointer to the integer keeping track of the opened FITS files
 //! @param out a pointer to the MessageImp that should be used to log errors
-//
-void Fits::Open(const string& fileName, const string& tableName, FITS* file, int* fitsCounter, MessageImp* out)//ostream& out)
+//! @param runNumber the runNumber for which this file is opened. -1 means nightly file.
+//
+void Fits::Open(const string& fileName, const string& tableName, FITS* file, int* fitsCounter, MessageImp* out, int runNumber)
 {		
 //	if (fMess)
 //		delete fMess;
 //	fMess = new MessageImp(out);
+    fRunNumber = runNumber;
 	fMess = out;
 	fFileName = fileName;
Index: /trunk/FACT++/src/Fits.h
===================================================================
--- /trunk/FACT++/src/Fits.h	(revision 10813)
+++ /trunk/FACT++/src/Fits.h	(revision 10814)
@@ -59,5 +59,7 @@
 		///were to log the errors
 		MessageImp* fMess;		
-	public:
+public:
+        ///current run number being logged
+        int fRunNumber;
 		
 		Fits() :  fFile(NULL),
@@ -73,5 +75,6 @@
 					 fFileName(""),
 					 fNumOpenFitsFiles(NULL),
-					 fMess(NULL)
+					 fMess(NULL),
+					 fRunNumber(-1)
 		 {}
 		
@@ -91,5 +94,5 @@
 
 		///Opens a FITS file
-        void Open(const string& fileName, const string& tableName, CCfits::FITS* file, int* fitsCounter, MessageImp* out);//ostream& out);
+        void Open(const string& fileName, const string& tableName, CCfits::FITS* file, int* fitsCounter, MessageImp* out, int runNumber);//ostream& out);
 
 		///Write one line of data. Use the given converter.
Index: /trunk/FACT++/src/dataLogger.cc
===================================================================
--- /trunk/FACT++/src/dataLogger.cc	(revision 10813)
+++ /trunk/FACT++/src/dataLogger.cc	(revision 10814)
@@ -78,4 +78,5 @@
 
 //Dim structures
+///Distributes the writting statistics
 struct DataLoggerStats {
     long sizeWritten;
@@ -83,13 +84,198 @@
     long writingRate;
 };
-
+///distributes the number of opened subscriptions and fits files
 struct NumSubAndFitsType {
     int numSubscriptions;
     int numOpenFits;
 };
-
+///distributes which files were opened.
 struct OpenFileToDim {
     int code;
     char fileName[FILENAME_MAX];
+};
+
+///Run number record. Used to keep track of which run numbers are still active
+struct RunNumberType {
+    ///the run number log file
+    ofstream* logFile;
+    ///the run number report file
+    ofstream* reportFile;
+#ifdef HAVE_FITS
+    ///the run number group fits file
+    CCfits::FITS* runFitsFile;
+#endif
+    ///the log filename
+    string logName;
+    ///the report filename
+    string reportName;
+    ///the actual run number
+    int runNumber;
+    ///the time at which the run number was received
+    Time time;
+    ///internal counter used for smart referencing
+    int* numCopies;
+    ///list of opened fits used to create the fits grouping when the run ends
+    map<string, vector<string> > openedFits;
+    ///default constructor
+    RunNumberType()
+    {
+        logFile = new ofstream();
+        reportFile = new ofstream();
+#ifdef HAVE_FITS
+        runFitsFile = NULL;
+#endif
+        runNumber = -1;
+        time = Time(0,0);
+        numCopies = new int(1);
+    }
+    ///default destructor
+    ~RunNumberType()
+    {
+        if (numCopies)
+        {
+            (*numCopies)--;
+            if (*numCopies < 1)
+            {
+                if (logFile)
+                {
+                    if (logFile->is_open())
+                        logFile->close();
+                    delete logFile;
+                    logFile = NULL;
+                }
+                if (reportFile)
+                {
+                    if (reportFile->is_open())
+                        reportFile->close();
+                    delete reportFile;
+                    reportFile = NULL;
+                }
+#ifdef HAVE_FITS
+                if (runFitsFile)
+                {
+                    delete runFitsFile;
+                    runFitsFile = NULL;
+                }
+#endif
+                delete numCopies;
+                numCopies = NULL;
+            }
+        }
+    }
+    ///copy operator
+    void operator = (const RunNumberType& other)
+    {
+        logFile = other.logFile;
+        reportFile = other.reportFile;
+        logName = other.logName;
+        reportName = other.reportName;
+        runNumber = other.runNumber;
+        time = other.time;
+        numCopies = other.numCopies;
+#ifdef HAVE_FITS
+        runFitsFile = other.runFitsFile;
+#endif
+        (*numCopies)++;
+    }
+    ///copy constructor
+    RunNumberType(const RunNumberType& other)
+    {
+        logFile = other.logFile;
+        reportFile = other.reportFile;
+        logName = other.logName;
+        reportName = other.reportName;
+        runNumber = other.runNumber;
+        time = other.time;
+        numCopies = other.numCopies;
+#ifdef HAVE_FITS
+        runFitsFile = other.runFitsFile;
+#endif
+        (*numCopies)++;
+    }
+
+};
+///Dim subscription type. Stores all the relevant info to handle a Dim subscription
+struct SubscriptionType
+{
+#ifdef HAVE_FITS
+    ///Nightly FITS output file
+    Fits    nightlyFile;
+    ///run-specific FITS output file
+    Fits    runFile;
+#endif
+    ///the actual dimInfo pointer
+    DimStampedInfo* dimInfo;
+    ///the converter for outputting the data according to the format
+    Converter* fConv;
+    ///internal counter used for smart referencing
+    int* numCopies;
+    ///the current run number used by this subscription
+    int runNumber;
+    ///copy operator
+    void operator = (const SubscriptionType& other)
+    {
+#ifdef HAVE_FITS
+        nightlyFile = other.nightlyFile;
+        runFile = other.runFile;
+#endif
+        dimInfo = other.dimInfo;
+        numCopies = other.numCopies;
+        fConv = other.fConv;
+        runNumber = other.runNumber;
+        (*numCopies)++;
+    }
+    ///copy constructor
+    SubscriptionType(const SubscriptionType& other)
+    {
+#ifdef HAVE_FITS
+        nightlyFile = other.nightlyFile;
+        runFile = other.runFile;
+#endif
+        dimInfo = other.dimInfo;
+        numCopies = other.numCopies;
+        fConv = other.fConv;
+        runNumber = other.runNumber;
+        (*numCopies)++;
+    }
+    ///Dim info constructor
+    SubscriptionType(DimStampedInfo* info)
+    {
+        dimInfo = info;
+        fConv = NULL;
+        runNumber = -1;
+        numCopies = new int(1);
+    }
+    ///default constructor
+    SubscriptionType()
+    {
+        dimInfo = NULL;
+        fConv = NULL;
+        runNumber = -1;
+        numCopies = new int(1);
+    }
+    ///default destructor
+    ~SubscriptionType()
+    {
+        if (numCopies)
+            (*numCopies)--;
+        if (numCopies)
+        if (*numCopies < 1)
+        {
+            if (dimInfo)
+            delete dimInfo;
+#ifdef HAVE_FITS
+            if (nightlyFile.IsOpen())
+                nightlyFile.Close();
+            if (runFile.IsOpen())
+                runFile.Close();
+#endif
+            if (numCopies)
+            delete numCopies;
+            delete fConv;
+            fConv = NULL;
+            dimInfo = NULL;
+            numCopies = NULL;
+        }
+    }
 };
 
@@ -109,22 +295,21 @@
     DataLogger(ostream &out);
     ~DataLogger(); 
-    
+
+    bool SetConfiguration(Configuration& conf);
+
 private:
-    //Define all the data structure specific to the DataLogger here
-     /// ofstream for the NightlyLogfile
+    /************************************************
+     * MEMBER VARIABLES
+     ************************************************/
+    /// ofstream for the NightlyLogfile
     ofstream fNightlyLogFile;
-    /// ofstream for the run-specific Log file
-    ofstream fRunLogFile;
-
     /// ofstream for the Nightly report file
     ofstream fNightlyReportFile;
-    /// ofstream for the run-specific report file
-    ofstream fRunReportFile;
     /// base path of the Nightlyfile
-    string fNightlyFileName;
+    string fNightlyFilePath;
     ///base path of the run file
-    string fRunFileName;
-    ///run number (-1 means no run number specified)
-    int fRunNumber; 
+    string fRunFilePath;
+    ///run numbers
+    list<RunNumberType> fRunNumber;
     ///previous run number. to check if changed while logging
     int fPreviousRunNumber;
@@ -133,5 +318,18 @@
     ///Modified Julian Date
     double fMjD;
+    ///for obtaining the name of the existing services
+    ServiceList fServiceList;
+    typedef map<const string, map<string, SubscriptionType> > SubscriptionsListType;
+    ///All the services to which we have subscribed to, sorted by server name.
+    SubscriptionsListType fServiceSubscriptions;
+    ///full name of the nightly log file
+    string fFullNightlyLogFileName;
+    ///full name of the nightly report file
+    string fFullNightlyReportFileName;
+
 public:
+    /***************************************************
+     * STATIC COMMAND NAMES
+     ***************************************************/
     ///Define all the static names
     static const char* fConfigDay;
@@ -152,91 +350,15 @@
     static const char* fStartStopNumSubsAndFits;
 private:
+    /***************************************************
+     * DIM INFO HANDLER
+     ***************************************************/
     //overloading of DIM's infoHandler function
     void infoHandler(); 
-    
-    ///for obtaining the name of the existing services
-    ServiceList fServiceList;
-    
-    ///A std pair to store both the DimInfo pointer and the corresponding outputted fits file
-    struct SubscriptionType
-    { 
-#ifdef HAVE_FITS
-        ///Nightly FITS output file
-        Fits    nightlyFile;
-        ///run-specific FITS output file
-        Fits    runFile;
-#endif
-        ///the actual dimInfo pointer
-        DimStampedInfo* dimInfo;
-        ///the converter for outputting the data according to the format
-        Converter* fConv;
-        ///the number of existing handlers to this structure.
-        ///This is required otherwise I MUST handle the deleting of dimInfo outside from the destructor
-        int* numCopies;
-        void operator = (const SubscriptionType& other)
-        {
-#ifdef HAVE_FITS
-            nightlyFile = other.nightlyFile;
-            runFile = other.runFile;
-#endif
-            dimInfo = other.dimInfo;    
-            numCopies = other.numCopies;
-            fConv = other.fConv;
-            (*numCopies)++;
-        }
-        SubscriptionType(const SubscriptionType& other)
-        {
-#ifdef HAVE_FITS
-            nightlyFile = other.nightlyFile;
-            runFile = other.runFile;
-#endif
-            dimInfo = other.dimInfo;
-            numCopies = other.numCopies;
-            fConv = other.fConv;
-            (*numCopies)++;    
-        }
-        SubscriptionType(DimStampedInfo* info)
-        {
-            dimInfo = info;    
-            fConv = NULL;
-            numCopies = new int(1);
-        }
-        SubscriptionType()
-        {
-            dimInfo = NULL;
-            fConv = NULL;
-            numCopies = new int(1);
-        }
-        ~SubscriptionType()
-        {
-            if (numCopies)
-            (*numCopies)--;
-            if (numCopies)
-            if (*numCopies < 1)
-            {
-                if (dimInfo)
-                delete dimInfo;
-#ifdef HAVE_FITS
-                if (nightlyFile.IsOpen())
-                    nightlyFile.Close();
-                if (runFile.IsOpen())
-                    runFile.Close();
-#endif
-                if (numCopies)    
-                delete numCopies;
-                delete fConv;
-                fConv = NULL;
-                dimInfo = NULL;
-                numCopies = NULL;
-            }
-        }
-    };
-    typedef map<const string, map<string, SubscriptionType> > SubscriptionsListType;
-    ///All the services to which we have subscribed to, sorted by server name.
-    SubscriptionsListType fServiceSubscriptions;
-
+
+    /***************************************************
+     * TRANSITION FUNCTIONS
+     ***************************************************/
     ///Reporting method for the services info received
     void ReportPlease(DimInfo* I, SubscriptionType& sub);  
-
     ///Configuration of the nightly file path
     int ConfigureNightlyFileName(const Event& evt); 
@@ -246,5 +368,5 @@
     int ConfigureRunNumber(const Event& evt); 
     ///logging method for the messages
-    int LogMessagePlease(const Event& evt); 
+//    int LogMessagePlease(const Event& evt);
     ///print the current state of the dataLogger
     int PrintStatePlease(const Event& evt);
@@ -263,29 +385,24 @@
 #ifdef HAVE_FITS
     ///Open fits files
-    void OpenFITSFilesPlease(SubscriptionType& sub);
+    void OpenFITSFilesPlease(SubscriptionType& sub, RunNumberType* cRunNumber);
     ///Write data to FITS files
     void WriteToFITS(SubscriptionType& sub);
     ///Allocate the buffers required for fits
     void AllocateFITSBuffers(SubscriptionType& sub);
-    ///FITS file for runs grouping. only one, hence dealt with in the dataLogger itself
-    CCfits::FITS* fRunFitsFile;
 #endif//has_fits
-public:    
-    ///checks with fServiceList whether or not the services got updated
-    bool CheckForServicesUpdate(); 
-
-private:    
+
+    /***************************************
+     * DIM SERVICES PROVIDED BY THE DATA LOGGER
+     ***************************************/
     ///monitoring notification loop
     void ServicesMonitoring();
+    inline void NotifyOpenedFile(string name, int type, DimDescribedService* service);
     ///services notification thread
     boost::thread fMonitoringThread;
     ///end of the monitoring
     bool fContinueMonitoring;
-    ///required for accurate monitoring
+    ///stores the size of each file that is or was open
     map<string, long> fFileSizesMap;
-    string fFullNightlyLogFileName;
-    string fFullNightlyReportFileName;
-    string fFullRunLogFileName;
-    string fFullRunReportFileName;
+    ///total size of the opened files BEFORE they were opened by the logger
     long fBaseSizeNightly;
     long fPreviousSize;
@@ -296,14 +413,16 @@
     DimDescribedService* fNumSubAndFits;
     NumSubAndFitsType fNumSubAndFitsData;
-
-    inline void NotifyOpenedFile(string name, int type, DimDescribedService* service);
-
-public:    
-    bool SetConfiguration(Configuration& conf);
-
-private:
+    ///Small function for calculating the total size written so far
+    void calculateTotalSizeWritten(DataLoggerStats& statVar, bool& shouldWarn, bool isPrinting);
+
+    /***************************************************
+     * DATA LOGGER's CONFIGURATION STUFF
+     ***************************************************/
+    ///black/white listing
     set<string> fBlackList;
     set<string> fWhiteList;
+    ///list of services to be grouped
     set<string> fGrouping;
+    ///configuration flags
     bool fHasBlackList;
     bool fHasWhiteList;
@@ -317,17 +436,67 @@
     int SetOpenedFilesOnOff(const Event& evt);
     int SetNumSubsAndFitsOnOff(const Event& evt);
+
     ///boolean to prevent DIM update while desctructing the dataLogger
     bool fDestructing;    
-
-    ///Small function for calculating the total size written so far
-    void calculateTotalSizeWritten(DataLoggerStats& statVar, bool& shouldWarn, bool isPrinting);
-
+    /***************************************************
+     * UTILITIES
+     ***************************************************/
     ///vectors to keep track of opened Fits files, for grouping purposes.
-    //This cannot be done otherwise, as services may disapear before the files are nicely closed. Hence which files were opened must be remembered.
-    map<string, vector<string> > fOpenedRunFits;
     map<string, vector<string> > fOpenedNightlyFits;
-    void CreateFitsGrouping(bool runGroup);
+    ///creates a group fits file based on a list of files to be grouped
+    void CreateFitsGrouping(map<string, vector<string> >& filesToGroup, int runNumber);
+    ///Open the relevant text files related to a particular run
+    int OpenRunFile(RunNumberType& run);
+    ///add a new run number
+    void AddNewRunNumber(int newRun, Time time);
+    ///removes the oldest run number, and close the relevant files.
+    void RemoveOldestRunNumber();
+    ///checks with fServiceList whether or not the services got updated
+    bool CheckForServicesUpdate();
 }; //DataLogger
 
+// --------------------------------------------------------------------------
+//
+//! Removes the oldest run number and closes the fits files that should be closed
+//! Also creates the fits grouping file
+//
+void DataLogger::RemoveOldestRunNumber()
+{
+    if (fDebugIsOn)
+    {
+        stringstream str;
+        str << "Removing run number " << fRunNumber.front().runNumber;
+        Debug(str.str());
+    }
+    CreateFitsGrouping(fRunNumber.front().openedFits, fRunNumber.front().runNumber);
+    //crawl through the subscriptions to see if there are still corresponding fits files opened.
+    SubscriptionsListType::iterator x;
+    map<string, SubscriptionType>::iterator y;
+    for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
+        for (y=x->second.begin(); y != x->second.end(); y++)
+            if (y->second.runFile.fRunNumber == fRunNumber.front().runNumber && y->second.runFile.IsOpen())
+            {
+                if (fDebugIsOn)
+                {
+                    stringstream str;
+                    str << "Closing Fits run file " << y->second.runFile.fFileName;
+                    Debug(str.str());
+                }
+                y->second.runFile.Close();
+            }
+    //if a grouping file is on, decrease the number of opened fits manually
+    if (fRunNumber.front().runFitsFile)
+        (fNumSubAndFitsData.numOpenFits)--;
+    //remove the entry
+    fRunNumber.pop_front();
+}
+
+// --------------------------------------------------------------------------
+//
+//! Calculate the total number of written bytes since the logger was started
+//! @param statVar the data structure that should be updated
+//! @param shouldWarn whether or not error messages should be outputted
+//! @param isPrinting whether this function was called from the PRINT command or not. If so, displays relevant information
+//
 void DataLogger::calculateTotalSizeWritten(DataLoggerStats& statVar, bool& shouldWarn, bool isPrinting)
 {
@@ -377,16 +546,19 @@
         fFileSizesMap[fFullNightlyReportFileName] = st.st_size;
     }
-    if (fRunLogFile.is_open())
-    {
-        stat(fFullRunLogFileName.c_str(), &st);
-        fFileSizesMap[fFullRunLogFileName] = st.st_size;
-    }
-    if (fRunReportFile.is_open())
-    {
-        stat(fFullRunReportFileName.c_str(), &st);
-        fFileSizesMap[fFullRunReportFileName] = st.st_size;
+    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;
+        }
+        if (it->logFile->is_open())
+        {
+            stat(it->logName.c_str(), &st);
+            fFileSizesMap[it->logName] = st.st_size;
+        }
     }
     struct statvfs vfs;
-    if (!statvfs(fNightlyFileName.c_str(), &vfs))
+    if (!statvfs(fNightlyFilePath.c_str(), &vfs))
     {
         statVar.freeSpace = vfs.f_bsize*vfs.f_bavail;
@@ -397,5 +569,5 @@
         ostringstream str;
         str.str("");
-        str << "Unable to retrieve stats for " << fNightlyFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
+        str << "Unable to retrieve stats for " << fNightlyFilePath << ". Reason: " << strerror(errno) << " [" << errno << "]";
         if (!shouldWarn)
             Error(str);
@@ -429,4 +601,9 @@
 const char* DataLogger::fStartStopNumSubsAndFits = "NUM_SUBS_SRVC";
 
+// --------------------------------------------------------------------------
+//
+//! Monitor the number of opened files and total size written, and distributes this data through a Dim service
+//
+//
 void DataLogger::ServicesMonitoring()
 {
@@ -437,5 +614,5 @@
 
         struct statvfs vfs;
-        if (!statvfs(fNightlyFileName.c_str(), &vfs))
+        if (!statvfs(fNightlyFilePath.c_str(), &vfs))
             statVar.freeSpace = vfs.f_bsize*vfs.f_bavail;
         else
@@ -448,4 +625,9 @@
         while (fContinueMonitoring)
         {
+            //check if some run number entries can be deleted
+            while (fRunNumber.size() > 1 && (Time() - fRunNumber.front().time) > boost::posix_time::time_duration(0,0,10,0))
+            {
+                RemoveOldestRunNumber();
+            }
             if (fStatsPeriodDuration == 0.0f)
             {
@@ -477,5 +659,4 @@
 }
 
-
 // --------------------------------------------------------------------------
 //
@@ -490,57 +671,52 @@
     dis_disable_padding();
     //initialize member data
-    fNightlyFileName = ".";
-    fRunFileName = ".";
-    fRunNumber = -1;
-    fPreviousRunNumber = fRunNumber;
-#ifdef HAVE_FITS
-    fRunFitsFile = NULL;
-#endif
-
-     //Give a name to this machine's specific states
-     AddStateName(kSM_NightlyOpen,      "NightlyFileOpen",  "The summary files for the night are open.");
-     AddStateName(kSM_WaitingRun,       "WaitForRun",       "The summary files for the night are open and we wait for a run to be started.");
-     AddStateName(kSM_Logging,          "Logging",          "The summary files for the night and the files for a single run are open.");
-     AddStateName(kSM_BadNightlyConfig, "ErrNightlyFolder", "The folder for the nighly summary files is invalid.");
-     AddStateName(kSM_BadRunConfig,     "ErrRunFolder",     "The folder for the run files is invalid.");
-
-     /*Add the possible transitions for this machine*/
-     AddEvent(kSM_NightlyOpen, fTransStart, kSM_Ready, kSM_BadNightlyConfig)
-             (boost::bind(&DataLogger::StartPlease, this))
-             ("Start the nightly logging. Nightly file location must be specified already");
-
-     AddEvent(kSM_Ready, fTransStop, kSM_NightlyOpen, kSM_WaitingRun, kSM_Logging)
-             (boost::bind(&DataLogger::GoToReadyPlease, this))
-             ("Stop all data logging, close all files.");
-
-     AddEvent(kSM_Logging, fTransStartRun, kSM_WaitingRun, kSM_BadRunConfig)
-             (boost::bind(&DataLogger::StartRunPlease, this))
-             ("Start the run logging. Run file location must be specified already.");
-
-     AddEvent(kSM_WaitingRun, fTransStopRun, kSM_Logging)
-             (boost::bind(&DataLogger::StopRunPlease, this))
-             ("Wait for a run to be started, open run-files as soon as a run number arrives.");
-
-     AddEvent(kSM_Ready, fTransReset, kSM_Error, kSM_BadNightlyConfig, kSM_BadRunConfig, kSM_Error)
-             (boost::bind(&DataLogger::GoToReadyPlease, this))
-             ("Transition to exit error states. Closes the nightly file if already opened.");
-
-     AddEvent(kSM_WaitingRun, fTransWait, kSM_NightlyOpen)
-             (boost::bind(&DataLogger::NightlyToWaitRunPlease, this));
-
-     /*Add the possible configurations for this machine*/
-     AddEvent(fConfigDay, "C", kSM_Ready, kSM_BadNightlyConfig)
-             (boost::bind(&DataLogger::ConfigureNightlyFileName, this, _1))
-             ("Configure the folder for the nightly files."
-              "|Path[string]:Absolute or relative path name where the nightly files should be stored.");
-
-     AddEvent(fConfigRun, "C", kSM_Ready, kSM_BadNightlyConfig, kSM_NightlyOpen, kSM_WaitingRun, kSM_BadRunConfig)
-             (boost::bind(&DataLogger::ConfigureRunFileName, this, _1))
-             ("Configure the folder for the run files."
-              "|Path[string]:Absolute or relative path name where the run files should be stored.");
-
-     AddEvent(fConfigRunNumber, "I", kSM_Ready, kSM_BadNightlyConfig, kSM_NightlyOpen, kSM_WaitingRun, kSM_BadRunConfig)
-             (boost::bind(&DataLogger::ConfigureRunNumber, this, _1))
-             ("configure the run number. cannot be done in logging state");
+    fNightlyFilePath = ".";
+    fRunFilePath = ".";
+
+    //Give a name to this machine's specific states
+    AddStateName(kSM_NightlyOpen,      "NightlyFileOpen",  "The summary files for the night are open.");
+    AddStateName(kSM_WaitingRun,       "WaitForRun",       "The summary files for the night are open and we wait for a run to be started.");
+    AddStateName(kSM_Logging,          "Logging",          "The summary files for the night and the files for a single run are open.");
+    AddStateName(kSM_BadNightlyConfig, "ErrNightlyFolder", "The folder for the nighly summary files is invalid.");
+    AddStateName(kSM_BadRunConfig,     "ErrRunFolder",     "The folder for the run files is invalid.");
+
+    /*Add the possible transitions for this machine*/
+    AddEvent(kSM_NightlyOpen, fTransStart, kSM_Ready, kSM_BadNightlyConfig)
+            (boost::bind(&DataLogger::StartPlease, this))
+            ("Start the nightly logging. Nightly file location must be specified already");
+
+    AddEvent(kSM_Ready, fTransStop, kSM_NightlyOpen, kSM_WaitingRun, kSM_Logging)
+            (boost::bind(&DataLogger::GoToReadyPlease, this))
+            ("Stop all data logging, close all files.");
+
+    AddEvent(kSM_Logging, fTransStartRun, kSM_WaitingRun, kSM_BadRunConfig)
+            (boost::bind(&DataLogger::StartRunPlease, this))
+            ("Start the run logging. Run file location must be specified already.");
+
+    AddEvent(kSM_WaitingRun, fTransStopRun, kSM_Logging)
+            (boost::bind(&DataLogger::StopRunPlease, this))
+            ("Wait for a run to be started, open run-files as soon as a run number arrives.");
+
+    AddEvent(kSM_Ready, fTransReset, kSM_Error, kSM_BadNightlyConfig, kSM_BadRunConfig, kSM_Error)
+            (boost::bind(&DataLogger::GoToReadyPlease, this))
+            ("Transition to exit error states. Closes the nightly file if already opened.");
+
+    AddEvent(kSM_WaitingRun, fTransWait, kSM_NightlyOpen)
+            (boost::bind(&DataLogger::NightlyToWaitRunPlease, this));
+
+    /*Add the possible configurations for this machine*/
+    AddEvent(fConfigDay, "C", kSM_Ready, kSM_BadNightlyConfig)
+            (boost::bind(&DataLogger::ConfigureNightlyFileName, this, _1))
+            ("Configure the folder for the nightly files."
+             "|Path[string]:Absolute or relative path name where the nightly files should be stored.");
+
+    AddEvent(fConfigRun, "C", kSM_Ready, kSM_BadNightlyConfig, kSM_NightlyOpen, kSM_WaitingRun, kSM_BadRunConfig)
+            (boost::bind(&DataLogger::ConfigureRunFileName, this, _1))
+            ("Configure the folder for the run files."
+             "|Path[string]:Absolute or relative path name where the run files should be stored.");
+
+    AddEvent(fConfigRunNumber, "I", kSM_Ready, kSM_NightlyOpen, kSM_WaitingRun, kSM_BadRunConfig, kSM_Logging)
+            (boost::bind(&DataLogger::ConfigureRunNumber, this, _1))
+            ("configure the run number. cannot be done in logging state");
 
      //Provide a logging command
@@ -549,8 +725,8 @@
      //is already done in StateMachineImp.cc
      //Thus I'll simply add a configuration, which I will treat as the logging command
-     AddEvent(fConfigLog, "C", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_BadRunConfig)
-             (boost::bind(&DataLogger::LogMessagePlease, this, _1))
-             ("Log a single message to the log-files."
-              "|Message[string]:Message to be logged.");
+//     AddEvent(fConfigLog, "C", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_BadRunConfig)
+ //            (boost::bind(&DataLogger::LogMessagePlease, this, _1))
+ //            ("Log a single message to the log-files."
+//              "|Message[string]:Message to be logged.");
         
      //Provide a print command
@@ -768,16 +944,14 @@
     if (fNightlyReportFile.is_open())
         fNightlyReportFile.close();
-    if (fRunLogFile.is_open())
-        fRunLogFile.close();
-    if (fRunReportFile.is_open())
-        fRunReportFile.close();
+    //check if some run number entries can be deleted
+     while (fRunNumber.size() > 0)
+     {
+         RemoveOldestRunNumber();
+     }
+
     delete fOpenedNightlyFiles;
     delete fOpenedRunFiles;
     delete fNumSubAndFits;
-#ifdef HAVE_FITS
-    if (fRunFitsFile != NULL)
-        delete fRunFitsFile;
-    fRunFitsFile = NULL;
-#endif
+
     if (fDebugIsOn)
     {
@@ -847,15 +1021,132 @@
     CheckForRunNumber(I);
 
-    if (fPreviousRunNumber != fRunNumber)
-    {//run number has changed. close and reopen run files.
-        StopRunPlease();
-        StartRunPlease();
-        fPreviousRunNumber = fRunNumber;
-    }
-
     ReportPlease(I, y->second);
 
 }
-
+// --------------------------------------------------------------------------
+//
+//! Open the text files associated with the given run number
+//! @param run the run number to be dealt with
+//
+int DataLogger::OpenRunFile(RunNumberType& run)
+{
+    ostringstream sRun;
+    sRun << run.runNumber;
+    run.logName = fRunFilePath + '/' + sRun.str() + ".log";
+    if (run.logFile->is_open())
+    {
+        stringstream str;
+        str << "Log file " << run.logName << " was already open when trying to open it in OpenRunFile";
+        Error(str.str());
+        return -1;
+    }
+
+    run.logFile->open(run.logName.c_str(), ios_base::out | ios_base::app);
+    if (errno != 0)
+    {
+        ostringstream str;
+        str << "Unable to open run Log " << run.logName << ". Reason: " << strerror(errno) << " [" << errno << "]";
+        Error(str);
+    }
+    //open report file
+    run.reportName = fRunFilePath + '/' + sRun.str() + ".rep";
+    if (run.reportFile->is_open())
+    {
+        stringstream str;
+        str << "Report file " << run.reportName << " was already open when trying to open it in OpenRunFile";
+        Error(str.str());
+        return -1;
+    }
+    run.reportFile->open(run.reportName.c_str(), ios_base::out | ios_base::app);
+    if (errno != 0)
+    {
+        ostringstream str;
+        str << "Unable to open run report " << run.reportName << ". Reason: " << strerror(errno) << " [" << errno << "]";
+        Error(str);
+    }
+
+    if (!run.logFile->is_open() || !run.reportFile->is_open())
+    {
+        ostringstream str;
+        str << "Something went wrong while openning nightly files " << run.logName << " and " << run.reportName;
+        Error(str.str());
+        return -1;
+    }
+    //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);
+    run.openedFits.clear();
+    return 0;
+}
+// --------------------------------------------------------------------------
+//
+//! Add a new active run number
+//! @param newRun the new run number
+//! @param time the time at which the new run number was issued
+//
+void DataLogger::AddNewRunNumber(int newRun, Time time)
+{
+    if (fDebugIsOn)
+    {
+        stringstream str;
+        str << "Adding new run number " << newRun << " that was issued on " << time;
+        Debug(str.str());
+    }
+    //Add new run number to run number list
+    fRunNumber.push_back(RunNumberType());
+    fRunNumber.back().runNumber = newRun;
+    fRunNumber.back().time = time;
+    if (GetCurrentState() != kSM_Logging)
+        return;
+    //open the log and report files
+    OpenRunFile(fRunNumber.back());
+}
 // --------------------------------------------------------------------------
 //
@@ -869,7 +1160,7 @@
     if (strstr(I->getName(), fRunNumberInfo) != NULL)
     {//assumes that the run number is an integer
-        fRunNumber = I->getInt();    
+        AddNewRunNumber(I->getInt(), Time(I->getTimestamp(), I->getTimestampMillisecs()*1000));
         ostringstream str;
-        str << "New run number is " << fRunNumber;
+        str << "New run number is " << fRunNumber.back().runNumber;
         Message(str.str());
     }
@@ -912,5 +1203,4 @@
         }
     }
-        
     //construct the header
     ostringstream header;
@@ -918,4 +1208,36 @@
     fQuality = I->getQuality();
     fMjD = cTime.Mjd();
+
+    //figure out which run file should be used
+    ofstream* targetRunFile = NULL;
+    RunNumberType* cRunNumber = NULL;
+    if (GetCurrentState() == kSM_Logging)
+    {
+        list<RunNumberType>::reverse_iterator rit;
+        for (rit=fRunNumber.rbegin(); rit != fRunNumber.rend(); rit++)
+        {
+            if (rit->time < cTime) //this is the run number that we want to use
+            {
+                //Find something better to convert iterator to pointer than the ugly line below....
+                cRunNumber = &(*rit);
+                sub.runNumber = rit->runNumber;
+                targetRunFile = isItaReport ? rit->reportFile : rit->logFile;
+                break;
+            }
+        }
+        if (rit == fRunNumber.rend() && fRunNumber.size() != 0)
+        {
+            stringstream str;
+            str << "Could not find an appropriate run number for info coming at time: " << cTime;
+            Error(str.str());
+            Error("Active run numbers: ");
+            for (rit=fRunNumber.rbegin(); rit != fRunNumber.rend(); rit++)
+            {
+                str.str("");
+                str << rit->runNumber;
+                Error(str.str());
+            }
+        }
+    }
 
     if (isItaReport)
@@ -972,5 +1294,5 @@
         }
         //write entry to run-report
-        if (fRunReportFile.is_open())
+        if (targetRunFile && targetRunFile->is_open())
         {
             if (fDebugIsOn)
@@ -980,10 +1302,10 @@
                 Debug(str.str());    
             }
-            fRunReportFile << header.str() << text << endl;
-            if (!fRunReportFile.good())
+            *targetRunFile << header.str() << text << endl;
+            if (!targetRunFile->good())
             {
                 Error("An error occured while writing to the run report file. Closing it.");
-                if (fRunReportFile.is_open())
-                    fRunReportFile.close();    
+                if (targetRunFile->is_open())
+                    targetRunFile->close();
             }
         }
@@ -993,5 +1315,5 @@
         string n = I->getName();
         ostringstream msg;
-        msg << n << ": " << I->getString();//n.substr(0, n.find_first_of('/')) << ": " << I->getString();
+        msg << n << ": " << I->getString();
 
         if (fNightlyLogFile.is_open())
@@ -1012,5 +1334,5 @@
             }
         }
-        if (fRunLogFile.is_open())
+        if (targetRunFile && targetRunFile->is_open())
         {
             if (fDebugIsOn)
@@ -1020,11 +1342,11 @@
                 Debug(str.str());    
             }
-            MessageImp runMess(fRunLogFile);
+            MessageImp runMess(*targetRunFile);
             runMess.Write(cTime, msg.str().c_str(), fQuality);
-            if (!fRunLogFile.good())
+            if (!targetRunFile->good())
             {
                 Error("An error occured while writing to the run log file. Closing it.");
-                if (fRunLogFile.is_open())
-                    fRunLogFile.close();    
+                if (targetRunFile->is_open())
+                    targetRunFile->close();
             }
         }
@@ -1034,6 +1356,6 @@
     if (isItaReport)
     {
-        if (!sub.nightlyFile.IsOpen() || !sub.runFile.IsOpen())
-            OpenFITSFilesPlease(sub);    
+        if (!sub.nightlyFile.IsOpen() || !sub.runFile.IsOpen() || sub.runNumber != sub.runFile.fRunNumber)
+            OpenFITSFilesPlease(sub, cRunNumber);
         WriteToFITS(sub);
     }    
@@ -1045,4 +1367,5 @@
 //
 //! write messages to logs. 
+/*
 //! @param evt
 //!        the current event to log
@@ -1138,5 +1461,5 @@
     }
     return GetCurrentState();
-}
+}*/
 // --------------------------------------------------------------------------
 //
@@ -1154,5 +1477,5 @@
     //print the path configuration
     string actualTargetDir;
-    if (fNightlyFileName == ".")
+    if (fNightlyFilePath == ".")
     {
         char currentPath[FILENAME_MAX];
@@ -1161,7 +1484,7 @@
     }
     else
-        actualTargetDir = fNightlyFileName;
+        actualTargetDir = fNightlyFilePath;
     Message("Nightly Path: " + actualTargetDir);
-    if (fRunFileName == ".")
+    if (fRunFilePath == ".")
     {
         char currentPath[FILENAME_MAX];
@@ -1170,8 +1493,10 @@
     }
     else
-        actualTargetDir = fRunFileName;
+        actualTargetDir = fRunFilePath;
     Message("Run Path: " + actualTargetDir);
     ostringstream str;
-    str << "Run Number: " << fRunNumber;
+    str << "Active Run Numbers: ";//<< fRunNumber;
+    for (list<RunNumberType>::iterator it=fRunNumber.begin(); it!=fRunNumber.end(); it++)
+        str << "\n" << it->runNumber;
     Message(str.str());
     Message("----------- OPENED FILES ----------------");
@@ -1185,12 +1510,15 @@
     else
         Message("Nightly Report.....CLOSED");
-    if (fRunLogFile.is_open())
-        Message("Run Log..............OPEN");
-    else
-        Message("Run Log............CLOSED");
-    if (fRunReportFile.is_open())
-        Message("Run Report...........OPEN");
-    else
-        Message("Run Report.........CLOSED");
+    for (list<RunNumberType>::iterator it=fRunNumber.begin(); it!=fRunNumber.end(); it++)
+    {
+        if (it->logFile->is_open())
+            Message("Run Log " + it->logName + " is OPEN");
+        else
+            Message("Run Log " + it->logName + " is CLOSED");
+        if (it->reportFile->is_open())
+            Message("Run Report " + it->reportName + " is OPEN");
+        else
+            Message("Run Report " + it->reportName + " CLOSED");
+    }
     bool statWarning = false;
     DataLoggerStats statVar;
@@ -1365,6 +1693,6 @@
     if (evt.GetText() != NULL)
     {
-        fNightlyFileName = string(evt.GetText());
-        Message("New Nightly folder specified: " + fNightlyFileName);
+        fNightlyFilePath = string(evt.GetText());
+        Message("New Nightly folder specified: " + fNightlyFilePath);
     }
     else
@@ -1384,6 +1712,6 @@
     if (evt.GetText() != NULL)
     {
-        fRunFileName = string(evt.GetText());
-        Message("New Run folder specified: " + fRunFileName);
+        fRunFilePath = string(evt.GetText());
+        Message("New Run folder specified: " + fRunFilePath);
     }
     else
@@ -1401,7 +1729,8 @@
 int DataLogger::ConfigureRunNumber(const Event& evt)
 {
-    fRunNumber = evt.GetInt();
+    AddNewRunNumber(evt.GetInt(), evt.GetTime());
+//    fRunNumber = evt.GetInt();
     ostringstream str;
-    str << "The new run number is: " << fRunNumber;
+    str << "The new run number is: " << fRunNumber.back().runNumber;
     Message(str.str());
     return GetCurrentState();
@@ -1452,5 +1781,5 @@
     sTime << time.Y() << "_" << time.M() << "_" << time.D();
 
-    fFullNightlyLogFileName = fNightlyFileName + '/' + sTime.str() + ".log"; 
+    fFullNightlyLogFileName = fNightlyFilePath + '/' + sTime.str() + ".log";
     fNightlyLogFile.open(fFullNightlyLogFileName.c_str(), ios_base::out | ios_base::app);
     if (errno != 0)
@@ -1460,5 +1789,5 @@
         Error(str);    
     }
-    fFullNightlyReportFileName = fNightlyFileName + '/' + sTime.str() + ".rep";
+    fFullNightlyReportFileName = fNightlyFilePath + '/' + sTime.str() + ".rep";
     fNightlyReportFile.open(fFullNightlyReportFileName.c_str(), ios_base::out | ios_base::app);
     if (errno != 0)
@@ -1487,5 +1816,5 @@
     //notify that files were opened
     string actualTargetDir;
-    if (fNightlyFileName == ".")
+    if (fNightlyFilePath == ".")
     {
         char currentPath[FILENAME_MAX];
@@ -1503,5 +1832,5 @@
     else
     {
-        actualTargetDir = fNightlyFileName;    
+        actualTargetDir = fNightlyFilePath;
     }
     //notify that a new file has been opened.
@@ -1519,8 +1848,17 @@
 //! @param sub
 //!     the current DimInfo subscription being examined
-void DataLogger::OpenFITSFilesPlease(SubscriptionType& sub)
+void DataLogger::OpenFITSFilesPlease(SubscriptionType& sub, RunNumberType* cRunNumber)
 {
     string serviceName(sub.dimInfo->getName());
-    //we must check if we should group this service subscription to a single fits file before we replace the / by _
+    //if run number has changed, reopen a new fits file with the correct run number.
+     if (sub.runFile.IsOpen() && sub.runFile.fRunNumber != sub.runNumber)
+     {
+         if (fDebugIsOn)
+         {
+             Debug("Run number changed. Closing " + sub.runFile.fFileName);
+         }
+         sub.runFile.Close();
+     }
+      //we must check if we should group this service subscription to a single fits file before we replace the / by _
     bool hasGrouping = false;
     if (!sub.runFile.IsOpen() && (GetCurrentState() == kSM_Logging))
@@ -1535,4 +1873,5 @@
         }
     }
+    hasGrouping = true;
     for (unsigned int i=0;i<serviceName.size(); i++)
     {
@@ -1550,5 +1889,5 @@
     {
         string fileNameOnly = sTime.str() + '_' + serviceName + ".fits";
-        string partialName = fNightlyFileName + '/' + fileNameOnly;
+        string partialName = fNightlyFilePath + '/' + fileNameOnly;
         AllocateFITSBuffers(sub);
         //get the size of the file we're about to open
@@ -1562,9 +1901,9 @@
             fOpenedNightlyFits[fileNameOnly].push_back(serviceName);
         }
-        sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this);//Out());
+        sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, -1);//Out());
 
         //notify the opening
         string actualTargetDir;
-        if (fNightlyFileName == ".")
+        if (fNightlyFilePath == ".")
         {
             char currentPath[FILENAME_MAX];
@@ -1574,5 +1913,5 @@
         else
         {
-            actualTargetDir = fNightlyFileName;    
+            actualTargetDir = fNightlyFilePath;
         }        
         NotifyOpenedFile(actualTargetDir + '/' + sTime.str(), 7, fOpenedNightlyFiles);
@@ -1586,8 +1925,9 @@
         }
     }
-    if (!sub.runFile.IsOpen() && (GetCurrentState() == kSM_Logging))
+    //to 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 << fRunNumber;
+        sRun << sub.runNumber;
         string fileNameOnly;
         string partialName;
@@ -1596,10 +1936,10 @@
         {
             fileNameOnly = sRun.str() + "_group.fits";
-            partialName = fRunFileName + '/' + fileNameOnly;
+            partialName = fRunFilePath + '/' + fileNameOnly;
         }
         else
         {
             fileNameOnly = sRun.str() + '_' + serviceName + ".fits";
-            partialName = fRunFileName + '/' + fileNameOnly;
+            partialName = fRunFilePath + '/' + fileNameOnly;
         }
         //get the size of the file we're about to open
@@ -1612,5 +1952,5 @@
                 fBaseSizeRun = 0;
             fFileSizesMap[partialName] = 0;
-            fOpenedRunFits[fileNameOnly].push_back(serviceName);
+            cRunNumber->openedFits[fileNameOnly].push_back(serviceName);
         }
         else
@@ -1620,5 +1960,5 @@
              //and reopened again. Unlikely to happen, but well it may
                 bool found = false;
-                for (vector<string>::iterator it=fOpenedRunFits[fileNameOnly].begin(); it!=fOpenedRunFits[fileNameOnly].end(); it++)
+                for (vector<string>::iterator it=cRunNumber->openedFits[fileNameOnly].begin(); it!=cRunNumber->openedFits[fileNameOnly].end(); it++)
                     if (*it == serviceName)
                     {
@@ -1627,10 +1967,11 @@
                     }
                 if (!found)
-                    fOpenedRunFits[fileNameOnly].push_back(serviceName);
-            }
-        if (hasGrouping && fRunFitsFile == NULL)
+                    cRunNumber->openedFits[fileNameOnly].push_back(serviceName);
+            }
+
+        if (hasGrouping && cRunNumber->runFitsFile == NULL)
             try
             {
-                fRunFitsFile = new CCfits::FITS(partialName, CCfits::RWmode::Write);
+                cRunNumber->runFitsFile = new CCfits::FITS(partialName, CCfits::RWmode::Write);
                 (fNumSubAndFitsData.numOpenFits)++;
             }    
@@ -1640,9 +1981,9 @@
                 str << "Could not open FITS Run file " << partialName << " reason: " << e.message();
                 Error(str);
-                fRunFitsFile = NULL;
+                cRunNumber->runFitsFile = NULL;
             }
 
         string actualTargetDir;
-        if (fRunFileName == ".")
+        if (fRunFilePath == ".")
         {
             char currentPath[FILENAME_MAX];
@@ -1652,11 +1993,11 @@
         else
         {
-           actualTargetDir = fRunFileName;
+           actualTargetDir = fRunFilePath;
         }
         NotifyOpenedFile(actualTargetDir + '/' + sRun.str(), 7, fOpenedRunFiles);// + '_' + serviceName, 4);
         if (hasGrouping)
-            sub.runFile.Open(partialName, serviceName, fRunFitsFile, &fNumSubAndFitsData.numOpenFits, this);//Out());
+            sub.runFile.Open(partialName, serviceName, cRunNumber->runFitsFile, &fNumSubAndFitsData.numOpenFits, this, sub.runNumber);//Out());
         else
-            sub.runFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this);//Out());
+            sub.runFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this, sub.runNumber);//Out());
 
        if (fNumSubAndFitsIsOn)
@@ -1671,4 +2012,7 @@
 }    
 // --------------------------------------------------------------------------
+//
+//! Allocates the required memory for a given pair of fits files (nightly and run)
+//! @param sub the subscription of interest.
 //
 void DataLogger::AllocateFITSBuffers(SubscriptionType& sub)
@@ -1785,89 +2129,20 @@
         Debug("Starting Run Logging...");    
     }
-    //attempt to open run file with current parameters
-//    if (fRunNumber == -1)
-//        return kSM_BadRunConfig;
-    ostringstream sRun;
-    sRun << fRunNumber;
-    fFullRunLogFileName = fRunFileName + '/' + sRun.str() + ".log";
-    fRunLogFile.open(fFullRunLogFileName.c_str(), ios_base::out | ios_base::app); //maybe should be app instead of ate
-    if (errno != 0)
-    {
-        ostringstream str;
-        str << "Unable to open run Log " << fFullRunLogFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-        Error(str);    
-    }
-    fFullRunReportFileName = fRunFileName + '/' + sRun.str() + ".rep";
-    fRunReportFile.open(fFullRunReportFileName.c_str(), ios_base::out | ios_base::app);
-    if (errno != 0)
-    {
-        ostringstream str;
-        str << "Unable to open run report " << fFullRunReportFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-        Error(str);    
-    }
-    
-    if (!fRunLogFile.is_open() || !fRunReportFile.is_open())
-    {
-        ostringstream str;
-        str << "Something went wrong while openning nightly files " << fFullRunLogFileName << " and " << fFullRunReportFileName;
-        Error(str.str());
-        return kSM_BadRunConfig;
-    }
-    //get the size of the newly opened file.
-    struct stat st;
-    fBaseSizeRun = 0;
-    if (fFileSizesMap.find(fFullRunLogFileName) == fFileSizesMap.end())
-    {
-        stat(fFullRunLogFileName.c_str(), &st);
-        if (errno != 0)
-        {
-            ostringstream str;
-            str << "Unable to stat " << fFullRunLogFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-            Error(str);    
-        }
-        else
-            fBaseSizeRun += st.st_size;
-        fFileSizesMap[fFullRunLogFileName] = 0;
-    }
-    if (fFileSizesMap.find(fFullRunReportFileName) == fFileSizesMap.end())
-    {
-        stat(fFullRunReportFileName.c_str(), &st);
-        if (errno != 0)
-        {
-            ostringstream str;
-            str << "Unable to stat " << fFullRunReportFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-            Error(str);    
-        }
-        else
-            fBaseSizeRun += st.st_size;
-        fFileSizesMap[fFullRunReportFileName] = 0;
-    }
-    string actualTargetDir;
-    if (fRunFileName == ".")
-    {
-        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 = fRunFileName;    
-    }        
-    NotifyOpenedFile(actualTargetDir + '/' + sRun.str(), 3, fOpenedRunFiles);
-    
-    fOpenedRunFits.clear();
+    //open all the relevant run-files. i.e. all the files associated with run numbers.
+    for (list<RunNumberType>::iterator it=fRunNumber.begin(); it != fRunNumber.end(); it++)
+        OpenRunFile(*it);
 
     return kSM_Logging;
 }
+
 #ifdef HAVE_FITS
-void DataLogger::CreateFitsGrouping(bool runGroup)
+// --------------------------------------------------------------------------
+//
+//! Create a fits group file with all the run-fits that were written (either daily or run)
+//! @param filesToGroup a map of filenames mapping to table names to be grouped (i.e. a
+//!        single file can contain several tables to group
+//! @param runNumber the run number that should be used for grouping. -1 means nightly group
+//
+void DataLogger::CreateFitsGrouping(map<string, vector<string> > & filesToGroup, int runNumber)
 {
     if (fDebugIsOn)
@@ -1875,5 +2150,5 @@
         ostringstream str;
         str << "Creating fits group for ";
-        if (runGroup)
+        if (runNumber != -1)
             str << "run files";
         else
@@ -1883,5 +2158,4 @@
     //create the FITS group corresponding to the ending run.
     CCfits::FITS* groupFile;
-    map<string, vector<string> >& filesToGroup = runGroup? fOpenedRunFits : fOpenedNightlyFits;
     unsigned int numFilesToGroup = 0;
     for (map<string, vector<string> >::iterator it=filesToGroup.begin(); it != filesToGroup.end(); it++)
@@ -1901,7 +2175,7 @@
     }
     ostringstream groupName;
-    if (runGroup)
-    {
-        groupName << fRunFileName << '/' << fRunNumber << ".fits";
+    if (runNumber != -1)
+    {
+        groupName << fRunFilePath << '/' << runNumber << ".fits";
     }
     else
@@ -1911,5 +2185,5 @@
         sTime << time.Y() << "_" << time.M() << "_" << time.D();
 
-        groupName << fNightlyFileName << '/' << sTime.str() << ".fits";
+        groupName << fNightlyFilePath << '/' << sTime.str() << ".fits";
     }
     CCfits::Table* groupTable;
@@ -1933,4 +2207,5 @@
 
         groupTable = groupFile->addTable("GROUPING", numFilesToGroup, names, dataTypes);
+//TODO handle the case when the logger was stopped and restarted during the same day, i.e. the grouping file must be updated
      }
      catch (CCfits::FitsException e)
@@ -1952,5 +2227,5 @@
     char* startOfLocation = reinterpret_cast<char*>(&fitsBuffer[8 + 3]);
     char* startOfName = reinterpret_cast<char*>(&fitsBuffer[8+3+maxCharLength]);
- //   char* startOfNameVisible = reinterpret_cast<char*>(&fitsBuffer[8 + 3 + 2*maxCharLength]);
+
     sprintf(startOfExtension, "%s", "BINTABLE");
     sprintf(startOfURI, "%s", "URL");
@@ -1967,5 +2242,4 @@
                 Debug(str.str());
             }
- //           strcpy(startOfNameVisible, jt->c_str());
             int status = 0;
             fits_write_tblbytes(groupFile->fitsPointer(), i, 1, 8+3+2*maxCharLength, fitsBuffer, &status);
@@ -1982,4 +2256,5 @@
 }
 #endif //HAVE_FITS
+
 // --------------------------------------------------------------------------
 //
@@ -1995,10 +2270,12 @@
         Debug("Stopping Run Logging...");    
     }
-    if (!fRunLogFile.is_open() || !fRunReportFile.is_open())
-        return kSM_FatalError;
-    if (fRunLogFile.is_open())
-        fRunLogFile.close();
-    if (fRunReportFile.is_open())
-        fRunReportFile.close();
+    for (list<RunNumberType>::iterator it=fRunNumber.begin(); it != fRunNumber.end(); it++)
+    {
+        if (!it->logFile->is_open() || !it->reportFile->is_open())
+            return kSM_FatalError;
+        it->logFile->close();
+        it->reportFile->close();
+    }
+
 #ifdef HAVE_FITS
     for (SubscriptionsListType::iterator i = fServiceSubscriptions.begin(); i != fServiceSubscriptions.end(); i++)
@@ -2008,10 +2285,4 @@
                 j->second.runFile.Close();
         }
-    if (fRunFitsFile != NULL)
-    {
-        delete fRunFitsFile;
-        fRunFitsFile = NULL;    
-        (fNumSubAndFitsData.numOpenFits)--;
-    }
 #endif
     NotifyOpenedFile("", 0, fOpenedRunFiles);
@@ -2019,8 +2290,10 @@
         fNumSubAndFits->updateService();
 
-    CreateFitsGrouping(true);
+    while (fRunNumber.size() > 0)
+    {
+        RemoveOldestRunNumber();
+    }
 
     return kSM_WaitingRun;
-
 }
 // --------------------------------------------------------------------------
@@ -2032,17 +2305,15 @@
 int DataLogger::GoToReadyPlease()
 {
-    if (fDebugIsOn)
+   if (fDebugIsOn)
     {
         Debug("Going to the Ready state...");
     }    
+   if (GetCurrentState() == kSM_Logging)
+       StopRunPlease();
+
     if (fNightlyLogFile.is_open())
         fNightlyLogFile.close();
     if (fNightlyReportFile.is_open())
         fNightlyReportFile.close();
-
-    if (fRunLogFile.is_open())
-        fRunLogFile.close();
-    if (fRunReportFile.is_open())
-        fRunReportFile.close();
         
 #ifdef HAVE_FITS
@@ -2051,17 +2322,7 @@
         {
             if (j->second.nightlyFile.IsOpen())
-                j->second.nightlyFile.Close();
-            if (j->second.runFile.IsOpen())
-                j->second.runFile.Close();
-        }
-    if (fRunFitsFile != NULL)
-    {
-        delete fRunFitsFile;
-        fRunFitsFile = NULL;
-        (fNumSubAndFitsData.numOpenFits)--;
-    }
+                j->second.nightlyFile.Close();;
+        }
 #endif
-    if (GetCurrentState() == kSM_Logging)
-        NotifyOpenedFile("", 0, fOpenedRunFiles);
     if (GetCurrentState() == kSM_Logging || 
         GetCurrentState() == kSM_WaitingRun || 
@@ -2072,7 +2333,7 @@
             fNumSubAndFits->updateService();
     }
-    CreateFitsGrouping(true);
-    CreateFitsGrouping(false);
-
+#ifdef HAVE_FITS
+    CreateFitsGrouping(fOpenedNightlyFits, -1);
+#endif
     return kSM_Ready;
 }
@@ -2091,5 +2352,9 @@
     return kSM_WaitingRun;    
 }
-
+// --------------------------------------------------------------------------
+//
+//! Setup Logger's configuration from a Configuration object
+//! @param conf the configuration object that should be used
+//!
 bool DataLogger::SetConfiguration(Configuration& conf)
 {
@@ -2149,5 +2414,4 @@
 
 // --------------------------------------------------------------------------
-
 int RunDim(Configuration &conf)
 {
@@ -2169,5 +2433,5 @@
     return 0;
 }
-
+// --------------------------------------------------------------------------
 void RunThread(DataLogger* logger)
 {
@@ -2177,5 +2441,5 @@
     Readline::Stop();    
 }
-
+// --------------------------------------------------------------------------
 template<class T>
 int RunShell(Configuration &conf)
@@ -2235,5 +2499,5 @@
 
 }
-
+// --------------------------------------------------------------------------
 void PrintHelp()
 {
@@ -2268,5 +2532,5 @@
     cout << endl;
 }
-
+// --------------------------------------------------------------------------
 void SetupConfiguration(Configuration &conf)
 {
@@ -2293,5 +2557,5 @@
     conf.AddOptions(configs);
 }
-
+// --------------------------------------------------------------------------
 int main(int argc, const char* argv[])
 {
