# Setting kEntriesReshuffled when using RDatasetSpec

**URL:** <https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735>\
**Category:** ROOT\
**Tags:** rdataframe, python, tree\
**Created:** [March 4, 2026, 8:12am UTC](https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735 "2026-03-04T08:12:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamie.gooding](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jamie.gooding/32/39795_2.png) [@jamie.gooding](https://root-forum.cern.ch/u/jamie.gooding)\
**Post date:** [March 4, 2026, 8:12am UTC](https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735/1 "2026-03-04T08:12:19Z")

</div>

* * *

Hi all,

I’m trying to use `RDatasetSpec` to create an `RDataFrame` from a main tree and a friend tree in PyROOT. Since I’m passing the RDF between different python classes/functions, this seems to be the only way to do this safely w.r.t the python garbage collector (please correct me if I’m wrong!).

When I do this I get the warning

```auto
Error in <AddFriend>: Tree 'DecayTree' has the kEntriesReshuffled bit set and cannot have friends nor can be added as a friend unless the main tree has a TTreeIndex on the friend tree 'FriendTree'. You can also unset the bit manually if you know what you are doing; note that you risk associating wrong TTree entries of the friend with those of the main TTree!

```

If I was loading these with `TTree`/`TChain` I would just reset the bit as in this case it is safe to do so (the events in the friend line up with those of the main tree), but with `RDatasetSpec` and `RSample` it’s not clear if there is a way to do this?

My current configuration is effectively the same as in the examples in the documentation:

```python
import ROOT as R

path = "file.root"
tree = "DecayTree"

friend_path = "friend_file.root"
friend_tree = "FriendTree"

sample = R.RDF.Experimental.RSample(tree, tree, path)
dataspec = R.RDF.Experimental.RDatasetSpec()
dataspec.AddSample(sample)
dataspec.WithGlobalFriends(friend_tree, friend_path)

```

_ROOT Version:_ 6.30/04  
_Platform:_ Linux  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [March 4, 2026, 10:07am UTC](https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735/2 "2026-03-04T10:07:40Z")

</div>

Hello Jamie,

> [@jamie.gooding](#):
>
> Since I’m passing the RDF between different python classes/functions, this seems to be the only way to do this safely w.r.t the python garbage collector (please correct me if I’m wrong!).

I’m not following. Where do you suspect the problems? Usually, if you want to ensure that a C++ object built in Python survives, you can keep a reference around somewhere.

As for suppressing the error, RDatasetSpec probably doesn’t have the support for unsetting that bit. If we solve the above, it might not be necessary to investigate whether this needs to be added, though, so let’s do one iteration of checking whether we can win the race against the garbage collector.

---

<div class="post-metadata">

**Author:** ![jamie.gooding](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jamie.gooding/32/39795_2.png) [@jamie.gooding](https://root-forum.cern.ch/u/jamie.gooding)\
**Post date:** [March 4, 2026, 2:48pm UTC](https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735/3 "2026-03-04T14:48:59Z")

</div>

Hi Stephan,

This is all within a python module, which has a submodule `io` which contains the function `configure_rdf`, which to simplify for a minimal example looks like this:

```python
import ROOT as R

def configure_rdf(path, tree_name, friend_path, friend_tree_name, cuts):
    _file = R.TFile(path)
    _tree = _file.Get(tree_name)
    tree.ResetBit(R.TTree.kEntriesReshuffled)

    _friend_file = R.TFile(friend_path)
    _friend_tree = _friend_file.Get(friend_tree_name)
    _tree.AddFriend(_friend_tree)

    rdf = R.RDataFrame(tree)
    for cut in cuts: # cuts is a list of any length including 0
        rdf = rdf.Filter(cut) 

    return rdf 

```

Then in another submodule I define a class which uses this function

```python
from .io import configure_rdf

class example:
    def __init__ (self, path, tree_name, friend_path, friend_tree_name, cuts, *args, **kwargs):
        # ... 
        self.rdf = configure_rdf(path, tree_name, friend_path, friend_tree_name, cuts)

        dataset = self.rdf.AsNumpy(["branch"]) # Seems to crash here, also for other methods such as `GetColumnNames()
        # ...

```

Everything is definitely in scope just before the `return` of the function and seems that something goes out of scope after.

I also tried attaching the trees to `rdf` as attributes `rdf._tree` and `rdf._friend_tree` but this didn’t solve things.

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [March 4, 2026, 3:26pm UTC](https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735/4 "2026-03-04T15:26:00Z")

</div>

> [@jamie.gooding](#):
>
> I also tried attaching the trees to `rdf` as attributes `rdf._tree` and `rdf._friend_tree` but this didn’t solve things.

This is what I would have recommended. Can we have another look as to why the garbage collector seems to have intervened?

---

<div class="post-metadata">

**Author:** ![jamie.gooding](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jamie.gooding/32/39795_2.png) [@jamie.gooding](https://root-forum.cern.ch/u/jamie.gooding)\
**Post date:** [March 11, 2026, 12:23pm UTC](https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735/5 "2026-03-11T12:23:09Z")

</div>

Hi Stephan,

I’m trying the following (I added the `.SetDirectory(0)` and this has at least prevented the segfaults):

```python
def configure_rdf(path, tree_name, friend_path, friend_tree_name, cuts):
    _file = R.TFile(path)
    _tree = _file.Get(tree)
    _tree.SetDirectory(0)
    _tree.ResetBit(R.TTree.kEntriesReshuffled)

    _friend_file = R.TFile(friend_path)
    _friend_tree = _friend_file.Get(friend_tree)
    _friend_tree.SetDirectory(0)
    _tree.AddFriend(_friend_tree)

    rdf = R.RDataFrame(_tree)
    rdf._tree = _tree
    rdf._friend_tree = _friend_tree

    rdf = R.RDataFrame(tree)
    for cut in cuts: # cuts is a list of any length including 0
        rdf = rdf.Filter(cut) 

    return rdf 

```

I am now met with a new bug, where when I try to run any operation (a simple `.Count()` for example):

```auto
    self.rdf.Count().GetValue()
cppyy.gbl.std.out_of_range: const ULong64_t& ROOT::RDF::RResultPtr<ULong64_t>::GetValue() =>
    out_of_range: RDataFrame: Filter could not retrieve value for column 'B_DTF_Jpsi_MASS' for entry 0. You can use the DefaultValueFor operation to provide a default value, or FilterAvailable/FilterMissing to discard/keep entries with missing values instead.

```

Which is particularly odd as I print the columns in `rdf` immediately before and `B_DTF_Jpsi_MASS` is definitely listed. For additional info, I am running with `ROOT.DisableImplicitMT()` called explicitly at the start of my script using the `example` class

---

<div class="post-metadata">

**Author:** ![jamie.gooding](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jamie.gooding/32/39795_2.png) [@jamie.gooding](https://root-forum.cern.ch/u/jamie.gooding)\
**Post date:** [March 11, 2026, 12:33pm UTC](https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735/6 "2026-03-11T12:33:51Z")

</div>

Ah I may have solved this now, the `.SetDirectory(0)` seemed to cause the issue above. I now have the following, which persists `_file` and `_friend_file`

```python
def configure_rdf(path, tree_name, friend_path, friend_tree_name, cuts):
    _file = R.TFile(path)
    _tree = _file.Get(tree)
    _tree.ResetBit(R.TTree.kEntriesReshuffled)

    _friend_file = R.TFile(friend_path)
    _friend_tree = _friend_file.Get(friend_tree)
    _tree.AddFriend(_friend_tree)

    rdf = R.RDataFrame(_tree)
    rdf._file = _file
    rdf._tree = _tree
    rdf._friend_file = _friend_file
    rdf._friend_tree = _friend_tree

    rdf = R.RDataFrame(tree)
    for cut in cuts: # cuts is a list of any length including 0
        rdf = rdf.Filter(cut) 

```

With then importantly that these need to be also assigned as attributes of `examples`, as the modifications of `rdf` with `Filter` and `Define` drop these attributes at some point:

```python
class example:
    def __init__ (self, path, tree_name, friend_path, friend_tree_name, cuts, *args, **kwargs):
        # ... 
        self.rdf = configure_rdf(path, tree_name, friend_path, friend_tree_name, cuts)
        self._file = self.rdf._file
        self._tree = self.rdf._tree
        self._friend_file = self.rdf._friend_file
        self._friend_tree = self.rdf._friend_tree

        dataset = self.rdf.AsNumpy(["branch"]) # Seems to crash here, also for other methods such as `GetColumnNames()
        # ...

```

Which now works as everything is in scope 🎉

---

<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:** [March 11, 2026, 2:45pm UTC](https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735/7 "2026-03-11T14:45:27Z")

</div>

> [@jamie.gooding](#):
>
> ```auto
> _tree = _file.Get(tree)
> _tree.SetDirectory(0)
> 
> ```

Means that the `TTree` can no longer find its data … (i.e. it is now ‘useless’).

---

<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:** [March 11, 2026, 2:47pm UTC](https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735/8 "2026-03-11T14:47:20Z")

</div>

```auto
    return rdf 

```

As you noted, at least the `TFile` lifetime needs to be extended/ensured to be the same as the `rdf`.

---

<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:** [March 25, 2026, 2:47pm UTC](https://root-forum.cern.ch/t/setting-kentriesreshuffled-when-using-rdatasetspec/64735/9 "2026-03-25T14:47:31Z")

</div>

Closed on March 25, 2026, 2:47pm UTC

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