# Reading TBranch with leaf list

**URL:** <https://root-forum.cern.ch/t/reading-tbranch-with-leaf-list/43267>\
**Category:** Newbie\
**Created:** [January 25, 2021, 10:46pm UTC](https://root-forum.cern.ch/t/reading-tbranch-with-leaf-list/43267 "2021-01-25T22:46:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erdrick](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/7bcc69/32.png) [@Erdrick](https://root-forum.cern.ch/u/Erdrick)\
**Post date:** [January 25, 2021, 10:46pm UTC](https://root-forum.cern.ch/t/reading-tbranch-with-leaf-list/43267/1 "2021-01-25T22:46:59Z")

</div>

* * *

_ROOT Version:_ v6.14.06  
_Platform:_ Ubuntu 16.04.6 LTS  
_Compiler:_ 5.4.0

* * *

I have a root file in which a branch was written as a leaf list instead of a full object. What is the best way to read a leaf list in this scenario? I had never done this until today, which is why I am asking here if what I am doing is the “standard” way.

Here is my minimal example that shows how I am currently reading the leaf list. Is there a better/safer way to do this in the function `read_tree()`?

```auto
#include <iostream>

#include "TFile.h"
#include "TLeaf.h"
#include "TTree.h"

struct Channel {
  double time = 0;
  double energy = 0;
};

void make_tree() {

  auto file = std::unique_ptr<TFile>{TFile::Open("output.root", "RECREATE")};
  auto tree = new TTree("tree", "");

  Channel channel;
  tree->Branch("channel", &channel, "channel.time/D:channel.energy/D");

  channel.time = 1;
  channel.energy = 10.123;
  tree->Fill();

  channel.time = 2;
  channel.energy = 20.987;
  tree->Fill();

  tree->Write();
  file->Close();

}

void read_tree() {

  auto file = std::unique_ptr<TFile>{TFile::Open("output.root", "READ")};
  auto tree = (TTree*) file->Get("tree");

  double time = 0;
  TLeaf* leaf_time = (TLeaf*) tree->GetListOfLeaves()->FindObject("channel.time");
  double energy = 0;
  TLeaf* leaf_energy = (TLeaf*) tree->GetListOfLeaves()->FindObject("channel.energy");

  for (int i = 0; i < tree->GetEntries(); ++i) {
    tree->GetEntry(i);
    std::cout << i << "\t"
  	      << leaf_time->GetValue() << "\t"
	      << leaf_energy->GetValue() << std::endl;
  }

}

```

For reference, I looked at the following before posting:

- [Accessing each Leaf in a .root file over a loop to extract its data](https://root-forum.cern.ch/t/accessing-each-leaf-in-a-root-file-over-a-loop-to-extract-its-data/35633/9)
- [https://roottalk.root.cern.narkive.com/Q4MDKilz/help-with-ttree-setbranchaddress](https://roottalk.root.cern.narkive.com/Q4MDKilz/help-with-ttree-setbranchaddress)
- [TTreeReader reading branch created as leaf list](https://root-forum.cern.ch/t/ttreereader-reading-branch-created-as-leaf-list/29969)

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [January 26, 2021, 9:20am UTC](https://root-forum.cern.ch/t/reading-tbranch-with-leaf-list/43267/2 "2021-01-26T09:20:47Z")

</div>

I think there is an extra “channel” in your leaflist:

```auto
tree->Branch("channel", &channel, "time/D:energy/D");

```

Then one option is to read back a `Channel` object:

```c++
  Channel channel;                                                                                                      
  tree->SetBranchAddress("channel", &channel);                                                                          
                                                                                                                        
  for (int i = 0; i < tree->GetEntries(); ++i) {                                                                        
    tree->GetEntry(i);                                                                                                  
    std::cout << i << "\t" << channel.time << "\t" << channel.energy << std::endl;                                                                                
  }                                        

```

Also note that `for (int i = 0; i < tree->GetEntries(); ++i) {` is probably much less efficient than `const auto entries = tree->GetEntries(); for (int i = 0; i < entries; ++i) {`.

Cheers,  
Enrico
