Skip to content

Improve usability of LinkNavigator from python#919

Open
tmadlener wants to merge 6 commits intoAIDASoft:masterfrom
tmadlener:link-nav-python
Open

Improve usability of LinkNavigator from python#919
tmadlener wants to merge 6 commits intoAIDASoft:masterfrom
tmadlener:link-nav-python

Conversation

@tmadlener
Copy link
Collaborator

@tmadlener tmadlener commented Jan 13, 2026

Attempt for checking whether this is enough to get some parts of the LinkNavigator work more easily from the python side

BEGINRELEASENOTES

  • Add dedicated overloads for Mutable handles to LinkNavigator::getLinked to make python bindings also work with Mutable handles.

ENDRELEASENOTES

See #918 for some more discussion and insights.

@dudarboh this currently adds the "easy" part of more overloads that should hopefully help with calling getLinked with Mutable handles. It doesn't yet add any other python wrapping for improving the other cumbersome parts that you found.

@dudarboh
Copy link

@tmadlener, I quickly tested, seems to work with mutables now 👀

@tmadlener
Copy link
Collaborator Author

Thanks for checking.

@tmadlener
Copy link
Collaborator Author

We should add also the python wrapping parts here to make it really easy to use.

Comment on lines 119 to 125
/// Overload for cppyy that makes things work with mutable handles
template <typename ToU = ToT>
std::enable_if_t<!std::is_same_v<FromT, ToU>, std::vector<WeightedObject<FromT>>>
getLinked(const ToT::mutable_type& object) const {
return getLinked(ToT(object), podio::ReturnFrom);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't check but would it still work with cppyy if std::enable_if was changed to requires and concept? (here and I the code we already have for immutable object)

@tmadlener
Copy link
Collaborator Author

I made Claude come up with some "python bindings" and this looks like a reasonable solution to me. The one thing that doesn't yet transpire through the whole cppyy machinery is the capabilities of structured bindings for the WeightedObject, i.e. in c++ I can do

const auto& [cluster, weight] = linkedClusters[0];

But trying to do the same in python currently fails with

Traceback (most recent call last):
  File "/home/madlener/work/AIDASoft/podio/python/podio/test_LinkNavigator.py", line 85, in test_multiple_links
    for c, w in linked:
        ^^^^
TypeError: WeightedObject<ExampleCluster> object does not support indexing

@tmadlener tmadlener changed the title [WiP] Improve usability of LinkNavigator from python Improve usability of LinkNavigator from python Feb 4, 2026
@tmadlener tmadlener linked an issue Feb 4, 2026 that may be closed by this pull request
@tmadlener
Copy link
Collaborator Author

Many CI jobs are failing due to the strace test. It looks like adding new python bindings also increases the calls to open shared libraries.

@jmcarcell do you think adding the workarounds that were added to resolve #770 (#808 and #775) should also be added to the LinkCollectionImpl.h?

@jmcarcell
Copy link
Member

I have pushed a fix, as usual it is not trivial and it's in a different place from the other cases. I'll think about this tomorrow.

@tmadlener
Copy link
Collaborator Author

Thanks :)

@dudarboh do you want to give this another go before we merge to see if it indeed fixes the issues you observed?

tmadlener and others added 4 commits February 17, 2026 10:40
Attempt for checking whether this is enough to get some parts of the
LinkNavigator work more easily from the python side
Simple factory function with the same name, that takes care of the
proper instantiation
@dudarboh
Copy link

dudarboh commented Feb 17, 2026

I quickly tested everything looks good, @tmadlener .

Could we maybe also add LinkCollection to the podio __init__.py? Because

import edm4hep
import ROOT
ROOT.podio.LinkCollection[edm4hep.ReconstructedParticle, edm4hep.MCParticle]

works, but

import edm4hep
import podio
podio.LinkCollection[edm4hep.ReconstructedParticle, edm4hep.MCParticle]

doesn't

@tmadlener
Copy link
Collaborator Author

Would the use case for that be to write LinkCollections? Or do you still need it for using them when reading them from file? In principle, I assume it's fairly straight forward to do.

@dudarboh
Copy link

I think only for writing. Reading works fine now without providing explicit temlplate arguments:

nav = podio.LinkNavigator( link_col )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add pythonisation for LinkNavigator

4 participants