Logo ROOT  
Reference Guide
No Matches
NumericalMinimization.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Example on how to use the new Minimizer class in ROOT Show usage with all the possible minimizers.

Minimize the Rosenbrock function (a 2D -function)

input : minimizer name + algorithm name randomSeed: = <0 : fixed value: 0 random with seed 0; >0 random with given seed

#include "Math/Minimizer.h"
#include "Math/Factory.h"
#include "Math/Functor.h"
#include "TRandom2.h"
#include "TError.h"
#include <iostream>
double RosenBrock(const double *xx )
const double x = xx[0];
const double y = xx[1];
const double tmp1 = y-x*x;
const double tmp2 = 1-x;
return 100*tmp1*tmp1+tmp2*tmp2;
int NumericalMinimization(const char * minName = "Minuit2",
const char *algoName = "" ,
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
if (!minimum) {
std::cerr << "Error: cannot create minimizer \"" << minName
<< "\". Maybe the required library was not built?" << std::endl;
return 1;
// set tolerance , etc...
minimum->SetMaxFunctionCalls(1000000); // for Minuit/Minuit2
minimum->SetMaxIterations(10000); // for GSL
// create function wrapper for minimizer
// a IMultiGenFunction type
ROOT::Math::Functor f(&RosenBrock,2);
double step[2] = {0.01,0.01};
// starting point
double variable[2] = { -1.,1.2};
if (randomSeed >= 0) {
TRandom2 r(randomSeed);
variable[0] = r.Uniform(-20,20);
variable[1] = r.Uniform(-20,20);
// Set the free variables to be minimized !
minimum->SetVariable(0,"x",variable[0], step[0]);
minimum->SetVariable(1,"y",variable[1], step[1]);
// do the minimization
const double *xs = minimum->X();
std::cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): "
<< minimum->MinValue() << std::endl;
// expected minimum is 0
if ( minimum->MinValue() < 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");
return 0;
#define f(i)
Definition RSha256.hxx:104
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
static ROOT::Math::Minimizer * CreateMinimizer(const std::string &minimizerType="", const std::string &algoType="")
static method to create the corresponding Minimizer given the string Supported Minimizers types are: ...
Definition Factory.cxx:63
Documentation for class Functor class.
Definition Functor.h:47
Abstract Minimizer class, defining the interface for the various minimizer (like Minuit2,...
Definition Minimizer.h:117
virtual const double * X() const =0
return pointer to X values at the minimum
void SetMaxIterations(unsigned int maxiter)
set maximum iterations (one iteration can have many function calls)
Definition Minimizer.h:349
virtual void SetFunction(const ROOT::Math::IMultiGenFunction &func)=0
set the function to minimize
void SetTolerance(double tol)
set the tolerance
Definition Minimizer.h:352
virtual bool Minimize()=0
method to perform the minimization
void SetPrintLevel(int level)
set print level
Definition Minimizer.h:343
virtual bool SetVariable(unsigned int ivar, const std::string &name, double val, double step)=0
set a new free variable
void SetMaxFunctionCalls(unsigned int maxfcn)
set maximum of function calls
Definition Minimizer.h:346
virtual double MinValue() const =0
return minimum function value
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition TRandom2.h:27
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Lorenzo Moneta

Definition in file NumericalMinimization.C.