TApplication usage and live updating canvas

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++


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

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
1 Like

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?

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

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();
    }
}

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.

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