# Slicing a TH2 in Y axis

**URL:** <https://root-forum.cern.ch/t/slicing-a-th2-in-y-axis/40598>\
**Category:** Newbie\
**Created:** [July 23, 2020, 11:49pm UTC](https://root-forum.cern.ch/t/slicing-a-th2-in-y-axis/40598 "2020-07-23T23:49:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [July 23, 2020, 11:49pm UTC](https://root-forum.cern.ch/t/slicing-a-th2-in-y-axis/40598/1 "2020-07-23T23:49:31Z")

</div>

Dear,

I have a TH2D (X vs. Y), and I want to get X TH1D’s, in specific ranges of Y, so I do the following

hX\_i\_j = hXY-\>ProjectionX(“hX\_i\_j”, hXY-\>GetYaxis()-\>FindBin(i), hXY-\>GetYaxis()-\>FindBin(j))

but this is giving something else from what I would expect. Is there anything wrong with what I’m doing? is there any cleaner way of doing that? Thanks!!

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 24, 2020, 1:34pm UTC](https://root-forum.cern.ch/t/slicing-a-th2-in-y-axis/40598/2 "2020-07-24T13:34:54Z")

</div>

Hi,

If you want to get a slice, i.e. the 1D histogram of X given the Y value, you should use the same bin:

```auto
hX_Y = hXY->ProjectionX(“hX_Yi”, hXY->GetYaxis()->FindBin(yvalue), hXY->GetYaxis()->FindBin(yvalue))

```

where yvalue is the y value for which you want to get the slice. It is not clear to me what are `i` and `j`.  
They should be y values and not bin numbers. If you have a range (inlacing more bins in y) you will have a projection, i.e. the sum of all contents of the y bins.

Lorenzo

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [July 24, 2020, 2:27pm UTC](https://root-forum.cern.ch/t/slicing-a-th2-in-y-axis/40598/3 "2020-07-24T14:27:19Z")

</div>

Hi Moneta! sorry for the bad notation, but i and j are indeed yvalues, but when I compare the entries on the actual TH2 plot, it’s not the same, … any idea why this is?

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [July 24, 2020, 3:11pm UTC](https://root-forum.cern.ch/t/slicing-a-th2-in-y-axis/40598/4 "2020-07-24T15:11:50Z")

</div>

Hi,

I understand. But what is not the same and how is different ? Please provide an example and better a script reproducing the problem, thank you

Lorenzo

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [July 27, 2020, 4:02pm UTC](https://root-forum.cern.ch/t/slicing-a-th2-in-y-axis/40598/5 "2020-07-27T16:02:30Z")

</div>

Hi, sorry for the late message, I tried the method on a simple TH2D histogram and it worked fine, I will double-check my original problem, thanks a lot!
