# Saving complex numbers on a TBranch

**URL:** <https://root-forum.cern.ch/t/saving-complex-numbers-on-a-tbranch/45349>\
**Category:** Newbie\
**Created:** [June 10, 2021, 10:02pm UTC](https://root-forum.cern.ch/t/saving-complex-numbers-on-a-tbranch/45349 "2021-06-10T22:02:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ed20](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/f04885/32.png) [@ed20](https://root-forum.cern.ch/u/ed20)\
**Post date:** [June 10, 2021, 10:02pm UTC](https://root-forum.cern.ch/t/saving-complex-numbers-on-a-tbranch/45349/1 "2021-06-10T22:02:48Z")

</div>

What is the _fastest_ way to write a complex variable to a branch in ROOT? I have real and complex variables, and custom structs containing arrays of real and complex variables, as follows (simplified version):

```auto
template<typename T, std::size_t N>
struct MyVector{
	T element[N];
};

double var;
std::complex<double> cvar;
MyVector<double, 3> vec;
MyVector<std::complex<double>, 3> cvec; 

```

To save `var` and `vec` is apparently straightforward:

```auto
tree->Branch("real_var", &var, "D");
tree->Branch("real_vec", &vec, "element[3]/D");

```

But how can I efficiently save a branch containing elements of the type `std::complex<double>` or `MyVector<std::complex<double>, 3>`? Speed of writing the file is important (reading speed matters too, but much less) so I don’t want to split the structs into TLeaves for each component.

Apologies for the very elementary question, but any help would be greatly appreciated.

* * *

_ROOT Version:_ 6.22  
_Platform:_ Linux  
_Compiler:_ g++

* * *

---

<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:** [June 15, 2021, 9:32am UTC](https://root-forum.cern.ch/t/saving-complex-numbers-on-a-tbranch/45349/2 "2021-06-15T09:32:37Z")

</div>

Hi @ed20 ,  
sorry for the high latency, @pcanal or @Axel might be able to indicate the most performant way to write `std::complex<double>` to a TTree.

Cheers,  
Enrico

---

<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:** [June 16, 2021, 8:24pm UTC](https://root-forum.cern.ch/t/saving-complex-numbers-on-a-tbranch/45349/3 "2021-06-16T20:24:47Z")

</div>

ROOT contains a dictionary for `std::complex<double>` so you can directly do:

```auto
std::complex<double> cvar;
tree->Branch("complex_var", &cvar);

```

For a collection of `std::complex<double>` (for example `std::vector<std::complex<double>>` or your `MyVector<double, 3>`) you will need to generate a dictionary for that class/container; for example:

```auto
#pragma link C++ class std::vector<std::complex<double>>+;
or
#pragma link C++ class MyVector<double, 3>+;

```

Cheers,  
Philippe.

PS. In all those case, you can play with the split level to determine if the default is the fastest (default split level is 9, you would compare it to 0). Note that another factor that you may care about is the final file size (the split case is likely to compress better than the non-split case).

If write speed is important, the choice of compression algorithm might also be important (at the expense of file size, for example ‘no compression’ will, of course, be the fastest.

Next, depending of how costly it is to acquire the data and whether you need compression of not, having multiple prepare portion of the output on their own (i.e. run in parallel the processing, serialization and compression steps) and merge the output into the final file, might be of interest to you.
