# How to map 2D array to 1D array and drawing resulting 1D histogram

**URL:** <https://root-forum.cern.ch/t/how-to-map-2d-array-to-1d-array-and-drawing-resulting-1d-histogram/36746>\
**Category:** Newbie\
**Created:** [November 9, 2019, 10:36pm UTC](https://root-forum.cern.ch/t/how-to-map-2d-array-to-1d-array-and-drawing-resulting-1d-histogram/36746 "2019-11-09T22:36:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mulb](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mulb](https://root-forum.cern.ch/u/mulb)\
**Post date:** [November 9, 2019, 10:36pm UTC](https://root-forum.cern.ch/t/how-to-map-2d-array-to-1d-array-and-drawing-resulting-1d-histogram/36746/1 "2019-11-09T22:36:04Z")

</div>

Hello everyone,  
Searched root tutorials but found nothing similar to my task yet.

How can I write a program to map a 2D array to a 1D array and finally draw the resulting 1D array into a dimensional histogram using C++/ROOT?

\_ROOT Version: 6.16/00  
\_Platform: macosx64  
\_Compiler:clang-1100.0.33.12

---

<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:** [November 11, 2019, 8:02am UTC](https://root-forum.cern.ch/t/how-to-map-2d-array-to-1d-array-and-drawing-resulting-1d-histogram/36746/2 "2019-11-11T08:02:20Z")

</div>

> [@mulb](#):
>
> I have 12 phi bins total.  
> 6phi bins on negative x axis and 6 phi bins on positive x axis.  
> Each negative phi bin has 5 pt bins, so 30 pt bins in negative 6 phi bins.  
> Each positive phi bin has 5 pt bins, so 30 pt bins in positive 6 phi bins.

It looks like a 1D histogram with 10 bins . The only missing parameters are the low and upper limits. The lower limits is negative and the upper posisbitve but that’s all what you said. See the TH1D constructor here: [ROOT: TH1D Class Reference](https://root.cern/doc/master/classTH1D.html#a4e0536d7ab2c883c26b2e4ea5b540334)

---

<div class="post-metadata">

**Author:** ![mulb](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mulb](https://root-forum.cern.ch/u/mulb)\
**Post date:** [November 12, 2019, 3:11pm UTC](https://root-forum.cern.ch/t/how-to-map-2d-array-to-1d-array-and-drawing-resulting-1d-histogram/36746/3 "2019-11-12T15:11:03Z")

</div>

Hi couet,

Thanks a lot for the reply. I think my question was not very clear. Let me rephrase this way:

How can I write a program to map a 2D array to a 1D array and finally draw the resulting 1D array into a dimensional histogram using C++/ROOT?

---

<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:** [November 12, 2019, 3:39pm UTC](https://root-forum.cern.ch/t/how-to-map-2d-array-to-1d-array-and-drawing-resulting-1d-histogram/36746/4 "2019-11-12T15:39:21Z")

</div>

There is several way to do a “mapping” from the 2D array in a 1D array…  
You can avoid this “mapping” and fill directly the 1D histogram from the 2D array  
but here again there is several ways … can you clarify how the lines and columns should be combined ?

---

<div class="post-metadata">

**Author:** ![mulb](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mulb](https://root-forum.cern.ch/u/mulb)\
**Post date:** [November 12, 2019, 7:50pm UTC](https://root-forum.cern.ch/t/how-to-map-2d-array-to-1d-array-and-drawing-resulting-1d-histogram/36746/5 "2019-11-12T19:50:27Z")

</div>

Hello couet,  
Yes, I want the lines and columns to be combined this way:  
array[][] :low phi = -3, upper phi = 0 (Negative side = 3 bins) and low phi = 0, upper phi = 3 (Positive side = 3 bins)  
:low pt = -0.25, upper pt = 0 (Negative side = 30 bins) and low pt = 0, upper pt = 0.25 (positive side = 30 bins)

The above 2D is what I want to map to 1D or fill directly into 1D histogram from the above 2D array above as you mentioned above. Is this clear?  
If not clear, could you help me with any code that maps 2D array to 1D array and finally draws a 1D histogram or any code that directly fill the 1D histogram from the 2D array? Thank you…

---

<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:** [November 13, 2019, 8:01am UTC](https://root-forum.cern.ch/t/how-to-map-2d-array-to-1d-array-and-drawing-resulting-1d-histogram/36746/6 "2019-11-13T08:01:01Z")

</div>

You should book a 1D histogram. The pseudo code is:

```auto
int number_of_bins = ... ;
double xmin = ... ;
double xmax = ... ;
TH1D *h1 = new TH1D("h1", "h1", number_of_bins, xmin, xmax);

```

Then you loop on the1D histogram bins, compute the value `v` (from the array) to be put in each bin. Use [SetBinContent](https://root.cern/doc/master/classTH1.html#a9599e2dae40c574fd9d8970aaf638ee6) to fill the `h1` bins. The pseudo code is:

```auto
double v;
for (int i =1, i<= number_of_bins; i++) {
    ... compute v from the 2D array
   h1->SetBinContent(i, v);
 }

```

---

<div class="post-metadata">

**Author:** ![mulb](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mulb](https://root-forum.cern.ch/u/mulb)\
**Post date:** [November 13, 2019, 5:32pm UTC](https://root-forum.cern.ch/t/how-to-map-2d-array-to-1d-array-and-drawing-resulting-1d-histogram/36746/7 "2019-11-13T17:32:53Z")

</div>

Thanks a lot couet. Let me try this out.  
I really appreciate~
