TCanvas *c = new TCanvas("plot", "plot");
c->BuildLegend();
c->Modified(); c->Update();
It gives a warning the second time:
Naturally, I tried to encapsulate the code within an if-statement:
if (!gROOT->GetListOfCanvases()->FindObject("plot")) {
TCanvas *c = new TCanvas("plot", "plot");
}
so that it wouldn’t create a new TCanvas the second time. But this only makes things worse:
: error: use of undeclared identifier 'c'
c->BuildLegend();
^
error: use of undeclared identifier 'c'
c->Modified(); c->Update();
^
error: use of undeclared identifier 'c'
c->Modified(); c->Update();
^
Now I completely fail to see how these warnings and errors are consistent.
My question is how do I modify the ‘TCanvas *c = new TCanvas’-statement so that it works properly the second time I run it (either within the if-statement or without)?
the issue is that you are declaring the canvas pointer within a scope:
upon closing the curly brace, all variables local to the scope, e.g. your “c”, are forgotten by the compiler.
You could do this:
Cheers,
Danilo[/quote]
The comment about variables local to the scope was insightful. But the code gave a crash after the second run:
[code] *** Break *** segmentation violation
There was a crash.
This is the entire stack trace of all threads:
[/code]
[quote=“Pepe Le Pew”]Try: TCanvas *c = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("plot");
if (c) c->Clear(); else c = new TCanvas("plot", "plot");[/quote]
This keeps working fine. I’ll study on why/how the first line works. The second line seems clear to me.