Error in fitting function of three variables

void MyFitCode(){
    //data(# of points, # of dimensions)
    ROOT::Fit::BinData data(5,3);
    Double_t coor1[3] = {1,2,3};
    Double_t coor2[3] = {1,-2,1};
    Double_t coor3[3] = {2,7,3};
    Double_t coor4[3] = {3,2,-4};
    Double_t coor5[3] = {-2,0,3};
    Double_t coor6[3] = {-3,9,-3};
    data.Add(coor1, 2);
    data.Add(coor2, 3);
    data.Add(coor3, 4);
    data.Add(coor4, 5);
    data.Add(coor5, 6);
    data.Add(coor6, 7);


    TF3 *f3 = new TF3("f3", "[0] * sin(x) + [1] * cos(y) + [2] * z", 0,10,0,10,0,10);
    f3 -> SetParameters(2,2,2);
    ROOT::Fit::Fitter fitter;
    ROOT::Math::WrappedMultiTF1 wf(*f3, 3);
    fitter.SetFunction(wf);
    bool ret = fitter.Fit(data); 
    if (ret) { 
        ROOT::Fit::FitResult res( fitter.Result() );
        res.Print(std::cout);
    }
    else{
        cout << "Fit Failed" << endl;
    } 
}

Much of the code was derived from Fitting and integration (Windows Build Problem) since I was unsure how to fit such a function.

When I run this code, it prints out “Fit Failed” and I’m not sure why. What error lies in my code?

Thanks

Hi,

I checked the documentation of BinData:
https://root.cern/doc/master/classROOT_1_1Fit_1_1BinData.html

When constructing the BinData, you need to properly set up the mode (with / without errors) and the number of entries:

    ROOT::Fit::BinData data(6,3, ROOT::Fit::BinData::kNoError);

Thank you; it worked. However, I have another question. How would I get the fitting coefficients without running the code? There is a TF1::GetParameters method but that doesn’t apply here. What command could I use here?

Hi asp211,

f3 -> GetParameter(i)
should work for you.

Adding this to your code:

TF3 *f3 = new TF3("f3", "[0] * sin(x) + [1] * cos(y) + [2] * z", 0,10,0,10,0,10);
cout<<"Displaying Parameters before Initialisation"<<endl;
double param0 = f3 -> GetParameter(0);
double param1 = f3 -> GetParameter(1);
double param2 = f3 -> GetParameter(2);   
cout<<"param0:   "<< param0 <<endl <<"param1:   "<< param1 << endl <<"param2:   "<< param2 << endl;
    
f3 -> SetParameters(2,2,2);
    
cout<<"Displaying Parameters after initialisation"<<endl;
double sparam0 = f3 -> GetParameter(0);
double sparam1 = f3 -> GetParameter(1);
double sparam2 = f3 -> GetParameter(2);   
cout<<"setparam0:   "<< sparam0 <<endl <<"setparam1:   "<< sparam1 << endl <<"setparam2:   "<< sparam2 << endl;

Gives you:

Processing MyFitCode.C...
Displaying Parameters before Initialisation
param0:   0
param1:   0
param2:   0
Displaying Parameters after initialisation
setparam0:   2
setparam1:   2
setparam2:   2

****************************************
Minimizer is Minuit / Migrad
Chi2                      =      137.185
NDf                       =            3
Edm                       =   5.1905e-23
NCalls                    =           31
p0                        =      0.68216   +/-   0.732561    
p1                        =      -0.1796   +/-   0.955638    
p2                        =   -0.0521488   +/-   0.222151

Is this what you were looking for?

Somewhat. Is there a way to get the GetParameter method to retrieve and print the values of 0.68216, -0.1796, and -0.0521488 in advance?

Thanks for responding.