How to retrieve TTree from TFile?


I am trying to retrieve a TTree from a TFile. I have tried using Get, which results in a segmentation violation (the TTree is called “ntp2” in all of the files that I am working with):

TFile * f = new TFile("foo.root");

I want to be able to store the TTree as a class variable for plotting purposes, so that I can use the draw command in my class. Right now, I have a command of the form:

_ntp2 = (TTree*)_File->Get("ntp2");

which generates the error described above. What is the proper way to retrieve the TTree from the TFile?

I am using ROOT 4.04/02b with CINT 5.15.169.

Thank you,

Is the statement "f->Get(“ntp2”) failing? or
_ntp2 = (TTree*)_File->Get(“ntp2”);
Is your _ntp2 member a TTree or TTree* ?


The problem turned out to be with the TFile * declaration. I am able to use the ->Get command to retrieve the TTree if I declare the file as follows:

TFile * _File = new TFile("MyFile.root")
TTree * _ntp2 = (TTree*)_File->Get("ntp2")

However, if I use the Open command, I get a segmentation error,

TFile * f = new TFile;
_ntp2 = (TTree*)_File->Get("ntp2")