Index: trunk/MagicSoft/Mars/mhbase/MH.cc
===================================================================
--- trunk/MagicSoft/Mars/mhbase/MH.cc	(revision 8888)
+++ trunk/MagicSoft/Mars/mhbase/MH.cc	(revision 8892)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MH.cc,v 1.36 2007-08-25 15:30:24 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MH.cc,v 1.37 2008-05-19 14:04:12 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -212,5 +212,40 @@
 // --------------------------------------------------------------------------
 //
-// Applies a given binning to a 1D-histogram
+// If labels are set for this axis the correct MBinning corresponding
+// to the existing label range is returned (this is necessary to
+// maintain the correct number of bins in the histogram)
+// otherwise the given binning is returned.
+//
+MBinning MH::GetBinningForLabels(TAxis &x, const MBinning *bins)
+{
+    if (!x.GetLabels())
+        return *bins;
+
+    const Int_t n = TMath::Max(x.GetLabels()->GetEntries(), 1);
+    return MBinning(n, 0, n);
+}
+
+// --------------------------------------------------------------------------
+//
+// If Labels are set this function deletes the fXbins Array from
+// the axis (which makes the axis a variable bin-size axis)
+// and sets the Nbins, Xmin and Xmax according to the number of labels.
+//
+void MH::RestoreBinningForLabels(TAxis &x)
+{
+    if (!x.GetLabels())
+        return;
+
+    const Int_t n = TMath::Max(x.GetLabels()->GetEntries(), 1);
+    x.Set(n, 0, n);
+
+    const_cast<TArrayD*>(x.GetXbins())->Set(0);
+}
+
+// --------------------------------------------------------------------------
+//
+// Applies a given binning to a 1D-histogram. In case the axis has labels
+// (e.g. GetXaxis()->GetLabels()) the binning is set according to the
+// labels.
 //
 void MH::SetBinning(TH1 *h, const MBinning *binsx)
@@ -225,4 +260,5 @@
 #endif
 
+#if ROOT_VERSION_CODE < ROOT_VERSION(5,12,00)
     // All this is reset by TAxis::Set
     const TAttAxis att(x);
@@ -247,4 +283,9 @@
     x.SetTimeDisplay(tm);
     x.SetTimeFormat(tf);
+#else
+    if (!x.GetLabels())
+        h->SetBins(binsx->GetNumBins(), binsx->GetEdges());
+#endif
+
 
 #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
@@ -255,5 +296,7 @@
 // --------------------------------------------------------------------------
 //
-// Applies given binnings to the two axis of a 2D-histogram
+// Applies given binnings to the two axis of a 2D-histogram.
+// In case the axis has labels (e.g. GetXaxis()->GetLabels())
+// the binning is set according to the labels.
 //
 void MH::SetBinning(TH2 *h, const MBinning *binsx, const MBinning *binsy)
@@ -261,4 +304,7 @@
     TAxis &x = *h->GetXaxis();
     TAxis &y = *h->GetYaxis();
+
+    const MBinning bx(GetBinningForLabels(x, binsx));
+    const MBinning by(GetBinningForLabels(y, binsy));
 
     //
@@ -270,4 +316,5 @@
 #endif
 
+#if ROOT_VERSION_CODE < ROOT_VERSION(5,12,00)
     // All this is reset by TAxis::Set
     const TAttAxis attx(x);
@@ -283,6 +330,6 @@
     // TH1D::fNcells must be set correctly.
     //
-    h->SetBins(binsx->GetNumBins(), 0, 1,
-               binsy->GetNumBins(), 0, 1);
+    h->SetBins(bx.GetNumBins(), 0, 1,
+               by.GetNumBins(), 0, 1);
 
     //
@@ -290,6 +337,6 @@
     // in one of the two given histograms
     //
-    x.Set(binsx->GetNumBins(), binsx->GetEdges());
-    y.Set(binsy->GetNumBins(), binsy->GetEdges());
+    x.Set(bx.GetNumBins(), bx.GetEdges());
+    y.Set(by.GetNumBins(), by.GetEdges());
 
     // All this is reset by TAxis::Set
@@ -300,4 +347,11 @@
     x.SetTimeFormat(tfx);
     y.SetTimeFormat(tfy);
+#else
+    h->SetBins(bx.GetNumBins(), bx.GetEdges(),
+               by.GetNumBins(), by.GetEdges());
+#endif
+
+    RestoreBinningForLabels(x);
+    RestoreBinningForLabels(y);
 
 #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
@@ -310,4 +364,6 @@
 //
 // Applies given binnings to the three axis of a 3D-histogram
+// In case the axis has labels (e.g. GetXaxis()->GetLabels())
+// the binning is set according to the labels.
 //
 void MH::SetBinning(TH3 *h, const MBinning *binsx, const MBinning *binsy, const MBinning *binsz)
@@ -319,4 +375,8 @@
     TAxis &y = *h->GetYaxis();
     TAxis &z = *h->GetZaxis();
+
+    const MBinning bx(GetBinningForLabels(x, binsx));
+    const MBinning by(GetBinningForLabels(y, binsy));
+    const MBinning bz(GetBinningForLabels(z, binsz));
 
 #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
@@ -326,4 +386,5 @@
 #endif
 
+#if ROOT_VERSION_CODE < ROOT_VERSION(5,12,00)
     // All this is reset by TAxis::Set
     const TAttAxis attx(x);
@@ -336,4 +397,5 @@
     const TString  tfy(y.GetTimeFormat());
     const TString  tfz(z.GetTimeFormat());
+#endif
 
     //
@@ -342,7 +404,7 @@
     // TH1D::fNcells must be set correctly.
     //
-    h->SetBins(binsx->GetNumBins(), 0, 1,
-               binsy->GetNumBins(), 0, 1,
-               binsz->GetNumBins(), 0, 1);
+    h->SetBins(bx.GetNumBins(), 0, 1,
+               by.GetNumBins(), 0, 1,
+               bz.GetNumBins(), 0, 1);
 
     //
@@ -350,8 +412,13 @@
     // in one of the two given histograms
     //
-    x.Set(binsx->GetNumBins(), binsx->GetEdges());
-    y.Set(binsy->GetNumBins(), binsy->GetEdges());
-    z.Set(binsz->GetNumBins(), binsz->GetEdges());
-
+    x.Set(bx.GetNumBins(), bx.GetEdges());
+    y.Set(by.GetNumBins(), by.GetEdges());
+    z.Set(bz.GetNumBins(), bz.GetEdges());
+
+    RestoreBinningForLabels(x);
+    RestoreBinningForLabels(y);
+    RestoreBinningForLabels(z);
+
+#if ROOT_VERSION_CODE < ROOT_VERSION(5,12,00)
     // All this is reset by TAxis::Set
     attx.Copy(x);
@@ -364,4 +431,5 @@
     y.SetTimeFormat(tfy);
     z.SetTimeFormat(tfz);
+#endif
 
 #if ROOT_VERSION_CODE < ROOT_VERSION(3,03,03)
Index: trunk/MagicSoft/Mars/mhbase/MH.h
===================================================================
--- trunk/MagicSoft/Mars/mhbase/MH.h	(revision 8888)
+++ trunk/MagicSoft/Mars/mhbase/MH.h	(revision 8892)
@@ -26,4 +26,7 @@
     Byte_t fSerialNumber;   // Serial number (eg of telecope)
     UInt_t fNumExecutions;  // Number of calls to Fill function
+
+    static MBinning GetBinningForLabels(TAxis &x, const MBinning *bins);
+    static void RestoreBinningForLabels(TAxis &x);
 
 public:
