Create and pass a C++ function in PyROOT

@Luke_K: no, it’s a separate option, see this other post. I’ve never perused it myself, though.

@ibles: in the world outside HEP, the two most desired features are automatic template instantiations and callbacks. The latter both as in the example here, and cross-inheritance. Needless to say that those are the two corners with the most functional improvements. Below is an example with cppyy master, covering the same as above (where it needs to be said that callbacks into Python are (a lot) more expensive then callbacks into C++, so even as it looks more nice, for simple functions like this, JIT-ing a C++ function should still be preferred).

import cppyy

def myOwnFitEF(xPos_mm, yPos_mm, zPos_mm):
    return xPos_mm + yPos_mm + zPos_mm

cb_def="""
double reduce(const std::vector<double>& data, std::function<double(double, double, double)> func){
    return func(data[0], data[1], data[2]);
}
"""
cppyy.cppdef(cb_def);

v = cppyy.gbl.std.vector['double']([(x+1)**2 for x in range(3)])

print('From Python:', myOwnFitEF(*v))
print('From C++:   ', cppyy.gbl.reduce(v, myOwnFitEF))