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 [41] 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 [42] h1.Fit("f1","H")
Unknown function: f1
(class TFitResultPtr)64677040
root [43]

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.

TF1 *f1 = new TF1("f1","gaus",-.5,.5);
// set initial parameters (not really needed for gaus)
f1->SetParameters(h1.GetMaximum(), h1.GetMean(), h1.GetRMS() );
h1.Fit("f1")

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.

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 [18] gfit.GetMean()
Error: Can't call TF1::GetMean() in current scope (tmpfile):1:
Possible candidates are...
(in TF1)
(in TFormula)
*** Interpreter error recovered ***
root [19]

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: