# Get Profile X from TH2D considering average and weights

**URL:** <https://root-forum.cern.ch/t/get-profile-x-from-th2d-considering-average-and-weights/50402>\
**Category:** Newbie\
**Created:** [June 14, 2022, 6:56pm UTC](https://root-forum.cern.ch/t/get-profile-x-from-th2d-considering-average-and-weights/50402 "2022-06-14T18:56:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nyx](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Nyx](https://root-forum.cern.ch/u/Nyx)\
**Post date:** [June 14, 2022, 6:56pm UTC](https://root-forum.cern.ch/t/get-profile-x-from-th2d-considering-average-and-weights/50402/1 "2022-06-14T18:56:44Z")

</div>

I have a TH2D object where for each (x,y) there is an associated variable z. This variable z is in the range [0,1]. I want to project this onto a TH1D histogram in 2 different ways:

1. For each bin x, the content is the average value of the bins on the y axis. For example, let’s suppose that I only have the points (1,1,0.3), (1,2,0.4), (1,3,0.5). By calling `ProjectionX()`, I would get that the content of the TH1D is (1,1.2). I would like it to be (1,0.4).

2. The same thing but instead of the arithmetic mean of the values along the y axis, do a weighted average with a gaussian function centered at y=0, and with some Std Dev.

What’s the best way to do this?

---

<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:** [June 15, 2022, 1:54pm UTC](https://root-forum.cern.ch/t/get-profile-x-from-th2d-considering-average-and-weights/50402/2 "2022-06-15T13:54:13Z")

</div>

Hi,

If I have understood you well, in the first case you just need to divide the resulting projected histogram by the number of Y bins, so just do:

```auto
hx = h2->ProjectionX()->Scale(1./h2->GetNbinsY());

```

For the second case is more complicated, but you could use `TH1::Multiply` for the 2d histogram.  
If you don’t want to modify the original histogram copy it before. Here is an example:

```auto
TF2 f2("f2","gausn(y)")
f2.SetParameters(1,0, stdDev);
h2->Multiply(&f2);
hx = h2->ProjectionX()->Scale(1./h2->GetNbinsY());

```

Cheers

Lorenzo
