# Empty bins after calibration

**URL:** <https://root-forum.cern.ch/t/empty-bins-after-calibration/36046>\
**Category:** Newbie\
**Created:** [September 16, 2019, 3:20pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046 "2019-09-16T15:20:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christoph\_Vogl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/christoph_vogl/32/19258_2.png) [@Christoph\_Vogl](https://root-forum.cern.ch/u/Christoph_Vogl)\
**Post date:** [September 16, 2019, 3:20pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/1 "2019-09-16T15:20:18Z")

</div>

Greetings,

I am having difficulties calibrating an energy spectrum.

I have a vector ENERGY which I fill an histogram with. The result looks fine (uncal.pdf).

However, after I apply the calibration  
`ENERGY_CAL[i] = channel2Energy(ENERGY[i]),`  
where channel2Energy is a linear function, I fill another histogram with ENERGY\_CAL and get a weird looking result (cal.pdf).

I already tried using a different binning for the second histogram but I cannot get it to look nice.

Thanks for your time!  
Christoph

[uncal.pdf](https://root-forum.cern.ch/uploads/short-url/8SSIjyHImir4CwzG3KPeHmnc53K.pdf) (77.4 KB)  
[cal.pdf](https://root-forum.cern.ch/uploads/short-url/8ngg1qQUvkg5x0uKxhR2gPFHYHJ.pdf) (92.8 KB)

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [September 16, 2019, 3:25pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/2 "2019-09-16T15:25:08Z")

</div>

Hi Christoph, it is because you pass from integer values (channel) to continuous values (energy). To smooth your spectrum, in your function `channel2Energy`, you should add a random number (between 0 and 1) to the channel value.  
Doing so smooths your calibrated spectrum because when you have channel with value 10, the “exact” value is between 10 and 11. Adding a random value solves this “issue”.

---

<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:** [September 16, 2019, 3:25pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/3 "2019-09-16T15:25:40Z")

</div>

Maybe it will be sufficient to try:

```auto
Double_t dE = hSpectrumCalibrated->GetBinWidth(1) / 2.0; // half of the "fixed" bin width
// ...
hSpectrumCalibrated->Fill(ENERGY_CAL[i] + dE);

```

However, in principle, you should create a histogram with [variable bin sizes](https://root.cern.ch/doc/master/classTH1.html).

See also:

> [@Can we shift histogram for several channels?](https://root-forum.cern.ch/t/can-we-shift-histogram-for-several-channels/12050/2):
>
> I’m not sure if this is fully kosher, but suit yourself : #include "TAxis.h" #include "TH1.h" #include "TArrayD.h" Double\_t ScaleX(Double\_t x) { Double\_t v; v = 10 \* x + 100; // "linear scaling" function example return v; } Double\_t ScaleY(Double\_t y) { Double\_t v; v = 20 \* y + 200; // "linear scaling" function example return v; } Double\_t ScaleZ(Double\_t z) { Double\_t v; v = 30 \* z + 300; // "linear scaling" function example return v; } void ScaleAxis(TAxis \*a, Double\_t (…

---

<div class="post-metadata">

**Author:** ![Christoph\_Vogl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/christoph_vogl/32/19258_2.png) [@Christoph\_Vogl](https://root-forum.cern.ch/u/Christoph_Vogl)\
**Post date:** [September 16, 2019, 3:30pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/4 "2019-09-16T15:30:55Z")

</div>

Actually ENERGY does not have integer values. The function `channel2Energy` is named very badly, I know. It _was_ used to convert channels once, that’s why it is still called like that.  
Some examples:

```auto
i=0, ENERGY=752.125345, ENERGY_CAL=173.638841
i=10000, ENERGY=11236.404687, ENERGY_CAL=2612.239957
i=20000, ENERGY=9045.618917, ENERGY_CAL=2102.608610
i=30000, ENERGY=281.856741, ENERGY_CAL=64.083220
i=40000, ENERGY=9664.323836, ENERGY_CAL=2246.589351
i=50000, ENERGY=2040.249410, ENERGY_CAL=473.230432
i=60000, ENERGY=3721.239336, ENERGY_CAL=864.234674
i=70000, ENERGY=2446.173003, ENERGY_CAL=567.666911
i=80000, ENERGY=3925.829752, ENERGY_CAL=911.685516
i=90000, ENERGY=11248.942738, ENERGY_CAL=2615.031183
i=100000, ENERGY=3133.295953, ENERGY_CAL=727.464600

```

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [September 16, 2019, 3:43pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/5 "2019-09-16T15:43:39Z")

</div>

Ok, but this effect comes clearly from the fact you started with integer values that you converted to continuous values. So, if you do not have the channel values anymore, maybe you can try what @Wile_E_Coyote has proposed.

---

<div class="post-metadata">

**Author:** ![Christoph\_Vogl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/christoph_vogl/32/19258_2.png) [@Christoph\_Vogl](https://root-forum.cern.ch/u/Christoph_Vogl)\
**Post date:** [September 16, 2019, 4:15pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/6 "2019-09-16T16:15:17Z")

</div>

The easy solution with `dE` did not work for me.

I looked at your link and tried out the scaling for histograms with fixed size. Explicitly I did

```auto
TH1F* hSpectrumCalibrated = (TH1F*) hSpectrumUncalibrated->Clone("hSpectrumCalibrated");
auto a = hSpectrumCalibrated->GetXaxis();
a->Set( a->GetNbins(), 0, channel2Energy(fCalibration, a->GetXmax()) );

```

which produces exactly the spectrum I want! very nice.

Sadly this does break something further down in my code, what could I have changed?  
Why should I create a histogram with variable bin size? At least something seems to have been fixed with this approach.

---

<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:** [September 16, 2019, 5:56pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/7 "2019-09-16T17:56:24Z")

</div>

In your current approach, you should use (instead of “0”):  
`channel2Energy(fCalibration, a->GetXmin())`  
and in the end you need to add:  
`hSpectrumCalibrated->ResetStats();`

If at some point your “channel2Energy” function is no longer linear then the energy bins’ widths will not be constant / equal (so you will need a histogram with [variable bin sizes](https://root.cern.ch/doc/master/classTH1.html)).

BTW. If “+ dE” didn’t work well then you might try “- dE” (or e.g. “+ dE / 2.0” or “- dE / 2.0”).

---

<div class="post-metadata">

**Author:** ![Christoph\_Vogl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/christoph_vogl/32/19258_2.png) [@Christoph\_Vogl](https://root-forum.cern.ch/u/Christoph_Vogl)\
**Post date:** [September 17, 2019, 9:29am UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/8 "2019-09-17T09:29:02Z")

</div>

Thank you very much!  
The problem has already been solved by your previous answer. The crash was my fault.

However, now the number of entries has been reduced even though I did `ResetStats()` and everything looks fine now.

Something else also still bothers me. I don’t understand why it should make a difference if I calculate ENERGY\_CAL and fill a histogram with it, or if I simply scale an axis if I use the same calibration/scaling function in both cases. I have constructed an example creating a histogram hSpectrumCalibrated2 which displays the difference in both cases (see attached pdfs):

```auto
hSpectrumUncalibrated = new TH1F("hSpectrumUncalibrated", "Th-228 -- uncalibrated", nEnergyBins, 0, nEnergyBins-1);
// ...
TH1F * hSpectrumCalibrated = new TH1F("hSpectrumCalibrated", "Th-228 -- calibrated", nEnergyBins, channel2Energy(fCalibration, 0), channel2Energy(fCalibration, nEnergyBins-1));
  for(Int_t i=0; i<nEVENTS; i++) {
    ENERGY_CAL[i] = channel2Energy(fCalibration, ENERGY[i]);
    hSpectrumCalibrated->Fill(ENERGY_CAL[i]);
  }

TH1F* hSpectrumCalibrated2 = (TH1F*) hSpectrumUncalibrated->Clone("hSpectrumCalibrated2");
hSpectrumCalibrated2->SetTitle("Th-228 calibrated2");
hSpectrumCalibrated2->SetXTitle("energy (keV)");
hSpectrumCalibrated2->SetYTitle("counts");
auto a = hSpectrumCalibrated2->GetXaxis();
a->Set( a->GetNbins(), channel2Energy(fCalibration, a->GetXmin()), channel2Energy(fCalibration, a->GetXmax()) );
hSpectrumCalibrated2->ResetStats();

hSpectrumCalibrated2->Add(hSpectrumCalibrated, -1);

```

It seems like the difference is highest at the peaks. Can someone elaborate on that and explain which of the two approaches is the correct one to use?

NoE shows that the number of entries has changed.

[calibrated2.pdf](https://root-forum.cern.ch/uploads/short-url/h2GIlxcOPxuTHuFgmDFV4k3hhGF.pdf) (90.2 KB) [calibrated2\_zoom.pdf](https://root-forum.cern.ch/uploads/short-url/tOnZGwFvjFnPyWWGjyl9TW00zha.pdf) (16.2 KB)[NoE.pdf](https://root-forum.cern.ch/uploads/short-url/qgfMg8nY82uD4XnyPk95PnlsDIz.pdf) (77.2 KB)

---

<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:** [September 17, 2019, 9:34am UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/9 "2019-09-17T09:34:50Z")

</div>

[TH1::ResetStats](https://root.cern.ch/doc/master/classTH1.html#aa71fe2024f8e65eb35e39a1ad70fffd3)

---

<div class="post-metadata">

**Author:** ![Christoph\_Vogl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/christoph_vogl/32/19258_2.png) [@Christoph\_Vogl](https://root-forum.cern.ch/u/Christoph_Vogl)\
**Post date:** [September 17, 2019, 9:39am UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/10 "2019-09-17T09:39:23Z")

</div>

Well, nothing unexpected there.

But out of curiosity I just did ResetStats() on hSpectrumUncalibrated and now it also shows me the same number as for hSpectrumCalibrated.

Therfore after filling hSpectrumUncalibrated has somehow stored a wrong number of entries. How come?

---

<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:** [September 17, 2019, 9:43am UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/11 "2019-09-17T09:43:33Z")

</div>

Try to print the underflow:  
`hSpectrumUncalibrated->GetBinContent(0)`  
and the overflow:  
`hSpectrumUncalibrated->GetBinContent(nEnergyBins+1)`

BTW. I think you want the upper edge “nEnergyBins” and NOT “nEnergyBins - 1”.

---

<div class="post-metadata">

**Author:** ![Christoph\_Vogl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/christoph_vogl/32/19258_2.png) [@Christoph\_Vogl](https://root-forum.cern.ch/u/Christoph_Vogl)\
**Post date:** [September 17, 2019, 9:53am UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/12 "2019-09-17T09:53:22Z")

</div>

The under- and overflow don’t suffice to explain the difference:

```auto
root [1] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->GetEntries()
(double) 8665377.000000
root [2] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->ResetStats()
root [3] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->GetEntries()
(double) 5543289.000000
root [4] 8665377.000000-5543289.000000
(double) 3122088.000000
root [5] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->GetBinContent(0)
(double) 0.000000
root [6] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->GetBinContent(12000+1)
(double) 42154.000000
// ...
root [15] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->GetBinContent(12000)
(double) 10.000000

```

BWT: Yeah, you are right.

---

<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:** [September 17, 2019, 10:08am UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/13 "2019-09-17T10:08:36Z")

</div>

Make sure that you did not use: [TH1::SetAxisRange](https://root.cern.ch/doc/master/classTH1.html#adbae3b5110245637496d843afd55717f)  
Try to print: [TH1::GetEffectiveEntries](https://root.cern.ch/doc/master/classTH1.html#a0972906cbf9163fa7ad9f622b0ab1230)  
Try to print: [TH1::Integral](https://root.cern.ch/doc/master/classTH1.html#a919887c7b8431299713ec22e544cc396)

---

<div class="post-metadata">

**Author:** ![Christoph\_Vogl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/christoph_vogl/32/19258_2.png) [@Christoph\_Vogl](https://root-forum.cern.ch/u/Christoph_Vogl)\
**Post date:** [September 17, 2019, 11:36am UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/14 "2019-09-17T11:36:38Z")

</div>

There is no string containing “SetRange” or “SetAxis” in my code above the relevant lines.

Here are the print-outs:

```auto
root [1] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->GetEntries()
(double) 8665377.000000
root [2] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->GetEffectiveEntries()
(double) 5543289.000000
root [3] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->Integral()
(double) 5543289.000000
root [4] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->ResetStats()
root [5] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->GetEntries()
(double) 5543289.000000
root [6] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->GetEffectiveEntries()
(double) 5543289.000000
root [7] ((TH1F*)E_cal->Get("hSpectrumUncalibrated"))->Integral()
(double) 5543289.000000

```

Can you infer anything from that?

---

<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:** [September 17, 2019, 1:26pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/15 "2019-09-17T13:26:37Z")

</div>

I guess you fill the “hSpectrumUncalibrated” with weights not equal to 1.

---

<div class="post-metadata">

**Author:** ![Christoph\_Vogl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/christoph_vogl/32/19258_2.png) [@Christoph\_Vogl](https://root-forum.cern.ch/u/Christoph_Vogl)\
**Post date:** [September 17, 2019, 1:34pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/16 "2019-09-17T13:34:48Z")

</div>

I simply to the following for every event:  
`hSpectrumUncalibrated->Fill(energy);`

---

<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:** [September 17, 2019, 1:38pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/17 "2019-09-17T13:38:56Z")

</div>

Maybe @couet has some another idea.

In the meantime, print (just to make sure that it is less than about 1.6777e7):  
`hSpectrumUncalibrated->GetBinContent(hSpectrumUncalibrated->GetMaximumBin())`

---

<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:** [September 17, 2019, 1:49pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/18 "2019-09-17T13:49:05Z")

</div>

Do you have some code (we could run) reproducing the problem ?

---

<div class="post-metadata">

**Author:** ![Christoph\_Vogl](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/christoph_vogl/32/19258_2.png) [@Christoph\_Vogl](https://root-forum.cern.ch/u/Christoph_Vogl)\
**Post date:** [September 17, 2019, 1:50pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/19 "2019-09-17T13:50:40Z")

</div>

The one generating the histograms I guess?  
I will try to produce an example, but this will take a while.

---

<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:** [September 17, 2019, 1:55pm UTC](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046/20 "2019-09-17T13:55:01Z")

</div>

Yes if you can that might be useful.  
Or the histogram saved in a ROOT file ?

[Next page](https://root-forum.cern.ch/t/empty-bins-after-calibration/36046.md?page=2)
