Adding entries to a TTree

Hello
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.
Roberto

	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);

				t->Fill();

				f->Write();

				delete f;

Replace the line

f->Write(); by

t->Write(TObject::kWriteDelete);
for more info, see the doc of TObject::Write

Rene

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.

t->Write(0,TObject::kWriteDelete,0);
f->Close("R");
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
Roberto

[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).

Cheers,
Philippe.

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