# TTree::AddFriend() from TDirectory?

**URL:** <https://root-forum.cern.ch/t/ttree-addfriend-from-tdirectory/2604>\
**Category:** ROOT\
**Created:** [November 6, 2005, 10:10pm UTC](https://root-forum.cern.ch/t/ttree-addfriend-from-tdirectory/2604 "2005-11-06T22:10:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstrato](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cstrato](https://root-forum.cern.ch/u/cstrato)\
**Post date:** [November 6, 2005, 10:10pm UTC](https://root-forum.cern.ch/t/ttree-addfriend-from-tdirectory/2604/1 "2005-11-06T22:10:44Z")

</div>

Dear Rooters

I have root files where my trees are stored in different subdirectories.

Is it possible to add the trees from different files and different subdirectories as friends to one tree?

Thank you in advance  
Best regards  
Christian

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 7, 2005, 8:31pm UTC](https://root-forum.cern.ch/t/ttree-addfriend-from-tdirectory/2604/2 "2005-11-07T20:31:14Z")

</div>

[quote]Is it possible to add the trees from different files and different subdirectories as friends to one tree?[/quote]Yes. Just specify the proper location of the file when calling AddFriend.

Cheers,  
Philippe

---

<div class="post-metadata">

**Author:** ![cstrato](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cstrato](https://root-forum.cern.ch/u/cstrato)\
**Post date:** [November 7, 2005, 9:32pm UTC](https://root-forum.cern.ch/t/ttree-addfriend-from-tdirectory/2604/3 "2005-11-07T21:32:06Z")

</div>

Dear Philippe

I have a method where everything worked when I stored the trees in the root files.  
Now I have decided to store the trees in subdirectories and get the error:  
Warning in TTree::AddFriend: Cannot add FriendElement Test3-2=Test3-2.cel in file /Users/cs/ROOT/rootcode/xpstest/Test4\_cel.root

It seems that storing the proper location of TFile is not sufficient.  
The comments to Tree::AddFriend() states:  
// This function:  
// -opens a file if filename is specified  
// -reads a Tree with name treename from the file (current directory)  
// -adds the Tree to the list of friends

Does this mean that I have to open each TFile first, then call file-\>cd(directory)  
before calling tree-\>AddFriend()?

Best regards  
Christian

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 7, 2005, 9:37pm UTC](https://root-forum.cern.ch/t/ttree-addfriend-from-tdirectory/2604/4 "2005-11-07T21:37:59Z")

</div>

[quote]Does this mean that I have to open each TFile first, then call file-\>cd(directory) before calling tree-\>AddFriend()?  
[/quote]Now I am a little confused 🙂 When you say ‘subdirectory’ are you talking about a file-system sub-directory or a ROOT sub-(T)directory inside a TFile?  
For the first one`T->AddFriend("name","directory/filename.root");`works. For the later`T->AddFriend("dir/name","filename.root");`might work.

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![cstrato](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@cstrato](https://root-forum.cern.ch/u/cstrato)\
**Post date:** [November 7, 2005, 10:06pm UTC](https://root-forum.cern.ch/t/ttree-addfriend-from-tdirectory/2604/5 "2005-11-07T22:06:27Z")

</div>

Dear Philippe

Thank you very much, T-\>AddFriend(“dir/name”,“filename.root”) is the solution.

Best regards  
Christian
