Index: trunk/MagicSoft/Mars/mhist/MFillH.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MFillH.cc	(revision 1613)
+++ trunk/MagicSoft/Mars/mhist/MFillH.cc	(revision 1629)
@@ -85,82 +85,4 @@
 ClassImp(MFillH);
 
-//////////////////////////////////////////////////////////////////////////////
-//
-// MMap
-//
-// This class maps a key-value to a given value. In its simple versions it
-// maps a key to an index.
-//
-//////////////////////////////////////////////////////////////////////////////
-#include <TArrayI.h>
-
-class MMap
-{
-private:
-    TArrayI fKeys;
-    TArrayI fValues;
-
-    Int_t K(Int_t i) const { return ((TArrayI)fKeys)[i]; }
-    Int_t V(Int_t i) const { return ((TArrayI)fValues)[i]; }
-
-public:
-    // --------------------------------------------------------------------------
-    //
-    // Get the value which corresponds to the given key-value
-    //
-    Int_t GetValue(Int_t key) const
-    {
-        const Int_t n = fKeys.GetSize();
-        for (int i=0; i<n; i++)
-        {
-            if (K(i)==key)
-                return V(i);
-        }
-        return -1;
-    }
-
-    // --------------------------------------------------------------------------
-    //
-    // Adds a new pair key-value. While the key is the key to the value.
-    // if the key already exists the pair is ignored.
-    //
-    void Add(Int_t key, Int_t value)
-    {
-        if (GetValue(key)>=0)
-            return;
-
-        const Int_t n = fKeys.GetSize();
-
-        fKeys.Set(n+1);
-        fValues.Set(n+1);
-
-        fKeys[n] = key;
-        fValues[n] = value;
-    }
-
-    // --------------------------------------------------------------------------
-    //
-    // Adds a new pair key-value. While the key is the key to the value.
-    // In this case the value is an automatically sequential created index.
-    // if the key already exists the pair is ignored.
-    //
-    Int_t Add(Int_t key)
-    {
-        const Int_t k = GetValue(key);
-        if (k>=0)
-            return k;
-
-        const Int_t n = fKeys.GetSize();
-
-        fKeys.Set(n+1);
-        fValues.Set(n+1);
-
-        fKeys[n] = key;
-        fValues[n] = n;
-
-        return n;
-    }
-};
-
 // --------------------------------------------------------------------------
 //
@@ -177,5 +99,4 @@
 
     fIndex  = NULL;
-    fMapIdx = new MMap;
 }
 
@@ -326,6 +247,4 @@
         if (fIndex->TestBit(kCanDelete))
             delete fIndex;
-
-    delete fMapIdx;
 }
 
