RDataFrame: save Snapshot with arrays

I follow the examples in df102_NanoAODDimuonAnalysis.py and f007_snapshot.py, and I try to save a snapshot before making the plots keeping only di-muon mass and muon pt

import ROOT
df = ROOT.RDataFrame("Events", "root://eospublic.cern.ch//eos/opendata/cms/derived-data/AOD2NanoAODOutreachTool/Run2012BC_DoubleMuParked_Muons.root")
df_sel= df.Filter("nMuon == 2", "Events with exactly two muons").Filter("Muon_charge[0] != Muon_charge[1]", "Muons with opposite charge")
df_mass = df_sel.Define("Dimuon_mass", "std::sqrt(2*Muon_pt[0]*Muon_pt[1]*(std::cosh(Muon_eta[0]-Muon_eta[1])-std::cos(Muon_phi[0]-Muon_phi[1])))")

#select branches
branchList = ROOT.vector('string')()
for attr in ['pt']:
    branchList.push_back('Muon_{}'.format(attr))
branchList.push_back('Dimuon_mass')

# skim samples before running the analysis:
df_mass.Snapshot('Events','./df_dimu.root',branchList)

Error in <TBranch::TBranch>: Illegal leaf: Muon_pt/Muon_pt[nMuon]/F If this is a variable size C array it’s possible that the branch holding the size is not available.

How I can save arrays

PS: Can I save all arrays from the pattern Muon_*? (relevant for other datasets with more branches)


ROOT Version: 6.14/09
Platform: centos7
Compiler: gcc7


Our RDataFrame expert, @eguiraud is on vacation, but maybe @etejedor can help

@eguiraud solved my problem, thanks!

Before passing arrays to the list of branches, one needs to pass the branch that contains the size of the array. In my case, it will be:

#select branches
branchList = ROOT.vector('string')()
branchList.push_back('nMuon') #needs to appear before any branches whose size depends on it
for attr in ['pt']:
    branchList.push_back('Muon_{}'.format(attr))
branchList.push_back('Dimuon_mass')

For my second question, the answers was already given by @eguiraud in 38705, so the solution will be:

df_mass.Snapshot('Events','./df_dimu.root','(^nMuon$|^.*Muon_.*$)')
1 Like

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