How can I use SaveAs without using Draw

Hi all friends,

I am using XShell and Xming to work on the cluster.

In the past, I always used TH1->Draw() and checkd the result by seeing the automatically jump out GUI window. (That window is similar to TBrowser)

I recently draw many histograms which including 2-D histogram. And found that using TH2->Draw() to check the result is soooooo slow because of the GUI. That waste so much time.

I’d like to use a way to save the histogram as a .png or .pdf file directly without any jump out GUI.
I think it may be faster if I check it by downloading to local.

I have tried a way as follow

TH2F *h2 = new TH2F ( initialization );
TCanvas *c1 = new TCanvas();

h2->Add("other 2d hist");
h2->Draw();
c1->SaveAs("xxx.pdf");

I can obtain a readable “xxx.pdf” file under the working directory, however I still got the jump out GUI window.

It still wastes time and it is inconvenient for me.

So is there any suggestions to deal with this situation? I have spent all day on google it… #-o

It’s also welcomed if you post a tutorial link only, I can figure it out by myself.

Either start “root -b” (try “root -?”) or:
gROOT->SetBatch(kTRUE); // before creating the canvas

Hi,

the method you are looking for is “TCanvas::Print”: root.cern.ch/doc/master/classTP … e094de93b6
To rephrase your example:

auto h2 = new TH2F ( initialization );
auto c1 = new TCanvas();

h2->Add("other 2d hist");
h2->Draw();
c1->Print("xxx.pdf");

Cheers,
D

[quote=“Pepe Le Pew”]Either start “root -b” (try “root -?”) or:
gROOT->SetBatch(kTRUE); // before creating the canvas[/quote]

Thanks a lot.
Although I didn’t try “root -b”, gROOT is working. 8)
Hummmmmmm… I should ask root talker earlier #-o

[quote=“dpiparo”]Hi,

the method you are looking for is “TCanvas::Print”: root.cern.ch/doc/master/classTP … e094de93b6
To rephrase your example:

auto h2 = new TH2F ( initialization );
auto c1 = new TCanvas();

h2->Add("other 2d hist");
h2->Draw();
c1->Print("xxx.pdf");

Cheers,
D[/quote]

I tried this way before. But somehow it doesn’t work.
Anyway, thank you for your kind and suggestion. :smiley:

Hi,

this should be the right way to go.
Let us know if you encounter issues.

D

TPad::Print(“SomeFileName”) is exactly the same as TPad:SaveAs(“SomeFileName”)

BTW. Note that “root -b” doesn’t need any source code changes (so that, for “visual inspection” purposes, you can execute the same macro in “interactive mode” using simply “root”).