@@ -500,9 +419,10 @@
 {
     if (fIndex)
-    {
-        const Int_t key = (Int_t)fIndex->GetValue();
-        const Int_t idx = fMapIdx->Add(key);
-        ((MHArray*)fH)->SetIndex(idx);
-    }
+        ((MHArray*)fH)->SetIndexByKey(fIndex->GetValue());
+    /*
+     const Int_t key = (Int_t)fIndex->GetValue();
+     const Int_t idx = fMapIdx->Add(key);
+     ((MHArray*)fH)->SetIndex(idx);
+     */
 
     return fH->Fill(fParContainer);
Index: trunk/MagicSoft/Mars/mhist/MHArray.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHArray.cc	(revision 1613)
+++ trunk/MagicSoft/Mars/mhist/MHArray.cc	(revision 1629)
@@ -37,6 +37,13 @@
 //  by its index.
 //
-//  To map a key to the index use/see MFillH, which automatically maps a
-//  key-value to the array index.
+//  To access the histograms by a key instead of an index use SetIndexByKey
+//  instead of Set/Inc/DecIndex. It will take the integerpart of the
+//  floating point value (2 in case of 2.9). For each new key a new
+//  index in the Mapping Table is created. So that you can access your
+//  histograms by the key (eg in case of the Angle Theta=23.2deg use
+//  SetIndexByKey(23.2)
+//
+//  If the index is equal to the number of histograms in the array a call
+//  to the Fill-member-function will create a new histogram.
 //
 //  In the constructor istempl leads to two different behaviours of the
@@ -73,8 +80,109 @@
 ClassImp(MHArray);
 
-// --------------------------------------------------------------------------
-//
-// Default Constructor. hname is the name of the histogram class which
-// is in the array.
+//////////////////////////////////////////////////////////////////////////////
+//
+// MMap
+//
+// This class maps a key-value to a given value. In its simple versions it
+// maps a key to an index.
+//
+//////////////////////////////////////////////////////////////////////////////
+#include <TArrayI.h>
+
+class MMap
+{
+private:
+    TArrayI fKeys;
+    TArrayI fValues;
+
+    Int_t K(Int_t i) const { return ((TArrayI)fKeys)[i]; }
+    Int_t V(Int_t i) const { return ((TArrayI)fValues)[i]; }
+
+public:
+    // --------------------------------------------------------------------------
+    //
+    // Return the size of the table
+    //
+    Int_t GetSize() const
+    {
+        return fKeys.GetSize();
+    }
+
+    // --------------------------------------------------------------------------
+    //
+    // Get the value which corresponds to the given key-value
+    //
+    Int_t GetValue(Int_t key) const
+    {
+        const Int_t n = fKeys.GetSize();
+        for (int i=0; i<n; i++)
+        {
+            if (K(i)==key)
+                return V(i);
+        }
+        return -1;
+    }
+
+    // --------------------------------------------------------------------------
+    //
+    // Get the key which corresponds to the given index
+    //
+    Int_t GetKey(Int_t value) const
+    {
+        const Int_t n = fKeys.GetSize();
+        for (int i=0; i<n; i++)
+        {
+            if (V(i)==value)
+                return K(i);
+        }
+        return -1;
+    }
+
+    // --------------------------------------------------------------------------
+    //
+    // Adds a new pair key-value. While the key is the key to the value.
+    // if the key already exists the pair is ignored.
+    //
+    void Add(Int_t key, Int_t value)
+    {
+        if (GetValue(key)>=0)
+            return;
+
+        const Int_t n = fKeys.GetSize();
+
+        fKeys.Set(n+1);
+        fValues.Set(n+1);
+
+        fKeys[n] = key;
+        fValues[n] = value;
+    }
+
+    // --------------------------------------------------------------------------
+    //
+    // Adds a new pair key-value. While the key is the key to the value.
+    // In this case the value is an automatically sequential created index.
+    // if the key already exists the pair is ignored.
+    //
+    Int_t Add(Int_t key)
+    {
+        const Int_t k = GetValue(key);
+        if (k>=0)
+            return k;
+
+        const Int_t n = fKeys.GetSize();
+
+        fKeys.Set(n+1);
+        fValues.Set(n+1);
+
+        fKeys[n] = key;
+        fValues[n] = n;
+
+        return n;
+    }
+};
+
+// --------------------------------------------------------------------------
+//
+// hname is the name of the histogram class which is in the array.
 //
 // istempl=kTRUE tells MHArray to use the first histogram retrieved from the
@@ -95,4 +203,6 @@
     fTitle = title ? TString(title) : (TString("Base class for Mars histogram arrays:") + hname);
 
+    fMapIdx = new MMap;
+
     fArray = new TList;
     fArray->SetOwner();
@@ -131,4 +241,32 @@
 // --------------------------------------------------------------------------
 //
+// Default Constructor. hname is the name of the histogram class which
+// is in the array.
+//
+// istempl=kTRUE tells MHArray to use the first histogram retrieved from the
+// ParameterList by hname to be used as a template. New histograms are not
+// created using the root dictionary, but the New-member function (see
+// MParConatiner)
+// In the case istempl=kFALSE new histograms are created using the root
+// dictionary while hname is the class name. For the creation their
+// default constructor is used.
+//
+MHArray::MHArray(const MH *hist, const char *name, const char *title)
+    : fIdx(0), fClass(NULL), fTemplate(hist), fTemplateName("<dummy>")
+{
+    //
+    //   set the name and title of this object
+    //
+    fName  = name  ? name  : "MHArray";
+    fTitle = title ? TString(title) : (TString("Base class for Mars histogram arrays:") + hist->GetName());
+
+    fMapIdx = new MMap;
+
+    fArray = new TList;
+    fArray->SetOwner();
+}
+
+// --------------------------------------------------------------------------
+//
 // Destructor: Deleteing the array and all histograms which are part of the
 // array.
@@ -138,7 +276,29 @@
     fArray->Delete();
     delete fArray;
-}
-
-
+    delete fMapIdx;
+}
+
+// --------------------------------------------------------------------------
+//
+//  Use this to access the histograms by a key. If you use values like
+//   (in this order) 2.5, 7.2, 2.5, 9.3, 9.3, 3.3, 2.2, 1.1
+//  it will be mapped to the following indices internally:
+//                    0    1    0    2    2    3    4    5
+//
+//  WARNING: Make sure that you don't create new histograms by setting
+//           a new index (SetIndex or IncIndex) which is equal the size
+//           of the array and create new histogram by CreateH. In this
+//           case you will confuse the mapping completely.
+//
+void MHArray::SetIndexByKey(Double_t key)
+{
+    fIdx = fMapIdx->Add((Int_t)key);
+}
+
+// --------------------------------------------------------------------------
+//
+//  Use this function to access a histogram by its index in the array.
+//  Becarefull the index isn't checked!
+//
 MH &MHArray::operator[](Int_t i)
 {
@@ -146,4 +306,9 @@
 }
 
