I am writing compiled C++ code to read data from a particular TTree, which I will call myTree. Several TLeaves in this myTree are arrays of pointers. For example, myLeaf is an array of pointers to Int_ts; those Int_ts are, in turn, the the first elements of arrays. So, abstractly, myLeaf is a two-dimensional array of Int_ts.
The problem: I cannot figure out how to get the elements of that array. Here is what I am trying, based on how TTree::MakeClass attempted to read the elements:
const Int_t firstDim = 1; // size of the first dimension of the array
Int_t* myArray[firstDim];
myArray[0] = 0;
TBranch* b_myLeaf;
myTree->SetBranchAddress("myLeaf", myArray, &b_myLeaf);
for(Int_t i=0; i<myTree->GetEntries(); ++i)
{
myTree->GetEntry(i);
cout << myArray[0][0] << endl;
}
I think this should output the zeroth element of the Int_t array which is pointed to by myLeaf[0], or, in other words, the (0,0) element of the two-dimensional array. Instead, I get a segmentation fault. If I try to output myArray[0] instead, I get all zeros, so that pointer is pointing nowhere.
Using TTree::Draw, I can see that myLeaf does point to meaningful, nonzero integers. For instance:
myTree->Draw("myLeaf[0]")
produces a histogram of the (0,x) elements of the myLeaf array for all events in myTree, where x ranges from 0 to secondDim = the size of the second array dimension. That is what I expect. However, curiously, doing:
myTree->Draw("myLeaf[0][2]")
does not produce a histogram of the (0,2) elements of the myLeaf array. It produces a histogram with the correct number of entries but the wrong values for those entries (the values shown correspond to the (0,0) elements).
Am I doing something wrong? What is the correct way to read TLeaves that are arrays of pointers?