Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 8191)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 8192)
@@ -38,4 +38,7 @@
      - further improved grouping
 
+   * datacenter/macros/plotrundb.C:
+     - added
+
    * msql/MSQLServer.[h,cc]:
      - implemented a new function returning the primary key of a table
@@ -43,4 +46,53 @@
        necessary to do a query
      - changed the call to GetTables and GetColumns
+
+   * mhflux/MHFalseSource.[h,cc]:
+     - added a DrawNicePlot feature like for MHAlpha
+
+   * mmain/MEventDisplay.cc:
+     - added MPedestalSubtract
+     - new default size
+
+   * mbase/MLogHtml.cc:
+     - improved colors
+
+   * mcalib/MCalibrationRelTimeCalc.cc:
+     - reformatted some output
+
+   * mhcalib/MHCalibrationCam.cc:
+     - reformatted and improved output
+     - do not complain about empty lo-gain histograms (BTW:
+       it should complain if the histogram should not be empty!)
+
+   * mhcalib/MHCalibrationChargeCam.cc:
+     - when filling the histograms only fill signals which are
+       valid
+     - do not count the number of lo-gain saturation slices, just
+       check whether it is saturating or not
+     - for the moment we do not take the fTimeLower/UpperLimit into
+       account. It is enough if the extracted time is within the
+       extraction range (TO BE IMPROVED!)
+
+   * mhcalib/MHCalibrationPulseTimeCam.cc:
+     - improved output
+     - do not check th elo-gain if hi-gain is already above saturation
+       limit.
+
+   * mhcalib/MHCalibrationRelTimeCam.cc:
+     - extended first bin from -4.975 to -9.975
+     - correctly handle validity and saturation of the pixels
+     - allow the reference pixel to be set from the resource file
+
+   * mhcalib/MHGausEvents.h:
+     - exclude empty histograms in IsOnlyUnderflow and IsOnlyOverflow
+
+   * msignal/MExtractTimeAndCharge.[h,cc]:
+     - added a new function which give an estimate for the arrival time
+       in case of saturation
+     - allow the arrival time to be a minimum of -1 before replacing
+       it by a random value
+     - in case of saturation use a polynomial of second order to
+       get a better estimation of the arrival time
+
 
 
Index: /trunk/MagicSoft/Mars/mbase/MLogHtml.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MLogHtml.cc	(revision 8191)
+++ /trunk/MagicSoft/Mars/mbase/MLogHtml.cc	(revision 8192)
@@ -110,8 +110,8 @@
     {
     case 0:  break;
-    case 1:  *fOut << "<font color=#aa0000>"; break;  // err
-    case 2:  *fOut << "<font color=#00aaaa>"; break;  // warn
-    case 3:  *fOut << "<font color=#00aa00>"; break;  // inf
-    default: *fOut << "<font color=#0000aa>"; break;  // all others (dbg)
+    case 1:  *fOut << "<font color='maroon'>";  break;  // err
+    case 2:  *fOut << "<font color='#FF6600'>"; break;  // warn (olive?)
+    case 3:  *fOut << "<font color='green'";    break;  // inf
+    default: *fOut << "<font color='navy'>";    break;  // all others (dbg)
     }
 
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationRelTimeCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationRelTimeCalc.cc	(revision 8191)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationRelTimeCalc.cc	(revision 8192)
@@ -362,6 +362,5 @@
       if (numareavalid[aidx] < 20)
         {
-          *fLog << warn << GetDescriptor() << ": Less than 20 pixels with valid time resolution found "
-                << "in area index: " << aidx << endl;
+          *fLog << warn << "Area   " << setw(4) << aidx << ": Less than 20 pixels with valid time resolution found." << endl;
           continue;
         }
