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

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

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: https://root.cern/doc/master/classTH1D.html#a4e0536d7ab2c883c26b2e4ea5b540334

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?

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 ?

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…

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

``````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 to fill the `h1` bins. The pseudo code is:

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

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