Save a TGraph in a root file

Dear Experts,

I have a root file with leaves and I am trying to draw a TGraph using two variables (x & y) which are present in the root file. I can make the TGraph directly on the ubuntu terminal using the command line:
treename->Draw->(x:y). However, I am not able to store simply (I cam store the canvas in the root file) in a root file just like we save 2D histograms and it also takes the ranges by default.

I want to write a macro to make a TGraph using x and y from the root file, set the ranges according to my requirement and finally save it in a root file.

It will be great if you help me sort it out. The macro that I am using is shown below.


TFile *f = new TFile(“proc11_bucket_9to12.root”);
TTree tree = (TTree)f->Get(“DstD0PiKSKS”);
TCanvas *c1 = new TCanvas(“c1”,“Canvas_1”,399,135,538,325);
//TGraph *g = new TGraph();

TFile * f1 = new TFile(“tgraph_data.root”,“RECREATE”);

Please read tips for efficient and successful posting and posting code

_ROOT Version:5.34

Hi Sanjeeda,

In your example code I don’t see any ranges being set.

TGraph doesn’t have ranges itself; that’s a property of the “synthetic” histogram that the TCanvas produces to provide axes (and ranges). So storing the TCanvas is a good approach. You can find the TGraph in the TCanvas using c1->GetListOfPrimitives(): it’s in there!

Cheers, Axel.