I have a TH1F named hist and I want to fit a Gaussian to it, if I do it through "Fit Panel", it produces desired result in just a single click, it somehow initializes the parameters smartly, so sometimes single click sometimes double… maximum three clicks give me perfect fit. But if I do it manually like this:
root [5] Double_t entr = mytree->GetEntries(); // will be useful to estimate Gaussian amplitude
root [6] Double_t mean = mytree->GetMean(); // guessing the Gaussian mean
root [7] Double_t height = entr/400.; // it's a (not so bad) guess for the amplitude of the Gaussian
root [8] TF1 *myfit = new TF1("myfit","[0]*exp(-0.5*((x-[1])/[2])**2)", 1500, 14000);
root [9] myfit->SetParameter(0,height); //amplitude or height
root [10] myfit->SetParameter(1,mean); //mean
root [11] myfit->SetParameter(2,1/height); //sigma
root [12] hist->Fit("myfit")
where “height” is my guess for the amplitude of the Gaussian and 1/height is for the sigma, mean is the mean of the histogram that also should be a good initialization for the Gaussian mean. This fit just fails… gives me a flat line ! I guess it just tries once… what I want is an iteration of some sort so that it minimizes the gaussian against the actuall data. How do I achieve that ?
Your fit probably fails because the estimate of the initial parameter is not so good. If you use the “gaus” pre-defined function it will work as in the fit panel.
Hi Lorenzo, the problem is I don’t know how to get/set the parameters in a pre-defined gaus function as I know in my own definition. I don’t know how to call the parameters ! Will this:
myfit->SetParameter(0,some_number);
will still mean the height of the Gaussian ? Do I need to specify ranges of parameters ? How do I specify which method to use to carry out the fitting, like Chsquare fitting… then other settings like “minuit” etc… how do I implement them in the macro ? Anyway, I will try something now and will update here.
Thanks Pepe, but how do I get a specific parameter ? For example I need to store the "mean"s and "sigma"s in another file, that’s why I need “GetParameter” kind of something.
I have encountered another problem, I would like to select only a particular range of the x-axis (10000 to 20000) to carry out the fitting for my histogram “h” that starts from 0, so I am defining (using pre-defined gaus):
TF1 *f1 = new TF1("f1","gaus",10000,20000)
and trying to fit using h->Fit("f1"); . But seems like “f1” still tries to fit over the whole range… that is 0 to 20000 ! I don’t understand this strange behavior… if I have specified f1-lower limit 10000 shouldn’t it be blind to points below that range ? In the Fit Panel of course everything works… there we can crop the histogram to fit.
Hi sorry to bother you again “R” option works like a charm but then if I want to use two options, like “R” and “Q” to silence the output… only the first option works:
hist->Fit("f2","Q","R")
in this case only “Q” works “R” doesn’t and in
->Fit("f2","R","Q")
“R” works, “Q” doesn’t ! How can I activate both… or let’s say how can I use multiple options together… n of them ?