How can I use TCutG in Pyroot with RDF?


Dear ROOT experts:
I want to use TCutG in pyroot with RDF. So I want to use
gInterpreter.Declare().

cuttheta_phicode = """
Bool_t cuttheta_phi(Double_t  dphi, Double_t fangle, TCutG cutg){
    Bool_t s{kFALSE};
    if (cutg.IsInside(dphi, fangle))
    {
      s = kTRUE;
    }
    return s;
  }
"""
ROOT.gInterpreter.Declare(cuttheta_phicode)

But if I define TCutG by Python

cutg_dp_ag = TCutG("cutg_dp_ag", 6)
cutg_dp_ag.SetPoint(0,122.834,171.266)
...

when I use RDF,

fdata.Filter("cuttheta_phi(dphi, fangle, cutg_dp_ag)" )

it shows

error: use of undeclared identifier ‘cutg_dp_ag’
auto lambda9 = (const double var0, const double var1){return cuttheta_phi(var0, var1, cutg_dp_ag)

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi @qyj ,

in the functions you pass to Filter, Define etc. the input parameters to the function should correspond to columns in the dataset (which is not the case for the cutg parameter in your example code, hence the error).

In order to use a TCutG object (that you define outside of the function) in the body of the function you need to capture that object. There are several ways to do this, but in all cases we need to make the C++ code cuttheta_phi aware of the TCutG object you define from Python.
The most straightforward way to do this (although not the least verbose) is using a helper functor (I haven’t tested the code but it should give you the idea):

cuttheta_phicode = """
// A functor class with an operator() that evaluates a TCutG.
class Cuttheta_phi {
  TCutG _cutg;

  Cuttheta_phi(const TCutG &cutg) : _cutg(cutg) {}

  bool operator()(double dphi, double fangle) {
    return cutg.IsInside(dphi, fangle);
  }
};
"""
ROOT.gInterpreter.Declare(cuttheta_phicode)

and then from Python:

cutg_dp_ag = TCutG("cutg_dp_ag", 6)
cutg_dp_ag.SetPoint(0,122.834,171.266)
cuttheta_functor = ROOT.Cuttheta_phi(cutg_dp_ag)
...
fdata = fdata.Filter(cuttheta_functor, ["dphi", "fangle"])

I hope this helps!
Enrico

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