Redefinition error using ROOT.gInterpreter.Declare in python ROOTv6.24.00

Dear ROOT experts,

I’m trying to use RDataFrame in python to do nTuple processing. Whilst trying the ROOT::RDF::AsRNode helper in Python with the following code

import xgboost as xgb
import ROOT as r
import os
 
r.gInterpreter.Declare("""
ROOT::RDF::RNode OATransformation(ROOT::RDF::RNode dframe) {
    auto openingAngle = [](double px1, double py1, double pz1, double px2, double py2, double pz2){ return acos((px1*px2 + py1*py2 + pz1*pz2)/sqrt((px1*px1 + py1*py1 + pz1*pz1)*(px2*px2 + py2*py2 + pz2*pz2))); };
    return dframe.Define("H1H2_OA", openingAngle, {"H1_PX", "H1_PY", "H1_PZ", "H2_PX", "H2_PY", "H2_PZ"})
                 .Define("H2H3_OA", openingAngle, {"H2_PX", "H2_PY", "H2_PZ", "H3_PX", "H3_PY", "H3_PZ"})
                 .Define("H3H1_OA", openingAngle, {"H3_PX", "H3_PY", "H3_PZ", "H1_PX", "H1_PY", "H1_PZ"})
                 .Define("H1muminus_OA", openingAngle, {"H1_PX", "H1_PY", "H1_PZ", "muminus_PX", "muminus_PY", "muminus_PZ"})
                 .Define("H2muminus_OA", openingAngle, {"H2_PX", "H2_PY", "H2_PZ", "muminus_PX", "muminus_PY", "muminus_PZ"})
                 .Define("H3muminus_OA", openingAngle, {"H3_PX", "H3_PY", "H3_PZ", "muminus_PX", "muminus_PY", "muminus_PZ"})
                 .Define("H1muplus_OA", openingAngle, {"H1_PX", "H1_PY", "H1_PZ", "muplus_PX", "muplus_PY", "muplus_PZ"})
                 .Define("H2muplus_OA", openingAngle, {"H2_PX", "H2_PY", "H2_PZ", "muplus_PX", "muplus_PY", "muplus_PZ"})
                 .Define("H3muplus_OA", openingAngle, {"H3_PX", "H3_PY", "H3_PZ", "muplus_PX", "muplus_PY", "muplus_PZ"})
                 .Define("mumu_OA", openingAngle, {"muminus_PX", "muminus_PY", "muminus_PZ", "muplus_PX", "muplus_PY", "muplus_PZ"});
 }
""")

I get the following error instead

input_line_35:2:18: error: redefinition of 'OATransformation'
ROOT::RDF::RNode OATransformation(ROOT::RDF::RNode dframe) {
                 ^
input_line_34:2:18: note: previous definition is here
ROOT::RDF::RNode OATransformation(ROOT::RDF::RNode dframe) {

Thanks for your help.

My setup:
ROOT v6.24.00
Ubuntu 20.04
gcc 10.3.0
ROOT built from source on local setup

I just tried without error (on Windows, see below).

C:\Users\bellenot>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT as r
>>> import os
>>>
>>> r.gInterpreter.Declare("""
... ROOT::RDF::RNode OATransformation(ROOT::RDF::RNode dframe) {
...     auto openingAngle = [](double px1, double py1, double pz1, double px2, double py2, double pz2){ return acos((px1*px2 + py1*py2 + pz1*pz2)/sqrt((px1*px1 + py1*py1 + pz1*pz1)*(px2*px2 + py2*py2 + pz2*pz2))); };
...     return dframe.Define("H1H2_OA", openingAngle, {"H1_PX", "H1_PY", "H1_PZ", "H2_PX", "H2_PY", "H2_PZ"})
...                  .Define("H2H3_OA", openingAngle, {"H2_PX", "H2_PY", "H2_PZ", "H3_PX", "H3_PY", "H3_PZ"})
...                  .Define("H3H1_OA", openingAngle, {"H3_PX", "H3_PY", "H3_PZ", "H1_PX", "H1_PY", "H1_PZ"})
...                  .Define("H1muminus_OA", openingAngle, {"H1_PX", "H1_PY", "H1_PZ", "muminus_PX", "muminus_PY", "muminus_PZ"})
...                  .Define("H2muminus_OA", openingAngle, {"H2_PX", "H2_PY", "H2_PZ", "muminus_PX", "muminus_PY", "muminus_PZ"})
...                  .Define("H3muminus_OA", openingAngle, {"H3_PX", "H3_PY", "H3_PZ", "muminus_PX", "muminus_PY", "muminus_PZ"})
...                  .Define("H1muplus_OA", openingAngle, {"H1_PX", "H1_PY", "H1_PZ", "muplus_PX", "muplus_PY", "muplus_PZ"})
...                  .Define("H2muplus_OA", openingAngle, {"H2_PX", "H2_PY", "H2_PZ", "muplus_PX", "muplus_PY", "muplus_PZ"})
...                  .Define("H3muplus_OA", openingAngle, {"H3_PX", "H3_PY", "H3_PZ", "muplus_PX", "muplus_PY", "muplus_PZ"})
...                  .Define("mumu_OA", openingAngle, {"muminus_PX", "muminus_PY", "muminus_PZ", "muplus_PX", "muplus_PY", "muplus_PZ"});
... }
... """)
True
>>>

But maybe @etejedor or @eguiraud can give it a try?

Hi @Da_Yu_Tou ,
if you take that code, put it in a file (e.g. foo.py) and run it (e.g. with python foo.py), it should work just fine. If you are running that code in a Jupyter notebook, you cannot run the cell that declares your helper function twice (because C++ does not like redefinitions) – equivalently if you are running this at the Python prompt, you can only declare a certain function to the C++ interpreter once per session.

Otherwise please prepare a small repro.py file that we can run to reproduce the issue.

Cheers,
Enrico

1 Like

Thanks for your help. I dug a little further and the redefinition was just a very silly mistake on my side (I named my script xgboost.py and import xgboost as xgb at the start of my script).

Sorry for noise and thank you for trying to help.

Cheers,
Da Yu

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