# Filtering out part of event in RDataFrame

**URL:** <https://root-forum.cern.ch/t/filtering-out-part-of-event-in-rdataframe/39489>\
**Category:** Newbie\
**Tags:** rdataframe\
**Created:** [May 19, 2020, 12:52pm UTC](https://root-forum.cern.ch/t/filtering-out-part-of-event-in-rdataframe/39489 "2020-05-19T12:52:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yunu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/b5ac83/32.png) [@yunu](https://root-forum.cern.ch/u/yunu)\
**Post date:** [May 19, 2020, 12:52pm UTC](https://root-forum.cern.ch/t/filtering-out-part-of-event-in-rdataframe/39489/1 "2020-05-19T12:52:35Z")

</div>

Hello,

My problem is next, I have a TTree with next Branches : `Mult, pID[Mult], pEnergy[Mult], pTime[Mult], nID[Mult], nEnergy[Mult], nTime[Mult]`. I have defined TCutG `EnergyCut` and would like to keep `i` parts of arrays per event where `EnergyCut->IsInside(pEnergy[i], nEnergy[i])` and save the result into new TTree.

I could achieve my goal just by manually looping through my input Tree and selecting only parts that satisfy the condition. But since I am new to ROOT, I would like to ask what is a good practice for such a procedure? After reading about RDataFrame I had an idea that I could possibly create a custom filter, following another [topic](https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865/5) , but I am not sure how to proceed or if it is possible at all :

```auto
 ROOT::RDataFrame df("PermutationTree", "generatedTree_with_permutations.root");

 using doubles = ROOT::VecOps::RVec<Double_t>;

 auto cutInside = [](const doubles& pside, const doubles& nside, int n)
 {
     for(auto i = 0; i < n; ++i)
        if EnergyCut->IsInside(pside[i], nside[i])
          return ????
 };
 df.Filter(cutInside, {"pEnergy", "nEnergy", "Mult"}).Snapshot("filteredTree", "output.root");

```

Since I will be using ROOT a lot in the future and also gonna work with large TTrees, I just want to see what is a good practice for complicated cuts and filters: doing manual selection during looping or using RDataFrame with Filters?

Thanks a lot.

Best regards,  
Yuliia

---

<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:** [May 19, 2020, 1:13pm UTC](https://root-forum.cern.ch/t/filtering-out-part-of-event-in-rdataframe/39489/2 "2020-05-19T13:13:04Z")

</div>

Hi Yuliia,  
RDataFrame is definitely a good choice for complicated cuts on large TTrees. I suggest you take a look at the “crash course” [here](https://root.cern.ch/doc/master/classROOT_1_1RDataFrame.html).

About your problem: `Filter` selects _rows_/events of the dataset that satisfy a certain condition. In your case, if I understand correctly, _for each row_/event you want to `Define` a new array that contains only some elements of the original array:

```c++
auto pside_selection = [](const doubles& pside, const doubles& nside, int n)
{
     doubles selected_pside;
     for(auto i = 0; i < n; ++i)
        if EnergyCut->IsInside(pside[i], nside[i])
           selected_pside.push_back(pside);
     return selected_pside;
}

df.Define("selected_pside", pside_selection, {"pEnetry", "nEnergy", "Mult"}).Snapshot(...);

```

This might not be _exactly_ what you need but I hope it gives you an idea of what you can do.  
Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![yunu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/b5ac83/32.png) [@yunu](https://root-forum.cern.ch/u/yunu)\
**Post date:** [May 20, 2020, 10:09am UTC](https://root-forum.cern.ch/t/filtering-out-part-of-event-in-rdataframe/39489/3 "2020-05-20T10:09:08Z")

</div>

@eguiraud Thanks a lot for your reply, that’s exactly what I needed!

I expanded your code to fully cover my needs (might be useful for someone):

```auto
  using doubles = ROOT::VecOps::RVec<Double_t>;
  auto selection = [&](const doubles& data, const doubles& cond1, const doubles& cond2, const Int_t n)
  {
          std::vector<double> selectedPart;
          for(Int_t i = 0; i < n; ++i)
          {
            if ( energyCut->IsInside(cond1[i], cond2[i]) ) // check if within the Cut
              selectedPart.push_back(data[i]);
          }
          return selectedPart;
  };

  auto selectedData = df.Define("selected_pEnergy", selection, {"pEnergy", "pEnergy", "nEnergy", "Mult"})
                        .Define("selected_nEnergy", selection, {"nEnergy", "pEnergy", "nEnergy", "Mult"})
                        .Define("selected_pTime", selection, {"pTime", "pEnergy", "nEnergy", "Mult"})
                        .Define("selected_nTime", selection, {"nTime", "pEnergy", "nEnergy", "Mult"});

  // Save columns with selected data into TTree
  selectedData.Snapshot("myNewTree", "newfile.root", {"selected_pEnergy", "selected_nEnergy", "selected_pTime", "selected_nTime"});

```

Now I can think on improving my selection function, but I totally got the idea on how to work with RDataFrame, so thanks a lot!

Best regards,  
Yuliia