+// --------------------------------------------------------------------------
+//
+//  Use this function to access a histogram by its index in the array.
+//  Becarefull the index isn't checked!
+//
 MH *MHArray::At(Int_t i)
 {
@@ -151,4 +316,10 @@
 }
 
+// --------------------------------------------------------------------------
+//
+//  Use this function to access the histogram corresponding to the
+//  currently set index (by Set/Inc/DecIndex or SetIndexByKey)
+//  Becarefull the index set isn't checked!
+//
 MH *MHArray::GetH()
 {
@@ -170,4 +341,8 @@
     if (fTemplate)
     {
+        //
+        // create the parameter container as a clone of the existing
+        // template histogram.
+        //
         hist = (MH*)fTemplate->New();
     }
@@ -226,4 +401,7 @@
     fIdx = 0;
 
+    if (fTemplate)
+        return kTRUE;
+
     if (!fTemplateName.IsNull())
     {
@@ -246,5 +424,5 @@
     const Int_t n = fArray->GetSize();
 
-    if (fIdx <0 || fIdx>n)
+    if (fIdx<0 || fIdx>n)
     {
         *fLog << warn << "Histogram Index #" << fIdx << " out of bounds (>";
@@ -287,5 +465,29 @@
     *fLog << all << GetDescriptor() << " contains " << fArray->GetSize();
     *fLog << " histograms." << endl;
-}
+
+    if (fMapIdx->GetSize()<=0)
+        return;
+
+    *fLog << " idx\t     key" << endl;
+    for (int i=0; i<fMapIdx->GetSize(); i++)
+        *fLog << "  " << i << "\t<-->  " << fMapIdx->GetKey(i) << endl;
+    *fLog << endl;
+}
+
+// --------------------------------------------------------------------------
+//
+// Adds the given object to the given legend (if != NULL). The Legend
+// entry name is created from the key...
+//
+void MHArray::AddLegendEntry(TLegend *leg, TObject *obj, Int_t idx) const
+{
+    if (!leg)
+        return;
+
+    TString name = " ";
+    name += fMapIdx->GetKey(idx);
+    leg->AddEntry(obj, name, "lpf"); // l=line, p=polymarker, f=fill
+}
+
 
 // --------------------------------------------------------------------------
@@ -302,8 +504,18 @@
     gStyle->SetOptStat(0);
 
+    //
+    // if the keymapping is used create a legend to identify the histograms
+    //
+    TLegend *leg = NULL;
+    if (fMapIdx->GetSize()>0)
+    {
+        leg = new TLegend(0.85, 0.80, 0.99, 0.99);
+        leg->SetBit(kCanDelete);
+    }
+
     TIter Next(fArray);
     MH *hist = (MH*)Next();
 
-    Int_t col=2;
+    Int_t idx=0;
     Double_t max=0;
     Double_t min=0;
@@ -311,4 +523,9 @@
     TH1 *h1=NULL;
 
+    //
+    // If the array has at least one entry:
+    //  - find the starting boundaries
+    //  - draw it and set its line color
+    //
     if (hist)
     {
@@ -316,10 +533,17 @@
         {
             h1->Draw();
-            h1->SetLineColor(col++);
+            h1->SetLineColor(idx+2);
             max = h1->GetMaximum();
             min = h1->GetMinimum();
+
+            AddLegendEntry(leg, h1, idx);
         }
     }
 
+    //
+    // For all following histograms:
+    //  - update the boundaries
+    //  - draw it and set its line color
+    //
     while ((hist=(MH*)Next()))
     {
@@ -330,11 +554,16 @@
 
         h->Draw("same");
-        h->SetLineColor(col++);
+        h->SetLineColor(idx+2);
         if (max<h->GetMaximum())
             max = h->GetMaximum();
         if (min>h->GetMinimum())
             min = h->GetMinimum();
-    }
-
+
+        AddLegendEntry(leg, h, idx++);
+    }
+
+    //
+    // Now update the drawing region so that everything is displayed
+    //
     if (h1)
     {
@@ -342,4 +571,8 @@
         h1->SetMaximum(max>0 ? max*1.05 : max*0.95);
     }
