Index: trunk/MagicSoft/Mars/mhbase/MH3.cc
===================================================================
--- trunk/MagicSoft/Mars/mhbase/MH3.cc	(revision 8892)
+++ trunk/MagicSoft/Mars/mhbase/MH3.cc	(revision 8893)
@@ -18,5 +18,5 @@
 !   Author(s): Thomas Bretz  2002 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2007
+!   Copyright: MAGIC Software Development, 2000-2008
 !
 !
@@ -45,6 +45,6 @@
 //
 //
-// Binning name
-// ============
+// Binning/Binning  name
+// =====================
 //
 // The axis binning is retrieved from the parameter list, too. Create a
@@ -57,4 +57,11 @@
 // Instead of BinningMyHistName[XYZ] the parameter list will be searched
 // for BinningMyXBinning, BinningMyYBins and BinningMyHistNameZ
+//
+// If you don't want to use a MBinning object from the parameter list
+// you can also set one directly, for example
+//    MBinning bins(10, 0, 1);
+//    mh3.SetBinningX(&bins);
+// You must not delete the MBinning object before the class has been
+// PreProcessed.
 //
 //
@@ -71,13 +78,58 @@
 //
 //
-// For example:
-//   MH3 myhist("MHillas.fLength");
-//   myhist.SetName("MyHist");
-//   myhist.SetScaleX(geomcam.GetConvMm2Deg()); //convert length to degree
-//   MBinning bins("BinningMyHistX");
-//   bins.SetEdges(10, 0, 150);
-//   plist.AddToList(&myhist);
-//   plist.AddToList(&bins);
-//
+// Labels
+// ======
+//
+// To use labels at an axis you have to initialize this for the axis
+// by either calling InitLabels(Labels_t) or setiting a DefaultLabel.
+// For the axis for which the labels have been initialized the
+// number returned by the given corresponding phrase is converted
+// to int with TMath::Nint and used as a label. If you want to replace
+// this id by a named label you can call DefineLabel to do that.
+// Several ids can be replaced by the same label. If you define
+// named labels for every label which was not defined the default
+// is used, if any, otherwise an unnamed label is created.
+//
+// In the case of an axis with labels the axis-title cannot be
+// set via a MBinning, because the MBinning is not evaluated.
+//
+// Please note that for some reason not all combinations of
+// labels, dimensions and weights are available in the root-
+// histogram classes. Please check the MH3::Fill function to see
+// whether your combination is supported.
+//
+//
+// Examples:
+// =========
+//
+//   1) MH3 myhist("MHillas.fLength");
+//      myhist.SetName("MyHist");
+//      myhist.SetScaleX(geomcam.GetConvMm2Deg()); //convert length to degree
+//      MBinning bins("BinningMyHistX", "Title for my x-axis [Hz]");
+//      bins.SetEdges(10, 0, 150);
+//      plist.AddToList(&bins);
+//
+//   2) MH3 myhist("MHillas.fLength");
+//      myhist.SetName("MyHist;MyX");
+//      myhist.SetTitle("Histogram Title;X-Title [mm];Counts");
+//      MBinning bins("BinningMyX");
+//      bins.SetEdges(10, 0, 150);
+//      plist.AddToList(&bins);
+//
+//   3) MH3 myhist("MTriggerPatter.GetUnprescaled");
+//      myhist.SetWeight("1./MRawRunHeader.GetRunLength");
+//      myhist.SetTitle("Rate of the trigger pattern [Hz];Run Number;Trigger Pattern;Rate [Hz]");
+//      myhist.InitLabels(MH3::kLabelsXY);
+//      myhist.DefaultLabelY("UNKNOWN");     // Lvl1
+//      myhist.DefineLabelY( 1, "Trig");     // Lvl1
+//      myhist.DefineLabelY( 2, "Cal");      // Cal
+//      myhist.DefineLabelY( 4, "Trig");     // Lvl2
+//      myhist.DefineLabelY( 8, "Ped");      // Ped
+//
+//
+// Class Version 3:
+// ----------------
+//   - MData      *fData[3];
+//   + MData      *fData[4];
 //
 // Class Version 2:
@@ -97,4 +149,6 @@
 #include <fstream>
 
+#include <TObjString.h>
+
 //#include <TPad.h>
 #include <TStyle.h>
@@ -122,9 +176,39 @@
 // --------------------------------------------------------------------------
 //
+// Set fStyleBits to 0, Reset fBins to NULL, fScale to 1, set name and title
+// to gsDefName and gsDefTitle and if fHist!=NULL UseCurrentStyle and
+// SetDirectory(0)
+//
+void MH3::Init()
+{
+    fStyleBits = 0;
+
+    fData[3] = NULL;
+
+    fBins[0] = NULL;
+    fBins[1] = NULL;
+    fBins[2] = NULL;
+
+    fScale[0] = 1;
+    fScale[1] = 1;
+    fScale[2] = 1;
+
+    fName  = gsDefName;
+    fTitle = gsDefTitle;
+
+    if (!fHist)
+        return;
+
+    fHist->UseCurrentStyle();
+    fHist->SetDirectory(NULL);
+}
+
+// --------------------------------------------------------------------------
+//
 // Default constructor.
 //
