Write the same memory-resident TTree on many files

Dear all,

in my program I have a memory-resident TTree, created before any TFile object; in this particular application I do not expect to have any memory issue and things appera to run smoothly.

The problems begin when I want to save my acquisition. What I do is:

TFile *output_file = new TFile(fileName,"RECREATE"); output_file->cd(); datatree->Write(); datatree->SetDirectory(0); output_file->Close();
When I do this the first time after the TTree has been filled, everything goes fine and the file is OK.
If I do this one more time, this new file is corrupted and the data unreadable. I can fill the TTree with a new acquisition and then save it, but only on one single file.

What am I missing?