# TTree: multiple variable length arrays

**URL:** <https://root-forum.cern.ch/t/ttree-multiple-variable-length-arrays/17466>\
**Category:** ROOT\
**Created:** [April 4, 2014, 3:50pm UTC](https://root-forum.cern.ch/t/ttree-multiple-variable-length-arrays/17466 "2014-04-04T15:50:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Thomassen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@Peter\_Thomassen](https://root-forum.cern.ch/u/Peter_Thomassen)\
**Post date:** [April 4, 2014, 3:50pm UTC](https://root-forum.cern.ch/t/ttree-multiple-variable-length-arrays/17466/1 "2014-04-04T15:50:53Z")

</div>

Hi,

I’d like to store several arrays in a tree, and the lengths of the arrays is variable. Thus,

```auto
	double var1[maxLength]; // first array to store
	double var2[maxLength]; // second array to store
	unsigned int var1n, var2n; // lengths of the arrays for a given event

	tree->Branch("var1n",&var1n,"var1f/i");
	tree->Branch("var2n",&var2n,"var2f/i");
	tree->Branch("var1",var1,"var1[var1f]/D");
	tree->Branch("var2",var2,"var2[var2f]/D");
```

This works fine.

Is it possible put var1n and var2n into the same branch (e.g. as an array)? I imagine something like this:

```auto
	double var1[maxLength]; // first array to store
	double var2[maxLength]; // second array to store
	unsigned int n[2]; // lengths of the arrays for a given event

	tree->Branch("n",n,"n[2]/i");
	tree->Branch("var1",var1,"var1[n[0]]/D");
	tree->Branch("var2",var2,"var2[n[1]]/D");
```

… but that doesn’t work.

So, the question is: Can something like this be done?

Motivation: I have many arrays (not just 2), and doing this would reduce the number of branches from 2N to N+1, making things much more tractable.

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 5, 2014, 11:53am UTC](https://root-forum.cern.ch/t/ttree-multiple-variable-length-arrays/17466/2 "2014-04-05T11:53:10Z")

</div>

Hi Peter,

why not moving directly to std::vector then?  
ROOT is able to handle it in a pretty optimised way and these day the stl is also _very_ performant on basically all platforms.

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![Peter\_Thomassen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@Peter\_Thomassen](https://root-forum.cern.ch/u/Peter_Thomassen)\
**Post date:** [April 6, 2014, 8:58pm UTC](https://root-forum.cern.ch/t/ttree-multiple-variable-length-arrays/17466/3 "2014-04-06T20:58:33Z")

</div>

Hi Danilo,

good idea, thank you.

Cheers,  
Peter
