Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 951)
+++ trunk/MagicSoft/Mars/Changelog	(revision 952)
@@ -8,4 +8,15 @@
    * mmain/MAnalysis.[h,cc]:
      - added entry field for cleaning levels
+     - restructured code a bit
+
+   * mhist/MHHillas.[h,cc]:
+     - Added comments
+     - Added DrawClone
+     - Removed kCanDelete bits from Draw function
+     
+   * mhist/MHStarMap.[h,cc]:
+     - Added comments
+     - Added DrawClone
+     - moved some code from the Draw-functions to PrepareDrawing
 
 
Index: trunk/MagicSoft/Mars/mhist/MHHillas.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHHillas.cc	(revision 951)
+++ trunk/MagicSoft/Mars/mhist/MHHillas.cc	(revision 952)
@@ -19,4 +19,8 @@
 ClassImp(MHHillas);
 
+// --------------------------------------------------------------------------
+//
+// Setup four histograms for Alpha, Width, Length and Dist
+//
 MHHillas::MHHillas (const char *name, const char *title)
 {
@@ -57,4 +61,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Delete the four histograms
+//
 MHHillas::~MHHillas()
 {
@@ -65,4 +73,9 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Fill the four histograms with data from a MHillas-Container.
+// Be careful: Only call this with an object of type MHillas
+//
 void MHHillas::Fill(const MParContainer *par)
 {
@@ -75,4 +88,46 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Draw clones of all four histograms. So that the object can be deleted
+// and the histograms are still visible in the canvas.
+// The cloned object are deleted together with the canvas if the canvas is
+// destroyed. If you want to handle dostroying the canvas you can get a
+// pointer to it from this function
+//
+TObject *MHHillas::DrawClone(Option_t *opt)
+{
+    TCanvas *c = new TCanvas("Hillas", "Histograms of Hillas Parameters");
+    c->Divide(2,2);
+
+    //
+    // This is necessary to get the expected bahviour of DrawClone
+    //
+    gROOT->SetSelectedPad(NULL);
+
+    c->cd(1);
+    fAlpha->DrawClone()->SetBit(kCanDelete);
+
+    c->cd(2);
+    fLength->DrawClone()->SetBit(kCanDelete);
+
+    c->cd(3);
+    fDist->DrawClone()->SetBit(kCanDelete);
+
+    c->cd(4);
+    fWidth->DrawClone()->SetBit(kCanDelete);
+
+    c->Modified();
+    c->Update();
+
+    return c;
+}
+
+// --------------------------------------------------------------------------
+//
+// Creates a new canvas and draws the four histograms into it.
+// Be careful: The histograms belongs to this object and won't get deleted
+// together with the canvas.
+//
 void MHHillas::Draw(Option_t *)
 {
@@ -86,14 +141,17 @@
 
     c->cd(1);
-    fAlpha->SetBit(kCanDelete);
+    //fAlpha->SetBit(kCanDelete);
     fAlpha->Draw();
+
     c->cd(2);
-    fLength->SetBit(kCanDelete);
+    //fLength->SetBit(kCanDelete);
     fLength->Draw();
+
     c->cd(3);
-    fDist->SetBit(kCanDelete);
+    //fDist->SetBit(kCanDelete);
     fDist->Draw();
+
     c->cd(4);
-    fWidth->SetBit(kCanDelete);
+    //fWidth->SetBit(kCanDelete);
     fWidth->Draw();
 
Index: trunk/MagicSoft/Mars/mhist/MHHillas.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHHillas.h	(revision 951)
+++ trunk/MagicSoft/Mars/mhist/MHHillas.h	(revision 952)
@@ -33,4 +33,5 @@
 
     void Draw(Option_t *opt=NULL);
+    TObject *DrawClone(Option_t *opt=NULL);
 
     ClassDef(MHHillas, 1) // Container which holds hostograms for the Hillas parameters
Index: trunk/MagicSoft/Mars/mhist/MHStarMap.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHStarMap.cc	(revision 951)
+++ trunk/MagicSoft/Mars/mhist/MHStarMap.cc	(revision 952)
@@ -44,4 +44,8 @@
 ClassImp(MHStarMap);
 
+// --------------------------------------------------------------------------
+//
+// Create the star map histogram
+//
 MHStarMap::MHStarMap (const char *name, const char *title)
 {
@@ -68,4 +72,8 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// delete the histogram instance
+//
 MHStarMap::~MHStarMap()
 {
@@ -73,64 +81,9 @@
 }
 
-void MHStarMap::Draw(Option_t *)
-{
-    //
-    // Creates a new canvas, creates a useful palette and
-    // draws the histogram in the new created canvas
-    //
-
-    TCanvas *c = new TCanvas("Star Map", "Star Map created from Hillas Parameters", 500, 500);
-
-    //
-    // Set the palette you wanna use:
-    //  - you could set the root "Pretty Palette Violet->Red" by
-    //    gStyle->SetPalette(1, 0), but in some cases this may look
-    //    confusing
-    //  - The maximum colors root allowes us to set by ourself
-    //    is 50 (idx: 51-100). This colors are set to a grayscaled
-    //    palette
-    //  - the number of contours must be two less than the number
-    //    of palette entries
-    //
-
-    const Int_t numg = 32; // number of gray scaled colors
-    const Int_t numw = 32; // number of white
-
-    Int_t palette[numg+numw];
-
-    //
-    // The first half of the colors are white.
-    // This is some kind of optical background supression
-    //
-    gROOT->GetColor(51)->SetRGB(1, 1, 1);
-
-    Int_t i;
-    for (i=0; i<numw; i++)
-        palette[i] = 51;
-
-    //
-    // now the (gray) scaled part is coming
-    //
-    for (;i<numw+numg; i++)
-    {
-        const Float_t gray = 1.0-(float)(i-numw)/(numg-1.0);
-
-        gROOT->GetColor(52+i)->SetRGB(gray, gray, gray);
-        palette[i] = 52+i;
-    }
-
-    //
-    // Set the palette and the number of contour levels
-    //
-    gStyle->SetPalette(numg+numw, palette);
-    fStarMap->SetContour(numg+numw-2);
-
-    // gStyle->SetPalette(1, 0);
-    fStarMap->Draw("colz");
-
-    c->Modified();
-    c->Update();
-}
-
+// --------------------------------------------------------------------------
+//
+// Fill the four histograms with data from a MHillas-Container.
+// Be careful: Only call this with an object of type MHillas
+//
 void MHStarMap::Fill(const MParContainer *par)
 {
@@ -160,2 +113,95 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Set the palette you wanna use:
+//  - you could set the root "Pretty Palette Violet->Red" by
+//    gStyle->SetPalette(1, 0), but in some cases this may look
+//    confusing
+//  - The maximum colors root allowes us to set by ourself
+//    is 50 (idx: 51-100). This colors are set to a grayscaled
+//    palette
+//  - the number of contours must be two less than the number
+//    of palette entries
+//
+void MHStarMap::PrepareDrawing()
+{
+    const Int_t numg = 32; // number of gray scaled colors
+    const Int_t numw = 32; // number of white
+
+    Int_t palette[numg+numw];
+
+    //
+    // The first half of the colors are white.
+    // This is some kind of optical background supression
+    //
+    gROOT->GetColor(51)->SetRGB(1, 1, 1);
+
+    Int_t i;
+    for (i=0; i<numw; i++)
+        palette[i] = 51;
+
+    //
+    // now the (gray) scaled part is coming
+    //
+    for (;i<numw+numg; i++)
+    {
+        const Float_t gray = 1.0-(float)(i-numw)/(numg-1.0);
+
+        gROOT->GetColor(52+i)->SetRGB(gray, gray, gray);
+        palette[i] = 52+i;
+    }
+
+    //
+    // Set the palette and the number of contour levels
+    //
+    gStyle->SetPalette(numg+numw, palette);
+    fStarMap->SetContour(numg+numw-2);
+}
+
+
+// --------------------------------------------------------------------------
+//
+// Draw clones of the  histograms, so that the object can be deleted
+// and the histogram is still visible in the canvas.
+// The cloned object is deleted together with the canvas if the canvas is
+// destroyed. If you want to handle destroying the canvas you can get a
+// pointer to it from this function
+//
+TObject *MHStarMap::DrawClone(Option_t *opt)
+{
+    TCanvas *c = new TCanvas("Star Map", "Star Map created from Hillas Parameters", 500, 500);
+
+    //
+    // This is necessary to get the expected bahviour of DrawClone
+    //
+    gROOT->SetSelectedPad(NULL);
+
+    PrepareDrawing();
+
+    fStarMap->DrawClone("colz")->SetBit(kCanDelete);
+
+    c->Modified();
+    c->Update();
+
+    return c;
+}
+
+// --------------------------------------------------------------------------
+//
+// Creates a new canvas and draw the histogram into it.
+// Be careful: The histogram belongs to this object and won't get deleted
+// together with the canvas.
+//
+void MHStarMap::Draw(Option_t *)
+{
+    TCanvas *c = new TCanvas("Star Map", "Star Map created from Hillas Parameters", 500, 500);
+
+    PrepareDrawing();
+
+    fStarMap->Draw("colz");
+
+    c->Modified();
+    c->Update();
+}
+
Index: trunk/MagicSoft/Mars/mhist/MHStarMap.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHStarMap.h	(revision 951)
+++ trunk/MagicSoft/Mars/mhist/MHStarMap.h	(revision 952)
@@ -23,4 +23,6 @@
     TH2F *fStarMap;
 
+    void PrepareDrawing();
+
 public:
      MHStarMap(const char *name=NULL, const char *title=NULL);
@@ -29,6 +31,8 @@
     void Fill(const MParContainer *par);
 
-    TH2F *GetHist()               { return fStarMap; }
+    TH2F *GetHist() { return fStarMap; }
+
     void Draw(Option_t *opt=NULL);
+    TObject *DrawClone(Option_t *opt=NULL);
 
     ClassDef(MHStarMap, 1) // Container to hold 2-dim histogram (starmap)
Index: trunk/MagicSoft/Mars/mmain/MAnalysis.cc
===================================================================
--- trunk/MagicSoft/Mars/mmain/MAnalysis.cc	(revision 951)
+++ trunk/MagicSoft/Mars/mmain/MAnalysis.cc	(revision 952)
@@ -230,11 +230,8 @@
     plist.AddToList(&hillas);
 
-    MHHillas *hists = new MHHillas;
-    plist.AddToList(hists);
-
-    MHStarMap *smap = new MHStarMap;
-    plist.AddToList(smap);
-
-    // FIXME: Where do we delete this two objects???
+    MHHillas  hists;
+    MHStarMap smap;
+    plist.AddToList(&hists);
+    plist.AddToList(&smap);
 
     //
@@ -254,10 +251,8 @@
     //   CalEvents:  Calibration Events
     //
-    MReadTree      read("Events", fInputFile);
-    MCerPhotCalc   ncalc;
-    MImgCleanStd   clean(cleanlvl1, cleanlvl2);
-    MHillasCalc    hcalc;
-    MFillH         hfill(&hillas, hists);
-    MFillH         sfill(&hillas, smap);
+    MReadTree    read("Events", fInputFile);
+    MCerPhotCalc ncalc;
+    MImgCleanStd clean(cleanlvl1, cleanlvl2);
+    MHillasCalc  hcalc;
 
     tlist.AddToList(&read);
@@ -265,6 +260,13 @@
     tlist.AddToList(&clean);
     tlist.AddToList(&hcalc);
-    tlist.AddToList(&hfill);
-    tlist.AddToList(&sfill);
+
+    MFillH hfill(&hillas, &hists);
+    MFillH sfill(&hillas, &smap);
+
+    if (displhillas)
+        tlist.AddToList(&hfill);
+
+    if (displstarmap)
+        tlist.AddToList(&sfill);
 
     //
@@ -283,11 +285,11 @@
     // After the analysis is finished we can display the histograms
     //
-
     if (displhillas)
-        hists->Draw();
+        hists.DrawClone();
 
     if (displstarmap)
-        smap->Draw();
-
+        smap.DrawClone();
+
+    cout << endl;
     cout << "Calculation of Hillas Parameters finished without error!" << endl;
 }
Index: trunk/MagicSoft/Mars/mmain/MBrowser.cc
===================================================================
--- trunk/MagicSoft/Mars/mmain/MBrowser.cc	(revision 951)
+++ trunk/MagicSoft/Mars/mmain/MBrowser.cc	(revision 952)
@@ -255,4 +255,5 @@
     fInputFile[0] = '\0';
 
+    fList = new TList;
     fList->SetOwner();
 
@@ -308,4 +309,6 @@
     fClient->FreePicture(fPic2);
     fClient->FreePicture(fPic3);
+
+    delete fList;
 } 
 
