Get Profile X from TH2D considering average and weights

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?


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:

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:

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