Obtain data from a TGraph saved in a ROOT file


Maybe my problem is very basics, but I have had many problems with this code. Some times ago, I write a code like this:

TCanvas *c1 = newTcanvas(); TGraph *gr[5]; for(i=0;i<5;i++){ gr[i] = new TGraph(64,x,y); // with x and y array of 64 c1->Uptade(); c1->Print("file.root","root"); }

Well, now I need to recover the data in the arrays x and y, the 10 different arrays (x and y by 5). I was tried with:

TFile *file = new TFile("file.root"); TCanvas *c1 = (TCanvas*) file->Get(file->GetListOfKeys()->Last()->GetName);

But later, I don’t know what I must do for recovery the data. I was tried too with:

TFile *file = new TFile("file.root"); TGraph *gr = (TGraph*) file->Get(file->GetListOfKeys()->Last()->GetName); gr->GetPoint(0,x,y);

But sometimes that give me segmentation fault or only 0.00 in x and y. If someboy could help me, I will be very grateful.

I believe you must give the TGraph object a name i.e.

TFile* f0 = new TFile(“theFile.root”,“RECREATE”);

TGraph* graph = new TGraph();

Now fill the graph with your data…


Now when you want to get data from a graph do the following

TFile* f0 = new TFile(“theFile.root”);

TGraph* graph = (TGraph*)f0->Get(“theGraph”);



Hope this helps.

Yes, it’s true, Now I know this, but at the time that I save the data, yet I did know.

I resolved this with file->Get(file->GetLisOfKeys()->Last()->GetName()). My problem is, in more specific words (I think), to get the data in the next position of that pointed by the element saved.

In other way, I save “gr”, that is a pointer to an array of TGraph [2], for example. I think (I don’t know how it work) that gr is saved, but when I try to get the element, I can only to get the first element of the array, I don’t know how to get the second “TGraph” element in the array pointed by gr.

If I use “Draw()” I can see all the curves corresponding to each of the elements in the array. Only I need to obtain this points. (i.e gr[i]->GetPoint(j,xaxis,yaxis))