# Conversion of 2D histogram into a 1D array

**URL:** <https://root-forum.cern.ch/t/conversion-of-2d-histogram-into-a-1d-array/26166>\
**Category:** ROOT\
**Created:** [September 10, 2017, 6:26am UTC](https://root-forum.cern.ch/t/conversion-of-2d-histogram-into-a-1d-array/26166 "2017-09-10T06:26:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HIMANSHU\_SHARMA](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@HIMANSHU\_SHARMA](https://root-forum.cern.ch/u/HIMANSHU_SHARMA)\
**Post date:** [September 10, 2017, 6:26am UTC](https://root-forum.cern.ch/t/conversion-of-2d-histogram-into-a-1d-array/26166/1 "2017-09-10T06:26:50Z")

</div>

Hello  
I have a 2D histogram with total bins on x = 40, on y = 72. As this histo is 2D, I want to make it 1D by sequencing all the bins 1-after-1. One row will be advanced by the second row (appending row after row), there will be no columns.

 ![20170910_115111](https://root-forum.cern.ch/uploads/default/original/2X/2/2de2b9a0bfcbee61e7473d70ab732d907935f613.jpg)  
How can I do it?  
Thankyou 🙂  
PS: These all are bin numbers.

---

<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, 2017, 12:30pm UTC](https://root-forum.cern.ch/t/conversion-of-2d-histogram-into-a-1d-array/26166/2 "2017-09-10T12:30:23Z")

</div>

Try:

```auto
for (int ix = 1; ix <= 40; ix++)
  for (int iy = 1; iy <= 72; iy++)
    std::cout << ((ix - 1) * 72 + iy - 1) << " : "
              << YourTH2->GetBinContent(ix, iy) << std::endl;

```

---

<div class="post-metadata">

**Author:** ![HIMANSHU\_SHARMA](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@HIMANSHU\_SHARMA](https://root-forum.cern.ch/u/HIMANSHU_SHARMA)\
**Post date:** [September 10, 2017, 1:50pm UTC](https://root-forum.cern.ch/t/conversion-of-2d-histogram-into-a-1d-array/26166/3 "2017-09-10T13:50:55Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> YourTH2-\>GetBinContent(ix, iy)

What will this give, numbers of entities stored in the bin or data stored at each specific point inside the bin?

---

<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:** [September 11, 2017, 7:53am UTC](https://root-forum.cern.ch/t/conversion-of-2d-histogram-into-a-1d-array/26166/4 "2017-09-11T07:53:05Z")

</div>

> [@HIMANSHU\_SHARMA](#):
>
> What will this give

It will return the content of the bin ix,iy . Which is the number of time this cell (bin) was hit by the Fill method (if the weight of each call to Fill was 1. (default)).

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [September 25, 2017, 7:53am UTC](https://root-forum.cern.ch/t/conversion-of-2d-histogram-into-a-1d-array/26166/5 "2017-09-25T07:53:15Z")

</div>

Closed on September 25, 2017, 7:53am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
