Getting the TF2 function from a 2D fit

Dear Rooters,

I am using ROOT version 3.10_02.

Using the same method as the example provided on the root website, fit2.C, I am trying to fit a user-defined TF2 to a TH2D. However, I cannot seem to get the function from the histogram after I have performed the fit. When I try to compile I get the following message:

 type 'TF2' is not a base type for type 'TF1'

This is my code for the fit:

[code]TString datafile(“silicon.root”);
TFile *data = new TFile(datafile);

TCanvas *c1 = new TCanvas(“c1”,“c1”,800,1000);
c1->cd(1);
PlotData2d(data,“hxy”,“x”,“y”);
c1->Update();

const Int_t npar = 5;
Double_t f2params[npar] = {30,30,0.05,0.05,0.05};
TF2 *f2 = new TF2(“f2”,ellipseFunction,0,60,0,60,npar);
f2->SetParameters(f2params);
TH2D hxy = (TH2D)data->Get(“hxy”);
hxy->Fit(“f2”);
f2->Draw(“cont1 same”);
TF2 *fitXY = hxy->GetFunction(“f2”);
c1->Update();[/code]

here is the ellipse function:

Double_t ellipseFunction(Double_t *x, Double_t *par){ Double_t term1 = Double_t(par[2]*(x[0]-par[0])*(x[0]-par[0])); Double_t term2 = Double_t(2*par[3]*(x[0]-par[0])*(x[1]-par[1])); Double_t term3 = Double_t(par[4]*(x[1]-par[1])*(x[1]-par[1])); Double_t result = TMath::Exp(-0.5*(term1+term2+term3)); return result; }

here is the PlotData2d function:

void PlotData2d(TFile* data, TString Hist, TString XAxisLabel, TString YAxisLabel){ TH2D *Data = (TH2D*)data->Get(Hist); Data->SetMarkerStyle(1); Data->SetMarkerColor(kBlack); Data->GetXaxis()->SetTitle(XAxisLabel); Data->GetYaxis->SetTitle(YAxisLabel); Data->Draw("e"); }

If GetFunction() cannot be used for TF2’s (I’m guessing this is the problem here), is there another way of getting the fit parameters and errors from the fit?

Thanks for any help you can give me,

Catherine

Hi Catherine,

Replace the statement:
TF2 *fitXY = hxy->GetFunction(“f2”);
by
TF2 fitXY = (TF2)hxy->GetFunction(“f2”);

Rene