Wrapping a Object in a ROOT::Math::Funtor

Hello all,
I am trying to minimize a chi square function using ROOT::Math::Minimizer. The chi square function is defined in a class as the override of the () operator. This is then wrapped as a ROOT::Math::Functor. However, I keep running in to the trouble that the () operator is never actually called. I found some code in the documentation about how to do this and it shows the same problem. I have include a small example (altered a bit from the documentation) which illustrates the problem.

#include "Math/Functor.h"
#include <iostream>
 
class MyFunction { 
 public: 
  double operator()(const vector<double> x) const { 
    cout<<"Hello.\n";
    return x[0]+x[1]; 
   } 
};
 
int function(){
   // test directly calling the function object
   MyFunction myf;
    
   // test from function object
   ROOT::Math::Functor f1(myf,2); 
 
   double x[] = {1,2};
 
   cout << f1(x) << endl;
    
   return 0;
}

When I run this I would expect the output to be:

However what I get is:

So it looks like the () operator is never called! Overwise the cout statement would appear and the correct value of 3 would be displayed. Has anyone encountered a similar issue?

On a side note, is ROOT::Math::Minimization the most up to date way to do function minimization in root?

thanks everyone!

Nicholi Shiell.

Hi Nicholi,

I see a small error in your code. The operator() in MyFunctor must have a specific signature in order to work. It does not work with STL vectors.

  double operator()(const double* x) const { ... }

After making this change, I ran your code and it works fine for me. Try making this change and let me know if it fixed the issue.

Regarding minimization, I recommend you check out this page: http://root.cern.ch/drupal/content/numerical-minimization.

Cheers,
Gabriel

Hey Gabriel,
Thanks for the response. I tried the change you suggested the operator now looks like:

double operator()(const double* x) const { 
    cout<<"Hello.\n";
    return x[0]+x[1]; 
 } 

however I got the following error:

if I change the definition of the functor to

 ROOT::Math::Functor f1(&myf,2); 

I get the old output back:

Could it be the version of root I am running? I have version 5.28/00f. Also I check the root includes directory and Functor.h is there so I have the right root packages installed.

Any other ideas? What version are you running?

thanks again!

Hi,

You should compile your macro with ACLIC (recommended) or you can use this syntax to work in interpreter mode:

ROOT::Math::Functor f1(&myf,2,"MyFunction"); 

Lorenzo

Thanks Lorenzo, it works now. However, should I use the previous declaration if I am not using CINT?

Hi,

The one without the class name works with both, the one without works only without CINT

Lorenzo