# Issues in the conversion of RDataFrame column into a numpy array

**URL:** <https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737>\
**Category:** Newbie\
**Tags:** tree, rdataframe, python\
**Created:** [May 4, 2023, 3:51pm UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737 "2023-05-04T15:51:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![soureek](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/soureek/32/33236_2.png) [@soureek](https://root-forum.cern.ch/u/soureek)\
**Post date:** [May 4, 2023, 3:51pm UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/1 "2023-05-04T15:51:39Z")

</div>

Dear experts,

I am trying to convert a column of a `RDataFrame` object into a numpy array.

The dataFrame is defined on a **memory-resident tree** created via `TTree.MergeTrees(TList)`.

However, the `AsNumpy` function as shown in the tutorial `df026_AsNumpyArrays.py` does not work. I get the following error message.

```auto
    cpp_reference = self._result_ptrs[column].GetValue()
cppyy.gbl.std.runtime_error: const vector<double>& ROOT::RDF::RResultPtr<vector<double> >::GetValue() =>
    runtime_error: The input TTree is not linked to any file, in-memory-only trees are not supported.

```

I am running `ROOTv6.26.07` with `Python 3.9.6` in `el8_amd64_gcc10` environment.

Is there any other method to do this?

Any help will be highly appreciated.

Best,  
Soureek

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [May 4, 2023, 7:30pm UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/2 "2023-05-04T19:30:47Z")

</div>

Can you open a file before calling `TTree.MergeTrees(TList)` so the resulting tree has a `TFile` as “home”?

---

<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:** [May 4, 2023, 11:50pm UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/3 "2023-05-04T23:50:49Z")

</div>

Hi @soureek ,

and welcome to the ROOT forum. In case Axel’s suggestion does not pan out, as a much worse workaround I seem to remember (I hope I remember correctly) that memory-resident trees are not supported _with EnableImplicitMT()_ active. In other words a single-thread run (without `EnableImplicitMT()`) might work.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![soureek](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/soureek/32/33236_2.png) [@soureek](https://root-forum.cern.ch/u/soureek)\
**Post date:** [May 5, 2023, 10:24am UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/4 "2023-05-05T10:24:16Z")

</div>

Hi @eguiraud and @Axel

Both options work for me. Thanks for your suggestions.

> I seem to remember (I hope I remember correctly) that memory-resident trees are not supported _with EnableImplicitMT()_ active.

@eguiraud  
Is there particular reason why this feature is not supported ?  
In my experience, merging small trees on-the-fly is a standard requirement for various studies. One should be able to seamlessly handle them in RDataFrame without deactivating `EnableImplicitMT()` or creating a temporary file to store the merged tree.

Can this feature be added in RDataFrame?

Cheers,  
Soureek

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 5, 2023, 11:47am UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/5 "2023-05-05T11:47:41Z")

</div>

I think, in principle, a [TTree](https://root.cern/doc/master/classTTree.html) is always expected to be “owned” by some file, e.g., either a “physical” [TFile](https://root.cern/doc/master/classTFile.html) (or any [“network” resident variant](https://root.cern/doc/master/classTFile.html#ad8870806a04da2c2f4aa02bee4ec6833)) or a “memory-mapped” [TMemFile](https://root.cern/doc/master/classTMemFile.html) (that’s maybe what you want now).

---

<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:** [May 5, 2023, 2:17pm UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/6 "2023-05-05T14:17:50Z")

</div>

> [@soureek](#):
>
> Is there particular reason why this feature is not supported ?

Different threads cannot work on the same TTree, it’s not supported (the tree would need to “be at different entries” at the same time). So each thread needs to open its own copy of the file and work on its copy of the TTree. And that’s not possible for memory-resident trees.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 5, 2023, 2:23pm UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/7 "2023-05-05T14:23:02Z")

</div>

@eguiraud @pcanal So, you say that [TMemFile](https://root.cern/doc/master/classTMemFile.html) cannot be used with RDataFrame (at least not in “multi-threaded” mode).

---

<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:** [May 5, 2023, 2:51pm UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/8 "2023-05-05T14:51:21Z")

</div>

Yes, as far as I know multi-thread access to a TTree backed by a TMemFile is not something ROOT supports (so RDF can’t do it either).

---

<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:** [May 6, 2023, 9:22pm UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/9 "2023-05-06T21:22:19Z")

</div>

> TTree backed by a TMemFile is not something ROOT supports

In what context? It should work for most uses.

> that memory-resident trees are not supported _with EnableImplicitMT()_ active.  
> Is there particular reason why this feature is not supported ?

The technical reasons is that `TFile` objects are not thread safe in themselves. In order to support accessing the same file from multiple thread, the simplest way is to create multiple `TFile` object looking/viewing the same physical file. In the case of a memory file, with the current code, it would require duplicating in memory the entire ‘file’ which too many cases would blow up the memory. Instead we need to develop code so that multiple `TMemFile` objects can view the same in-memory file and/or develop a thread safe `TMemFile`. Either way this requires code that we have not developed yet.

> The dataFrame is defined on a **memory-resident tree** created via `TTree.MergeTrees(TList)`.

Why? Why not skip that step and create a `TChain` and use it with `RDataFrame`?

---

<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:** [May 6, 2023, 10:16pm UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/10 "2023-05-06T22:16:56Z")

</div>

> [@pcanal](#):
>
> > TTree backed by a TMemFile is not something ROOT supports
> 
> In what context? It should work for most uses.

The beginning of that sentence was “multi-thread access to a…” 😃 Or do you mean that should work as well?

---

<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:** [May 7, 2023, 7:13pm UTC](https://root-forum.cern.ch/t/issues-in-the-conversion-of-rdataframe-column-into-a-numpy-array/54737/11 "2023-05-07T19:13:10Z")

</div>

> The beginning of that sentence was “multi-thread access to a…” 😃 Or do you mean that should work as well?

Yes, I missed the qualifier and in that my answer is the right explanation. (i.e. Technically ROOT does not support **direct** multi-thread access to any `TFile` or `TTree`. RDF has code to allow multi-thread access to the same on-disk file (using multiple `TFile` object), doing the same for a `TMemFile` is not implemented … yet?)
