# Transferring externally allocatted (pinned, aligned) columns to/from TTrees and files

**URL:** <https://root-forum.cern.ch/t/transferring-externally-allocatted-pinned-aligned-columns-to-from-ttrees-and-files/48983>\
**Category:** Newbie\
**Created:** [February 28, 2022, 4:37pm UTC](https://root-forum.cern.ch/t/transferring-externally-allocatted-pinned-aligned-columns-to-from-ttrees-and-files/48983 "2022-02-28T16:37:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericcano](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ericcano/32/28938_2.png) [@ericcano](https://root-forum.cern.ch/u/ericcano)\
**Post date:** [February 28, 2022, 4:37pm UTC](https://root-forum.cern.ch/t/transferring-externally-allocatted-pinned-aligned-columns-to-from-ttrees-and-files/48983/1 "2022-02-28T16:37:12Z")

</div>

Hello,

I am trying to find a way to efficiently (with a minimum of copies) transfer externally allocated columns of data (mostly build-in types, integers or floats) into a TTree, and eventually a file. Multiples columns are stored in pinned memory and properly aligned and co-located, for consumption by a GPU. And of course, we need the operations in the opposite direction too.

I was thinking of something conceptually like using a user-allocated fBuffer for the TBasket behind each TBranch, and then let root compress and write the data via the fZipBuffer and write to the file, but did not find the way/interface to do this.

Is this the proper way to attack this problem?

Thanks a lot,  
Eric

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [March 1, 2022, 9:00am UTC](https://root-forum.cern.ch/t/transferring-externally-allocatted-pinned-aligned-columns-to-from-ttrees-and-files/48983/2 "2022-03-01T09:00:27Z")

</div>

Hello,

I think we need @pcanal here!

---

<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 1, 2022, 9:57pm UTC](https://root-forum.cern.ch/t/transferring-externally-allocatted-pinned-aligned-columns-to-from-ttrees-and-files/48983/3 "2022-03-01T21:57:36Z")

</div>

Because in TTree, the buffer is always stored in (compressed) big-endian format, you are likely to need a copy/transformation before getting to the file. RNtuple has a mode where the endianness can match the in-memory format (@jblomer @jalopezg might help) and might be better suited for this task.

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [May 17, 2022, 8:34pm UTC](https://root-forum.cern.ch/t/transferring-externally-allocatted-pinned-aligned-columns-to-from-ttrees-and-files/48983/4 "2022-05-17T20:34:07Z")

</div>

Hi @ericcano,

Sorry, I skipped over this. Yes, RNTuple offers zero-copy access to the values stored in a column through an `RNTupleView`.  
This works for fields that can be directly mapped, i.e., if the endianness matches and the type does not require specific packing requirements (which is the case for fundamental types in little-endian architectures, e.g. x86).

As an example, one can do the following for a field of type `float`:

```c++
auto ntuple = RNTupleReader::Open("ntuple", "/path/to/file.ntuple");
auto view = ntuple->GetView<float>("field_name");

NTupleSize_t nItems = 0;
const float *buf = view.MapV(0, nItems);
// `nItems` should contain the number of elements in the mapped page. Values may
// be accessed via `buf[i]`.

for (unsigned i = 0; i < nItems; i++)
   std::cout << buf[i] << std::endl;

```

Cheers,  
J.
