# 2D arrays as a leaf

Hello,
I’m trying to do the following:

``````const Int_t nbins=2;
const Int_t nbins2=3;

struct mystruct
{
Double_t N[nbins][nbins2];
};

void func()
{
struct mystruct myone;

TTree tr("abc","tree");
tr.Branch("a",&nbins,"nbins/I");
tr.Branch("b",&nbins2,"nbins2/I");
tr.Branch("c",myone.N,"N[nbins][nbins2]/D");

myone.N=4;
tr.Fill();
tr.Scan("N");
}``````

If N is 1D array such code works well. It also works well If I explicit write a dimensions of array:

``tr.Branch("c",myone.N,"N/D");  //instead of N[nbins][nbins2]``

Is there a way to have 2D arrays and number of dimensions declared as a const variable?

TTree’s leaflist mechanism only support one variable dimension (and several fixed dimensions).

Cheers,
Philippe.

To bad Hi,

If you really need a double variable array, you should use a higher level structure (for example a vector<vector >)

Cheers,
Philippe.

Thanks for hint.

``````const Int_t nbins=2;
const Int_t nbins2=3;
const Int_t dim=nbins*nbins2;

struct mystruct
{
Double_t N[nbins][nbins2];
};

void wiocha()
{

struct mystruct myone;

TTree tr("abc","tree");

tr.Branch("a",&dim,"dim/I");
tr.Branch("b",myone.N,"N[dim]/D");

myone.N=1;
myone.N=2;
myone.N=3;
myone.N=4;
myone.N=5;
myone.N=6;

tr.Fill();
tr.Scan("N");
}
``````

It seems to works ok.
May I expect some unexpected behavior?

this is fine (but TTree::Draw will only know about one dimension)

Philippe

Hi,

If you say

tr.Branch(“c”,myone.N,“N/D”);
works and
tr.Branch(“c”,myone.N,“N[nbins][nbins2]/D”);
doesn’t

try:

TString bvar(“N[”);
bvar += nbins;
bvar += “][”;
bvar += nbins2;
bvar += “]/D”;
tr.Branch(“c”,myone.N,bvar.Data());

I use this technique a lot.

A nice day to all,

Rui