# TCanvas grpahs

**URL:** <https://root-forum.cern.ch/t/tcanvas-grpahs/58840>\
**Category:** Newbie\
**Tags:** root\
**Created:** [April 3, 2024, 8:06pm UTC](https://root-forum.cern.ch/t/tcanvas-grpahs/58840 "2024-04-03T20:06:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![akberovic](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/akberovic/32/32917_2.png) [@akberovic](https://root-forum.cern.ch/u/akberovic)\
**Post date:** [April 3, 2024, 8:06pm UTC](https://root-forum.cern.ch/t/tcanvas-grpahs/58840/1 "2024-04-03T20:06:16Z")

</div>

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

 ![Screenshot 2024-04-02 164914](https://root-forum.cern.ch/uploads/default/original/3X/6/a/6a88a33f03da6954daaf599f015d20867d76c6b1.png)  
 ![Screenshot 2024-04-02 164938](https://root-forum.cern.ch/uploads/default/original/3X/1/a/1a0519f421f529f9b015a5987a2c123b8be72261.png)

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 4, 2024, 5:48am UTC](https://root-forum.cern.ch/t/tcanvas-grpahs/58840/2 "2024-04-04T05:48:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![akberovic](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/akberovic/32/32917_2.png) [@akberovic](https://root-forum.cern.ch/u/akberovic)\
**Post date:** [April 4, 2024, 4:58pm UTC](https://root-forum.cern.ch/t/tcanvas-grpahs/58840/3 "2024-04-04T16:58:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 5, 2024, 6:39am UTC](https://root-forum.cern.ch/t/tcanvas-grpahs/58840/4 "2024-04-05T06:39:41Z")

</div>

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

```auto
   TCanvas c;
   TGraph g,

```

do:

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

```

and to acesss tthe methods, instead of:

```auto
   g.Draw();

```

do:

```auto
   g->Draw();

```
