# New histogram filled with only number of bins from old histogram

**URL:** <https://root-forum.cern.ch/t/new-histogram-filled-with-only-number-of-bins-from-old-histogram/38523>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [March 22, 2020, 2:57pm UTC](https://root-forum.cern.ch/t/new-histogram-filled-with-only-number-of-bins-from-old-histogram/38523 "2020-03-22T14:57:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jitrusko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jitrusko/32/19224_2.png) [@Jitrusko](https://root-forum.cern.ch/u/Jitrusko)\
**Post date:** [March 22, 2020, 2:57pm UTC](https://root-forum.cern.ch/t/new-histogram-filled-with-only-number-of-bins-from-old-histogram/38523/1 "2020-03-22T14:57:47Z")

</div>

Hi all,

if I have a histogram with 10 000 bins and I need a new histogram containing only first 1000 bins from the old one - Is there an easy way to do this?

Thanks!

---

<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:** [March 22, 2020, 5:58pm UTC](https://root-forum.cern.ch/t/new-histogram-filled-with-only-number-of-bins-from-old-histogram/38523/2 "2020-03-22T17:58:29Z")

</div>

I don’t think there is any direct method to do it but maybe you could live with the original histogram. Many histogram methods respect the set “axis range”, e.g:  
`h10k->SetAxisRange(h10k->GetBinCenter(1), h10k->GetBinCenter(1000), "X");`

---

<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:** [March 23, 2020, 7:56am UTC](https://root-forum.cern.ch/t/new-histogram-filled-with-only-number-of-bins-from-old-histogram/38523/3 "2020-03-23T07:56:09Z")

</div>

To make a partial copy would need to make a a macro looping on the 1000 bins you want to copy and fill a new histogram with them. I guess. @moneta can confirm.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [March 23, 2020, 9:38am UTC](https://root-forum.cern.ch/t/new-histogram-filled-with-only-number-of-bins-from-old-histogram/38523/4 "2020-03-23T09:38:10Z")

</div>

If you want to avoid to loop and be fast you can use directly the histogram array of the bin content.  
So suppose h1 is your original histogram (type TH1D or TH1F) you can do something like:

```auto
auto h2 = new TH1D("h2","Histogram built with first 1000 bins of h1",1000, h1->GetXaxis()->GetBinLowEdge(1), h1->GetXaxis()->GetBinUpEdge(1000) );
// note that first array element is underflow bin 
std::copy( h1->GetArray()+1, h1->GetArray() + 1001, h2->GetArray()+1 ); 
// copy also error array if existing
if (h1->GetSumw2N() > 0) { 
  h2->Sumw2(); 
  std::copy( h1->GetSumw2()->GetArray()+1, h1->GetSumw2()->GetArray()+1001 , h2->GetSumw2()->GetArray()+1 ); 
}

```

Lorenzo

---

<div class="post-metadata">

**Author:** ![Jitrusko](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jitrusko/32/19224_2.png) [@Jitrusko](https://root-forum.cern.ch/u/Jitrusko)\
**Post date:** [March 23, 2020, 12:46pm UTC](https://root-forum.cern.ch/t/new-histogram-filled-with-only-number-of-bins-from-old-histogram/38523/5 "2020-03-23T12:46:45Z")

</div>

Thank you all for your help!

---

<div class="post-metadata">

**Author:** ![Haidar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/96bed5/32.png) [@Haidar](https://root-forum.cern.ch/u/Haidar)\
**Post date:** [April 13, 2021, 1:19pm UTC](https://root-forum.cern.ch/t/new-histogram-filled-with-only-number-of-bins-from-old-histogram/38523/6 "2021-04-13T13:19:59Z")

</div>

@moneta I also have a similar problem but in my case, the histogram has variable binning. What is the best way to treat that?
