# Histogram from arrays

**URL:** <https://root-forum.cern.ch/t/histogram-from-arrays/36778>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [November 12, 2019, 10:52am UTC](https://root-forum.cern.ch/t/histogram-from-arrays/36778 "2019-11-12T10:52:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arianna](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arianna/32/20222_2.png) [@Arianna](https://root-forum.cern.ch/u/Arianna)\
**Post date:** [November 12, 2019, 10:52am UTC](https://root-forum.cern.ch/t/histogram-from-arrays/36778/1 "2019-11-12T10:52:45Z")

</div>

* * *

_ROOT Version:_ 5.34  
_Platform:_ Linux Virtual Machine on WIndows10 host  
_Compiler:_ CINT/ROOT C/C++ Interpreter version 5.18.00

* * *

Hello. I’m using ROOT for my astrophysics thesis and I have to recreate a power density spectrum from a .dat file with 4 columns. After reading the file and applying a geometric rebin, I have my data in _arrays_ (frequency, power and respective errors) and I need to create a histogram. I did it in this way

> TH1F \*pds\_rebin= new TH1F(“pds\_rebin”,“Rebin logaritmico”,dim,0,2048); //histogram

> for(int i=0; i\<dim;i++){

> pds\_rebin-\>SetBinContent(i+1,potenza\_rebin[i]); //fill histogram  
> pds\_rebin-\>SetBinError(i+1,err\_pot\_rebin[i]);  
> }

where ‘potenza’ is the powers array, ‘err\_pot\_rebin’ is the powers’ errors array and ‘dim’ is the dimension of the arrays (330).

Things work mostly fine (without errors), if it wasn’t that (after subtracting a background), I get my peak at too high frequencies (the red Lorentzian at around 1700 Hz)

 ![thing](https://root-forum.cern.ch/uploads/default/original/3X/4/9/4926fcebe975c7bd5360b857416fafa9593fc19f.jpeg)  
while on gnuplot I can see it where it should be, around 850 Hz.

It can be some problem in the way I’m creating the histogram?

I’m adding the full macro and data files (I had to change .dat to .txt to upload them here):

> **Data**
>
> [QPO.C](https://root-forum.cern.ch/uploads/short-url/gvRrC7ZvK8QDX2dsalkC3vZGiOd.C) (10.6 KB)

---

<div class="post-metadata">

**Author:** ![Arianna](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arianna/32/20222_2.png) [@Arianna](https://root-forum.cern.ch/u/Arianna)\
**Post date:** [November 12, 2019, 10:53am UTC](https://root-forum.cern.ch/t/histogram-from-arrays/36778/2 "2019-11-12T10:53:33Z")

</div>

(Sorry, couldn’t put it in the first post) Gnuplot plot

 ![gnu](https://root-forum.cern.ch/uploads/default/original/3X/b/2/b2bb8aa09b36b686fc2831f2e89fd62b0e4523fd.png)  
[10095-01-02-00.txt](https://root-forum.cern.ch/uploads/short-url/xwNnR7Q8qI2VakJu9KY3VHxRcSn.txt) (1.8 MB) [10095-01-02-00\_reb.txt](https://root-forum.cern.ch/uploads/short-url/h1nBKz9Hjk9Ixco4avuUrLgy1hU.txt) (11.9 KB)

---

<div class="post-metadata">

**Author:** ![Arianna](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arianna/32/20222_2.png) [@Arianna](https://root-forum.cern.ch/u/Arianna)\
**Post date:** [November 12, 2019, 11:42am UTC](https://root-forum.cern.ch/t/histogram-from-arrays/36778/4 "2019-11-12T11:42:12Z")

</div>

Mm, the last point on the x axis in gnuplot is 2035 (it’s logarithmic scale) so it actually covers all the data, if that’s what you mean. Did I understand it correctly?

---

<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:** [November 12, 2019, 4:50pm UTC](https://root-forum.cern.ch/t/histogram-from-arrays/36778/5 "2019-11-12T16:50:54Z")

</div>

Maybe this:

```cpp
   if (n > dim) std::cout << "Fatal error: " << n << " > " << dim << std::endl;
   frequenza_rebin[n] = frequenza[dim_array - 1] + err_freq[dim_array - 1]; // the most right edge
   TH1F *pds_rebin= new TH1F("pds_rebin", "Rebin logaritmico", n, frequenza_rebin); // istogramma rebin log
   for(int i = 0; i < n; i++) {
     pds_rebin->SetBinContent(i + 1, potenza_rebin[i]); // riempi istogramma rebinnato
     pds_rebin->SetBinError(i + 1, err_pot_rebin[i]);
   }

```

---

<div class="post-metadata">

**Author:** ![Arianna](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arianna/32/20222_2.png) [@Arianna](https://root-forum.cern.ch/u/Arianna)\
**Post date:** [November 12, 2019, 10:17pm UTC](https://root-forum.cern.ch/t/histogram-from-arrays/36778/6 "2019-11-12T22:17:31Z")

</div>

I am actually pretty certain of the values in the array of frequencies, since I got the same results with another script.

> [@Wile\_E\_Coyote](#):
>
> frequenza\_rebin[n] = frequenza[dim\_array - 1] + err\_freq[dim\_array - 1]

What is this used for? Does it shift the values one step backwards?

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 14, 2019, 9:34am UTC](https://root-forum.cern.ch/t/histogram-from-arrays/36778/7 "2019-11-14T09:34:51Z")

</div>

So the issue is likely that you use linear histogram bins in ROOT but weird-scale scale ones in Gnuplot: compare the bin widths say at frequency 1000: for ROOT, the bin width is invisible, while for Gnuplot its largish.

You likely have non-uniform bin boundaries. Are some of your columns “frequency” values, describing the bin? If so, don’t create the histogram using `TH1F *pds_rebin= new TH1F(...,dim,0,2048);` which will create `dim` equally sized bins between 0 and 2048, but provide the bin borders as a `float` or `double` array. Note that for `dim` bins you need `dim + 1` bin boundaries: each of the `dim` bins has a lower boundary, and the last one also needs to know it’s upper bin boundary!

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![Arianna](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arianna/32/20222_2.png) [@Arianna](https://root-forum.cern.ch/u/Arianna)\
**Post date:** [November 14, 2019, 10:27am UTC](https://root-forum.cern.ch/t/histogram-from-arrays/36778/8 "2019-11-14T10:27:05Z")

</div>

Thank you. I understand the process, but how can I know what are the best boundaries to give the bins? Maybe it’s a ‘very newbie’ question 😅

> [@Axel](#):
>
> Are some of your columns “frequency” values, describing the bin?

They are describing my data in the bins, not the bins themselves.

---

<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:** [November 14, 2019, 1:59pm UTC](https://root-forum.cern.ch/t/histogram-from-arrays/36778/9 "2019-11-14T13:59:51Z")

</div>

[QPO.C](https://root-forum.cern.ch/uploads/short-url/oHyCsUbeg8tyszDAOtZ4i4aJTG6.C) (11.0 KB)

---

<div class="post-metadata">

**Author:** ![Arianna](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arianna/32/20222_2.png) [@Arianna](https://root-forum.cern.ch/u/Arianna)\
**Post date:** [November 14, 2019, 6:28pm UTC](https://root-forum.cern.ch/t/histogram-from-arrays/36778/10 "2019-11-14T18:28:44Z")

</div>

Thank you 🙂 Now I see what you meant 🙂