-MH3::MH3(const Int_t dim, Type_t type)
-    : fDimension(dim), fHist(NULL), fStyleBits(0)
-{
+MH3::MH3(const Int_t dim, Type_t type) : fDimension(dim), fHist(NULL)
+{
+    // FIXME?
     switch (type)
     {
@@ -167,20 +251,5 @@
     fData[2] = NULL;
 
-    fBins[0] = NULL;
-    fBins[1] = NULL;
-    fBins[2] = NULL;
-
-    fName  = gsDefName;
-    fTitle = gsDefTitle;
-
-    if (fHist)
-    {
-        fHist->SetDirectory(NULL);
-        fHist->UseCurrentStyle();
-    }
-
-    fScale[0] = 1;
-    fScale[1] = 1;
-    fScale[2] = 1;
+    Init();
 }
 
@@ -190,8 +259,8 @@
 // description see the class description above.
 //
-MH3::MH3(const char *memberx)
-    : fDimension(1), fStyleBits(0)
+MH3::MH3(const char *memberx, Type_t type) : fDimension(1)
 {
     fHist = new TH1D;
+    fHist->SetYTitle("Counts");
 
     fData[0] = new MDataPhrase(memberx);
@@ -199,18 +268,5 @@
     fData[2] = NULL;
 
-    fBins[0] = NULL;
-    fBins[1] = NULL;
-    fBins[2] = NULL;
-
-    fName  = gsDefName;
-    fTitle = gsDefTitle;
-
-    fHist->UseCurrentStyle();
-    fHist->SetDirectory(NULL);
-    fHist->SetYTitle("Counts");
-
-    fScale[0] = 1;
-    fScale[1] = 1;
-    fScale[2] = 1;
+    Init();
 }
 
@@ -219,6 +275,5 @@
 // Adapt a given histogram
 //
-MH3::MH3(const TH1 &h1)
-    : fDimension(1), fStyleBits(0)
+MH3::MH3(const TH1 &h1) : fDimension(1)
 {
     if (h1.InheritsFrom(TH3::Class()))
@@ -227,17 +282,22 @@
         fDimension = 2;
 
+    if (h1.InheritsFrom(TProfile2D::Class()))
+        fDimension = -2;
+    if (h1.InheritsFrom(TProfile::Class()))
+        fDimension = -1;
+
+    fHist = (TH1*)h1.Clone();
+
     fData[0] = NULL;
     fData[1] = NULL;
     fData[2] = NULL;
 
-    fBins[0] = NULL;
-    fBins[1] = NULL;
-    fBins[2] = NULL;
-
     switch (fDimension)
     {
     case 3:
+    case -2:
         fData[2] = new MDataPhrase(h1.GetZaxis()->GetTitle());
     case 2:
+    case -1:
         fData[1] = new MDataPhrase(h1.GetYaxis()->GetTitle());
     case 1:
@@ -245,13 +305,5 @@
     }
 
-    fName  = gsDefName;
-    fTitle = gsDefTitle;
-
-    fHist = (TH1*)h1.Clone();
-    fHist->SetDirectory(NULL);
-
-    fScale[0] = 1;
-    fScale[1] = 1;
-    fScale[2] = 1;
+    Init(); // Before without SeUseCurrentStyle!
 }
 
@@ -263,7 +315,18 @@
 //
 MH3::MH3(const char *memberx, const char *membery, Type_t type)
-    : fDimension(type==kHistogram?2:-1), fStyleBits(0)
-{
-    fHist = type==kHistogram ? static_cast<TH1*>(new TH2D) : static_cast<TH1*>(new TProfile); //new TH2D;
+    : fDimension(type&kProfile?-1:2)
+{
+
+    switch (TMath::Abs(fDimension))
+    {
+    case 2:
+        fHist = static_cast<TH1*>(new TH2D);
+        break;
+    case -1:
+        fHist = static_cast<TH1*>(new TProfile);
+        break;
+    }
+
+    fHist->SetZTitle(fDimension>0?"Counts":"Average");
 
     fData[0] = new MDataPhrase(memberx);
@@ -271,18 +334,5 @@
     fData[2] = NULL;
 
-    fBins[0] = NULL;
-    fBins[1] = NULL;
-    fBins[2] = NULL;
-
-    fName  = gsDefName;
-    fTitle = gsDefTitle;
-
-    fHist->UseCurrentStyle();
-    fHist->SetDirectory(NULL);
-    fHist->SetZTitle(fDimension>0?"Counts":"Average");
-
-    fScale[0] = 1;
-    fScale[1] = 1;
-    fScale[2] = 1;
+    Init();
 }
 
@@ -294,7 +344,7 @@
 //
 MH3::MH3(const char *memberx, const char *membery, const char *memberz, Type_t type)
-    : fDimension(type==kHistogram?3:-2), fStyleBits(0)
-{
-    fHist = type==kHistogram ? static_cast<TH1*>(new TH3D) : static_cast<TH1*>(new TProfile2D); //new TH2D;
+    : fDimension(type==kHistogram?3:-2)
+{
+    fHist = type&kProfile ? static_cast<TH1*>(new TProfile2D) : static_cast<TH1*>(new TH3D);
 
     fData[0] = new MDataPhrase(memberx);
@@ -302,17 +352,5 @@
     fData[2] = new MDataPhrase(memberz);
 
-    fBins[0] = NULL;
-    fBins[1] = NULL;
-    fBins[2] = NULL;
-
-    fName  = gsDefName;
-    fTitle = gsDefTitle;
-
-    fHist->UseCurrentStyle();
-    fHist->SetDirectory(NULL);
-
-    fScale[0] = 1;
-    fScale[1] = 1;
-    fScale[2] = 1;
+    Init();
 }
 
@@ -325,7 +363,118 @@
     delete fHist;
 
-    for (int i=0; i<3; i++)
+    for (int i=0; i<4; i++)
         if (fData[i])
             delete fData[i];
+
+    for (int i=0; i<3; i++)
+        if (fLabels[i].GetDefault())
+            delete fLabels[i].GetDefault();
+}
+
+// --------------------------------------------------------------------------
+//
+// You can set a weight as a phrase additionally to the one given
+// as an argument to Fill (most likely from MFillH). The two weights
+// are multiplied together.
+//
+void MH3::SetWeight(const char *phrase)
+{
+    if (fData[3])
+        delete fData[3];
+    fData[3] = new MDataPhrase(phrase);
+}
+
+// --------------------------------------------------------------------------
+//
+// The axis label is centered and the labeling of the axis is initialized.
+//
+// This function must not be called after any label has been created!
+//
+void MH3::InitLabels(TAxis &x) const
+{
+    x.CenterTitle();
+    x.SetBinLabel(1, "");
+    x.LabelsOption("h");  // FIXME: Is "a" thread safe? (Paint and Fill?)
+    x.GetLabels()->Delete();
+}
+
+// --------------------------------------------------------------------------
+//
+// Depending on the bits set the InitLabels(TAxis&) function for
+// the corresponding axes are called. In any case the kCanRebin bit
+// is set.
+//
+// This function must not be called after any label has been created!
+//
+void MH3::InitLabels(Labels_t type) const
+{
+    if (!fHist)
+        return;
+
+    if (type&kLabelsX && fHist->GetXaxis())
+        InitLabels(*fHist->GetXaxis());
+
+    if (type&kLabelsY && fHist->GetYaxis())
+        InitLabels(*fHist->GetYaxis());
+
+    if (type&kLabelsZ && fHist->GetZaxis())
+        InitLabels(*fHist->GetZaxis());
+
+    if (type&kLabelsXYZ)
+        fHist->SetBit(TH1::kCanRebin);
+}
+
+// --------------------------------------------------------------------------
+//
+// Return the corresponding Labels_t describing for which axis
+// axis-labels are switched on.
+//
+MH3::Labels_t MH3::GetLabels() const
+{
+    UInt_t type = kNoLabels;
+    if (fHist->GetXaxis() && fHist->GetXaxis()->GetLabels())
+        type |= kLabelsX;
+    if (fHist->GetYaxis() && fHist->GetYaxis()->GetLabels())
+        type |= kLabelsY;
+    if (fHist->GetZaxis() && fHist->GetZaxis()->GetLabels())
+        type |= kLabelsZ;
+    return (Labels_t)type;
+}
+
+// --------------------------------------------------------------------------
+//
+// Calls the LabelsDeflate from the histogram for all axes.
+// LabelsDeflate will just do nothing if the axis has no labels
+// initialized.
+//
+void MH3::DeflateLabels() const
+{
+    fHist->LabelsDeflate("X");
+    fHist->LabelsDeflate("Y");
+    fHist->LabelsDeflate("Z");
+}
+
+// --------------------------------------------------------------------------
+//
+// Returns the named label corresponding to the given value
+// and the given axis. The names are defined with the
+// DefineLabel-functions. if no name is defined the value
+// is converted to a string with %d and TMath::Nint.
+// If names are defined, but not for the given value, the default
+// label is returned instead. If no default is defined the
+// %d-converted string is returned.
+//
+const char *MH3::GetLabel(Int_t axe, Double_t val) const
+{
+    const Int_t v = TMath::Nint(val);
+
+    if (fLabels[axe].GetSize())
+    {
+        const char *l = fLabels[axe].GetObjName(v);
+        if (l)
+            return l;
+    }
+
+    return Form("%d", v);
 }
 
@@ -338,14 +487,12 @@
 {
     TString str=fData[0]->GetDataMember();
-    if (fData[1])
-    {
-        str += ";";
-        str += fData[1]->GetDataMember();
-    }
-    if (fData[2])
-    {
-        str += ";";
-        str += fData[2]->GetDataMember();
-    }
+
+    for (int i=1; i<4; i++)
+        if (fData[i])
+        {
+            str += ";";
+            str += fData[i]->GetDataMember();
+        }
+
     return str;
 }
@@ -377,60 +524,70 @@
     delete tok;
 
-
     MBinning *binsx = NULL;
     MBinning *binsy = NULL;
     MBinning *binsz = NULL;
 
+    const Labels_t labels = GetLabels();
+
     switch (TMath::Abs(fDimension))
     {
     case 3:
-        binsz = fBins[2] ? fBins[2] : (MBinning*)plist->FindObject(bz, "MBinning");
-        if (!binsz)
-        {
-            *fLog << err << dbginf << "MBinning '" << bz << "' not found... aborting." << endl;
-            return kFALSE;
-        }
         if (fData[2])
             fHist->SetZTitle(fData[2]->GetTitle());
-        if (binsz->HasTitle())
-            fHist->SetZTitle(binsz->GetTitle());
-        if (binsz->IsLogarithmic())
-            fHist->SetBit(kIsLogz);
+        if (!labels&kLabelsZ)
+        {
+            binsz = fBins[2] ? fBins[2] : (MBinning*)plist->FindObject(bz, "MBinning");
+            if (!binsz)
+            {
+                *fLog << err << dbginf << "MBinning '" << bz << "' not found... aborting." << endl;
+                return kFALSE;
+            }
+            if (binsz->HasTitle())
+                fHist->SetZTitle(binsz->GetTitle());
+            if (binsz->IsLogarithmic())
+                fHist->SetBit(kIsLogz);
+        }
     case 2:
-        binsy = fBins[1] ? fBins[1] : (MBinning*)plist->FindObject(by, "MBinning");
-        if (!binsy)
-        {
-            *fLog << err << dbginf << "MBinning '" << by << "' not found... aborting." << endl;
-            return kFALSE;
-        }
         if (fData[1])
             fHist->SetYTitle(fData[1]->GetTitle());
-        if (binsy->HasTitle())
-            fHist->SetYTitle(binsy->GetTitle());
-        if (binsy->IsLogarithmic())
-            fHist->SetBit(kIsLogy);
+        if (!labels&kLabelsY)
+        {
+            binsy = fBins[1] ? fBins[1] : (MBinning*)plist->FindObject(by, "MBinning");
+            if (!binsy)
+            {
+                *fLog << err << dbginf << "MBinning '" << by << "' not found... aborting." << endl;
+                return kFALSE;
+            }
+            if (binsy->HasTitle())
+                fHist->SetYTitle(binsy->GetTitle());
+            if (binsy->IsLogarithmic())
+                fHist->SetBit(kIsLogy);
+        }
     case 1:
-        binsx = fBins[0] ? fBins[0] : (MBinning*)plist->FindObject(bx, "MBinning");
-        if (!binsx)
+        if (fData[0]!=NULL)
+            fHist->SetXTitle(fData[0]->GetTitle());
+        if (!labels&kLabelsX)
         {
-            if (fDimension==1)
-                binsx = (MBinning*)plist->FindObject("Binning"+fName, "MBinning");
-
+            binsx = fBins[0] ? fBins[0] : (MBinning*)plist->FindObject(bx, "MBinning");
             if (!binsx)
             {
-                *fLog << err << dbginf << "Neither '" << bx << "' nor '" << binsx << fName << "' found... aborting." << endl;
-                return kFALSE;
+                if (fDimension==1)
+                    binsx = (MBinning*)plist->FindObject("Binning"+fName, "MBinning");
+
+                if (!binsx)
+                {
+                    *fLog << err << dbginf << "Neither '" << bx << "' nor '" << binsx << fName << "' found... aborting." << endl;
+                    return kFALSE;
+                }
             }
+            if (binsx->HasTitle())
+                fHist->SetXTitle(binsx->GetTitle());
+            if (binsx->IsLogarithmic())
+                fHist->SetBit(kIsLogx);
         }
-        if (fData[0]!=NULL)
-            fHist->SetXTitle(fData[0]->GetTitle());
-        if (binsx->HasTitle())
-            fHist->SetXTitle(binsx->GetTitle());
-        if (binsx->IsLogarithmic())
-            fHist->SetBit(kIsLogx);
     }
 
     // PreProcess existing fData members
-    for (int i=0; i<3; i++)
+    for (int i=0; i<4; i++)
         if (fData[i] && !fData[i]->PreProcess(plist))
             return kFALSE;
@@ -445,4 +602,14 @@
     fHist->SetDirectory(0);
 
+    // This is for the case we have set lables
+    const MBinning def(1, 0, 1);
+    if (!binsx)
+        binsx = const_cast<MBinning*>(&def);
+    if (!binsy)
+        binsy = const_cast<MBinning*>(&def);
+    if (!binsz)
+        binsz = const_cast<MBinning*>(&def);
+
+    // set binning
     switch (TMath::Abs(fDimension))
     {
@@ -451,8 +618,8 @@
         return kTRUE;
     case 2:
-        SetBinning((TH2*)fHist, binsx, binsy);
+        SetBinning(static_cast<TH2*>(fHist), binsx, binsy);
         return kTRUE;
     case 3:
-        SetBinning((TH3*)fHist, binsx, binsy, binsz);
+        SetBinning(static_cast<TH3*>(fHist), binsx, binsy, binsz);
         return kTRUE;
     }
@@ -504,9 +671,15 @@
 // Fills the one, two or three data members into our histogram
 //
-Bool_t MH3::Fill(const MParContainer *par, const Stat_t w)
-{
+Bool_t MH3::Fill(const MParContainer *par, const Stat_t ww)
+{
+    // Get Information about labels (UInt_t, to supress warning about
+    // unhandeled cases in switch)
+    const UInt_t type = GetLabels();
+
+    // Get values for axis
     Double_t x=0;
     Double_t y=0;
     Double_t z=0;
+    Double_t w=ww;
 
     switch (fDimension)
@@ -522,24 +695,108 @@
     }
 
+    if (fData[3])
+        w *= fData[3]->GetValue();
+
+    // If label option is set, convert value to label
+    TString labelx, labely, labelz;
+    if (type&kLabelsX)
+        labelx = GetLabel(0, x);
+    if (type&kLabelsY)
+        labely = GetLabel(1, y);
+    if (type&kLabelsZ)
+        labelz = GetLabel(2, z);
+
+    // Fill histogram
     switch (fDimension)
     {
     case  3:
-        static_cast<TH3*>(fHist)->Fill(x, y, z, w);
-        return kTRUE;
+        switch (type)
+        {
+        case kNoLabels:
+            static_cast<TH3*>(fHist)->Fill(x,      y,      z,      w);
+            return kTRUE;
+        case kLabelsX:
+            static_cast<TH3*>(fHist)->Fill(labelx, y,      z);
+            return kTRUE;
+        case kLabelsY:
+            static_cast<TH3*>(fHist)->Fill(x,      labely, z,      w);
+            return kTRUE;
+        case kLabelsZ:
+            static_cast<TH3*>(fHist)->Fill(x,      y,      labelz, w);
+            return kTRUE;
+        case kLabelsXY:
+            static_cast<TH3*>(fHist)->Fill(labelx, labely, z,      w);
+            return kTRUE;
+        case kLabelsXZ:
+            static_cast<TH3*>(fHist)->Fill(labelx, y,      labelz, w);
+            return kTRUE;
+        case kLabelsYZ:
+            static_cast<TH3*>(fHist)->Fill(x,      labely, labelz, w);
+            return kTRUE;
+        case kLabelsXYZ:
+            static_cast<TH3*>(fHist)->Fill(labelx, labely, labelz, w);
+            return kTRUE;
+        }
+        break;
     case  2:
-        static_cast<TH2*>(fHist)->Fill(x, y, w);
-        return kTRUE;
+        switch (type)
+        {
+        case kNoLabels:
+            static_cast<TH2*>(fHist)->Fill(x,      y,      w);
+            return kTRUE;
+        case kLabelsX:
+            static_cast<TH2*>(fHist)->Fill(x,      labely, w);
+            return kTRUE;
+        case kLabelsY:
+            static_cast<TH2*>(fHist)->Fill(labelx, y,      w);
+            return kTRUE;
+        case kLabelsXY:
+            static_cast<TH2*>(fHist)->Fill(labelx, labely, w);
+            return kTRUE;
+        }
+        break;
     case 1:
-        fHist->Fill(x, w);
-        return kTRUE;
+        switch (type)
+        {
+        case kNoLabels:
+            fHist->Fill(x,      w);
+            return kTRUE;
+        case kLabelsX:
+            fHist->Fill(labelx, w);
+            return kTRUE;
+        }
+        break;
     case -1:
-        static_cast<TProfile*>(fHist)->Fill(x, y, w);
-        return kTRUE;
+        switch (type)
+        {
+        case kNoLabels:
+            static_cast<TProfile*>(fHist)->Fill(x,      y, w);
+            return kTRUE;
+        case kLabelsX:
+            static_cast<TProfile*>(fHist)->Fill(labelx, y, w);
+            return kTRUE;
+        }
+        break;
     case -2:
-        static_cast<TProfile2D*>(fHist)->Fill(x, y, z, w);
-        return kTRUE;
-    }
-
-    return kFALSE;
+        switch (type)
+        {
+        case kNoLabels:
+            static_cast<TProfile2D*>(fHist)->Fill(x,      y,      z, w);
+            return kTRUE;
+        case kLabelsX:
+            static_cast<TProfile2D*>(fHist)->Fill(labelx, y,      z);
+            return kTRUE;
+        case kLabelsY:
+            static_cast<TProfile2D*>(fHist)->Fill(x,      labely, z);
+            return kTRUE;
+        case kLabelsXY:
+            static_cast<TProfile2D*>(fHist)->Fill(labelx, labely, z);
+            return kTRUE;
+        }
+        break;
+    }
+
+    *fLog << err << "MH3::Fill: ERROR - A fatal error occured." << endl;
+    return kERROR;
 }
 
@@ -552,4 +809,6 @@
 Bool_t MH3::Finalize()
 {
+    DeflateLabels();
+
     Bool_t autorangex=TESTBIT(fStyleBits, 0);
     Bool_t autorangey=TESTBIT(fStyleBits, 1);
@@ -557,5 +816,4 @@
 
     Int_t lo, hi;
-
     if (autorangex)
     {
@@ -578,4 +836,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// FIXME
+//
 void MH3::Paint(Option_t *o)
 {
@@ -607,4 +869,9 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// If Xmax is < 3000*Xmin SetMoreLogLabels is called. If Xmax<5000
+// the exponent is switched off (SetNoExponent)
+//
 void MH3::HandleLogAxis(TAxis &axe) const
 {
@@ -661,4 +928,7 @@
     TString str(opt);
     str.ToLower();
+
+    if (str.IsNull() && GetLabels() && fDimension==2)
+        str = "colz";
 
     const Bool_t only  = str.Contains("only")  && TMath::Abs(fDimension)==2;
@@ -747,4 +1017,7 @@
     if (fTitle!=gsDefTitle)
         out << "   " << name << ".SetTitle(\"" << fTitle << "\");" << endl;
+
+    if (fData[3])
+        out << "   " << name << ".SetWeight(\"" << fData[3]->GetRule() << "\");" << endl;
 
     switch (fDimension)
@@ -771,4 +1044,6 @@
 MParContainer *MH3::New() const
 {
+    // FIXME: TREAT THE NEW OPTIONS CORRECTLY (PROFILE, LABELS)
+
     MH3 *h = NULL;
 
@@ -794,4 +1069,5 @@
             break;
         }
+
     switch (fDimension)
     {
@@ -805,7 +1081,15 @@
         h->SetScaleX(fScale[0]);
     }
+
+    if (fData[3])
+        h->SetWeight(fData[3]->GetRule());
+
     return h;
 }
 
+// --------------------------------------------------------------------------
+//
+// FIXME
+//
 TString MH3::GetRule(const Char_t axis) const
 {
@@ -813,9 +1097,15 @@
     {
     case 'x':
+    case 'X':
         return fData[0] ? fData[0]->GetRule() : TString("");
     case 'y':
+    case 'Y':
         return fData[1] ? fData[1]->GetRule() : TString("");
     case 'z':
+    case 'Z':
         return fData[2] ? fData[2]->GetRule() : TString("");
+    case 'w':
+    case 'W':
+        return fData[3] ? fData[3]->GetRule() : TString("");
     default:
         return "<n/a>";
@@ -883,2 +1173,117 @@
     return binx + nx*(biny +ny*binz);
 }
+
+// --------------------------------------------------------------------------
+//
+// Return the MObjLookup corresponding to the axis/character.
+// Note that only lower-case charecters (x, y, z) are supported.
+// If for the axis no labels were set, the corresponding
+// InitLabels is called.
+//
+MObjLookup *MH3::GetLabels(char axe)
+{
+    if (!fHist)
+        return 0;
+
+    TAxis *x = 0;
+
+    switch (axe)
+    {
+    case 'x':
+        x = fHist->GetXaxis();
+        break;
+    case 'y':
+        x = fHist->GetYaxis();
+        break;
+    case 'z':
+        x = fHist->GetZaxis();
+        break;
+    }
+
+    if (!x)
+        return 0;
+
+    const Int_t idx = axe-'x';
+
+    if (!x->GetLabels())
+        switch (idx)
+        {
+        case 0:
+            InitLabels(kLabelsX);
+            break;
+        case 1:
+            InitLabels(kLabelsY);
+            break;
+        case 2:
+            InitLabels(kLabelsZ);
+            break;
+        }
+
+    return &fLabels[idx];
+}
+
+// --------------------------------------------------------------------------
+//
+// Set a default label which is used if no other is found in the list
+// of labels. if a default was set already it is overwritten. If the
+// axis has not yet been initialized to use labels it it now.
+//
+void MH3::DefaultLabel(char axe, const char *name)
+{
+    MObjLookup *arr = GetLabels(axe);
+    if (!arr)
+        return;
+
+    if (arr->GetDefault())
+    {
+        delete arr->GetDefault();
+        arr->SetDefault(0);
+    }
+
+    if (name)
+        arr->SetDefault(new TObjString(name));
+}
+
+// --------------------------------------------------------------------------
+//
+// Define a name for a label. More than one label can have the same
+// name. If the axis has not yet been initialized to use labels
+// it it now.
+//
+void MH3::DefineLabel(char axe, Int_t label, const char *name)
+{
+    MObjLookup *arr = GetLabels(axe);
+
+    if (!arr || !name)
+        return;
+
+    if (arr->GetObj(label)!=arr->GetDefault())
+        return;
+
+    arr->Add(label, name);
+}
+
+// --------------------------------------------------------------------------
+//
+// Define names for labels, like
+//    1=Trig;2=Cal;4=Ped;8=Lvl2
+// More than one label can have the same name. If the axis has not
+// yet been initialized to use labels it it now.
+//
+// A default cannot be set here. Use DefaultLabel instead.
+//
+void MH3::DefineLabels(char axe, const TString &labels)
+{
+    TObjArray *arr = labels.Tokenize(';');
+
+    for (int i=0; i<arr->GetEntries(); i++)
+    {
+        const char *s = (*arr)[0]->GetName();
+        const char *v = strchr(s, '=');
+
+        if (v)
+            DefineLabel(axe, atoi(s), v+1);
+    }
+
+    delete arr;
+}
Index: trunk/MagicSoft/Mars/mhbase/MH3.h
===================================================================
--- trunk/MagicSoft/Mars/mhbase/MH3.h	(revision 8892)
+++ trunk/MagicSoft/Mars/mhbase/MH3.h	(revision 8893)
@@ -8,4 +8,7 @@
 #include "MH.h"
 #endif
+#ifndef MARS_MObjLookup
+#include "MObjLookup.h"
+#endif
 
 class TH1;
@@ -15,7 +18,31 @@
 class MH3 : public MH
 {
+public:
+    enum Labels_t {
+        kNoLabels  = 0,
+        kLabelsX   = BIT(0),
+        kLabelsY   = BIT(1),
+        kLabelsZ   = BIT(2),
+        kLabelsXY  = kLabelsX|kLabelsY,
+        kLabelsXZ  = kLabelsX|kLabelsZ,
+        kLabelsYZ  = kLabelsY|kLabelsZ,
+        kLabelsXYZ = kLabelsX|kLabelsY|kLabelsZ,
+    };
+
 private:
     static const TString gsDefName;
     static const TString gsDefTitle;
+
+    // Helper for constructor
+    void        Init();
+
+    // Helper for dealing with labeled histograms
+    MObjLookup *GetLabels(char axe);
+    void        InitLabels(TAxis &x) const;
+    void        DeflateLabels() const;
+    Labels_t    GetLabels() const;
+    const char *GetLabel(Int_t axe, Double_t val) const;
+
+    MObjLookup fLabels[3];       //! Lookup table to conflate and name labels
 
 protected:
@@ -23,5 +50,5 @@
     Int_t       fDimension;      // Number of dimensions of histogram
     TH1        *fHist;           // Histogram to fill
-    MData      *fData[3];        // Object from which the data is filled
+    MData      *fData[4];        // Object from which the data is filled (+additional weight)
     MBinning   *fBins[3];        // Binning set omitting the parlist access
     Double_t    fScale[3];       // Scale for the three axis (eg unit)
@@ -43,10 +70,10 @@
     enum Type_t {
         kHistogram,
-        kProfile
+        kProfile,
     };
 
     MH3(const Int_t dim=0, Type_t type=MH3::kHistogram);
     MH3(const TH1 &h1);
-    MH3(const char *memberx);
+    MH3(const char *memberx, Type_t type=MH3::kHistogram);
     MH3(const char *memberx, const char *membery, Type_t type=MH3::kHistogram);
     MH3(const char *memberx, const char *membery, const char *memberz, Type_t type=MH3::kHistogram);
@@ -75,4 +102,23 @@
 
     void Sumw2() const { if (fHist) fHist->Sumw2(); }
+
+    void InitLabels(Labels_t labels) const;
+
+    void DefaultLabel(char axe, const char *name=0);
+    void DefaultLabelX(const char *name=0) { DefaultLabel('x', name); }
+    void DefaultLabelY(const char *name=0) { DefaultLabel('y', name); }
+    void DefaultLabelZ(const char *name=0) { DefaultLabel('z', name); }
+
+    void DefineLabel(char axe, Int_t label=0, const char *name=0);
+    void DefineLabelX(Int_t label, const char *name) { DefineLabel('x', label, name); }
+    void DefineLabelY(Int_t label, const char *name) { DefineLabel('y', label, name); }
+    void DefineLabelZ(Int_t label, const char *name) { DefineLabel('z', label, name); }
+
+    void DefineLabels(char axe, const TString &labels);
+    void DefineLabelsX(const TString &labels) { DefineLabels('x', labels); }
+    void DefineLabelsY(const TString &labels) { DefineLabels('y', labels); }
+    void DefineLabelsZ(const TString &labels) { DefineLabels('z', labels); }
+
+    void SetWeight(const char *phrase);
 
     // Getter
@@ -109,5 +155,5 @@
     void Paint(Option_t *opt="");
 
-    ClassDef(MH3, 3) // Generalized 1/2/3D-histogram for Mars variables
+    ClassDef(MH3, 4) // Generalized 1/2/3D-histogram for Mars variables
 };
 
Index: trunk/MagicSoft/Mars/mhbase/MHn.cc
===================================================================
--- trunk/MagicSoft/Mars/mhbase/MHn.cc	(revision 8892)
+++ trunk/MagicSoft/Mars/mhbase/MHn.cc	(revision 8893)
@@ -374,4 +374,114 @@
 // --------------------------------------------------------------------------
 //
+// call MH3::InitLabels for the current histogram
+//
+void MHn::InitLabels(MH3::Labels_t labels) const
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->InitLabels(labels);
+}
+
+// --------------------------------------------------------------------------
+//
+// call MH3::DefaultLabelX for the current histogram
+//
+void MHn::DefaultLabelX(const char *name)
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->DefaultLabelX(name);
+}
+
+// --------------------------------------------------------------------------
+//
+// call MH3::DefaultLabelY for the current histogram
+//
+void MHn::DefaultLabelY(const char *name)
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->DefaultLabelY(name);
+}
+
+// --------------------------------------------------------------------------
+//
+// call MH3::DefaultLabelZ for the current histogram
+//
+void MHn::DefaultLabelZ(const char *name)
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->DefaultLabelZ(name);
+}
+
+// --------------------------------------------------------------------------
+//
+// call MH3::DefineLabelX for the current histogram
+//
+void MHn::DefineLabelX(Int_t label, const char *name)
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->DefineLabelX(label, name);
+}
+
+// --------------------------------------------------------------------------
+//
+// call MH3::DefineLabelY for the current histogram
+//
+void MHn::DefineLabelY(Int_t label, const char *name)
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->DefineLabelY(label, name);
+}
+
+// --------------------------------------------------------------------------
+//
+// call MH3::DefineLabelZ for the current histogram
+//
+void MHn::DefineLabelZ(Int_t label, const char *name)
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->DefineLabelZ(label, name);
+}
+
+// --------------------------------------------------------------------------
+//
+// call MH3::DefineLabelsX for the current histogram
+//
+void MHn::DefineLabelsX(const TString &labels)
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->DefineLabelsX(labels);
+}
+
+// --------------------------------------------------------------------------
+//
+// call MH3::DefineLabelsY for the current histogram
+//
+void MHn::DefineLabelsY(const TString &labels)
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->DefineLabelsY(labels);
+}
+
+// --------------------------------------------------------------------------
+//
+// call MH3::DefineLabelsZ for the current histogram
+//
+void MHn::DefineLabelsZ(const TString &labels)
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->DefineLabelsZ(labels);
+}
+
+// --------------------------------------------------------------------------
+//
+// call MH3::SetWeight for the current histogram
+//
+void MHn::SetWeight(const char *phrase)
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->SetWeight(phrase);
+}
+
+// --------------------------------------------------------------------------
+//
 // Call SetupFill for all initialized histograms
 //
