Index: trunk/FACT++/gui/RawEventsViewer/RawEventsViewer.cc
===================================================================
--- trunk/FACT++/gui/RawEventsViewer/RawEventsViewer.cc	(revision 12310)
+++ trunk/FACT++/gui/RawEventsViewer/RawEventsViewer.cc	(revision 12343)
@@ -477,4 +477,5 @@
     drawBlur = false;
     loopCurrentEvent = false;
+    fIsDrsCalibration = false;
     SetAutoRefresh(true);
 #ifdef LOAD_RAW
@@ -1059,77 +1060,70 @@
     //calibrationLoaded = false;
 
-    MessageImp msg;
-    if (!DataCalib::ReadFits(file, msg))
-    {
-        DataCalib::Restart();
-        return;
-    }
-    /*
-
-    calibInputFile = new fits(file);
-    if (!*calibInputFile)
-    {
-        delete calibInputFile;
-        calibInputFile = NULL;
-        return;
-    }
-
-//    if (calibInputFile->HasKey("NROI"))
-//    {
-//        cout << "Looks like you're trying to load a regular raw data file as DRS calib. aborting." << endl;
-//        delete calibInputFile;
-//        calibInputFile = NULL;
-//        return;
-//    }
-
-    if (!calibInputFile->SetPtrAddress("BaselineMean", baseLineMean)){
-        cout << "Missing column " << "BaseLineMean" << " Aborting load..." << endl;
-        return;}
-    if (!calibInputFile->SetPtrAddress("GainMean", gainMean)){
-        cout << "Missing column " << "GainMean" << " Aborting load..." << endl;
-        return;}
-    if (!calibInputFile->SetPtrAddress("TriggerOffsetMean", triggerOffsetMean)){
-        cout << "Missing column " << "TriggerOffsetMean" << " Aborting load..." << endl;
-        return;}
-
-    calibInputFile->GetNextRow();
-
- //   for (int i=0;i<1024;i++)
- //       cout << gainMean[i] << " ";
- //   cout << endl << endl;
-
-    delete calibInputFile;
-*/
-    //calibrationLoaded = true;
-
-    emit newFileLoaded();
-
-    //if (calibratedCheckBox->isChecked())
+    string msg;
+    try
+    {
+        msg = fDrsCalib.ReadFitsImp(file);
+        if (msg.empty())
+        {
+            emit newFileLoaded();
+            updateGL();
+            return;
+        }
+    }
+    catch (const runtime_error &e)
+    {
+        msg = e.what();
+    }
+
+    cerr << msg << endl;
+    fDrsCalib.Clear();
+}
+
+/************************************************************
+ * PLUS EVENT
+ ************************************************************/
+void RawDataViewer::plusEvent()
+{
+    eventStepping(true);
+}
+/************************************************************
+ * MINUS EVENT
+ ************************************************************/
+void RawDataViewer::minusEvent()
+{
+    eventStepping(false);
+}
+/************************************************************
+ * SET EVENT STEP
+ ************************************************************/
+void RawDataViewer::setEventStep(int step)
+{
+    eventStep = step;
+}
+/************************************************************
+ * EVENT STEPPING
+ ************************************************************/
+
+void RawDataViewer::ApplyCalibration()
+{
+    for (int i=0;i<(1440+160)*nRoi;i++)
+        eventData[i] = (float)rawEventData[i];
+
+    if (fIsDrsCalibration)
+    {
+        fDrsCalib.Apply(eventData, rawEventData, startPix, nRoi);
+        DrsCalibrate::RemoveSpikes(eventData, nRoi);
+    }
+
+    vector<float> pixelStatsData(1440*4);
+    DrsCalibrate::GetPixelStats(pixelStatsData.data(), eventData, nRoi);
+
+    for (vector<PixelMapEntry>::const_iterator it=fPixelMap.begin(); it!=fPixelMap.end(); it++)
+        RMSvalues[it->index] = pixelStatsData[1*1440+it->hw()];
+
+    if (isVisible())
         updateGL();
 }
