Adding entries to a TTree

we are trying to add new entries to an existing tree having just two branches.
Our attempt is the following and basically it works, but with a great redundancy. In facts the rootfile we get has many trees inside, one for each entry we added. The first tree contains only the first of the new entries, the second tree contains the first of the newentries and the second, and so on …
So at the end if we add N entries we get N tress each containing one more entry than the previous… this seems a waste of space and maybe time when we want to read data from the file.

Is this the way data are supposed to be stored? how can we change this and have only one tree containing all the entries?

Thanks a lot for reading.

	TFile *f;
       f = TFile::Open("rootfile.root", "update");

				TTree *t = (TTree*) f->Get("tvec");

				t->SetBranchAddress("eventnumber", &eventnum);
				std::vector<std::vector<int> > *vpx = 0;
				vpx = &cluster;
				t->SetBranchAddress("clusters", &vpx);



				delete f;

Replace the line

f->Write(); by

for more info, see the doc of TObject::Write


Thank you very much for replying, that option was really unknown to us an with it now the program correctly writes ROOT files.
This is what we wrote exactly.

delete f;

The only thing now is that after adding some number of entries (around 1000) we get an error about the max number of files that can be opened. Actually we work only on a single file into which we add all the new entries and using the Close() method this error seems really weired. Is there something more than Close and delete to unload the file?

With the code shown above the error we get is

SysError in <TFile::TFile>: file file.root can not be opened (Too many open files)

Thanks again

[quote]Is there something more than Close and delete to unload the file? [/quote]No, it should have been enough! Can you provide a complete running example reproducing the problem?

Also, I wonder if it would not be better to re-use the same TFile object (i.e. not open/close the same file for each entry) … doing so seems very un-efficient (in particular the part of writing the TTree header for each entry).


Hello, thanks a lot for the suggestion. Declaring the TFile only once and passing it around to the various functions made the job.