# Event mixing with RDataFrame

**URL:** <https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009>\
**Category:** ROOT\
**Tags:** rdataframe\
**Created:** [December 9, 2021, 11:47am UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009 "2021-12-09T11:47:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuel1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/9fc348/32.png) [@Samuel1](https://root-forum.cern.ch/u/Samuel1)\
**Post date:** [December 9, 2021, 11:47am UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/1 "2021-12-09T11:47:43Z")

</div>

Hi all,

I am currently discovering the RDataFrame tool, and I was wondering if mixing events are possible. I couldn’t see anything close to it in the different tutorials.  
Let´s say I have a Tree with a Jpsi and its muons for each entry, how can I get something like the difference of the pseudorapidity of the j/psi in entry 1 and the pseudorapidity of the j/psi in entry 2 (or any other uncorrelated entry).  
Maybe it is better to use plain ROOT for this kind of thing but if anybody knows something let me know !  
Cheers,  
Samuel

---

<div class="post-metadata">

**Author:** ![imanol097](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/imanol097/32/28137_2.png) [@imanol097](https://root-forum.cern.ch/u/imanol097)\
**Post date:** [December 9, 2021, 1:33pm UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/2 "2021-12-09T13:33:27Z")

</div>

I don’t have any idea, maybe @eguiraud can help you.

---

<div class="post-metadata">

**Author:** ![nmangane](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nmangane/32/23112_2.png) [@nmangane](https://root-forum.cern.ch/u/nmangane)\
**Post date:** [December 9, 2021, 3:28pm UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/3 "2021-12-09T15:28:16Z")

</div>

This might be of _some_ relevance (“TimeFrame”):  
[Using ROOT in high-frequency finance](https://indico.cern.ch/event/855454/contributions/4597414/attachments/2356540/4021591/Using%20ROOT%20in%20high-frequency%20finance.pdf)

Here, correlations between events in time (naturally in separate ‘entries’) need to be analyzed together.

---

<div class="post-metadata">

**Author:** ![Samuel1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/9fc348/32.png) [@Samuel1](https://root-forum.cern.ch/u/Samuel1)\
**Post date:** [December 10, 2021, 9:35am UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/4 "2021-12-10T09:35:35Z")

</div>

Hi @nmangane, I think I understood why it might be of interest but it is not clear now how to use it. Did you ever try this ? Is it like an extension of RDataFrame or a totally new independent class ?  
Sam

---

<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:** [December 13, 2021, 9:45am UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/5 "2021-12-13T09:45:15Z")

</div>

Hi @Samuel1 ,  
sorry for the high latency, I was off last week.  
The most tricky part about processing entries with a sliding window is multi-threading – each entry will process a bunch of entries at a time, and the first and last entries in the bunch will not have a previous/next entry so you’ll miss some statistics.

If you are happy with single-thread processing, or you don’t mind losing some of the pairs of consecutive entries, you can use a stateful functor + RDataFrame, something like (haven’t tested, it’s just to give you an idea):

```cpp

// not thread-safe, but can easily be made thread-safe by using a vector of
// previousPseudoRapidity values, one per thread (df.GetNSlots() returns the
// the required number of threads/processing slots).
struct PseudoRapidityDiff {
  double previousPseudoRapidity = 1e20;

  double operator()(double pseudoRapidity) {
     if (previousPseudoRapidity > 1e19) // no previous value
        return -999;
     double diff = pseudoRapidity - previousPseudoRapidity;
     previousPseudoRapidity = pseudoRapidity;
     return diff;
  }
};

// only safe in single-thread processing
df.Define("pseudoRapDiff", PseudoRapidityDiff{}, {"pseudoRapidity"});

```

I hope this helps!  
Enrico

P.S.  
I am not familiar with `TimeFrame` personally, but my understanding is that it is an independent class that reuses some RDF concepts but natively works with sliding windows. @Axel will know more.

---

<div class="post-metadata">

**Author:** ![Samuel1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/9fc348/32.png) [@Samuel1](https://root-forum.cern.ch/u/Samuel1)\
**Post date:** [December 13, 2021, 11:03am UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/6 "2021-12-13T11:03:07Z")

</div>

Hi @eguiraud,

Thanks a lot for taking the time to reply. Ok I understand, my point was to use the multi threading as I am processing a lots of data. I am actually pairing the pseudorapidity of my jpsi with all the hadrons of the next event (ideally a selected independent event) so it is quite long! I’ll try anyway thanks !  
Samuel

---

<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:** [December 13, 2021, 11:09am UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/7 "2021-12-13T11:09:35Z")

</div>

> [@Samuel1](#):
>
> my point was to use the multi threading as I am processing a lots of data.

I completely understand, and then you need to decide what to do with those boundaries at which RDataFrame splits the dataset for multi-thread processing, I don’t think it’s clear in general.

---

<div class="post-metadata">

**Author:** ![Samuel1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/9fc348/32.png) [@Samuel1](https://root-forum.cern.ch/u/Samuel1)\
**Post date:** [December 13, 2021, 11:12am UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/8 "2021-12-13T11:12:41Z")

</div>

Is it possible to get the entry number of the last entry of one bunch of data ? So I can add something like “if last take first”. That would fix it I think.

---

<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:** [December 13, 2021, 11:29am UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/9 "2021-12-13T11:29:51Z")

</div>

Yes but only in ROOT master and the upcoming release v6.26: you need [`DefinePerSample`](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a29d77593e95c0f84e359a802e6836a0e). The expression you pass to `DefinePerSample` takes a [RSampleInfo](https://root.cern/doc/master/classROOT_1_1RDF_1_1RSampleInfo.html) object as input that can tell you the entry range that is going to be processed.

Cheers,  
Enrico

---

<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:** [December 27, 2021, 11:30am UTC](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/10 "2021-12-27T11:30:39Z")

</div>

Closed on December 27, 2021, 11:30am UTC

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