-/************************************************************
- * PLUS EVENT
- ************************************************************/
-void RawDataViewer::plusEvent()
-{
-    eventStepping(true);
-}
-/************************************************************
- * MINUS EVENT
- ************************************************************/
-void RawDataViewer::minusEvent()
-{
-    eventStepping(false);
-}
-/************************************************************
- * SET EVENT STEP
- ************************************************************/
-void RawDataViewer::setEventStep(int step)
-{
-    eventStep = step;
-}
-/************************************************************
- * EVENT STEPPING
- ************************************************************/
+
 void RawDataViewer::eventStepping(bool plus)
 {
@@ -1149,42 +1143,12 @@
     inputFile->GetRow(rowNum);
 //    cout << "Getting row " << rowNum << endl;
-    for (int i=0;i<(1440+160)*nRoi;i++)
-        eventData[i] = (float)rawEventData[i];
 #endif
 
-//    if (drawCalibrationLoaded && calibrationLoaded)
-//    {
-
-//    if (calibratedCheckBox->isChecked())
-        DataCalib::Apply(eventData, rawEventData, startPix, nRoi);
-        CalibData::RemoveSpikes(eventData, nRoi);
-
-        vector<float> pixelStatsData(1440*4);
-        CalibData::GetPixelStats(pixelStatsData.data(), eventData, nRoi);
-
-
-        for (vector<PixelMapEntry>::const_iterator it=fPixelMap.begin(); it!=fPixelMap.end(); it++)
-        {
-            RMSvalues[it->index] = pixelStatsData[1*1440+it->hw()];
-        }
-
-        /*
-
-        for (int i=0;i<1440;i++)
-            for (int j=0;j<nRoi;j++)
-            {
-                int realj = (j+startPix[i])%1024;
-                eventData[i*1024+j] *= 2000.f/4096.f;
-                eventData[i*1024+j] -= (baseLineMean[i*1024+realj]+triggerOffsetMean[i*1024+j]);
-                eventData[i*1024+j] /= gainMean[i*1024+realj];
-                eventData[i*1024+j] *= (50000.f/65536.f) * 2500.f;
-            }
-    }
-    */
-        if (isVisible())
-            updateGL();
+    ApplyCalibration();
+
     emit signalCurrentEvent(eventNum);
     emit signalCurrentPixel(selectedPixel);
 }
+
 /************************************************************
  * NEXT SLICE. deprec ?
@@ -1375,17 +1339,6 @@
 void UIConnector::on_calibratedCheckBox_stateChanged(int state)
 {
-    for (int i=0;i<(1440+160)*GLWindow->nRoi;i++)
-        GLWindow->eventData[i] = (float)GLWindow->rawEventData[i];
-
-//    if (GLWindow->calibratedCheckBox->isChecked())
-    if (state)
-    {
-        DataCalib::Apply(GLWindow->eventData, GLWindow->rawEventData, GLWindow->startPix, GLWindow->nRoi);
-        CalibData::RemoveSpikes(GLWindow->eventData, GLWindow->nRoi);
-    }
-
-    GLWindow->updateGL();
-
-    //drawCalibrationCheckBox->setChecked(false);
+    GLWindow->fIsDrsCalibration = state;
+    GLWindow->ApplyCalibration();
 
     on_autoScaleColor_clicked();
@@ -2220,5 +2173,4 @@
     if (!autoScaleColor->isChecked())
     {
-        /*
         GLWindow->ss[0] = 0.496;
         GLWindow->ss[1] = 0.507;
@@ -2231,5 +2183,4 @@
         colorRange3->setValue(GLWindow->ss[3]);
         colorRange4->setValue(GLWindow->ss[4]);
-        */
         return;
     }
Index: trunk/FACT++/gui/RawEventsViewer/RawEventsViewer.h
===================================================================
--- trunk/FACT++/gui/RawEventsViewer/RawEventsViewer.h	(revision 12310)
+++ trunk/FACT++/gui/RawEventsViewer/RawEventsViewer.h	(revision 12343)
@@ -61,5 +61,11 @@
 
     friend class UIConnector;
+
+    DrsCalibration fDrsCalib;
+
+
 public:
+    bool fIsDrsCalibration;
+
     RawDataViewer(QWidget *parent = 0);
     ~RawDataViewer();
@@ -179,4 +185,6 @@
     double vCorr[1024];
 
+    void ApplyCalibration();
+
 //    GLfloat pixelsCoords[MAX_NUM_PIXELS][3];
 //    PixelsNeighbors neighbors[MAX_NUM_PIXELS];