@@ -397,7 +396,7 @@
       if ( res < lowlim[aidx] || res > upplim[aidx] )
         {
-          *fLog << warn << "Deviating time resolution: "
-            << Form("%4.2f",res) << " out of range ["
-                << Form("%4.2f,%4.2f",lowlim[aidx],upplim[aidx]) << "] in pixel " << i << endl;
+            *fLog << warn << "Pixel  " << setw(4) << i << ": Deviating time resolution: "
+                << Form("%4.2f",res) << " out of range ["
+                << Form("%4.2f,%4.2f",lowlim[aidx],upplim[aidx]) << "]" << endl;
           bad.SetUncalibrated( MBadPixelsPix::kDeviatingTimeResolution);
           pix.SetExcluded();
Index: /trunk/MagicSoft/Mars/mhcalib/MHCalibrationChargeCam.cc
===================================================================
--- /trunk/MagicSoft/Mars/mhcalib/MHCalibrationChargeCam.cc	(revision 8191)
+++ /trunk/MagicSoft/Mars/mhcalib/MHCalibrationChargeCam.cc	(revision 8192)
@@ -1,3 +1,5 @@
 /* ======================================================================== *\
+! $Name: not supported by cvs2svn $:$Id: MHCalibrationChargeCam.cc,v 1.46 2006-11-01 15:48:31 tbretz Exp $
+! --------------------------------------------------------------------------
 !
 ! *
@@ -17,6 +19,7 @@
 !
 !   Author(s): Markus Gaug   02/2004 <mailto:markus@ifae.es>
+!   Author(s): Thomas Bretz <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2004
+!   Copyright: MAGIC Software Development, 2000-2006
 !
 !
@@ -709,28 +712,16 @@
 
   for (UInt_t i=0; i<npixels; i++)
-    {
-
+  {
       MHCalibrationChargePix &histhi = (MHCalibrationChargePix&)(*this)[i];
-
       if (histhi.IsExcluded())
 	continue;
 
-      const MExtractedSignalPix &pix = (*signal)[i];
-      
-      const Float_t sumhi = pix.GetExtractedSignalHiGain();
-      const Int_t   sathi = pix.IsHiGainSaturated() ? 1 : 0;
-
-      if (IsOscillations())
-        histhi.FillHistAndArray(sumhi);
-      else
-        histhi.FillHist(sumhi);
-        
-      histhi.AddSaturated(sathi);
-
       const Int_t aidx   = (*fGeom)[i].GetAidx();
       const Int_t sector = (*fGeom)[i].GetSector();
 
-      fSumhiarea[aidx]  += sumhi;
-      fSumhisector[sector]  += sumhi;
+      const MExtractedSignalPix &pix = (*signal)[i];
+
+      const Int_t sathi = pix.IsHiGainSaturated() ? 1 : 0;
+      histhi.AddSaturated(sathi);
       if (sathi)
       {
@@ -739,29 +730,52 @@
       }
 
-      if (IsLoGain())
-        {
-          MHCalibrationChargePix &histlo = (MHCalibrationChargePix&)(*this)(i);
+      if (pix.IsHiGainValid())
+      {
+          const Float_t sumhi = pix.GetExtractedSignalHiGain();
+          if (IsOscillations())
+              histhi.FillHistAndArray(sumhi);
+          else
+              histhi.FillHist(sumhi);
+
+          fSumhiarea[aidx]     += sumhi;
+          fSumhisector[sector] += sumhi;
+      }
+
+      if (!IsLoGain())
+          continue;
+
+      MHCalibrationChargePix &histlo = (MHCalibrationChargePix&)(*this)(i);
+
+      const Int_t satlo = pix.IsLoGainSaturated() ? 1 : 0;
+      histlo.AddSaturated(satlo);
+      if (satlo)
+      {
+          fSatloarea[aidx]++;
+          fSatlosector[sector]++;
+      }
+
+      /*
+       // Previously:
+       fSatloarea[aidx]     += pix.GetNumHiGainSaturated();
+       fSatlosector[sector] += pix.GetNumHiGainSaturated();
+       */
+
+      if (pix.IsLoGainValid())
+      {
           const Float_t sumlo = pix.GetExtractedSignalLoGain();
-          const Int_t   satlo = pix.IsLoGainSaturated() ? 1 : 0;
-          
+
           if (IsOscillations())
-            histlo.FillHistAndArray(sumlo);
+              histlo.FillHistAndArray(sumlo);
           else
-            histlo.FillHist(sumlo);
-          
-          histlo.AddSaturated(satlo); 
-          
-          fSumloarea[aidx]  += sumlo;
-          fSatloarea[aidx]  += satlo;
-          fSumlosector[sector]  += sumlo;
-          fSatlosector[sector]  += satlo;
-        }
-      
-    }
+              histlo.FillHist(sumlo);
+
+          fSumloarea[aidx]     += sumlo;
+          fSumlosector[sector] += sumlo;
+      }
+  }
 
   MRawEvtPixelIter pixel(fRawEvt);
   while (pixel.Next())