+
+    if (leg)
+        leg->Draw();
+
     gPad->Modified();
     gPad->Update();
@@ -353,4 +586,6 @@
 // the MH entries by calling their GetHistByName function.
 // If the option also contains 'nonew' no new canvas is created.
+// The option "Scale=1" scales the area of all histogram to 1
+// The option "Scale=max" scales the maximum of all histogram to 1
 //
 TObject *MHArray::DrawClone(Option_t *opt) const
@@ -360,5 +595,15 @@
     TCanvas *c = NULL;
 
-    Int_t nonew = o.Index("nonew", TString::kIgnoreCase);
+    Int_t scale1   = o.Index("scale=1",   TString::kIgnoreCase);
+    Int_t scalemax = o.Index("scale=max", TString::kIgnoreCase);
+    Int_t nonew    = o.Index("nonew",     TString::kIgnoreCase);
+
+    if (o.BeginsWith("scale=1", TString::kIgnoreCase))
+        scale1 = 0;
+    if (o.BeginsWith("scale=max", TString::kIgnoreCase))
+        scalemax = 0;
+    if (o.BeginsWith("nonew", TString::kIgnoreCase))
+        nonew = 0;
+
     if (nonew>=0)
     {
@@ -372,12 +617,26 @@
         o.Remove(nonew, 5);
     }
+    if (scale1>=0)
+        o.Remove(scale1, 7);
+    if (scalemax>=0)
+        o.Remove(scalemax, 9);
 
     const Stat_t sstyle = gStyle->GetOptStat();
     gStyle->SetOptStat(0);
 
+    //
+    // if the keymapping is used create a legend to identify the histograms
+    //
+    TLegend *leg = NULL;
+    if (fMapIdx->GetSize()>0)
+    {
+        leg = new TLegend(0.85, 0.80, 0.99, 0.99);
+        leg->SetBit(kCanDelete);
+    }
+
     TIter Next(fArray);
     MH *hist = (MH*)Next();
 
-    Int_t col=2;
+    Int_t idx=0;
     Double_t max=0;
     Double_t min=0;
@@ -385,4 +644,9 @@
     TH1 *h1=NULL;
 
+     //
+    // If the array has at least one entry:
+    //  - find the starting boundaries
+    //  - draw it and set its line color
+    //
     if (hist)
     {
@@ -390,12 +654,25 @@
         {
             h1 = (TH1*)h1->DrawCopy();
-            h1->SetMarkerColor(col);
-            h1->SetLineColor(col++);
+
+            if (scale1>=0)
+                h1->Scale(1./h1->Integral());
+            if (scalemax>=0)
+                h1->Scale(1./h1->GetMaximum());
+
+            h1->SetMarkerColor(idx);
+            h1->SetLineColor(idx+2);
             h1->SetFillStyle(4000);
             max = h1->GetMaximum();
             min = h1->GetMinimum();
+
+            AddLegendEntry(leg, h1, idx++);
         }
     }
 
+    //
+    // For all following histograms:
+    //  - update the boundaries
+    //  - draw it and set its line color
+    //
     while ((hist=(MH*)Next()))
     {
@@ -406,13 +683,24 @@
 
         h = (TH1*)h->DrawCopy("same");
-        h->SetMarkerColor(col);
-        h->SetLineColor(col++);
-        h->SetFillStyle(4000);
+
+        if (scale1>=0)
+            h->Scale(1./h->Integral());
+        if (scalemax>=0)
+            h->Scale(1./h->GetMaximum());
+
+        h->SetMarkerColor(idx);
+        h->SetLineColor(idx+2);
+        h->SetFillStyle(4000); // transperent (why is this necessary?)
         if (max<h->GetMaximum())
             max = h->GetMaximum();
         if (min>h->GetMinimum())
             min = h->GetMinimum();
-    }
-
+
+        AddLegendEntry(leg, h, idx++);
+    }
+
+    //
+    // Now update the drawing region so that everything is displayed
+    //
     if (h1)
     {
@@ -420,4 +708,8 @@
         h1->SetMaximum(max>0 ? max*1.05 : max*0.95);
     }
