This is the main problem … but let’s get back to that.
The TTree in the file input_data.root has the structure:
******************************************************************************
*Tree :Data : Data *
*Entries : 100231 : Total = 1609598 bytes File Size = 458293 *
* : : Tree compression factor = 3.51 *
******************************************************************************
*Br 0 :dev_ID : dev_ID/l *
*Entries : 100231 : Total Size= 804583 bytes File Size = 7474 *
*Baskets : 26 : Basket Size= 32000 bytes Compression= 107.54 *
*............................................................................*
*Br 1 :timestamp : timestamp/l *
*Entries : 100231 : Total Size= 804673 bytes File Size = 449948 *
*Baskets : 26 : Basket Size= 32000 bytes Compression= 1.79 *
*............................................................................*
which means that there is a single value of dev_id and timestamp for each entry (i.e. l_var->GetLen()
returns the expected value: “1”.
On the other hand the TTree and the TLeaf has many entries ( 100231
)
The code provide loops properly (albeit in an unusual way, in particular not support TChain):
const int nents = branch->GetEntries();
for(int i=0; i<nentries;i++)
(as a side note the correct type for the number of entries is “Long64_t” or “long long”).
but it is then using the index variable incorrectly.:
cout << l_timestamp->GetValueLong64(i) << endl;
2 problems. The dynamic range of the parameter of GetValueLong64 is [0, l_timestamp->GetLen() [ whereas the code is passing [0, branch->GetEntries() [
and worse … it never actually loads the data.
The smallest change to make the code works is
const int nents = branch->GetEntries(); // in rare case 2 branches may have a different number of entries, so technically the wrong branch is used here.
for(int i=0; i<nentries;i++) {
l_timestamp->GetBranch()->GetEntry(i);
cout << l_timestamp->GetValueLong64(0) << endl;
}
and a more usual way to write this.
for(Long64_t i=0; i < tree->GetEntriesFast(); ++i) {
Long64_t localentry = tree->LoadTree( i ); // this is needed in case of a TChain and to enable the TTreeCache.
l_timestamp->GetBranch()->GetEntry( localentry );
cout << l_timestamp->GetValueLong64(0) << endl;
}
But then we have to go back on the way you created input_data.root you may or many not have intended on having ‘arrays’ inside each entry (in which case GetLen would be greater than one). It really depends on the semantic (and ‘shape’) of your data …
Cheers,
Philippe.