Index: /trunk/FACT++/src/EventBuilderWrapper.h
===================================================================
--- /trunk/FACT++/src/EventBuilderWrapper.h	(revision 11676)
+++ /trunk/FACT++/src/EventBuilderWrapper.h	(revision 11677)
@@ -120,4 +120,128 @@
     }
 };
+
+#include "DrsCalib.h"
+
+class DataFileCalib : public DataFileImp, CalibData
+{
+    static vector<int32_t> fOffset;
+    static vector<int32_t> fGain;
+    static vector<int32_t> fTrgOff;
+
+    static uint32_t fNumOffset;
+    static uint32_t fNumGain;
+    static uint32_t fNumTrgOff;
+
+    static int fStep;
+
+public:
+    DataFileCalib(uint32_t id, MessageImp &imp) : DataFileImp(id, imp) { }
+
+    static void Restart() { fStep = 0; }
+
+    bool OpenFile(RUN_HEAD* h)
+    {
+        if (fStep==4)
+        {
+            fMsg.Warn("DRS Calibration already finished... please restart!");
+            return false;
+        }
+
+        if (h->NPix != 1440)
+        {
+            fMsg.Error("Number of pixels in header not 1440.");
+            return false;
+        }
+
+        if (h->Nroi != 1024)
+        {
+            fMsg.Error("Region of interest not 1024.");
+            return false;
+        }
+
+        ostringstream name;
+        name << "drs-calib-" << fStep;
+        fFileName = name.str();
+
+        Reset();
+        InitSize(1440, 1024);
+
+        if (fStep==0)
+        {
+            fGain.assign(1440*1024, 1);
+            fTrgOff.assign(1440*1024, 0);
+
+            fNumGain   = 1;
+            fNumTrgOff = 1;
+        }
+
+        return true;
+    }
+    bool WriteEvt(EVENT *e)
+    {
+        // FIXME: SET StartPix to 0 if StartPix is -1
+
+        if (fStep==0)
+        {
+            AddRel(e->Adc_Data, e->StartPix);
+        }
+        if (fStep==1)
+        {
+            AddRel(e->Adc_Data, e->StartPix, fOffset.data(), fNumOffset);
+        }
+        if (fStep==2)
+        {
+            AddAbs(e->Adc_Data, e->StartPix, fOffset.data(), fNumOffset);
+        }
+
+        return true;
+    }
+    bool Close(RUN_TAIL * = 0)
+    {
+        if (fStep==0)
+        {
+            fOffset.assign(fSum.begin(), fSum.end());
+            fNumOffset = fNumEntries;
+        }
+        if (fStep==1)
+        {
+            fGain.assign(fSum.begin(), fSum.end());
+            fNumGain = fNumEntries*32768;
+            for (int i=0; i<1024*1440; i++)
+                fOffset -= 16384;
+            // Now we have a scale from -16384 to 16384
+        }
+        if (fStep==2)
+        {
+            fTrgOff.assign(fSum.begin(), fSum.end());
+            fNumTrgOff = fNumEntries;
+        }
+        fStep++;
+
+        return true;
+    }
+
+    static void Apply(int16_t *val, const int16_t *start, uint32_t roi)
+    {
+        if (fStep==0)
+            return;
+
+        CalibData::Apply(val, start, roi,
+                         fOffset.data(), fNumOffset,
+                         fGain.data(),   fNumGain,
+                         fTrgOff.data(), fNumTrgOff);
+    }
+};
+
+int DataFileCalib::fStep = 0;
+
+vector<int32_t> DataFileCalib::fOffset;
+vector<int32_t> DataFileCalib::fGain;
+vector<int32_t> DataFileCalib::fTrgOff;
+
+uint32_t DataFileCalib::fNumOffset = 0;
+uint32_t DataFileCalib::fNumGain   = 0;
+uint32_t DataFileCalib::fNumTrgOff = 0;
+
 
 class DataFileDebug : public DataFileNone
@@ -779,5 +903,6 @@
         kDebug,
         kFits,
-        kRaw
+        kRaw,
+        kCalib
     };
 
@@ -1064,5 +1189,10 @@
     bool IsIgnored(int i) const { return g_port[i].sockDef==-1; }
 
-    void SetOutputFormat(FileFormat_t f) { fFileFormat = f; }
+    void SetOutputFormat(FileFormat_t f)
+    {
+        fFileFormat = f;
+        if (fFileFormat==kCalib)
+            DataFileCalib::Restart();
+    }
 
     void SetDebugLog(bool b) { fDebugLog = b; }
@@ -1166,8 +1296,9 @@
         switch (fFileFormat)
         {
-        case kNone:  file = new DataFileNone(runid, fMsg);  break;
+        case kNone:  file = new DataFileNone(runid,  fMsg); break;
         case kDebug: file = new DataFileDebug(runid, fMsg); break;
-        case kFits:  file = new DataFileFits(runid, fMsg);  break;
-	case kRaw:   file = new DataFileRaw(runid, fMsg);   break;
+        case kFits:  file = new DataFileFits(runid,  fMsg); break;
+	case kRaw:   file = new DataFileRaw(runid,   fMsg); break;
+	case kCalib: file = new DataFileCalib(runid, fMsg); break;
         }
 
@@ -1430,4 +1561,6 @@
         oldt = newt;
 
+        DataFileCalib::Apply(event->Adc_Data, event->StartPix, event->Roi);
+
 	const size_t sz = sizeof(EVENT)+event->Roi*2*1440;
 
