TMinuit in a Class: problem

I’ve been trying to use TMinuit within a Class and I think I have everything set up correctly, but the code won’t compile. I believe the error is in my implementation of TMinuit::SetFCN.

In Reconstruction.h (class header)

class Reconstruction : public SubAnalysis {
public:
Reconstruction(){};
~Reconstruction(){};
static const int NPARFITCHI2 = 4;
void other_function ();
static void fcn_chi2(int &npar, double *gin, double &f, double *par,int iflag);
};

In Reconstruction.cpp

void Reconstruction :: other_funtion () {
//function does stuff
TMinuit *minuitchi2 = new TMinuit(NPARFITCHI2);
minuitchi2->SetFCN(fcn_chi2);
//function does stuff
}
void fcn_chi2(int &npar, double *gin, double &f, double *par, int iflag) {
//function does stuff
}

This gives a compiler error :

Reconstruction.cpp:439: undefined reference to `Reconstruction::fcn_chi2(int&, double*, double&, double*, int)’
collect2: error: ld returned 1 exit status

Does anybody know how to solve this?
Thanks a lot!
Sofia

ROOT Version: 6.16

Hi,
you should either add the scope operator Reconstruction::fcn_chi2
in your .cpp or take fcn_chii2 out of your class definition and make
it global static fcn.

See also this:

Cheers
Otto

Hi Otto,

thank you for your reply, it works. Sorry to bother you, but then I have one more question, I’m very new with these class stuff.

I changed my code by adding the scope operator (void Reconstruction::fcn_chi2) in my .cpp file and now other_function actually enters in fcn_chi2. But being static fcn_chi cannot use non-static member (that I define in other_function, which is non-static). Example of the error:

error: invalid use of member ‘Reconstruction::EMeasParchi2’ in static member function
double EMeasParchi2[NMEASCHI2];

I declare EMeasParchi2[NMEASCHI2] in Reconstruction.h in this way:

class Reconstruction : public SubAnalysis {
public:
Reconstruction(){};
~Reconstruction(){};

static const int NMEASCHI2 = 3;
double EMeasParchi2[NMEASCHI2];

void other_function ();
static void fcn_chi2(int &npar, double *gin, double &f, double *par,int iflag);
};

Any idea how can I solve this?
Thank you very much for your help,
Sofia

Hi Sofia,
everthing (functions, variables) you use in fcn_chi2 needs
to be static.
(Minuit expects their addresses are fix)
So use:

static const int NMEASCHI2 = 3;
static double EMeasParchi2[NMEASCHI2];

Otto

Yes, that’s right.
But the problem is that doing so I cannot use EMeasParchi2 inside other_function (unless I make other_function a static function too, but is something I wouldn’t do cause I need it non-static).
Is there any way I can “transform” a non-static member into a static one? I would like to create EMeasParchi2 inside other_function (which means create a non-static quantity) and then use it inside fcn_chi2 (which needs a static everything).

Thank you again,
Sofia

what happens if you make
double EMeasParchi2[NMEASCHI2] a global,
outside your class Reconstruction.
Then both, your static fcn_chi2 and on static other_function
should be able to access it

Otto

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.