# Getting the number of bins from histogram in root

**URL:** <https://root-forum.cern.ch/t/getting-the-number-of-bins-from-histogram-in-root/50314>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [June 8, 2022, 12:45pm UTC](https://root-forum.cern.ch/t/getting-the-number-of-bins-from-histogram-in-root/50314 "2022-06-08T12:45:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AAA1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/9fc29f/32.png) [@AAA1](https://root-forum.cern.ch/u/AAA1)\
**Post date:** [June 8, 2022, 12:45pm UTC](https://root-forum.cern.ch/t/getting-the-number-of-bins-from-histogram-in-root/50314/1 "2022-06-08T12:45:36Z")

</div>

\_Hello

I have a histogram and I want to get the number of bins of this his. But I cant find any thing about that in the root manual . Is there any way to do that.

Thanks in advance

* * *

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 8, 2022, 12:52pm UTC](https://root-forum.cern.ch/t/getting-the-number-of-bins-from-histogram-in-root/50314/2 "2022-06-08T12:52:36Z")

</div>

Welcome to the ROOT forum.  
See [here](https://root.cern/doc/master/classTH1.html#a68ac42aeb65a689b5b32e1865b4dfd13).

---

<div class="post-metadata">

**Author:** ![AAA1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/9fc29f/32.png) [@AAA1](https://root-forum.cern.ch/u/AAA1)\
**Post date:** [June 9, 2022, 10:39am UTC](https://root-forum.cern.ch/t/getting-the-number-of-bins-from-histogram-in-root/50314/3 "2022-06-09T10:39:52Z")

</div>

thanks for that I use this code to get the number of bins in the hole histogram

```auto
H1F* hh2 = (TH1F*) gDirectory->Get("Pt_mu") ;
       Int_t *bins = new Int_t[hh2->GetSize()];

       for (Int_t i=0;i<hh2->GetNbinsX();i++)
 bins[i] = hh2->GetBinContent(i);
       cout<< bins;

```

but it does work, can anyone tell me what I miss

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 9, 2022, 11:43am UTC](https://root-forum.cern.ch/t/getting-the-number-of-bins-from-histogram-in-root/50314/4 "2022-06-09T11:43:00Z")

</div>

You do not want to get the number of bins, but the bin contents of all the bins? right?

---

<div class="post-metadata">

**Author:** ![AAA1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/9fc29f/32.png) [@AAA1](https://root-forum.cern.ch/u/AAA1)\
**Post date:** [June 9, 2022, 12:06pm UTC](https://root-forum.cern.ch/t/getting-the-number-of-bins-from-histogram-in-root/50314/5 "2022-06-09T12:06:43Z")

</div>

Yes the content of all the bins

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 9, 2022, 12:16pm UTC](https://root-forum.cern.ch/t/getting-the-number-of-bins-from-histogram-in-root/50314/6 "2022-06-09T12:16:51Z")

</div>

```auto
{
   auto h = new TH1F("h","h",10,-4,4);
   h->FillRandom("gaus",10000);
   h->Draw();
   float bins[10];

   int i;
   for (i=0; i<10; i++) bins[i] = h->GetBinContent(i+1);
   for (i=0; i<10; i++) printf("bin[%d] = %g\n",i,bins[i]);
}

```

```auto
root [0] .x aaa1.C
bin[0] = 5
bin[1] = 75
bin[2] = 456
bin[3] = 1563
bin[4] = 2885
bin[5] = 2890
bin[6] = 1550
bin[7] = 485
bin[8] = 81
bin[9] = 10
root [1] 

```

---

<div class="post-metadata">

**Author:** ![AAA1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/9fc29f/32.png) [@AAA1](https://root-forum.cern.ch/u/AAA1)\
**Post date:** [June 10, 2022, 9:30am UTC](https://root-forum.cern.ch/t/getting-the-number-of-bins-from-histogram-in-root/50314/7 "2022-06-10T09:30:12Z")

</div>

Thank you so much it works now.  
but what if I want only the bins between the range [0-4] on the x-axis.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 10, 2022, 10:35am UTC](https://root-forum.cern.ch/t/getting-the-number-of-bins-from-histogram-in-root/50314/8 "2022-06-10T10:35:06Z")

</div>

```auto
{
   auto h = new TH1F("h","h",10,-4,4);
   h->FillRandom("gaus",10000);
   h->Draw();
   float bins[10];

   int i,b=0;
   for (i=0; i<10; i++) {
      if (h->GetBinCenter(i)>=0 && h->GetBinCenter(i)<=4) {
         bins[b] = h->GetBinContent(i+1);
         b++;
      }
   }
   for (i=0; i<b; i++) printf("bin[%d] = %g\n",i,bins[i]);
}

```

```auto
root [0] .x aaa1.C
bin[0] = 1550
bin[1] = 485
bin[2] = 81
bin[3] = 10
root [1] 

```

Note that I used `GetBinCenter` to get the x value. You can decide for yourself what is the best way to compute x, and use a combination of `GetBinLowEdge` and `GetBinWidth`.
