# Finding the peak positions

Dear @couet,

Is it possible to get the X, Y positions of the peak of a fit (it may be Gaussian or may not be)?

Does this example work for finding peak from fit too?
https://root.cern.ch/root/html534/tutorials/spectrum/peaks.C.html

Try to play with this:

and that:

1 Like

I meant that I have a fit already. Which is not a Gaussian. Can I get the X, Y centers of the its peak.
Like in 1D we have GetMean() for Gaussian which gives X mean.

Likewise for 2D fit (not Gaussian), is there any which give the value of X mean and Y mean?

Iâ€™m sure your own fit function has some â€śparametersâ€ť. So you need to use the fitted parametersâ€™ values to calculate what you want (it may well be that one of the â€śparametersâ€ť is simply the â€śmeanâ€ť).

TF1, TF2 and TF3 functions do not have any â€śGetMeanâ€ť method. You are mixing here â€śfunctionsâ€ť with â€śhistogramsâ€ť and / or â€śgraphsâ€ť.

On the other hand, TF1, TF2 and TF3 functions do provide various â€śMean...â€ť, â€śMoment...â€ť and â€śCentralMoment...â€ť methods, which you may find convenient.

The problem with MeanX or MeanY is it does not take function with the form

fit= new TF2 (â€śfitâ€ť,func,0.,1.,0.,1.)
where â€śfuncâ€ť is a pre-defined function.

Sorry, no idea where you got â€śMeanXâ€ť and â€śMeanYâ€ť from (i.e. it seems to me that you again try to apply some methods to an inappropriate object).

Suppose I have a TF2. How do I get Mean, RMS both in X and Y direction?

If by â€śRMSâ€ť you mean the â€śstandard deviationâ€ť then you could use the â€śsqrt(second central moment)â€ť to calculate it.

If by â€śmeanâ€ť, you mean the â€śexpected valueâ€ť then you could use the â€śfirst (raw) momentâ€ť to calculate it.

Does that mean

RMS = SQRT( myfunction->TF2::CentralMoment2());

and expected value = myfunction->TF2::Moment2();

Try to play, e.g. with this function:

``````{
TF2 *f = new TF2("f", "[0] * TMath::Gaus(x, [1], [2]) * TMath::Gaus(y, [3], [4])", -10., 10., -20., 20.);
// f->Print();
f->SetParameters(10., 2., 3., 4., 5.);
f->Draw("colz");
}
``````

Are you asking to use TF2:moment2() or TF2:CentralMoment2() with your bi Gaussian?

Yes, do try to apply various class methods to some easy well defined function, with precisely known parametersâ€™ settings. This way you will learn how to use them properly (and what they really return).

The following is giving error (Error: Canâ€™t call TF2::CentralMoment2(-10.,10.,-20.,20.) in current scope):

``````  TF2 *f = new TF2("f", "[0] * TMath::Gaus(x, [1], [2]) * TMath::Gaus(y, [3], [4])", -10., 10., -20., 20.);
f->SetParameters(10., 2., 3., 4., 5.);
f->Draw("Surf");

Double_t RMS = SQRT(f->CentralMoment2(-10., 10., -20., 20.));
Double_t MeanY = f->Moment2(-10., 10., -20., 20.);
``````

See: TF2::Moment2

I donâ€™t know where you take the â€śSQRTâ€ť from but, I would use TMath::Sqrt there, thought itâ€™s just my private preference.

Sorry.
Now when I do this:

``````  Double_t RMS1 = TMath::Sqrt(f->CentralMoment2(1,-10., 10., 1, -20., 20., 0.000001));
Double_t Mean1 = f->Moment2(1,-10., 10., 1, -20., 20., 0.000001);
Double_t RMS2 = TMath::Sqrt(f->CentralMoment2(2,-10., 10., 2, -20., 20., 0.000001));
Double_t Mean2 = f->Moment2(2,-10., 10., 2, -20., 20., 0.000001);
``````

I get the following:
RMS1 =-nan
Mean1 = 7.84092
RMS2 =14.7077
Mean2 = 512.375

I donâ€™t know among those which is for X direction and which is for Y direction?
And what are the errors on the calculated values?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.