RDataFrame Aggregate within pyROOT


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.27.01
Platform: linux
Compiler: gcc9.3.0


Hi community.

I thought I have a very basic question, but given that I did not manage to find any examples of it I have to ask.

While in RDataFrame tutorials it is clearly stated how one would use Aggregate method in C++, I could not find examples on how to use it in python. JIT version is apparently not there, since

rdf.Aggregate("[](double acc, double x) { return acc + x; }", "[](double acc1, double acc2) { return acc1 + acc2; }", "x")

call resolves to nonexisting Aggregate(std::string,std::string,std::string). Call does not get resolved even I replace those jit-lambdas with python lambdas.

That being said, I’m seemingly missing something fairly simple here, otherwise Aggregate would be a broken method in pyROOT I think. Could you please point out how I can get through this?

Thanks.

Technically I found at least one solution based on link, but I suspect this one is not the most elegant.

ROOT.EnableImplicitMT()
rdf = ROOT.ROOT.RDataFrame(10)
rdf = rdf.Define("x", "return rdfentry_;")
rdf = rdf.Define("x_sqr", "return rdfentry_*rdfentry_;")
#disp = rdf.Display("", 10)
#disp.Print()
ROOT.gInterpreter.Declare("""
struct Accumulator {
    ULong64_t operator()(ULong64_t acc, ULong64_t x) const { return acc + x; }
};
struct Combiner {
    ULong64_t operator()(ULong64_t acc1, ULong64_t acc2) const { return acc1 + acc2; }
};
""")
sum = rdf.Aggregate(ROOT.Accumulator(), ROOT.Combiner(), "x").GetValue()

Please let me know if you know a better way.

Good, you found a solution! I am sure @eguiraud will tell you is that’s the best one.

Looks awkward I must say, but I understand it’s probably not easy to implement another one.
Is jit-compiled version expected in the future?

Hi @Silence2107 ,

that’s correct, using Aggregate, Reduce, Fill and Foreach from Python requires helper functors.

I made a note to clarify this in the docs.

Cheers,
Enrico

cc: @vpadulan

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