Hi all, I’m just new to ROOT and I have a small question related to fitting a histogram using User defined function:
the context is, I want to fit a histogram (gaussian for the sake of simplicity), so I set up code like this:
//Create a pointer to histogram
TH1D *h1 = new TH1D("his","test fit funtion",10,-3,3);
//Fill data with 100 random data points from standard normal
for (int i = 0; i < 100; ++i) {
h1->Fill(gen3.Gaus());
}
//Then fit, it works perfectly fine
h1->Fit("gaus");
Then I change “gaus” to another function which is still “gaus” but different name
//Still "gaus"
double pos_dis(double *x, double *par) {
double arg = 0;
if (par[2]) arg = (x[0] - par[1])/par[2];
return par[0]*TMath::Exp(-0.5*arg*arg);
}
TF1 *fun_1 = new TF1("f",pos_dis,-1,1,3);
fun_1 -> SetParameters(10,h1->GetMean(), h1->GetRMS());
h1->Fit("f");
//It still works fine.
//Then i tweak it a little bit more by changing
//Now it's still "gaus" but I use pdf from TMath library
double pos_dis(double *x, double *par) {
return ROOT::Math::normal_pdf(x[0],par[1],par[0]);
}
this time, I have to normalize my histogram to get it drawn correctly or the fitted curve would be so tiny or even a straight line because the Y-axis represents "counts" while the function returns a pdf .
h1->Scale(1./h1->Integral("width");
h1->Fit("f")
I dont know why it didnt work just like 2 versions because I dont see any difference between them apart from syntax, they all return a pdf corressponding to bin center data points.
Thank you guys so much
ROOT Version: newest
Platform: Mac OS
Compiler: Not Provided