# After energy calibration, some histogram bins are empty

**URL:** <https://root-forum.cern.ch/t/after-energy-calibration-some-histogram-bins-are-empty/53539>\
**Category:** Newbie\
**Created:** [February 8, 2023, 9:19pm UTC](https://root-forum.cern.ch/t/after-energy-calibration-some-histogram-bins-are-empty/53539 "2023-02-08T21:19:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LUSI2323](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lusi2323/32/32395_2.png) [@LUSI2323](https://root-forum.cern.ch/u/LUSI2323)\
**Post date:** [February 8, 2023, 9:19pm UTC](https://root-forum.cern.ch/t/after-energy-calibration-some-histogram-bins-are-empty/53539/1 "2023-02-08T21:19:05Z")

</div>

Hello everybody!  
I am quite a beginner with ROOT and I have some problems with the energy calibration of my gamma spectra. Hope you can help me.  
I use PyROOT.  
Below you can find the code I am using:

 ![Schermata 2023-02-08 alle 22.10.32](https://root-forum.cern.ch/uploads/default/original/3X/9/9/994f75767875c83d4eedf71f1ed1f4702a65c2ff.png)  
For reference, channels are 8191, while xmin=-0.5 and xmax=8190.5  
And here is the result. As you can see some bins are empty. I have followed some suggestion about adding a random dE between 0 and 1 to the x\_new values, but it does not work.  
 ![Schermata 2023-02-08 alle 22.11.12](https://root-forum.cern.ch/uploads/default/original/3X/4/b/4bf28549eb8379ba19624d1e6aa2b5d5f024f7ff.png)

How should I proceed? Many many thanks!

\_ROOT Version:\_6.26/06  
_Platform:_ MacOS  
_Compiler:_ PyROOT

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 9, 2023, 6:09am UTC](https://root-forum.cern.ch/t/after-energy-calibration-some-histogram-bins-are-empty/53539/2 "2023-02-09T06:09:21Z")

</div>

Welcome to the ROOT forum.

Note that the histogram’s bins’ numbers start at 1. Not at 0. The first bin of an histogram with `nbins` has the number 1 and the last bin has the number `nbins`. So `xmin` and the range you are using in the loop are not appropriate.

Instead of using `GetBinCenter` to compute `xmin` and `xmax` it might be more appropriate to use `GetBinLowEdge` and `GetBinUpEdge`.

The new histogram `hcalib` is filled at the positions `z[1]+z[0]*i`. It is not clear what `z[0]` and `z[1]` are. It could be that with this value you sometimes hit the bins’ borders and the filling may end up in the wrong bin. Or it could be that this computation gives a wrong result and some bins are skipped. The screen dump you posted is not enough to fully understand the logic.

---

<div class="post-metadata">

**Author:** ![LUSI2323](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lusi2323/32/32395_2.png) [@LUSI2323](https://root-forum.cern.ch/u/LUSI2323)\
**Post date:** [February 9, 2023, 9:20am UTC](https://root-forum.cern.ch/t/after-energy-calibration-some-histogram-bins-are-empty/53539/3 "2023-02-09T09:20:55Z")

</div>

Thank you very much for your reply and suggestions!  
I was wrongly assuming the notation of the bins should start from the underflow ones.  
I tried changing those things, but the result is still the same.

Sorry, you are right I didn’t specify what z[1] and z[0] are. Basically they are the coefficients of a linear fit I performed using the channel number of peaks in 60Co and 137Cs sources spectra and the corresponding energy. Parameters evaluated are such that the linear relations should be:  
Energy=-2.8708+1.0035\*channel.  
One of the ‘empty’ bin in **hcalib** is for example 828.  
I don’t know if I am gettin ROOT’s logic right, but if I take channel=827 and apply my calibration, it gives me Energy=826.99(…) , but then channel=828 gives Energy=828.00659(…): does this imply that the xrange between 827 and 828 is ‘missing’ and the bin is skipped? If so, is there a way to solve the computational issue?

Many thanks again!

---

<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:** [February 9, 2023, 9:31am UTC](https://root-forum.cern.ch/t/after-energy-calibration-some-histogram-bins-are-empty/53539/4 "2023-02-09T09:31:21Z")

</div>

A better idea is to “calibrate” the original histogram:

```python
a = z[0]; b = z[1] # En = a * Ch + b ("channel to energy" calibration)
ax = h.GetXaxis() # an axis with fix bins (will be scaled with a linear function)
ax.Set(ax.GetNbins(), a * ax.GetXmin() + b, a * ax.GetXmax() + b)
h.ResetStats()

```

---

<div class="post-metadata">

**Author:** ![LUSI2323](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lusi2323/32/32395_2.png) [@LUSI2323](https://root-forum.cern.ch/u/LUSI2323)\
**Post date:** [February 10, 2023, 8:32am UTC](https://root-forum.cern.ch/t/after-energy-calibration-some-histogram-bins-are-empty/53539/5 "2023-02-10T08:32:12Z")

</div>

Thank you! This method indeed does not produce empty bins.
