Histogram from arrays


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)


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 (10.6 KB)

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


10095-01-02-00.txt (1.8 MB) 10095-01-02-00_reb.txt (11.9 KB)

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?

Maybe this:

   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]);
   }

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

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

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.

1 Like

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 :sweat_smile:

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

QPO.C (11.0 KB)

1 Like

Thank you :slight_smile: Now I see what you meant :slight_smile: