# Adding multiple 1D histograms

**URL:** <https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498>\
**Category:** Newbie\
**Created:** [May 19, 2020, 7:18pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498 "2020-05-19T19:18:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [May 19, 2020, 7:18pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/1 "2020-05-19T19:18:32Z")

</div>

When I run the macro attached here like 3 or 4 times, I start getting the same histogram plot every single time. I thought the use of TRandom3 meant I should be getting different plots since the data points generated are pseudo-random. I have attached the gif file here. Does anyone know why that is the case?

Also, root keeps on giving me following warnings:

> Warning in TROOT::Append: Replacing existing TH1: sig\_h (Potential memory leak).  
> Warning in TROOT::Append: Replacing existing TH1: gaus\_h1 (Potential memory leak).  
> Warning in TROOT::Append: Replacing existing TH1: gaus\_h2 (Potential memory leak).  
> Warning in TROOT::Append: Replacing existing TH1: exp\_h (Potential memory leak).  
> Warning in TROOT::Append: Replacing existing TH1: exp\_h (Potential memory leak).

How do I get rid of such warnings?  
[histexample3.cpp](https://root-forum.cern.ch/uploads/short-url/7wHF0hTxOTL4B8ofKx3ICiak7MQ.cpp) (1.2 KB)

 ![plothist](https://root-forum.cern.ch/uploads/default/original/3X/0/b/0b1db3390918bbd1fab06a9c30a937763cfde4e0.gif)

P.S. The macro is based on an example in ROOT 5 primer. I was playing with the code.

_ROOT Version:_ 5.34/18  
_Platform:_ Ubuntu 18.04  
_Compiler:_ Not Provided

* * *

---

<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:** [May 19, 2020, 7:27pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/2 "2020-05-19T19:27:28Z")

</div>

`TRandom3 rndgen(0);`

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [May 19, 2020, 7:38pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/3 "2020-05-19T19:38:31Z")

</div>

Not sure if it is helping. On the very first run, I am getting a plot like this:

 ![plothistold](https://root-forum.cern.ch/uploads/default/original/3X/b/6/b6656708b7a07a5450edf9f0e8e79c0418f9c425.gif)

If I run the macro again, I get a different kind of plot, but the histograms indicated by red and blue color are still being plotted as in the first run.

But after the third run and further, the histogram indicated by red just flattens out (as shown in the main question body), and no matter how many times I run the macro, it stays that way. I cannot even see the blue one.

---

<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:** [May 20, 2020, 7:52am UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/4 "2020-05-20T07:52:52Z")

</div>

Your macro has a problem:

```auto
Processing histexample3.cpp...
In file included from input_line_11:1:
/Users/couet/Downloads/histexample3.cpp:18:7: error: use of undeclared identifier 'exp_h'
      exp_h->Fill(rndgen.Exp(4));

```

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [May 20, 2020, 1:30pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/5 "2020-05-20T13:30:07Z")

</div>

For me, it does not show that error. Furthermore, based on some other questions posted in the forum, I have modified the code a bit by including few lines from line 13-18 in my new code attached here. This does not give any memory leak warnings.  
[histexample3.cpp](https://root-forum.cern.ch/uploads/short-url/mSDCxZ9EnQfDISTim0ZCN2FdDTh.cpp) (2.2 KB)

---

<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:** [May 20, 2020, 1:43pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/6 "2020-05-20T13:43:21Z")

</div>

Look at your code … exp\_h is not defined … I guess you mean bkg\_h instead

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [May 20, 2020, 1:50pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/7 "2020-05-20T13:50:32Z")

</div>

I completely did not see it, but I am not lying when I say that my code is running fine just the way it is right now. But now, I have changed bkg\_h to exp\_h everywhere.

---

<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:** [May 20, 2020, 2:04pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/8 "2020-05-20T14:04:34Z")

</div>

So I get these two canvases. What is wrong ?

 ![Screenshot 2020-05-20 at 16.03.45](https://root-forum.cern.ch/uploads/default/original/3X/7/5/7582be4196badca940fa7d9347843d5cce3b6a39.png)

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [May 20, 2020, 2:08pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/9 "2020-05-20T14:08:30Z")

</div>

In my original code in the question (where the second canvas is not a part of the code), I am only trying to plot Exponential+Gaussian plot. Let’s say, I run it in root, it works fine. I get something similar to what you have on the left side. Let’s say, I close the canvas window and run the macro again, I get something similar. But when I do it the third time and more, I keep on getting something like this:

 ![plothist](https://root-forum.cern.ch/uploads/default/original/3X/0/b/0b1db3390918bbd1fab06a9c30a937763cfde4e0.gif)

No matter how many times, I run the macro, the histogram indicated by red color just flattens out totally, and I don’t even see blue one anymore. That was my original question.

---

<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:** [May 20, 2020, 2:22pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/10 "2020-05-20T14:22:13Z")

</div>

I just ran it several time in the same root session (closing the canvases in between the runs) I always get similar plots.

---

<div class="post-metadata">

**Author:** ![hep\_1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hep_1/32/21930_2.png) [@hep\_1](https://root-forum.cern.ch/u/hep_1)\
**Post date:** [May 20, 2020, 2:56pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/11 "2020-05-20T14:56:43Z")

</div>

Ok, interesting, I am not sure what is going on at my end, then.

---

<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:** [May 20, 2020, 2:58pm UTC](https://root-forum.cern.ch/t/adding-multiple-1d-histograms/39498/12 "2020-05-20T14:58:39Z")

</div>

I guess the ROOT version… I am using 6.20  
5.34 is very old… can you try a more recent version ?
