Index: /trunk/Mars/mcore/factfits.h
===================================================================
--- /trunk/Mars/mcore/factfits.h	(revision 16416)
+++ /trunk/Mars/mcore/factfits.h	(revision 16417)
@@ -6,92 +6,63 @@
  */
 
-#ifndef FACTFITS_H_
-#define FACTFITS_H_
+#ifndef MARS_FACTFITS
+#define MARS_FACTFITS
 
 #include "zfits.h"
-
 
 #ifndef __MARS__
 namespace std
 {
-#else
-using namespace std;
 #endif
 
-class factFits : public zfits
+class factfits : public zfits
 {
 public:
-    /*
-     *  Default constructor
-     */
-    factFits(const string& fname,
-             const string& tableName="",
-             bool          force=false,
-             bool          mightFail=false) : zfits(fname, tableName, force, mightFail),
-                                              fOffsetCalibration(0),
-                                              fStartCellOffset(0)
+    // Default constructor
+    factfits(const string& fname, const string& tableName="", bool force=false) :
+        zfits(fname, tableName, force)
     {
-        readDrsCalib(fname);
-        GetStartCellOffset();
+        if (init())
+            readDrsCalib(fname);
     }
-    /*
-     *  Alternative constructor
-     */
-    factFits(const string& fname,
-             const string& fout,
-             const string& tableName="",
-             bool          force=false,
-             bool          mightFail=false): zfits(fname, fout, tableName, force, mightFail),
-                                             fOffsetCalibration(0),
-                                             fStartCellOffset(0)
+
+    // Alternative constructor
+    factfits(const string& fname, const string& fout, const string& tableName, bool force=false) :
+        zfits(fname, fout, tableName, force)
     {
-        readDrsCalib(fname);
-        GetStartCellOffset();
+        if (init())
+            readDrsCalib(fname);
     }
-    /*
-     *  Default destrctor
-     */
-    ~factFits()
-    {}
-    /*
-     *  Overload of zfits.h
-     */
+
+    //
 #if !defined(__MARS__) && !defined(__CINT__)
-    virtual bool GetRow(size_t row, bool check=true)
+    bool GetRow(size_t row, bool check=true)
 #else
-    virtual bool GetRowNum(size_t row, bool check=true)
+    bool GetRowNum(size_t row, bool check=true)
 #endif
     {
 #if !defined(__MARS__) && !defined(__CINT__)
-        const bool isGood = zfits::GetRow(row, check);
+        if (!zfits::GetRow(row, check))
+            return false;
 #else
-        const bool isGood = zfits::GetRowNum(row, check);
+        if (!zfits::GetRowNum(row, check))
+            return false;
 #endif
-        //row is loaded in internal memory. Should we un-apply the integer drs ?
-        if (!isGood)                        return false;
-        if (!fTable.isCompressed)           return true;
-        if (fOffsetCalibration.size() == 0) return true;
-
-        //now Drs un-Calibrate, if required
-        const Pointers::iterator dtaIt = fPointers.find("Data");
-        if (dtaIt == fPointers.end()) return true;
+        // This file does not contain fact data or no calibration to be applied
+        if (fOffsetCalibration.empty())
+            return true;
 
         //re-get the pointer to the data to access the offsets
         const uint8_t offset = (row*fTable.bytes_per_row)%4;
-        const char* ptr = fBufferRow.data() + offset;
 
-        //get column details for Data
-        const Table::Columns::const_iterator dataColIt = fTable.cols.find("Data");
-        if (dataColIt == fTable.cols.end()) return false;
-        const Table::Column& dataColumn = dataColIt->second;
-        const uint32_t numSlices = dataColumn.num/1440;
+        int16_t *startCell = reinterpret_cast<int16_t*>(fBufferRow.data() + offset + fOffsetStartCellData);
+        int16_t *data      = reinterpret_cast<int16_t*>(fBufferRow.data() + offset + fOffsetData);
 
-        //Drs un-calibrate !
-        for (uint32_t i=0;i<1440;i++)
+        for (uint32_t i=0; i<1440*1024; i+=1024, startCell++)
         {
-            const int32_t thisStartCell = reinterpret_cast<const int16_t*>(ptr+fStartCellOffset)[i];
-            for (uint32_t j=0;j<numSlices;j++)
-                reinterpret_cast<int16_t*>(dtaIt->second)[numSlices*i+j] += fOffsetCalibration[1024*i + (thisStartCell+j)%1024];
+            for (uint32_t j=0; j<fNumRoi; j++, data++)
+                *data += fOffsetCalibration[i + (*startCell+j)%1024];
         }
+
         return true;
     }
@@ -99,20 +70,53 @@
 private:
 
-    /*
-     *  Read the Drs calibration data
-     */
+    bool init()
+    {
+        if (!HasKey("NPIX") || !HasKey("NROI"))
+            return false;
+
+        if (Get<uint16_t>("NPIX")!=1440)
+            return false;
+
+        fNumRoi = Get<uint16_t>("NROI");
+        if (fNumRoi>1024)
+            return false;
+
+        // check column details for Data
+        const Table::Columns::const_iterator it = fTable.cols.find("Data");
+        if (it==fTable.cols.end() || it->second.num!=1440*fNumRoi || it->second.type!='I')
+            return false;
+
+        // check column details for StartCellData
+        const Table::Columns::const_iterator is = fTable.cols.find("StartCellData");
+        if (is==fTable.cols.end() || is->second.num!=1440 || is->second.type!='I')
+            return false;
+
+        fOffsetStartCellData = is->second.offset;
+        fOffsetData          = it->second.offset;
+
+        return true;
+    }
+
+    //  Read the Drs calibration data
     void readDrsCalib(const string& fileName)
     {
-        zfits calib(fileName, "DrsCalib", false, true);
+        zfits calib(fileName, "DrsCalib");
+        if (calib.bad())
+        {
+            clear(rdstate()|ios::badbit);
+            return;
+        }
 
-        const bool isDrsCalibTable = (bool)calib;
-        if (!isDrsCalibTable) return;
+        if (calib.eof())
+            return;
+
         // Check correct size and format of table
         if (calib.GetNumRows() != 1)
         {
+            clear(rdstate()|ios::badbit);
 #ifdef __EXCEPTIONS
-            throw runtime_error("ERROR: DrsCalib table found, but not with one row as expected");
+            throw runtime_error("Table 'DrsCalib' found, but not with one row as expected");
 #else
-            gLog << ___err___ << "ERROR: DrsCalib table found, but not with one row as expected" << endl;
+            gLog << ___err___ << "ERROR - Table 'DrsCalib' found, but not with one row as expected" << endl;
             return;
 #endif
@@ -120,17 +124,19 @@
         if (calib.GetStr("TTYPE1") != "OffsetCalibration")
         {
+            clear(rdstate()|ios::badbit);
 #ifdef __EXCEPTIONS
-            throw runtime_error("ERROR: DrsCalib table found, but first column is not the one expected");
+            throw runtime_error("Table 'DrsCalib' found, but first column is not the one expected");
 #else
-            gLog << ___err___ << "ERROR: DrsCalib table found, but first column is not the one expected" << endl;
+            gLog << ___err___ << "ERROR - Table 'DrsCalib' found, but first column is not the one expected" << endl;
             return;
 #endif
         }
-        if (calib.GetStr("TFORM1") != "1474560I")
+        if (calib.GetStr("TFORM1") != "1474560I")  // 1024*1440
         {
+            clear(rdstate()|ios::badbit);
 #ifdef __EXCEPTIONS
-            throw runtime_error("ERROR: DrsCalib table found, but has wrong column format");
+            throw runtime_error("Table 'DrsCalib' has wrong column format (TFROM1)");
 #else
-            gLog << ___err___ << "ERROR: DrsCalib table found, but has wrong column format" << endl;
+            gLog << ___err___ << "ERROR - Table 'DrsCalib' has wrong column format (TFORM1)" << endl;
             return;
 #endif
@@ -138,26 +144,27 @@
 
         fOffsetCalibration.resize(1024*1440);
-        calib.SetPtrAddress("OffsetCalibration",fOffsetCalibration.data());
 
-        calib.GetNextRow();
-    }
+        calib.SetPtrAddress("OffsetCalibration", fOffsetCalibration.data());
+        if (calib.GetNextRow())
+            return;
 
-    /*
-     *  Get the offset of the StartCell column
-     */
-    void GetStartCellOffset()
-    {
-        for (Table::Columns::const_iterator jt=fTable.cols.begin(); jt != fTable.cols.end(); jt++)
-        {
-            if (jt->first == "StartCellData")
-            {
-                fStartCellOffset = jt->second.offset;
-                break;
-            }
-        }
+        clear(rdstate()|ios::badbit);
+
+#ifdef __EXCEPTIONS
+        throw runtime_error("Reading column 'OffsetCalibration' failed.");
+#else
+        gLog << ___err___ << "ERROR - Reading column 'OffsetCalibration' failed." << endl;
+#endif
+
     }
 
     vector<int16_t> fOffsetCalibration; ///< integer values of the drs calibration used for compression
-    size_t          fStartCellOffset;   ///< offset of the StartCellData column in the uncompressed data
+
+    size_t fOffsetData;
+    size_t fOffsetStartCellData;
+
+    uint16_t fNumRoi;
+
+#warning Time marker channels currently unhandled
 
 }; //class factfits
@@ -167,3 +174,3 @@
 #endif
 
-#endif /* FACTFITS_H_ */
+#endif
