# Extracting mean after fitting a distribution

**URL:** <https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610>\
**Category:** Newbie\
**Tags:** root\
**Created:** [December 20, 2023, 11:04am UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610 "2023-12-20T11:04:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamaanand](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/9d8465/32.png) [@iamaanand](https://root-forum.cern.ch/u/iamaanand)\
**Post date:** [December 20, 2023, 11:04am UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/1 "2023-12-20T11:04:02Z")

</div>

I am able a fit a distribution using a fitting function

```auto
"[1]*(exp(0.5*(log(2)/[0])*((log(2)/[0])*[2]**2-2*x))*TMath::Erfc(((log(2)/[0])*[2]**2-x)/(sqrt(2)*[2])))"

```

Now I am interested to get the mean of this distribution through fitting.

Here is the code :

```auto
   TF1 *f1 = new TF1("f1", "[1]*(exp(0.5*(log(2)/[0])*((log(2)/[0])*[2]**2-2*x))*TMath::Erfc(((log(2)/[0])*[2]**2-x)/(sqrt(2)*[2])))", 0.005, 100);
   f1->SetParameter(0, 6);
   f1->SetParameter(1, 25);
   f1->SetParameter(2, 10);

   f1->GetParameter(0);
   f1->GetParameter(1);
   f1->GetParameter(2);

```

How to obtain the mean in this fitting?

---

<div class="post-metadata">

**Author:** ![mczurylo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mczurylo/32/33269_2.png) [@mczurylo](https://root-forum.cern.ch/u/mczurylo)\
**Post date:** [December 20, 2023, 12:27pm UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/2 "2023-12-20T12:27:47Z")

</div>

Hi @iamaanand,

thank you for the question. For the general, beginner level info on fitting please refer to: [Fitting histograms - ROOT](https://root.cern/manual/fitting/)

Cheers,  
Marta

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [December 20, 2023, 1:59pm UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/3 "2023-12-20T13:59:43Z")

</div>

Hi,  
you could use the definition of mean.

Below you find a code to do it with a gaussian, you can change the gaussian with your function.

```auto
    double dx=1e-2;
    double x_min = 0.005;
    double x_max = 100;
    
    
    TF1 *f1= new TF1("f","TMath::Gaus(x,19.73, 3.5)",x_min, x_max);
    
    double sum=0;
    double norm=0;
    
    
    
    
    for(double x=x_min; x<=x_max; x+=dx)
    {
        sum+= x*f1->Eval(x)*dx;
        norm+= f1->Eval(x)*dx;
    }
    
    cout<<"mean is "<< sum/norm<<endl;

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 20, 2023, 2:43pm UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/4 "2023-12-20T14:43:52Z")

</div>

You can also use `TF1::Mean()`, it gives the same result as @Dilicus code:

```auto
void getmean(){
   double dx=1e-2;
   double x_min = 0.005;
   double x_max = 100;

   TF1 *f1= new TF1("f","TMath::Gaus(x,19.73, 3.5)",x_min, x_max);
   double sum=0;
   double norm=0;

   for (double x=x_min; x<=x_max; x+=dx) {
      sum+= x*f1->Eval(x)*dx;
      norm+= f1->Eval(x)*dx;
   }

   cout << "1) mean is " << sum/norm<<endl;
   cout << "2) mean is " << f1->Mean(x_min,x_max) << endl;
}

```

```auto
root [0] .x getmean.C
1) mean is 19.73
2) mean is 19.73
root [1] 

```

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [December 20, 2023, 3:19pm UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/5 "2023-12-20T15:19:24Z")

</div>

I looked on TF1 documentation searching for GetMean() instead of Mean(), and since I did not found it, I wrote a a small code.  
I just realised that I got confused with TH1.  
Of course @couet solution is way better than mine in terms of readability and simplicity.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 20, 2023, 3:22pm UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/6 "2023-12-20T15:22:50Z")

</div>

> [@Dilicus](#):
>
> I looked on TF1 documentation searching for GetMean() instead of Mean(),

I did the same and found Mean finally 😀

> [@Dilicus](#):
>
> Of course @couet solution is way better than mine in terms of readability and simplicity.

It is good to crosscheck anyway 👍🏻

---

<div class="post-metadata">

**Author:** ![SapphireShade](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sapphireshade/32/35327_2.png) [@SapphireShade](https://root-forum.cern.ch/u/SapphireShade)\
**Post date:** [December 21, 2023, 9:08am UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/7 "2023-12-21T09:08:07Z")

</div>

As far as I know, in ROOT, once you’ve fit your data with a function (like the one you’ve described), the mean of the distribution can be obtained, but it requires some understanding of the fitted function. The mean of a distribution is not always directly given by the fit parameters, especially for complex functions. Your function looks quite specific and might not have a straightforward mean. But correct me if I’m wrong.

---

<div class="post-metadata">

**Author:** ![EmberEnigma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emberenigma/32/35316_2.png) [@EmberEnigma](https://root-forum.cern.ch/u/EmberEnigma)\
**Post date:** [December 21, 2023, 11:05am UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/8 "2023-12-21T11:05:43Z")

</div>

I know the topic’s been solved but I just wanted to suggest using numerical integration for your unique function to find the mean. This method is really handy for complex functions where the mean isn’t obvious.  
I think it might look something like this:

```auto
double x_min = 0.005;
double x_max = 100;
TF1 *f1 = new TF1("f1", yourFunction, x_min, x_max);

double mean = f1->Integral(x_min, x_max, [](double *x, double *p) { return x[0] * f1->EvalPar(x, p); }) / f1->Integral(x_min, x_max);
cout << "Mean: " << mean << endl;

```

This code integrates your function multiplied by x (for the weighted average) over your range and normalizes it. It’s a great way to handle non-standard distributions.

What do you all think of this? Might it be a good solution?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 21, 2023, 11:11am UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/9 "2023-12-21T11:11:15Z")

</div>

Can you provide a running script ? The code you posted does not work if I put it my previous script.

---

<div class="post-metadata">

**Author:** ![EmberEnigma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emberenigma/32/35316_2.png) [@EmberEnigma](https://root-forum.cern.ch/u/EmberEnigma)\
**Post date:** [December 22, 2023, 11:09am UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/10 "2023-12-22T11:09:32Z")

</div>

In essence, the idea is to use numerical integration to calculate the weighted mean of a complex function, which is particularly useful when dealing with non-standard distributions. The code I provided demonstrates how to integrate the function multiplied by ‘x’ to find the weighted average, and then normalizes it over a specified range. This approach should be adaptable to a variety of functions, but the exact implementation might vary depending on the details of your function and script.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 22, 2023, 11:53am UTC](https://root-forum.cern.ch/t/extracting-mean-after-fitting-a-distribution/57610/11 "2023-12-22T11:53:09Z")

</div>

You provided a code snippet not a working script. Can you provide a working script please ?
