@@ -21,11 +21,14 @@ using namespace ROOT::Fit;
2121const ROOT::Math::IMultiGenFunction *gFunction = nullptr ;
2222// / function wrapper for the gradient of the function to be minimized
2323const ROOT::Math::IMultiGradFunction *gGradFunction = nullptr ;
24-
24+ // / function wrapper for the hessian of the function to be minimized
2525std::function<bool (const std::vector<double > &, double *)> gfHessianFunction;
2626
27+ // / simple function for debugging
2728#define PyPrint (pyo ) PyObject_Print(pyo, stdout, Py_PRINT_RAW)
2829
30+
31+ // / function to wrap into Python the C/C++ target function to be minimized
2932PyObject *target_function (PyObject * /* self*/ , PyObject *args)
3033{
3134 PyArrayObject *arr = (PyArrayObject *)PyTuple_GetItem (args, 0 );
@@ -36,6 +39,7 @@ PyObject *target_function(PyObject * /*self*/, PyObject *args)
3639 return PyFloat_FromDouble (r);
3740};
3841
42+ // / function to wrap into Python the C/C++ jacobian function
3943PyObject *jac_function (PyObject * /* self*/ , PyObject *args)
4044{
4145 PyArrayObject *arr = (PyArrayObject *)PyTuple_GetItem (args, 0 );
@@ -49,6 +53,7 @@ PyObject *jac_function(PyObject * /*self*/, PyObject *args)
4953 return py_array;
5054};
5155
56+ // / function to wrap into Python the C/C++ hessian function
5257PyObject *hessian_function (PyObject * /* self*/ , PyObject *args)
5358{
5459 PyArrayObject *arr = (PyArrayObject *)PyTuple_GetItem (args, 0 );
@@ -60,9 +65,6 @@ PyObject *hessian_function(PyObject * /*self*/, PyObject *args)
6065 gfHessianFunction (x, values);
6166 npy_intp dims[2 ] = {size, size};
6267 PyObject *py_array = PyArray_SimpleNewFromData (2 , dims, NPY_DOUBLE, values);
63- // std::cout<<"---------------"<<std::endl;
64- // PyPrint(py_array);
65- // std::cout<<"---------------"<<std::endl;
6668 return py_array;
6769};
6870
@@ -71,9 +73,7 @@ ScipyMinimizer::ScipyMinimizer() : BasicMinimizer()
7173{
7274 fOptions .SetMinimizerType (" Scipy" );
7375 fOptions .SetMinimizerAlgorithm (" L-BFGS-B" );
74- if (!PyIsInitialized ()) {
75- PyInitialize ();
76- }
76+ PyInitialize ();
7777 fHessianFunc = [](const std::vector<double > &, double *) -> bool { return false ; };
7878 // set extra options
7979 SetAlgoExtraOptions ();
@@ -84,9 +84,7 @@ ScipyMinimizer::ScipyMinimizer(const char *type)
8484{
8585 fOptions .SetMinimizerType (" Scipy" );
8686 fOptions .SetMinimizerAlgorithm (type);
87- if (!PyIsInitialized ()) {
88- PyInitialize ();
89- }
87+ PyInitialize ();
9088 fHessianFunc = [](const std::vector<double > &, double *) -> bool { return false ; };
9189 // set extra options
9290 SetAlgoExtraOptions ();
@@ -169,23 +167,23 @@ void ScipyMinimizer::PyInitialize()
169167// Finalize Python interpreter
170168void ScipyMinimizer::PyFinalize ()
171169{
172- if (fMinimize )
173- Py_DECREF (fMinimize );
174- if (fBoundsMod )
175- Py_DECREF (fBoundsMod );
176170 Py_Finalize ();
177171}
178172
179173// _______________________________________________________________________
180174int ScipyMinimizer::PyIsInitialized ()
181175{
182- if (!Py_IsInitialized ())
183- return kFALSE ;
184- return kTRUE ;
176+ return Py_IsInitialized ();
185177}
186178
187179// _______________________________________________________________________
188- ScipyMinimizer::~ScipyMinimizer () {}
180+ ScipyMinimizer::~ScipyMinimizer ()
181+ {
182+ if (fMinimize )
183+ Py_DECREF (fMinimize );
184+ if (fBoundsMod )
185+ Py_DECREF (fBoundsMod );
186+ }
189187
190188// _______________________________________________________________________
191189bool ScipyMinimizer::Minimize ()
0 commit comments