Thanks for the quick answer! It does solve my problem but I’m not sure why.
@pcanal I knew about object ownership but I don’t understand what is wrong here. The TTree is created before opening any file. It should not be owned by the first or second file.
and that ‘without’ the TTree is associated with the current directory at the time creation (In your example this is gROOT itself). Internally the TTree code behaves differently whether or not its attached to a TDirectory (and ‘forget’ to check whether this directory is a file or just an in-memory directory like gROOT). Because of that, in the case where the TTree is attached to gROOT after the first call to Write it deletes from memory the TBaskets (ie. the data which has now been copied to the disk) and thus the 2nd Write does not do anything (and wrongly remembers the location of the baskets but in the wrong file).