TChain::AddFriend

Dear all,
The TChain will add friends for three different branches or one branch with same name “tree”? Very confused, thanks!

ch = TChain("DecayTree")
ch.Add("file1.root")
ch.Add("file2.root")
ch.Add("file3.root")

ch.AddFriend("tree", "file1's friend.root")
ch.AddFriend("tree", "file2's friend.root")
ch.AddFriend("tree", "file3's friend.root")

Hi @realTay_John ,

AddFriend (and “TTree friendship” in general) works at the level of full datasets, not at the level of individual branches/columns. Adding tree1 as a friend of tree2 basically performs a horizontal concatenation of the two datasets, like putting the two “tables” side by side to make up a wider table.

With TChains, you probably want to construct a second friend TChain and then add that as a friend of the main TChain.

Cheers,
Enrico

1 Like

What if I add three friends with same tree name like what I have put above.
I am confused the three friend with same name “tree” will be horizontal concatenation or vertical.
or more intuitive, the position of added three friends with same name will located ft1.v1 ft2.v2, ft3.v3 or they all added as ft1.v1

image

Add(tree) concatenates the tree vertically into a chain, AddFriend(tree) concatenates the tree (or chain) horizontally with the main chain.

That just adds 3 friends (with the same name), so each will be horizontally concatenated with the others. If you want the 3 friend trees to make up a single large friend chain, you should first construct the chain of friends (which is a vertical concatenation) and then add that chain as a friend of the original chain.

1 Like

Got it! Thanks for your reply! :partying_face: @eguiraud

But how the entries in friend tree match with the tchain tree’s entries?
I mean, if tchain tree has entries with order 1,2,3.
How root know the friend tree’s entries 1’,2’,3’ corresponding entries 1,2,3.

It’s just a horizontal concatenation. The event loop will go through the entries in order.

If you need to build a look-up table to pick the right entries in the friend tree/chain depending on which entry is loaded for the main tree, you can use a TTreeIndex (you can search the forum/docs for more information), but that random access into friend entries adds a significant performance overhead w.r.t. just looping through.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.