Problem with canvas update in TGraph2D

Dear all,
I’ve been trying to draw some small animations using TGraph and TGraph2D. My aim is to draw particle trajectories on a TCanvas that keeps updating and lets the user see the motion as it happens. I’m using ROOT version 5.34/12 (now compiled with debug symbols, since I’ve tried to look deeper into the matter).

As far as TGraph is concerned, everything works fine. For example, this snippet of code draws the trajectory of a particle going around in a circle:

[code]//WORKING CODE
{
TCanvas *c = new TCanvas;
TGraph *g = new TGraph;
const Double_t pi = TMath::Pi();

for(Int_t i=0; i<100; ++i) {
    g->SetPoint(i,cos(pi/50*i),sin(pi/50*i));
    g->Draw("ALINE");
    c->Modified();
    c->Update();
}

}
//END OF WORKING CODE[/code]

My problem is, when I try to do the exact same thing with a TGraph2D (which a naive user would think to behave in the same way), I get a very weird behaviour and in the end at most two points are drawn on canvas. This is a macro that is supposed to draw a spiral:

[code]//BROKEN CODE
{
TCanvas *c = new TCanvas;
TGraph2D *g = new TGraph2D;
Double_t pi = TMath::Pi();

/*add the first two points to set the scale beforehand
(I'm aware there are better ways to do it)*/
g->SetPoint(0,-1,-1,0);
g->SetPoint(1,1,1,180);

for(Int_t i=0; i<200; ++i) {
    g->SetPoint(i+2,cos(pi/50*i),sin(pi/50*i),i);
    g->Draw("ALINE");
    c->Modified();
    c->Update();
}
/* everything works fine if I move Draw(), Modified() and
Update() down here, out of the loop, but then I wouldn't get to "see" the motion */

}
//END OF BROKEN CODE[/code]

Any help is greatly appreciated, thanks in advance.

yes … That’s weird … give me time to investigate.
One thing already, The option LINE does not exist for Graph2D
try P0 for instance … but that doesn’t really help.

I found a way to do it:

{
    TCanvas *c = new TCanvas;
    Double_t pi = TMath::Pi();

    TH3F* h3 = new TH3F("h3", "h3", 10., -1., 1., 10., -1., 1., 10, 0., 200.);
    h3->Draw();
   TPolyLine3D *l = new TPolyLine3D();
    l->Draw();

    for (Int_t i=0; i<200; ++i) {
        l->SetPoint(i+2,cos(pi/50*i),sin(pi/50*i),i);
        c->Modified();
        c->Update();
    }

Hello,
thank you very much for your quick answer!

Your code works like a charm, even though I guess I don’t know enough about ROOT internals to take a guess as to why :stuck_out_tongue:

Just to let you know, the documentation here states that LINE is a valid option for a TGraph2D too.

Oops … yes sorry … I had forgot about that one…