Index: trunk/MagicSoft/Mars/mjobs/MJPedestal.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJPedestal.cc	(revision 8301)
+++ trunk/MagicSoft/Mars/mjobs/MJPedestal.cc	(revision 8304)
@@ -102,6 +102,6 @@
 const TString  MJPedestal::fgReferenceFile   = "mjobs/pedestalref.rc";
 const TString  MJPedestal::fgBadPixelsFile   = "mjobs/badpixels_0_559.rc";
-const Float_t  MJPedestal::fgExtractWinLeft  = 2.5;
-const Float_t  MJPedestal::fgExtractWinRight = 4.5;
+const Float_t  MJPedestal::fgExtractWinLeft  = 0;
+const Float_t  MJPedestal::fgExtractWinRight = 0;
 
 // --------------------------------------------------------------------------
@@ -859,28 +859,15 @@
         return kTRUE;
 
-    Int_t numhigainsamples = 0;
-    Int_t numlogainsamples = 0;
-
-    Float_t meanpulsetime  = 0.;
-    Float_t rmspulsetime   = 0.;
-
-    if (IsUseMC())
-    {
-        //
-        // FIXME:
-        // The MC cannot run over the first 2000 pedestal events since almost all
-        // events are empty, therefore a pulse pos. check is not possible, either.
-        // For the moment, have to fix the problem hardcoded...
-        //
-        //            MMcEvt *evt = (MMcEvt*)plist.FindObject("MMcEvt");
-        //            const Float_t meanpulsetime = evt->GetFadcTimeJitter();
-        meanpulsetime = 4.5;
-        rmspulsetime  = 1.0;
-
-        numhigainsamples = 15;
-        numlogainsamples = 15;
-
-    }
-    else
+    // FIXME:
+    // The MC cannot run over the first 2000 pedestal events since almost all
+    // events are empty, therefore a pulse pos. check is not possible, either.
+    // For the moment, have to fix the problem hardcoded...
+    //
+    //            MMcEvt *evt = (MMcEvt*)plist.FindObject("MMcEvt");
+    //            const Float_t meanpulsetime = evt->GetFadcTimeJitter();
+    Float_t meanpulsetime  = 4.5;
+    Float_t rmspulsetime   = 1.0;
+
+    if (!IsUseMC())
     {
         if (fIsPixelCheck)
@@ -909,14 +896,4 @@
         meanpulsetime = cam->GetAverageArea(0).GetHiGainMean();
         rmspulsetime  = cam->GetAverageArea(0).GetHiGainRms();
-
-        MRawEvtData *data = (MRawEvtData*)plist.FindObject("MRawEvtData");
-        if (!data)
-        {
-            *fLog << err << "MRawEvtData not found... abort." << endl;
-            return kFALSE;
-        }
-
-        numhigainsamples = data->GetNumHiGainSamples();
-        numlogainsamples = data->GetNumLoGainSamples();
     }
 
@@ -924,66 +901,93 @@
     *fLog << meanpulsetime << "+-" << rmspulsetime << endl;
 
