TFile->close and TTree


I have a general question about ROOT file I/O. I am developing a data analysis system for use in a muon collider design. My system is based around a menu driven GUI.

I am somewhat confused whether I can use a TTree object as a member variable in one of my self defined classes. I am using this member variable in various methods in my class.

In one of my classes I define a member variable, which is a TTree pointer:

class TICOOLout {

TTree *for009data;


In the constructor for TICOOlout, I allocate the TTree:

for009data=new TTree(…

In another member function, I open a TFile, read in data from another file and populate the tree. I then write the TFile and close it.

In another member function, I call draw on my ttree. I noticed that this works sometimes. However, if my TTree is large, the program crashes with a bus error.

I noticed that if I didn’t close the TFile, everything works ok.

My question is this: can a ttree exist independently as a member function of a class. Or, is the TTree destructed when I closed the TFile?

What is the best way for performance to deal with large objects in a TFile when I need them? Do I need to open a TFile and read the TTree each time i want to access it. Or, is there a way I can use a TTree as a member variable in my classes and have that be used independently?

Thank you.


When closing the file, The TTree object is automatically deleted becausr it does not make much sense to continue to use this object when its data buffers are on the closed file.
If you want to keep the Ttree object, you should do
before closing the file.


Thanks Rene,

In follow up, I had a few questions. What is the best way to get the best performance using Root? My program will be using the data in the TTree stored in the TFile over and over for various calculations as the user interacts with my program. I would like to provide the best performance without having to reload the data unnecessarily.

It seems there are at least 3 approaches:

  1. When I need to redraw the data for a new plot based upon user input, I open the TFile again and reload the TTree? This seems inefficient. Is the object somehow cached in memory even when I close the TFile?

  2. Keep a TFile open indefinitely until the program is terminated while the user works with the data? Is there any danger in doing this?

3)Using the approach you provided in your last reply.

  1. Also, can you please explain how the TTree object destructor is called when the TFile->Close method is called? I don’t fully understand how this happens and where the linkage between TFiles and objects stored in the files occurs in root.

Can you say anything about the pros and cons of these 3 possibilities?

Thank you.

Best Regards,


The TFile keeps trace of objects like TH1, TTree. When closing/deleting the file
these objects are destroyed too (please read chapter about Object Ownership).
In your case, you should keep the file open as long as you use it, read the TTree
header only once, otherwise you destroy the caching mechanism.
You can do mytree->SetDirectory(0) before closing the file if you want to do
yourself the management in your class.