Hi. I need to transform a python dictionary into a TTree. So far I’ve been using RDataFrames. I can do it succesfully with simple data structures (dict1D) via MakeNumpyDataFrame. But it fails when the data contains collections (dictND). I tried two (unsuccesful) methods based on this and this example.
Below I provide some simplified data with the same structure as my real case.
# Dummy data
import numpy as np
dict1D = {'energy': np.array([11.,22.,33.]),
'momentum': np.array([10.,20.,30.]),
'nparticles': np.array([5.,15.,25.])}
dictND = {'position': np.array([list([1.,1.,2.]),list([2.,2.,3.]),list([4.,4.,5.])], dtype=object),
'otherparticles': np.array([list([22.,33.]),list([44.,66.,77.,88.]),list([11.,99.])], dtype=object)}
# RDataframe with simple branches
import ROOT
rdf = ROOT.RDF.MakeNumpyDataFrame(dict1D)
# - - - - - - - - - - #
# Branches with arrays (FAILS)
#1st try
rdf = rdf.Define("position", '''auto to_eval = "dictND['position'][" + std::to_string(rdfentry_) + "]"; return RVec<float>(TPython::Eval(to_eval.c_str()));''')
#2nd try
@ROOT.Numba.Declare(['int'], 'RVec<float>')
def func(var1):
return dictND['position'][var1]
rdf = rdf.Define("position", "Numba::func(rdfentry_)")
# - - - - - - - - - - #
rdf.Snapshot('DecayTree', 'mytree.root')
ROOT 6.24