Multiple variable size arrays in a TBranch

Hey! I would like to use arrays with variable size as leaves in the branch of a TTree. But when I do a simple test like this:

root [0] TTree* t=new TTree(“mytree”,“mytree”)
(TTree *) 0x30d8d60
root [1] int a[13]
(int [13]) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
root [2] t->Branch(“a”,a,“a_/I:a.1[a_]:a.2[a_]:a.3[a_]:a.4[a_]”)
(TBranch *) 0x381c200
root [3] for(int i=1;i<13;i++)a[i]=i-1
root [4] a[0]=3
(int) 3
root [5] a
(int [13]) { 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }
root [6] t->Fill()
(int) 52
root [7] t->Scan(“a.1:a.2:a.3:a.4”)


  • Row * Instance * a.1 * a.2 * a.3 * a.4 *

  •    0 *        0 *         0 *         0 *         0 *         0 *
    
  •    0 *        1 *         1 *         1 *         1 *         1 *
    
  •    0 *        2 *         2 *         2 *         2 *         2 *
    

(long long) 3

All four arrays have been filled with the data belonging to the first. When I replace “[a_]” by “[3]” in the leaflist, the result is as expected.

Am I missing something, is this a bug or are multiple variable size arrays not supported?

I’m on Debian testing, ROOT 6.10/04 compiled with clang 4.0 in c++14 mode.

Hello @all, I played around a little bit and found a workaround:

root [0] TTree* t=new TTree(“mytree”,“mytree”)
(TTree *) 0x2c5d5e0
root [1] int a[13]
(int [13]) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
root [2] t->Branch(“a”,a,“a_/I:a.1[a_]:a.2[a_]:a.3[a_]:a.4[a_]”)
(TBranch *) 0x3409c20
root [3] t->GetBranch(“a”)->GetLeaf(“a.1”)->SetAddress(a+1)
root [4] t->GetBranch(“a”)->GetLeaf(“a.2”)->SetAddress(a+4)
root [5] t->GetBranch(“a”)->GetLeaf(“a.3”)->SetAddress(a+7)
root [6] t->GetBranch(“a”)->GetLeaf(“a.4”)->SetAddress(a+10)
root [7] for(int i=1;i<13;i++)a[i]=i-1
root [8] a[0]=3
(int) 3
root [9] a
(int [13]) { 3, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }
root [10] t->Fill()
(int) 52
root [11] t->Scan(“a.1:a.2:a.3:a.4”)


  • Row * Instance * a.1 * a.2 * a.3 * a.4 *

  •    0 *        0 *         0 *         3 *         6 *         9 *
    
  •    0 *        1 *         1 *         4 *         7 *        10 *
    
  •    0 *        2 *         2 *         5 *         8 *        11 *
    

(long long) 3

Moreover, the ability to individually set the leaf addresses makes it possible to use different C-Arrays instead of one contiguous area, very convenient when using leaves of different types.

I’d suggest you consider using vectors.

root [1] int a[13]
(int [13]) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
root [2] t->Branch(“a”,a,“a_/I:a.1[a_]:a.2[a_]:a.3[a_]:a.4[a_]”)
(TBranch *) 0x381c200
root [3] for(int i=1;i<13;i++)a[i]=i-1
root [4] a[0]=3

Note that the leaflist mechanism for variable size array requires the index to be set to the maximum value of the number of element prior to creating the branch … otherwise it has no way of calculating the stride … it also requires that the ‘start’ address of the various element does not change over time.

I.e. the following might work for you

root [1] int a[13]
(int [13]) { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
root [2] a[0]=3
root [3] t->Branch(“a”,a,“a_/I:a.1[a_]:a.2[a_]:a.3[a_]:a.4[a_]”)
(TBranch *) 0x381c200
root [4] for(int i=1;i<13;i++)a[i]=i-1

Cheers,
Philippe.

PS. As @ksmith already mentioned, you would be better off with vectors :slight_smile:

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