# Calculating number of entries in a several peaks with different energies in a Histogram and calculating uncertainties and drawing error bar graph

**URL:** <https://root-forum.cern.ch/t/calculating-number-of-entries-in-a-several-peaks-with-different-energies-in-a-histogram-and-calculating-uncertainties-and-drawing-error-bar-graph/43222>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [January 23, 2021, 2:24am UTC](https://root-forum.cern.ch/t/calculating-number-of-entries-in-a-several-peaks-with-different-energies-in-a-histogram-and-calculating-uncertainties-and-drawing-error-bar-graph/43222 "2021-01-23T02:24:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mo922022](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mo922022/32/24415_2.png) [@Mo922022](https://root-forum.cern.ch/u/Mo922022)\
**Post date:** [January 23, 2021, 2:24am UTC](https://root-forum.cern.ch/t/calculating-number-of-entries-in-a-several-peaks-with-different-energies-in-a-histogram-and-calculating-uncertainties-and-drawing-error-bar-graph/43222/1 "2021-01-23T02:24:22Z")

</div>

Hello rooters,

I have 2 Root files from two different Simulation and as seen in the image is a comparison between energy spectrum of prompt gamma radiation produced from proton interactions. Since this is the last part of my thesis I want a help in this part please.

I want to Calculate the number of entries in some peaks in the histograms and since those entries are absolute number I want also to normalize the Histogram, moreover drawing error bar graph of uncertainties of both histograms and compare between them.  
-peaks of interest are (0.71, 2.3, 4.43, 6.128) MeV.

 ![image_2021-01-23_012529](https://root-forum.cern.ch/uploads/default/original/3X/3/5/354502bf35a79483969b5365b9a55724f75f45bb.jpeg)

I did a simple code to calculate number of entries in a specific peak but since each peak has an energy range I estimated binX1 and binX2 on X-axis by eye. but I want to do it by a scientific way to be accurate as possible.

- Here is the Code I have done  
[DrawIntegral.cpp](https://root-forum.cern.ch/uploads/short-url/pXEIFQ9V1FXvMZWc42RnBwjXks0.cpp) (2.8 KB)
- Here also the root files of Simulation  
[Out\_run\_0.root](https://root-forum.cern.ch/uploads/short-url/9qvr4X8G0NWzJ0vvoiocyGcny2c.root) (10.2 KB) [Out\_run\_1.root](https://root-forum.cern.ch/uploads/short-url/iIVLc3cu7uFpuS1N738ZAeaccRR.root) (12.0 KB)

Any help would be appreciated  
Thanks in Advance  
Mo

---

<div class="post-metadata">

**Author:** ![Mo922022](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mo922022/32/24415_2.png) [@Mo922022](https://root-forum.cern.ch/u/Mo922022)\
**Post date:** [January 24, 2021, 10:19pm UTC](https://root-forum.cern.ch/t/calculating-number-of-entries-in-a-several-peaks-with-different-energies-in-a-histogram-and-calculating-uncertainties-and-drawing-error-bar-graph/43222/2 "2021-01-24T22:19:45Z")

</div>

I have modified over last 2 days the code to get the Peak integral of a Specific peak but I am not pretty sure if I am correct or not.

- in the attachment you will find the modified code use root file in the main post

[DrawPeakIntegral.cpp](https://root-forum.cern.ch/uploads/short-url/sBPASYWPwF6py0blv65X7BZrQHK.cpp) (6.4 KB)

- in the photo attached you will see the output from the code.

 ![image_2021-01-24_225845](https://root-forum.cern.ch/uploads/default/original/3X/a/f/af914bd053fcb6c9f722667cda4e5a5952199453.png)

---

<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:** [January 24, 2021, 11:10pm UTC](https://root-forum.cern.ch/t/calculating-number-of-entries-in-a-several-peaks-with-different-energies-in-a-histogram-and-calculating-uncertainties-and-drawing-error-bar-graph/43222/3 "2021-01-24T23:10:56Z")

</div>

Try something like this ("`h`" is a pointer to your histogram):

```cpp
TF1 *f = new TF1("f", "gausn(0) + pol1(3)"); // normalized gaussian peak + linear background
f->SetParNames("Area", "Mean", "Sigma", "p0", "p1");
Double_t xmin = 4.2, xmax = 4.7;
// set "reasonable" initial values for all parameters
f->SetParameters(h->Integral(h->FindFixBin(xmin), h->FindFixBin(xmax), "width"), // "Area"
                 (xmax + xmin) / 2., (xmax - xmin) / 10., // "Mean", "Sigma"
                 0., 0.); // "p0", "p1"
h->Fit(f, "L", "", xmin, xmax); // Loglikelihood fit from "xmin" to "xmax"
std::cout << "Gaussian peak's integral = "
          << f->GetParameter(0) / h->GetBinWidth(h->FindFixBin(f->GetParameter(1)))
          << " +- "
          << f->GetParError(0) / h->GetBinWidth(h->FindFixBin(f->GetParameter(1)))
          << std::endl;

```

---

<div class="post-metadata">

**Author:** ![Mo922022](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mo922022/32/24415_2.png) [@Mo922022](https://root-forum.cern.ch/u/Mo922022)\
**Post date:** [January 24, 2021, 11:50pm UTC](https://root-forum.cern.ch/t/calculating-number-of-entries-in-a-several-peaks-with-different-energies-in-a-histogram-and-calculating-uncertainties-and-drawing-error-bar-graph/43222/4 "2021-01-24T23:50:46Z")

</div>

Thanks a lot for your reply 😍

I applied these lines in the Code and I got this Graph after commenting part of the code and I do not know if it is correct or not and, if yes, where is the information about Peak Integral.

- The code used for this output Histogram is the following one:

[DrawPeakIntegral.cpp](https://root-forum.cern.ch/uploads/short-url/jprj9oUO8G4jDQarvMaFBbVrJ1n.cpp) (7.0 KB)

Sorry if I am little bit Stupid in my question but I am new to root and my programming skills are not advanced.

 ![image_2021-01-25_003846](https://root-forum.cern.ch/uploads/default/original/3X/4/e/4e51281e1839beaf3e57b4faea81f21bb47e6fec.png)

---

<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:** [January 25, 2021, 12:20am UTC](https://root-forum.cern.ch/t/calculating-number-of-entries-in-a-several-peaks-with-different-energies-in-a-histogram-and-calculating-uncertainties-and-drawing-error-bar-graph/43222/5 "2021-01-25T00:20:54Z")

</div>

The fit misbehaved due to a very bad initial value of the Gaussian peak’s “Area” parameter (I corrected its estimating formula and added the fitted Gaussian peak’s integral printout in my previous post above).

---

<div class="post-metadata">

**Author:** ![Mo922022](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mo922022/32/24415_2.png) [@Mo922022](https://root-forum.cern.ch/u/Mo922022)\
**Post date:** [January 25, 2021, 2:31pm UTC](https://root-forum.cern.ch/t/calculating-number-of-entries-in-a-several-peaks-with-different-energies-in-a-histogram-and-calculating-uncertainties-and-drawing-error-bar-graph/43222/6 "2021-01-25T14:31:03Z")

</div>

Thanks a lot for your reply.

I applied the update and the fitting is better but it exclude the upper part of the peak and I should include all the peak with this small smear on the top as shown as follows:

 ![image_2021-01-25_151108](https://root-forum.cern.ch/uploads/default/original/3X/3/c/3c312d653d3421027f2c5da1321e2f168a261f7f.png)

- Another point also here, that I give the range of Xmin and Xmax which I estimate it as I want by eye but the point here is there is a possibility to choose this range with a piece of lines that could help me in this.

Thanks in advance

---

<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:** [January 25, 2021, 2:56pm UTC](https://root-forum.cern.ch/t/calculating-number-of-entries-in-a-several-peaks-with-different-energies-in-a-histogram-and-calculating-uncertainties-and-drawing-error-bar-graph/43222/7 "2021-01-25T14:56:35Z")

</div>

The “`xmin = 4.3, xmax = 4.6`” range is too narrow. You’d better try with “`xmin = 4.2, xmax = 4.7`” or “`xmin = 4.1, xmax = 4.8`”.

If the returned fit status says “converged” but the function does not really follow data points (try to draw it from 4 to 5 in order to see it better) then maybe the peak’s shape is not gaussian.

---

<div class="post-metadata">

**Author:** ![Mo922022](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mo922022/32/24415_2.png) [@Mo922022](https://root-forum.cern.ch/u/Mo922022)\
**Post date:** [January 25, 2021, 4:09pm UTC](https://root-forum.cern.ch/t/calculating-number-of-entries-in-a-several-peaks-with-different-energies-in-a-histogram-and-calculating-uncertainties-and-drawing-error-bar-graph/43222/8 "2021-01-25T16:09:43Z")

</div>

Thanks a lot 😍