-    {
-      
+  {
       const UInt_t pixid = pixel.GetPixelId();
 
@@ -791,5 +805,5 @@
           fTimelosector[sector] += timelo;
         }
-    }
+  }
   
   for (UInt_t j=0; j<nareas; j++)
@@ -1125,13 +1139,12 @@
     pix.SetAbsTimeRms(rms);
 
-    const Float_t lowerlimit = (Float_t)first + fTimeLowerLimit;
-    const Float_t upperlimit = (Float_t)last  - fTimeUpperLimit;
+    const Float_t lowerlimit = (Float_t)first;// + fTimeLowerLimit;
+    const Float_t upperlimit = (Float_t)last ;// - fTimeUpperLimit;
 
     if (mean<lowerlimit)
     {
         *fLog << warn << hist.GetName() << ": Mean Arr.Time: "
-            << Form("%4.1f < %4.1f, %3.1f", mean, TMath::Floor(first)+fTimeLowerLimit, fTimeLowerLimit)
-            << " slices below " << Form("%2i", (Int_t)first) << endl;
-        bad.SetUncalibrated( MBadPixelsPix::kMeanTimeInFirstBin );
+            << Form("%4.1f < %4.1f, (%3.1f + %3.1)", mean, lowerlimit, fTimeLowerLimit, first) << endl;
+        bad.SetUncalibrated(MBadPixelsPix::kMeanTimeInFirstBin);
     }
 
@@ -1139,7 +1152,6 @@
     {
         *fLog << warn << hist.GetName() << ": Mean Arr.Time: "
-            << Form("%4.1f > %4.1f, %3.1f", mean, TMath::Floor(last)-fTimeUpperLimit, fTimeUpperLimit)
-            << " slices above " << Form("%2i", (Int_t)last) << endl;
-        bad.SetUncalibrated( MBadPixelsPix::kMeanTimeInLast2Bins );
+            << Form("%4.1f > %4.1f, (%3.1f - %3.1)", mean, upperlimit, fTimeUpperLimit, last) << endl;
+        bad.SetUncalibrated(MBadPixelsPix::kMeanTimeInLast2Bins);
     }
 }
