Index: /trunk/Mars/mcore/DrsCalib.h
===================================================================
--- /trunk/Mars/mcore/DrsCalib.h	(revision 12325)
+++ /trunk/Mars/mcore/DrsCalib.h	(revision 12326)
@@ -1,2 +1,5 @@
+#ifndef MARS_CalibData
+#define MARS_CalibData
+
 class CalibData
 {
@@ -167,36 +170,30 @@
     }
 
-    static void Apply(int16_t *val, const int16_t *start, uint32_t roi,
-                      const int32_t *offset, const uint32_t scaleabs,
-                      const int64_t *gain,   const uint32_t scalegain,
-                      const int64_t *trgoff, const uint32_t scalerel)
-    {
-        return ;// "FIXME: scalegain exceeds valid range..."
-        for (size_t ch=0; ch<1440; ch++)
-        {
-            const int16_t spos = start[ch];
-            if (spos<0)
-                continue;
-
-            const size_t pos = ch*roi;
-
-            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 rel = pos +  i;
-                const size_t abs = pos + (spos+i)%1024;
-
-                const int64_t v =
-                    + int64_t(val[rel])   *scaleabs*scalerel
-                    - int64_t(trgoff[rel])*scaleabs
-                    - int64_t(offset[abs])*scalerel
-                    ;
-
-                // The gain value is multiplied by fNumOffset and fNumGain
-                const int64_t div = int64_t(gain[abs])*scaleabs*scalerel;
-                val[rel] = (v*scalegain)/div;
-             }
+    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,
+                        const int64_t *trgoff, const uint64_t scalerel)
+    {
+        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 rel = i;
+            const size_t abs = (start+i)%1024;
+
+            const int64_t v =
+                + (int64_t(val[rel])*scaleabs-offset[abs])*scalerel
+                - trgoff[rel]
+                ;
+
+            const int64_t div = gain[abs]*scalerel;
+            vec[rel] = double(v)*scalegain/div;
         }
     }
@@ -204,68 +201,15 @@
     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)
-    {
-        /*
-        scalegain *= scaleabs;
-        scalerel  *= 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 int16_t spos = start[ch];
-            if (spos<0)
-            {
-                memset(vec+pos, 0, roi);
-                continue;
-            }
-
-            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 rel = pos +  i;
-                const size_t abs = pos + (spos+i)%1024;
-
-                const int64_t v =
-                    + int64_t(val[rel])   *scaleabs*scalerel
-                    - int64_t(trgoff[rel])*scaleabs
-                    - int64_t(offset[abs])*scalerel
-                    ;
-
-                const int64_t div = int64_t(gain[abs])*scaleabs*scalerel;
-
-                vec[rel] = double(v)*scalegain/div;
-            }
-        }*/
-
-        for (size_t ch=0; ch<1440; ch++)
-        {
-            const size_t pos = ch*roi;
-
-            const int16_t spos = start[ch];
-            if (spos<0)
-            {
-                memset(vec+pos, 0, roi);
-                continue;
-            }
-
-            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 rel = pos +  i;
-                const size_t abs = pos + (spos+i)%1024;
-
-                const int64_t v =
-                    + (int64_t(val[rel])*scaleabs-offset[abs])*scalerel
-                    - trgoff[rel]
-                    ;
-
-                const int64_t div = gain[abs]*scalerel;
-                vec[rel] = double(v)*scalegain/div;
-            }
+            const size_t drs = ch*1024;
+            ApplyCh(vec+pos, val+pos, start[ch], roi,
+                    offset+drs, scaleabs,
+                    gain  +drs, scalegain,
+                    trgoff+drs, scalerel);
         }
     }
@@ -409,2 +353,3 @@
 };
 
+#endif
