Index: /trunk/FACT++/src/Fits.cc
===================================================================
--- /trunk/FACT++/src/Fits.cc	(revision 10740)
+++ /trunk/FACT++/src/Fits.cc	(revision 10741)
@@ -35,8 +35,8 @@
 //! @param numDataBytes the number of bytes taken by the variable
 //
-void Fits::AddStandardColumn(Description& desc, std::string dataFormat, void* dataPointer, long unsigned int numDataBytes)
+void Fits::AddStandardColumn(Description& desc, string dataFormat, void* dataPointer, long unsigned int numDataBytes)
 {
 	//check if entry already exist
-	for (std::vector<Description>::iterator it=fStandardColDesc.begin(); it != fStandardColDesc.end(); it++)
+	for (vector<Description>::iterator it=fStandardColDesc.begin(); it != fStandardColDesc.end(); it++)
 		if (it->name == desc.name)
 			return;
@@ -54,5 +54,5 @@
 //! @param numDataBytes the number of bytes taken by the DIM data. 
 //	
-void Fits::InitDataColumns(std::vector<Description> desc, std::vector<std::string>& dataFormat, void* dataPointer, int numDataBytes)
+void Fits::InitDataColumns(vector<Description> desc, vector<string>& dataFormat, void* dataPointer, int numDataBytes)
 {//we will copy this information here. It duplicates the data, which is not great, but it is the easiest way of doing it right now
 	if (desc.size() == dataFormat.size())
@@ -65,5 +65,5 @@
 		for (unsigned int i=0;i<dataFormat.size();i++)
 		{
-			std::stringstream stt;
+			stringstream stt;
 			stt << "Data" << i;
 			fDataColDesc.push_back(Description(stt.str(), "comment", "unit"));
@@ -83,5 +83,5 @@
 //! @param out a pointer to the MessageImp that should be used to log errors
 //
-void Fits::Open(const std::string& fileName, const std::string& tableName, FITS* file, int* fitsCounter, MessageImp* out)//std::ostream& out)
+void Fits::Open(const string& fileName, const string& tableName, FITS* file, int* fitsCounter, MessageImp* out)//ostream& out)
 {		
 //	if (fMess)
@@ -98,5 +98,5 @@
 		catch (CCfits::FitsError e)
 		{			
-			std::stringstream str;
+			stringstream str;
 			str << "Could not open FITS file " << fileName << " reason: " << e.message();
 			fMess->Error(str);
@@ -115,7 +115,7 @@
 	//concatenate the standard and data columns
 	//do it the inneficient way first: its easier and faster to code.
-	std::vector<std::string> allNames;
-	std::vector<std::string> allDataTypes;
-	std::vector<std::string> allUnits;
+	vector<string> allNames;
+	vector<string> allDataTypes;
+	vector<string> allUnits;
 	fTotalNumBytes = 0;
 	for (unsigned int i=0;i<fStandardColDesc.size();i++)
@@ -133,5 +133,5 @@
 		else 
 		{
-			std::stringstream stt;
+			stringstream stt;
 			stt << "Data" << i;
 			allNames.push_back(stt.str());
@@ -145,5 +145,5 @@
 	try
 	{
-		std::string factTableName = "FACT-" + tableName;
+		string factTableName = "FACT-" + tableName;
 		fTable = fFile->addTable(factTableName, 0, allNames, allDataTypes, allUnits);
 		fTable->makeThisCurrent();
@@ -159,10 +159,10 @@
 			}	
 			//read the table binary data.
-			std::vector<string> colName;
+			vector<string> colName;
 			bTable->readData(true, colName);
 
 			//double check that the data was indeed read from the disk. Go through the fTable instead as colName is empty (yes, it is !)
-			std::map<std::string, Column*> cMap = fTable->column();
-			std::map<std::string, Column*>::iterator cMapIt;
+			map<string, Column*> cMap = fTable->column();
+			map<string, Column*>::iterator cMapIt;
 
 			for (cMapIt = cMap.begin(); cMapIt != cMap.end(); cMapIt++)
@@ -179,5 +179,5 @@
 	catch(CCfits::FitsError e)
 	{
-		std::stringstream str;
+		stringstream str;
 		str << "Could not open or create FITS table " << tableName << " in  file " << fileName << " reason: " << e.message();
 		fMess->Error(str);
@@ -190,4 +190,10 @@
 		WriteHeaderKeys();
 }
+// --------------------------------------------------------------------------
+//
+//!This writes a single header key in the currently open file.
+//!@param name the key
+//!@param value the key value
+//!@param a comment explaining the meaning of the key
 template <typename T>
 void Fits::WriteSingleHeaderKey(string name, T value, string comment)
@@ -199,5 +205,5 @@
 	catch (CCfits::FitsError e)
 	{
-		std::stringstream str;
+		stringstream str;
 		str << "Could not add header keys in file " << fFileName << " reason: " << e.message();
 		fMess->Error(str);
@@ -212,10 +218,8 @@
 	if (!fTable)
 		return;
-	std::string name;
-	std::string comment;
-	
-//	float floatValue;
-//	double doubleValue;
-	std::string stringValue;
+	string name;
+	string comment;
+
+	string stringValue;
 
 	WriteSingleHeaderKey("EXTREL", 1.0f, "Release Number");
@@ -230,4 +234,5 @@
 	WriteSingleHeaderKey("TIMEREF", "UTC", "Time reference frame");
 	WriteSingleHeaderKey("MJDREF", fRefMjD, "Modified Julian Date of origin");
+    WriteSingleHeaderKey("TSTOP", fEndMjD, "Time of the last receied data");
 }
 // --------------------------------------------------------------------------
@@ -239,22 +244,12 @@
 {
 
-//	try
-//	{
 	fTable->makeThisCurrent();
 	int status(0);
 	if (fits_insert_rows(fTable->fitsPointer(), fNumRows, 1, &status))
 	{
-		std::stringstream str;
+		stringstream str;
 		str << "Could not insert row in file " << fFileName << ". cfitsio error code: " << status;
 		fMess->Error(str);
 	}
-//		fTable->insertRows(fNumRows);
-//	}
-//	catch(CCfits::FitsError e)
-//	{
-//		std::stringstream str;
-//		str << "Could not insert row in file " << fFileName << " reason: " << e.message();
-//		fMess->Error(str);
-//	}
 	fNumRows++;
 
@@ -273,8 +268,5 @@
 	{
 		const char * charSrc = static_cast<char*>(fStandardPointers[i]);
-
 		reverse_copy(charSrc, charSrc+fStandardNumBytes[i], &fCopyBuffer[shift]);
-//		for (int j=0; j<fStandardNumBytes[i]; j++)
-//			fCopyBuffer[shift+j] = static_cast<char*>(fStandardPointers[i])[fStandardNumBytes[i]-(j+1)];
 		shift+= fStandardNumBytes[i];	
 	}
@@ -284,6 +276,4 @@
 			 
 	//data copied to buffer, can write to fits
-//	int status = 0;
-	//TODO check the status after the write operation
    	fits_write_tblbytes(fFile->fitsPointer(), fNumRows, 1, fTotalNumBytes, fCopyBuffer, &status);
 	if (status)
@@ -291,21 +281,9 @@
 		char text[30];//max length of cfitsio error strings (from doc)
 		fits_get_errstatus(status, text);
-		std::stringstream str;
+		stringstream str;
 		str << "Error while writing FITS row in " << fFileName << ". Message: " << text << " [" << status << "]";
 		fMess->Error(str);	
 	}
-	//This forces the writting of each row to the disk. Otherwise all rows are written when the file is closed.
-	///TODO check whether this consumes too much resources or not. If it does, flush every N write operations instead
-/*	try
-	{
-		fFile->flush();
-	}
-	catch (CCfits::FitsError e)
-	{
-		std::stringstream str;
-		str << "Error while flushing bytes to disk. File: " << fFileName << " reason: " << e.message();
-		fMess->Error(str);	
-	}
-*/}
+}
 // --------------------------------------------------------------------------
 //
@@ -335,5 +313,5 @@
 
 // --------------------------------------------------------------------------
-//
+//! Returns the size on the disk of the Fits file being written.
 int Fits::GetWrittenSize()
 {
Index: /trunk/FACT++/src/Fits.h
===================================================================
--- /trunk/FACT++/src/Fits.h	(revision 10740)
+++ /trunk/FACT++/src/Fits.h	(revision 10741)
@@ -10,9 +10,11 @@
 class MessageImp;
 
+using namespace std;
+
 class Fits
 {
 	private:
 		///The CCfits object to the FITS file
-                CCfits::FITS* fFile;
+        CCfits::FITS* fFile;
 		///Flag indicating whether the FITS object should be managed internally or not.
 		bool fOwner;
@@ -24,15 +26,15 @@
 		///TODO make these variable static so that they are shared by every object.
 		///TODO add also a static boolean to initialize these only once
-		std::vector<Description> fStandardColDesc;
+		vector<Description> fStandardColDesc;
 		///Format of the standard columns.
-		std::vector<std::string> fStandardFormats;
+		vector<string> fStandardFormats;
 		///the pointers to the standard variables
-		std::vector<void*> fStandardPointers;
+		vector<void*> fStandardPointers;
 		///the number of bytes taken by each standard variable
-		std::vector<int> fStandardNumBytes;
+		vector<int> fStandardNumBytes;
 		///the vector of data column names
-		std::vector<Description> fDataColDesc;
+		vector<Description> fDataColDesc;
 		///the data format of the data columns
-		std::vector<std::string> fDataFormats;
+		vector<string> fDataFormats;
 		///the pointer to the contiguous memory location where the data is stored (i.e. the dim data pointer)
 		void* fDataPointer;
@@ -51,5 +53,5 @@
 public:
 		///Name of the openned file. For querying stats
-		std::string fFileName;
+		string fFileName;
 private:
 		///Keep track of number of opened fits
@@ -83,11 +85,11 @@
 		
 		///Adds a column that exists in all FITS files
-		void AddStandardColumn(Description& desc, std::string dataFormat, void* dataPointer, long unsigned int numDataBytes);
+		void AddStandardColumn(Description& desc, string dataFormat, void* dataPointer, long unsigned int numDataBytes);
 		
 		///Adds columns specific to the service being logged.
-		void InitDataColumns(std::vector<Description> desc, std::vector<std::string>& dataFormat, void* dataPointer, int numDataBytes);
+		void InitDataColumns(vector<Description> desc, vector<string>& dataFormat, void* dataPointer, int numDataBytes);
 
 		///Opens a FITS file
-        void Open(const std::string& fileName, const std::string& tableName, CCfits::FITS* file, int* fitsCounter, MessageImp* out);//std::ostream& out);
+        void Open(const string& fileName, const string& tableName, CCfits::FITS* file, int* fitsCounter, MessageImp* out);//ostream& out);
 
 		///Write one line of data. Use the given converter.
Index: /trunk/FACT++/src/dataLogger.cc
===================================================================
--- /trunk/FACT++/src/dataLogger.cc	(revision 10740)
+++ /trunk/FACT++/src/dataLogger.cc	(revision 10741)
@@ -11,10 +11,10 @@
   \dot
   digraph datalogger {
-  		node [shape=record, fontname=Helvetica, fontsize=10];
-  	e [label="Error" color="red"];
+          node [shape=record, fontname=Helvetica, fontsize=10];
+      e [label="Error" color="red"];
    r [label="Ready"]
    d [label="NightlyOpen"]
    w [label="WaitingRun"]
-  	l [label="Logging"]
+      l [label="Logging"]
    b [label="BadNightlyconfig" color="red"]
    c [label="BadRunConfig" color="red"]
@@ -37,7 +37,4 @@
   }
   \enddot
-  
-  @todo
-  	- Retrieve also the messages, not only the infos
  */
  //****************************************************************
@@ -82,349 +79,334 @@
 //Dim structures
 struct DataLoggerStats {
-	long sizeWritten;
-	long freeSpace;
-	long writingRate;
+    long sizeWritten;
+    long freeSpace;
+    long writingRate;
 };
 
 struct NumSubAndFitsType {
-	int numSubscriptions;
-	int numOpenFits;
+    int numSubscriptions;
+    int numOpenFits;
 };
 
 struct OpenFileToDim {
-	int code;
-	char fileName[FILENAME_MAX];
+    int code;
+    char fileName[FILENAME_MAX];
 };
-//For debugging DIM's services
-class MyService
+
+class DataLogger : public StateMachineDim, DimInfoHandler
 {
 public:
-	MyService(){};
-	MyService(std::string, std::string, void*, int){};
-	MyService(std::string, const char*){};
-	void updateService(){};
-	void updateService(void*, int){};
-	void setQuality(int){};
-};
-class DataLogger : public StateMachineDim, DimInfoHandler
-{
+    /// The list of existing states specific to the DataLogger
+    enum
+    {
+        kSM_NightlyOpen = 20, ///< Nightly file openned and writing
+        kSM_WaitingRun = 30, ///< waiting for the run number to open the run file
+        kSM_Logging = 40, ///< both files openned and writing
+        kSM_BadNightlyConfig = 0x101, ///< the folder specified for Nightly logging does not exist or has bad permissions
+        kSM_BadRunConfig = 0x102, ///<  the folder specified for the run logging does not exist or has wrong permissions or no run number
+    } localstates_t;
+    
+    DataLogger(ostream &out);
+    ~DataLogger(); 
+    
+private:
+    //Define all the data structure specific to the DataLogger here
+     /// 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;
+    ///base path of the run file
+    string fRunFileName;
+    ///run number (-1 means no run number specified)
+    int fRunNumber; 
+    ///previous run number. to check if changed while logging
+    int fPreviousRunNumber;
+    ///Current Service Quality
+    int fQuality;
+    ///Modified Julian Date
+    double fMjD;
 public:
-	/// The list of existing states specific to the DataLogger
-	enum
-	{
-		kSM_NightlyOpen = 20, ///< Nightly file openned and writing
-		kSM_WaitingRun = 30, ///< waiting for the run number to open the run file
-		kSM_Logging = 40, ///< both files openned and writing
-		kSM_BadNightlyConfig = 0x101, ///< the folder specified for Nightly logging does not exist or has bad permissions
-		kSM_BadRunConfig = 0x102, ///<  the folder specified for the run logging does not exist or has wrong permissions or no run number
-	} localstates_t;
-	
-    DataLogger(std::ostream &out);
-	~DataLogger(); 
-	
+    ///Define all the static names
+    static const char* fConfigDay;
+    static const char* fConfigRun;
+    static const char* fConfigRunNumber;
+    static const char* fConfigLog;
+    static const char* fTransStart;
+    static const char* fTransStop;
+    static const char* fTransStartRun;
+    static const char* fTransStopRun;
+    static const char* fTransReset;
+    static const char* fTransWait;
+    static const char* fRunNumberInfo; ///< This is the name of the dimInfo received to specify the run number. It must be updated once the final name will be defined
+    static const char* fPrintCommand;
+    static const char* fDebugOnOff;
+    static const char* fStatsPeriod;
+    static const char* fStartStopOpenedFiles;
+    static const char* fStartStopNumSubsAndFits;
 private:
-	//Define all the data structure specific to the DataLogger here
- 	/// ofstream for the NightlyLogfile
-	std::ofstream fNightlyLogFile;
-	/// ofstream for the run-specific Log file
-	std::ofstream fRunLogFile; 
-
-	/// ofstream for the Nightly report file
-	std::ofstream fNightlyReportFile;
-	/// ofstream for the run-specific report file
-	std::ofstream fRunReportFile;
-	/// base path of the Nightlyfile
-	std::string fNightlyFileName; 
-	///base path of the run file
-	std::string fRunFileName; 
-	///run number (-1 means no run number specified)
-	int fRunNumber; 
-	///previous run number. to check if changed while logging
-	int fPreviousRunNumber;
-	///Current Service Quality
-	int fQuality;
-	///Modified Julian Date
-	double fMjD;
-public:
-	///Define all the static names
-	static const char* fConfigDay;
-	static const char* fConfigRun;
-	static const char* fConfigRunNumber;
-	static const char* fConfigLog;
-	static const char* fTransStart;
-	static const char* fTransStop;
-	static const char* fTransStartRun;
-	static const char* fTransStopRun;
-	static const char* fTransReset;
-	static const char* fTransWait;
-	static const char* fRunNumberInfo; ///< This is the name of the dimInfo received to specify the run number. It must be updated once the final name will be defined
-	static const char* fPrintCommand;
-	static const char* fDebugOnOff;
-	static const char* fStatsPeriod;
-	static const char* fStartStopOpenedFiles;
-	static const char* fStartStopNumSubsAndFits;
+    //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;
+
+    ///Reporting method for the services info received
+    void ReportPlease(DimInfo* I, SubscriptionType& sub);  
+
+    ///Configuration of the nightly file path
+    int ConfigureNightlyFileName(const Event& evt); 
+    ///Configuration fo the file name
+    int ConfigureRunFileName(const Event& evt); 
+    ///DEPREC - configuration of the run number
+    int ConfigureRunNumber(const Event& evt); 
+    ///logging method for the messages
+    int LogMessagePlease(const Event& evt); 
+    ///print the current state of the dataLogger
+    int PrintStatePlease(const Event& evt);
+    ///checks whether or not the current info being treated is a run number
+    void CheckForRunNumber(DimInfo* I); 
+    /// start transition
+    int StartPlease(); 
+    ///from waiting to logging transition
+    int StartRunPlease(); 
+    /// from logging to waiting transition
+    int StopRunPlease(); 
+    ///stop and reset transition
+    int GoToReadyPlease(); 
+    ///from NightlyOpen to waiting transition
+    int NightlyToWaitRunPlease(); 
+#ifdef HAVE_FITS
+    ///Open fits files
+    void OpenFITSFilesPlease(SubscriptionType& sub);
+    ///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:    
+    ///monitoring notification loop
+    void ServicesMonitoring();
+    ///services notification thread
+    boost::thread fMonitoringThread;
+    ///end of the monitoring
+    bool fContinueMonitoring;
+    ///required for accurate monitoring
+    map<string, long> fFileSizesMap;
+    string fFullNightlyLogFileName;
+    string fFullNightlyReportFileName;
+    string fFullRunLogFileName;
+    string fFullRunReportFileName;
+    long fBaseSizeNightly;
+    long fPreviousSize;
+    long fBaseSizeRun;
+    ///Service for opened files
+    DimDescribedService* fOpenedNightlyFiles;
+    DimDescribedService* fOpenedRunFiles;
+    DimDescribedService* fNumSubAndFits;
+    NumSubAndFitsType fNumSubAndFitsData;
+
+    inline void NotifyOpenedFile(string name, int type, DimDescribedService* service);
+
+public:    
+    bool SetConfiguration(Configuration& conf);
+
 private:
-	//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
-	{ 
+    set<string> fBlackList;
+    set<string> fWhiteList;
+    set<string> fGrouping;
+    bool fHasBlackList;
+    bool fHasWhiteList;
+    bool fDebugIsOn;
+    float fStatsPeriodDuration;
+    bool fOpenedFilesIsOn;
+    bool fNumSubAndFitsIsOn;
+    //functions for controlling the services behavior
+    int SetDebugOnOff(const Event& evt);
+    int SetStatsPeriod(const Event& evt);
+    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);
+
+    ///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);
+}; //DataLogger
+
+void DataLogger::calculateTotalSizeWritten(DataLoggerStats& statVar, bool& shouldWarn, bool isPrinting)
+{
 #ifdef HAVE_FITS
-		///Nightly FITS output file
-		Fits	nightlyFile;
-		///run-specific FITS output file
-		Fits	runFile;
+    if (isPrinting)
+    {
+        stringstream str;
+        str.str("");
+        str << "There are " << fNumSubAndFitsData.numOpenFits << " FITS files open:";
+        Message(str.str());
+    }
+    SubscriptionsListType::iterator x;
+    map<string, SubscriptionType>::iterator y;
+///TODO the grouping file is dealt with several times. This should not be a problem but well, better to fix it I guess.
+    for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
+    {
+        for (y=x->second.begin(); y != x->second.end(); y++)
+        {
+            if (y->second.runFile.IsOpen())
+            {
+                    fFileSizesMap[y->second.runFile.fFileName] = y->second.runFile.GetWrittenSize();
+                    if (isPrinting)
+                        Message("-> "+y->second.runFile.fFileName);
+            }
+            if (y->second.nightlyFile.IsOpen())
+            {
+                fFileSizesMap[y->second.nightlyFile.fFileName] = y->second.nightlyFile.GetWrittenSize();
+                if (isPrinting)
+                    Message("-> "+y->second.nightlyFile.fFileName);
+            }
+        }
+    }
+#else
+    if (isPrinting)
+        Message("FITS output disabled at compilation");
 #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 std::map<const std::string, std::map<std::string, SubscriptionType>> SubscriptionsListType;
-	///All the services to which we have subscribed to, sorted by server name.
-	SubscriptionsListType fServiceSubscriptions;
-
-	///Reporting method for the services info received
-	void ReportPlease(DimInfo* I, SubscriptionType& sub);  
-
-	///Configuration of the nightly file path
-	int ConfigureNightlyFileName(const Event& evt); 
-	///Configuration fo the file name
-	int ConfigureRunFileName(const Event& evt); 
-	///DEPREC - configuration of the run number
-	int ConfigureRunNumber(const Event& evt); 
-	///logging method for the messages
-	int LogMessagePlease(const Event& evt); 
-	///print the current state of the dataLogger
-	int PrintStatePlease(const Event& evt);
-	///checks whether or not the current info being treated is a run number
-	void CheckForRunNumber(DimInfo* I); 
-	/// start transition
-	int StartPlease(); 
-	///from waiting to logging transition
-	int StartRunPlease(); 
-	/// from logging to waiting transition
-	int StopRunPlease(); 
-	///stop and reset transition
-	int GoToReadyPlease(); 
-	///from NightlyOpen to waiting transition
-	int NightlyToWaitRunPlease(); 
-#ifdef HAVE_FITS
-	///Open fits files
-	void OpenFITSFilesPlease(SubscriptionType& sub);
-	///Write data to FITS files
-	void WriteToFITS(SubscriptionType& sub);
-	///Allocate the buffers required for fits
-	void AllocateFITSBuffers(SubscriptionType& sub);
-		
-#ifdef ONE_RUN_FITS_ONLY
-	///FITS file for runs. only one, hence dealt with in the dataLogger itself
-	CCfits::FITS* fRunFitsFile;
-#endif //one_run_fits_only
-#endif//has_fits
-public:	
-	///checks with fServiceList whether or not the services got updated
-	bool CheckForServicesUpdate(); 
-
-private:	
-	///monitoring notification loop
-	void ServicesMonitoring();
-	///services notification thread
-	boost::thread fMonitoringThread;
-	///end of the monitoring
-	bool fContinueMonitoring;
-	///required for accurate monitoring
-	std::map<std::string, long> fFileSizesMap;
-	std::string fFullNightlyLogFileName;
-	std::string fFullNightlyReportFileName;
-	std::string fFullRunLogFileName;
-	std::string fFullRunReportFileName;
-	long fBaseSizeNightly;
-	long fPreviousSize;
-	long fBaseSizeRun;
-	///Service for opened files
-	DimDescribedService* fOpenedNightlyFiles;
-	DimDescribedService* fOpenedRunFiles;
-	DimDescribedService* fNumSubAndFits;
-	NumSubAndFitsType fNumSubAndFitsData;
-
-	inline void NotifyOpenedFile(std::string name, int type, DimDescribedService* service);
-
-public:	
-	bool SetConfiguration(Configuration& conf);
-
-private:
-	std::set<std::string> fBlackList;
-	std::set<std::string> fWhiteList;
-	bool fHasBlackList;
-	bool fHasWhiteList;
-	bool fDebugIsOn;
-	float fStatsPeriodDuration;
-	bool fOpenedFilesIsOn;
-	bool fNumSubAndFitsIsOn;
-	//functions for controlling the services behavior
-	int SetDebugOnOff(const Event& evt);
-	int SetStatsPeriod(const Event& evt);
-	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);
-
-	///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, string> fOpenedRunFits;
-	map<string, string> fOpenedNightlyFits;
-	void CreateFitsGrouping(bool runGroup);
-}; //DataLogger
-
-void DataLogger::calculateTotalSizeWritten(DataLoggerStats& statVar, bool& shouldWarn, bool isPrinting)
-{
-#ifdef HAVE_FITS
-	if (isPrinting)
-	{
-		stringstream str;
-		str.str("");
-		str << "There are " << fNumSubAndFitsData.numOpenFits << " FITS files open:";
-		Message(str.str());
-	}
-	SubscriptionsListType::iterator x;
-	std::map<std::string, SubscriptionType>::iterator y;
-	bool runFileDone = false;
-	for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
-	{
-		for (y=x->second.begin(); y != x->second.end(); y++)
-		{
-			if (y->second.runFile.IsOpen() && !runFileDone)
-			{
-					fFileSizesMap[y->second.runFile.fFileName] = y->second.runFile.GetWrittenSize();
-					if (isPrinting)
-						Message("-> "+y->second.runFile.fFileName);
-#ifdef ONE_FITS_ONLY
-					runFileDone = true;
-#endif
-			}
-			if (y->second.nightlyFile.IsOpen())
-			{
-				fFileSizesMap[y->second.nightlyFile.fFileName] = y->second.nightlyFile.GetWrittenSize();
-				if (isPrinting)
-					Message("-> "+y->second.nightlyFile.fFileName);
-			}
-		}
-	}
-#else
-	if (isPrinting)
-		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;
-	}
-	if (fNightlyReportFile.is_open())
-	{
-		stat(fFullNightlyReportFileName.c_str(), &st);
-		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;
-	}
-	struct statvfs vfs;
-	if (!statvfs(fNightlyFileName.c_str(), &vfs))
-	{
-		statVar.freeSpace = vfs.f_bsize*vfs.f_bavail;
-		shouldWarn = false;
-	}
-	else
-	{
-		stringstream str;
-		str.str("");
-		str << "Unable to retrieve stats for " << fNightlyFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-		if (!shouldWarn)
-			Error(str);
-		shouldWarn = true;
-		statVar.freeSpace = -1;
-	}
-	//sum up all the file sizes. past and present
-	statVar.sizeWritten = 0;
-	for (std::map<std::string, long>::iterator it=fFileSizesMap.begin(); it != fFileSizesMap.end();  it++)
-		statVar.sizeWritten += it->second;
-	statVar.sizeWritten -= fBaseSizeNightly;
-	statVar.sizeWritten -= fBaseSizeRun;
+    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;
+    }
+    if (fNightlyReportFile.is_open())
+    {
+        stat(fFullNightlyReportFileName.c_str(), &st);
+        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;
+    }
+    struct statvfs vfs;
+    if (!statvfs(fNightlyFileName.c_str(), &vfs))
+    {
+        statVar.freeSpace = vfs.f_bsize*vfs.f_bavail;
+        shouldWarn = false;
+    }
+    else
+    {
+        stringstream str;
+        str.str("");
+        str << "Unable to retrieve stats for " << fNightlyFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
+        if (!shouldWarn)
+            Error(str);
+        shouldWarn = true;
+        statVar.freeSpace = -1;
+    }
+    //sum up all the file sizes. past and present
+    statVar.sizeWritten = 0;
+    for (map<string, long>::iterator it=fFileSizesMap.begin(); it != fFileSizesMap.end();  it++)
+        statVar.sizeWritten += it->second;
+    statVar.sizeWritten -= fBaseSizeNightly;
+    statVar.sizeWritten -= fBaseSizeRun;
 }
 //static members initialization
@@ -449,48 +431,48 @@
 void DataLogger::ServicesMonitoring()
 {
-		DataLoggerStats statVar;
-		statVar.sizeWritten = 0;
-		statVar.freeSpace = 0;
-		statVar.writingRate = 0;
-
-		struct statvfs vfs;
-		if (!statvfs(fNightlyFileName.c_str(), &vfs))
-			statVar.freeSpace = vfs.f_bsize*vfs.f_bavail;
-		else
-			statVar.freeSpace = -1;
-
-		DimDescribedService srvc ("DATA_LOGGER/STATS", "X:3", statVar, "Add description here");
-		fPreviousSize = 0;
-		bool statWarning = false;
-		//loop-wait for broadcast
-		while (fContinueMonitoring)
-		{
-			if (fStatsPeriodDuration == 0.0f)
-			{
-				sleep(0.1f);
-				continue;
-			}
-			else
-				sleep(fStatsPeriodDuration);
-			//update the fits files sizes
-			calculateTotalSizeWritten(statVar, statWarning, false);
-			if (fStatsPeriodDuration == 0.0f)
-				continue;
-			statVar.writingRate = (statVar.sizeWritten - fPreviousSize)/fStatsPeriodDuration;  
-
-			fPreviousSize = statVar.sizeWritten;
-			if (statVar.writingRate != 0) //if data has been written
-			{
-				srvc.updateService();
-				if(fDebugIsOn)
-				{
-					stringstream str;
-					str << "Size written: " << statVar.sizeWritten/1024 << " KB; writting rate: ";
-					str << statVar.writingRate/1024 << " KB/s; free space: ";
-					str << statVar.freeSpace/(1024*1024) << " MB";
-					Debug(str.str());
-				}
-			}
-		}
+        DataLoggerStats statVar;
+        statVar.sizeWritten = 0;
+        statVar.freeSpace = 0;
+        statVar.writingRate = 0;
+
+        struct statvfs vfs;
+        if (!statvfs(fNightlyFileName.c_str(), &vfs))
+            statVar.freeSpace = vfs.f_bsize*vfs.f_bavail;
+        else
+            statVar.freeSpace = -1;
+
+        DimDescribedService srvc ("DATA_LOGGER/STATS", "X:3", statVar, "Add description here");
+        fPreviousSize = 0;
+        bool statWarning = false;
+        //loop-wait for broadcast
+        while (fContinueMonitoring)
+        {
+            if (fStatsPeriodDuration == 0.0f)
+            {
+                sleep(0.1f);
+                continue;
+            }
+            else
+                sleep(fStatsPeriodDuration);
+            //update the fits files sizes
+            calculateTotalSizeWritten(statVar, statWarning, false);
+            if (fStatsPeriodDuration == 0.0f)
+                continue;
+            statVar.writingRate = (statVar.sizeWritten - fPreviousSize)/fStatsPeriodDuration;  
+
+            fPreviousSize = statVar.sizeWritten;
+            if (statVar.writingRate != 0) //if data has been written
+            {
+                srvc.updateService();
+                if(fDebugIsOn)
+                {
+                    stringstream str;
+                    str << "Size written: " << statVar.sizeWritten/1024 << " KB; writting rate: ";
+                    str << statVar.writingRate/1024 << " KB/s; free space: ";
+                    str << statVar.freeSpace/(1024*1024) << " MB";
+                    Debug(str.str());
+                }
+            }
+        }
 }
 
@@ -503,149 +485,145 @@
 //!Setup the allows states, configs and transitions for the data logger
 //
-DataLogger::DataLogger(std::ostream &out) : StateMachineDim(out, "DATA_LOGGER")
+DataLogger::DataLogger(ostream &out) : StateMachineDim(out, "DATA_LOGGER")
 {
     dic_disable_padding();
     dis_disable_padding();
-		//initialize member data
-		fNightlyFileName = ".";
-		fRunFileName = ".";
-		fRunNumber = -1;
-		fPreviousRunNumber = fRunNumber;
+    //initialize member data
+    fNightlyFileName = ".";
+    fRunFileName = ".";
+    fRunNumber = -1;
+    fPreviousRunNumber = fRunNumber;
 #ifdef HAVE_FITS
-#ifdef ONE_RUN_FITS_ONLY
-		fRunFitsFile = NULL;
+    fRunFitsFile = NULL;
 #endif
-#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");
-
-                //Provide a logging command
-                //I get the feeling that I should be going through the EventImp
-                //instead of DimCommand directly, mainly because the commandHandler
-                //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.");
-		
-		//Provide a print command
-                stringstream str;
-                str << 	kSM_Ready << " " << kSM_NightlyOpen << " " << kSM_WaitingRun << " " << kSM_Logging << " " << kSM_BadNightlyConfig;
-                str << " " << kSM_BadRunConfig;
-
-				AddEvent(fPrintCommand, str.str().c_str(), "")
-//                AddEvent(fPrintCommand, kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_BadNightlyConfig, kSM_BadRunConfig)
-                    (boost::bind(&DataLogger::PrintStatePlease, this, _1))
-                    ("Print information about the internal status of the data logger.");
-
- 		fServiceList.SetHandler(this);
-		CheckForServicesUpdate();
-
-		//start the monitoring service
-		fContinueMonitoring = true;
-		fMonitoringThread = boost::thread(boost::bind(&DataLogger::ServicesMonitoring, this));
-		fBaseSizeNightly = 0;
-		fBaseSizeRun = 0;
-		OpenFileToDim fToDim;
-		fToDim.code = 0;
-		fToDim.fileName[0] = '\0';
-
-                fOpenedNightlyFiles = new DimDescribedService(GetName() + "/FILENAME_NIGHTLY", "I:1;C", fToDim,
-                                                              "Path and base name which is used to compile the filenames for the nightly files."
-                                                              "|Type[int]:type of open files (1=log, 2=rep, 4=fits)"
-                                                              "|Name[string]:path and base file name");
-
-                fOpenedRunFiles = new DimDescribedService(GetName() + "/FILENAME_RUN", "I:1;C", fToDim,
-                                                          "Path and base name which is used to compile the filenames for the run files."
-                                                          "|Type[int]:type of open files (1=log, 2=rep, 4=fits)"
-                                                          "|Name[string]:path and base file name");
-
-                fNumSubAndFitsData.numSubscriptions = 0;
-		fNumSubAndFitsData.numOpenFits = 0;
-                fNumSubAndFits = new DimDescribedService(GetName() + "/NUM_SUBS", "I:2", fNumSubAndFitsData,
-                                                         "Shows number of services to which the data logger is currently subscribed and the total number of open files."
-                                                         "|Subscriptions[int]:number of dim services to which the data logger is currently subscribed."
-                                                         "|NumOpenFiles[int]:number of files currently open by the data logger");
-
-        //black/white list
-        fHasBlackList = false;
-        fHasWhiteList = false;
-		fBlackList.clear();
-		fWhiteList.clear();
-		//services parameters
-		fDebugIsOn = false;
-		fStatsPeriodDuration = 1.0f;
-		fOpenedFilesIsOn = true;
-		fNumSubAndFitsIsOn = true;
-
-                //provide services control commands
-                AddEvent(fDebugOnOff, "B:1", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
-                    (boost::bind(&DataLogger::SetDebugOnOff, this, _1))
-                    ("Switch debug mode on off. Debug mode prints ifnormation about every service written to a file."
-                     "|Enable[bool]:Enable of disable debuig mode (yes/no).");
-
-                AddEvent(fStatsPeriod, "F", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
-                    (boost::bind(&DataLogger::SetStatsPeriod, this, _1))
-                    ("Interval in which the data-logger statitistics service (STATS) is updated."
-                     "Interval[s]:Floating point value in seconds.");
-
-                AddEvent(fStartStopOpenedFiles, "B:1", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
-                    (boost::bind(&DataLogger::SetOpenedFilesOnOff ,this, _1))
-                    ("Can be used to switch the service off which distributes information about the open files.");
-
-                AddEvent(fStartStopNumSubsAndFits, "B:1", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
-                    (boost::bind(&DataLogger::SetNumSubsAndFitsOnOff, this, _1))
-                    ("Can be used to switch the service off which distributes information about the number of subscriptions and open files.");
-
-		fDestructing = false;
-		if(fDebugIsOn)
-		{
-                    Debug("DataLogger Init Done.");
-                }
+
+     //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");
+
+     //Provide a logging command
+     //I get the feeling that I should be going through the EventImp
+     //instead of DimCommand directly, mainly because the commandHandler
+     //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.");
+        
+     //Provide a print command
+     stringstream str;
+     str <<     kSM_Ready << " " << kSM_NightlyOpen << " " << kSM_WaitingRun << " " << kSM_Logging << " " << kSM_BadNightlyConfig;
+     str << " " << kSM_BadRunConfig;
+     AddEvent(fPrintCommand, str.str().c_str(), "")
+             (boost::bind(&DataLogger::PrintStatePlease, this, _1))
+             ("Print information about the internal status of the data logger.");
+
+     fServiceList.SetHandler(this);
+     CheckForServicesUpdate();
+
+     //start the monitoring service
+     fContinueMonitoring = true;
+     fMonitoringThread = boost::thread(boost::bind(&DataLogger::ServicesMonitoring, this));
+     fBaseSizeNightly = 0;
+     fBaseSizeRun = 0;
+     OpenFileToDim fToDim;
+     fToDim.code = 0;
+     fToDim.fileName[0] = '\0';
+
+     fOpenedNightlyFiles = new DimDescribedService(GetName() + "/FILENAME_NIGHTLY", "I:1;C", fToDim,
+                               "Path and base name which is used to compile the filenames for the nightly files."
+                               "|Type[int]:type of open files (1=log, 2=rep, 4=fits)"
+                               "|Name[string]:path and base file name");
+
+     fOpenedRunFiles = new DimDescribedService(GetName() + "/FILENAME_RUN", "I:1;C", fToDim,
+                               "Path and base name which is used to compile the filenames for the run files."
+                               "|Type[int]:type of open files (1=log, 2=rep, 4=fits)"
+                               "|Name[string]:path and base file name");
+
+     fNumSubAndFitsData.numSubscriptions = 0;
+     fNumSubAndFitsData.numOpenFits = 0;
+     fNumSubAndFits = new DimDescribedService(GetName() + "/NUM_SUBS", "I:2", fNumSubAndFitsData,
+                               "Shows number of services to which the data logger is currently subscribed and the total number of open files."
+                               "|Subscriptions[int]:number of dim services to which the data logger is currently subscribed."
+                               "|NumOpenFiles[int]:number of files currently open by the data logger");
+
+     //black/white list
+     fHasBlackList = false;
+     fHasWhiteList = false;
+     fBlackList.clear();
+     fWhiteList.clear();
+     //services parameters
+     fDebugIsOn = false;
+     fStatsPeriodDuration = 1.0f;
+     fOpenedFilesIsOn = true;
+     fNumSubAndFitsIsOn = true;
+
+     //provide services control commands
+     AddEvent(fDebugOnOff, "B:1", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
+             (boost::bind(&DataLogger::SetDebugOnOff, this, _1))
+             ("Switch debug mode on off. Debug mode prints ifnormation about every service written to a file."
+              "|Enable[bool]:Enable of disable debuig mode (yes/no).");
+
+     AddEvent(fStatsPeriod, "F", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
+             (boost::bind(&DataLogger::SetStatsPeriod, this, _1))
+             ("Interval in which the data-logger statitistics service (STATS) is updated."
+              "Interval[s]:Floating point value in seconds.");
+
+     AddEvent(fStartStopOpenedFiles, "B:1", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
+              (boost::bind(&DataLogger::SetOpenedFilesOnOff ,this, _1))
+              ("Can be used to switch the service off which distributes information about the open files.");
+
+     AddEvent(fStartStopNumSubsAndFits, "B:1", kSM_NightlyOpen, kSM_Logging, kSM_WaitingRun, kSM_Ready)
+             (boost::bind(&DataLogger::SetNumSubsAndFitsOnOff, this, _1))
+             ("Can be used to switch the service off which distributes information about the number of subscriptions and open files.");
+
+     fDestructing = false;
+     if(fDebugIsOn)
+     {
+         Debug("DataLogger Init Done.");
+     }
 }
 // --------------------------------------------------------------------------
@@ -653,72 +631,69 @@
 //! Checks for changes in the existing services.
 //! Any new service will be added to the service list, while the ones which disappeared are removed.
-//! @todo
-//! 	add the configuration (using the conf class ?)
 //
 //FIXME The service must be udpated so that I get the first notification. This should not be
 bool DataLogger::CheckForServicesUpdate()
 { 
-	bool serviceUpdated = false;
-	//get the current server list
-	const std::vector<std::string> serverList = fServiceList.GetServerList();
-	//first let's remove the servers that may have disapeared
-	//can't treat the erase on maps the same way as for vectors. Do it the safe way instead
-	std::vector<std::string> toBeDeleted;
-	for (SubscriptionsListType::iterator cListe = fServiceSubscriptions.begin(); cListe != fServiceSubscriptions.end(); cListe++)
-	{
-		std::vector<std::string>::const_iterator givenServers;
-		for (givenServers=serverList.begin(); givenServers!= serverList.end(); givenServers++)
-			if (cListe->first == *givenServers)
-				break;
-		if (givenServers == serverList.end())//server vanished. Remove it
-		{	
-			toBeDeleted.push_back(cListe->first);
-			serviceUpdated = true;
-		}
-			
-	} 
-	for (std::vector<std::string>::const_iterator it = toBeDeleted.begin(); it != toBeDeleted.end(); it++)
-		fServiceSubscriptions.erase(*it);
-	//now crawl through the list of servers, and see if there was some updates
-	for (std::vector<std::string>::const_iterator i=serverList.begin(); i!=serverList.end();i++)
-	{
-		//skip the two de-fact excluded services
-		//Dim crashes if the publisher subscribes to its own service. This sounds weird, I agree.
-		if ((i->find("DIS_DNS") != std::string::npos) ||
-		    (i->find("DATA_LOGGER") != std::string::npos))
-			continue;
-		//find the current server in our subscription list	
-		SubscriptionsListType::iterator cSubs = fServiceSubscriptions.find(*i);
-		//get the service list of the current server
-		std::vector<std::string> cServicesList = fServiceList.GetServiceList(*i);
-		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.
-			std::map<std::string, SubscriptionType>::iterator serverSubs;
-			std::vector<std::string>::const_iterator givenSubs;
-			toBeDeleted.clear();
-			for (serverSubs=cSubs->second.begin(); serverSubs != cSubs->second.end(); serverSubs++)
-			{
-				for (givenSubs = cServicesList.begin(); givenSubs != cServicesList.end(); givenSubs++)
-					if (serverSubs->first == *givenSubs)
-						break;
-				if (givenSubs == cServicesList.end())
-				{
-					toBeDeleted.push_back(serverSubs->first);
-					serviceUpdated = true;
-				}	
-			}
-			for (std::vector<std::string>::const_iterator it = toBeDeleted.begin(); it != toBeDeleted.end(); it++)
-				cSubs->second.erase(*it);
-			//now check for new services
-			for (givenSubs = cServicesList.begin(); givenSubs != cServicesList.end(); givenSubs++)
-			{
-				if (*givenSubs == "SERVICE_LIST")
-					continue;
-
-				if (fHasWhiteList && (fWhiteList.find(*i + "/") == fWhiteList.end()) &&
+    bool serviceUpdated = false;
+    //get the current server list
+    const vector<string> serverList = fServiceList.GetServerList();
+    //first let's remove the servers that may have disapeared
+    //can't treat the erase on maps the same way as for vectors. Do it the safe way instead
+    vector<string> toBeDeleted;
+    for (SubscriptionsListType::iterator cListe = fServiceSubscriptions.begin(); cListe != fServiceSubscriptions.end(); cListe++)
+    {
+        vector<string>::const_iterator givenServers;
+        for (givenServers=serverList.begin(); givenServers!= serverList.end(); givenServers++)
+            if (cListe->first == *givenServers)
+                break;
+        if (givenServers == serverList.end())//server vanished. Remove it
+        {    
+            toBeDeleted.push_back(cListe->first);
+            serviceUpdated = true;
+        }
+    } 
+    for (vector<string>::const_iterator it = toBeDeleted.begin(); it != toBeDeleted.end(); it++)
+        fServiceSubscriptions.erase(*it);
+    //now crawl through the list of servers, and see if there was some updates
+    for (vector<string>::const_iterator i=serverList.begin(); i!=serverList.end();i++)
+    {
+        //skip the two de-fact excluded services
+        //Dim crashes if the publisher subscribes to its own service. This sounds weird, I agree.
+        if ((i->find("DIS_DNS") != string::npos) ||
+            (i->find("DATA_LOGGER") != string::npos))
+            continue;
+        //find the current server in our subscription list    
+        SubscriptionsListType::iterator cSubs = fServiceSubscriptions.find(*i);
+        //get the service list of the current server
+        vector<string> cServicesList = fServiceList.GetServiceList(*i);
+        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.
+            map<string, SubscriptionType>::iterator serverSubs;
+            vector<string>::const_iterator givenSubs;
+            toBeDeleted.clear();
+            for (serverSubs=cSubs->second.begin(); serverSubs != cSubs->second.end(); serverSubs++)
+            {
+                for (givenSubs = cServicesList.begin(); givenSubs != cServicesList.end(); givenSubs++)
+                    if (serverSubs->first == *givenSubs)
+                        break;
+                if (givenSubs == cServicesList.end())
+                {
+                    toBeDeleted.push_back(serverSubs->first);
+                    serviceUpdated = true;
+                }    
+            }
+            for (vector<string>::const_iterator it = toBeDeleted.begin(); it != toBeDeleted.end(); it++)
+                cSubs->second.erase(*it);
+            //now check for new services
+            for (givenSubs = cServicesList.begin(); givenSubs != cServicesList.end(); givenSubs++)
+            {
+                if (*givenSubs == "SERVICE_LIST")
+                    continue;
+
+                if (fHasWhiteList && (fWhiteList.find(*i + "/") == fWhiteList.end()) &&
                                      (fWhiteList.find(*i + "/" + *givenSubs) == fWhiteList.end()) &&
                                      (fWhiteList.find("/" + *givenSubs) == fWhiteList.end()))
-				    continue;
+                    continue;
                 if (fHasBlackList && ((fBlackList.find(*i + "/") != fBlackList.end()) ||
                                       (fBlackList.find(*i + "/" + *givenSubs) != fBlackList.end()) ||
@@ -727,24 +702,24 @@
 
                 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);
-					serviceUpdated = true;
-					if(fDebugIsOn)
-					{
-						stringstream str;
-						str << "Subscribing to service " << *i << "/" << *givenSubs;
-						Debug(str.str());	
-					}
-				}	
-			}
-		}
-		else //server not found in our list. Create its entry
-		{
-			fServiceSubscriptions[*i] = std::map<std::string, SubscriptionType>();
-			std::map<std::string, SubscriptionType>& liste = fServiceSubscriptions[*i];
-			for (std::vector<std::string>::const_iterator j = cServicesList.begin(); j!= cServicesList.end(); j++)
-			{
-				if (*j == "SERVICE_LIST")
-					continue;
+                {//service not found. Add it
+                    cSubs->second[*givenSubs].dimInfo = new DimStampedInfo(((*i) + "/" + *givenSubs).c_str(), const_cast<char*>(""), this);
+                    serviceUpdated = true;
+                    if(fDebugIsOn)
+                    {
+                        stringstream str;
+                        str << "Subscribing to service " << *i << "/" << *givenSubs;
+                        Debug(str.str());    
+                    }
+                }    
+            }
+        }
+        else //server not found in our list. Create its entry
+        {
+            fServiceSubscriptions[*i] = map<string, SubscriptionType>();
+            map<string, SubscriptionType>& liste = fServiceSubscriptions[*i];
+            for (vector<string>::const_iterator j = cServicesList.begin(); j!= cServicesList.end(); j++)
+            {
+                if (*j == "SERVICE_LIST")
+                    continue;
                 if (fHasWhiteList && (fWhiteList.find(*i + "/") == fWhiteList.end()) &&
                                      (fWhiteList.find(*i + "/" + *j) == fWhiteList.end()) &&
@@ -756,17 +731,16 @@
                     continue;
 
-					
-				liste[*j].dimInfo = new DimStampedInfo(((*i) + "/" + (*j)).c_str(), const_cast<char*>(""), this);
-				serviceUpdated = true;
-				if(fDebugIsOn)
-				{
-					stringstream str;
-					str << "Subscribing to service " << *i << "/" << *j;
-					Debug(str.str());	
-				}
-			}
-		}	
-	}
-	return serviceUpdated;
+                liste[*j].dimInfo = new DimStampedInfo(((*i) + "/" + (*j)).c_str(), const_cast<char*>(""), this);
+                serviceUpdated = true;
+                if(fDebugIsOn)
+                {
+                    stringstream str;
+                    str << "Subscribing to service " << *i << "/" << *j;
+                    Debug(str.str());    
+                }
+            }
+        }    
+    }
+    return serviceUpdated;
 }
 // --------------------------------------------------------------------------
@@ -776,42 +750,38 @@
 DataLogger::~DataLogger()
 {
-	if (fDebugIsOn)
-	{
-		Debug("DataLogger destruction starts");	
-	}
-	fDestructing = true;
-	//first let's go to the ready state
-	//TODO some closing done below has already been executed by GoToReady. figure out what should be removed.
-	GoToReadyPlease(); 
-	//release the services subscriptions
-	fServiceSubscriptions.clear();
-	//exit the monitoring loop
-	fContinueMonitoring = false;
-//	delete[] fDimBuffer;
-	fMonitoringThread.join();
-	//close the files
-	if (fNightlyLogFile.is_open())
-		fNightlyLogFile.close();
-	if (fNightlyReportFile.is_open())
-		fNightlyReportFile.close();
-	if (fRunLogFile.is_open())
-		fRunLogFile.close();
-	if (fRunReportFile.is_open())
-		fRunReportFile.close();
-	delete fOpenedNightlyFiles;
-	delete fOpenedRunFiles;
-	delete fNumSubAndFits;
-//TODO notify that all files were closed
+    if (fDebugIsOn)
+    {
+        Debug("DataLogger destruction starts");    
+    }
+    fDestructing = true;
+    //first let's go to the ready state
+    GoToReadyPlease(); 
+    //release the services subscriptions
+    fServiceSubscriptions.clear();
+    //exit the monitoring loop
+    fContinueMonitoring = false;
+
+    fMonitoringThread.join();
+    //close the files
+    if (fNightlyLogFile.is_open())
+        fNightlyLogFile.close();
+    if (fNightlyReportFile.is_open())
+        fNightlyReportFile.close();
+    if (fRunLogFile.is_open())
+        fRunLogFile.close();
+    if (fRunReportFile.is_open())
+        fRunReportFile.close();
+    delete fOpenedNightlyFiles;
+    delete fOpenedRunFiles;
+    delete fNumSubAndFits;
 #ifdef HAVE_FITS
-#ifdef ONE_RUN_FITS_ONLY
-	if (fRunFitsFile != NULL)
-		delete fRunFitsFile;
-	fRunFitsFile = NULL;
+    if (fRunFitsFile != NULL)
+        delete fRunFitsFile;
+    fRunFitsFile = NULL;
 #endif
-#endif
-	if (fDebugIsOn)
-	{
-		Debug("DataLogger desctruction ends");	
-	}
+    if (fDebugIsOn)
+    {
+        Debug("DataLogger desctruction ends");    
+    }
 }
 
@@ -823,66 +793,66 @@
 {
     // Make sure getTimestamp is called _before_ getTimestampMillisecs
-	if (fDestructing)
-		return;
+    if (fDestructing)
+        return;
 
     DimInfo* I = getInfo();
-	SubscriptionsListType::iterator x;
-	std::map<std::string, SubscriptionType>::iterator y;
-	if (I==NULL)
-	{
-		if (CheckForServicesUpdate())
-		{
-			//services were updated. Notify
-			fNumSubAndFitsData.numSubscriptions = 0;
-			for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
-				fNumSubAndFitsData.numSubscriptions += x->second.size();
-			if (fNumSubAndFitsIsOn)
-			{
-				if (fDebugIsOn)
-				{
-					stringstream str;
-					str << "Updating number of subscriptions service: Num Subs=" << fNumSubAndFitsData.numSubscriptions << " Num open FITS=" << fNumSubAndFitsData.numOpenFits;
-					Debug(str.str());	
-				}
-				fNumSubAndFits->updateService();
-			}
-		}
-		return;
-	}
-	//check if the service pointer corresponds to something that we subscribed to
-	//this is a fix for a bug that provides bad Infos when a server starts
-	bool found = false;
-	for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
-	{//find current service is subscriptions
-		for (y=x->second.begin(); y!=x->second.end();y++)
-			if (y->second.dimInfo == I)
-			{
-				found = true;	
-				break;
-			}
-		if (found)
-			break;
-	}
-	if (!found)
-		return;
-	if (I->getSize() <= 0)
-		return;
+    SubscriptionsListType::iterator x;
+    map<string, SubscriptionType>::iterator y;
+    if (I==NULL)
+    {
+        if (CheckForServicesUpdate())
+        {
+            //services were updated. Notify
+            fNumSubAndFitsData.numSubscriptions = 0;
+            for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
+                fNumSubAndFitsData.numSubscriptions += x->second.size();
+            if (fNumSubAndFitsIsOn)
+            {
+                if (fDebugIsOn)
+                {
+                    stringstream str;
+                    str << "Updating number of subscriptions service: Num Subs=" << fNumSubAndFitsData.numSubscriptions << " Num open FITS=" << fNumSubAndFitsData.numOpenFits;
+                    Debug(str.str());    
+                }
+                fNumSubAndFits->updateService();
+            }
+        }
+        return;
+    }
+    //check if the service pointer corresponds to something that we subscribed to
+    //this is a fix for a bug that provides bad Infos when a server starts
+    bool found = false;
+    for (x=fServiceSubscriptions.begin(); x != fServiceSubscriptions.end(); x++)
+    {//find current service is subscriptions
+        for (y=x->second.begin(); y!=x->second.end();y++)
+            if (y->second.dimInfo == I)
+            {
+                found = true;    
+                break;
+            }
+        if (found)
+            break;
+    }
+    if (!found)
+        return;
+    if (I->getSize() <= 0)
+        return;
 
         // Make sure that getTimestampMillisecs is NEVER called before
         // getTimestamp is properly called
         // check that the message has been updated by something, i.e. must be different from its initial value
-	if (I->getTimestamp() == 0)
-		return;
-
-	CheckForRunNumber(I);
-
-	if (fPreviousRunNumber != fRunNumber)
-	{//run number has changed. close and reopen run files.
-		StopRunPlease();
-		StartRunPlease();
-		fPreviousRunNumber = fRunNumber;
-	}
-
-	ReportPlease(I, y->second);
+    if (I->getTimestamp() == 0)
+        return;
+
+    CheckForRunNumber(I);
+
+    if (fPreviousRunNumber != fRunNumber)
+    {//run number has changed. close and reopen run files.
+        StopRunPlease();
+        StartRunPlease();
+        fPreviousRunNumber = fRunNumber;
+    }
+
+    ReportPlease(I, y->second);
 
 }
@@ -893,16 +863,15 @@
 //! If so, then remember it. A run number is required to open the run-log file
 //! @param I
-//!		the current DimInfo
+//!        the current DimInfo
 //
 void DataLogger::CheckForRunNumber(DimInfo* I)
 {
-	if (strstr(I->getName(), fRunNumberInfo) != NULL)
-	{//assumes that the run number is an integer
-		//TODO check the format here
-		fRunNumber = I->getInt();	
-		stringstream str;
-		str << "New run number is " << fRunNumber;
-		Message(str.str());
-	}
+    if (strstr(I->getName(), fRunNumberInfo) != NULL)
+    {//assumes that the run number is an integer
+        fRunNumber = I->getInt();    
+        stringstream str;
+        str << "New run number is " << fRunNumber;
+        Message(str.str());
+    }
 }
 
@@ -911,163 +880,162 @@
 //! write infos to log files.
 //! @param I
-//! 	The current DimInfo 
+//!     The current DimInfo 
 //! @param sub
-//!		The dataLogger's subscription corresponding to this DimInfo
+//!        The dataLogger's subscription corresponding to this DimInfo
 //
 void DataLogger::ReportPlease(DimInfo* I, SubscriptionType& sub)
 {
-	//should we log or report this info ? (i.e. is it a message ?)
-	bool isItaReport = ((strstr(I->getName(), "Message") == NULL) && (strstr(I->getName(), "MESSAGE") == NULL));
-	if (I->getFormat()[0] == 'C')
-		isItaReport = false;
-	//TODO add service exclusion
-	
-	if (!fNightlyReportFile.is_open())
-		return;
-
-	//create the converter for that service
-	if (sub.fConv == NULL && isItaReport)
-	{
-		//trick the converter in case of 'C'. why do I do this ? well simple: the converter checks that the right number
-		//of bytes was written. because I skip 'C' with fits, the bytes will not be allocated, hence the "size copied ckeck"
-		//of the converter will fail, hence throwing an exception.
-		std::string fakeFormat(I->getFormat());
-		if (fakeFormat[fakeFormat.size()-1] == 'C')
-			fakeFormat = fakeFormat.substr(0, fakeFormat.size()-1);
-		sub.fConv = new Converter(Out(), I->getFormat());	
-		if (!sub.fConv)
-		{
-			std::stringstream str;
-			str << "Couldn't properly parse the format... service " << sub.dimInfo->getName() << " ignored.";
-			Error(str);
-			return;	
-		}
-	}
-		
-	//construct the header
-	std::stringstream header;
-	Time cTime(I->getTimestamp(), I->getTimestampMillisecs()*1000);
-	fQuality = I->getQuality();
-	fMjD = cTime.Mjd();
-
-	if (isItaReport)
-	{
-		//write text header
-		header << I->getName() << " " << fQuality << " ";
-		header << cTime.Y() << " " << cTime.M() << " " << cTime.D() << " ";
-		header << cTime.h() << " " << cTime.m() << " " << cTime.s() << " ";
-		header << cTime.ms() << " " << I->getTimestamp() << " ";
-
-		std::string text;
+    //should we log or report this info ? (i.e. is it a message ?)
+    bool isItaReport = ((strstr(I->getName(), "Message") == NULL) && (strstr(I->getName(), "MESSAGE") == NULL));
+    if (I->getFormat()[0] == 'C')
+        isItaReport = false;
+    
+    if (!fNightlyReportFile.is_open())
+        return;
+
+    //create the converter for that service
+    if (sub.fConv == NULL && isItaReport)
+    {
+        //trick the converter in case of 'C'. why do I do this ? well simple: the converter checks that the right number
+        //of bytes was written. because I skip 'C' with fits, the bytes will not be allocated, hence the "size copied ckeck"
+        //of the converter will fail, hence throwing an exception.
+        string fakeFormat(I->getFormat());
+        if (fakeFormat[fakeFormat.size()-1] == 'C')
+            fakeFormat = fakeFormat.substr(0, fakeFormat.size()-1);
+        sub.fConv = new Converter(Out(), I->getFormat());    
+        if (!sub.fConv)
+        {
+            stringstream str;
+            str << "Couldn't properly parse the format... service " << sub.dimInfo->getName() << " ignored.";
+            Error(str);
+            return;    
+        }
+    }
+        
+    //construct the header
+    stringstream header;
+    Time cTime(I->getTimestamp(), I->getTimestampMillisecs()*1000);
+    fQuality = I->getQuality();
+    fMjD = cTime.Mjd();
+
+    if (isItaReport)
+    {
+        //write text header
+        header << I->getName() << " " << fQuality << " ";
+        header << cTime.Y() << " " << cTime.M() << " " << cTime.D() << " ";
+        header << cTime.h() << " " << cTime.m() << " " << cTime.s() << " ";
+        header << cTime.ms() << " " << I->getTimestamp() << " ";
+
+        string text;
         try
         {
-        	text = sub.fConv->GetString(I->getData(), I->getSize());
-        }
-        catch (const std::runtime_error &e)
-        {
-        	Out() << kRed << e.what() << endl;
-        	std::stringstream str;
-        	str << "Could not properly parse the data for service " << sub.dimInfo->getName();
-        	str << " reason: " << e.what() << ". Entry ignored";
+            text = sub.fConv->GetString(I->getData(), I->getSize());
+        }
+        catch (const runtime_error &e)
+        {
+            Out() << kRed << e.what() << endl;
+            stringstream str;
+            str << "Could not properly parse the data for service " << sub.dimInfo->getName();
+            str << " reason: " << e.what() << ". Entry ignored";
             Error(str);
             return;
         }
 
-		if (text.empty())
-		{
-			std::stringstream str;
-			str << "Service " << sub.dimInfo->getName() << " sent an empty string";
-			Info(str);
-        	return;
-		}
+        if (text.empty())
+        {
+            stringstream str;
+            str << "Service " << sub.dimInfo->getName() << " sent an empty string";
+            Info(str);
+            return;
+        }
         //replace bizarre characters by white space
         replace(text.begin(), text.end(), '\n', '\\');
-        replace_if(text.begin(), text.end(), std::ptr_fun<int, int>(&std::iscntrl), ' ');
+        replace_if(text.begin(), text.end(), ptr_fun<int, int>(&iscntrl), ' ');
         
         //write entry to Nightly report
-		if (fNightlyReportFile.is_open())
-		{
-			if (fDebugIsOn)
-			{
-				stringstream str;
-				str << "Writing: \"" << header.str() << text << "\" to Nightly report file";
-				Debug(str.str());	
-			}
-			fNightlyReportFile << header.str() << text << std::endl;
-			//check if either eof, bailbit or batbit are set
-			if (!fNightlyReportFile.good())
-			{
-				Error("An error occured while writing to the nightly report file. Closing it");
-				if (fNightlyReportFile.is_open())
-					fNightlyReportFile.close();
-			}
-		}
-		//write entry to run-report
-		if (fRunReportFile.is_open())
-		{
-			if (fDebugIsOn)
-			{
-				stringstream str;
-				str << "Writing: \"" << header.str() << text << "\" to Run report file";
-				Debug(str.str());	
-			}
-			fRunReportFile << header.str() << text << std::endl;
-			if (!fRunReportFile.good())
-			{
-				Error("An error occured while writing to the run report file. Closing it.");
-				if (fRunReportFile.is_open())
-					fRunReportFile.close();	
-			}
-		}
-	}
-	else
-	{//write entry to both Nightly and run logs
-		std::string n = I->getName();
-		std::stringstream msg;
-		msg << n << ": " << I->getString();//n.substr(0, n.find_first_of('/')) << ": " << I->getString();
-
-		if (fNightlyLogFile.is_open())
-		{
-			if (fDebugIsOn)
-			{
-				stringstream str;
-				str << "Writing: \"" << msg.str() << "\" to Nightly log file";
-				Debug(str.str());	
-			}
-			MessageImp nightlyMess(fNightlyLogFile);
-			nightlyMess.Write(cTime, msg.str().c_str(), fQuality);
-			if (!fNightlyLogFile.good())
-			{
-				Error("An error occured while writing to the nightly log file. Closing it.");
-				if (fNightlyLogFile.is_open())
-					fNightlyLogFile.close();	
-			}
-		}
-		if (fRunLogFile.is_open())
-		{
-			if (fDebugIsOn)
-			{
-				stringstream str;
-				str << "Writing: \"" << msg.str() << "\" to Run log file";
-				Debug(str.str());	
-			}
-			MessageImp runMess(fRunLogFile);
-			runMess.Write(cTime, msg.str().c_str(), fQuality);
-			if (!fRunLogFile.good())
-			{
-				Error("An error occured while writing to the run log file. Closing it.");
-				if (fRunLogFile.is_open())
-					fRunLogFile.close();	
-			}
-		}
-	}
+        if (fNightlyReportFile.is_open())
+        {
+            if (fDebugIsOn)
+            {
+                stringstream str;
+                str << "Writing: \"" << header.str() << text << "\" to Nightly report file";
+                Debug(str.str());    
+            }
+            fNightlyReportFile << header.str() << text << endl;
+            //check if either eof, bailbit or batbit are set
+            if (!fNightlyReportFile.good())
+            {
+                Error("An error occured while writing to the nightly report file. Closing it");
+                if (fNightlyReportFile.is_open())
+                    fNightlyReportFile.close();
+            }
+        }
+        //write entry to run-report
+        if (fRunReportFile.is_open())
+        {
+            if (fDebugIsOn)
+            {
+                stringstream str;
+                str << "Writing: \"" << header.str() << text << "\" to Run report file";
+                Debug(str.str());    
+            }
+            fRunReportFile << header.str() << text << endl;
+            if (!fRunReportFile.good())
+            {
+                Error("An error occured while writing to the run report file. Closing it.");
+                if (fRunReportFile.is_open())
+                    fRunReportFile.close();    
+            }
+        }
+    }
+    else
+    {//write entry to both Nightly and run logs
+        string n = I->getName();
+        stringstream msg;
+        msg << n << ": " << I->getString();//n.substr(0, n.find_first_of('/')) << ": " << I->getString();
+
+        if (fNightlyLogFile.is_open())
+        {
+            if (fDebugIsOn)
+            {
+                stringstream str;
+                str << "Writing: \"" << msg.str() << "\" to Nightly log file";
+                Debug(str.str());    
+            }
+            MessageImp nightlyMess(fNightlyLogFile);
+            nightlyMess.Write(cTime, msg.str().c_str(), fQuality);
+            if (!fNightlyLogFile.good())
+            {
+                Error("An error occured while writing to the nightly log file. Closing it.");
+                if (fNightlyLogFile.is_open())
+                    fNightlyLogFile.close();    
+            }
+        }
+        if (fRunLogFile.is_open())
+        {
+            if (fDebugIsOn)
+            {
+                stringstream str;
+                str << "Writing: \"" << msg.str() << "\" to Run log file";
+                Debug(str.str());    
+            }
+            MessageImp runMess(fRunLogFile);
+            runMess.Write(cTime, msg.str().c_str(), fQuality);
+            if (!fRunLogFile.good())
+            {
+                Error("An error occured while writing to the run log file. Closing it.");
+                if (fRunLogFile.is_open())
+                    fRunLogFile.close();    
+            }
+        }
+    }
 
 #ifdef HAVE_FITS
-	if (isItaReport)
-	{
-		if (!sub.nightlyFile.IsOpen() || !sub.runFile.IsOpen())
-			OpenFITSFilesPlease(sub);	
-		WriteToFITS(sub);
-	}	
+    if (isItaReport)
+    {
+        if (!sub.nightlyFile.IsOpen() || !sub.runFile.IsOpen())
+            OpenFITSFilesPlease(sub);    
+        WriteToFITS(sub);
+    }    
 #endif
 
@@ -1078,97 +1046,96 @@
 //! write messages to logs. 
 //! @param evt
-//!		the current event to log
+//!        the current event to log
 //! @returns 
-//!		the new state. Currently, always the current state
+//!        the new state. Currently, always the current state
 //!
 //! @deprecated
 //!    I guess that this function should not be any longer
 //
-//TODO isn't that function not used any longer ? If so I guess that we should get rid of it...
 //Otherwise re-write it properly with the MessageImp class
 int DataLogger::LogMessagePlease(const Event& evt)
 {
-	if (!fNightlyLogFile.is_open())
-		return GetCurrentState();
-	
-	std::stringstream header;
-	const Time& cTime = evt.GetTime();
-	header << evt.GetName() << " " << cTime.Y() << " " << cTime.M() << " " << cTime.D() << " ";
-	header << cTime.h() << " " << cTime.m() << " " << cTime.s() << " ";
-	header << cTime.ms() << " ";
-		
+    if (!fNightlyLogFile.is_open())
+        return GetCurrentState();
+    Warn("LogMessagePlease has not been checked nor updated since a long while. Undefined behavior to be expected");
+    stringstream header;
+    const Time& cTime = evt.GetTime();
+    header << evt.GetName() << " " << cTime.Y() << " " << cTime.M() << " " << cTime.D() << " ";
+    header << cTime.h() << " " << cTime.m() << " " << cTime.s() << " ";
+    header << cTime.ms() << " ";
+        
     const Converter conv(Out(), evt.GetFormat());
     if (!conv)
     {
-    	Error("Couldn't properly parse the format... ignored.");
+        Error("Couldn't properly parse the format... ignored.");
         return GetCurrentState();
     }
 
-    std::string text;
+    string text;
     try
     {
-    	text = conv.GetString(evt.GetData(), evt.GetSize());
-    }
-    catch (const std::runtime_error &e)
-    {
-    	Out() << kRed << e.what() << endl;
+        text = conv.GetString(evt.GetData(), evt.GetSize());
+    }
+    catch (const runtime_error &e)
+    {
+        Out() << kRed << e.what() << endl;
         Error("Couldn't properly parse the data... ignored.");
         return GetCurrentState();
     }
 
-	if (text.empty())
+    if (text.empty())
         return GetCurrentState();
 
     //replace bizarre characters by white space
     replace(text.begin(), text.end(), '\n', '\\');
-    replace_if(text.begin(), text.end(), std::ptr_fun<int, int>(&std::iscntrl), ' ');
-	if (fDebugIsOn)
-	{
-		stringstream str;
-		str << "Logging: \"" << header << text << "\"";
-		Debug(str.str());	
-	}
-	
-	if (fNightlyLogFile.is_open())
-	{
-		fNightlyLogFile << header;
-		if (!fNightlyLogFile.good())
-		{
-			Error("An error occured while writing to the run log file. Closing it.");
-			if (fNightlyLogFile.is_open())
-				fNightlyLogFile.close();	
-		}
-	}
-	if (fRunLogFile.is_open())
-	{
-		fRunLogFile << header;
-		if (!fRunLogFile.good())
-		{
-			Error("An error occured while writing to the run log file. Closing it.");
-			if (fRunLogFile.is_open())
-				fRunLogFile.close();	
-		}
-	}
-	if (fNightlyLogFile.is_open())
-	{
-		fNightlyLogFile << text;
-		if (!fNightlyLogFile.good())
-		{
-			Error("An error occured while writing to the run log file. Closing it.");
-			if (fNightlyLogFile.is_open())
-				fNightlyLogFile.close();	
-		}
-	}
-	if (fRunLogFile.is_open())
-	{
-		fRunLogFile << text;
-		if (!fRunLogFile.good())
-		{
-			Error("An error occured while writing to the run log file. Closing it.");
-			if (fRunLogFile.is_open())
-				fRunLogFile.close();	
-		}
-	}
-	return GetCurrentState();
+    replace_if(text.begin(), text.end(), ptr_fun<int, int>(&iscntrl), ' ');
+    if (fDebugIsOn)
+    {
+        stringstream str;
+        str << "Logging: \"" << header << text << "\"";
+        Debug(str.str());    
+    }
+    
+    if (fNightlyLogFile.is_open())
+    {
+        fNightlyLogFile << header;
+        if (!fNightlyLogFile.good())
+        {
+            Error("An error occured while writing to the run log file. Closing it.");
+            if (fNightlyLogFile.is_open())
+                fNightlyLogFile.close();    
+        }
+    }
+    if (fRunLogFile.is_open())
+    {
+        fRunLogFile << header;
+        if (!fRunLogFile.good())
+        {
+            Error("An error occured while writing to the run log file. Closing it.");
+            if (fRunLogFile.is_open())
+                fRunLogFile.close();    
+        }
+    }
+    if (fNightlyLogFile.is_open())
+    {
+        fNightlyLogFile << text;
+        if (!fNightlyLogFile.good())
+        {
+            Error("An error occured while writing to the run log file. Closing it.");
+            if (fNightlyLogFile.is_open())
+                fNightlyLogFile.close();    
+        }
+    }
+    if (fRunLogFile.is_open())
+    {
+        fRunLogFile << text;
+        if (!fRunLogFile.good())
+        {
+            Error("An error occured while writing to the run log file. Closing it.");
+            if (fRunLogFile.is_open())
+                fRunLogFile.close();    
+        }
+    }
+    return GetCurrentState();
 }
 // --------------------------------------------------------------------------
@@ -1176,96 +1143,106 @@
 //! print the dataLogger's current state. invoked by the PRINT command
 //! @param evt
-//!		the current event. Not used by the method
+//!        the current event. Not used by the method
 //! @returns 
-//!		the new state. Which, in that case, is the current state
+//!        the new state. Which, in that case, is the current state
 //!
 int DataLogger::PrintStatePlease(const Event& )
 {
-	Message("-----------------------------------------");
-	Message("------ DATA LOGGER CURRENT STATE --------");
-	Message("-----------------------------------------");
-	//print the path configuration
-	std::string actualTargetDir;
-	if (fNightlyFileName == ".")
-	{
-            char currentPath[FILENAME_MAX];
-            if (getcwd(currentPath, sizeof(currentPath)))
-                actualTargetDir = currentPath;
-        }
-	else
-		actualTargetDir = fNightlyFileName;
-	Message("Nightly Path: " + actualTargetDir);
-	if (fRunFileName == ".")
-	{
-            char currentPath[FILENAME_MAX];
-            if (getcwd(currentPath, sizeof(currentPath)))
-                actualTargetDir = currentPath;
-	}
-	else
-		actualTargetDir = fRunFileName;
-	Message("Run Path: " + actualTargetDir);
-	stringstream str;
-	str << "Run Number: " << fRunNumber;
-	Message(str.str());
-	Message("----------- OPENED FILES ----------------");
-	//print all the open files. 
-	if (fNightlyLogFile.is_open())
-		Message("Nightly Log..........OPEN");
-	else
-		Message("Nightly log........CLOSED");
-	if (fNightlyReportFile.is_open())
-		Message("Nightly Report.......OPEN");
-	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");
-	bool statWarning = false;
-	DataLoggerStats statVar;
-	calculateTotalSizeWritten(statVar, statWarning, false);
-	Message("---------------- STATS ------------------");
-	str.str("");
-	str << "Total Size written: " << statVar.sizeWritten << " bytes.";
+    Message("-----------------------------------------");
+    Message("------ DATA LOGGER CURRENT STATE --------");
+    Message("-----------------------------------------");
+    //print the path configuration
+    string actualTargetDir;
+    if (fNightlyFileName == ".")
+    {
+        char currentPath[FILENAME_MAX];
+        if (getcwd(currentPath, sizeof(currentPath)))
+            actualTargetDir = currentPath;
+    }
+    else
+        actualTargetDir = fNightlyFileName;
+    Message("Nightly Path: " + actualTargetDir);
+    if (fRunFileName == ".")
+    {
+        char currentPath[FILENAME_MAX];
+        if (getcwd(currentPath, sizeof(currentPath)))
+            actualTargetDir = currentPath;
+    }
+    else
+        actualTargetDir = fRunFileName;
+    Message("Run Path: " + actualTargetDir);
+    stringstream str;
+    str << "Run Number: " << fRunNumber;
+    Message(str.str());
+    Message("----------- OPENED FILES ----------------");
+    //print all the open files. 
+    if (fNightlyLogFile.is_open())
+        Message("Nightly Log..........OPEN");
+    else
+        Message("Nightly log........CLOSED");
+    if (fNightlyReportFile.is_open())
+        Message("Nightly Report.......OPEN");
+    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");
+    bool statWarning = false;
+    DataLoggerStats statVar;
+    calculateTotalSizeWritten(statVar, statWarning, false);
+    Message("---------------- STATS ------------------");
+    str.str("");
+    str << "Total Size written: " << statVar.sizeWritten << " bytes.";
         Message(str.str());
-	str.str("");
-	str << "Disk free space:    " << statVar.freeSpace   << " bytes.";
+    str.str("");
+    str << "Disk free space:    " << statVar.freeSpace   << " bytes.";
         Message(str.str());
     str.str("");
     str << "Statistics are updated every " << fStatsPeriodDuration << " seconds";
     if (fStatsPeriodDuration != 0)
-    	Message(str);
+        Message(str);
     else
-    	Message("Statistics updates are currently disabled");
-	Message("----------- DIM SUBSCRIPTIONS -----------");
-
-        str.str("");
-	str << "There are " << fNumSubAndFitsData.numSubscriptions << " active DIM subscriptions:";
-	Message(str.str());
-
-	for (std::map<const std::string, std::map<std::string, SubscriptionType>>::const_iterator it=fServiceSubscriptions.begin(); it!= fServiceSubscriptions.end();it++)
-	{
-		Message("Server "+it->first);
-		for (std::map<std::string, SubscriptionType>::const_iterator it2=it->second.begin(); it2!=it->second.end(); it2++)
-			Message(" -> "+it2->first);
-	}
-	if (fHasBlackList)
-	{
-	    Message("------------- BLOCK LIST ----------------");
-	    for (set<string>::iterator it=fBlackList.begin(); it != fBlackList.end(); it++)
-	        Message(*it);
-	}
-	if (fHasWhiteList)
-	{
-			Message("----------- ALLOW LIST ------------------");
-			for (set<string>::iterator it=fWhiteList.begin(); it != fWhiteList.end(); it++)
-			    Message(*it);
-	}
-
-	return GetCurrentState();
+        Message("Statistics updates are currently disabled");
+    Message("----------- DIM SUBSCRIPTIONS -----------");
+
+    str.str("");
+    str << "There are " << fNumSubAndFitsData.numSubscriptions << " active DIM subscriptions:";
+    Message(str.str());
+
+    for (map<const string, map<string, SubscriptionType> >::const_iterator it=fServiceSubscriptions.begin(); it!= fServiceSubscriptions.end();it++)
+    {
+        Message("Server "+it->first);
+        for (map<string, SubscriptionType>::const_iterator it2=it->second.begin(); it2!=it->second.end(); it2++)
+            Message(" -> "+it2->first);
+    }
+    if (fHasBlackList)
+    {
+        Message("------------- BLOCK LIST ----------------");
+        for (set<string>::iterator it=fBlackList.begin(); it != fBlackList.end(); it++)
+            Message(*it);
+    }
+    if (fHasWhiteList)
+    {
+            Message("----------- ALLOW LIST ------------------");
+            for (set<string>::iterator it=fWhiteList.begin(); it != fWhiteList.end(); it++)
+                Message(*it);
+    }
+    if (fGrouping.size() != 0)
+    {
+            Message("--------- GROUPING LIST -----------------");
+            Message("The following servers and/or services will be grouping under a single run fits file:");
+            for (set<string>::iterator it=fGrouping.begin(); it != fGrouping.end(); it++)
+                Message(*it);
+    }
+    Message("-----------------------------------------");
+    Message("------ END OF DATA LOGGER STATE ---------");
+    Message("-----------------------------------------");
+
+    return GetCurrentState();
 }
 
@@ -1274,21 +1251,21 @@
 //! turn debug mode on and off
 //! @param evt
-//!		the current event. contains the instruction string: On, Off, on, off, ON, OFF, 0 or 1
+//!        the current event. contains the instruction string: On, Off, on, off, ON, OFF, 0 or 1
 //! @returns 
-//!		the new state. Which, in that case, is the current state
+//!        the new state. Which, in that case, is the current state
 //!
 int DataLogger::SetDebugOnOff(const Event& evt)
 {
-	bool backupDebug = fDebugIsOn;
-	fDebugIsOn = evt.GetBool();
-	if (fDebugIsOn == backupDebug)
-		Warn("Warning: debug mode was already in the requested state");
-	else
-	{
-		stringstream str;
-		str << "Debug mode is now " << fDebugIsOn;
-		Message(str.str());
-	}
-	return GetCurrentState();
+    bool backupDebug = fDebugIsOn;
+    fDebugIsOn = evt.GetBool();
+    if (fDebugIsOn == backupDebug)
+        Warn("Warning: debug mode was already in the requested state");
+    else
+    {
+        stringstream str;
+        str << "Debug mode is now " << fDebugIsOn;
+        Message(str.str());
+    }
+    return GetCurrentState();
 }
 // --------------------------------------------------------------------------
@@ -1296,38 +1273,38 @@
 //! set the statistics update period duration. 0 disables the statistics
 //! @param evt
-//!		the current event. contains the new duration.
+//!        the current event. contains the new duration.
 //! @returns 
-//!		the new state. Which, in that case, is the current state
+//!        the new state. Which, in that case, is the current state
 //!
 int DataLogger::SetStatsPeriod(const Event& evt)
 {
-	float backupDuration = fStatsPeriodDuration;
-	fStatsPeriodDuration = evt.GetFloat();
-	if (fStatsPeriodDuration < 0)
-	{
-		Error("Statistics period duration should be greater than zero. Discarding provided value.");
-		fStatsPeriodDuration = backupDuration;
-		return GetCurrentState();	
-	}
-	if (fStatsPeriodDuration != fStatsPeriodDuration)
-	{
-		Error("Provided duration does not appear to be a valid float. discarding it.");
-		fStatsPeriodDuration = backupDuration;
-		return GetCurrentState();	
-	}
-	if (backupDuration == fStatsPeriodDuration)
-		Warn("Warning: statistics period was not modified: supplied value already in use");
-	else
-	{
-		if (fStatsPeriodDuration == 0.0f)
-			Message("Statistics are now OFF");
-		else
-		{
-			stringstream str;
-			str << "Statistics period is now " << fStatsPeriodDuration << " seconds";
-			Message(str.str()); 	
-		}	
-	}
-	return GetCurrentState();
+    float backupDuration = fStatsPeriodDuration;
+    fStatsPeriodDuration = evt.GetFloat();
+    if (fStatsPeriodDuration < 0)
+    {
+        Error("Statistics period duration should be greater than zero. Discarding provided value.");
+        fStatsPeriodDuration = backupDuration;
+        return GetCurrentState();    
+    }
+    if (fStatsPeriodDuration != fStatsPeriodDuration)
+    {
+        Error("Provided duration does not appear to be a valid float. discarding it.");
+        fStatsPeriodDuration = backupDuration;
+        return GetCurrentState();    
+    }
+    if (backupDuration == fStatsPeriodDuration)
+        Warn("Warning: statistics period was not modified: supplied value already in use");
+    else
+    {
+        if (fStatsPeriodDuration == 0.0f)
+            Message("Statistics are now OFF");
+        else
+        {
+            stringstream str;
+            str << "Statistics period is now " << fStatsPeriodDuration << " seconds";
+            Message(str.str());     
+        }    
+    }
+    return GetCurrentState();
 }
 // --------------------------------------------------------------------------
@@ -1335,22 +1312,22 @@
 //! set the opened files service on or off. 
 //! @param evt
-//!		the current event. contains the instruction string. similar to setdebugonoff
+//!        the current event. contains the instruction string. similar to setdebugonoff
 //! @returns 
-//!		the new state. Which, in that case, is the current state
+//!        the new state. Which, in that case, is the current state
 //!
 int DataLogger::SetOpenedFilesOnOff(const Event& evt)
 {
-	bool backupOpened = fOpenedFilesIsOn;
-	fOpenedFilesIsOn = evt.GetBool();
-	if (fOpenedFilesIsOn == backupOpened)
-		Warn("Warning: opened files service mode was already in the requested state");
-	else
-	{
-		stringstream str;
-		str << "Opened files service mode is now " << fOpenedFilesIsOn;
-		Message(str.str());
-	}
-	return GetCurrentState();
-	
+    bool backupOpened = fOpenedFilesIsOn;
+    fOpenedFilesIsOn = evt.GetBool();
+    if (fOpenedFilesIsOn == backupOpened)
+        Warn("Warning: opened files service mode was already in the requested state");
+    else
+    {
+        stringstream str;
+        str << "Opened files service mode is now " << fOpenedFilesIsOn;
+        Message(str.str());
+    }
+    return GetCurrentState();
+    
 }
 // --------------------------------------------------------------------------
@@ -1358,41 +1335,41 @@
 //! set the number of subscriptions and opened fits on and off
 //! @param evt
-//!		the current event. contains the instruction string. similar to setdebugonoff
+//!        the current event. contains the instruction string. similar to setdebugonoff
 //! @returns 
-//!		the new state. Which, in that case, is the current state
+//!        the new state. Which, in that case, is the current state
 //!
 int DataLogger::SetNumSubsAndFitsOnOff(const Event& evt)
 {
-	bool backupSubs = fNumSubAndFitsIsOn;
-	fNumSubAndFitsIsOn = evt.GetBool();
-	if (fNumSubAndFitsIsOn == backupSubs)
-		Warn("Warning: Number of subscriptions service mode was already in the requested state");
-	else
-	{
-		stringstream str;
-		str << "Number of subscriptions service mode is now " << fNumSubAndFitsIsOn;
-		Message(str.str());
-	}
-	return GetCurrentState();
-}
-// --------------------------------------------------------------------------
-//
-//!	Sets the path to use for the Nightly log file.
+    bool backupSubs = fNumSubAndFitsIsOn;
+    fNumSubAndFitsIsOn = evt.GetBool();
+    if (fNumSubAndFitsIsOn == backupSubs)
+        Warn("Warning: Number of subscriptions service mode was already in the requested state");
+    else
+    {
+        stringstream str;
+        str << "Number of subscriptions service mode is now " << fNumSubAndFitsIsOn;
+        Message(str.str());
+    }
+    return GetCurrentState();
+}
+// --------------------------------------------------------------------------
+//
+//!    Sets the path to use for the Nightly log file.
 //! @param evt
-//! 	the event transporting the path
+//!     the event transporting the path
 //! @returns
-//!		currently only the current state.
+//!        currently only the current state.
 //
 int DataLogger::ConfigureNightlyFileName(const Event& evt)
 {
-	if (evt.GetText() != NULL)
-	{
-		fNightlyFileName = std::string(evt.GetText());	
-		Message("New Nightly folder specified: " + fNightlyFileName);
-	}
-	else
-		Error("Empty Nightly folder given. Please specify a valid path.");
-
-	return GetCurrentState();
+    if (evt.GetText() != NULL)
+    {
+        fNightlyFileName = string(evt.GetText());
+        Message("New Nightly folder specified: " + fNightlyFileName);
+    }
+    else
+        Error("Empty Nightly folder given. Please specify a valid path.");
+
+    return GetCurrentState();
 }
 // --------------------------------------------------------------------------
@@ -1400,18 +1377,18 @@
 //! Sets the path to use for the run log file.
 //! @param evt
-//!		the event transporting the path
+//!        the event transporting the path
 //! @returns
-//! 	currently only the current state
+//!     currently only the current state
 int DataLogger::ConfigureRunFileName(const Event& evt)
 {
-	if (evt.GetText() != NULL)
-	{
-		fRunFileName = std::string(evt.GetText());
-		Message("New Run folder specified: " + fRunFileName);
-	}
-	else
-		Error("Empty Nightly folder given. Please specify a valid path");
-
-	return GetCurrentState();
+    if (evt.GetText() != NULL)
+    {
+        fRunFileName = string(evt.GetText());
+        Message("New Run folder specified: " + fRunFileName);
+    }
+    else
+        Error("Empty Nightly folder given. Please specify a valid path");
+
+    return GetCurrentState();
 }
 // --------------------------------------------------------------------------
@@ -1419,15 +1396,14 @@
 //! Sets the run number.
 //! @param evt
-//!		the event transporting the run number
+//!        the event transporting the run number
 //! @returns
-//! 	currently only the current state
-//TODO remove this function as the run numbers will be distributed through a dedicated service
+//!     currently only the current state
 int DataLogger::ConfigureRunNumber(const Event& evt)
 {
-	fRunNumber = evt.GetInt();
-	std::stringstream str;
-	str << "The new run number is: " << fRunNumber;
-	Message(str.str());
-	return GetCurrentState();
+    fRunNumber = evt.GetInt();
+    stringstream str;
+    str << "The new run number is: " << fRunNumber;
+    Message(str.str());
+    return GetCurrentState();
 }
 // --------------------------------------------------------------------------
@@ -1435,27 +1411,27 @@
 //! Notifies the DIM service that a particular file was opened
 //! @ param name the base name of the opened file, i.e. without path nor extension. 
-//! 	WARNING: use string instead of string& because I pass values that do not convert to string&.
-//!		this is not a problem though because file are not opened so often.
+//!     WARNING: use string instead of string& because I pass values that do not convert to string&.
+//!        this is not a problem though because file are not opened so often.
 //! @ param type the type of the opened file. 0 = none open, 1 = log, 2 = text, 4 = fits
-inline void DataLogger::NotifyOpenedFile(std::string name, int type, DimDescribedService* service)
-{
-	if (fOpenedFilesIsOn)
-	{
-		if (fDebugIsOn)
-		{
-			stringstream str;
-			str << "Updating files service " << service->getName() << "with code: " << type << " and file: " << name;
-			Debug(str.str());
-			str.str("");
-			str << "Num subs: " << fNumSubAndFitsData.numSubscriptions << " Num open FITS: " << fNumSubAndFitsData.numOpenFits;
-			Debug(str.str());
-		}
-		OpenFileToDim fToDim;
-		fToDim.code = type;
-		memcpy(fToDim.fileName, name.c_str(), name.size()+1);
-		service->setData(reinterpret_cast<void*>(&fToDim), name.size()+1+sizeof(int));
-		service->setQuality(0);
-		service->updateService();
-	}
+inline void DataLogger::NotifyOpenedFile(string name, int type, DimDescribedService* service)
+{
+    if (fOpenedFilesIsOn)
+    {
+        if (fDebugIsOn)
+        {
+            stringstream str;
+            str << "Updating files service " << service->getName() << "with code: " << type << " and file: " << name;
+            Debug(str.str());
+            str.str("");
+            str << "Num subs: " << fNumSubAndFitsData.numSubscriptions << " Num open FITS: " << fNumSubAndFitsData.numOpenFits;
+            Debug(str.str());
+        }
+        OpenFileToDim fToDim;
+        fToDim.code = type;
+        memcpy(fToDim.fileName, name.c_str(), name.size()+1);
+        service->setData(reinterpret_cast<void*>(&fToDim), name.size()+1+sizeof(int));
+        service->setQuality(0);
+        service->updateService();
+    }
 }
 // --------------------------------------------------------------------------
@@ -1465,72 +1441,74 @@
 //! and tries to open it.
 //! @returns 
-//!		kSM_NightlyOpen if success, kSM_BadNightlyConfig if failure
+//!        kSM_NightlyOpen if success, kSM_BadNightlyConfig if failure
 int DataLogger::StartPlease()
 {
-	if (fDebugIsOn)
-	{
-		Debug("Starting...");	
-	}
-	Time time;
-	std::stringstream sTime;
-	sTime << time.Y() << "_" << time.M() << "_" << time.D();
-
-	fFullNightlyLogFileName = fNightlyFileName + '/' + sTime.str() + ".log"; 
-	fNightlyLogFile.open(fFullNightlyLogFileName.c_str(), std::ios_base::out | std::ios_base::app); 
-	if (errno != 0)
-	{
-		std::stringstream str;
-		str << "Unable to open Nightly Log " << fFullNightlyLogFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-		Error(str);	
-	}
-	fFullNightlyReportFileName = fNightlyFileName + '/' + sTime.str() + ".rep";
-	fNightlyReportFile.open(fFullNightlyReportFileName.c_str(), std::ios_base::out | std::ios_base::app);
-	if (errno != 0)
-	{
-		std::stringstream str;
-		str << "Unable to open Nightly Report " << fFullNightlyReportFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-		Error(str);	
-	}
-
-	if (!fNightlyLogFile.is_open() || !fNightlyReportFile.is_open())
-	{	
-		//TODO send an error message	
-	    return kSM_BadNightlyConfig;
-	}
-	//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;
-	fFileSizesMap.clear();
-	fBaseSizeRun = 0;
-	fPreviousSize = 0;
-	//notify that files were opened
-	std::string actualTargetDir;
-	if (fNightlyFileName == ".")
-	{
-		char currentPath[FILENAME_MAX];
-                if (!getcwd(currentPath, sizeof(currentPath)))
-                {
-                    if (errno != 0)
-                    {
-                        std::stringstream str;
-                        str << "Unable retrieve current path" << ". Reason: " << strerror(errno) << " [" << errno << "]";
-                        Error(str);
-                    }
-                }
-                actualTargetDir = currentPath;
-	}
-	else
-	{
-		actualTargetDir = fNightlyFileName;	
-	}
-	//notify that a new file has been opened.
-	NotifyOpenedFile(actualTargetDir + '/' + sTime.str(), 3, fOpenedNightlyFiles);
-
-	fOpenedNightlyFits.clear();
-	
-	return kSM_NightlyOpen; 	
+    if (fDebugIsOn)
+    {
+        Debug("Starting...");    
+    }
+    Time time;
+    stringstream sTime;
+    sTime << time.Y() << "_" << time.M() << "_" << time.D();
+
+    fFullNightlyLogFileName = fNightlyFileName + '/' + sTime.str() + ".log"; 
+    fNightlyLogFile.open(fFullNightlyLogFileName.c_str(), ios_base::out | ios_base::app);
+    if (errno != 0)
+    {
+        stringstream str;
+        str << "Unable to open Nightly Log " << fFullNightlyLogFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
+        Error(str);    
+    }
+    fFullNightlyReportFileName = fNightlyFileName + '/' + sTime.str() + ".rep";
+    fNightlyReportFile.open(fFullNightlyReportFileName.c_str(), ios_base::out | ios_base::app);
+    if (errno != 0)
+    {
+        stringstream str;
+        str << "Unable to open Nightly Report " << fFullNightlyReportFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
+        Error(str);    
+    }
+
+    if (!fNightlyLogFile.is_open() || !fNightlyReportFile.is_open())
+    {    
+        stringstream str;
+        str << "Something went wrong while openning nightly files " << fFullNightlyLogFileName << " and " << fFullNightlyReportFileName;
+        Error(str.str());
+        return kSM_BadNightlyConfig;
+    }
+    //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;
+    fFileSizesMap.clear();
+    fBaseSizeRun = 0;
+    fPreviousSize = 0;
+    //notify that files were opened
+    string actualTargetDir;
+    if (fNightlyFileName == ".")
+    {
+        char currentPath[FILENAME_MAX];
+        if (!getcwd(currentPath, sizeof(currentPath)))
+        {
+            if (errno != 0)
+            {
+                stringstream str;
+                str << "Unable retrieve current path" << ". Reason: " << strerror(errno) << " [" << errno << "]";
+                Error(str);
+            }
+        }
+        actualTargetDir = currentPath;
+    }
+    else
+    {
+        actualTargetDir = fNightlyFileName;    
+    }
+    //notify that a new file has been opened.
+    NotifyOpenedFile(actualTargetDir + '/' + sTime.str(), 3, fOpenedNightlyFiles);
+
+    fOpenedNightlyFits.clear();
+    
+    return kSM_NightlyOpen;     
 }
 
@@ -1540,202 +1518,232 @@
 //! open if required a the FITS files corresponding to a given subscription
 //! @param sub
-//! 	the current DimInfo subscription being examined
+//!     the current DimInfo subscription being examined
 void DataLogger::OpenFITSFilesPlease(SubscriptionType& sub)
 {
-	std::string serviceName(sub.dimInfo->getName());
-	for (unsigned int i=0;i<serviceName.size(); i++)
-	{
-		if (serviceName[i] == '/')
-		{
-			serviceName[i] = '_';
-			break;	
-		}	
-	}
-	Time time;
-	std::stringstream 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())
-	{
-	    std::string fileNameOnly = sTime.str() + '_' + serviceName + ".fits";
-		std::string partialName = fNightlyFileName + '/' + fileNameOnly;
-		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.
-			fOpenedNightlyFits[fileNameOnly] =  serviceName;
-		}
-		sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this);//Out());
-
-		//notify the opening
-		std::string actualTargetDir;
-		if (fNightlyFileName == ".")
-		{
-			char currentPath[FILENAME_MAX];
-			if (getcwd(currentPath, sizeof(currentPath)))
-                            actualTargetDir = currentPath;
-		}
-		else
-		{
-			actualTargetDir = fNightlyFileName;	
-		}		
-		NotifyOpenedFile(actualTargetDir + '/' + sTime.str(), 7, fOpenedNightlyFiles);
-		if (fNumSubAndFitsIsOn)
-			fNumSubAndFits->updateService();
-		if (fDebugIsOn)
-		{
-			stringstream str;
-			str << "Opened Nightly FITS: " << partialName << " and table: FACT-" << serviceName << ".current number of opened FITS: " << fNumSubAndFitsData.numOpenFits;
-			Debug(str.str());	
-		}
-	}
-	if (!sub.runFile.IsOpen() && (GetCurrentState() == kSM_Logging))
-	{//buffer for the run file have already been allocated when doing the Nightly file
-		std::stringstream sRun;
-		sRun << fRunNumber;
-#ifdef ONE_RUN_FITS_ONLY
-		std::string fileNameOnly = sRun.str() + ".fits";
-		std::string partialName = fRunFileName + '/' + fileNameOnly;
-		if (fRunFitsFile == NULL)
-		{
-#else
-		std::string fileNameOnly = sRun.str() + '_' + serviceName + ".fits";
-		std::string partialName = fRunFileName + '/' + fileNameOnly;
-#endif
-			//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;	
-				fOpenedRunFits[fileNameOnly] = serviceName;
-			}
-#ifdef ONE_RUN_FITS_ONLY
-			try
-			{
-				fRunFitsFile = new CCfits::FITS(partialName, CCfits::RWmode::Write);
-				(fNumSubAndFitsData.numOpenFits)++;
-			}	
-			catch (CCfits::FitsError e)
-			{
-				std::stringstream str;
-				str << "Could not open FITS Run file " << partialName << " reason: " << e.message();
-				Error(str);
-				fRunFitsFile = NULL;
-			}
-#endif
-			std::string actualTargetDir;
-			if (fRunFileName == ".")
-			{
-				char currentPath[FILENAME_MAX];
-                                if (getcwd(currentPath, sizeof(currentPath)))
-                                    actualTargetDir = currentPath;
-			}
-			else
-			{
-				actualTargetDir = fRunFileName;	
-			}		
-			NotifyOpenedFile(actualTargetDir + '/' + sRun.str(), 7, fOpenedRunFiles);// + '_' + serviceName, 4);
-#ifdef ONE_RUN_FITS_ONLY
-		}
-		sub.runFile.Open(partialName, serviceName, fRunFitsFile, &fNumSubAndFitsData.numOpenFits, this);//Out());
-#else
-		sub.runFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this);//Out());
-#endif //one_run_fits_only
-	   if (fNumSubAndFitsIsOn)
-		   fNumSubAndFits->updateService();
-	   	if (fDebugIsOn)
-		{
-			stringstream str;
-			str << "Opened Run FITS: " << partialName << " and table: FACT-" << serviceName << ".current number of opened FITS: " << fNumSubAndFitsData.numOpenFits;
-			Debug(str.str());	
-		}
-	}
-}	
+    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 _
+    bool hasGrouping = false;
+    if (!sub.runFile.IsOpen() && (GetCurrentState() == kSM_Logging))
+    {//will we find this service in the grouping list ?
+        for (set<string>::iterator it=fGrouping.begin(); it!=fGrouping.end(); it++)
+        {
+            if (serviceName.find(*it) != string::npos)
+            {
+                hasGrouping = true;
+                break;
+            }
+        }
+    }
+    for (unsigned int i=0;i<serviceName.size(); i++)
+    {
+        if (serviceName[i] == '/')
+        {
+            serviceName[i] = '_';
+            break;    
+        }    
+    }
+    Time time;
+    stringstream 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 = fNightlyFileName + '/' + fileNameOnly;
+        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.
+            fOpenedNightlyFits[fileNameOnly].push_back(serviceName);
+        }
+        sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this);//Out());
+
+        //notify the opening
+        string actualTargetDir;
+        if (fNightlyFileName == ".")
+        {
+            char currentPath[FILENAME_MAX];
+            if (getcwd(currentPath, sizeof(currentPath)))
+                actualTargetDir = currentPath;
+        }
+        else
+        {
+            actualTargetDir = fNightlyFileName;    
+        }        
+        NotifyOpenedFile(actualTargetDir + '/' + sTime.str(), 7, fOpenedNightlyFiles);
+        if (fNumSubAndFitsIsOn)
+            fNumSubAndFits->updateService();
+        if (fDebugIsOn)
+        {
+            stringstream str;
+            str << "Opened Nightly FITS: " << partialName << " and table: FACT-" << serviceName << ".current number of opened FITS: " << fNumSubAndFitsData.numOpenFits;
+            Debug(str.str());    
+        }
+    }
+    if (!sub.runFile.IsOpen() && (GetCurrentState() == kSM_Logging))
+    {//buffer for the run file have already been allocated when doing the Nightly file
+        stringstream sRun;
+        sRun << fRunNumber;
+        string fileNameOnly;
+        string partialName;
+
+        if (hasGrouping)
+        {
+            fileNameOnly = sRun.str() + "_group.fits";
+            partialName = fRunFileName + '/' + fileNameOnly;
+        }
+        else
+        {
+            fileNameOnly = sRun.str() + '_' + serviceName + ".fits";
+            partialName = fRunFileName + '/' + fileNameOnly;
+        }
+        //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;
+            fOpenedRunFits[fileNameOnly].push_back(serviceName);
+        }
+        else
+            if (hasGrouping)
+            {//most likely I should add this service name.
+             //the only case for which I should not add it is if a service disapeared, hence the file was closed
+             //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++)
+                    if (*it == serviceName)
+                    {
+                        found = true;
+                        break;
+                    }
+                if (!found)
+                    fOpenedRunFits[fileNameOnly].push_back(serviceName);
+            }
+        if (hasGrouping && fRunFitsFile == NULL)
+            try
+            {
+                fRunFitsFile = new CCfits::FITS(partialName, CCfits::RWmode::Write);
+                (fNumSubAndFitsData.numOpenFits)++;
+            }    
+            catch (CCfits::FitsError e)
+            {
+                stringstream str;
+                str << "Could not open FITS Run file " << partialName << " reason: " << e.message();
+                Error(str);
+                fRunFitsFile = NULL;
+            }
+
+        string actualTargetDir;
+        if (fRunFileName == ".")
+        {
+            char currentPath[FILENAME_MAX];
+            if (getcwd(currentPath, sizeof(currentPath)))
+                actualTargetDir = currentPath;
+        }
+        else
+        {
+           actualTargetDir = fRunFileName;
+        }
+        NotifyOpenedFile(actualTargetDir + '/' + sRun.str(), 7, fOpenedRunFiles);// + '_' + serviceName, 4);
+        if (hasGrouping)
+            sub.runFile.Open(partialName, serviceName, fRunFitsFile, &fNumSubAndFitsData.numOpenFits, this);//Out());
+        else
+            sub.runFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this);//Out());
+
+       if (fNumSubAndFitsIsOn)
+           fNumSubAndFits->updateService();
+           if (fDebugIsOn)
+        {
+            stringstream str;
+            str << "Opened Run FITS: " << partialName << " and table: FACT-" << serviceName << ".current number of opened FITS: " << fNumSubAndFitsData.numOpenFits;
+            Debug(str.str());    
+        }
+    }
+}    
 // --------------------------------------------------------------------------
 //
 void DataLogger::AllocateFITSBuffers(SubscriptionType& sub)
 {
-	int size = sub.dimInfo->getSize();
-	 
-	//Init the time columns of the file
-	Description dateDesc(std::string("Time"), std::string("Modified Julian Date"), std::string("MjD"));
-	sub.nightlyFile.AddStandardColumn(dateDesc, "1D", &fMjD, sizeof(double));
-	sub.runFile.AddStandardColumn(dateDesc, "1D", &fMjD, sizeof(double));
-
-	Description QoSDesc("Qos", "Quality of service", "None");
-	sub.nightlyFile.AddStandardColumn(QoSDesc, "1J", &fQuality, sizeof(int));
-	sub.runFile.AddStandardColumn(QoSDesc, "1J", &fQuality, sizeof(int));
-
-	const Converter::FormatList flist = sub.fConv->GetList();
+    int size = sub.dimInfo->getSize();
+     
+    //Init the time columns of the file
+    Description dateDesc(string("Time"), string("Modified Julian Date"), string("MjD"));
+    sub.nightlyFile.AddStandardColumn(dateDesc, "1D", &fMjD, sizeof(double));
+    sub.runFile.AddStandardColumn(dateDesc, "1D", &fMjD, sizeof(double));
+
+    Description QoSDesc("Qos", "Quality of service", "None");
+    sub.nightlyFile.AddStandardColumn(QoSDesc, "1J", &fQuality, sizeof(int));
+    sub.runFile.AddStandardColumn(QoSDesc, "1J", &fQuality, sizeof(int));
+
+    const Converter::FormatList flist = sub.fConv->GetList();
     // Compilation failed
     if (flist.empty() || flist.back().first.second!=0)
     {
-    	Error("Compilation of format string failed.");
+        Error("Compilation of format string failed.");
         return;
     }
 
-	//we've got a nice structure describing the format of this service's messages.
-	//Let's create the appropriate FITS columns
-	std::vector<std::string> dataFormatsLocal;
-	for (unsigned int i=0;i<flist.size()-1;i++)
-	{
-	 	std::stringstream dataQualifier; 
-
-	 	dataQualifier << flist[i].second.first;
-	 	switch (flist[i].first.first->name()[0])
-	 	{//TODO handle all the data format cases
-	 		case 'c':
-	 		case 'C':
-	 			dataQualifier.str("S");
-	 		break;
-	 		case 's':
-	 			dataQualifier << "I";
-	 		break;
-	 		case 'i':
-	 		case 'I':
-	 			dataQualifier << "J";
-	 		break;
-	 		case 'l':
-	 		case 'L':
-	 			dataQualifier << "J";
-	 			//TODO triple check that in FITS, long = int
-	 		break;
-	 		case 'f':
-	 		case 'F':
-	 			dataQualifier << "E";
-	 		break;
-	 		case 'd':
-	 		case 'D':
-	 			dataQualifier << "D";
-	 		break;
-	 		case 'x':
-	 		case 'X':
-	 			dataQualifier << "K";
-	 		break;
-	 		case 'S':
-	 			//for strings, the number of elements I get is wrong. Correct it
-	 			dataQualifier.str(""); //clear
-	 			dataQualifier << size-1 <<  "A";
-	 			size = size-1;
-	 		break;
-	 		
-	 		default:
-	 			Fatal("THIS SHOULD NEVER BE REACHED. dataLogger.cc ln 1198.");
-	 	};
-	 	//we skip the variable length strings for now (in fits only)
-	 	if (dataQualifier.str() != "S")
-		 	dataFormatsLocal.push_back(dataQualifier.str());
-	 }
-	 sub.nightlyFile.InitDataColumns(fServiceList.GetDescriptions(sub.dimInfo->getName()), dataFormatsLocal, sub.dimInfo->getData(), size);
-	 sub.runFile.InitDataColumns(fServiceList.GetDescriptions(sub.dimInfo->getName()), dataFormatsLocal, sub.dimInfo->getData(), size);
+    //we've got a nice structure describing the format of this service's messages.
+    //Let's create the appropriate FITS columns
+    vector<string> dataFormatsLocal;
+    for (unsigned int i=0;i<flist.size()-1;i++)
+    {
+         stringstream dataQualifier;
+
+         dataQualifier << flist[i].second.first;
+         switch (flist[i].first.first->name()[0])
+         {
+             case 'c':
+             case 'C':
+                 dataQualifier.str("S");
+             break;
+             case 's':
+                 dataQualifier << "I";
+             break;
+             case 'i':
+             case 'I':
+                 dataQualifier << "J";
+             break;
+             case 'l':
+             case 'L':
+                 dataQualifier << "J";
+             break;
+             case 'f':
+             case 'F':
+                 dataQualifier << "E";
+             break;
+             case 'd':
+             case 'D':
+                 dataQualifier << "D";
+             break;
+             case 'x':
+             case 'X':
+                 dataQualifier << "K";
+             break;
+             case 'S':
+                 //for strings, the number of elements I get is wrong. Correct it
+                 dataQualifier.str(""); //clear
+                 dataQualifier << size-1 <<  "A";
+                 size = size-1;
+             break;
+             
+             default:
+                 Fatal("THIS SHOULD NEVER BE REACHED. dataLogger.cc ln 1198.");
+         };
+         //we skip the variable length strings for now (in fits only)
+         if (dataQualifier.str() != "S")
+             dataFormatsLocal.push_back(dataQualifier.str());
+     }
+     sub.nightlyFile.InitDataColumns(fServiceList.GetDescriptions(sub.dimInfo->getName()), dataFormatsLocal, sub.dimInfo->getData(), size);
+     sub.runFile.InitDataColumns(fServiceList.GetDescriptions(sub.dimInfo->getName()), dataFormatsLocal, sub.dimInfo->getData(), size);
 }
 // --------------------------------------------------------------------------
