How to merge RDataFrames or append to a TTree in a file with RDataFrames in pyROOT

ROOT Version: 6.26/10


Hi all,
I’m not sure if it is currently possible to do this, but I would like to be able to combine multiple RDataFrames into one, or at least write multiple RDataFrames into one file. eg for combining multiple TTrees into one:

snapshot_options = ROOT.RDF.RSnapshotOptions()
snapshot_options.fMode = "Update"

for tree in ['tree1', 'tree2', 'tree3']:
    Rdf = ROOT.RDataFrame(tree, 'myfile.root')
    Rdf.Snapshot("outTree", "out.root", ["branch1", "branch2"], snapshot_options)

This doesn’t work as it prompts me to make a new TTree instead of appending to the current TTree.

I’ve found a similar question without using dataframes: Append data to an existing TTree (with Branches)

Hi @kghorban ,

it’s not possible to merge RDataFrames.

What is possible is reading multiple trees into a single RDataFrame, as a vertical concatenation (a “chain”: same columns, concatenation of rows) or horizontal concatenation (a main tree plus “friends”: same number of rows, union of columns).

For example after your 3 Snapshots above you can read them in all trees in a chain with:

TChain c;
for t in ['tree1', 'tree2', 'tree3']:
  c.Add(f"myfile.root/{t}")
df= ROOT.RDataFrame(c)

Cheers,
Enrico

Hi thanks for the reply @eguiraud. I am used to using pandas dataframes in python and this really helped me reframe my thinking when it comes to RDataFrames