Skip to content

[Python][RDF] Enable Support for C++ Free Functions in RDF Define and Filter#19437

Merged
siliataider merged 5 commits intoroot-project:masterfrom
siliataider:py-rdf
Jul 31, 2025
Merged

[Python][RDF] Enable Support for C++ Free Functions in RDF Define and Filter#19437
siliataider merged 5 commits intoroot-project:masterfrom
siliataider:py-rdf

Conversation

@siliataider
Copy link
Contributor

@siliataider siliataider commented Jul 22, 2025

This Pull request:

This PR extends the RDataFrame pythonizations by adding support for passing C++ free functions to .Define() and Filter operations, with support for:

  • Templated functions
  • Overloaded functions
  • Functions taking any input/output combination of
    • Fundamental types
    • ROOT types (e.g. TObject-derived classes)
    • User-defined C++ classes

The implementation works by converting the free function to std::function using the function's signature.

Warning

This currently doesn't work if the function takes templated types as input arguments like STL containers (e.g. std::vector<T>) because of #19358
Concretely, this doesn't work:

bool has_even_sum(const RVec<int>& v) {
   return Sum(v) % 2 == 0;
} 

Important

Prerequisites / TODO:

Quick demo:

import ROOT

ROOT.gInterpreter.Declare(""" 
struct MyStruct {
    int a;
    MyStruct() : a(0) {}
    MyStruct(int a_) : a(a_) {}
};
                          
float struct_a(const MyStruct& s) {
    return s.a;
}                          

bool is_even(int x) {
    return x % 2 == 0;
}
                          
bool is_even(int x, float y) {
    return x % 2 == 0 && x == y;
}                                                 

template <typename T>
bool is_even_t(const T x, const float y) {
    return x % 2 == 0 && x == y;
}
""")

rdf = ROOT.RDataFrame(3) \
         .Define("x", "(int)rdfentry_") \
         .Define("y", "(float)rdfentry_") \
         .Define("is_even_x", ROOT.is_even, ["x"]) \
         .Define("is_even_x_y", ROOT.is_even, ["x", "y"]) \
         .Define("is_even_t", ROOT.is_even_t[int], ["x", "y"]) \
         .Define("my_struct", "MyStruct((int)rdfentry_)") \
         .Define("struct_a", ROOT.struct_a, ["my_struct"]) 
         
rdf.Display().Print()

@github-actions
Copy link

github-actions bot commented Jul 22, 2025

Test Results

    20 files      20 suites   3d 0h 3m 40s ⏱️
 3 219 tests  3 218 ✅ 0 💤 1 ❌
62 972 runs  62 971 ✅ 0 💤 1 ❌

For more details on these failures, see this check.

Results for commit e1c1f4c.

♻️ This comment has been updated with latest results.

@siliataider siliataider force-pushed the py-rdf branch 6 times, most recently from 74aa29e to 91b5612 Compare July 25, 2025 14:10
@siliataider siliataider changed the title [Python][RDF] Enable Support for C++ Free Functions in RDF Define [Python][RDF] Enable Support for C++ Free Functions in RDF Define and Filter Jul 26, 2025
@siliataider siliataider marked this pull request as ready for review July 26, 2025 21:57
@siliataider siliataider added test coverage Run the test code coverage workflow on this PR and removed test coverage Run the test code coverage workflow on this PR labels Jul 26, 2025
Copy link
Member

@vepadulano vepadulano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, this is great! I left a couple of minor remarks

@siliataider siliataider force-pushed the py-rdf branch 2 times, most recently from 436854f to ad8ea5e Compare July 28, 2025 16:42
@siliataider siliataider requested a review from vepadulano July 29, 2025 12:39
Copy link
Member

@vepadulano vepadulano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, very nicely done! I just left a comment to check commit consistency

@siliataider siliataider merged commit c37c953 into root-project:master Jul 31, 2025
21 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants