# How to draw and fit the histogram

**URL:** <https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374>\
**Category:** Newbie\
**Created:** [August 27, 2022, 11:32am UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374 "2022-08-27T11:32:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [August 27, 2022, 11:32am UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/1 "2022-08-27T11:32:35Z")

</div>

Hii experts,

I have a dat file and want to fill a histogram. Ensure that each bin contains at least 5 counts.  
Then after it will fit two Gaussian distributions. How to count at least 5 in each bin? kindly help me.  
Here i have attached the macro:

> TFile \*f1 = new TFile(“count.root”, “RECREATE”);  
> TTree \*tr = new TTree(“ntuple”, “data”);  
> tr-\>ReadFile(“hist\_plot.dat”, “x”);  
> TH1F \*h1 = new TH1F(“h1”, “Gaussian Distribution”, 20, -2.4, 2.9);  
> h1-\>GetXaxis()-\>SetTitle(“log\_{10}(Value)”);  
> h1-\>GetYaxis()-\>SetTitle(“Counts (Frequency)”);  
> tr-\>Project(“h1”, “log10(x)”);  
> f1-\>Write();  
> TCanvas \*c1 = new TCanvas();  
> c1-\>cd(1);  
> h1-\>Draw(“hist”);

Thanks,  
Anil

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [August 28, 2022, 9:05am UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/2 "2022-08-28T09:05:54Z")

</div>

Hi,  
I would do something like this

```auto
bool check_bins=kTrue;
int i=1;
do{

if(h1->GetBinContent(i)<5)
check_bins=kFalse;
i++;
}while(check_bins && i<=h1->GetNbinsX())

```

if checks\_bin is true after the do-while loop all your bins have more than 5 counts

Stefano

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [August 29, 2022, 4:41am UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/3 "2022-08-29T04:41:30Z")

</div>

Thank you @Dilicus. The first two bins contain less than 5 counts however others are fine. Should we add these two bin counts so that it might be at least 5?

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [August 29, 2022, 5:44am UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/4 "2022-08-29T05:44:32Z")

</div>

It is **wrong** to modify the data.  
What you can do is to exclude the first bins from the fit or better you can do a fit using the sum of a gaussian for the signal and a pol(0) or pol(1) for the background

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [August 29, 2022, 6:16am UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/5 "2022-08-29T06:16:12Z")

</div>

Ok will try. Thank you @Dilicus

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [September 10, 2022, 11:10am UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/6 "2022-09-10T11:10:40Z")

</div>

I want to fit double Gaussian into a histogram. Here is the macro:

> `TFile *f = new TFile("assign1.root"); `  
> `TH1F *h1 = (TH1F*)f->Get("h1"); `  
> `TF1 *g1 = new TF1("fit1", "gaus", -2.4,1.);`  
> ` g1->SetLineColor(kRed);`  
> ` TF1 *g2 = new TF1("fit2", "gaus", 0., 2.9);`  
> ` g2->SetLineColor(kGreen);`  
> `TF1 *f1 = new TF1("double_gaus", "gaus(0) + gaus(2)", -2.4, 2.9);`  
> ` f1->SetParNames("Constant 1", "Mean 1", "Sigma 1", "Constant 2", "Mean 2", "Sigma 2");`  
> `f1->SetLineColor(kBlue);`  
> `gStyle->SetOptFit(1);`  
> `h1->Fit(g1, "R");`  
> `h1->Fit(g2, "R+");`  
> `Double_t par[6];`  
> ` g1->GetParameters(&par[0]);`  
> ` g2->GetParameters(&par[2]);`  
> ` f1->SetParameters(par);`  
> ` h1->Fit(f1, "R+");`

I got the plot:

 ![Screenshot from 2022-09-10 16-21-30](https://root-forum.cern.ch/uploads/default/original/3X/1/a/1a238b8176c4bbd23112a93578877f662abbfb68.png)

In the graph, the red line does not end at xaxis. I want fit the red line into **small peak around -0.2** of the histogram. How do I do that. Kindly help me.

Thanks!  
Anil

---

<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:** [September 10, 2022, 11:17am UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/7 "2022-09-10T11:17:23Z")

</div>

`TF1 *f1 = new TF1("double_gaus", "gaus(0) + gaus(3)", -2.4, 2.9);`

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [September 10, 2022, 11:33am UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/8 "2022-09-10T11:33:08Z")

</div>

If I use `"gaus(0) + gaus(3)"`. The blue line flats on xaxis and red does not change.  
 ![Screenshot from 2022-09-10 17-01-38](https://root-forum.cern.ch/uploads/default/original/3X/8/0/80c17c2c0b2ea4be94e0fe9404c873cc5559e2fd.png)

---

<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:** [September 10, 2022, 12:30pm UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/9 "2022-09-10T12:30:14Z")

</div>

`TF1 *g1 = new TF1("fit1", "gaus", -2.4, 0.1);`  
`TF1 *g2 = new TF1("fit2", "gaus", 0.6, 2.9);`  
`g2->GetParameters(&par[3]);`

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [September 10, 2022, 2:59pm UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/10 "2022-09-10T14:59:28Z")

</div>

Here is the graph  
 ![Screenshot from 2022-09-10 20-25-37](https://root-forum.cern.ch/uploads/default/original/3X/c/9/c978a56001a68812a3d86cb5bb114663cea2f842.png)  
But the end and start point of red and green lines respectively are not meet to xaxis. Can we meet them?

---

<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:** [September 10, 2022, 3:07pm UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/11 "2022-09-10T15:07:24Z")

</div>

`TF1 *g1 = new TF1("fit1", "gaus", -3., 3.); h1->Fit(g1, "", "", -2.4, 0.1);`  
`TF1 *g2 = new TF1("fit2", "gaus", -3., 3.); h1->Fit(g2, "+", "", 0.6, 2.9);`

`h1->Fit(f1, "R+");`  
`g1->SetParameters(f1->GetParameters()); g2->SetParameters(f1->GetParameters() + 3);`  
`g1->Draw("SAME"); g2->Draw("SAME"); // f1->Draw("SAME");`

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [September 10, 2022, 3:42pm UTC](https://root-forum.cern.ch/t/how-to-draw-and-fit-the-histogram/51374/12 "2022-09-10T15:42:01Z")

</div>

Thank you very much @Wile_E_Coyote
