Index: trunk/MagicSoft/Mars/mhbase/MH.cc
===================================================================
--- trunk/MagicSoft/Mars/mhbase/MH.cc	(revision 3144)
+++ trunk/MagicSoft/Mars/mhbase/MH.cc	(revision 3151)
@@ -894,8 +894,8 @@
 Int_t MH::CutEdges(TH1 *h, Int_t nbins)
 {
-    TAxis* axe = h->GetXaxis();
-
-    const Int_t min1   = axe->GetFirst();
-    const Int_t max1   = axe->GetLast();
+    TAxis &axe = *h->GetXaxis();
+
+    const Int_t min1   = axe.GetFirst();
+    const Int_t max1   = axe.GetLast();
     const Int_t range1 = max1-min1;
 
@@ -929,27 +929,28 @@
 
     //
-    // Check for rebinning
-    //
-    if (nbins < 1)
-    {
-        axe->SetRange(min2,max2);
-        return axe->GetLast()-axe->GetFirst();
-    }
-
-    //
     // Appying TAxis->SetRange before ReBin does not work ...
     // But this workaround helps quite fine
     //
-    const Axis_t min = h->GetBinLowEdge(min2);
-    const Axis_t max = h->GetBinLowEdge(max2)+h->GetBinWidth(max2);
-
-    const Int_t ngroup = (int)((max2-min2)*h->GetNbinsX()/nbins/(max1-min1));
-
-    if (ngroup > 1)
-        h->Rebin(ngroup);
-
-    axe->SetRangeUser(min,max);
-
-    return axe->GetLast()-axe->GetFirst();
+    Axis_t min = h->GetBinLowEdge(min2);
+    Axis_t max = h->GetBinLowEdge(max2)+h->GetBinWidth(max2);
+
+    Int_t nbins2 = max2-min2;
+    //
+    // Check for rebinning
+    //
+    if (nbins > 0)
+      {
+        const Int_t ngroup = (int)(nbins2*h->GetNbinsX()/nbins/(max1-min1));
+        if (ngroup > 1)
+          {
+            h->Rebin(ngroup);
+            nbins2 /= ngroup;
+          }
+      }
+    
+    Int_t newbins = 0;
+    FindGoodLimits(nbins2, newbins, min, max, kFALSE);
+    axe.SetRangeUser(min,max);
+    return axe.GetLast()-axe.GetFirst();
 }
 
