Saving a TGraph in a root file


I am running the following script

Where I am trying to get the contour 2 from a TH2F.

I would like to have the TGraph saved in the root file in order to be able to use it afterwards combined with other TGraphs to do a Multigraph.

For some reason I am not able to save the object as a TGraph. It saved it as a Canvas.

How should I save it?



Why do you think it’s a canvas written? It’s a TGraph (while you code is invalid, CINT still can understand it).

[quote]root [0] TFile f(“rootcontour.root”, “read”)
root [1] TObject * o = f.Get(“prueba”)
root [2] o->IsA()->GetName()
(const char* 0x7f92ac79e209)“TGraph”[/quote]

The fundamental problem here is that some objects (TH* and TTree for example) are automatically added to an open-writeable file when created. So the user doesn’t have to think about “saving” histograms, they just create a TFile, create some TH1s, then close the file and it works. TGraphs on the other hand are not automatically added for some reason. This is very obvious to ROOT developers and experts but is not very clearly explained anywhere. Thus when exports answer questions about saving TGraphs, they answer the wrong question. If you search ROOTTalk for “Save TGraph to file” you will find dozens of examples.

There is no information easily found on the ROOT website about how to actually save a TGraph object in a TFile. I searched for the word “save” or “saving” in the ROOT manual chapter about TGraphs and there is nothing. There is also nothing in the TGraph doc page about saving the TGraph in a TFile.

The developers need to add some useful instructions to the TGraph doc page and the TGraph manual chapter about how to save a TGraph as a named object in a TFile. Even more useful would be a special method in order to do this in one line.


1 Like

I skipped your long post. The object Lara saved in a file is a TGraph. I do not know if it’s difficult for you to do or not (obj->Write():wink: - but Lara was able to write a TGraph object into a file, and there is no reason to confuse Lara with other irrelevant problems here.


This statement is simply wrong and misleading. TFile can become an owner for some objects (the ownership policy is well described in the ROOT’s User’s guide), but it NEVER writes your histogram unless you call Write. Try not to misinform people.

I would recommend you (re)read this- … Output.pdf it explains all what you need.

For example:

1.2.4 Saving Histograms to Disk
1.2.6 Saving Objects to Disk.
1.2.7 Saving Collections to Disk
1.2.8 A TFile Object Going Out of Scope.

I admit my understanding of WHY TGraphs and histograms behave differently is likely flawed, but the fact remains: they behave differently. This:

TFile f("myfile.root","RECREATE");
TH1F h("h","h",10,0,10);

does something different than this:

TFile f("myfile.root","RECREATE");
TGraph g(10);

And yes, in my above post I forgot about Write().

These issues also remain:

  • Users frequently need to ask how to save TGraphs
  • The TGraph doc page and manual chapter do not describe how to do this

If I am having a problem saving a TGraph to a file, I am going to look in the TGraph manual section and doc page. I am not going to skim 20 pages of “The physical layout of a TFile”. My complaint is that the relevant manual section is difficult to find, as it’s not the place people tend to look.

I think it’s worth including a note in the TGraph manual chapter and doc page. “Users should know that TGraphs are not and thus need to be saved explicitly with .” Users have no reason to expect TGraphs to behave so differently from histograms, so they should be warned.


You do not have to read about TFIle’s structure, you just have to read about object ownership/read/write operations and special treatment of TH objects and TTrees. I spent a couple of seconds of my invaluable life finding the chapters in the manual for you, I see now, it was a mistake since instead of reading you continue complaining. All the differences (what is written/not when you call TFile::Write) - this is all described in the chapters I’ve mentioned.

There is nothing special about TGraphs - you save them as any ROOT object. Why is TGraph such a trouble for you - this is something beyond my understanding. Is TH objects the only thing you ever saved in a file?
Do you think, we need a special chapter in a guide on how to save, for example, TList with histograms? And another one on how to save a TClonesArray with histograms? Next - TClonesArray with TGraphs? And etc. etc.?

Anyway, do not spend your time replying me, better read a couple of pages in the manual. If it’s not good for you, then let’s hope somebody will write a special book for you on how to save a TGraph.

Did the OP found the solution to Saving TGraphs?

This is noted on the documentation for TGraphs.

Note:* Unlike histogram or tree (or even TGraph2D), TGraph objects are not automatically attached to the current TFile, in order to keep the management and size of the TGraph has small as possible.

This makes it sound like you can force the TGraph to be saved but TGraph::Write() doesn’t do the trick either. The way I found to get around this is to Draw the TGraph on a Canvas and grab the TCanvas Primivtives afterward which is a really ridiculous way to do things.

No idea what you’re talking about: { // open a new ROOT file TFile *f = TFile::Open("MyFile.root", "RECREATE"); // create the graph (in memory) TGraph *g = new TGraph(); for (Int_t i = 0; i < 100; i++) g->SetPoint(g->GetN(), i*i, i*i*i); f->cd(); // make sure that we will write to this ROOT file g->Write("MyGraph"); // save the graph as "MyGraph" f->ls(); // show the contents of the ROOT file delete f; // close the ROOT file g->Draw("A*"); // draw the graph (which is still in memory) // delete g; // delete the graph (from memory) } { // open the ROOT file with the graph TFile *f = TFile::Open("MyFile.root", "READ"); f->ls(); // show the contents of the ROOT file TGraph *g; f->GetObject("MyGraph", g); // retrieve "MyGraph" delete f; // close the ROOT file if (g) g->Draw("A*"); // draw the graph (from memory) // delete g; // delete the graph (from memory) }