Channel -> Energy Calibration: Histogram

I need to change the x-axis of my histograms, from their origional 1:1 channel number scale into a calibrated Energy axis.

In my case I have available a function for the required conversion - for example :
E(ch#) = ch/5000 MeV

how can I do this and leave the y-axis values as they were?

thanks

Hi,

you can either use GetBinContent/SetBinContent to fill a new histogram with the proper bin borders, or a little hack: use GetBinLabel to set the label to the result of your transformation as if it was text, e.g. looping over bin number i: hist->SetBinLabel(i, Form("%g",(i-1)/5000.)).

Cheers, Axel.

1 Like

[quote=“Axel”]or a little hack: use GetBinLabel to set the label to the result of your transformation as if it was text, e.g. looping over bin number i: hist->SetBinLabel(i, Form("%g",(i-1)/5000.)).
[/quote]
Axel, are there any side effects of TAxis::SetLimits()?

TAxis *axis = h->GetXaxis(); axis->SetLimits(h->GetXmin()/5000, h->GetXmax()/5000);

Thank you,
Max

1 Like

Hi Max,

no, your approach is fine for a linear conversion, so it should work for Chris, too. The SetBinLabel approach also works for non-linear axis conversions.

Cheers, Axel.

Max, thanks for the simple alternative. I think you meant:

TAxis *axis = h->GetXaxis();
axis->SetLimits(axis->GetXmin()/5000, axis->GetXmax()/5000);

Can we shift histogram for several channels?

1 Like