Hi Lorenzo, thanks a lot!
I ended also trying to just doing it without creating these arrays (std::vector) by doing everything inside my Loop. In part because some of the variables I needed were not exactly a measured value, but a calculated value from measured values (after applying some selections).
Here is how it looks. Just a final doubt for setting some limits at the end.
//Before this part I have defined jte1, jte2, cost, sig1,sig2,gam
auto myFCN = [&](const double * par) {
double chi2 = 0;
double k1 = par[0];
double k2 = par[1];
double M12 = sqrt(2.*k1*k2*(1.-cost));
chi2 = (jte1-k1)*(jte1-k1)/sig1/sig1 + (jte2-k2)*(jte2-k2)/sig2/sig2 + (M12-MW)*(M12-MW)/gam ;
return chi2;
};
ROOT::Math::Functor f(myFCN,2);
//-------------------------------------------
//-----------------------------------------------------------
ROOT::Math::Minimizer* minimum =
ROOT::Math::Factory::CreateMinimizer("Minuit", "Migrad");
double step[2] = {0.001,0.001};
// starting point
double variable[2] = { jte[iq1],jte[iq2]}; //same as measured value
minimum->SetFunction(f);
// Set the free variables to be minimized !
//minimum->SetVariable(0,"jte1",variable[0], step[0]);
//minimum->SetVariableLowerLimit(0, 0.);
minimum->SetLowerLimitedVariable(0,"k1",variable[0], step[0],0); //Lower value is energy=0
minimum->SetLowerLimitedVariable(1,"k2",variable[1], step[1],0);
// do the minimization
minimum->Minimize();
const double *xs = minimum->X();
cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): "
<< minimum->MinValue() << std::endl;
I tried to set some limits on my fit variables, but it is giving me a warning which I don’t understand.
I tried to set the lower limit of k1 to 0, as I can’t get negative values of energy, but it returns the following:
Warning in <TMinuitMinimizer::SetLowerLimitedVariable>: not implemented - use as upper limit 1.E7 instead of +if
Not sure why it says anything about upper limit (and +inf) if I’m trying to set a lower limit, and I could’t find some implementation of this online. Neither the SetVariableLowerLimit works. So, any help is appreciated.
Thanks!