Where p1,p2,p3 and p4 are Lorenz vectors. I am trying to minimize it using minuit, there are other four constraints but I am not considering them yet for simplicity. To do this I been following this example-> ROOT: tutorials/fit/NumericalMinimization.C Source File . There is a line in the example which is:

but neither worked. So i wonder, how can i write it in PyROOT? And there is any other minimization examples in python or c++? i just found this one until now.

The code above is correct, but it will not work when used for Minimization. The function to be passed to the minimiser needs to take only one parameter that is a list of values corresponding to the parameters to be found by the minimiser.
I attached as example the NumericalMinimization script translated in Python

If the minimizer takes only one parameter, how can I minimize a multivariate function with root or other Roo tools? I need to minimize the function that I wrote above, but subject to other 4 constraints.

The function to pass to the minimizer must be a function requiring only one argument, but this can be a list or an array of several variables. These variables are the minimization parameters. In your case it is not clear to me what are these variables between x,p1,p3,p3,p4.
Adding the constraints is another story, we do not support an interface for including them directly in the minimiser, you need to implement yourself as was already answered to you in Minimization subject to constrains with Tminuit, is it possible? [PyROOT] - #4 by couet.

The function that I am trying to minimize is the Mandelstam S variable for a system of 4 particles (two visible and two neutrinos) . Actually, I am trying to minimize it with respect to the neutrino variables (p,theta,phi),and I already have all he information about the visible particles. This leaves 6 variables at the total. And writing S explicitly in terms of p, theta and phi for all the variables is kinda of messy, so i tried to work directly with the four vectors.

āIn your case it is not clear to me what are these variables between x,p1,p3,p3,p4.ā. So these variables you said above are the 3d arrays n3 and n4? If it is, I created them so I could just use them .Pt and .Eta components of them directly into a .SetPtEtaPhiE().

There is some problem trying to minimize the function in this way? Or is it ok?

Just one more question, I know you said that "The function to be passed to the minimiser needs to take only one parameter " but there is a way to read the p1 and p2 lorenz vectors from the main function? Or I would have to try to read them from the .root file directly inside the function that will be minimized?

>>> import ROOT
>>> def f(x, p1, p2): return .1
...
>>> a = ROOT.Math.Functor(f, 6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Template method resolution failed:
none of the 2 overloaded methods succeeded. Full details:
Functor::Functor(const ROOT::Math::Functor& rhs) =>
TypeError: takes at most 1 arguments (2 given)
Functor::Functor() =>
TypeError: takes at most 0 arguments (2 given)
Failed to instantiate "Functor(function,int)"

I donāt know what you meant by āThis works for me with master:ā, sorry but I donāt know what āmasterā is. I just used the terminal to try the code line I wrote above.

Yes, let me clarify, master is the current development branch of ROOT. With what ROOT version did you get that error?

I just tried 6.24 and I also see it, but it seems fixed in 6.26 which will be released soon.

Until then, if you want to work with master, one easy way is to do from lxplus:

[etejedor@lxplus766 ~]$ source /cvmfs/sft.cern.ch/lcg/views/dev3python39/latest/x86_64-centos7-gcc9-opt/setup.sh
[etejedor@lxplus766 ~]$ python3
Python 3.9.5 (default, Jun 16 2021, 18:19:24)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>> def f(x, p1, p2): return .1
...
>>> a = ROOT.Math.Functor(f, 6)
>>> a
<cppyy.gbl.ROOT.Math.Functor object at 0x83a0490>

Alternatively if you use SWAN, you can also pick the ādevelopment bleeding edgeā stack when starting your session, and that will have the same effect.

The other inputs to the function to be minimised, need to be either taken from the ROOT file inside the functions or defined as global variables that will be visible inside the function.
In C++ you can define a functor class and use the other inputs as member data of the class.
I am not sure if you can use a class to define your function in PyROOT

Iām also having facing this issue, and unfortunately Iām restricted to the root version of our analysis framework. Are there any work arounds to get this to work in 6.24?