Skip to content

Conversation

@guitargeek
Copy link
Contributor

This is inspired by the existing Klass::size -> Klass.__len__ pythonization, lifting another STL-typical C++ method to a magic Python method for the sake of Pythonization.

This PR enables for example checking for the existence of a specific snapshot in a RooWorkspace.

import ROOT

name_1 = 'empty_snapshot'
name_2 = 'non_existent_snapshot'


ws = ROOT.RooWorkspace('ws')
ws.saveSnapshot(name_1, ROOT.RooArgSet())

empty_snapshot = ws.getSnapshot(name_1)
non_existent_snapshot = ws.getSnapshot(name_2)

ws.Print()

print(name_1 in ws.getSnapshots())
print(name_2 in ws.getSnapshots())

Test coverage is provided by the cppyy unit tests.

Closes #19986.

As ROOT requires C++17 for a long time now, cppyy also requires it and
we can remove the checks for C++17 or lower standards from the code.
This mimics what is done in the newer C++ standards for multiple
standard library classes, and also tells cppyy how to implement
`__contains__` in the Python proxy.
This is to make all overloads for looking up elements available via the
`contains()` pythonization.
The newer C++ standards already implement a `contains()` method for
multiple STL classes. We can just forward to these in the `__contains__`
method for the Python proxy.

Forwarding to `contains()` on the C++ side means also that it will
become easier to implement classes that Pythonize nicely automatically.
Just as happened already before with `size()` (that got used to
automatically implement `__len__`), one just needs to implement
`contains()` on the C++ side now to implicitly get the `__contains__`
pythonization.
@github-actions
Copy link

Test Results

    22 files      22 suites   3d 15h 49m 7s ⏱️
 3 683 tests  3 683 ✅ 0 💤 0 ❌
79 131 runs  79 131 ✅ 0 💤 0 ❌

Results for commit 3baa681.

Copy link
Contributor

@aaronj0 aaronj0 left a comment

Choose a reason for hiding this comment

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

The changes on the cppyy side look good to me! Thanks for this improvement in the contains() pythonizations utilizing the C++ implementation

@guitargeek
Copy link
Contributor Author

Thanks for the review! As the change to RooFit was also seen as useful and intuitive by @AlkaidCheng who opened the issue, I'll merge.

@guitargeek guitargeek merged commit 1c245bf into root-project:master Sep 30, 2025
25 of 27 checks passed
@guitargeek guitargeek deleted the issue-19986 branch September 30, 2025 09:22
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.

[PyROOT] Proper way to check if snapshot exists in workspace

2 participants