Histogram bins shifted


I’m tring overlapping 2 histograms from a ttree. First I set the same number of bins for both and same range, and then simply I draw one of 2 and the I draw the second one with draw(same).
But I have 2 problems:

Rebinnin the histogram, all data shift in left direction. Before the maximum entry was 0.662, then after set 1000 bins the maximum entry is 0.0662. There is a shift facto 10.
The second problem is that thedata shifs not only of a factor 10 but also of 1 bin in the right direction.

Here a pice of code:

TH1D* Ek_incidenti_tot = (TH1D*)ttree_incident_TOT->GetHistogram()->Clone();

G4double min_Ek_incidenti_tot= Ek_incidenti_tot->GetXaxis()->GetBinCenter(0);
G4double max_Ek_incidenti_tot= Ek_incidenti_tot->GetXaxis()->GetBinCenter(Ek_incidenti_tot->GetNbinsX());

Ek_incidenti_tot->SetBins(1000, min_Ek_incidenti_tot, max_Ek_incidenti_tot);

TH1D* Ek_incidenti_signal = (TH1D*)ttree_Ek_incident_signal->GetHistogram()->Clone();
Ek_incidenti_signal->SetBins(1000, min_Ek_incidenti_tot, max_Ek_incidenti_tot);


I tried also to overlap them manually from the root panel and in this way I don’t have problem.

In attached there is the also the root file. The plots wrong is the canvas saved as: “Ek_TOT_incident-signal”

14_07_2017_18_21_58_GIFpp.root (929.7 KB)

Thanks a lot in advance :slight_smile:

It doen’t make sense to increase the bi size after filling. By default root creates histograms with 100 bins. If you then (later) change 100 to 1000, you split every bin in 10. Thus it makes sense that the maximum is divided by 10.

So to fix your problem, you need to request 1000 bins already when drawing, not after drawing (that’s too late).
You can either create the histogram before drawing like this

auto h = new TH1D("hKINETIC_ENERGY_TOT", "KINETIC_ENERGY_TOT", 1000, 0, 0); 
                // instead of 0,0, you can pass other min/max of course

or you can request the number of bins directly in the Draw command:


In case ROOT developers are reading this: the doxygen for SetBins is a little odd (I can’t parse the sentence with “if errors the errors”, also are prev bin contents really lost or are they distributed evenly into the new bins? Or is it undefined?):
SetBins documentation: “The bins content array is resized if errors (Sumw2) the errors array is resized The previous bin contents are lost To change only the axis limits, see TAxis::SetRange”

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