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

**URL:** <https://root-forum.cern.ch/t/cant-fit-an-histogram-with-the-generating-function-for-said-histogram/43348>\
**Category:** Newbie\
**Tags:** roofit\
**Created:** [February 1, 2021, 11:20am UTC](https://root-forum.cern.ch/t/cant-fit-an-histogram-with-the-generating-function-for-said-histogram/43348 "2021-02-01T11:20:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zero1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zero1/32/24444_2.png) [@zero1](https://root-forum.cern.ch/u/zero1)\
**Post date:** [February 1, 2021, 11:20am UTC](https://root-forum.cern.ch/t/cant-fit-an-histogram-with-the-generating-function-for-said-histogram/43348/1 "2021-02-01T11:20:00Z")

</div>

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

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 1, 2021, 11:46am UTC](https://root-forum.cern.ch/t/cant-fit-an-histogram-with-the-generating-function-for-said-histogram/43348/2 "2021-02-01T11:46:40Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![zero1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/zero1/32/24444_2.png) [@zero1](https://root-forum.cern.ch/u/zero1)\
**Post date:** [February 1, 2021, 12:08pm UTC](https://root-forum.cern.ch/t/cant-fit-an-histogram-with-the-generating-function-for-said-histogram/43348/3 "2021-02-01T12:08:33Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 1, 2021, 12:25pm UTC](https://root-forum.cern.ch/t/cant-fit-an-histogram-with-the-generating-function-for-said-histogram/43348/4 "2021-02-01T12:25:10Z")

</div>

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