+
+    if (leg)
+        leg->Draw();
+
     gPad->Modified();
     gPad->Update();
Index: trunk/MagicSoft/Mars/mhist/MHArray.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHArray.h	(revision 1613)
+++ trunk/MagicSoft/Mars/mhist/MHArray.h	(revision 1629)
@@ -7,4 +7,7 @@
 
 class TList;
+class TLegend;
+
+class MMap;
 
 class MHArray : public MH
@@ -15,10 +18,13 @@
 
     const MParList *fParList; //! pointer to parameter list used for SetupFill when a new Hist is created
-    TClass *fClass;           //! pointer to class entry in root dictionary
+    TClass *fClass;           // pointer to class entry in root dictionary
 
-    MH *fTemplate;            //! pointer to a template histogram
-    TString fTemplateName;    //! name of the template class
+    const MH *fTemplate;      //-> pointer to a template histogram
+    TString fTemplateName;    // name of the template class
+
+    MMap *fMapIdx;            //! Table to map keys to array indices
 
     Bool_t CreateH();
+    void   AddLegendEntry(TLegend *leg, TObject *obj, Int_t idx) const;
 
     enum { kUseTemplate=BIT(14) };
@@ -26,4 +32,5 @@
 public:
     MHArray(const TString hname, Bool_t istempl=kFALSE, const char *name=NULL, const char *title=NULL);
+    MHArray(const MH *hist, const char *name=NULL, const char *title=NULL);
     ~MHArray();
 
@@ -36,4 +43,6 @@
 
     MH *GetH();
+
+    void SetIndexByKey(Double_t key);
 
     void SetIndex(Int_t i) { fIdx=i; }
Index: trunk/MagicSoft/Mars/mhist/MHFadcCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHFadcCam.cc	(revision 1613)
+++ trunk/MagicSoft/Mars/mhist/MHFadcCam.cc	(revision 1629)
@@ -100,5 +100,5 @@
         const UInt_t id = pixel.GetPixelId();
 
-        for (Int_t i=0;  i<nhisamples; i++)
+        for (Int_t i=0; i<nhisamples; i++)
             FillHi(id, pixel.GetHiGainSamples()[i]);
 
@@ -113,4 +113,9 @@
 }
 
+Bool_t MHFadcCam::Fill(const MRawEvtData *par)
+{
+    return Fill((MParContainer*)par);
+}
+
 void MHFadcCam::ResetHistograms()
 {
Index: trunk/MagicSoft/Mars/mhist/MHFadcCam.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHFadcCam.h	(revision 1613)
+++ trunk/MagicSoft/Mars/mhist/MHFadcCam.h	(revision 1629)
@@ -17,4 +17,6 @@
 #endif
 
+class MRawEvtData;
+
 class MHFadcCam : public MH
 {
@@ -30,9 +32,14 @@
 
     MHFadcPix *operator[](UInt_t i) { return (MHFadcPix*)(fArray->At(i)); }
+    const MHFadcPix *operator[](UInt_t i) const { return (MHFadcPix*)(fArray->At(i)); }
 
     TH1F *GetHistHi(UInt_t i)  { return (*this)[i]->GetHistHi(); }
     TH1F *GetHistLo(UInt_t i)  { return (*this)[i]->GetHistLo(); }
 
+    const TH1F *GetHistHi(UInt_t i) const { return (*this)[i]->GetHistHi(); }
+    const TH1F *GetHistLo(UInt_t i) const { return (*this)[i]->GetHistLo(); }
+
     Bool_t Fill(const MParContainer *par);
+    Bool_t Fill(const MRawEvtData *par);
 
     void ResetHistograms();
Index: trunk/MagicSoft/Mars/mhist/MHFadcPix.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHFadcPix.h	(revision 1613)
+++ trunk/MagicSoft/Mars/mhist/MHFadcPix.h	(revision 1629)
@@ -23,4 +23,7 @@
     TH1F *GetHistLo() { return fHistLo; }
 
+    const TH1F *GetHistHi() const { return fHistHi; }
+    const TH1F *GetHistLo() const { return fHistLo; }
+
     void FillHi(Byte_t i);
     void FillLo(Byte_t i);
