Index: trunk/MagicSoft/Mars/mhbase/MH3.cc
===================================================================
--- trunk/MagicSoft/Mars/mhbase/MH3.cc	(revision 6280)
+++ trunk/MagicSoft/Mars/mhbase/MH3.cc	(revision 6869)
@@ -389,6 +389,19 @@
     if (fHist)
     {
+        if (gPad)
+        {
+            const TString pfx(Form("%sProfX", fHist->GetName()));
+            const TString pfy(Form("%sProfY", fHist->GetName()));
+
+            TProfile *p = 0;
+            if ((p=dynamic_cast<TProfile*>(gPad->FindObject(pfx))))
+                p->SetName(Form("%sProfX", name));
+            if ((p=dynamic_cast<TProfile*>(gPad->FindObject(pfy))))
+                p->SetName(Form("%sProfY", name));
+        }
+
         fHist->SetName(name);
         fHist->SetDirectory(0);
+
     }
     MParContainer::SetName(name);
@@ -505,53 +518,20 @@
 void MH3::Paint(Option_t *o)
 {
-    fHist->SetFillStyle(4000);
-
-    TString str(o);
-    str.ToLower();
-
-    const Bool_t only  = str.Contains("only")  && fDimension==2;
-    const Bool_t same  = str.Contains("same")  && fDimension==2;
-    const Bool_t blue  = str.Contains("blue")  && fDimension==2;
-    const Bool_t profx = str.Contains("profx") && fDimension==2;
-    const Bool_t profy = str.Contains("profy") && fDimension==2;
-    // Do NOT replace 'same'-option
-    str.ReplaceAll("only",  "");
-    str.ReplaceAll("blue",  "");
-    str.ReplaceAll("profx", "");
-    str.ReplaceAll("profy", "");
-
-    // WARNING: Don't use FindObject directly always use GetListOfPrimitives!!
-
-    // FIXME: We may have to remove all our own options from str!
-    if (!only && !gPad->GetListOfPrimitives()->FindObject(fHist))
-        fHist->Draw(str);
-
-    if (profx)
-    {
-        TProfile *p = ((TH2*)fHist)->ProfileX("ProfX", -1, 9999, "s");
-        if (!gPad->GetListOfPrimitives()->FindObject(p))
-        {
-            p->UseCurrentStyle();
-            p->SetLineColor(blue ? kBlue : fHist->GetLineColor());
-            p->SetBit(kCanDelete);
-            p->SetDirectory(NULL);
-            p->SetXTitle(fHist->GetXaxis()->GetTitle());
-            p->SetYTitle(fHist->GetYaxis()->GetTitle());
-            p->Draw(only&&!same?"":"same");
-        }
-    }
-    if (profy)
-    {
-        TProfile *p = ((TH2*)fHist)->ProfileY("ProfY", -1, 9999, "s");
-        if (!gPad->GetListOfPrimitives()->FindObject(p))
-        {
-            p->UseCurrentStyle();
-            p->SetLineColor(blue ? kBlue : fHist->GetLineColor());
-            p->SetBit(kCanDelete);
-            p->SetDirectory(NULL);
-            p->SetYTitle(fHist->GetXaxis()->GetTitle());
-            p->SetXTitle(fHist->GetYaxis()->GetTitle());
-            p->Draw(only&&!same?"":"same");
-        }
+    TProfile *p=0;
+
+    const TString pfx(Form("%sProfX", fHist->GetName()));
+    if ((p=dynamic_cast<TProfile*>(gPad->FindObject(pfx))))
+    {
+        Int_t col = p->GetLineColor();
+        p = ((TH2*)fHist)->ProfileX(pfx, -1, 9999, "s");
+        p->SetLineColor(col);
+    }
+
+    const TString pfy(Form("%sProfY", fHist->GetName()));
+    if ((p=dynamic_cast<TProfile*>(gPad->FindObject(pfy))))
+    {
+        Int_t col = p->GetLineColor();
+        p = ((TH2*)fHist)->ProfileY(pfy, -1, 9999, "s");
+        p->SetLineColor(col);
     }
 
@@ -581,8 +561,5 @@
     TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(fHist);
     pad->SetBorderMode(0);
-    AppendPad(opt);
-
-    return;
-/*
+
     fHist->SetFillStyle(4000);
 
@@ -590,37 +567,56 @@
     str = str.Strip(TString::kBoth);
 
-    Bool_t only = str.Contains("ONLY", TString::kIgnoreCase) && fDimension==2;
-    Bool_t same = str.Contains("SAME", TString::kIgnoreCase) && fDimension==2;
-    Bool_t blue = str.Contains("BLUE", TString::kIgnoreCase) && fDimension==2;
+    const Bool_t only  = str.Contains("ONLY",  TString::kIgnoreCase) && fDimension==2;
+    const Bool_t same  = str.Contains("SAME",  TString::kIgnoreCase) && fDimension==2;
+    const Bool_t blue  = str.Contains("BLUE",  TString::kIgnoreCase) && fDimension==2;
+    const Bool_t profx = str.Contains("PROFX", TString::kIgnoreCase) && fDimension==2;
+    const Bool_t profy = str.Contains("PROFY", TString::kIgnoreCase) && fDimension==2;
+
+    str.ReplaceAll("only",  "");
+    str.ReplaceAll("blue",  "");
+    str.ReplaceAll("profx", "");
+    str.ReplaceAll("profy", "");
+
     // FIXME: We may have to remove all our own options from str!
     if (!only)
         fHist->Draw(str);
 
-    AppendPad("upd");
-
-    if (str.Contains("PROFX", TString::kIgnoreCase) && fDimension==2)
-    {
-        TProfile *p = ((TH2*)fHist)->ProfileX(fNameProfX, -1, 9999, "s");
+    AppendPad();
+
+    TProfile *p=0;
+    if (profx)
+    {
+        const TString pfx(Form("%sProfX", fHist->GetName()));
+
+        if (same && (p=dynamic_cast<TProfile*>(gPad->FindObject(pfx))))
+            *fLog << warn << "TProfile " << pfx << " already in pad." << endl;
+
+        p = ((TH2*)fHist)->ProfileX(pfx, -1, 9999, "s");
         p->UseCurrentStyle();
         p->SetLineColor(blue ? kBlue : fHist->GetLineColor());
-        p->Draw(only&&!same?"":"same");
         p->SetBit(kCanDelete);
         p->SetDirectory(NULL);
         p->SetXTitle(fHist->GetXaxis()->GetTitle());
         p->SetYTitle(fHist->GetYaxis()->GetTitle());
-    }
-    if (str.Contains("PROFY", TString::kIgnoreCase) && fDimension==2)
-    {
-        TProfile *p = ((TH2*)fHist)->ProfileY(fNameProfY, -1, 9999, "s");
+        p->Draw(only&&!same?"":"same");
+    }
+    if (profy)
+    {
+        const TString pfy(Form("%sProfY", fHist->GetName()));
+
+        if (same && (p=dynamic_cast<TProfile*>(gPad->FindObject(pfy))))
+            *fLog << warn << "TProfile " << pfy << " already in pad." << endl;
+
+        p = ((TH2*)fHist)->ProfileY(pfy, -1, 9999, "s");
         p->UseCurrentStyle();
         p->SetLineColor(blue ? kBlue : fHist->GetLineColor());
-        p->Draw(only&&!same?"":"same");
         p->SetBit(kCanDelete);
         p->SetDirectory(NULL);
         p->SetYTitle(fHist->GetXaxis()->GetTitle());
         p->SetXTitle(fHist->GetYaxis()->GetTitle());
-    }
-
-    AppendPad("log");*/
+        p->Draw(only&&!same?"":"same");
+    }
+
+    //AppendPad("log");
 }
 
