# Different ways of normalizing histograms

**URL:** <https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582>\
**Category:** ROOT\
**Created:** [February 3, 2013, 1:37am UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582 "2013-02-03T01:37:46Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![aafshari](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ebca7d/32.png) [@aafshari](https://root-forum.cern.ch/u/aafshari)\
**Post date:** [February 3, 2013, 1:37am UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/1 "2013-02-03T01:37:46Z")

</div>

Hi everyone,

I’m a new user of both ROOT and RootTalk so please pardon my ignorance. I’ve been trying to normalize several histograms, and when I search in RootTalk, a multitude of ways comes up. How do I know which one is “right”? What is the difference between the following methods?

Here are the 4 methods I tried:

Method 1:

```auto
Double_t num = h->GetBinContent(i);
Double_t den = h->GetBinWidth(i);
Double_t value = 0;
if (den!=0)
  {
     value = num/den;
     h->SetBinContent(i,value);
  }

```

Method 2:

```auto
Double_t factor = 1.;
h->Scale(factor/h->GetEntries());

```

Method 3:

```auto
Double_t scale = h->GetXaxis()->GetBinWidth(1)/(h->Integral());
h->Scale(scale);

```

Method 4:

```auto
Double_t factor = 1.;
h->Scale(factor/h->Integral());

```

---

<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 3, 2013, 8:22am UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/2 "2013-02-03T08:22:05Z")

</div>

You can also have:

Method 5:

```auto
Double_t factor = 1.;
h->Scale(factor, "width");
```

Method 6:

```auto
Double_t factor = 1.;
h->Scale(factor/h->Integral(), "width");
```

Method 7:

```auto
Double_t factor = 1.;
h->Scale(factor/h->Integral("width"));
```

