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

**URL:** <https://root-forum.cern.ch/t/root-factory-createminimizer-does-not-work/47271>\
**Category:** Newbie\
**Created:** [October 16, 2021, 5:58pm UTC](https://root-forum.cern.ch/t/root-factory-createminimizer-does-not-work/47271 "2021-10-16T17:58:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lorenzo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@Lorenzo](https://root-forum.cern.ch/u/Lorenzo)\
**Post date:** [October 16, 2021, 5:58pm UTC](https://root-forum.cern.ch/t/root-factory-createminimizer-does-not-work/47271/1 "2021-10-16T17:58:57Z")

</div>

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](https://root.cern.ch/root/html534/tutorials/fit/NumericalMinimization.C.html) :

```auto
...
#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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 16, 2021, 6:12pm UTC](https://root-forum.cern.ch/t/root-factory-createminimizer-does-not-work/47271/2 "2021-10-16T18:12:31Z")

</div>

Check: `root-config --has-minuit2`

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [October 18, 2021, 8:15am UTC](https://root-forum.cern.ch/t/root-factory-createminimizer-does-not-work/47271/3 "2021-10-18T08:15:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lorenzo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/4da419/32.png) [@Lorenzo](https://root-forum.cern.ch/u/Lorenzo)\
**Post date:** [October 18, 2021, 8:41am UTC](https://root-forum.cern.ch/t/root-factory-createminimizer-does-not-work/47271/4 "2021-10-18T08:41:03Z")

</div>

I used Minuit and it works fine now. Thanks
