# Reading data from TLeaf returns false values

**URL:** <https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622>\
**Category:** Newbie\
**Created:** [November 1, 2019, 9:56am UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622 "2019-11-01T09:56:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fraglesos](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/ecae2f/32.png) [@fraglesos](https://root-forum.cern.ch/u/fraglesos)\
**Post date:** [November 1, 2019, 9:56am UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622/1 "2019-11-01T09:56:50Z")

</div>

Hello everyone,  
I’m creating a data file filled with ULong64\_t data read from a txt file. The code I wrote looks like this:

```auto
ULong64_t var;
TFile* output_file = new TFile("input_data.root", "RECREATE");
TTree* tree=new TTree("Data","Data");

tree->Branch("var",&var,"var/l");

while(text_file.good())
{
   if(text_file.eof()) break;
   getline(text_file,line);
   var = stol(line);
   tree->Fill();
}
output_file->Write();

```

Then I’m trying to read this data with another program like this:

```auto
TFile *file = new TFile("input_data.root", "READ");
TTree *tree = (TTree*) file->Get("Data");
TBranch* branch = (TBranch*) tree->GetBranch("var");
TLeaf *l_var = (TLeaf*)tree->GetLeaf("var");

const int nents = branch->GetEntries();
for(int i=0; i<nents;i++)
{
    cout << l_var->GetValueLong64(i);
}

```

The script above should display values stored in var leaf, but it returns random numbers instead. I suspect this is because of my data structure. When I try

```auto
l_var->GetLen()

```

it returns 1 instead of real number of elements in var. How should I fill the Tree for my script to work correctly?

_ROOT Version:_ 6.19  
_Platform:_ Manjaro Gnome 3.34  
_Compiler:_ gcc 9.2

* * *

---

<div class="post-metadata">

**Author:** ![StephanH](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@StephanH](https://root-forum.cern.ch/u/StephanH)\
**Post date:** [November 4, 2019, 7:30am UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622/2 "2019-11-04T07:30:24Z")

</div>

Hi,

@pcanal can surely help here.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 4, 2019, 3:12pm UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622/3 "2019-11-04T15:12:38Z")

</div>

> [@fraglesos](#):
>
> l\_var-\>GetLen()

This is supposed to be the number of elements _per entry_ which seems to the case in the way you are reading the input file, what am I missing?

On your ‘output’ ROOT file, what does

```auto
Data->Scan("var");

```

prints to the screen (i.e. the expected value or ‘random numbers’?)

---

<div class="post-metadata">

**Author:** ![fraglesos](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/ecae2f/32.png) [@fraglesos](https://root-forum.cern.ch/u/fraglesos)\
**Post date:** [November 4, 2019, 7:18pm UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622/4 "2019-11-04T19:18:59Z")

</div>

Thank you for your responces.

I did

```auto
tree->Scan("var");

```

both before writing file and after reading it in another script. It reads right values. The problem I have lies in this piece of code

```auto
const int nents = branch->GetEntries();
for(int i=0; i<nents;i++)
{
    cout << l_var->GetValueLong64(i);
}

```

and is twofold:

1. When I try to set `nents = l_var->GetLen()` the loop iterates once as if var had one element (it has over 100k ULongs written in it)
2. When I run the loop with right nents (over 100k) I get with `l_var->GetValueLong64(i);` random values (mostly 0s, sometimes other numbers) instead of values stored in “var”.

This is output of `tree->print()`

```auto
******************************************************************************
*Tree :Data : Data *
*Entries : 100231 : Total = 1609703 bytes File Size = 17505 *
* : : Tree compression factor = 91.39 *
******************************************************************************
*Br 0 :dev_ID : dev_ID/l *
*Entries : 100231 : Total Size= 804752 bytes File Size = 7312 *
*Baskets : 25 : Basket Size= 32000 bytes Compression= 109.39 *
*............................................................................*
*Br 1 :var : var/l *
*Entries : 100231 : Total Size= 804659 bytes File Size = 10193 *
*Baskets : 25 : Basket Size= 32000 bytes Compression= 78.48 *
*............................................................................*

```

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 4, 2019, 7:42pm UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622/5 "2019-11-04T19:42:23Z")

</div>

> [@fraglesos](#):
>
> When I try to set `nents = l_var->GetLen()` the loop iterates once as if var had one element (it has over 100k ULongs written in it)

According to your `tree->print();`, this is the right value. There is only one value per TTree entry.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 4, 2019, 7:42pm UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622/6 "2019-11-04T19:42:55Z")

</div>

> When I run the loop with right nents (over 100k) I get with `l_var->GetValueLong64(i);` random values (mostly 0s, sometimes other numbers) instead of values stored in “var”.

Can you send me the file so I can investigate the problem?

---

<div class="post-metadata">

**Author:** ![fraglesos](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/ecae2f/32.png) [@fraglesos](https://root-forum.cern.ch/u/fraglesos)\
**Post date:** [November 4, 2019, 8:16pm UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622/7 "2019-11-04T20:16:20Z")

</div>

[input\_data.root](https://root-forum.cern.ch/uploads/short-url/8GCt2EzaWpQGIgtZsisqCuZU1gz.root) (452.4 KB)  
[timedist.cc](https://root-forum.cern.ch/uploads/short-url/pfMCtm0XVDDHj0CkEZWuDYSz8gW.cc) (986 Bytes)

input\_data.root is a file generated from txt file with a piece of code I posted in the first messege. Data stored in it looks correct. timedist.cc is a script with which i try to work on the data.  
I’m pretty sure my problem is caused by lack of knowledge about TTree data structure and how to create it properly, because this method of getting data works on files created with other people’s scripts. I just don’t have acess to their code to see how they fill the tree.

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [November 5, 2019, 4:26pm UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622/8 "2019-11-05T16:26:15Z")

</div>

FYI, the file contains this data:

```auto
$> root-dump ./input_data.root
>>> file[./input_data.root]
key[000]: Data;1 "Data" (TTree)
[000][dev_ID]: 7044
[000][timestamp]: 1542909667832
[001][dev_ID]: 7044
[001][timestamp]: 1542914704622
[002][dev_ID]: 7044
[002][timestamp]: 1542917044017
[003][dev_ID]: 7044
[003][timestamp]: 1542918533305
[...]
[100227][dev_ID]: 8327
[100227][timestamp]: 1549420051561
[100228][dev_ID]: 8327
[100228][timestamp]: 1549596914556
[100229][dev_ID]: 8327
[100229][timestamp]: 1549641972080
[100230][dev_ID]: 8327
[100230][timestamp]: 1549699592149

```

_i.e._ the `TTree` named `"Data"` contains 100231 entries, with every entry containing 2 scalars (`dev_ID` and `timestamp`).

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 12, 2019, 9:26pm UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622/9 "2019-11-12T21:26:19Z")

</div>

> [@fraglesos](#):
>
> [`t_var->GetLen()`] returns 1 instead of real number of elements in var. How should I fill the Tree for my script to work correctly?

This is the main problem … but let’s get back to that.

The TTree in the file input\_data.root has the structure:

```auto
******************************************************************************
*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):

```auto
	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.:

```auto
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

```auto
	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.

```auto
	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.

---

<div class="post-metadata">

**Author:** ![fraglesos](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/ecae2f/32.png) [@fraglesos](https://root-forum.cern.ch/u/fraglesos)\
**Post date:** [November 12, 2019, 9:44pm UTC](https://root-forum.cern.ch/t/reading-data-from-tleaf-returns-false-values/36622/10 "2019-11-12T21:44:16Z")

</div>

Thank you for your insights. I’m selflearner and that’s why my code can be a mess.
