Functor in RDataFrame and PyROOT


ROOT Version:v6.26/00
Platform: centos7
Compiler: 10.3

I am trying to use a Functor as a Filter for RDataFrame. I have no problem with a C+±only solution …

class TryFunctor 
{
public:
  TryFunctor() {};
  bool operator() (unsigned int run, unsigned int luminosityBlock) { return run > 0; };
};

TryFunctor tf;
auto df1 = df.Filter(tf, { "run", "luminostyBlock"});

But I do not manage to create the Functor from Python and then pass it to RDataFrame …

tf = ROOT.TryFunctor()
df = df.Filter(tf,["run", "luminosityBlock"]).  # does not work
df = df.Filter("tf(run, luminosityBlock)") # does not work

I also tried to define template functions with no success …

template<typename RDF_t> 
auto filter(RDF_t df, TryFunctor tf) -> decltype(df.Filter(tf,{"run", "luminosityBlock"}))
{ return df.Filter(tf,{"run", "luminosityBlock"}); };

Thanks in advance for help and an idea on how to proceed!

Cheers, Dietrich

I think either @etejedor or @eguiraud can give some advice

Hi @Dietrich_Liko ,
and welcome to the ROOT forum! df = df.Filter(tf,["run", "luminosityBlock"]) should work after properly declaring to the ROOT interpreter TryFunctor. What’s the error?

Cheers,
Enrico

Ok I see at least a problem, I think it’s a bug in PyROOT, I opened [PyROOT] Crash when passing functor to template method · Issue #10092 · root-project/root · GitHub – let us know if that’s what you encounter as well.

If that’s the case a workaround until the bug is fixed would be going through a Define:

import ROOT

ROOT.gInterpreter.Declare("""
class TryFunctor 
{
public:
  TryFunctor() {};
  bool operator() (unsigned int run, unsigned int luminosityBlock) { return run > 0; };
};
""")

df.Define("c", ROOT.MyFunctor(), ["run", "luminosityBlock"])\
  .Filter("c")

Cheers,
Enrico

1 Like

Hi Enricio!

Thanks, that is my problem. And thanks a lot for the fast workaround!

Cheers, Dietrich

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