ROOT::Math:: Factory::CreateMinimizer does not work

I need to use the function minimizer, so I basically copied the code from Example on how to use the new Minimizer class in ROOT :

...
#include "Math/Minimizer.h"
#include "Math/Factory.h"
#include "Math/Functor.h"
#include "TRandom2.h"
#include "TError.h"
int NumericalMinimization(const char * minName = "Minuit2",
                          const char *algoName = "Migrad" , 
                          int randomSeed = 1)
{
   // create minimizer giving a name and a name (optionally) for the specific
   // algorithm
   // possible choices are: 
   //     minName                  algoName
   // Minuit /Minuit2             Migrad, Simplex,Combined,Scan  (default is Migrad)
   //  Minuit2                     Fumili2
   //  Fumili
   //  GSLMultiMin                ConjugateFR, ConjugatePR, BFGS, 
   //                              BFGS2, SteepestDescent
   //  GSLMultiFit
   //   GSLSimAn
   //   Genetic
  ROOT::Math::Minimizer* min = NULL;
  min =  ROOT::Math::Factory::CreateMinimizer(minName, algoName);
  //ROOT::Math::Minimizer *min = new ROOT::Minuit2::Minuit2Minimizer(algoName);
   // set tolerance , etc...
   min->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2 
   min->SetMaxIterations(10000);  // for GSL 
   min->SetTolerance(0.001);
   min->SetPrintLevel(1);

   // create function wrapper for minmizer
   // a IMultiGenFunction type 
   ROOT::Math::Functor f(&prova,1); 
   double step[1] = {0.01};
   // starting point
    
   double variable[1] = { 0.1};
   if (randomSeed >= 0) { 
      TRandom2 r(randomSeed);
      variable[0] = r.Uniform(-20,20);
   }
 
   min->SetFunction(f);
 
   // Set the free variables to be minimized!
   min->SetVariable(0,"x",variable[0], step[0]);
 
   // do the minimization
   min->Minimize(); 
 
   const double *xs = min->X();
   std::cout << "Minimum: f(" << xs[0] << "): " 
             << min->MinValue()  << std::endl;

   // expected minimum is 0
   if ( min->MinValue()  < 1.E-4  && f(xs) < 1.E-4) 
      std::cout << "Minimizer " << minName << " - " << algoName 
                << "   converged to the right minimum" << std::endl;
   else {
      std::cout << "Minimizer " << minName << " - " << algoName 
                << "   failed to converge !!!" << std::endl;
      Error("NumericalMinimization","fail to converge");
   }
 

    DrawoverlapIntegral(xs[0]);
   return 0;
}

But when I run the code, it looks like CreateMinimizer doesn’t work, i report my entire log:

root [0] NumericalMinimization()
#0  0x00007fd683f42dba in __GI___wait4 (pid=368846, stat_loc=stat_loc
entry=0x7ffda61fac28, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
#1  0x00007fd683f42d7b in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7ffda61fac28, options=options
entry=0) at waitpid.c:38
#2  0x00007fd683eb20e7 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3  0x00007fd684574cfe in TUnixSystem::StackTrace() () from /opt/applications/root_src/installdir/lib/libCore.so
#4  0x00007fd6807faad0 in cling::MultiplexInterpreterCallbacks::PrintStackTrace() () from /opt/applications/root_src/installdir/lib/libCling.so
#5  0x00007fd6807fa412 in cling_runtime_internal_throwIfInvalidPointer () from /opt/applications/root_src/installdir/lib/libCling.so
#6  0x00007fd6802daafd in ?? ()
#7  0x0000000000000000 in ?? ()
Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments.
Execution of your code was aborted.
In file included from input_line_11:1:
/myDirectory/myMacro.C:662:4: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
   min->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2 

Do you have any hint?

Check: root-config --has-minuit2

Hi,
Normally if Minuit2 is not available it should use Minuit as backup.
You can set gDebug=1 before using Factory::CreateMinimizer to understand better what is happening

Lorenzo

I used Minuit and it works fine now. Thanks