EZ question: can I write gr->Fit("pol1","N

Can I write

gr->Fit(“pol1”,“N”);

where gr is a TGraph object? I am asking this because I am getting segmentation violation when i execute the next statement:

TF1 *func=gr->GetFunction(“pol1”); //not plot fit
Doubel_t p0=func->GetParameter(0);
Double_t p1=func->GetParameter(1);

The thing is that I don’t want to see the linear Fit in my data, although I would like lo obtain the parameters p0 and p1 for analysis.

I try to write

gr->Fit(“pol1”);

and my program does not crash, but again, the linear fit is between my data :frowning:

There is a contradiction in your request. By specifying the option “N” you
tell TGraph::Fit TO NOT STORE the function in the list of functions of the graph. As a result gr->GetFunction returns a null pointer.
Instead of gr->Fit(“pol1”,“N”);
do
TF1 *pol1 = gROOT->GetFunctioon(“pol1”);
gr->Fit(pol1,“N”);
Double_t p0 = pol1->GetParameter(0);

Rene

Hi Rene

Thank you for your feedback. I really appreciate it. I had another solution to my problem. this is what I did:

linear = new TF1(“linear”,“pol1”,xTreme[0],xTreme[1]);
gr->Fit(“linear”,“R0”);

//get slope
linear=gr->GetFunction(“linear”);
p1=linear->GetParameter(1);
e1 = linear->GetParError(1);
gr->Draw(“lp”);

I forgot to mention I am working in c++, and I am not sure if I can use gROOT to do the fitting. However, by especifying the option zero in the Fit() function, it will do the fitting without plotting, plus I can still specified the range of the fitting.

thanks again,