Saving complex numbers on a TBranch

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):

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:

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++

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.


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

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:

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


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.

1 Like