Saving Canvas to PDF

Dear all,

I have some trouble saving a TCanvas to a pdf file. The canvas contains three histograms as well as a TF1. I have attached a ROOT file containing this canvas.

Now, when I save this canvas to a pdf, only the red histogram is visible (see attachment). Saving e.g. to a png or jpg works as expected, however.

What is the difference between saving to a pdf and to an image? Can I do anything about this?

ROOT version is 5.32/00

Thanks a lot,

test.root (96.6 KB)
test.pdf (30.1 KB)

I see the problem.
I am investigating.

The PDF file generated from your canvas stored in the root file you sent, is buggy.
I get an error message when I open it with Acrobat Reader. But acrobat does not tell what’s
wrong. As PDF is a binary compressed file it is hard to tell at the first glance what is wrong.
So I tried something else: I generated the .C file form the root file you sent. Then I executed
this macro and from the picture I get I produced the PDF file. This PDF is now ok.
My guess it that there is something the ROOT PDF generator does not like in the initial macro which
generated this canvas. It will be helpful to see it to debug this further.

That’s totally different code.


Thanks for investigating. It is a bit difficult to provide a simple script reproducing the canvas, but maybe you can give a general advice:

The special thing about my situation is that the histograms are drawn in one function into gPad and then gPad is saved as PDF in another function. Can this cause ROOT to somehow lose track of some objects?



No … il should be fine…
My guess would be more that you are using a wrong or not initialized attribute value …
Without some code to work on it will be hard to debug.

Maybe you could try to add:
or maybe even better (“gPad” does not necessarily point to “MyCanvas”):
after you draw everything and before you create a “.pdf”.

There is a real mistake in the PDF file. That is why it stops rendering. Very likely all the histograms are in the PDF file but because of some wrong value the rendering stops. That’s why I am suspecting you are either passing a wrong attribute value or some of your data are Nan or Inf … But that’s not easy to see . I would really need a macro which reproduces this behavior so I can add some protection in the appropriate place in the PDF engine.

Hi again,

I tried to create some working example for you, so that you can work on the problem. However, in all cases I came up with, it worked fine. I still have no clue about what might be the circumstances in which this problem appears (other than in the complex context which created the canvas above). As soon as I find some hint, I will get back to you.

Thanks anyway,


My guess is that some float value might be undefined (Nan or Inf).
Let me know if you find something reproducing le problem.