I am trying to fit a gaussian to a data file I have. When the I view the histogram, it has a mean value around close to the peak at which I want it to fit around. How would I comprise a code that would fit about the ‘mean’ of a histogram, and then output (cout) the ‘chi^2/ndf’ of the fit?
Also, how would I separate the ‘mean’ of the histogram and the ‘mean’ of the fit that I obtain? (The reason why is because my ultimate goal is to iterate the fit to histogram until the ‘chi^2/ndf’ is sufficiently low, any help with this also would be appreciated to but help with just the initial question is very much appreciated.)
Maybe this would make my question more clear, I tried the following code and it shows up with subsequent erros:
root  TF1 *f1 = new TF1("f1","gaus",h1.GetMean(),-.5,.5)
Error in <TF1::TF1>: can not find any function at the address 0x3dacb9c. This function requested for f1
root  h1.Fit("f1","H")
Unknown function: f1
Basically I’m trying to define a new function with the predefined gaussian function, and set the parameters so that the range to fit is set around the Mean (i.e. Mean-0.5 to the Mean+0.5). Any help would be much appreciated.
you can define your function as following:
TF1 *f1 = new TF1("f1","gaus",-.5,.5);
// set initial parameters (not really needed for gaus)
f1->SetParameters(h1.GetMaximum(), h1.GetMean(), h1.GetRMS() );
Note that the initial parameters are not needed when fitting a pre-defined gaussian function, since in this case they are computed automatically.
Also the option H is not valid when fitting an histogram. It is for a linear fitting of a TGraph.
Thank You so much,
I just have another question if you wouldn’t mind:
After I fit the historgram, I’d like to be able to call and output the Mean value of the fit. So I try the following but get the subsequent errors:
root  gfit.GetMean()
Error: Can't call TF1::GetMean() in current scope (tmpfile):1:
Possible candidates are...
*** Interpreter error recovered ***
I’m not sure what this means or how to resolve it. I thought it would be as easy as I stated because I can call the Chisquare and NDF via:
root  double chisq=gfit->GetChisquare();
root  double ndf=gfit->GetNDF();
root  double chisqdf=chisq/ndf;
root  cout << "Chisquare: " << chisq << "/" << ndf << " : " << chisqdf << endl;
Chisquare: 19.0439/10 : 1.90439
So, I am left a bit confused. Any help would be great. Thank you.
for getting the function parameters like the mean of your gaussian do:
double mean = gfit->GetParameter("Mean");
double mean = gfit->GetParameter(1);