# Subtracting bad events using RDataframe

**URL:** <https://root-forum.cern.ch/t/subtracting-bad-events-using-rdataframe/46008>\
**Category:** Newbie\
**Tags:** rdataframe\
**Created:** [July 26, 2021, 11:19am UTC](https://root-forum.cern.ch/t/subtracting-bad-events-using-rdataframe/46008 "2021-07-26T11:19:22Z")\
**Posts on this page:** 8\
**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:** [July 26, 2021, 11:19am UTC](https://root-forum.cern.ch/t/subtracting-bad-events-using-rdataframe/46008/1 "2021-07-26T11:19:23Z")

</div>

Hi, I would like to subtract some unwanted events to recreate a leaf using:

```auto
	auto new_events = df.Define("unwanted_events", "Take(myleaf1, myleaf1.size() > myleaf2.size())") // exclude these events
					    .Define("total_events", "myleaf1")
		   		        .Define("new_events", "total_events - unwanted_events"); // the problem is here

	auto h = new_events.Histo1D("new_events");
	h->DrawCopy();

```

I am getting the error:

Cannot call operator - on vectors of different sizes.

I understand that both vectors total\_events and unwanted\_events have different sizes, but what other way I can use to subtract them?

Thanks.

EDIT: example of data:  
 ![Capture](https://root-forum.cern.ch/uploads/default/original/3X/a/7/a7fd4b6a54f6bc8ecca26398f7df501b4791f1b9.png)

Notice event 4037. I want the whole event or preferably only particle [3] out.

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [July 26, 2021, 12:47pm UTC](https://root-forum.cern.ch/t/subtracting-bad-events-using-rdataframe/46008/2 "2021-07-26T12:47:22Z")

</div>

I’m not sure I understand the use of `Take()`. To me it looks like you’d like to just filter some events, like this

```
df.Define("isBadEvent", "myleaf1.size() > myleaf2.size()")
  .Filter("!isBadEvent")
```

---

<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:** [July 26, 2021, 1:18pm UTC](https://root-forum.cern.ch/t/subtracting-bad-events-using-rdataframe/46008/3 "2021-07-26T13:18:47Z")

</div>

Hi @jblomer

> [@jblomer](#):
>
> `I’m not sure I understand the use of Take()`

It’s to define the bad events then filter for them.

I tried:

```auto
auto c1 = df.Define("unwanted_events", "Take(myleaf1, myleaf1.size() > myleaf2.size())") // exclude these events
					    .Define("total_events", "myleaf1") //original leaf
		   		        .Filter("!unwanted_events") // filter bad events out

	auto h = c1.Histo1D("new_events"); // draw original leaf (bad events excluded)
	h->DrawCopy();

```

But I am getting an error:

```auto
error: no viable conversion from returned value of type 'RVec<unsigned int>' to
      function return type 'bool'

```

EDIT: Actually it’s one bad particle located in index [3] of some events. So if I can say somehow, exclude myleaf1[3] and return myleaf1, that would be it. I managed to define those indices using first code line, but I am so far unable to filter it out.

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [July 26, 2021, 2:14pm UTC](https://root-forum.cern.ch/t/subtracting-bad-events-using-rdataframe/46008/4 "2021-07-26T14:14:52Z")

</div>

If I understand correctly, you should be able to do something like this

```
df.Define("cleaned_myleaf1", "myleaf1[myleaf1 < 42]")

```

What this does is it creates a new vector column “cleaned\_myleaf1” that has only those elements from “myleaf1” for which the condition element \< 42 holds. Of course you’d need to adjust the condition `myleaf1 < 42` to whatever renders a particle as “good particle”.

---

<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:** [July 26, 2021, 2:22pm UTC](https://root-forum.cern.ch/t/subtracting-bad-events-using-rdataframe/46008/5 "2021-07-26T14:22:33Z")

</div>

Thanks for your answer.

Correct, and what this does is, defining for the first column, which is events (see photo above). What I would like to do is to control the index (column 2). So maybe something like:

```auto
df.Define("cleaned_myleaf1", "myleaf1[3].empty()")

```

So this should take out all index 3 and return column. But unfortunately it does not work.

I hope it is more clear now what I want to do 🙂

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [July 26, 2021, 5:09pm UTC](https://root-forum.cern.ch/t/subtracting-bad-events-using-rdataframe/46008/6 "2021-07-26T17:09:23Z")

</div>

Oh, now I also understand the use of `Take`. So that should work

```
df.Define("cleaned_myleaf1", "Take(myleaf1, myleaf2.size())");

```

I.e. cleaned\_myleaf1 takes only the first elements of myleaf1, where “the first” are as many as the size of myleaf2.

---

<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:** [July 27, 2021, 7:30am UTC](https://root-forum.cern.ch/t/subtracting-bad-events-using-rdataframe/46008/7 "2021-07-27T07:30:18Z")

</div>

Actually it is my fault. I was defining bad events. Instead, I could define good events and see what to do with them.

Apologies and thanks for your help.

---

<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:** [August 10, 2021, 7:30am UTC](https://root-forum.cern.ch/t/subtracting-bad-events-using-rdataframe/46008/8 "2021-08-10T07:30:22Z")

</div>

Closed on August 10, 2021, 7:30am UTC

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