Index: /trunk/MagicSoft/Mars/mhcalib/MHCalibrationPulseTimeCam.cc
===================================================================
--- /trunk/MagicSoft/Mars/mhcalib/MHCalibrationPulseTimeCam.cc	(revision 8191)
+++ /trunk/MagicSoft/Mars/mhcalib/MHCalibrationPulseTimeCam.cc	(revision 8192)
@@ -1,3 +1,5 @@
 /* ======================================================================== *\
+! $Name: not supported by cvs2svn $:$Id: MHCalibrationPulseTimeCam.cc,v 1.25 2006-11-01 15:48:31 tbretz Exp $
+! --------------------------------------------------------------------------
 !
 ! *
@@ -17,6 +19,7 @@
 !
 !   Author(s): Markus Gaug   02/2004 <mailto:markus@ifae.es>
+!   Author(s): Thomas Bretz <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2004
+!   Copyright: MAGIC Software Development, 2000-2006
 !
 !
@@ -427,4 +430,7 @@
 	}
 
+      if (*max > fSaturationLimit)
+	continue;
+
       start   = pixel.GetLoGainSamples();
       end     = start + pixel.GetNumLoGainSamples();
@@ -436,5 +442,5 @@
 	    {
 	      max = p;
-	      maxpos = p-start+pixel.GetNumHiGainSamples() - 1;
+	      maxpos = p-start-1 + pixel.GetNumHiGainSamples();
 	    }
 	  p++;
@@ -552,10 +558,19 @@
 {
 
-
-  if (hist.IsEmpty() || hist.IsOnlyOverflow() || hist.IsOnlyUnderflow())
-    {
-      *fLog << warn << GetDescriptor() << ": Only over- or underflow in " << hist.GetName() << endl;
+  if (hist.IsEmpty())
+  {
+      *fLog << warn << hist.GetName() << ": Histogram empty." << endl;
       return;
-    }  
+  }
+  if (hist.IsOnlyOverflow())
+  {
+      *fLog << warn << hist.GetName() << ": Histogram contains only overflows." << endl;
+      return;
+  }
+  if (hist.IsOnlyUnderflow())
+  {
+      *fLog << warn << hist.GetName() << ": Histogram contains only underflows." << endl;
+      return;
+  }
 
   hist.BypassFit();
Index: /trunk/MagicSoft/Mars/mhcalib/MHCalibrationRelTimeCam.cc
===================================================================
--- /trunk/MagicSoft/Mars/mhcalib/MHCalibrationRelTimeCam.cc	(revision 8191)
+++ /trunk/MagicSoft/Mars/mhcalib/MHCalibrationRelTimeCam.cc	(revision 8192)
@@ -128,6 +128,6 @@
 const Float_t MHCalibrationRelTimeCam::fgNumHiGainSaturationLimit = 0.25;
 const UInt_t  MHCalibrationRelTimeCam::fgReferencePixel = 1;
-const Int_t   MHCalibrationRelTimeCam::fgNbins      = 300;
-const Axis_t  MHCalibrationRelTimeCam::fgFirst      = -4.975;
+const Int_t   MHCalibrationRelTimeCam::fgNbins      = 400;
+const Axis_t  MHCalibrationRelTimeCam::fgFirst      = -9.975;
 const Axis_t  MHCalibrationRelTimeCam::fgLast       = 10.025;
 const Float_t MHCalibrationRelTimeCam::fgProbLimit  = 0.0;
@@ -324,34 +324,65 @@
   fSumareahi  .Reset();
   fSumarealo  .Reset();
-  fSumsectorhi.Reset(); 
+  fSumsectorhi.Reset();
   fSumsectorlo.Reset();
-  fNumareahi  .Reset(); 
+  fNumareahi  .Reset();
   fNumarealo  .Reset();
-  fNumsectorhi.Reset(); 
+  fNumsectorhi.Reset();
   fNumsectorlo.Reset();
 
   const MArrivalTimePix &refpix = (*arrtime)[fReferencePixel];
-  const Float_t reftime = refpix.IsHiGainSaturated()
-     ? refpix.GetArrivalTimeLoGain() : refpix.GetArrivalTimeHiGain();
+  // FIXME: What do we do if pixel is invalid?
+  if (!refpix.IsArrivalTimeValid())
+  {
+      gLog << warn << "WARNING - Arrival time in refrence pixel " << fReferencePixel << " invalid." << endl;
+      return kTRUE;
+  }
+
+  const Float_t reftime = refpix.GetArrivalTime();
+//      refpix.IsHiGainSaturated()
+//     ? refpix.GetArrivalTimeLoGain() : refpix.GetArrivalTimeHiGain();
 
   for (Int_t i=0; i<npixels; i++)
-    {
-
+  {
       MHCalibrationPix &histhi = (*this)[i];
-
       if (histhi.IsExcluded())
-	continue;
-
-      const MArrivalTimePix &pix = (*arrtime)[i];
+          continue;
+
       const Int_t aidx   = (*fGeom)[i].GetAidx();
       const Int_t sector = (*fGeom)[i].GetSector();
 
-      if (pix.IsHiGainSaturated() && IsLoGain())
-        { 
+      const MArrivalTimePix &pix = (*arrtime)[i];
+
+      // If hi-gain arrival time has been extracted successfully
+      // fill hi-gain histograms and arrays
+      if (pix.IsHiGainValid() && !pix.IsHiGainSaturated())
+      {
+          const Float_t time = pix.GetArrivalTimeHiGain();
+
+          if (IsOscillations())
+              histhi.FillHistAndArray(time-reftime);
+          else
+              histhi.FillHist(time-reftime);
+
+          fSumareahi  [aidx]   += time;
+          fNumareahi  [aidx]   ++;
+          fSumsectorhi[sector] += time;
+          fNumsectorhi[sector] ++;
+      }
+
+      if (!pix.IsHiGainSaturated())
+          continue;
+
+      histhi.AddSaturated(1);
+
+      // If lo-gain arrival time has been extracted successfully,
+      // the hi-gain has saturateed and the lo-gain is switched on
+      // fill hi-gain histograms and arrays
+      if (pix.IsLoGainValid() && IsLoGain())
+      {
           const Float_t time = pix.GetArrivalTimeLoGain();
-          histhi.AddSaturated(1); 
 
           MHCalibrationPix &histlo = (*this)(i);
-	  if (IsOscillations())
+          if (IsOscillations())
               histlo.FillHistAndArray(time-reftime);
           else
@@ -362,21 +393,7 @@
           fSumsectorlo[sector] += time;
           fNumsectorlo[sector] ++;
-        }
-      else
-        {
-          const Float_t time = pix.GetArrivalTimeHiGain();
-
-          if (IsOscillations())
-              histhi.FillHistAndArray(time-reftime);
-          else
-              histhi.FillHist(time-reftime);
-
-          fSumareahi  [aidx]   += time;
-          fNumareahi  [aidx]   ++;
-          fSumsectorhi[sector] += time;
-          fNumsectorhi[sector] ++;
-        }
-    }
-  
+      }
+  }
+
   for (Int_t j=0; j<nareas; j++)
     {
@@ -848,19 +865,23 @@
 Int_t MHCalibrationRelTimeCam::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
 {
-
-  Bool_t rc = kFALSE;
-
-  if (IsEnvDefined(env, prefix, "ReferenceFile", print))
-    {
-      SetReferenceFile(GetEnvValue(env,prefix,"ReferenceFile",fReferenceFile.Data()));
-      rc = kTRUE;
-    }
-
-  TEnv refenv(fReferenceFile);
-
-  fInnerRefTime = refenv.GetValue("InnerRefTime",fInnerRefTime);
-  fOuterRefTime = refenv.GetValue("OuterRefTime",fOuterRefTime);
-
-  return MHCalibrationCam::ReadEnv(env,prefix,print) ? kTRUE : rc;
-
-}
+    Bool_t rc = kFALSE;
+
+    if (IsEnvDefined(env, prefix, "ReferenceFile", print))
+    {
+        SetReferenceFile(GetEnvValue(env, prefix, "ReferenceFile", fReferenceFile.Data()));
+        rc = kTRUE;
+    }
+
+    if (IsEnvDefined(env, prefix, "ReferencePixel", print))
+    {
+        SetReferencePixel(GetEnvValue(env, prefix, "ReferencePixel", (Int_t)fReferencePixel));
+        rc = kTRUE;
+    }
+
+    TEnv refenv(fReferenceFile);
+
+    fInnerRefTime = refenv.GetValue("InnerRefTime", fInnerRefTime);
+    fOuterRefTime = refenv.GetValue("OuterRefTime", fOuterRefTime);
+
+    return MHCalibrationCam::ReadEnv(env,prefix,print) ? kTRUE : rc;
+}
Index: /trunk/MagicSoft/Mars/mhcalib/MHGausEvents.h
===================================================================
--- /trunk/MagicSoft/Mars/mhcalib/MHGausEvents.h	(revision 8191)
+++ /trunk/MagicSoft/Mars/mhcalib/MHGausEvents.h	(revision 8192)
@@ -146,6 +146,6 @@
   const Bool_t   IsFourierSpectrumOK()   const { return TESTBIT(fFlags,kFourierSpectrumOK); }
   const Bool_t   IsGausFitOK()           const { return TESTBIT(fFlags,kGausFitOK);         }
-  const Bool_t   IsOnlyOverflow()        const { return fHGausHist.GetEntries() == fHGausHist.GetBinContent(fNbins+1); }
-  const Bool_t   IsOnlyUnderflow()       const { return fHGausHist.GetEntries() == fHGausHist.GetBinContent(0);        }
+  const Bool_t   IsOnlyOverflow()        const { return fHGausHist.GetEntries()>0 && fHGausHist.GetEntries() == fHGausHist.GetBinContent(fNbins+1); }
+  const Bool_t   IsOnlyUnderflow()       const { return fHGausHist.GetEntries()>0 && fHGausHist.GetEntries() == fHGausHist.GetBinContent(0);        }
 
   // Prints
Index: /trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc
===================================================================
--- /trunk/MagicSoft/Mars/msignal/MExtractTimeAndCharge.cc	(revision 8191)
+++ /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 8191)
+++ /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:
