Adding a branch of multidimensional array to TTree

Can I add a branch of multidimensional array to TTree?
e.g.

Float_t f[10][5];
tree->Branch(“fBranch”,f,“f[10][5]/F”);

Hi,
yes you can, and precisely with the syntax you use in you post :slight_smile:

Cheers,
Enrico

is f really the correct reference to f[10][5]?

Hi,
yes, in C and C++ the name of a raw array is a pointer to the beginning of the array.
Multi-dimensional arrays are nothing else than arrays of arrays, and f, in your case, is a pointer to the first array of arrays.

Here is a small working example that you can start with:

#include <TTree.h>
#include <TFile.h>

int main()
{
   TFile file("f.root", "RECREATE");
   TTree t("t", "t");
   float f[2][3] = {{1,2,3}, {4,5,6}};
   t.Branch("fBranch", f,"f[2][3]/F");
   t.Fill();
   t.Write();

   return 0;
}

Cheers,
Enrico

1 Like

How about reading the tree?
Imagine I am only interested in element [1][1].

float interesting_element;
tree.SetBranchAddress(“f[1][1]”, &interesting_element);
?

Imagine I am interested in the whole matrix.

float matrix[2][3];
tree.SetBranchAddress(“f”, matrix);
?

Hi,
as far as I know you need to do a SetBranchAddress of the whole matrix, and then access the index you are interested in.
You can check out how this kind of things are done in the tree tutorials.

Cheers,
Enrico

One can find directions how to write multidimensional arrays to trees but the important issue how to retrieve them has been overlooked in the user guide and tutorials.

Hi,
I agree.
I think the reason is that SetBranchAddress takes a void* and therefore its usage is pretty much the same for any branch type, but I will make sure to pass your feedback along!

Cheers,
Enrico

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