Fit a histogram using User defined funtion

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) {
//Then fit, it works perfectly fine

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());


//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  . 


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

Hi. Maybe I can help you: I think after your post that your histogram is not normalized to 1 (total area of the histogram=1) so when you use the function “gaus” you use a 3 parameter function and the first parameter (p0) is necessary if your total area is not 1(p1 and p2 are average and standard deviation), same thing for the function you defined. This 2 functions are not pdf because there is p0 as a parameter used in the fit
When you use ROOT::Math::normal_pdf(x[0],par[1],par[0]) you use a probability density function, which by definition is a function that is normalized to 1 (total area under function=1), that has only two parameters (average and standard deviation)
and that doesn’t have a parameter like p0 to correct for the fact that the histogram is not normalized to 1 ( because of its definition as a pdf!!).
I think this can be your problem and I hope I have not said a stupid thing. :sweat_smile: