Define new column with a function of non-column parameters in RDF

I’m processing data with a RDF framework using PyROOT. I need to have a Define using a function of a preexisting column and an external ROOT object (a TSpline3) doing something like that:

reweight_code = '''
   double Func(TSpline3 spline, Double_t oldCol){
       return spline.Eval(oldCol);

file= ROOT.TFile.Open("externalFile.root")
splineObj= file.Get("spline")
d = d.Define('NewColName', 'Func(splineObj,oldCol)')

It is not working because in the Define root is not seeing splineObj, probably the sintax is not correct: how can I give to a function of the Define a non-column parameter like that?
A workaround solution is to open the ROOT file and do the Get("spline") inside the C++ function (and it works), but I prefer to avoid that if it is possible (it is not clear to me how many time the ROOT file will be opened in this case).
The use of C++ code for Func is not needed, if there are easier solution are welcome.

Thank you very much in advance.

ROOT Version: /cvmfs/
Platform: CentOS Linux release 7.4.1708 (Core)
Compiler: gcc version 6.2.0 (GCC

1 Like

I think @etejedor will be able to help

Hi Valerio,
the trick is making splineObj known to the C++ interpreter as a global variable. At that point you can just use it within the C++ functions that you pass to RDF. Something like this should work (with thanks to @swunsch):

import ROOT

f = ROOT.TFile.Open("f.root")
splineObj = f.Get("myspline")
ROOT.gInterpreter.Declare("""TSpline3 *cppspline;""")
# assign the python splineObj to cppspline, which is accessible from both worlds
ROOT.cppspline = splineObj

ROOT.RDataFrame(10).Define("x", "cppspline->Eval(42)")...

Once per event, if I understand correctly – definitely not what you want! :smile:

One related issue might be that when running with multiple threads spline.Eval might be called concurrently – and I have no idea whether that’s safe or not.



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