@@ -1745,21 +1753,21 @@
 void DataLogger::WriteToFITS(SubscriptionType& sub)
 {
-		//nightly File status (open or not) already checked
-		if (sub.nightlyFile.IsOpen())
-		{
-			sub.nightlyFile.Write(sub.fConv);
-			if (fDebugIsOn)
-			{
-				Debug("Writing to nightly FITS " + sub.nightlyFile.fFileName);	
-			}
-		}
-		if (sub.runFile.IsOpen())
-		{
-			sub.runFile.Write(sub.fConv);
-			if (fDebugIsOn)
-			{
-				Debug("Writing to Run FITS " + sub.runFile.fFileName);	
-			}
-		}
+        //nightly File status (open or not) already checked
+        if (sub.nightlyFile.IsOpen())
+        {
+            sub.nightlyFile.Write(sub.fConv);
+            if (fDebugIsOn)
+            {
+                Debug("Writing to nightly FITS " + sub.nightlyFile.fFileName);    
+            }
+        }
+        if (sub.runFile.IsOpen())
+        {
+            sub.runFile.Write(sub.fConv);
+            if (fDebugIsOn)
+            {
+                Debug("Writing to Run FITS " + sub.runFile.fFileName);    
+            }
+        }
 }
 #endif //if has_fits
