# Potential Memory leak?

**URL:** <https://root-forum.cern.ch/t/potential-memory-leak/52180>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [November 1, 2022, 12:29am UTC](https://root-forum.cern.ch/t/potential-memory-leak/52180 "2022-11-01T00:29:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![HMahdy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/7feea3/32.png) [@HMahdy](https://root-forum.cern.ch/u/HMahdy)\
**Post date:** [November 1, 2022, 12:29am UTC](https://root-forum.cern.ch/t/potential-memory-leak/52180/1 "2022-11-01T00:29:01Z")

</div>

I’m trying to create a 1D histogram but getting this error “Replacing existing TH1: Energy (Potential memory leak)”. Some ideas would be highly appreciated.

[GENIAnalysis.C](https://root-forum.cern.ch/uploads/short-url/wAJ4NTG5xWaVKhmRgXtcMXunicv.C) (1.9 KB)

* * *

ROOT Version:6.24  
Platform:\_ Not Provided  
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:** [November 1, 2022, 7:24am UTC](https://root-forum.cern.ch/t/potential-memory-leak/52180/2 "2022-11-01T07:24:02Z")

</div>

You need:

```cpp
   // ...
   TH1D *h = new TH1D("Energy", "Final Energy; Protons; Frequency", 50, 0., 2.);
   for (Long64_t jentry = 0; jentry < nentries; jentry++) {
   // ...
		      if (pdgf[pdgindex] == 2212) {
		      	  h->Fill(Your_Particle_Final_Energy_Value); // maybe (Ef[pdgindex])
		      }
   // ...
   }
   h->Draw();
   // ...

```

BTW. If this is the same tree as in the [“InteractionsII.root”](https://root-forum.cern.ch/t/creating-a-histogram-of-particle-final-energies-using-their-pdg-codes/52121) file, instead of:  
` for (Long64_t pdgindex=0; pdgindex<28;pdgindex++)`  
you need:  
` for (Int_t pdgindex = 0; pdgindex < nf; pdgindex++) // if you use the "pdgf[nf]/I " branch inside`  
or:  
` for (Int_t pdgindex = 0; pdgindex < ni; pdgindex++) // if you use the "pdgi[ni]/I " branch inside`

---

<div class="post-metadata">

**Author:** ![HMahdy](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/7feea3/32.png) [@HMahdy](https://root-forum.cern.ch/u/HMahdy)\
**Post date:** [November 1, 2022, 3:46pm UTC](https://root-forum.cern.ch/t/potential-memory-leak/52180/4 "2022-11-01T15:46:07Z")

</div>

Thanks Wile! It worked. I really appreciate it.  
Hany

![Screenshot from 2022-11-01 10-40-43](https://root-forum.cern.ch/uploads/default/original/3X/f/6/f6754f5ef6be61e02dcb5970ac5f47ffc3483586.png)
