Hi Kevin,
You will find below a small example showing how to fit a polynomial
to an histogram and access the results of the fit.
Rene Brun
void fitex()
{
// example of macro creating an histogram, fitting it
// and accessing the fit results
TH1F *h = new TH1F("h","test",100,-4,4);
h->FillRandom("gaus",3000);
h->Fit("pol7");
//Get a pointer to the fitted function
TF1 *func = (TF1*)h->GetFunction("pol7");
Int_t npar = func->GetNpar();
Double_t chi2 = func->GetChisquare();
printf("Function has % parameters. Chisquare = %g\n",
npar,func->GetChisquare());
for (Int_t i=0;i<npar;i++) {
printf("% %s, = %g, +- %g\n",
func->GetParName(i),
func->GetParameter(i),
func->GetParError(i));
}
TFile f("fitex.root","recreate");
//write histo to the file. Note that the full information about
//the histogram including the fitted function and its parameters
// are saved to the file
h->Write();
// in a new session, one can then do:
// TFile *f = new TFile("fitex.root");
// TH1F *h = (TH1F*)f->Get("h");
// and access the info from h as shown above
}
On Fri, 23 Jun 2000, Kevin M. Rhodes wrote:
> All I'm trying to do is fit an histogram with a poly, then write the
> output of the fit (the value, error, step size, and first derivative) to
> an array or file before moving on to the next histogram, for which I wish
> to repeat the process.
>
> I'm new to root/C++, so any help would be appreciated.
>
>
> Kevin Rhodes
> Amanda Project
> UW-Madison
>
This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:28 MET