Saving two trees into same file with RDataFrame Snapshot

Dear experts,

I am working in pyroot and I would like to save two trees into the same output root file with Snapshot. What I do is:

import ROOT as R

in_file = “input.root” 
out_file = "output.root" 

options = R.RDF.RSnapshotOptions() 
options.fMode = "RECREATE"

rdf = R.RDataFrame(“tree1”, input_file)

# define some columns on rdf

saveCols = [ str(i) for i in rdf.GetColumNames() ] 
rdf.Snapshot(“tree1, out_file, saveCols, options)


mcdt = R.RDataFrame( “tree2”, input_file )

# some defines

saveCols = [str(i) for i in mcdt.GetColumnNames()]  
options.fMode = "UPDATE"
mcdt.Snapshot(“tree”2, out_file, saveCols, options)

What I get doing this is that in out_file both trees appear but tree1 is missing those columns which do not share names with columns from tree2. When i remove the part of saving tree2, all the columns of tree1 are saved in out_file. How can I fix this behaviour?

Cheers,
Lidia.

Hi Lidia,

Thanks for the reproducer. I am sorry to read this did not work out of the box for you.
Before diving in the debugging, to unblock you, I can suggest to create and then merge two files with hadd - this is not a fix, but a workaround.
Then, could you share with us the ROOT version you are using while we try to reproduce the behaviour you see?

Best,
D

Hi Danilo,

I am working with ROOT Version 6.18/04

Best,
Lidia.

Dear @lidia ,

Thank you for reaching out to the forum! I have prepared a small snippet that should exemplify the situation you describe

import ROOT

ROOT.RDataFrame(10).Define("x", "1").Snapshot("tree", "input.root")

ROOT.RDataFrame("tree","input.root").Define("y","2").Snapshot("tree1", "output.root")

opts = ROOT.RDF.RSnapshotOptions()
opts.fMode = "UPDATE"

ROOT.RDataFrame("tree", "input.root").Define("z", "3").Snapshot("tree2", "output.root", ["x", "z"], opts)

I get the output.root file containing both tree1 and tree2. Both trees have the correct columns stored in it.

% rootls -t output.root
TTree  Jan 31 11:13 2025 tree1;1 "tree1" 
  y  "y/I"  109
  x  "x/I"  109
  Cluster INCLUSIVE ranges:
   - # 0: [0, 9]
  The total number of clusters is 1
TTree  Jan 31 11:13 2025 tree2;1 "tree2" 
  x  "x/I"  109
  z  "z/I"  109
  Cluster INCLUSIVE ranges:
   - # 0: [0, 9]
  The total number of clusters is 1

I am using the latest version of ROOT. So I believe that the behaviour you mention has meanwhile been fixed. Can you check if my snippet gives a different result for you? And in any case, could you test your code with a more recent version of ROOT to confirm that fixes the problem?

Cheers,
Vincenzo

Dear @vpadulan,

Sorry for the late reply. Your solution works perfectly with ROOT 6.18, and mine too! The problem was that I was exploring the output tree with the root file viewer extension of vscode, which does not show all the branches. I realised exploring the output tree directly with root that both examples behave as they should.

Sorry for the mess!

Best,
Lidia.

1 Like

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