# HISTOGRAM fill

**URL:** <https://root-forum.cern.ch/t/histogram-fill/51534>\
**Category:** Newbie\
**Created:** [September 11, 2022, 2:11pm UTC](https://root-forum.cern.ch/t/histogram-fill/51534 "2022-09-11T14:11:23Z")\
**Posts on this page:** 5\
**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:** [September 11, 2022, 2:11pm UTC](https://root-forum.cern.ch/t/histogram-fill/51534/1 "2022-09-11T14:11:23Z")

</div>

Hi experts,  
How to fill a histogram in which each bin will contain at least 5 counts.  
Here is the macro:

```c++
  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)");   
  bool checkBins=true;
  int count, sum;
  for( int i=1; i<=20; i++) {
  do {
  if(h1->GetBinContent(i)<5) 
  checkBins=false; 
// count = h1->GetBinContent(i); 
// sum = count + h1->GetBinContent(i+1); }
// else { continue;}
}
  while(checkBins && i <= h1->GetNbinsX());   
  tr->Project("h1", "log10(x)");
  f1->Write();   
  h1->Draw("hist"); 

```

However first two bins contain less than 5 counts, other bins are fine. How do I add these two bins to the next bin?

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:** [September 11, 2022, 2:43pm UTC](https://root-forum.cern.ch/t/histogram-fill/51534/2 "2022-09-11T14:43:32Z")

</div>

Hi,  
you can try to reduce the number of bins from 20 to 19 or 18.

---

<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 11, 2022, 3:30pm UTC](https://root-forum.cern.ch/t/histogram-fill/51534/3 "2022-09-11T15:30:27Z")

</div>

Thank you @Dilicus  
If i reduced the number of bins to 18 or 15 or 12 , first two bins contain less than 5 and at 10 only first bin contains 1 count.  
However if number of bins reduced to 5, all the bins contain more than 5 counts. In case of gaussian fit to this histogram, should it be relevant?

---

<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 11, 2022, 3:31pm UTC](https://root-forum.cern.ch/t/histogram-fill/51534/4 "2022-09-11T15:31:55Z")

</div>

It is not relevant for the fit at all (and you can always set some “`xmin`” and “`xmax`” when using the [`TH1::Fit`](https://root.cern/doc/master/classTH1.html#a7e7d34c91d5ebab4fc9bba3ca47dabdd) method).

---

<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 11, 2022, 3:44pm UTC](https://root-forum.cern.ch/t/histogram-fill/51534/5 "2022-09-11T15:44:06Z")

</div>

Ok. Thank you @Wile_E_Coyote
