# Generate root file event by event using RDataFrame for unknown number of events

**URL:** <https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362>\
**Category:** Newbie\
**Created:** [March 25, 2022, 8:31pm UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362 "2022-03-25T20:31:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![brencic](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/brencic/32/29280_2.png) [@brencic](https://root-forum.cern.ch/u/brencic)\
**Post date:** [March 25, 2022, 8:31pm UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362/1 "2022-03-25T20:31:07Z")

</div>

Hey

I have to generate a root tree and **don’t know how many events I will have to store at compile time**. So I’m forced to add to the tree event, by event, or every n events.

I know how to do this in the “old ROOT”, but is it possible to generate `trees` event by event with `RDataFrame` as well?

Found this tutorial: [ROOT: tutorials/dataframe/df002\_dataModel.C File Reference](https://root.cern.ch/doc/master/df002__dataModel_8C.html)

```auto
...
 ROOT::RDataFrame d(64);
 d.Define("tracks", genTracks)

```

But this is to generate `root trees` with `RDataFrame` when one knows how many event’s there will be.

So how can one do sth. along those lines:

```auto
ROOT::RDataFrame d(file_path + "/" + file_name) ;

while flag == True{
     d.add_event(event);
}

```

with `RDataFrame`;

Thanks!

* * *

_ROOT Version:_ Latest

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 28, 2022, 6:48am UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362/2 "2022-03-28T06:48:01Z")

</div>

I am not sure to understand your question, because, by definition, the number of event to be stored in a tree is not known in advance. Tree are filled “event by event”. Using `TTree` or `RDataFrame` is the same: the number of events (ie the number of line) in a tree is not fixed “at compile time”. Did you mean the number of variables (ie the number of columns)?

---

<div class="post-metadata">

**Author:** ![brencic](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/brencic/32/29280_2.png) [@brencic](https://root-forum.cern.ch/u/brencic)\
**Post date:** [March 28, 2022, 7:08am UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362/3 "2022-03-28T07:08:26Z")

</div>

No I meant number of events.

OK so if adding event’s to `TTree` is the same as in `RDataFrame` then:

How would one add arbitrary number of the events (one by one) with `RDataFrame` formalism to a root tree?

_Bit off back story_: I have a binary file in non-standard encoding that has to be converted to ROOT trees. In the past we used `tree->Fill` for that, but we would like to switch to the `RDataFrames` if it’s possible.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 28, 2022, 7:10am UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362/4 "2022-03-28T07:10:36Z")

</div>

> [@brencic](#):
>
> How would one add arbitrary number of the events (one by one) with `RDataFrame` formalism to a root tree?

I think @eguiraud can help you.

---

<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 28, 2022, 9:38am UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362/5 "2022-03-28T09:38:15Z")

</div>

Hi @brencic ,  
I am afraid there is currently no easy way to do that with RDataFrame (feel free to open a feature request at [Issues · root-project/root · GitHub](https://github.com/root-project/root/issues)).  
You can of course produce a batch of entries at a time, and do that in a loop as needed, but I realize it’s not exactly what you are asking for.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![brencic](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/brencic/32/29280_2.png) [@brencic](https://root-forum.cern.ch/u/brencic)\
**Post date:** [March 28, 2022, 9:45am UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362/6 "2022-03-28T09:45:02Z")

</div>

Aha 🙂 Tnx.

1.) So what would be the most “optimal” way to do the batch of entries? Do you have an example in mind?

2.) I saw in the examples that there’s a way to auto load [CSV to RDataFrame](https://root.cern.ch/doc/master/df014__CSVDataSource_8C.html). In that case software I guess reads the CSV file line by line (entry by entry) while doing the conversion or am I missing sth.?

---

<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 28, 2022, 9:53am UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362/7 "2022-03-28T09:53:44Z")

</div>

For 1., something like:

```cpp
void produce_batch() {
  ROOT::RDataFrame(N).Define(...).Snapshot(...);
}

while (some_condition) {
  produce_batch();
}

```

where each `Snapshot` writes a new TTree (all trees can be in the same file if they have different names and you tell Snapshot to open the file with mode “UPDATE”, or you can write each tree in a different file). Then you can use all the trees in a chain or merge them together with `hadd` as a final step.

1. 

Good catch, that’s what I meant when I said there is no _easy_ way 😬 you _can_ implement an [RDataSource](https://root.cern/doc/master/classROOT_1_1RDF_1_1RDataSource.html) that tells RDF to process new (empty) events until you got enough. You can use [ROOT: tree/dataframe/inc/ROOT/RTrivialDS.hxx Source File](https://root.cern/doc/master/RTrivialDS_8hxx_source.html#l00024) as a starting point. Basically RDF will keep asking for “entry ranges” to the RDataSource via the `GetEntryRanges` method until it returns an empty vector. So you can implement `YourDataSource::GetEntryRanges` as something that returns non-empty ranges until some condition is satisfied. This plugs into the usual RDF machinery, including multi-threaded Snapshots.

---

<div class="post-metadata">

**Author:** ![brencic](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/brencic/32/29280_2.png) [@brencic](https://root-forum.cern.ch/u/brencic)\
**Post date:** [March 28, 2022, 10:28am UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362/8 "2022-03-28T10:28:33Z")

</div>

1.) OK yeah little wired hack.

2.) Well that’s doesn’t sound too bad 🙂

- So basically one has to define a “custom” `ROOT::RDF::RDataSource` type object and create a method of type `ROOT::RDF::Make(file_format)DataFrame`.
- Write the line by line parser and call it in the constructor (as [here](https://root.cern.ch/doc/master/RCsvDS_8cxx_source.html#l00262))
- Specify custom `::GetEntryRanges()` and custom `::SetEntry()`.
- Anything else to watch for?

I think that I get the basic idea, but if you have any more tips, please let me know.

---

<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 28, 2022, 11:46am UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362/9 "2022-03-28T11:46:43Z")

</div>

I’m not sure what line parsing you might need, given your example in the first post the constructor of your RDataSource only needs to take whatever arguments are required to evaluate `flag`.

For everything else the RTrivialDS and the related tutorials should provide a reasonable starting point.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![brencic](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/brencic/32/29280_2.png) [@brencic](https://root-forum.cern.ch/u/brencic)\
**Post date:** [March 28, 2022, 2:01pm UTC](https://root-forum.cern.ch/t/generate-root-file-event-by-event-using-rdataframe-for-unknown-number-of-events/49362/10 "2022-03-28T14:01:04Z")

</div>

Alright. Tnx for the help.

Will circle back if I get stuck 🙂
