TBranch with std::array

ROOT Version: 6.14.06
Platform: Ubuntu 16.04.6 LTS
Compiler: gcc/5.4.0

I would like to create a TTree with a TBranch holding an std::array. The documentation for TTree (https://root.cern.ch/doc/master/classTTree.html) states that one can make a TBranch with a collection from the standard library. I can do this with an std::vector, as this minimal example shows:

#include "TFile.h"
#include "TTree.h"

void minimal() {

  TFile* file = new TFile("file.root", "RECREATE");
  TTree* tree = new TTree("t", "");
  //std::array<double, 10> values;
  std::vector<double> values(10);
  tree->Branch("values", &values);

}

However, if I comment out the line std::vector<double> values(10); and uncomment the line std::array<double, 10> values; then I get this error:

root [0] .L minimal.C++g
Info in <TUnixSystem::ACLiC>: creating shared library /path/to/./minimal_C.so
root [1] minimal()
Error in <TBranchElement::InitializeOffsets>: Could not find the real data member '_M_elems[10]' when constructing the branch 'values' [Likely missing ShowMember].

Any help would be appreciated as to how to proceed.

Hi, I think for array the tutorials around always showed them using C style arrays.
And usually what you do is
Int nentries =10
Double arrValue[nentries]
And then 2 branches
One for nentries &nentries
And one for &arrValue, with leaf name specified being “arrValues[nentries]”
I checked once the size of a file with array branches and vector branches, vector branches are barely bigger and in general I have the preference to use vectors (resized) as branches.
Hope it helps
Renato

Hi,

Thanks for the reply.

Unfortunately, I do not want to use a C-style array. The point is that std::array is a collection from the standard library, like std::vector. I don’t see why ROOT can handle std::vector, but not std::array. This seems like a bug.

Perhaps one of the ROOT experts can weigh in on why this is or how to use a std::array in a TBranch.

I don’t understand the details, but the word “array” is problematic in ROOT, and always has been. E.g. try making a simple int called “array”:

root [0] int array = 5
(int) 5
root [1] array
ROOT_prompt_1:1:1: error: reference to 'array' is ambiguous
array
^
ROOT_prompt_0:1:5: note: candidate found by name lookup is 'array'
int array = 5
    ^
/home/jfcaron/Software/root/etc/../include/Vc/scalar/../common/../traits/has_contiguous_storage.h:43:40: note: candidate found by name lookup is 'std::array'
template <typename T, size_t N> struct array;
                                       ^
root [2] int i = 5
(int) 5

You can’t even declare an std::array at the interactive prompt. So this goes beyond storing them in a TTree.

Hi,

That is certainly interesting, but I don’t think it is related to my problem. For example, you could do the same for “vector”:

root [0] int vector = 0
(int) 0
root [1] vector
ROOT_prompt_1:1:1: error: reference to 'vector' is ambiguous
vector
^
ROOT_prompt_0:1:5: note: candidate found by name lookup is 'vector'
int vector = 0
    ^
/usr/include/c++/5/bits/stl_vector.h:214:11: note: candidate found by name lookup is 'std::vector'
    class vector : protected _Vector_base<_Tp, _Alloc>
          ^

Root Forum -> Search -> “std::array”

Hi Wile,

Thanks! Sorry about that… I didn’t come across Adding std::array to a TTree, which says that this is implemented in root v6.20.

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