Index: trunk/MagicSoft/Mars/mhist/MH.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MH.cc	(revision 2106)
+++ trunk/MagicSoft/Mars/mhist/MH.cc	(revision 2109)
@@ -819,2 +819,130 @@
     return kFALSE;
 }
+
+void MH::ProjectionX(TH1D &dest, const TH2 &src, Int_t firstybin, Int_t lastybin)
+{
+    //*-*-*-*-*Project a 2-D histogram into a 1-D histogram along X*-*-*-*-*-*-*
+    //*-*      ====================================================
+    //
+    //   The projection dest is always of the type TH1D.
+    //   The projection is made from the channels along the Y axis
+    //   ranging from firstybin to lastybin included.
+    //   By default, bins 1 to ny are included
+    //   When all bins are included, the number of entries in the projection
+    //   is set to the number of entries of the 2-D histogram, otherwise
+    //   the number of entries is incremented by 1 for all non empty cells.
+    //
+    //   if Sumw2() was called for dest, the errors are computed.
+    //
+    TAxis &axex = *((TH2&)src).GetXaxis();
+    TAxis &axey = *((TH2&)src).GetYaxis();
+
+    const Int_t nx = axex.GetNbins();
+    const Int_t ny = axey.GetNbins();
+    if (firstybin < 0)
+        firstybin = 1;
+    if (lastybin > ny)
+        lastybin = ny;
+
+    dest.Reset();
+    SetBinning(&dest, &axex);
+
+    // Create the projection histogram
+    const Bool_t computeErrors = dest.GetSumw2N() ? 1 : 0;
+
+    // Fill the projected histogram
+    for (Int_t binx=0; binx<=nx+1; binx++)
+    {
+        Double_t err2 = 0;
+        for (Int_t biny=firstybin; biny<=lastybin; biny++)
+        {
+            const Double_t cont = src.GetCellContent(binx,biny);
+            const Double_t err1 = src.GetCellError(binx,biny);
+            err2 += err1*err1;
+            if (cont)
+                dest.Fill(axex.GetBinCenter(binx), cont);
+        }
+        if (computeErrors)
+            dest.SetBinError(binx, TMath::Sqrt(err2));
+    }
+    if (firstybin <=1 && lastybin >= ny)
+        dest.SetEntries(src.GetEntries());
+}
+
+void MH::ProjectionY(TH1D &dest, const TH2 &src, Int_t firstxbin, Int_t lastxbin)
+{
+    //*-*-*-*-*Project a 2-D histogram into a 1-D histogram along X*-*-*-*-*-*-*
+    //*-*      ====================================================
+    //
+    //   The projection dest is always of the type TH1D.
+    //   The projection is made from the channels along the Y axis
+    //   ranging from firstybin to lastybin included.
+    //   By default, bins 1 to ny are included
+    //   When all bins are included, the number of entries in the projection
+    //   is set to the number of entries of the 2-D histogram, otherwise
+    //   the number of entries is incremented by 1 for all non empty cells.
+    //
+    //   if Sumw2() was called for dest, the errors are computed.
+    //
+    TAxis &axex = *((TH2&)src).GetXaxis();
+    TAxis &axey = *((TH2&)src).GetYaxis();
+
+    const Int_t nx = axex.GetNbins();
+    const Int_t ny = axey.GetNbins();
+    if (firstxbin < 0)
+        firstxbin = 1;
+    if (lastxbin > nx)
+        lastxbin = nx;
+
+    dest.Reset();
+    SetBinning(&dest, &axey);
+
+    // Create the projection histogram
+    const Bool_t computeErrors = dest.GetSumw2N() ? 1 : 0;
+
+    // Fill the projected histogram
+    for (Int_t biny=0; biny<=ny+1; biny++)
+    {
+        Double_t err2 = 0;
+        for (Int_t binx=firstxbin; binx<=lastxbin; binx++)
+        {
+            const Double_t cont = src.GetCellContent(binx,biny);
+            const Double_t err1 = src.GetCellError(binx,biny);
+            err2 += err1*err1;
+            if (cont)
+                dest.Fill(axey.GetBinCenter(biny), cont);
+        }
+        if (computeErrors)
+            dest.SetBinError(biny, TMath::Sqrt(err2));
+    }
+    if (firstxbin <=1 && lastxbin >= nx)
+        dest.SetEntries(src.GetEntries());
+}
+
+// --------------------------------------------------------------------------
+//
+// In contradiction to TPad::FindObject this function searches recursively
+// in a pad for an object. gPad is the default.
+//
+TObject *MH::FindObjectInPad(const char *name, TVirtualPad *pad)
+{
+    if (!pad)
+        pad = gPad;
+
+    if (!pad)
+        return NULL;
+
+    TObject *o;
+
+    TIter Next(pad->GetListOfPrimitives());
+    while ((o=Next()))
+    {
+        if (!strcmp(o->GetName(), name))
+            return o;
+
+        if (o->InheritsFrom("TPad"))
+            if ((o = FindObjectInPad(name, (TVirtualPad*)o)))
+                return o;
+    }
+    return NULL;
+}
Index: trunk/MagicSoft/Mars/mhist/MH.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MH.h	(revision 2106)
+++ trunk/MagicSoft/Mars/mhist/MH.h	(revision 2109)
@@ -7,4 +7,5 @@
 
 class TH1;
