Update one TH1D in multigraph canvas

I have some problems updating a TH1D in a multigraph canvas in a separate function.
In the main I have:

TCanvas *cpha = new TCanvas("cpha","Separation diagram",0,0,600,600);
TH2D *hpha = new TH2D("Sep",pha_char,nxCh,0,nxCh-1,nyCh,0,nyCh-1);
for (jx = 0; jx < nxCh; jx++)
  {
  for(jy = 0; jy < nyCh; jy++)
    {
    hpha->SetBinContent(jx,jy, PHAmatrix[jy*nxCh + jx]);
    }
  }
TH1D *sepnew = new TH1D("phasep","Separatrix",nxCh,0,nxCh-1);
for (i=0; i<nxCh; i++)
  {
  sepnew->Fill(i,Separatrix[i]); // This is not just a TLine object
  }
sepnew->SetLineColor(kRed);
sepnew->Draw("same");

whereas in my “replot” subroutine after redefining the array “Separatrix” the code is

  TCanvas *canvas1 = (TCanvas*) gROOT->FindObject("cpha");
  TH1D *pha_sep =  (TH1D*) gROOT->FindObject("phasep");
  if (pha_sep)
    {
    PUTLOG(0,"Removing Sep plot");
    delete pha_sep;
    }
  TH1D *sepnew = new TH1D("phasep","Separatrix",nxCh,0,nxCh-1);
  for (i=0; i<nxCh; i++)
    {
    sepnew->Fill(i,Separatrix[i]);
    }
  sepnew->SetLineColor(kRed);
  sepnew->Draw("same");
  canvas1->Modified();
  canvas1->Update();

Actually it works whenever I call the function immediately after displaying the “cpha” TCanvas. However, if I create further TCanvases (with different names of course), the replotting routine does not work, usually it gives a segmentation violation. Does anybody know why?

Thanks in advance
Giovanni

Can you post a small running example reproducing the problem ?

In your “replot” subroutine, try to replace the line:
TCanvas canvas1 = (TCanvas) gROOT->FindObject(“cpha”);
with:
TCanvas *canvas1 = ((TCanvas *)(gROOT->GetListOfCanvases()->FindObject(“cpha”)));
if (canvas1) canvas1->cd();
else { std::cout << “Warning: cpha canvas not found.” << std::endl; return; }
Also, after the line:
delete pha_sep;
try to add:
canvas1->Modified();

Thanks a lot! The line

did it!!