Index: trunk/Mars/mcore/DrsCalib.h
===================================================================
--- trunk/Mars/mcore/DrsCalib.h	(revision 14935)
+++ trunk/Mars/mcore/DrsCalib.h	(revision 14949)
@@ -347,5 +347,5 @@
             //    std::cout << "     " << it->avg << std::endl;
 
-            const Int_t skip = list.size()/10;
+            const size_t skip = list.size()/10;
             rc = AverageSteps(list.begin()+skip, list.begin()+list.size()-skip);
 
@@ -727,25 +727,33 @@
                 const float &v1 = val[rel+1];//-avg;
 
-                // Is rising edge?
+                // If edge is positive ignore all falling edges
                 if (edge>0 && v0>0)
                     continue;
 
-                // Is falling edge?
+                // If edge is negative ignore all falling edges
                 if (edge<0 && v0<0)
                     continue;
 
-                // Has sign changed?
+                // Check if there is a zero crossing
                 if ((v0<0 && v1<0) || (v0>0 && v1>0))
                     continue;
 
+                // Calculate the position p of the zero-crossing
+                // within the interval [rel, rel+1] relative to rel
+                // by linear interpolation.
                 const double p = v0==v1 ? 0.5 : v0/(v0-v1);
 
-                const double l = i+p - (i_prev+p_prev);
-
+                // If this was at least the second zero-crossing detected
                 if (i_prev>=0)
                 {
+                    // Calculate the distance l between the
+                    // current and the last zero-crossing
+                    const double l = i+p - (i_prev+p_prev);
+
+                    // By summation, the average length of each
+                    // cell is calculated. For the first and last
+                    // fraction of a cell, the fraction is applied
+                    // as a weight.
                     const double w0 = 1-p_prev;
-                    const double w1 = p;
-
                     fStat[pos+(spos+i_prev)%1024].first  += w0*l;
                     fStat[pos+(spos+i_prev)%1024].second += w0;
@@ -757,8 +765,10 @@
                     }
 
+                    const double w1 = p;
                     fStat[pos+(spos+i)%1024].first  += w1*l;
                     fStat[pos+(spos+i)%1024].second += w1;
                 }
 
+                // Remember this zero-crossing position
                 p_prev = p;
                 i_prev = i;
@@ -819,8 +829,10 @@
             const auto end = beg + 1024;
 
-            // Calculate mean
+            // First calculate the average length s of a single
+            // zero-crossing interval in the whole range [0;1023]
+            // (which is identical to the/ wavelength of the
+            // calibration signal)
             double s = 0;
             double w = 0;
-
             for (auto it=beg; it!=end; it++)
             {
@@ -828,7 +840,29 @@
                 w += it->second;
             }
-
             s /= w;
 
+            // Dividing the average length s of the zero-crossing
+            // interval in the range [0;1023] by the average length
+            // in the interval [0;n] yields the relative size of
+            // the interval in the range [0;n].
+            //
+            // Example:
+            // Average [0;1023]: 10.00  (global interval size in samples)
+            // Average [0;512]:   8.00  (local interval size in samples)
+            //
+            // Globally, on average one interval is sampled by 10 samples.
+            // In the sub-range [0;512] one interval is sampled on average
+            // by 8 samples.
+            // That means that the interval contains 64 periods, while
+            // in the ideal case (each sample has the same length), it
+            // should contain 51.2 periods.
+            // So, the sampling position 512 corresponds to a time 640,
+            // while in the ideal case with equally spaces samples,
+            // it would correspond to a time 512.
+            //
+            // The offset (defined as 'ideal - real') is then calculated
+            // as 512*(1-10/8) = -128, so that the time is calculated as
+            // 'sampling position minus offset'
+            //
             double sumw = 0;
             double sumv = 0;
@@ -837,4 +871,5 @@
             // Sums about many values are numerically less stable than
             // just sums over less. So we do the exercise from both sides.
+            // First from the left
             for (auto it=beg; it!=end-512; it++, n++)
             {
@@ -842,5 +877,5 @@
                 const double valw = it->second;
 
-                it->first = sumv>0 ? n*(1-s*sumw/sumv) :0;
+                it->first = sumv>0 ? n*(1-s*sumw/sumv) : 0;
 
                 sumv += valv;
@@ -852,4 +887,5 @@
             n = 1;
 
+            // Second from the right
             for (auto it=end-1; it!=beg-1+512; it--, n++)
             {
@@ -862,4 +898,10 @@
                 it->first = sumv>0 ? n*(s*sumw/sumv-1) : 0;
             }
+
+            // A crosscheck has shown, that the values from the left
+            // and right perfectly agree over the whole range. This means
+            // the a calculation from just one side would be enough, but
+            // doing it from both sides might still make the numerics
+            // a bit more stable.
         }
     }