-    const MExtractTimeAndCharge *ext = dynamic_cast<MExtractTimeAndCharge*>(fExtractor);
-
-    //
-    // Get the ranges for the new extractor setting
-    //
-    const Int_t newfirst = TMath::Nint(meanpulsetime-fExtractWinLeft);
-
-    Int_t wshigain = ext ? ext->GetWindowSizeHiGain() : 6;
-    if (wshigain > 6)
-        wshigain = 6;
-
-    Int_t wslogain = ext ? ext->GetWindowSizeLoGain() : 4;
-    if (wslogain > 4)
-        wslogain = 4;
-
-    const Int_t newlast  = TMath::Nint(meanpulsetime+fExtractWinRight);
-
-    *fLog << underline;
-    *fLog << "Try to set new range limits: (" << newfirst << "," << newlast;
-    *fLog << "+" << wshigain << "," << newfirst-1 << "," << newlast << "+";
-    *fLog << wslogain << ")" << endl;
-
-    //
-    // Check the ranges for the new extractor setting
-    //
-    if (newfirst < 0)
-    {
-        //*fLog << err << "Pulse is too much to the left, cannot go below 0!" << endl;
-        //return -1;
-    }
-    if (newlast+wshigain > numhigainsamples+numlogainsamples-1)
-    {
-        *fLog << err << "Pulse is too much to the right, cannot go beyond limits: ";
-        *fLog << numhigainsamples << "+" << numlogainsamples << "-1" << endl;
-        *fLog << " Cannot extract at all!" << endl;
-        return -3;
-    }
-    if (newlast+wslogain > numlogainsamples)
-    {
-        *fLog << err << "Pulse is too much to the right, cannot go beyond logain limits!" << endl;
-        *fLog << endl;
-        *fLog << "Try to use a different extractor (e.g. with a window size of only 4 sl.) or:" << endl;
-        *fLog << "Set the limit to a lower value (callisto.rc):" << endl;
-        *fLog << "    MJPedestalY2:ExtractWinRight: 4.5" << endl;
-        *fLog << "(ATTENTION, you will lose late cosmics pulses!)" << endl;
+    MExtractTimeAndCharge *ext = dynamic_cast<MExtractTimeAndCharge*>(fExtractor);
+    if (!ext)
+    {
+        *fLog << warn << "WARNING - no extractor found inheriting from MExtractTimeAndCharge... no pulse position check." << endl;
+        return kTRUE;
+    }
+
+    const Int_t hi0 = ext->GetHiGainFirst();
+    const Int_t lo1 = ext->GetLoGainLast();
+    Int_t hi1 = ext->GetHiGainLast();
+    Int_t lo0 = ext->GetLoGainFirst();
+
+    //
+    // Get the ranges for the new extractor setting. The window
+    // size is always rounded to the next higher integer.
+    //
+    const Int_t wshigain = ext->GetWindowSizeHiGain();
+    const Int_t wslogain = ext->GetWindowSizeLoGain();
+
+    //
+    // Here we calculate the end of the lo-gain range
+    // as it is done in MExtractTimeAndCharge
+    //
+    const Double_t poshi  = meanpulsetime;
+    const Double_t poslo  = poshi + ext->GetOffsetLoGain();
+    const Double_t poslo2 = poslo + ext->GetLoGainStartShift();
+
+    //
+    // Do the right side checks range checks
+    //
+    if (poshi+wshigain+fExtractWinRight > hi1-0.5)
+    {
+        *fLog << err;
+        *fLog << "ERROR - Pulse is too much to the right, out of hi-gain range [";
+        *fLog << hi0 << "," << hi1 << "]" << endl;
         *fLog << endl;
         return -2;
     }
 
+    if (poslo+wslogain+fExtractWinRight > lo1-0.5)
+    {
+        *fLog << err;
+        *fLog << "ERROR - Pulse is too much to the right, out of lo-gain range [";
+        *fLog << lo0 << "," << lo1 << "]" << endl;
+        return -2;
+    }
+
+    //
+    // Do the left side checks range checks
+    //
+    if (poshi-fExtractWinLeft < hi0+0.5)
+    {
+        *fLog << err;
+        *fLog << "ERROR - Pulse is too much to the left, out of hi-gain range [";
+        *fLog << hi0 << "," << hi1 << "]" << endl;
+        return -3;
+    }
+
+    if (poslo2-fExtractWinLeft < lo0+0.5)
+    {
+        *fLog << warn;
+        *fLog << "WARNING - Pulse is too much to the left, out of lo-gain range [";
+        *fLog << lo0 << "," << lo1 << "]" << endl;
+        *fLog << "Trying to match extraction window and pulse position..." << endl;
+
+        //
+        // Set and store the new ranges
+        //
+        Int_t shift = 0;
+        while (poslo2-fExtractWinLeft < lo0+0.5)
+        {
+            hi1--;
+            lo0--;
+
+            if (poshi+wshigain+fExtractWinRight > hi1-0.5)
+            {
+                *fLog << err << "ERROR - No proper extraction window found.... abort." << endl;
+                return -3;
+            }
+        }
+
+        *fLog << "Changed extraction window by " << shift;
+        *fLog << "slices to hi-gain [" << hi0 << "," << hi1;
+        *fLog << "] and lo-gain [" << lo0 << "," << lo1 << "]" << endl;
+
+        ext->SetRange(hi0, hi1, lo0, lo1);
+    }
+
     return kTRUE;
-/*
-    //
-    // Set and store the new ranges
-    //
-    const Int_t hi0 = newfirst;
-    const Int_t hi1 = newlast+wshigain;
-
-    const Int_t lo0 = newfirst>0 ? newfirst-1 : newfirst;
-    const Int_t lo1 = numlogainsamples-1;
-
-    fExtractor->SetRange(hi0, hi1, lo0, lo1);
-    return kTRUE;*/
 }
 
