# Opening TClonesArray from Delphes Branches with RDataFrame

**URL:** <https://root-forum.cern.ch/t/opening-tclonesarray-from-delphes-branches-with-rdataframe/39392>\
**Category:** ROOT\
**Tags:** tree, rdataframe\
**Created:** [May 13, 2020, 3:49pm UTC](https://root-forum.cern.ch/t/opening-tclonesarray-from-delphes-branches-with-rdataframe/39392 "2020-05-13T15:49:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jndrf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jndrf](https://root-forum.cern.ch/u/jndrf)\
**Post date:** [May 13, 2020, 3:49pm UTC](https://root-forum.cern.ch/t/opening-tclonesarray-from-delphes-branches-with-rdataframe/39392/1 "2020-05-13T15:49:54Z")

</div>

Hi all,

I’m trying to open a TTree created by Delphes with RDataFrames, remove events based on some cut and then write the events that pass the cut into a new TTree with the same format as the previous one.

The relevant part of the code is

```auto
#include <algorithm>

#include "classes/DelphesClasses.h"
#include <ROOT/RDataFrame.hxx>
#include <ROOT/RSnapshotOptions.hxx>
#include <TFile.h>
  auto low = ROOT::RDataFrame("Delphes", "inputfile.root")
    .Define("TruthLeptonPt", TruthLeptonPt, {"GenElectron", "GenMuon"})
    .Filter("TruthLeptonPt.size() >= 2")
    .Filter("TruthLeptonPt.at(1) >= 100");

  low.Snapshot("Delphes", "outputfile.root", low.GetColumnNames(),
	       // {"Event", "Electron", "Muon", "Jet"}, // try explicitly, first method doesn't seem to work
	       ROOT::RDF::RSnapshotOptions("CREATE", ROOT::kZLIB, 1, 0, 99, false));

```

wich references a function

```auto
std::vector<double> TruthLeptonPt(std::vector<Electron> el, std::vector<Muon> mu)
{
  std::vector<double> ret;
  ret.reserve(el.size() + mu.size());

  for (const auto& e : el) {
    ret.push_back(e.PT);
  }

  for (const auto& m : mu) {
    ret.push_back(m.PT);
  }

  std::sort(ret.begin(), ret.end(),
	    [] (const double& x, const double& y) {
	      return x > y;
	    }
	    );

  return ret;
}

```

that is supposed to populate the branch I want to apply my cut to. I’m not quite sure whether `GenParticle` or `Electron` is the correct type, I’ve tried both with the same result. I’ve seen

When running the above code, I get copious error messages that

```auto
Error in <TTreeReaderValueBase::CreateProxy()>: The branch GenElectron contains data of type TClonesArray. It cannot be accessed by a TTreeReaderValue<vector<Electron>>

```

I’ve tried the obvious thing and replaced `GenParticle` with `TClonesArray`, but there the compilation fails, because a `TClonesArray` doesn’t have the attributes I try to access. I get no errors about the `GenMuon` branch though.

How does one open and manipulate Delphes output with RDataFrames?

Cheers,

jndrf

* * *

_ROOT Version:_ 6.20/02  
_Platform:_ x86\_64-centos7-gcc9-opt  
_Compiler:_ g++ 9.2

* * *

---

<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 13, 2020, 4:17pm UTC](https://root-forum.cern.ch/t/opening-tclonesarray-from-delphes-branches-with-rdataframe/39392/2 "2020-05-13T16:17:04Z")

</div>

Hi @jndrf,  
I _think_ the problem is that TTreeReader, which RDataFrame uses to read ROOT files internally, needs to be taught how to deal with TClonesArray properly.

@pcanal or @Axel might be able to confirm/clarify, but I think this is [ROOT-7823](https://sft.its.cern.ch/jira/browse/ROOT-7823).

Would you be able to share a file (even with just a few events) that reproduces the issue to facilitate debugging/fixing it please?

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [May 13, 2020, 5:44pm UTC](https://root-forum.cern.ch/t/opening-tclonesarray-from-delphes-branches-with-rdataframe/39392/3 "2020-05-13T17:44:44Z")

</div>

ROOT-7823 is about the case of unsplit TClonesArray. The error message talks about vector (`TTreeReaderValue<vector<Electron>>`) so it _may_ be a type inference issue in RDF.

---

<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 13, 2020, 7:03pm UTC](https://root-forum.cern.ch/t/opening-tclonesarray-from-delphes-branches-with-rdataframe/39392/4 "2020-05-13T19:03:42Z")

</div>

@jndrf , @pcanal is right that one thing that you could try is to use `ROOT::RVec<Electron>` instead of `std::vector<Electron>`. That should switch `TTreeReaderValue<std::vector<Electron>>` to `TTreeReaderArray<Electron>` (it might or might not help, but it’s worth a try).

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![jndrf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@jndrf](https://root-forum.cern.ch/u/jndrf)\
**Post date:** [May 14, 2020, 8:02am UTC](https://root-forum.cern.ch/t/opening-tclonesarray-from-delphes-branches-with-rdataframe/39392/5 "2020-05-14T08:02:39Z")

</div>

Good morning @eguiraud @pcanal,

thank you for the quick replies, using `ROOT::VecOps::RVec<Electron>` instead of `std::vector<Electron>` solves this problems. I now run into problems when saving the output, but I’ll make a new thread for that.

Cheers,

Jonas

---

<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:** [May 28, 2020, 8:02am UTC](https://root-forum.cern.ch/t/opening-tclonesarray-from-delphes-branches-with-rdataframe/39392/6 "2020-05-28T08:02:48Z")

</div>

Closed on May 28, 2020, 8:02am UTC

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