Index: trunk/MagicSoft/Mars/mhbase/MHn.h
===================================================================
--- trunk/MagicSoft/Mars/mhbase/MHn.h	(revision 8892)
+++ trunk/MagicSoft/Mars/mhbase/MHn.h	(revision 8893)
@@ -37,15 +37,8 @@
     Bool_t AddHist(const char *memberx, const char *membery, const char *memberz, MH3::Type_t type=MH3::kHistogram);
 
-    void InitName(const char *n)
-    {
-        InitName(fNum-1, n);
-    }
+    void InitName(const char *n)  { InitName(fNum-1, n); }
+    void InitTitle(const char *t) { InitTitle(fNum-1, t); }
 
-    void InitTitle(const char *t)
-    {
-        InitTitle(fNum-1, t);
-    }
-
-    // Interfact to MH3
+    // General interfact to MH3
     void SetScale(Double_t x, Double_t y=1, Double_t z=2) const;
     void SetLog(Bool_t x=kTRUE, Bool_t y=kTRUE, Bool_t z=kTRUE) const;
@@ -53,4 +46,22 @@
     void SetBinnings(MBinning *x=0, MBinning *y=0, MBinning *z=0) const;
     void Sumw2() const;
+
+    // Interface to labels of MH3
+    void InitLabels(MH3::Labels_t labels) const;
+
+    void DefaultLabelX(const char *name=0);
+    void DefaultLabelY(const char *name=0);
+    void DefaultLabelZ(const char *name=0);
+
+    void DefineLabelX(Int_t label, const char *name);
+    void DefineLabelY(Int_t label, const char *name);
+    void DefineLabelZ(Int_t label, const char *name);
+
+    void DefineLabelsX(const TString &labels);
+    void DefineLabelsY(const TString &labels);
+    void DefineLabelsZ(const TString &labels);
+
+    // Set additonal weights for MH3 
+    void SetWeight(const char *phrase);
 
     // MH
