# Writing a TTree with a complex structure (e.g. Delphes TTrees) using RDataFrames

**URL:** <https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426>\
**Category:** ROOT\
**Tags:** tree, rdataframe\
**Created:** [May 15, 2020, 10:55am UTC](https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426 "2020-05-15T10:55:58Z")\
**Posts on this page:** 9\
**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 15, 2020, 10:55am UTC](https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426/1 "2020-05-15T10:55:59Z")

</div>

Hi all,

I want to open a TTree created by Delphes with RDataFrames, remove events based on some cuts and then write the remaining events to a new TTree that has exactly the same format as the previous one. The first part works now thanks to [this thread](https://root-forum.cern.ch/t/opening-tclonesarray-from-delphes-branches-with-rdataframe/39392), but now I run into troubles saving.

When I process `delphestestsample.root` with the below program, the outputfile doesn’t have the same structure – in particular, I’d expect Jet to have sub-branches, see the attached picture. The in- and output files are also attached.

I’ve also tried to simply write all columns using `df.GetColumnNames()`, but then I run into the type issues I had in the linked thread.

How does one write a TTree with a complex structure such as the ones created by Delphes to disk with RDataFrames?

Cheers,

Jonas

```auto
#include <classes/DelphesClasses.h>
#include <ROOT/RDataFrame.hxx>
#include <ROOT/RSnapshotOptions.hxx>
#include <ROOT/RVec.hxx>
#include <TFile.h>

bool JetPtCut(ROOT::VecOps::RVec<Jet> jets)
{
  if (jets.size() > 1 && jets.at(1).PT > 100) {
    return true;
  }

  return false;
}

int main(int argc, char* argv[])
{
  auto df = ROOT::RDataFrame("Delphes", "delphestestsample.root")
    .Filter(JetPtCut, {"Jet"});

  df.Snapshot<ROOT::VecOps::RVec<LHEFEvent>, ROOT::VecOps::RVec<Electron>, ROOT::VecOps::RVec<Muon>, ROOT::VecOps::RVec<Jet>>
    ("Delphes", "reducedsample.root", //df.GetColumnNames(),
     {"Event", "Electron", "Muon", "Jet"}, // try explicitly, first method doesn't seem to work
     ROOT::RDF::RSnapshotOptions("RECreate", ROOT::kZLIB, 1, 0, 99, false));

  return 0;
}

```

_ROOT Version:_ 6.20.04  
_Platform:_ Linux 5.6.11-200.fc31.x86\_64  
_Compiler:_ g++ 9.3.1

* * *

 ![grafik](https://root-forum.cern.ch/uploads/default/original/3X/a/4/a4a2c465e2bb9de81b16c244e609d7e4f6e41d8e.png) [reducedsample.root](https://root-forum.cern.ch/uploads/short-url/56ApxPNxq1bZAO6iGmzy2usKl5a.root) (5.9 KB) [DelphesCutter.cxx](https://root-forum.cern.ch/uploads/short-url/bUWA3ocAqXAVGrJFZdfkxOMOIeq.cxx) (793 Bytes) [delphestestsample.root](https://root-forum.cern.ch/uploads/short-url/k2NlfayXhZyJK8OOZEtutVsPLzd.root) (249.8 KB)

---

<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 15, 2020, 12:05pm UTC](https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426/2 "2020-05-15T12:05:30Z")

</div>

Hi @jndrf,  
thanks for the thorough report with a file that we can use for debugging. If I understand correctly, the problem is that in the input file `Jet` has a series of sub-branches, but in the output file it somehow became just a leaf (similarly for other branches).

I will take a look as soon as possible, probably over the course of next week.  
Cheers,  
Enrico

---

<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 22, 2020, 1:53pm UTC](https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426/3 "2020-05-22T13:53:23Z")

</div>

Hi,  
sorry for the delay, I am having some difficulties setting up Delphes to reproduce the problem locally, I’ll get back to you as soon as possible.

Cheers,  
Enrico

---

<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 25, 2020, 7:26am UTC](https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426/4 "2020-05-25T07:26:57Z")

</div>

Hello,  
the problem is that TClonesArray are not well supported. I will try to improve the situation (certainly we must not silently write wrong data) but, in the meanwhile, it seems that telling Snapshot that these branches are in fact TClonesArrays (rather than the catch-all RVec) solves the problem:

```c++
#include <classes/DelphesClasses.h>
#include <TClonesArray.h>
#include <ROOT/RVec.hxx>
#include <TFile.h>
#include <ROOT/RDataFrame.hxx>

bool JetPtCut(ROOT::VecOps::RVec<Jet> jets)
{
  if (jets.size() > 1 && jets.at(1).PT > 100) {
    return true;
  }

  return false;
}

int main(int argc, char* argv[])
{
  auto df = ROOT::RDataFrame("Delphes", "delphestestsample.root")
    .Filter(JetPtCut, {"Jet"});

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

  return 0;
}

```

It would be great if you could confirm this works.

**EDIT:** in particular, please check whether the values written out are correct. work in progress…

Cheers,  
Enrico

---

<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 25, 2020, 7:34am UTC](https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426/5 "2020-05-25T07:34:06Z")

</div>

P.S.  
this is now [https://sft.its.cern.ch/jira/browse/ROOT-10792](https://sft.its.cern.ch/jira/browse/ROOT-10792)

---

<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 25, 2020, 11:22am UTC](https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426/6 "2020-05-25T11:22:13Z")

</div>

Hi again,  
if I understood the problem correctly, you have 3 options (1 and 2 are currently broken in different ways, but fixed by the PR I link below):

- call `Snapshot<RVec<Jet>>(..., {"Jet"})`: this tells `Snapshot` to read `Jet` as an `RVec` rather than a `TClonesArray`. At this point, we cannot easily write it out as a `TClonesArray`, but instead we would write a `std::vector<Jet>`. You will require dictionaries for `std::vector<Jet>`
- call `Snapshot(..., {"Jet"})`: this should just work and write out a TClonesArray with the fix linked below
- call `Snapshot<TClonesArray>(..., {"Jet"})`: this should already work, and it will keep working

Does this sound reasonable? If yes, is there any chance you can try whether [this PR](https://github.com/root-project/root/pull/5693) fixes your issue?

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 25, 2020, 11:58am UTC](https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426/7 "2020-05-25T11:58:33Z")

</div>

Hi Enrico,

thank you for the reply. I am using the last method from your list for now, this works.  
On a quick glance, the contents of the file look sensible (at least the cut is applied correctly), but I haven’t made a thorough check.

I don’t have a self compiled version of ROOT at hand right now, but I think I can get around to try the PR this week.

Cheers,

Jonas

---

<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 28, 2020, 5:36pm UTC](https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426/8 "2020-05-28T17:36:20Z")

</div>

Hi Jonas,  
just so you know, the next release v6.22 and the next v6.20/06 patch release will contain the PR that I linked above, which makes `Snapshot(..., {"Jet"})` work out of the box and `Snapshot<RVec<Jet>>(..., {"Jet"})` either work (if dictionaries are present) or error out noisily with a (hopefully) helpful error message. Note that `Snapshot<RVec<Jet>>(..., {"Jet"})` will write out a `vector<Jet>`, while the other two methods will write out `TClonesArray`s.

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:** [June 11, 2020, 5:36pm UTC](https://root-forum.cern.ch/t/writing-a-ttree-with-a-complex-structure-e-g-delphes-ttrees-using-rdataframes/39426/9 "2020-06-11T17:36:25Z")

</div>

Closed on June 11, 2020, 5:36pm UTC

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