Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc	(revision 8171)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc	(revision 8192)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MExtractTimeAndCharge.cc,v 1.57 2006-10-27 13:49:42 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MExtractTimeAndCharge.cc,v 1.58 2006-11-01 15:48:31 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -167,4 +167,23 @@
 // --------------------------------------------------------------------------
 //
+// Return the x-value lower than sat0 at which the signal has been
+// fallen bwlow maxcont/2. This time is determined using a simple second
+// order polynomial interpolation.
+//
+Double_t MExtractTimeAndCharge::GetSaturationTime(Int_t sat0, const Float_t *sig, Int_t maxcont) const
+{
+    const Int_t p = sat0>1 ? sat0-2 : sat0-1;
+    if (sat0<=0)
+        return 0;
+
+    // Find the place at which the signal is maxcont/2
+    const TVector3 vx(sig[p], sig[p+1], sig[p+2]);
+    const TVector3 vy(p, p+1, p+2);
+
+    return MMath::InterpolParabLin(vx, vy, maxcont/2);
+}
+
+// --------------------------------------------------------------------------
+//
 // Calculate the integral of the FADC time slices and store them as a new
 // pixel in the MArrivalTimeCam container.
@@ -199,20 +218,10 @@
         // Do not even try to extract the hi-gain if we have
         // more than one saturating slice
+        const Int_t rangehi = fHiGainLast - fHiGainFirst + 1;
+
         if (numsathi<2)
-        {
-            const Int_t rangehi = fHiGainLast - fHiGainFirst + 1;
             FindTimeAndChargeHiGain2(sig+fHiGainFirst, rangehi,
                                      sumhi, deltasumhi, timehi, deltatimehi,
                                      numsathi, maxposhi);
-
-
-            // Make sure that in cases the time couldn't be correctly determined
-            // more meaningfull default values are assigned
-            //if (timehi<fHiGainFirst || timehi>=fHiGainLast-1)
-            if (deltatimehi>-0.5 && (timehi<0 || timehi>=fHiGainLast-fHiGainFirst+1))
-                timehi = gRandom->Uniform(fHiGainLast-fHiGainFirst+1);
-
-            timehi += fHiGainFirst;
-        }
 
         // If we have saturating slices try to get a better estimate
@@ -221,15 +230,20 @@
         if (numsathi>1)
         {
-            const Int_t p = sathi0>1 ? sathi0-2 : sathi0-1;
-            if (sathi0>0)
-            {
-                // Find the place at which the signal is maxcont/2
-                const TVector3 vx(sig[p], sig[p+1], sig[p+2]);
-                const TVector3 vy(p, p+1, p+2);
-                timehi=MMath::InterpolParabLin(vx, vy, maxcont/2);
-            }
-            else
-                timehi=0;
+            timehi = GetSaturationTime(sathi0, sig, maxcont)-fHiGainFirst;
+            deltatimehi = 0;
         }
+
+        // Make sure that in cases the time couldn't be correctly determined
+        // more meaningfull default values are assigned.
+        // For extractors like the digital filter and the spline
+        // we allow extracpolation by one slice.
+        if (deltatimehi>-0.5 && (timehi<-1 || timehi>=rangehi))
+        {
+            // Flag this as unreliable!
+            timehi = gRandom->Uniform(rangehi+1)-1;
+            // deltatimehi=-1;
+        }
+
+        timehi += fHiGainFirst;
 
         Float_t sumlo =0, deltasumlo =-1;  // invalidate logain of MExtractedSignalPix
@@ -280,5 +294,8 @@
             //
             // The jitter in the hi-/lo-gain offset ssems to be around +/-0.5
-            first = TMath::FloorNint(timehi+numh+fOffsetLoGain+fLoGainStartShift);
+            if (deltatimehi>-0.5)
+                first = TMath::FloorNint(timehi+numh+fOffsetLoGain+fLoGainStartShift);
+            //else ???
+
             if (first<0)
                 first = 0;
@@ -302,13 +319,28 @@
             numsatlo = fSignal->GetSaturation(pixidx, fSaturationLimit, satlo0, satlo1);
 
-            FindTimeAndChargeLoGain2(sig+first, last-first+1,
+            const Int_t rangelo = last-first+1;
+            FindTimeAndChargeLoGain2(sig+first, rangelo,
                                      sumlo, deltasumlo, timelo, deltatimelo,
                                      numsatlo, maxposlo);
 
+            // If we have saturating slices try to get a better estimate
+            // of the arrival time than timehi or sathi0. This is
+            // usefull to know where to start lo-gain extraction.
+            if (numsatlo>1)
+            {
+                timelo = GetSaturationTime(satlo0, sig, maxlo)-numh-first;
+                deltatimelo = 0;
+            }
+
             // Make sure that in cases the time couldn't be correctly determined
             // more meaningfull default values are assigned
-            //if (timehi<fHiGainFirst || timehi>=fHiGainLast-1)
-            if (deltatimelo>-0.5 && (timelo<0 || timelo>=last-first+1))
-                timelo = gRandom->Uniform(last-first+1);
+            // For extractors like the digital filter and the spline
+            // we allow extracpolation by one slice.
+            if (deltatimelo>-0.5 && (timelo<-1 || timelo>=rangelo))
+            {
+                // Flag this as unreliable!
+                timelo = gRandom->Uniform(rangelo+1)-1;
+                //deltatimelo=-1;
+            }
 
             timelo += first-numh;
@@ -323,4 +355,5 @@
             //    deltatimelo = -1;
         }
+
 
         // Now store the result in the corresponding containers
Index: trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.h
===================================================================
--- trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.h	(revision 8171)
+++ trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.h	(revision 8192)
@@ -24,4 +24,6 @@
   Int_t  Process();
   Int_t  ReadEnv(const TEnv &env, TString prefix, Bool_t print);
+
+  Double_t GetSaturationTime(Int_t sat0, const Float_t *sig, Int_t maxcont) const;
 
 public:
