Problem with saving multiple canvases to a single pdf

i am having a hard time with saving multiple plots (canvases) to a single pdf (i always ended with damaged file / file without any content)
the reproducer:
prob_tpdf-Copy1.ipynb (107.5 KB)

a similar discussion is at


ROOT Version: 6.24.00 (conda)
Platform: centos7
Compiler: gcc9


The way to proceed is explained in the thread you are pointing to:

{

TFile f("hsimple.root","READ");

c1 = new TCanvas("c1","c1",200,10,700,500);
c2 = new TCanvas("c2","c2",200,300,700,500);

hpx->SetTitle("#alpha");
c1->cd(); hpx->Draw();
c2->cd(); hpx->Draw();

c1->Print("h1.pdf[");
c2->Print("h1.pdf", "Title: Page 1");
c2->Print("h1.pdf", "Title: Page 2");
c1->Print("h1.pdf", "Title: Page 3");
c1->Print("h1.pdf]");

}

Annd that’s what you notebook is doing. According to the file you sent it seems the execution went well. I am not sure why in the case of a notebooks you end with an empty file. Can you try with the normal ROOT and to see if the problem is due to you macro or to the fact you are running it in a notebooks ?

sorry but i have to say that :

code = """
{
   TCanvas* canvas = new TCanvas("canvas");
   TH1F* histo = new TH1F("histo","test 1",10,0.,10.);
   histo->SetFillColor(2);


   for (int i=0;i<4; i++){
      histo->Fill(2.*i);
      histo->Draw();

        if (i==0) canvas->Print("plots.pdf(",Form("bin %d",i));
        else if (i==3) canvas->Print("plots.pdf)",Form("bin %d",i));
        else canvas->Print("plots.pdf",Form("bin %d",i));
   };
}
"""


r.gROOT.ProcessLine(code)

and running the following as a root macro

void t()
{
   TCanvas* canvas = new TCanvas("canvas");
   TH1F* histo = new TH1F("histo","test 1",10,0.,10.);
   histo->SetFillColor(2);


   for (int i=0;i<4; i++){
      histo->Fill(2.*i);
      histo->Draw();

        if (i==0) canvas->Print("plots.pdf(",Form("bin %d",i));
        else if (i==3) canvas->Print("plots.pdf)",Form("bin %d",i));
        else canvas->Print("plots.pdf",Form("bin %d",i));
   };
}

didnt work for me (and it was taken and modified straight from the example on TPdf)

The correct syntax for the 2nd parameter is:

void t() {
   TCanvas* canvas = new TCanvas("canvas");
   TH1F* histo = new TH1F("histo","test 1",10,0.,10.);
   histo->SetFillColor(2);
   for (int i=0;i<4; i++){
      histo->Fill(2.*i);
      histo->Draw();
      if (i==0) canvas->Print("plots.pdf(",Form("Title: bin %d",i));
      else if (i==3) canvas->Print("plots.pdf)",Form("Title: bin %d",i));
      else canvas->Print("plots.pdf",Form("Title: bin %d",i));
   }
}

yes it worked. (I once thougth that the Title: is an arbitrary input)

by the way, is there any way to reduce the size of the output file ?

It is not arbitrary. See the doc.

No

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.