Saving Canvas to pdf ruins graphics

Dear All,

I am trying to save a 2D histogram to a .pdf file

 TCanvas *c1 = new TCanvas("c1","c1");
 c1->cd();
 TH2F *h   = new TH2F("h","M distribution", 100, 1000, 1050, 50, 0, 10);
 h->Draw("CONT4Z");
 c1->SaveAs("ntracks_vs_M.pdf");
 c1->SaveAs("ntracks_vs_M.eps");

However, when I open in using evince or macOS preview, the graphics are ruined, so that smooth surfaces appear pixelated.

Can anyone suggest a workaround?

Thanks,

Blaise

I am on Mac also. And I get a nice output of your df file using preview. May be you have turned off the smoothing in the preview preferences ?

Hello,

You’re right, it seems to depend on the reader. Preview, for some reason, does not present me with the option of smoothing in the preferences. However, using Adobe Reader all seems to work well. Thank you.

Do you happen to know how this behaviour affects generating a .pdf file in LaTeX? ( I use TexShop).

Edit: I have a similar problem in producing colour-filled histograms:

    TCanvas *c1 = new TCanvas("c1","c1");
    c1->cd();
    TH1F *h   = new TH1F("h","M distribution", 200, 4000, 5000);
    
    gStyle->SetOptStat(0);
    
    h->SetFillStyle( 3000);
    h->SetFillColor( kBlue-10);

    h->SetLineColor(kBlue);
    h->SetLineWidth(1);
  

When opening the file in any reader, the colour-filling disappears. How to prevent this?

Thanks,

Blaise

The pdf is generated by ROOT and Latex does not modify it. PDF is a vectorial data format. So there is no pixel stored in the file… you can enlarge as much a you want it will always been nicely displayed.

1 Like

Your latest script does not produce any graphics … can you provide an example showing the problem ?

Hi, sorry, here is the entire macro

    TFile *fin = new TFile("4CEPconstrainedphi.root");
    TTree *t1 = (TTree*)fin->Get("t1");
    
     
    
    Float_t M;
    t1->SetBranchAddress("M", &M);

    
    TCanvas *c1 = new TCanvas("c1","c1");
    c1->cd();
    TH1F *h   = new TH1F("h","M distribution", 200, 4000, 5000);
    
    gStyle->SetOptStat(0);
    
    h->SetFillStyle( 3000);
    h->SetFillColor( kBlue-10);
   
    h->SetLineColor(kBlue);
    h->SetLineWidth(1);
   
    
    for (Long64_t i=0;i<nentries;i++) {
      t1->GetEntry(i);
      h->Fill(M);
    }
    h->DrawClone(); //tried this as an alternative to the Draw() method

    c1->SaveAs("tetra_CEP_PID2.pdf");
}

Cheers,

Blaise

remove the line

h->SetFillStyle( 3000);

that will not change the screen output and the pdf will be fine.

1 Like

Great, Thank You.

All seems to work fine now.

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