Index: trunk/Mars/mcore/DrsCalib.h
===================================================================
--- trunk/Mars/mcore/DrsCalib.h	(revision 12466)
+++ trunk/Mars/mcore/DrsCalib.h	(revision 12471)
@@ -18,8 +18,4 @@
     std::vector<int64_t> fSum2;
 
-    /*
-    vector<map<int16_t, uint8_t> > fMedian;
-    vector<int16_t> fResult;
-     */
 public:
     DrsCalibrate() : fNumEntries(0), fNumSamples(0), fNumChannels(0) { }
@@ -41,52 +37,5 @@
         fSum.resize(samples*channels);
         fSum2.resize(samples*channels);
-
-        //fMedian.resize(40*9*1024);
-    }
-/*
-                continue;
-
-                if (ch<40*9)
-                {
-                    fMedian[abs][val[rel]]++;
-
-                    int n= 8;
-                    if (fNumEntries>0 && fNumEntries%(n*100)==0)
-                    {
-                        fResult.resize(40*9*1024);
-
-                        uint16_t sum = 0;
-                        for (map<int16_t, uint8_t>::const_iterator it=fMedian[abs].begin();
-                             it!=fMedian[abs].end(); it++)
-                        {
-                            map<int16_t, uint8_t>::const_iterator is = it;
-                            is++;
-                            if (is==fMedian[abs].end())
-                                break;
-
-                            sum += it->second;
-
-                            double med = 0;
-
-                            med += int64_t(it->second)*int64_t(it->first);
-                            med += int64_t(is->second)*int64_t(is->first);
-                            med /= int64_t(it->second)+int64_t(is->second);
-
-                            if (sum==n*50)
-                            {
-                                fResult[abs] = it->first;
-                                cout << ch << " MED+(50): " << it->first << endl;
-                            }
-                            if (sum<n*50 && sum+is->second>n*50)
-                            {
-                                fResult[abs] = med;
-                                cout << ch << " MED-(50): " << med << endl;
-                            }
-                        }
-                        cout << ch << " AVG=" << float(fSum[abs])/fNumEntries << endl;
-                        fMedian[abs].clear();
-                    }
-                } // -2029 -2012 -2003 -1996 -1990 // -1955
-                */
+    }
 
     void AddRel(const int16_t *val, const int16_t *start)
@@ -98,12 +47,12 @@
                 continue;
 
-            const size_t pos = ch*fNumSamples;
-
-            for (size_t i=0; i<fNumSamples; i++)
+            const size_t pos = ch*1024;
+
+            for (size_t i=0; i<1024; i++)
             {
                 // Value is relative to trigger
                 // Abs is corresponding index relative to DRS pipeline
                 const size_t rel = pos +  i;
-                const size_t abs = pos + (spos+i)%fNumSamples;
+                const size_t abs = pos + (spos+i)%1024;
 
                 const int64_t v = val[rel];
@@ -145,5 +94,5 @@
         fNumEntries++;
     }
-
+    /*
     void AddAbs(const int16_t *val,    const  int16_t *start,
                 const int32_t *offset, const uint32_t scale)
@@ -173,4 +122,61 @@
 
         fNumEntries++;
+    }*/
+
+    void AddAbs(const int16_t *val,    const  int16_t *start,
+                const int32_t *offset, const uint32_t scale)
+    {
+        // 1440 without tm, 1600 with tm
+        for (size_t ch=0; ch<fNumChannels; ch++)
+        {
+            const int16_t spos = start[ch];
+            if (spos<0)
+                continue;
+
+            const size_t pos = ch*fNumSamples;
+            const size_t drs = ch>1439 ? ((ch-1440)*9+8)*1024 : ch*1024;
+
+            for (size_t i=0; i<fNumSamples; i++)
+            {
+                // Value is relative to trigger
+                // Offset is relative to DRS pipeline
+                // Abs is corresponding index relative to DRS pipeline
+                const size_t rel = pos +  i;
+                const size_t abs = drs + (spos+i)%1024;
+
+                const int64_t v = int64_t(val[rel])*scale-offset[abs];
+
+                fSum[rel]  += v;
+                fSum2[rel] += v*v;
+            }
+        }
+
+        fNumEntries++;
+    }
+
+    static void ApplyCh(float *vec, const int16_t *val, int16_t start, uint32_t roi,
+                        const int32_t *offset, const uint32_t scaleabs,
+                        const int64_t *gain,   const uint64_t scalegain)
+    {
+        if (start<0)
+        {
+            memset(vec, 0, roi);
+            return;
+        }
+
+        for (size_t i=0; i<roi; i++)
+        {
+            // Value is relative to trigger
+            // Offset is relative to DRS pipeline
+            // Abs is corresponding index relative to DRS pipeline
+            const size_t abs = (start+i)%1024;
+
+            const int64_t v =
+                + int64_t(val[i])*scaleabs-offset[abs]
+                ;
+
+            const int64_t div = gain[abs];
+            vec[i] = double(v)*scalegain/div;
+        }
     }
 
