# Flatten multi-dimensional array in rDataframe

**URL:** <https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163>\
**Category:** ROOT\
**Tags:** rdataframe\
**Created:** [March 14, 2022, 3:39am UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163 "2022-03-14T03:39:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![SiewYan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/siewyan/32/20273_2.png) [@SiewYan](https://root-forum.cern.ch/u/SiewYan)\
**Post date:** [March 14, 2022, 3:39am UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/1 "2022-03-14T03:39:25Z")

</div>

Hello,

I am working on analyzing a root file generated from GEANT4, it consists of branches of 2 or 3 dimensional array , for example:

```auto
ntrkhits_pandoraTrack[ntracks_pandoraTrack][3]/S
trkresrg_pandoraTrack[ntracks_pandoraTrack][3][2000]/F

```

I am attempting to use RDataframe for offline postprocessing , aka flattening those multi-dimensional arrays into 1D branches, for example:

```auto
// reading the 2D array as if the 2D array is unrolled into 1D in TTree, n_x+n_y*i
auto df2 = df1.Define( "ntrkhits_Uplane" , "ntrkhits_pandoraTrack[ntracks_pandoraTrack+3*0]" )
.Define( "ntrkhits_Vplane" , "ntrkhits_pandoraTrack[ntracks_pandoraTrack+3*1]" )
.Define( "ntrkhits_WZplane" , "ntrkhits_pandoraTrack[ntracks_pandoraTrack+3*2]" );

```

However, I have gotten errors:

```auto
terminate called after throwing an instance of 'std::runtime_error'
  what(): TTree leaf ntrkhits_pandoraTrack has both a leaf count and a static length. This is not supported.

```

What is the correct way, or better way to read 2D array in RDataframe?

Thank you very much!  
Siewyan

\_ROOT Version:6.22/06  
\_Platform: CentOS 7  
\_Compiler: g++

* * *

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [March 14, 2022, 8:35am UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/2 "2022-03-14T08:35:03Z")

</div>

@eguiraud can most probably give you some hints

---

<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:** [March 14, 2022, 2:01pm UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/3 "2022-03-14T14:01:15Z")

</div>

Hi @SiewYan ,  
RDataFrame does not support 2D arrays well, because TTreeReader, which RDF uses internally, does not support 2D arrays well. The error you get is basically TTreeReader saying “I don’t know how to read this branch”.

But depending on the exact situation there might be a workaround – could you please share this data (even just a couple of events) with me so I can experiment a bit?

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![SiewYan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/siewyan/32/20273_2.png) [@SiewYan](https://root-forum.cern.ch/u/SiewYan)\
**Post date:** [March 14, 2022, 3:02pm UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/4 "2022-03-14T15:02:52Z")

</div>

Hello @eguiraud , thank you for your answer, and please find the link [\*] for your study. It would be great if there is a workaround on reading and manipulating 2D array in RDataframe.

Thanks and looking forward to hear from you.

Cheers,  
Siewyan

[\*] [CERNBox](https://cernbox.cern.ch/index.php/s/r2yrD920HEUXUmK)

---

<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:** [March 16, 2022, 10:43am UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/5 "2022-03-16T10:43:19Z")

</div>

Hi @SiewYan ,  
thank you for the data, I will take a look as soon as possible.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![SiewYan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/siewyan/32/20273_2.png) [@SiewYan](https://root-forum.cern.ch/u/SiewYan)\
**Post date:** [March 23, 2022, 3:02am UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/6 "2022-03-23T03:02:47Z")

</div>

Hello @eguiraud , may i know how it is going? thanks!

Cheers,  
Siewyan

---

<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:** [March 23, 2022, 2:24pm UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/7 "2022-03-23T14:24:34Z")

</div>

Hi @SiewYan ,  
sorry for the high latency, I can reproduce the problem but I could not find a workaround during my first investigation. I’ll give it another go asap.

Cheers,  
Enrico

---

<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:** [March 23, 2022, 4:46pm UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/8 "2022-03-23T16:46:06Z")

</div>

Ah, on a better look, I know what this is. It looks like this is an instance of [[ROOT-8827] TTreeReaderArray\<T\> does not work for VLEN branches whose length branch is not of type int - SFTJIRA](https://sft.its.cern.ch/jira/browse/ROOT-8827) . The problem is not that the array is 2-dimensional (as we are reading a flattened version anyway) but that its size is of type `short`.

This is a limitation of TTreeReader (which RDF uses for reading internally). A TTreeReader-only reproducer:

```cpp
void repro()
{
   TFile f("MUSUN_dunefd_1485_gen_g4filt_detsim_freco_ana.root");
   auto *t = f.Get<TTree>("analysistree/anatree");
   R__ASSERT(t != nullptr);
   TTreeReader r(t);
   TTreeReaderArray<float> idx(r, "trkdedx_pandoraTrack");
   r.Next();
}

```

results in:

```auto
~/S/w/forum_treereader_2dim_arrays root -l repro.C
root [0]
Processing repro.C...
Error in <TTreeReaderValueBase::CreateProxy()>: The branch ntracks_pandoraTrack contains data of type short. It cannot be accessed by a TTreeReaderValue<int>

```

So the only workaround I can propose would be to regenerate the file with a different size type (`int` or `unsigned int`) or to pre-process that file, without RDF, to do the conversion.

I realize that this is frustrating. You mentioned this file is generated from GEANT4, and we should be able to read files generated by GEANT. I’ll check how we can tackle this. FYI @Axel @pcanal .

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![SiewYan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/siewyan/32/20273_2.png) [@SiewYan](https://root-forum.cern.ch/u/SiewYan)\
**Post date:** [March 24, 2022, 2:47am UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/9 "2022-03-24T02:47:33Z")

</div>

@eguiraud thank you very much on taking the time to troubleshoot. Looks like this is the bottleneck for using RDF on my work; however, changing to `int` or `unsigned int` is not so trivial… (I reckon). I will need to refer to my colleague on the root file regeneration with the suggestion though.

On the other hand, it would be great if this is included in the future release.

I am open to other opinion if there is a mini-hack to get through it though.

Looking forward to hear from you all!

Cheers,  
Siewyan

---

<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:** [April 7, 2022, 2:48am UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/10 "2022-04-07T02:48:29Z")

</div>

Closed on April 7, 2022, 2:48am UTC

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

---

<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:** [April 19, 2022, 6:49pm UTC](https://root-forum.cern.ch/t/flatten-multi-dimensional-array-in-rdataframe/49163/11 "2022-04-19T18:49:04Z")

</div>

Hi @SiewYan , this is now fixed in the ROOT master branch. Tomorrow’s [nightly builds](https://root.cern/nightlies) will already contain the fix.

Cheers,  
Enrico
