How to detach a cloned TTree from the original

Dear ROOT expert,

In my code, I’m cloning a TTree into a second tree. Then the original TTree is deleted. How can I keep the correct branch addresses after that? Here is a piece of code to illustate my problem:

TTree *T = new TTree(“test”,“test”);
int myvar;
T->Branch(“myvar”, &myvar, “myvar/I”);
myvar=0; T->Fill();
myvar=1; T->Fill();
myvar=2; T->Fill();

TTree *T2 = T->CloneTree();
delete T;

for(int t=0; t<3; t++){

This prints “2,2,2” and I want it to print “0,1,2”.
Thank you.

Right before the “for” loop, try to add:
T2->SetBranchAddress(“myvar”, &myvar);