Save multiple TCanvas into the same root file

Hi,
how can I save multiple canvases into the same root file?
I can’t find a function for that. I use SaveAs, but it just overwrites the file.

h1->SaveAs(“file.root”);
h2->SaveAs(“file.root”);

(h1 and h2 are TCanvas*)

Thank you

Create/open the file before creating the canvases, then write them with Write (make sure the canvases have different names, not just different pointer/variable names); check the User’s guide for the options.

TFile *f = new TFile("file.root", "RECREATE");  // recreate deletes the file if it already exists
// create and do stuff...

f->cd();   // make sure they will be written to f
h1->Write();  // you can specify here the name of the canvas inside the file, e.g. h1->Write("canvas1")...
h2->Write();
f->Close();
// ...

Thank you, it worked :slight_smile:

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