Writing trees to different files

Hi,

I’m trying write a program that creates two different trees and writes each to a different file.
The program is running fine, but if I then open the two files I’ve written with root only the second one contains a tree, the first one is empty!

I’ve added a small example code for this problem. Running the code gives two files, file1.root and file2.root of which only file2.root contains a tree.

Can anybody help me with this problem? Thanks in advance!
test.cxx (727 Bytes)

I’ve found the error, TTree::SetDirectory doesn’t seem to be sufficient (if not to say useless?), it’s necessary do use a TFile::cd() instead before writing the trees.

Hi,

TObject::Write semantically is explicitly to store the current object in the current TFile!
Instead you should use TTree::AutoSave or call Write on the TFile object:

cout<<nofb1<<": "<<tr1->AutoSave()<<endl; cout<<nofb2<<": "<<tr2->AutoSave()<<endl;or cout<<nofb1<<": "<<file1->Write()<<endl; cout<<nofb2<<": "<<file2->Write()<<endl;Cheers,
Philippe.