Loop over all events within RDataFrame

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

I tried:

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

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

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!

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

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


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


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

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.

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

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

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 or the manual for more info.