Return values from TTree::SetBranchAddress

Try one of: if(MyTree->GetListOfBranches()->FindObject("SomeBranchName")) { MyTree->SetBranchAddress("SomeBranchName", &SomeVariable); } if(MyTree->GetBranch("SomeBranchName")) { MyTree->SetBranchAddress("SomeBranchName", &SomeVabiable); }
Note: the fact that “SetBranchAddress” (almost) always returns 0 is actually a bug/problem in ROOT. It has been fixed in the ROOT “trunk” and in the “v5-34-00-patches” branch, in revision 46373, as of 2012-10-06.
Hint: In case you use a TChain, before you try to “MyChain->SetBranchAddress(…);”, you need to make sure that the tree has been loaded -> execute, for example, “MyChain->LoadTree(0);” -> otherwise the “TBranch::SetBranchAddress” will always return 5 (kNoCheck = underlying TBranch not yet available so no check was made).

BTW: you MUST NOT “delete out_tree;” after you execute “out_file->Close();” (your “out_tree” is automatically deleted when you close your “out_file”) and you should execute “tree->ResetBranchAddresses();” before you “return;” from your “test()” function. See, for example, [url]GetEntry() crash in second using