# The fastest way to read all tree branch data?

**URL:** <https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263>\
**Category:** ROOT\
**Tags:** tree\
**Created:** [March 4, 2020, 10:47pm UTC](https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263 "2020-03-04T22:47:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![petrillo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@petrillo](https://root-forum.cern.ch/u/petrillo)\
**Post date:** [March 4, 2020, 10:47pm UTC](https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263/1 "2020-03-04T22:47:44Z")

</div>

Hello,

I would like to learn the fastest way to read _all_ the data from a simple tree branch.  
Imagine a single branch `MyData` with a single leaf of type `Float_t`, one entry per event.  
The tree is a single `TTree` stored in _one_ `TFile`.  
I would like to unpack all its data, i.e. across all events, in memory (`Float_t*`) at once.  
Which is the approach that takes _the least run time_?

Thank you.

* * *

_ROOT Version:_ 6.18

* * *

---

<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:** [March 5, 2020, 10:20am UTC](https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263/2 "2020-03-05T10:20:21Z")

</div>

Hi Gianluca,  
welcome to the ROOT forum! Without any other context, probably pre-allocating the vector and using `TBranch::GetEntry` is the fastest (@pcanal can comment on whether I’m missing a `tree->SetBranchStatus("*", 0); tree->SetBranchStatus("x", 1);`):

```cpp
TFile f("file.root");
TTree* t = f.Get<TTree>("tree");
TBranch* b = t->GetBranch("x");
float x;
b->SetAddress(&x);
const auto n_entries = t->GetEntries();
std::vector<float> v;
v.reserve(n_entries);
for (auto i = 0ll; i < n_entries; ++i) {
    b->GetEntry(i);
    v.push_back(x);
}

```

Note that, for performance, you should always use compiled code (not interpreted macros) and compile with at least `-O2`. Also, the exact ROOT API might matter as much as or less than TTree clustering/basket size or network bandwidth if reading over the network. Finally, depending on the dataset size, you could see significant speed-ups reading different TTree clusters in different threads (speed-ups that would trample any performance difference in the API used).

I attach benchmarking code that you can play with.

Cheers,  
Enrico

[read\_bench.cpp](https://root-forum.cern.ch/uploads/short-url/b0ct2duj8Jp14OqW5b0iVtGyNSD.cpp) (2.7 KB)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 5, 2020, 10:47am UTC](https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263/3 "2020-03-05T10:47:57Z")

</div>

```cpp
{
  TFile *f = TFile::Open("some_file.root");
  TTree *t; f->GetObject("some_tree", t);
  t->SetEstimate(-1); // keep all results (assumes one result per entry)
  Long64_t n = t->Draw("some_leaf", "", "goff");
#if 1 /* 0 or 1 */
  if (n > t->GetEstimate()) // just a precaution
    { t->SetEstimate(n); t->Draw("some_leaf", "", "goff"); }
#endif /* 0 or 1 */
  for (Long64_t i = 0; i < n; i++) std::cout << t->GetV1()[i] << std::endl;
}

```

---

<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:** [March 5, 2020, 11:29am UTC](https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263/4 "2020-03-05T11:29:59Z")

</div>

Wow thanks, I had no idea TTree::Draw could do that 😄  
I added your solution to the benchmark (hopefully without misinterpreting/mistranslating anything).  
The new version is below. On my machine the accumulated runtimes for 10 iterations on 1e6 entries (might not be super representative because it’s only one TTree cluster):

```auto
branch 0.326195
tree 0.505985
treereader 0.457618
take 0.62232
foreach 0.588362
tree_draw 0.665106

```

[read\_bench\_v2.cpp](https://root-forum.cern.ch/uploads/short-url/yuTo727Wr5AHgSpsCxmps1iyhMu.cpp) (3.4 KB)

---

<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:** [March 5, 2020, 2:47pm UTC](https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263/5 "2020-03-05T14:47:30Z")

</div>

By the way – may I ask what your actual usecase is? Depending on the answer there could be other options (e.g. if you have to fill a histogram with the data, it’s better to do it on the fly, and if you have more than one float per entry my numbers above might not really apply).

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![petrillo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@petrillo](https://root-forum.cern.ch/u/petrillo)\
**Post date:** [March 5, 2020, 3:28pm UTC](https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263/6 "2020-03-05T15:28:10Z")

</div>

Well, I cheated. My _actual_ use case is to fill a Python linear data structure (let’s say a `numpy.ndarray`, or an `array.array`) with data from the tree, where the data is stored sparsely in two branches, in a tree like `index:value`, and the indices not included in the tree are associated to value `0`. I have no control on the format on the input data.  
Going `for event in tree:` was slower than I had hoped.  
But in the end my question was genuine beyond the specific use case.

Thank you for the answers!

---

<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:** [March 5, 2020, 3:45pm UTC](https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263/7 "2020-03-05T15:45:45Z")

</div>

Well if the language is python that changes things. For loops are out of the question.  
You can use TTree::AsMatrix or RDataFrame::AsNumpy, tutorials [here](https://root.cern.ch/doc/master/pyroot002__TTreeAsMatrix_8py.html) and [here](https://root.cern.ch/doc/master/df026__AsNumpyArrays_8py.html).

Other helpful posts: [https://root-forum.cern.ch/search?context=topic&context\_id=38263&q=event%20loop%20Python%20slow&skip\_context=true](https://root-forum.cern.ch/search?context=topic&context_id=38263&q=event%20loop%20Python%20slow&skip_context=true)

---

<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:** [March 5, 2020, 3:52pm UTC](https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263/8 "2020-03-05T15:52:22Z")

</div>

Out of curiosity: what’s the physics usecase for a TTree like that?

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 19, 2020, 3:52pm UTC](https://root-forum.cern.ch/t/the-fastest-way-to-read-all-tree-branch-data/38263/9 "2020-03-19T15:52:23Z")

</div>

Closed on March 19, 2020, 3:52pm UTC

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