# Friend support within RDataFrame class

**URL:** <https://root-forum.cern.ch/t/friend-support-within-rdataframe-class/40048>\
**Category:** ROOT\
**Tags:** rdataframe, feature\
**Created:** [June 23, 2020, 3:16pm UTC](https://root-forum.cern.ch/t/friend-support-within-rdataframe-class/40048 "2020-06-23T15:16:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [June 23, 2020, 3:16pm UTC](https://root-forum.cern.ch/t/friend-support-within-rdataframe-class/40048/1 "2020-06-23T15:16:07Z")

</div>

It would be nice to be able to add an `RDataFrame` as a friend to another `RDataFrame` instead of having to use `TTree` first to handle friends. Usage example follows.

* * *

Suppose I apply a selection `fitSel`, do a fit, and extract some weights, which I save as a `TTree`. Further suppose I want to compare the unweighted dataset before the selection to the weighted one after the selection.

Right now, I have to do:

```python
rdf_initial = ROOT.RDataFrame('baseTree', 'base.root')
rdf_filtered = rdf_initial.Filter(fitSel)
rdf_filtered.Snapshot('temp', 'temp.root')
f1 = ROOT.TFile.Open('temp.root')
t1 = f.Get('temp')
f2 = ROOT.TFile.Open('weights.root')
t2 = f.Get('weightTree')
t1.AddFriend(t2)
rdf_filtered = ROOT.RDataFrame(t1)
# do some analysis
os.remove('temp.root')

```

I would like to do:

```python
rdf_initial = ROOT.RDataFrame('baseTree', 'base.root')
rdf_filtered = rdf_initial.Filter(fitSel)
rdf_weights = ROOT.RDataFrame('weightTree', 'weights.root')
rdf_filtered.AddFriend(rdf_weights)
# do some analysis

```

10 lines become 4 lines, and I don’t have to create a temporary file on disk.

* * *

_ROOT Version:_ 6.22/00  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 23, 2020, 3:29pm UTC](https://root-forum.cern.ch/t/friend-support-within-rdataframe-class/40048/2 "2020-06-23T15:29:12Z")

</div>

Hi @mwilkins,  
I totally agree it would be nice to have this, please open a jira ticket with the feature request.  
Note however that there is a reason this is not already there: it can’t just be syntactic sugar for TTree-friendliness, it also needs to work for custom `RDataSource`s, RDFs with no dataset, multi-thread runs, and it requires adding a notion of generic “RDF name” so you can refer to columns of the friends 😃

All doable, just needs some thinking and subsequent coding.  
Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![mwilkins](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mwilkins/32/28332_2.png) [@mwilkins](https://root-forum.cern.ch/u/mwilkins)\
**Post date:** [June 23, 2020, 3:55pm UTC](https://root-forum.cern.ch/t/friend-support-within-rdataframe-class/40048/3 "2020-06-23T15:55:54Z")

</div>

[Done.](https://sft.its.cern.ch/jira/browse/ROOT-10874) Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [July 7, 2020, 4:07pm UTC](https://root-forum.cern.ch/t/friend-support-within-rdataframe-class/40048/4 "2020-07-07T16:07:43Z")

</div>

Closed on July 7, 2020, 4:07pm UTC

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