See: [TH1::Scale (Double\_t c1 = 1, Option\_t\* option = “”)](http://root.cern.ch/root/html534/TH1.html#TH1:Scale) and [TH1::Integral (Option\_t\* option = “”)](http://root.cern.ch/root/html534/TH1.html#TH1:Integral)  
Note that [TH1::SetBinContent](http://root.cern.ch/root/html534/TH1.html#TH1:SetBinContent) will change the bin content of a given bin only (and it will increment the number of entries of the histogram) and you should then explicitly [TH1::SetBinError](http://root.cern.ch/root/html534/TH1.html#TH1:SetBinError) as well.  
Also, do remember that you may have [“fix or variable bin size” histograms](http://root.cern.ch/root/html534/TH1.html) (and so, in the latter case, the “Method 6” and “Method 7”, for example, will give different results).

Try:

```auto
root [0] TH1F *h = new TH1F("h","a trial histogram", 100, -1.5, 1.5);
root [1] h->Sumw2();
root [2] for (Int_t i = 0; i < 10000; i++) h->Fill(gRandom->Gaus(0, 1));
root [3] h->Draw();
root [4] h->GetEntries()
root [5] h->Integral()
root [6] h->Integral("width")
```

Then try to:

```auto
TH1F *h1 = (TH1F*)(h->Clone("h1"));
// ... one "Clone" per "Method"
TH1F *h7 = (TH1F*)(h->Clone("h7"));
```

and then apply each of your “Methods” (1 … 7) to these clones (“h1” … “h7”) and compare the results.  
Note: the “h” is a “fix bin size” histogram" so some “Methods” that utilize the “bin width” may give the same results, while for a “variable bin size” histogram you would get different results.

From all these different methods, two which utilize [TH1::Scale](https://root.cern.ch/root/html534/TH1.html#TH1:Scale) and [TH1::Integral](https://root.cern.ch/root/html534/TH1.html#TH1:Integral) are usually the most relevant:

- normalize by the “integral” to show the frequency probability in each bin (“Method 4”),
- normalize by the “integral \* bin width” to show the estimated probability density function (“Method 6”).

There is also the question of underflow / overflow bins.  
When normalizing by the “integral”, we could also include underflow / overflow bins in the “integral”, while we cannot do it in the second case, because we don’t know the underflow / overflow “bin width”.

BTW. In order to make sure that the errors are properly handled, first (i.e. before calling [TH1::Scale](https://root.cern.ch/root/html534/TH1.html#TH1:Scale)) execute:

```cpp
if (h->GetSumw2N() == 0) h->Sumw2(kTRUE);

```

---

<div class="post-metadata">

**Author:** ![aafshari](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ebca7d/32.png) [@aafshari](https://root-forum.cern.ch/u/aafshari)\
**Post date:** [February 5, 2013, 6:26am UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/3 "2013-02-05T06:26:17Z")

</div>

Thank you, I’ll try these methods and see the differences.

---

<div class="post-metadata">

**Author:** ![HIMANSHU\_SHARMA](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@HIMANSHU\_SHARMA](https://root-forum.cern.ch/u/HIMANSHU_SHARMA)\
**Post date:** [July 19, 2017, 5:54am UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/4 "2017-07-19T05:54:46Z")

</div>

Should we not redraw the histogram after normalizing.

---

<div class="post-metadata">

**Author:** ![p73](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/p73/32/14141_2.png) [@p73](https://root-forum.cern.ch/u/p73)\
**Post date:** [December 4, 2017, 8:27am UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/5 "2017-12-04T08:27:38Z")

</div>

After

```auto
myHist->Scale(/*any number here even 1*/);

```

draw style changes. Error bars appear instead of simple lines. How to avoid that?

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [December 4, 2017, 3:14pm UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/6 "2017-12-04T15:14:18Z")

</div>

```auto
myHist->Draw("HIST")

```

You can check out the 1D histogram draw options here: [https://root.cern.ch/doc/master/classTHistPainter.html#HP01a](https://root.cern.ch/doc/master/classTHistPainter.html#HP01a)

---

<div class="post-metadata">

**Author:** ![p73](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/p73/32/14141_2.png) [@p73](https://root-forum.cern.ch/u/p73)\
**Post date:** [December 6, 2017, 4:01pm UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/7 "2017-12-06T16:01:55Z")

</div>

Thank you. It worked.

---

<div class="post-metadata">

**Author:** ![wajahat](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wajahat/32/23051_2.png) [@wajahat](https://root-forum.cern.ch/u/wajahat)\
**Post date:** [August 7, 2020, 10:39am UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/8 "2020-08-07T10:39:07Z")

</div>

Thanks alot. you rocks… 😀

---

<div class="post-metadata">

**Author:** ![Gauri\_Devi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gauri_devi/32/28403_2.png) [@Gauri\_Devi](https://root-forum.cern.ch/u/Gauri_Devi)\
**Post date:** [January 8, 2022, 10:02am UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/9 "2022-01-08T10:02:03Z")

</div>

how do work scale function with draw line in place of histo in root version 6.10 while in root version 5.34 , it’s worked. I have given below command in my code-  
h11-\>Scale(1.2); here 1.2 as a scaling factor.  
h11-\>Draw(“same:l”); in case does not work in 6 version . if h11-\>Draw (“histo”); then worked in version 6. while in version 5, both worked.  
Please give suggestion.

---

<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 8, 2022, 11:15am UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/10 "2022-01-08T11:15:20Z")

</div>

> [@How do draw scale in histogram in version 6?](https://root-forum.cern.ch/t/how-do-draw-scale-in-histogram-in-version-6/48304/2):
>
> In ROOT 6, the [TH1::Scale](https://root.cern/doc/master/classTH1.html#add929909dcb3745f6a52e9ae0860bfbd) method automatically executes the [TH1::Sumw2](https://root.cern/doc/master/classTH1.html#aefa4ee94f053ec3d217f3223b01fa014) method. If you want the old ROOT 5 behaviour, try: h11-\>Scale(1.2, "nosw2"); @couet The “L” drawing option is broken for weighted histograms (in both ROOT versions).

---

<div class="post-metadata">

**Author:** ![mendoza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mendoza/32/26128_2.png) [@mendoza](https://root-forum.cern.ch/u/mendoza)\
**Post date:** [August 10, 2023, 3:23pm UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/11 "2023-08-10T15:23:14Z")

</div>

Hi, I come here because I am having troubles with normalisation of 3 histograms. I am using the following method:

> h\_Signal-\>Scale(1./h\_Signal-\>Integral());  
> h\_Lambda-\>Scale(1./h\_Lambda-\>Integral());  
> h\_Ks0-\> Scale(1./h\_Ks0-\> Integral());

but then when printing the histograms I see that the areas under each are not the same. You can find the plot here attached. Has someone faced the same doubt before?

Thanks in advance.

Cheers,

Diego

 ![UThits_all_layers-2](https://root-forum.cern.ch/uploads/default/original/3X/c/7/c7d9cb33034ab4d00544ca69a9928ba890648605.png)

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [August 10, 2023, 3:43pm UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/12 "2023-08-10T15:43:43Z")

</div>

Hi,

something else is probably going on here, it shouldn’t be like this. Do you mind sharing your entire code (including the files with the input histograms) so that we could have a look at this particular case?

---

<div class="post-metadata">

**Author:** ![mendoza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mendoza/32/26128_2.png) [@mendoza](https://root-forum.cern.ch/u/mendoza)\
**Post date:** [August 10, 2023, 3:49pm UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/13 "2023-08-10T15:49:13Z")

</div>

Sure, the code and the root file I use to read the histograms are attached. Apologies for the code format, I work in SWAN so I use the notebooks instead of running macros.

Thanks!

[work\_with\_hists.txt](https://root-forum.cern.ch/uploads/short-url/k9hJo7GzXphDfB0Mw5lcgJ9W10S.txt) (2.8 KB)  
[PrCheckerPlots\_newconds.root](https://root-forum.cern.ch/uploads/short-url/oIJ0XQnrAZDB0AgbwvUUJ9GsWO4.root) (1.3 MB)

---

<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:** [August 10, 2023, 3:57pm UTC](https://root-forum.cern.ch/t/different-ways-of-normalizing-histograms/15582/14 "2023-08-10T15:57:09Z")

</div>

`hs->Draw("NOSTACK");`