@@ -191,30 +197,13 @@
             // Offset is relative to DRS pipeline
             // Abs is corresponding index relative to DRS pipeline
-            const size_t rel = i;
             const size_t abs = (start+i)%1024;
 
             const int64_t v =
-                + (int64_t(val[rel])*scaleabs-offset[abs])*scalerel
-                - trgoff[rel]
+                + (int64_t(val[i])*scaleabs-offset[abs])*scalerel
+                - trgoff[i]
                 ;
 
             const int64_t div = gain[abs]*scalerel;
-            vec[rel] = double(v)*scalegain/div;
-        }
-    }
-
-    static void Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi,
-                      const int32_t *offset, const uint32_t scaleabs,
-                      const int64_t *gain,   const uint64_t scalegain,
-                      const int64_t *trgoff, const uint64_t scalerel)
-    {
-        for (size_t ch=0; ch<1440; ch++)
-        {
-            const size_t pos = ch*roi;
-            const size_t drs = ch*1024;
-            ApplyCh(vec+pos, val+pos, start[ch], roi,
-                    offset+drs, scaleabs,
-                    gain  +drs, scalegain,
-                    trgoff+drs, scalerel);
+            vec[i] = double(v)*scalegain/div;
         }
     }
@@ -282,7 +271,9 @@
     void GetSampleStats(float *ptr, float scale) const
     {
+        const size_t sz = fNumSamples*fNumChannels;
+
         if (fNumEntries==0)
         {
-            memset(ptr, 0, sizeof(float)*1024*1440*2);
+            memset(ptr, 0, sizeof(float)*sz*2);
             return;
         }
@@ -293,6 +284,6 @@
         while (it!=fSum.end())
         {
-            *ptr             = scale*double(*it)/fNumEntries;
-            *(ptr+1024*1440) = scale*sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
+            *ptr      = scale*double(*it)/fNumEntries;
+            *(ptr+sz) = scale*sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
 
             ptr++;
@@ -369,9 +360,13 @@
 
     uint32_t fStep;
+    uint16_t fRoi;   // Region of interest for trgoff
+    uint16_t fNumTm; // Number of time marker channels in trgoff
+
+//    uint16_t fDAC[8];
 
     DrsCalibration() :
-        fOffset(1440*1024, 0),
-        fGain(1440*1024, 4096),
-        fTrgOff(1440*1024, 0),
+        fOffset  (1440*1024, 0),
+        fGain    (1440*1024, 4096),
+        fTrgOff  (1600*1024, 0),
         fNumOffset(1),
         fNumGain(2000),
@@ -387,5 +382,5 @@
         fOffset.assign(1440*1024, 0);
         fGain.assign  (1440*1024, 4096);
-        fTrgOff.assign(1440*1024, 0);
+        fTrgOff.assign(1600*1024, 0);
 
         fNumOffset = 1;
@@ -424,25 +419,4 @@
             std::ostringstream msg;
             msg << "Reading " << str << " failed: Number of rows in table is not 1.";
-            return msg.str();
-        }
-
-        vec.resize(1440*1024*6+3);
-
-        float *base = vec.data();
-
-        file.SetPtrAddress("RunNumberBaseline",      base,   1);
-        file.SetPtrAddress("RunNumberGain",          base+1, 1);
-        file.SetPtrAddress("RunNumberTriggerOffset", base+2, 1);
-        file.SetPtrAddress("BaselineMean",           base+0*1024*1440+3, 1024*1440);
-        file.SetPtrAddress("BaselineRms",            base+1*1024*1440+3, 1024*1440);
-        file.SetPtrAddress("GainMean",               base+2*1024*1440+3, 1024*1440);
-        file.SetPtrAddress("GainRms",                base+3*1024*1440+3, 1024*1440);
-        file.SetPtrAddress("TriggerOffsetMean",      base+4*1024*1440+3, 1024*1440);
-        file.SetPtrAddress("TriggerOffsetRms",       base+5*1024*1440+3, 1024*1440);
-
-        if (!file.GetNextRow())
-        {
-            std::ostringstream msg;
-            msg << "Reading data from " << str << " failed.";
             return msg.str();
         }
@@ -452,8 +426,45 @@
         fNumGain   = file.GetUInt("NBGAIN");
         fNumTrgOff = file.GetUInt("NBTRGOFF");
-
+        fRoi       = file.GetUInt("NROI");
+        fNumTm     = file.GetUInt("NTM");
+/*
+        fDAC[0]    = file.GetUInt("DAC_A");
+        fDAC[1]    = file.GetUInt("DAC_B");
+        fDAC[4]    = file.GetUInt("DAC_C");
+*/
+        vec.resize(1440*1024*4 + (1440+fNumTm)*fRoi*2 + 4);
+
+        float *base = vec.data();
+
+        file.SetPtrAddress("RunNumberBaseline",      base,   1);
+        file.SetPtrAddress("RunNumberGain",          base+1, 1);
+        file.SetPtrAddress("RunNumberTriggerOffset", base+2, 1);
+        file.SetPtrAddress("BaselineMean",           base+4+0*1024*1440,                           1024*1440);
+        file.SetPtrAddress("BaselineRms",            base+4+1*1024*1440,                           1024*1440);
+        file.SetPtrAddress("GainMean",               base+4+2*1024*1440,                           1024*1440);
+        file.SetPtrAddress("GainRms",                base+4+3*1024*1440,                           1024*1440);
+        file.SetPtrAddress("TriggerOffsetMean",      base+4+4*1024*1440,                           fRoi*1440);
+        file.SetPtrAddress("TriggerOffsetRms",       base+4+4*1024*1440+   fRoi*1440,              fRoi*1440);
+        file.SetPtrAddress("TriggerOffsetTMMean",    base+4+4*1024*1440+ 2*fRoi*1440,              fRoi*fNumTm);
+        file.SetPtrAddress("TriggerOffsetTMRms",     base+4+4*1024*1440+ 2*fRoi*1440+ fRoi*fNumTm, fRoi*fNumTm);
+
+        if (!file.GetNextRow())
+        {
+            std::ostringstream msg;
+            msg << "Reading data from " << str << " failed.";
+            return msg.str();
+        }
+/*
+        fDAC[2] = fDAC[1];
+        fDAC[4] = fDAC[1];
+
+        fDAC[5] = fDAC[4];
+        fDAC[6] = fDAC[4];
+        fDAC[7] = fDAC[4];
+*/
         fOffset.resize(1024*1440);
         fGain.resize(1024*1440);
-        fTrgOff.resize(1024*1440);
+
+        fTrgOff.resize(fRoi*(1440+fNumTm));
 
         // Convert back to ADC counts: 256/125 = 4096/2000
@@ -461,8 +472,14 @@
         for (int i=0; i<1024*1440; i++)
         {
-            fOffset[i] = fNumOffset           *256*base[i+1024*1440*0+3]/125;
-            fGain[i]   = fNumOffset*fNumGain  *256*base[i+1024*1440*2+3]/125;
-            fTrgOff[i] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+3]/125;
-        }
+            fOffset[i] = fNumOffset         *256*base[i+1024*1440*0+4]/125;
+            fGain[i]   = fNumOffset*fNumGain*256*base[i+1024*1440*2+4]/125;
+        }
+
+        for (int i=0; i<fRoi*1440; i++)
+            fTrgOff[i] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+4]/125;
+
+        for (int i=0; i<fRoi*fNumTm; i++)
+            fTrgOff[i+1440*fRoi] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+2*fRoi*1440+4]/125;
+
 
         // DAC:  0..2.5V == 0..65535
