# Loop over all events within RDataFrame

**URL:** <https://root-forum.cern.ch/t/loop-over-all-events-within-rdataframe/47599>\
**Category:** Newbie\
**Tags:** rdataframe\
**Created:** [November 9, 2021, 4:21pm UTC](https://root-forum.cern.ch/t/loop-over-all-events-within-rdataframe/47599 "2021-11-09T16:21:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [November 9, 2021, 4:21pm UTC](https://root-forum.cern.ch/t/loop-over-all-events-within-rdataframe/47599/1 "2021-11-09T16:21:57Z")

</div>

Hi, is it possible to loop over all events and show the “inserted event” in some sort of an interactive way.

I tried:

```auto
	auto all_events = df.Filter("EventNumber > 0 ");
	for i in all_events {}.... // not sure what to put here

```

---

<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:** [November 9, 2021, 4:56pm UTC](https://root-forum.cern.ch/t/loop-over-all-events-within-rdataframe/47599/2 "2021-11-09T16:56:12Z")

</div>

Hi @Karl007 ,  
you cannot write an explicit loop over events, but you can tell RDataFrame that you want to do something at every event:

```cpp
df.Foreach([]() { std::cout << "hello I'm in an event\n"; });

```

You can of course pass column values as input to `Foreach`. In particular if you pass the special column name `rdfentry_`, that is a unique event number identifier.

I hope this helps!  
Enrico

P.S.  
for debugging you can also put the printout in a Filter or Define expression:

```cpp
df.Filter("std::cout << EventNumber << '\n'; return true;")

```

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [November 9, 2021, 6:14pm UTC](https://root-forum.cern.ch/t/loop-over-all-events-within-rdataframe/47599/3 "2021-11-09T18:14:51Z")

</div>

Hi,

I am not entirely sure how to put an if statement in here. I am trying:

> [@eguiraud](#):
>
> `df.Foreach([](int i) { std::cout << i << std::endl;}, "{if inserted event is j, return something"; });`

Thanks!

---

<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:** [November 9, 2021, 6:20pm UTC](https://root-forum.cern.ch/t/loop-over-all-events-within-rdataframe/47599/4 "2021-11-09T18:20:11Z")

</div>

What is `something` in `return something`? I.e. what do you want to do _exactly_ when an event matches a certain condition?

P.S.  
the short answer is that if you write `df.Filter("..condition here..").Foreach(..)` the expression passed to `Foreach` is executed only for events satisfying the Filter condition, but there might be a better way to do what you want to do depending on what it is exactly.

---

<div class="post-metadata">

**Author:** ![Karl007](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/8491ac/32.png) [@Karl007](https://root-forum.cern.ch/u/Karl007)\
**Post date:** [November 10, 2021, 11:58am UTC](https://root-forum.cern.ch/t/loop-over-all-events-within-rdataframe/47599/5 "2021-11-10T11:58:29Z")

</div>

I would like to draw the chosen event. So somehow integrate allow “cin \>\> event\_number” and draw the inserted event. For example:

```auto
df.Foreach([](int i) { std::cout << i << std::endl;}, "{cin >> event_number, draw that event somehow"; });

```

---

<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:** [November 10, 2021, 1:27pm UTC](https://root-forum.cern.ch/t/loop-over-all-events-within-rdataframe/47599/6 "2021-11-10T13:27:06Z")

</div>

I see. Operating on a single, manually selected event is not really something that RDataFrame’s interfaces do well.

You might want to instaed open the TTree/TChain, set up the branches you need to read and call `GetEntry(entryNumber)` to retrieve just that event.

See the [tree tutorials](https://root.cern/doc/master/group __tutorial__ tree.html) or the [manual](https://root.cern/manual/trees/) for more info.

Cheers,  
Enrico
