# TApplication usage and live updating canvas

**URL:** <https://root-forum.cern.ch/t/tapplication-usage-and-live-updating-canvas/37317>\
**Category:** Newbie\
**Created:** [December 25, 2019, 5:07pm UTC](https://root-forum.cern.ch/t/tapplication-usage-and-live-updating-canvas/37317 "2019-12-25T17:07:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bernardosilva](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@bernardosilva](https://root-forum.cern.ch/u/bernardosilva)\
**Post date:** [December 25, 2019, 5:07pm UTC](https://root-forum.cern.ch/t/tapplication-usage-and-live-updating-canvas/37317/1 "2019-12-25T17:07:52Z")

</div>

I am having some trouble finding useful TApplication examples so I can understand it’s usage better. The only successfull programs I’ve made were with `SetReturnFromRun(true)` and `gPad->WaitPrimitive()` which, makes it so that the program continues after double clicking the TApplication (although I’m not really 100% sure what is doing what).  
Now I would like to have something like opening the Application and updating the canvas to show a different TGraph (or the same one with different values) without having to click it (some kind of animation, if that is even possible). If anyone could shine some light and help me better understand this I would be very grateful.

Thanks

\_ROOT Version:\_6.14  
\_Platform:\_Linux  
_Compiler:_ g++

* * *

---

<div class="post-metadata">

**Author:** ![marty1885](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marty1885/32/19232_2.png) [@marty1885](https://root-forum.cern.ch/u/marty1885)\
**Post date:** [December 26, 2019, 4:18am UTC](https://root-forum.cern.ch/t/tapplication-usage-and-live-updating-canvas/37317/2 "2019-12-26T04:18:17Z")

</div>

Hi,  
you might want to check out my blog post on real time plotting using ROOT. I’ve updated the code to have it working on 6.18.

The gist is: after you updated all your TGraph/THx you do

```cpp
for(int i=1;i<=num_subplots;i++){
	c1->cd(i)
	c1->Update();
	c1->Pad()->Draw();
}
// if TApplication is allocated on heap 
gSystem->ProcessEvent(); // remove this line if TApp is on stack

```

> **[Real time plotting in C++ using ROOT](https://mightynotes.wordpress.com/2019/07/09/real-time-plotting-in-c-using-root/)**
>
> Sometime you just need to plot stuff as time progresses. Maybe you need to plot some statistics of your software, or visualize in realtime. But there’s not much option. matplotlib is too slow…

---

<div class="post-metadata">

**Author:** ![bernardosilva](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@bernardosilva](https://root-forum.cern.ch/u/bernardosilva)\
**Post date:** [December 26, 2019, 10:16am UTC](https://root-forum.cern.ch/t/tapplication-usage-and-live-updating-canvas/37317/3 "2019-12-26T10:16:52Z")

</div>

That’s very useful, thanks! How would you go about implementing a method that is called multiple times to redraw the canvas, but only declares everything needed (TApplication, canvas, TGraph) once, the first time it is called?

---

<div class="post-metadata">

**Author:** ![marty1885](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marty1885/32/19232_2.png) [@marty1885](https://root-forum.cern.ch/u/marty1885)\
**Post date:** [December 26, 2019, 4:28pm UTC](https://root-forum.cern.ch/t/tapplication-usage-and-live-updating-canvas/37317/4 "2019-12-26T16:28:12Z")

</div>

You initialize the everything somewhere and put the drawing in a loop.

```auto
int main()
{
    // The initialization phase
    TApplication app(...);
    auto c1 = new TCanvas(...);
    auto g1 = new TGraph(...);
    g1->Draw(); // remember to draw here

    while(true) {
        .... // Do whatever you want

        // Redraw the plots
        c1->Update();
        c1->Pad()->Draw();
    }
}

```

---

<div class="post-metadata">

**Author:** ![bernardosilva](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/f08c70/32.png) [@bernardosilva](https://root-forum.cern.ch/u/bernardosilva)\
**Post date:** [December 27, 2019, 7:24pm UTC](https://root-forum.cern.ch/t/tapplication-usage-and-live-updating-canvas/37317/5 "2019-12-27T19:24:57Z")

</div>

Meanwhile I was able to do it, thanks!  
Last unrelated question: any idea on how to add an extra pad on a divided canvas (Imagine I have a row with 3 columns and want to add a forth one). Right now I’m doing that by clearing the canvas and re-dividing it, but I feel like there should be a more elegant way of doing that.

---

<div class="post-metadata">

**Author:** ![marty1885](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/marty1885/32/19232_2.png) [@marty1885](https://root-forum.cern.ch/u/marty1885)\
**Post date:** [December 28, 2019, 2:26am UTC](https://root-forum.cern.ch/t/tapplication-usage-and-live-updating-canvas/37317/6 "2019-12-28T02:26:21Z")

</div>

I don’t know if this is better. You can `c1->Clear()` then `c1->Divide(4, 1)` and the re-add all your graphs.

**Edit**. nevermind. I thought you are deleting and recreating the canvas
