Can't fit an histogram with the generating function for said histogram

I have to generate data according to a distribution defined by me and then I have to fit said data. My code is as follows:

#define N 50000
#define XMin 0.0
#define XMax 2.0
#define Mu1 1.0
#define Sigma1 0.20
#define Mu2 1.50
#define Sigma2 0.10
#define a 0.3
#define b 0.2
#define t 3
#define NPar 7
#define BinDim 0.05
#define NBins (XMax-XMin)/BinDim

Double_t GeneratingFunction(Double_t *xx, Double_t *par);

void es(){
TH1D *histo=new TH1D(“h1”, “N gen”, NBins, XMin, XMax);

 TF1 *f1 = new TF1("f1",GeneratingFunction,XMin,XMax,NPar);
 f1 -> SetParameters(a,b,t,Mu1,Sigma1,Mu2,Sigma2);
 f1 -> SetParNames("a","b","t","Mu1","Sigma1","Mu2","Sigma2");
 for(int i=0; i<N; i++){
      histo -> Fill(f1->GetRandom());
 }
 gStyle->SetOptFit(kTRUE);
 histo -> Fit(f1);
 histo -> Draw();

}

Double_t GeneratingFunction(Double_t *xx, Double_t *par){
Double_t first, second, third;
first = par[0]*TMath::Gaus(xx[0],par[3],par[4]);
second = par[1]*TMath::Gaus(xx[0],par[5],par[6]);
third = (1-par[0]-par[1])*TMath::Exp(-par[2]*xx[0]);
return first+second+third;
}

From the graph it’s obvious something is wrong here but I can’t seem to understand what; any help is very appreciated.


ROOT Version: 6.22/06
_Platform:_Ubuntu 20.10
Compiler: g++ (Ubuntu 10.2.0-13ubuntu1) 10.2.0


histo->Scale(f1->Integral(XMin, XMax) / N, "width");
histo->Fit(f1, "WL");

Thank you for the answer it does solve the problem but I’m not sure I understand what the issue is. From your answer it seems to me that the issue is the histogram normalization is this correct? I tried redefining the fitting function by adding an overall parameter

return par[7]+first+second+third

and although the histogram was heavily improved it was far worse than the one I obtained by scaling.

return par[7] * (first + second + third);