Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 1503)
+++ trunk/MagicSoft/Mars/Changelog	(revision 1504)
@@ -7,5 +7,6 @@
 
    * mhist/MHHillas.cc, mhist/MHHillasExt.cc:
-     - added legends. (still work to be done with the stat boxes)
+     - use the new MH::Draw[Copy] function
+     - added names to the histograms in MHHillasExt
 
    * manalysis/MCerPhotPix.h:
@@ -15,4 +16,9 @@
      - added comment
 
+   * mhist/MH.[h,cc]:
+     - added function to draw two histograms in one pad with
+       a layout of two stat boxes and a legend: Draw[Copy]
+     - added comments
+     - added another sanity check in SetBinning(TH1*,TH1*)
 
 
Index: trunk/MagicSoft/Mars/mhist/MH.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MH.cc	(revision 1503)
+++ trunk/MagicSoft/Mars/mhist/MH.cc	(revision 1504)
@@ -54,4 +54,6 @@
 #include <TGaxis.h>
 #include <TCanvas.h>
+#include <TLegend.h>
+#include <TPaveStats.h>
 
 #include "MLog.h"
@@ -133,4 +135,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Applies a given binning to a 1D-histogram
+//
 void MH::SetBinning(TH1 *h, const MBinning *binsx)
 {
@@ -161,4 +167,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Applies given binnings to the two axis of a 2D-histogram
+//
 void MH::SetBinning(TH2 *h, const MBinning *binsx, const MBinning *binsy)
 {
@@ -194,4 +204,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Applies given binnings to the three axis of a 3D-histogram
+//
 void MH::SetBinning(TH3 *h, const MBinning *binsx, const MBinning *binsy, const MBinning *binsz)
 {
@@ -232,4 +246,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Applies given binning (the n+1 edges)  to the axis of a 1D-histogram
+//
 void MH::SetBinning(TH1 *h, const TArrayD &binsx)
 {
@@ -239,4 +257,9 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Applies given binning (the n+1 edges) to the two axis of a
+// 2D-histogram
+//
 void MH::SetBinning(TH2 *h, const TArrayD &binsx, const TArrayD &binsy)
 {
@@ -248,4 +271,9 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Applies given binning (the n+1 edges) to the three axis of a
+// 3D-histogram
+//
 void MH::SetBinning(TH3 *h, const TArrayD &binsx, const TArrayD &binsy, const TArrayD &binsz)
 {
@@ -259,4 +287,9 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Applies the binning of a TAxis (eg from a root histogram) to the axis
+// of a 1D-histogram
+//
 void MH::SetBinning(TH1 *h, const TAxis *binsx)
 {
@@ -270,4 +303,9 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Applies the binnings of the TAxis' (eg from a root histogram) to the
+// two axis' of a 2D-histogram
+//
 void MH::SetBinning(TH2 *h, const TAxis *binsx, const TAxis *binsy)
 {
@@ -285,4 +323,9 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Applies the binnings of the TAxis' (eg from a root histogram) to the
+// three axis' of a 3D-histogram
+//
 void MH::SetBinning(TH3 *h, const TAxis *binsx, const TAxis *binsy, const TAxis *binsz)
 {
@@ -304,4 +347,9 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Applies the binnings of one root-histogram x to another one h
+// Both histograms must be of the same type: TH1, TH2 or TH3
+//
 void MH::SetBinning(TH1 *h, TH1 *x)
 {
@@ -311,4 +359,6 @@
         return;
     }
+    if (h->InheritsFrom(TH3::Class()) || x->InheritsFrom(TH3::Class()))
+        return;
     if (h->InheritsFrom(TH2::Class()) && x->InheritsFrom(TH2::Class()))
     {
@@ -316,4 +366,6 @@
         return;
     }
+    if (h->InheritsFrom(TH2::Class()) || x->InheritsFrom(TH2::Class()))
+        return;
     if (h->InheritsFrom(TH1::Class()) && x->InheritsFrom(TH1::Class()))
     {
@@ -323,4 +375,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Multiplies all entries in a TArrayD by a float f
+//
 void MH::ScaleArray(TArrayD &bins, Double_t f)
 {
@@ -329,4 +385,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Scales the binning of a TAxis by a float f
+//
 TArrayD MH::ScaleAxis(TAxis &axe, Double_t f)
 {
@@ -341,4 +401,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Scales the binning of one, two or three axis of a histogram by a float f
+//
 void MH::ScaleAxis(TH1 *h, Double_t fx, Double_t fy, Double_t fz)
 {
@@ -364,4 +428,10 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Tries to find a MBinning container with the name "Binning"+name
+// in the given parameter list. If it was found it is applied to the
+// given histogram. This is only valid for 1D-histograms
+//
 Bool_t MH::ApplyBinning(const MParList &plist, TString name, TH1 *h)
 {
@@ -442,4 +512,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+//  Returns the lowest entry in a histogram which is greater than gt (eg >0)
+//
 Double_t MH::GetMinimumGT(const TH1 &h, Double_t gt)
 {
@@ -456,2 +530,111 @@
     return min;
 }
+
+// --------------------------------------------------------------------------
+//
+// Draws a copy of the two given histograms. Uses title as the pad title.
+// Also layout the two statistic boxes and a legend.
+//
+void MH::DrawCopy(const TH1 &hist1, const TH1 &hist2, const TString title)
+{
+    //
+    // Draw first histogram
+    //
+    TH1 *h1 = (TH1*)((TH1&)hist1).DrawCopy();
+    gPad->Update();
+
+    //
+    // Rename first statistics box
+    //
+    TPaveStats &s1 = *(TPaveStats*)gPad->FindObject("stats");
+    s1.SetX1NDC(s1.GetX1NDC()-0.01);
+    s1.SetName((TString)"Stat"+hist1.GetTitle());
+
+    //
+    // Draw second histogram
+    //
+    TH1 *h2 = (TH1*)((TH1&)hist2).DrawCopy("sames");
+
+    gPad->Update();
+
+    //
+    // Set new position of second statistics box
+    //
+    TPaveStats &s2 = *(TPaveStats*)gPad->FindObject("stats");
+    s2.SetX1NDC(s1.GetX1NDC()-(s2.GetX2NDC()-s2.GetX1NDC())-0.01);
+    s2.SetX2NDC(s1.GetX1NDC()-0.01);
+
+    //
+    // Draw Legend
+    //
+    TLegend &l = *new TLegend(s1.GetX1NDC(),
+                              s1.GetY1NDC()-0.015-(s1.GetY2NDC()-s1.GetY1NDC())/2,
+                              s1.GetX2NDC(),
+                              s1.GetY1NDC()-0.01
+                            );
+    l.AddEntry(h1, h1->GetTitle());
+    l.AddEntry(h2, h2->GetTitle());
+    l.SetTextSize(s2.GetTextSize());
+    l.SetTextFont(s2.GetTextFont());
+    l.SetBorderSize(s2.GetBorderSize());
+    l.SetBit(kCanDelete);
+
+    // FIXME: Don't change the hist title. Change TPaveText::title
+    h1->SetTitle(" "+title+" ");
+
+    l.Draw();
+}
+
+// --------------------------------------------------------------------------
+//
+// Draws the two given histograms. Uses title as the pad title.
+// Also layout the two statistic boxes and a legend.
+//
+void MH::Draw(TH1 &hist1, TH1 &hist2, const TString title)
+{
+    //
+    // Draw first histogram
+    //
+    hist1.Draw();
+    // FIXME: Don't change the hist title. Change TPaveText::title
+    //h1->SetTitle(" "+title+" ");
+
+    gPad->Update();
+
+    //
+    // Rename first statistics box
+    //
+    TPaveStats &s1 = *(TPaveStats*)gPad->FindObject("stats");
+    s1.SetX1NDC(s1.GetX1NDC()-0.01);
+    s1.SetName((TString)"Stat"+hist1.GetTitle());
+
+    //
+    // Draw second histogram
+    //
+    hist2.Draw("sames");
+
+    gPad->Update();
+
+    //
+    // Set new position of second statistics box
+    //
+    TPaveStats &s2 = *(TPaveStats*)gPad->FindObject("stats");
+    s2.SetX1NDC(s1.GetX1NDC()-(s2.GetX2NDC()-s2.GetX1NDC())-0.01);
+    s2.SetX2NDC(s1.GetX1NDC()-0.01);
+
+    //
+    // Draw Legend
+    //
+    TLegend &l = *new TLegend(s1.GetX1NDC(),
+                              s1.GetY1NDC()-0.015-(s1.GetY2NDC()-s1.GetY1NDC())/2,
+                              s1.GetX2NDC(),
+                              s1.GetY1NDC()-0.01
+                             );
+    l.AddEntry(&hist1, hist1.GetTitle());
+    l.AddEntry(&hist2, hist2.GetTitle());
+    l.SetTextSize(s2.GetTextSize());
+    l.SetTextFont(s2.GetTextFont());
+    l.SetBorderSize(s2.GetBorderSize());
+    l.SetBit(kCanDelete);
+    l.Draw();
+}
Index: trunk/MagicSoft/Mars/mhist/MH.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MH.h	(revision 1503)
+++ trunk/MagicSoft/Mars/mhist/MH.h	(revision 1504)
@@ -50,4 +50,7 @@
     static void    ScaleAxis(TH1 *bins, Double_t fx=1, Double_t fy=1, Double_t fz=1);
 
+    static void DrawCopy(const TH1 &hist1, const TH1 &hist2, const TString title);
+    static void Draw(TH1 &hist1, TH1 &hist2, const TString title);
+
     static void FindGoodLimits(Int_t nbins, Int_t &newbins, Double_t &xmin, Double_t &xmax, Bool_t isInteger);
     static Double_t GetMinimumGT(const TH1 &h, Double_t gt=0);
Index: trunk/MagicSoft/Mars/mhist/MHHillas.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHHillas.cc	(revision 1503)
+++ trunk/MagicSoft/Mars/mhist/MHHillas.cc	(revision 1504)
@@ -41,5 +41,4 @@
 #include <TStyle.h>
 #include <TCanvas.h>
-#include <TLegend.h>
 
 #include "MLog.h"
@@ -295,14 +294,5 @@
 
     c->cd(1);
-
-    TObject *obj;
-    TLegend *l = new TLegend(0.75, 0.8, 0.9, 0.9);
-    obj = fWidth->DrawCopy();
-    ((TH1*)obj)->SetTitle(" Width / Length ");
-    l->AddEntry(obj, "Width");
-    obj = fLength->DrawCopy("same");
-    l->AddEntry(obj,  "Length");
-    l->SetBit(kCanDelete);
-    l->Draw();
+    DrawCopy(*fWidth, *fLength, "Width / Length");
 
     c->cd(2);
@@ -311,12 +301,5 @@
 
     c->cd(3);
-    l = new TLegend(0.70, 0.8, 0.9, 0.9);
-    obj = fCorePix->DrawCopy();
-    ((TH1*)obj)->SetTitle(" Number of core/used Pixels ");
-    l->AddEntry(obj, "# Core Pixels");
-    obj = fUsedPix->DrawCopy("same");
-    l->AddEntry(obj,  "# Used Pixels");
-    l->SetBit(kCanDelete);
-    l->Draw();
+    DrawCopy(*fCorePix, *fUsedPix, "Number of core/used Pixels");
 
     c->cd(4);
@@ -350,6 +333,5 @@
 
     gPad->cd(1);
-    fWidth->Draw();
-    fLength->Draw("same");
+    MH::Draw(*fWidth, *fLength, "Width / Length");
 
     gPad->cd(2);
@@ -358,6 +340,5 @@
 
     gPad->cd(3);
-    fCorePix->Draw();
-    fUsedPix->Draw("same");
+    MH::Draw(*fCorePix, *fUsedPix, "Number of core/used Pixels");
 
     gPad->cd(4);
Index: trunk/MagicSoft/Mars/mhist/MHHillasExt.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHHillasExt.cc	(revision 1503)
+++ trunk/MagicSoft/Mars/mhist/MHHillasExt.cc	(revision 1504)
@@ -80,4 +80,10 @@
     fHM3Trans.SetDirectory(NULL);
 
+    fHConc.SetName("Conc2");
+    fHConc1.SetName("Conc1");
+    fHAsym.SetName("Asymmetry");
+    fHM3Long.SetName("3rd Mom Long");
+    fHM3Trans.SetName("3rd Mom Trans");
+
     fHConc.SetTitle("Ratio: Conc");
     fHConc1.SetTitle("Ratio: Conc1");
@@ -261,22 +267,14 @@
 
     c.cd(1);
-    TObject *obj;
-    TLegend *l = new TLegend(0.75, 0.8, 0.9, 0.9);
-    obj = ((TH1F)fHConc1).DrawCopy();
-    ((TH1*)obj)->SetTitle(" Concentrations ");
-    l->AddEntry(obj, "Conc1");
-    obj = ((TH1F)fHConc).DrawCopy("same");
-    l->AddEntry(obj,  "Conc2");
-    l->SetBit(kCanDelete);
-    l->Draw();
+    DrawCopy(fHConc1, fHConc, "Concentrations");
 
     c.cd(2);
-    ((TH1F)fHAsym).DrawCopy();
+    ((TH1&)fHAsym).DrawCopy();
 
     c.cd(3);
-    ((TH1F)fHM3Long).DrawCopy();
+    ((TH1&)fHM3Long).DrawCopy();
 
     c.cd(4);
-    ((TH1F)fHM3Trans).DrawCopy();
+    ((TH1&)fHM3Trans).DrawCopy();
 
     c.Modified();
@@ -300,12 +298,5 @@
 
     gPad->cd(1);
-    fHConc1.Draw();
-    fHConc.Draw("same");
-
-    TLegend *l = new TLegend(0.75, 0.8, 0.9, 0.9);
-    l->AddEntry((TH1*)&fHConc1, "Conc 1");
-    l->AddEntry((TH1*)&fHConc,  "Conc");
-    l->SetBit(kCanDelete);
-    l->Draw();
+    MH::Draw(fHConc1, fHConc, "Concentrations");
 
     gPad->cd(2);
