Index: /trunk/MagicSoft/Mars/mhist/MHCamera.cc
===================================================================
--- /trunk/MagicSoft/Mars/mhist/MHCamera.cc	(revision 2809)
+++ /trunk/MagicSoft/Mars/mhist/MHCamera.cc	(revision 2810)
@@ -52,4 +52,5 @@
 #include <TClonesArray.h>
 #include <THistPainter.h>
+#include <THLimitsFinder.h>
 
 #include "MLog.h"
@@ -447,26 +448,96 @@
 }
 
+// ------------------------------------------------------------------------
+//
+// Creates a TH1D which contains the projection of the contents of the
+// MHCamera onto the y-axis. The maximum and minimum are calculated
+// such that a slighly wider range than (GetMinimum(), GetMaximum()) is
+// displayed using THLimitsFinder::OptimizeLimits.
+//
+// If no name is given the newly allocated histogram is removed from
+// the current directory calling SetDirectory(0)
+//
+// If the standard name "_py" is given "_py" is appended to the name
+// of the MHCamera and the corresponding histogram is searched using
+// gROOT->FindObject and updated with the present projection.
+//
+// It is the responsibility of the user to make sure, that the newly
+// created histogram is freed correctly.
+//
+// Currently the new histogram is restrictred to 50 bins.
+// Maybe a optimal number can be calulated from the number of
+// bins on the x-axis of the MHCamera?
+//
+// The code was taken mainly from TH2::ProjectX such the interface
+// is more or less the same than to TH2-projections.
+//
+TH1D *MHCamera::Projection(const char *name) const
+{
+    // Create the projection histogram
+    TString pname(name);
+    if (name=="_py")
+        pname.Prepend(GetName());
+
+    TH1D *h1=0;
+
+    //check if histogram with identical name exist
+    TObject *h1obj = gROOT->FindObject(pname);
+    if (h1obj && h1obj->InheritsFrom("TH1D")) {
+        h1 = (TH1D*)h1obj;
+        h1->Reset();
+    }
+
+    if (!h1)
+    {
+        Double_t min = GetMinimum();
+        Double_t max = GetMaximum();
+
+        Int_t newbins=0;
+
+        THLimitsFinder::OptimizeLimits(50, newbins, min, max, kFALSE);
+
+        h1 = new TH1D(pname, GetTitle(), 50, min, max);
+
+        h1->SetXTitle(GetYaxis()->GetTitle());
+        h1->SetYTitle("Counts");
+        h1->Sumw2();
+        if (pname.IsNull())
+            h1->SetDirectory(NULL);
+    }
+
+    // Fill the projected histogram
+    for (Int_t binx =0;binx<=GetNbinsX()+1;binx++) {
+        const Double_t cont = GetBinContent(binx);
+        if (cont!=0)
+            h1->Fill(cont);
+    }
+
+    h1->SetEntries(GetXaxis()->GetNbins());
+
+    return h1;
+}
+
+// ------------------------------------------------------------------------
+// Will be removed in the future
 void MHCamera::CreateProjection()
 {
-  
-  Int_t nbins = 50;
-
-  // Create the projection histogram
-  TString ytitle(GetYaxis()->GetTitle());
-  fYProj = new TH1D(ytitle.Data(),GetTitle(),nbins,GetMinimum()-0.1,GetMaximum()+0.1);
-  fYProj->SetXTitle(ytitle.Data());
-  fYProj->SetYTitle("Nr. of pixels");
-  fYProj->Sumw2();
-  fYProj->SetDirectory(NULL);
-
-  // Fill the projected histogram
-  Double_t cont;
-  for (Int_t binx =0;binx<=GetNbinsX()+1;binx++) {
-    cont  = GetBinContent(binx);
-    if (cont) 
-      fYProj->Fill(cont);
-  }
-}
-
+    Int_t nbins = 50;
+
+    // Create the projection histogram
+    TString ytitle(GetYaxis()->GetTitle());
+    fYProj = new TH1D(ytitle.Data(),GetTitle(),nbins,GetMinimum()-0.1,GetMaximum()+0.1);
+    fYProj->SetXTitle(ytitle.Data());
+    fYProj->SetYTitle("Nr. of pixels");
+    fYProj->Sumw2();
+    fYProj->SetDirectory(NULL);
+
+    // Fill the projected histogram
+    Double_t cont;
+    for (Int_t binx =0;binx<=GetNbinsX()+1;binx++) {
+        cont  = GetBinContent(binx);
+        if (cont)
+            fYProj->Fill(cont);
+    }
+}
 
 // ------------------------------------------------------------------------
@@ -618,15 +689,14 @@
     if (opt.Contains("proj"))
     {
-
-      CreateProjection();
-      opt.ReplaceAll("proj", "");
-      Float_t he = gStyle->GetStatH();
-      Float_t wi = gStyle->GetStatH();
-      gStyle->SetStatH(0.4);
-      gStyle->SetStatW(0.25);
-      fYProj->Paint(opt);
-      gStyle->SetStatH(he);      
-      gStyle->SetStatW(wi);      
-      return;
+        CreateProjection();
+        opt.ReplaceAll("proj", "");
+        Float_t he = gStyle->GetStatH();
+        Float_t wi = gStyle->GetStatH();
+        gStyle->SetStatH(0.4);
+        gStyle->SetStatW(0.25);
+        fYProj->Paint(opt);
+        gStyle->SetStatH(he);
+        gStyle->SetStatW(wi);
+        return;
     }
     
Index: /trunk/MagicSoft/Mars/mhist/MHCamera.h
===================================================================
--- /trunk/MagicSoft/Mars/mhist/MHCamera.h	(revision 2809)
+++ /trunk/MagicSoft/Mars/mhist/MHCamera.h	(revision 2810)
@@ -44,4 +44,5 @@
     Bool_t         fFreezed;     //! Just a dummy!!!! ([Set,Is]Freezed)
 
+    // Will be removed in the future
     TH1D          *fYProj;       //! Y-Projection of the histogram (does not exist in ROOT)
     
@@ -192,7 +193,10 @@
     UInt_t   GetNumPixels() const;
 
-    TH1D     *GetYProj()          { return fYProj;  }
-    TH1D     *GetYProj() const    { return fYProj;  }
-    
+    TH1D    *Projection(const char *name="_py") const;
+
+    // Will be removed in the future
+    TH1D    *GetYProj()          { return fYProj;  }
+    TH1D    *GetYProj() const    { return fYProj;  }
+
     //void SetStatusBar(TGStatusBar *bar) { fStatusBar = bar; }
 
