Std::function in PyROOT

I have a function in C++ that accepts std::function as one of arguments.
Is there a way to create std::funcntion in PyROOT and invoke my C++ funcntion from PyROOT?

I think @etejedor can most probably give you an answer about this

In the new PyROOT (starting from ROOT 6.22), if you have a function in C++ that accepts a parameter of type std::function, you can pass a Python function as argument and PyROOT will automatically create a C++ wrapper for it.

wow! I It is great!
Are there some examples?

is that recommended for scientific applications? I thought performance would be abysmal.

Another option is to let PyROOT know about your C++ functions and then just pass them as argument to the higher-level function, also via PyROOT:

>>> import ROOT
>>> ROOT.gInterpreter.Declare("""void run_my_f(std::function<void()> f) { f(); }""")
>>> ROOT.gInterpreter.Declare("""void my_f() { std::cout << "42" << std::endl; }""")
>>> ROOT.run_my_f(ROOT.my_f)


Thank you!

As Enrico mentioned, it depends on whether performance is crucial or not (if it is, use his option definitely), or if you would prefer to write your user code in Python.

If you want to see examples of the Python-function option, here you can find how to pass a Python function to the constructor of TF1:

Thank you very much!

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