+class TH1D;
 class TH2;
 class TH3;
@@ -75,4 +76,9 @@
     static Double_t GetMinimumGT(const TH1 &h, Double_t gt=0);
 
+    static void ProjectionX(TH1D &dest, const TH2 &src, Int_t firstybin=-1, Int_t lastybin=9999);
+    static void ProjectionY(TH1D &dest, const TH2 &src, Int_t firstxbin=-1, Int_t lastxbin=9999);
+
+    static TObject *FindObjectInPad(const char *name, TVirtualPad *pad=NULL);
+
     ClassDef(MH, 0) //A base class for Mars histograms
 };
Index: trunk/MagicSoft/Mars/mhist/MHSigmaTheta.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHSigmaTheta.cc	(revision 2106)
+++ trunk/MagicSoft/Mars/mhist/MHSigmaTheta.cc	(revision 2109)
@@ -223,4 +223,29 @@
 // --------------------------------------------------------------------------
 //
+// Update the projections and (if possible) set log scales before painting
+//
+void MHSigmaTheta::Paint(Option_t *opt)
+{
+    TVirtualPad *padsave = gPad;
+
+    TH1D* h;
+
+    padsave->cd(1);
+    if ((h = (TH1D*)gPad->FindObject("ProjX-Theta")))
+    {
+        ProjectionX(*h, fSigmaTheta);
+        if (h->GetEntries()!=0)
+            gPad->SetLogy();
+    }
+
+    padsave->cd(4);
+    if ((h = (TH1D*)gPad->FindObject("ProjY-sigma")))
+        ProjectionY(*h, fSigmaTheta);
+
+    gPad = padsave;
+}
+
+// --------------------------------------------------------------------------
+//
 // Draw the histogram
 //
@@ -239,5 +264,4 @@
     pad->cd(1);
     gPad->SetBorderMode(0);
-    // gPad->SetLogy();
     h = fSigmaTheta.ProjectionX("ProjX-Theta", -1, 9999, "E");
     h->SetDirectory(NULL);
Index: trunk/MagicSoft/Mars/mhist/MHSigmaTheta.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHSigmaTheta.h	(revision 2106)
+++ trunk/MagicSoft/Mars/mhist/MHSigmaTheta.h	(revision 2109)
@@ -37,4 +37,5 @@
     TH3D fDiffPixTheta;  // 3D-distr.:Theta, pixel, sigma^2-sigmabar^2
 
+    void Paint(Option_t *opt="");
 
 public:
