TCanvas grpahs

Hello, when I run the code the histograms in ROOT are displayed correctly while the macro is running, but once the macro finishes, the histograms disappear or turn white. I couldn’t find the reason behind it. Can someone help me with that? I attached screenshots from snippet


Hi,

This seems a ownership matter. When the macro ends, the scope where the histograms are created is closed. As a result, the objects are deleted: this is standard C++. A way is to be adopted to make histograms survive after the scope where they are created. Three possibilities can be:

  • Use the DrawClone method instead of Draw to draw them
  • Store them in a container that is in the global scope
  • Create them as pointers

I hope this helps, if not don’t hesitate to come back with questions!

Cheers,
D

Hello, thank you for answering.

I couldn’t understand what you meant in step two and three. Can you please explain or give a small example?

Step three is the simplest seems to me. In your macro, instead of doing

   TCanvas c;
   TGraph g,

do:

    auto c = new TCanvas();
    auto g = new TGraph();

and to acesss tthe methods, instead of:

   g.Draw();

do:

   g->Draw();