# Generate a new histogram using center bins from another histogram

**URL:** <https://root-forum.cern.ch/t/generate-a-new-histogram-using-center-bins-from-another-histogram/41112>\
**Category:** Newbie\
**Created:** [August 24, 2020, 4:42am UTC](https://root-forum.cern.ch/t/generate-a-new-histogram-using-center-bins-from-another-histogram/41112 "2020-08-24T04:42:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![clkong](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@clkong](https://root-forum.cern.ch/u/clkong)\
**Post date:** [August 24, 2020, 4:42am UTC](https://root-forum.cern.ch/t/generate-a-new-histogram-using-center-bins-from-another-histogram/41112/1 "2020-08-24T04:42:17Z")

</div>

Hello everyone,

Please refer to to code below could draw a histogram h\_enu\_sub by getting the content from h\_ibd\_sub (which is a returned value / histogram that have been generated), Now I need to use the bin center of the “old” histogram “h\_ibd\_sub” to generate a new histogram “h\_enu\_sub”

TH1F\* h\_enu\_sub (TH1F\* enu, h\_ibd\_sub-\>GetBinContent(ibin));  
**for** ( **int** ibin = 1; ibin \<= h\_ibd\_sub-\>GetNbinsX(); ++ibin) {

**float** ep = h\_ibd\_sub-\>GetBinCenter(ibin);

**float** cos = 1;

**float** e = 0.51099895;

**float** p = 938.27208816;

**float** n = 939.56542052;

**float** k = ep - 2 \* e;

**float** q = sqrt (k \* k + 2 \* e \* k)

**float** enu = (k \* k - 2 \* k \* p + 2 \* e \* k - n \* n + p \* p - 2 \* e \* p - q \* q + e \* e) / (2 \* (-cos \* q + e + k - p));

TH1F\* h\_enu\_sub = (TH1F\*) h\_enu\_sub-\>Fill(enu, h\_ibd\_sub-\>GetBinContent(ibin));

**return** h\_enu\_sub

}

Your help will be appreciated

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 24, 2020, 5:45am UTC](https://root-forum.cern.ch/t/generate-a-new-histogram-using-center-bins-from-another-histogram/41112/2 "2020-08-24T05:45:41Z")

</div>

One way is creating the new histogram using an array that contains the lower edges of each bin, see [this](https://root.cern/doc/master/classTH1.html#a3ba472a2a1e853307c8368ddba833637) constructor.  
To create the array, you can just do, for each bin: lower\_edge = bin\_centre - bin\_width/2; e.g. in your loop over the bins:

```auto
low_e = h_ibd_sub->GetXaxis()->GetBinCenter(ibin) - h_ibd_sub->GetXaxis()->GetBinWidth(ibin)/2;
edges[ibin] = low_e;

```

GetBinCenter, GetBinWidth are described in the same page above.
