TCanvas->SaveAs()

Dear root experts,

It seems that I run into a problem about TCanvas->SaveAs().
If we want to SaveAs a TCanvas, do we have to do it immediately after
we draw on this canvas?
More specifically,
c1 = new TCanvas(“c1”, “c1”, 1000, 500);
h1 = new TH1F(“h1”, “h1”, 100, 0, 50);
h1->Draw();

    c2 = new TCanvas("c2", "c2", 1000, 500);
    h2 = new TH1F("h2", "h2", 50, 0, 25);
    h2->Draw();

    c1->SaveAs("c1.gif");
    c2->SaveAs("c2.gif");

So I call c1->SaveAs() after I do another draw() one more time. This little
piece of script runs correctly. And there is also message telling you,
"Processing test.C…
Info in TCanvas::Print: GIF file c1.gif has been created
Info in TCanvas::Print: GIF file c2.gif has been created
"
But when I log out of root and check the two saved graphs. c1.gif is a
really empty graph. If I move c1->SaveAs() up to the point right after
h1->Draw(). There will be no problem anymore. Do you know what
is going on?

I know I can just avoid the problem by move the c1->SaveAs() statement up. But when there are really a large number of histograms to
draw, you really want to do the same things together.

Thanks in advance for your help.

Guofan

1 Like

I guess you are running this these command from a macro. In such case the you should put the command:
gPad->Update();
after each Draw()

I am a rookie.
I still can’t think out why we need a gPad->Update() after each Draw().
But that really solve the problem.
Thanks very much for your reply.

Sorry for bothering again.
Using the gPad->Update() really works in that small piece of code.
But when I am trying the same thing in my big macro,
I end up with some strange error.
Here is the thing.
[color=red]“root -l plot_v2.C
TDR Style initialized
root [0]
Processing plot_v2.C…
Error: non class,struct,union object $gPad used with . or -> plot_v2.C:855:
*** Interpreter error recovered ***
”[/color]

In what situation will this happen?
My macro is really big. I can’t copy and paste them all here.
But in my understanding, it is really the same in essence with that
small piece of code.

I create an TCanvas array in my macro, and do an iteration over this
array. Each time I divide a single TCanvas into two subpads and draw
a THStack on each pad.

I really don’t see any difference between the two. So I am confused why one is working, and the other is not.

Hi,

it might be due to a bug in your code. Check the line 854 or run your code in compiled mode as “root -l plot_v2.C+”.

Cheers, Axel.

Hi, Axel,

Thanks for your reply. But I am not sure there is a bug or something.
Without those gPad->Update() statements, the code runs correctly.
And the drawn histograms look fine.

see root.cern.ch/drupal/content/graphics-pad

Hi,

as I said, please try in compiled mode using ACLiC (the “+” at the end). The compiler will probably complain about many errors (like missing includes etc); fix those and you will probably also fix the gPad problem you see.

Cheers, Axel.