Minuit2Minimizer with member function as FCN

Hi all,

I am trying to do some minimization using the Minuit2Minimizer from inside a class, since I need to use some trees and I don’t want to use any global objects because I plan to run more than one instance in parallel. After configuring the minimizer, when I try to perform the minimization I get a segmentation fault. My guess is that it is caused by a bad use of the Functor, but I cannot see why. I have searched for examples but I haven’t found a suitable one…

Any ideas? A simplified code is here:

[code]#ifndef TEST_H
#define TEST_H

#include
#include
#include
#include
#include
#include
#include
#include
#include <TObject.h>
#include <TString.h>
#include <math.h>
#include “Minuit2/Minuit2Minimizer.h”
#include “Math/Functor.h”

const double pionmass = 0.1349766;

class Test : public TObject {
private:
// Variables
int m_vars;
int m_nEntries;
ROOT::Minuit2::Minuit2Minimizer* gMinuit;
double fcn( const double* );
public:
// Constructors and configuration
Test() {};
void configureMinuit( const int );
int addVar( const int varNum , const TString varName , const float initialValue );
int minimize();
ClassDef( Test , 1 );
};

#endif[/code]

[code]#include “Test.h”

#if !defined( CINT )
ClassImp( Test );
#endif

void Test::configureMinuit( const int nVars ){
gMinuit = new ROOT::Minuit2::Minuit2Minimizer( ROOT::Minuit2::kMigrad );
ROOT::Math::Functor f( this , &Test::fcn , nVars );
m_vars = nVars;
gMinuit->SetFunction( f );
// Setting value of UP parameter
gMinuit->SetErrorDef( 1.0 );
gMinuit->SetMaxFunctionCalls(10000000);
gMinuit->SetMaxIterations(100000);
gMinuit->SetTolerance(10);
}

int Test::addVar( const int varNum , const TString varName , const float initialValue ){
gMinuit->SetLimitedVariable( varNum , varName.Data() , initialValue , 0.01 , 0.80 , 1.20 );
return 1;
}

int Test::minimize(){
time_t tstart, tend;
tstart = time(0);
gMinuit->Minimize();
tend = time(0);
std::cout << “The calibration took " << difftime( tend, tstart ) << " seconds” << std::endl;
return 1;
}

double Test::fcn( const double *par ){
return par[0]*par[0];
}[/code]

And for running:

root [0] gROOT->LoadMacro("Test.cxx+") Info in <TUnixSystem::ACLiC>: creating shared library /Users/albert/Arxiu/Fisica/LHCb/CaloCalibration/Calibration/analysis/PionCalibration/./Test_cxx.so (Int_t)0 root [1] minuit = Test() (class Test)55578400 root [2] minuit.configureMinuit(1) root [3] minuit.addVar(0,"test",1.0) (int)1 root [4] minuit.minimize()

Thank you,
Albert

Hello,

you need to have the function object passed to the Minimizer class available during all time the class is used. The fix is to have the Functor as a data member of your test class.
It is true we should probably change the Minimizer class to copy the object inside. I will think about putting this change for a future ROOT release.

I attached your modified code that works

Best Regards

Lorenzo
rootForum_9485.C (2.08 KB)