@@ -500,11 +517,44 @@
     bool IsValid() { return fStep>2; }
 
-    void Apply(float *vec, int16_t *val, const int16_t *start, uint32_t roi)
-    {
-        DrsCalibrate::Apply(vec, val, start, roi,
-                            fOffset.data(), fNumOffset,
-                            fGain.data(),   fNumGain,
-                            fTrgOff.data(), fNumTrgOff);
-
+    bool Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi)
+    {
+        if (roi!=fRoi)
+        {
+            for (size_t ch=0; ch<1440; ch++)
+            {
+                const size_t pos = ch*roi;
+                const size_t drs = ch*1024;
+
+                DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
+                                      fOffset.data()+drs, fNumOffset,
+                                      fGain.data()  +drs, fNumGain);
+            }
+
+            return false;
+        }
+
+        for (size_t ch=0; ch<1440; ch++)
+        {
+            const size_t pos = ch*fRoi;
+            const size_t drs = ch*1024;
+
+            DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
+                                  fOffset.data()+drs, fNumOffset,
+                                  fGain.data()  +drs, fNumGain,
+                                  fTrgOff.data()+pos, fNumTrgOff);
+        }
+
+        for (size_t ch=0; ch<fNumTm; ch++)
+        {
+            const size_t pos = (ch+1440)*fRoi;
+            const size_t drs = (ch*9+8)*1024;
+
+            DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
+                                  fOffset.data()+drs, fNumOffset,
+                                  fGain.data()  +drs, fNumGain,
+                                  fTrgOff.data()+pos, fNumTrgOff);
+        }
+
+        return true;
     }
 };
