# Reset the reader from TTreeReader

**URL:** <https://root-forum.cern.ch/t/reset-the-reader-from-ttreereader/49889>\
**Category:** Newbie\
**Created:** [May 6, 2022, 4:39pm UTC](https://root-forum.cern.ch/t/reset-the-reader-from-ttreereader/49889 "2022-05-06T16:39:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daumann](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Daumann](https://root-forum.cern.ch/u/Daumann)\
**Post date:** [May 6, 2022, 4:39pm UTC](https://root-forum.cern.ch/t/reset-the-reader-from-ttreereader/49889/1 "2022-05-06T16:39:02Z")

</div>

Hi, so I am reading a the branches of a tree in c++ with:

`TTreeReaderValue<float> mcWeight(reader, "mcWeight");`

and i am looping through the events in the tree with:

```auto
while( reader.Next()) {}

```

But i need to make a loop a trough the tree with something like:

```auto
for(int i=0; i < 3000 ; i++) {
   while( reader.Next()) {} 
}

```

but when I do it, the code just “get” into the while() for the first interaction of I, after that it just passes through. I guess I need to restart the reader right? How can I do that?

Best Regards,  
Caio.

---

<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:** [May 6, 2022, 4:52pm UTC](https://root-forum.cern.ch/t/reset-the-reader-from-ttreereader/49889/2 "2022-05-06T16:52:14Z")

</div>

You could try something like this:

```cpp
reader.Restart(); // start from the first entry
int i = 0;
while (reader.Next()) {
  if (++i > 3000) break; // exit "while" after (at most) the first 3000 entries
  // ...
}

```

Unless you really want:

```cpp
for (int i = 0; i < 3000; i++) {
  reader.Restart(); // always start from the first entry
  while(reader.Next()) { // loop over all available entries
    // ...
  }
}

```

---

<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:** [May 6, 2022, 6:46pm UTC](https://root-forum.cern.ch/t/reset-the-reader-from-ttreereader/49889/3 "2022-05-06T18:46:05Z")

</div>

> [@Daumann](#):
>
> i need to make a loop a trough the tree with something like:

Can you elaborate on why? `while (reader.Next())` already loops over the full tree, why do you need to do it 3k times? There might be a better way to do what you want to do.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Daumann](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Daumann](https://root-forum.cern.ch/u/Daumann)\
**Post date:** [May 6, 2022, 8:41pm UTC](https://root-forum.cern.ch/t/reset-the-reader-from-ttreereader/49889/4 "2022-05-06T20:41:58Z")

</div>

Thanks, @Wile_E_Coyote, and @eguiraud for the tips and solution! Yes indeed, there are better ways to handle this, as the one Wile suggested. But then I will need to create a bunch of histograms for each mass interval I need to look to, so the reader.Restart() is the lazy way I will use for exploratory purposes.