@@ -1770,104 +1778,121 @@
 //! and tries to open it.
 //! @returns
-//!		kSM_Logging if success, kSM_BadRunConfig if failure.
+//!        kSM_Logging if success, kSM_BadRunConfig if failure.
 int DataLogger::StartRunPlease()
 {
-	if (fDebugIsOn)
-	{
-		Debug("Starting Run Logging...");	
-	}
-	//attempt to open run file with current parameters
-//	if (fRunNumber == -1)
-//		return kSM_BadRunConfig;
-	std::stringstream sRun;
-	sRun << fRunNumber;
-	fFullRunLogFileName = fRunFileName + '/' + sRun.str() + ".log";
-	fRunLogFile.open(fFullRunLogFileName.c_str(), std::ios_base::out | std::ios_base::app); //maybe should be app instead of ate
-	if (errno != 0)
-	{
-		std::stringstream str;
-		str << "Unable to open run Log " << fFullRunLogFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-		Error(str);	
-	}
-	fFullRunReportFileName = fRunFileName + '/' + sRun.str() + ".rep";
-	fRunReportFile.open(fFullRunReportFileName.c_str(), std::ios_base::out | std::ios_base::app);
-	if (errno != 0)
-	{
-		std::stringstream str;
-		str << "Unable to open run report " << fFullRunReportFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-		Error(str);	
-	}
-	
-	if (!fRunLogFile.is_open() || !fRunReportFile.is_open())
-	{
-		//TODO send an error message
-		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)
-		{
-			std::stringstream 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)
-		{
-			std::stringstream str;
-			str << "Unable to stat " << fFullRunReportFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
-			Error(str);	
-		}
-		else
-			fBaseSizeRun += st.st_size;
-		fFileSizesMap[fFullRunReportFileName] = 0;
-	}
-	std::string actualTargetDir;
-	if (fRunFileName == ".")
-	{
-		char currentPath[FILENAME_MAX];
-                if (!getcwd(currentPath, sizeof(currentPath)))
-                {
-                    if (errno != 0)
-                    {
-                        std::stringstream 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();
-
-	return kSM_Logging;
+    if (fDebugIsOn)
+    {
+        Debug("Starting Run Logging...");    
+    }
+    //attempt to open run file with current parameters
+//    if (fRunNumber == -1)
+//        return kSM_BadRunConfig;
+    stringstream 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)
+    {
+        stringstream 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)
+    {
+        stringstream str;
+        str << "Unable to open run report " << fFullRunReportFileName << ". Reason: " << strerror(errno) << " [" << errno << "]";
+        Error(str);    
+    }
+    
+    if (!fRunLogFile.is_open() || !fRunReportFile.is_open())
+    {
+        stringstream 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)
+        {
+            stringstream 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)
+        {
+            stringstream 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)
+            {
+                stringstream 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();
+
+    return kSM_Logging;
 }
 #ifdef HAVE_FITS
 void DataLogger::CreateFitsGrouping(bool runGroup)
 {
-    //create the FITS group corresponding to the ending run.ETIENNEICI
+    if (fDebugIsOn)
+    {
+        stringstream str;
+        str << "Creating fits group for ";
+        if (runGroup)
+            str << "run files";
+        else
+            str << "nightly files";
+        Debug(str.str());
+    }
+    //create the FITS group corresponding to the ending run.
     CCfits::FITS* groupFile;
-
-#ifdef ONE_RUN_FITS_ONLY
-    if (runGroup)
-        return;
-#endif
-    map<string, string>& filesToGroup = runGroup? fOpenedRunFits : fOpenedNightlyFits;
-    unsigned int numFilesToGroup = filesToGroup.size();
+    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++)
+    {
+        numFilesToGroup += it->second.size();
+    }
+    if (fDebugIsOn)
+    {
+        stringstream str;
+        str << "There are " << numFilesToGroup << " tables to group";
+        Debug(str.str());
+    }
     if (numFilesToGroup <= 1)
     {
@@ -1883,14 +1908,15 @@
     {
         Time time;
-        std::stringstream sTime;
+        stringstream sTime;
         sTime << time.Y() << "_" << time.M() << "_" << time.D();
 
         groupName << fNightlyFileName << '/' << sTime.str() << ".fits";
     }
+    CCfits::Table* groupTable;
+    int maxCharLength = 50;//FILENAME_MAX;
     try
     {
         groupFile = new CCfits::FITS(groupName.str(), CCfits::RWmode::Write);
         //setup the column names
-        int maxCharLength = FILENAME_MAX;
         stringstream pathTypeName;
         pathTypeName << maxCharLength << "A";
@@ -1905,9 +1931,10 @@
         names.push_back("MEMBER_NAME");
         dataTypes.push_back(pathTypeName.str());
-        CCfits::Table* groupTable = groupFile->addTable("GROUPING", numFilesToGroup, names, dataTypes);
+
+        groupTable = groupFile->addTable("GROUPING", numFilesToGroup, names, dataTypes);
      }
      catch (CCfits::FitsError e)
      {
-         std::stringstream str;
+         stringstream str;
          str << "Could not open or create FITS table GROUPING in  file " << groupName.str() << " reason: " << e.message();
          Error(str);
@@ -1920,24 +1947,34 @@
     //create appropriate buffer.
     unsigned char* fitsBuffer = new unsigned char[8 + 3 + 2*maxCharLength + 1]; //+1 for trailling character
+    memset(fitsBuffer, 0, 8 + 3 + 2*maxCharLength + 1);
     char* startOfExtension = reinterpret_cast<char*>(fitsBuffer);
     char* startOfURI = reinterpret_cast<char*>(&fitsBuffer[8]);
     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");
     int i=1;
-    for (map<string, string>::iterator it=filesToGroup.begin(); it!=filesToGroup.end(); it++, i++)
-    {
-        strcpy(startOfLocation, it->first.c_str());
-        strcpy(startOfName, it->second.c_str());
-        int status = 0;
-        fits_write_tblbytes(groupFile->fitsPointer(), i, 1, 8+3+2*maxCharLength, fitsBuffer, &status);
-        if (status)
-        {
-            stringstream str;
-            str << "Could not write row #" << i << "In the fits grouping file " << groupName << ". Cfitsio error code: " << status;
-            Error(str.str());
-        }
-    }
+    for (map<string, vector<string> >::iterator it=filesToGroup.begin(); it!=filesToGroup.end(); it++)
+        for (vector<string>::iterator jt=it->second.begin(); jt != it->second.end(); jt++, i++)
+        {
+            strcpy(startOfLocation, it->first.c_str());
+            strcpy(startOfName, jt->c_str());
+            if (fDebugIsOn)
+            {
+                stringstream str;
+                str << "Grouping " << it->first << " " << *jt;
+                Debug(str.str());
+            }
+ //           strcpy(startOfNameVisible, jt->c_str());
+            int status = 0;
+            fits_write_tblbytes(groupFile->fitsPointer(), i, 1, 8+3+2*maxCharLength, fitsBuffer, &status);
+            if (status)
+            {
+                stringstream str;
+                str << "Could not write row #" << i << "In the fits grouping file " << groupName << ". Cfitsio error code: " << status;
+                Error(str.str());
+            }
+        }
 
     filesToGroup.clear();
@@ -1950,41 +1987,39 @@
 //! Attempts to close the run file.
 //! @returns
-//!		kSM_WaitingRun if success, kSM_FatalError otherwise
+//!        kSM_WaitingRun if success, kSM_FatalError otherwise
 int DataLogger::StopRunPlease()
 {
 
-	if (fDebugIsOn)
-	{
-		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();
+    if (fDebugIsOn)
+    {
+        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();
 #ifdef HAVE_FITS
-	for (SubscriptionsListType::iterator i = fServiceSubscriptions.begin(); i != fServiceSubscriptions.end(); i++)
-		for (std::map<std::string, SubscriptionType>::iterator j = i->second.begin(); j != i->second.end(); j++)
-		{
-				if (j->second.runFile.IsOpen())
-					j->second.runFile.Close();	
-		}
-#ifdef ONE_RUN_FITS_ONLY
-	if (fRunFitsFile != NULL)
-	{
-		delete fRunFitsFile;
-		fRunFitsFile = NULL;	
-		(fNumSubAndFitsData.numOpenFits)--;
-	}
+    for (SubscriptionsListType::iterator i = fServiceSubscriptions.begin(); i != fServiceSubscriptions.end(); i++)
+        for (map<string, SubscriptionType>::iterator j = i->second.begin(); j != i->second.end(); j++)
+        {
+            if (j->second.runFile.IsOpen())
+                j->second.runFile.Close();
+        }
+    if (fRunFitsFile != NULL)
+    {
+        delete fRunFitsFile;
+        fRunFitsFile = NULL;    
+        (fNumSubAndFitsData.numOpenFits)--;
+    }
 #endif
-#endif
-	NotifyOpenedFile("", 0, fOpenedRunFiles);
-	if (fNumSubAndFitsIsOn)
-		fNumSubAndFits->updateService();
-
-	CreateFitsGrouping(true);
-
-	return kSM_WaitingRun;
+    NotifyOpenedFile("", 0, fOpenedRunFiles);
+    if (fNumSubAndFitsIsOn)
+        fNumSubAndFits->updateService();
+
+    CreateFitsGrouping(true);
+
+    return kSM_WaitingRun;
 
 }
@@ -1994,53 +2029,51 @@
 //! Attempts to close any openned file.
 //! @returns
-//! 	kSM_Ready
+//!     kSM_Ready
 int DataLogger::GoToReadyPlease()
 {
-	if (fDebugIsOn)
-	{
-		Debug("Going to the Ready state...");
-	}	
-	if (fNightlyLogFile.is_open())
-		fNightlyLogFile.close();
-	if (fNightlyReportFile.is_open())
-		fNightlyReportFile.close();
-
-	if (fRunLogFile.is_open())
-		fRunLogFile.close();
-	if (fRunReportFile.is_open())
-		fRunReportFile.close();
-		
+    if (fDebugIsOn)
+    {
+        Debug("Going to the Ready state...");
+    }    
+    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
-	for (SubscriptionsListType::iterator i = fServiceSubscriptions.begin(); i != fServiceSubscriptions.end(); i++)
-		for (std::map<std::string, SubscriptionType>::iterator j = i->second.begin(); j != i->second.end(); j++)
-		{
-				if (j->second.nightlyFile.IsOpen())
-					j->second.nightlyFile.Close();
-				if (j->second.runFile.IsOpen())
-					j->second.runFile.Close();	
-		}
-#ifdef ONE_RUN_FITS_ONLY
-	if (fRunFitsFile != NULL)
-	{
-		delete fRunFitsFile;
-		fRunFitsFile = NULL;
-		(fNumSubAndFitsData.numOpenFits)--;
-	}
+    for (SubscriptionsListType::iterator i = fServiceSubscriptions.begin(); i != fServiceSubscriptions.end(); i++)
+        for (map<string, SubscriptionType>::iterator j = i->second.begin(); j != i->second.end(); j++)
+        {
+            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)--;
+    }
 #endif
-#endif
-	if (GetCurrentState() == kSM_Logging)
-		NotifyOpenedFile("", 0, fOpenedRunFiles);
-	if (GetCurrentState() == kSM_Logging || 
-	    GetCurrentState() == kSM_WaitingRun || 
-	    GetCurrentState() == kSM_NightlyOpen)
-	{ 
-		NotifyOpenedFile("", 0, fOpenedNightlyFiles);
-		if (fNumSubAndFitsIsOn)
-			fNumSubAndFits->updateService();
-	}
-	CreateFitsGrouping(true);
-	CreateFitsGrouping(false);
-
-	return kSM_Ready;
+    if (GetCurrentState() == kSM_Logging)
+        NotifyOpenedFile("", 0, fOpenedRunFiles);
+    if (GetCurrentState() == kSM_Logging || 
+        GetCurrentState() == kSM_WaitingRun || 
+        GetCurrentState() == kSM_NightlyOpen)
+    { 
+        NotifyOpenedFile("", 0, fOpenedNightlyFiles);
+        if (fNumSubAndFitsIsOn)
+            fNumSubAndFits->updateService();
+    }
+    CreateFitsGrouping(true);
+    CreateFitsGrouping(false);
+
+    return kSM_Ready;
 }
 // --------------------------------------------------------------------------
@@ -2048,13 +2081,13 @@
 //! Implements the transition towards kSM_WaitingRun
 //! Does nothing really.
-//!	@returns
-//!		kSM_WaitingRun
+//!    @returns
+//!        kSM_WaitingRun
 int DataLogger::NightlyToWaitRunPlease()
 {
-	if (fDebugIsOn)
-	{
-		Debug("Going to Wait Run Number state...");	
-	}
-	return kSM_WaitingRun;	
+    if (fDebugIsOn)
+    {
+        Debug("Going to Wait Run Number state...");    
+    }
+    return kSM_WaitingRun;    
 }
 
@@ -2063,38 +2096,52 @@
     fDebugIsOn = conf.Get<bool>("debug");
 
-	//Set the block or allow list
-	fBlackList.clear();
-	fWhiteList.clear();
+    //Set the block or allow list
+    fBlackList.clear();
+    fWhiteList.clear();
     if (conf.Has("block"))
     {
-    	vector<string> vec = conf.Get<vector<string>>("block");
-    	if (vec.size() != 0)
-    	{
-    		fHasBlackList = true;
-    		if (fDebugIsOn)
-				Debug("Setting BLOCK list:");
-    	}
-    	for (vector<string>::iterator it = vec.begin(); it != vec.end(); it++)
-    	{
-    	    fBlackList.insert(*it);
-    	    if (fDebugIsOn)
-    	        Debug("                   " + *it);
-    	}
+        vector<string> vec = conf.Get<vector<string>>("block");
+        if (vec.size() != 0)
+        {
+            fHasBlackList = true;
+            if (fDebugIsOn)
+                Debug("Setting BLOCK list:");
+        }
+        for (vector<string>::iterator it = vec.begin(); it != vec.end(); it++)
+        {
+            fBlackList.insert(*it);
+            if (fDebugIsOn)
+                Debug("                   " + *it);
+        }
     }
     if (conf.Has("allow"))
     {
-    	vector<string> vec = conf.Get<vector<string>>("allow");
-    	if (vec.size() != 0)
-    	{
-    		fHasWhiteList = true;
-    		if (fDebugIsOn)
-    			Debug("Setting ALLOW list:");
-    	}
-    	for (vector<string>::iterator it=vec.begin(); it != vec.end(); it++)
-    	{
-    		fWhiteList.insert(*it);
-    		if (fDebugIsOn)
-    			Debug("                   " + *it);
-    	}
+        vector<string> vec = conf.Get<vector<string>>("allow");
+        if (vec.size() != 0)
+        {
+            fHasWhiteList = true;
+            if (fDebugIsOn)
+                Debug("Setting ALLOW list:");
+        }
+        for (vector<string>::iterator it=vec.begin(); it != vec.end(); it++)
+        {
+            fWhiteList.insert(*it);
+            if (fDebugIsOn)
+                Debug("                   " + *it);
+        }
+    }
+    //Set the grouping
+    if (conf.Has("group"))
+    {
+        vector<string> vec = conf.Get<vector<string>>("group");
+        if (vec.size() != 0)
+            if (fDebugIsOn)
+                Debug("Setting GROUPING list:");
+        for (vector<string>::iterator it=vec.begin(); it != vec.end(); it++)
+        {
+            fGrouping.insert(*it);
+            if (fDebugIsOn)
+                Debug("                   " + *it);
+        }
     }
     return true;
@@ -2109,6 +2156,6 @@
     //log.SetWindow(stdscr);
     if (conf.Has("log"))
-        if (!wout.OpenLogFile(conf.Get<std::string>("log")))
-            wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<std::string>("log") << ": " << strerror(errno) << std::endl;
+        if (!wout.OpenLogFile(conf.Get<string>("log")))
+            wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
 
     // Start io_service.Run to use the StateMachineImp::Run() loop
@@ -2125,8 +2172,8 @@
 void RunThread(DataLogger* logger)
 {
-	// This is necessary so that the StateMachine Thread can signal the 
-	// Readline thread to exit
-	logger->Run(true);
-	Readline::Stop();	
+    // This is necessary so that the StateMachine Thread can signal the 
+    // Readline thread to exit
+    logger->Run(true);
+    Readline::Stop();    
 }
 
@@ -2140,6 +2187,6 @@
 
     if (conf.Has("log"))
-        if (!wout.OpenLogFile(conf.Get<std::string>("log")))
-            win << kRed << "ERROR - Couldn't open log-file " << conf.Get<std::string>("log") << ": " << strerror(errno) << std::endl;
+        if (!wout.OpenLogFile(conf.Get<string>("log")))
+            win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
 
     DataLogger logger(wout);
@@ -2149,14 +2196,14 @@
     shell.SetReceiver(logger);
 
-	boost::thread t(boost::bind(RunThread, &logger));
-	
-	shell.Run(); // Run the shell
-	
-	logger.Stop();
-	
-	//Wait until the StateMachine has finished its thread
-	//before returning and destroyinng the dim objects which might 
-	//still be in use.
-	t.join();
+    boost::thread t(boost::bind(RunThread, &logger));
+    
+    shell.Run(); // Run the shell
+    
+    logger.Stop();
+    
+    //Wait until the StateMachine has finished its thread
+    //before returning and destroyinng the dim objects which might 
+    //still be in use.
+    t.join();
 
     return 0;
@@ -2235,7 +2282,8 @@
     po::options_description configs("Scheduler options");
     configs.add_options()
-        ("block,b",   vars<string>(), "Black-list of services")
-        ("allow,a",   vars<string>(), "White-list of services")
-        ("debug",     po_bool(),      "Debug mode. Print clear text of received service reports to log-stream")
+        ("block,b", vars<string>(), "Black-list of services")
+        ("allow,a", vars<string>(), "White-list of services")
+        ("debug",   po_bool(),      "Debug mode. Print clear text of received service reports to log-stream")
+        ("group,g", vars<string>(), "Grouping of services into a single run-Fits")
         ;
 
@@ -2257,5 +2305,5 @@
         vm = conf.Parse(argc, argv);
     }
-    catch (std::exception &e)
+    catch (exception &e)
     {
 #if BOOST_VERSION > 104000
@@ -2300,5 +2348,5 @@
             return RunShell<LocalConsole>(conf);
     }
-/*    catch (std::exception& e)
+/*    catch (exception& e)
     {
         cerr << "Exception: " << e.what() << endl;
