# TH1D ProjectionX()

**URL:** <https://root-forum.cern.ch/t/th1d-projectionx/45929>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [July 20, 2021, 2:43pm UTC](https://root-forum.cern.ch/t/th1d-projectionx/45929 "2021-07-20T14:43:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aman\_Upadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aman_upadhyay/32/23958_2.png) [@Aman\_Upadhyay](https://root-forum.cern.ch/u/Aman_Upadhyay)\
**Post date:** [July 20, 2021, 2:43pm UTC](https://root-forum.cern.ch/t/th1d-projectionx/45929/1 "2021-07-20T14:43:41Z")

</div>

I have to project two separate TH2F to two TH1D. For that, I am using this code:

```auto
    TH2F *h2_s = (TH2F*)f->Get("hsig");
    TH2F *h2_b = (TH2F*)f->Get("hbg");
    TH1D *h1_s = h2_s->ProjectionX("",5,5);
    TH1D *h1_b = h2_b->ProjectionX("",5,5);

```

The problem is as soon as I project the background histogram signal, TH1D also fills up with the background.  
I also found that in the [ProjectionX()](https://root.cern.ch/doc/master/classTH2.html#a974ece9e7d260f92df00a39dba14e5b0) function page, it is mentioned that

> NOTE that if a TH1D named “name” exists in the current directory or pad the histogram is reset and filled again with the projected contents of the TH2.

I am guessing this is the problem, but I am not sure how to fix this.

* * *

ROOT Version:6.24/02  
Platform:(NAME=“HefftorLinux”,ID\_LIKE=“arch”,VERSION\_ID=v2021-02-05)  
Compiler: gcc 11.1.0

---

<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:** [July 20, 2021, 2:51pm UTC](https://root-forum.cern.ch/t/th1d-projectionx/45929/2 "2021-07-20T14:51:03Z")

</div>

```cpp
    TH1D *h1_s = h2_s->ProjectionX("h1_s",5,5);
    TH1D *h1_b = h2_b->ProjectionX("h1_b",5,5);

```

---

<div class="post-metadata">

**Author:** ![Aman\_Upadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aman_upadhyay/32/23958_2.png) [@Aman\_Upadhyay](https://root-forum.cern.ch/u/Aman_Upadhyay)\
**Post date:** [July 20, 2021, 2:59pm UTC](https://root-forum.cern.ch/t/th1d-projectionx/45929/3 "2021-07-20T14:59:59Z")

</div>

Thanks a lot!
