Hi ROOTers,
in order to fit some data I rewrote the example-files myfit.C and
hsimple.C. Here I have to fit no histogram but some data-points. But as
far as I recognized, that should be nearly the same. I tried to fit with a
simple linear term (because I wanted to get familiar with user defined
function), but I did not get what I expected (but a straight line on the
x-axis).
What maybe the mistake? And could you give me an advice, how to implement
an integral (with 2 or 3 parameters) as my fit function?
Thank you for your effort, maybe I am not able to read the manual
correct...
Danilo
This is my modified myfit.C
Double_t fitfunc(Double_t *x, Double_t *par)
{
Double_t fitval = par[0]+x[0]*par[1]+x[0]*x[0]*par[2];
return fitval;
}
gRoot->Reset();
//this function used fitfunc to fit a graph
void test_2()
{
TFile *f = new TFile("daten.root");
TH1F *data = (TH1F*)f->Get("data");
TF1 *function = new TF1("fitfunc","fitfunc",0,1,3);
function->SetParNames("const","linear","quadratic");
data->Fit(function,"R");
}
And this is the modified hsimple.C
{
gROOT->Reset();
c1 = new TCanvas("c1","Data",200,10,700,500);
c1->SetFillColor(42);
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(12);
c1->GetFrame()->SetBorderMode(-1);
TFile *hfile = (TFile*)gROOT->FindObject("daten.root");
if (hfile) hfile->Close();
hfile = new TFile("daten.root","RECREATE","DATA");
data = new TH1F("data","These are the data",42,0,1);
data->SetMarkerColor(4);
data->SetMarkerStyle(20);
gBenchmark->Start("daten");
//Create the points
const Int_t n = 42;
Double_t x[n] =
{0.458,0.354,0.425,0.374,0.420,0.372,0.378,0.453,0.465,0.498,0.655,0.4,0.615,0.480,0.450,0.388,0.570,0.490,0.495,0.656,0.828,0.450,0.430,0.580,0.763,0.526,0.172,0.619,0.592,0.550,0.180,0.374,0.472,0.430,0.657,0.612,0.320,0.579,0.450,0.581,0.416,0.830};
Double_t y[n] =
{23.11,22.38,22.13,21.72,22.55,22.26,22.58,23.17,23.33,23.71,23.27,22.36,23.19,22.96,22.51,22.65,23.27,23.10,22.83,23.57,24.65,23.17,23.13,23.46,24.47,23.15,20.17,23.80,24.42,23.51,20.43,23.52,23.11,22.57,23.83,23.69,21.86,23.48,22.83,23.09,22.57,24.32};
//create the width of errors in x and y direction
Double_t ex[n] =
{0.001,0.001,0.001,0.001,0.001,0.001,0.001,0.001,0.005,0.001,0.001,0.03,0.001,0.001,0.001,0.001,0.001,0.01,0.001,0.001,0.001,0.01,0.01,0.001,0.001,0.001,0.001,0.001,0.001,0.01,0.001,0.001,0.001,0.01,0.001,0.001,0.01,0.001,0.01,0.001,0.001,0.01};
Double_t ey[n] =
{0.46,0.33,0.49,0.22,0.25,0.20,0.37,0.25,0.30,0.25,0.21,0.19,0.25,0.24,0.23,0.2,0.22,0.2,0.19,0.28,0.54,0.23,0.22,0.23,0.53,0.20,0.18,0.28,0.37,0.25,0.17,0.24,0.19,0.18,0.23,0.21,0.18,0.22,0.3,0.22,0.2,0.22};
//create the TGraphErrors and draw
gr = new TGraphErrors(n,x,y,ex,ey);
gr->SetTitle("Data-file");
gr->SetMarkerColor(4);
gr->SetMarkerStyle(20);
gr->Draw("AP");
gBenchmark->Show("daten");
data->SetMarkerColor(0);
data->SetMarkerStyle(20);
hfile->Write();
data->SetMarkerColor(4);
data->SetMarkerStyle(20);
c1->Modified();
}
Danilo Behnke
This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:52 MET