# How does GetChisquare() is calculated when you fit data?!

**URL:** <https://root-forum.cern.ch/t/how-does-getchisquare-is-calculated-when-you-fit-data/60255>\
**Category:** Newbie\
**Created:** [July 18, 2024, 11:47am UTC](https://root-forum.cern.ch/t/how-does-getchisquare-is-calculated-when-you-fit-data/60255 "2024-07-18T11:47:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elena\_Veli](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elena_veli/32/31299_2.png) [@Elena\_Veli](https://root-forum.cern.ch/u/Elena_Veli)\
**Post date:** [July 18, 2024, 11:47am UTC](https://root-forum.cern.ch/t/how-does-getchisquare-is-calculated-when-you-fit-data/60255/1 "2024-07-18T11:47:04Z")

</div>

chi\_square\_ndf = ex.GetChisquare() / ex.GetNDF(); I want to calculate the goodness of my fit, but I cannot find what is the equation behind chi2. Is it like this?:  
# Calculate chi-square  
residuals = unique\_charges - fitted\_charges  
chi\_square = np.sum((residuals \*\* 2) / fitted\_charges)  
ndof = len(unique\_charges) - len(popt)  
chi2\_ndf = chi\_square / ndof if ndof != 0 else float(‘inf’)  
thanks you!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [July 18, 2024, 2:45pm UTC](https://root-forum.cern.ch/t/how-does-getchisquare-is-calculated-when-you-fit-data/60255/2 "2024-07-18T14:45:50Z")

</div>

Hi Elena,

Could you please give us some context? What is `ex`?

Best,  
D

---

<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:** [July 18, 2024, 3:04pm UTC](https://root-forum.cern.ch/t/how-does-getchisquare-is-calculated-when-you-fit-data/60255/3 "2024-07-18T15:04:24Z")

</div>

Hi Elena,

\chi^2 is defined as follow:

\chi^2 = \sum\_{i}{ \left(\frac{y(i) - f(x(i) | p )}{e(i)} \right)^2 }.

is the sum of the residual squared weighted with the inverse of the error squared.

---

<div class="post-metadata">

**Author:** ![Shiva\_Prasad\_Nayak](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/shiva_prasad_nayak/32/36391_2.png) [@Shiva\_Prasad\_Nayak](https://root-forum.cern.ch/u/Shiva_Prasad_Nayak)\
**Post date:** [July 18, 2024, 6:18pm UTC](https://root-forum.cern.ch/t/how-does-getchisquare-is-calculated-when-you-fit-data/60255/4 "2024-07-18T18:18:14Z")

</div>

Here is a code I worked in which calculates \chi^2 manually and compares with what root shows. Hope it helps.

```auto
    TRandom3 *rand = new TRandom3();

    TH1F *hist = new TH1F("hist", " ; x; y", 100, 0, 100);

    for (int i = 0; i < 10000; ++i) {
        hist->Fill(rand->Gaus(50, 10));
    }

    hist->Draw("E1");

    TF1 *fit = new TF1("fit", "gaus");
    hist->Fit(fit);

    cout << "Chi-square from fit: " << fit->GetChisquare() << endl;

    double chi = 0.0;

    for (int i = 1; i < 100; ++i) 
    {
        double binCenter = hist->GetBinCenter(i);
        double binContent = hist->GetBinContent(i);
        double binError = hist->GetBinError(i);
        double fitValue = fit->Eval(binCenter);

        if (binError != 0) 
        {
            chi += (binContent - fitValue) * (binContent - fitValue) / (binError * binError);
        }
    }

    cout << "Manually Calculated Chi-square: " << chi << endl;

```

---

<div class="post-metadata">

**Author:** ![Elena\_Veli](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/elena_veli/32/31299_2.png) [@Elena\_Veli](https://root-forum.cern.ch/u/Elena_Veli)\
**Post date:** [July 21, 2024, 12:09am UTC](https://root-forum.cern.ch/t/how-does-getchisquare-is-calculated-when-you-fit-data/60255/5 "2024-07-21T00:09:50Z")

</div>

Hello! thank you for the answer! ex is:

```auto
        std::stringstream function_def;
        function_def << max_charge << "*pow((1-exp(-[0]*x)),[1])";

        auto to_string = function_def.str();
TF1 ex("tf_name", to_string.c_str());

        ex.SetParameters(1./12., 1);

        std::stringstream predicate;

        data->Fit("tf_name", "charge:qtp", predicate.str().c_str(), "WQ0N");
       

        double chi_square_ndf = ex.GetChisquare() / ex.GetNDF();  
        calibrationParams[p] = { ex.GetParameter(0), ex.GetParameter(1), max_charge + 0.001f, chi_square_ndf };

```

I fit adc values vs charge. how does the chi2 is calculated?  
each adc value is the average of events adc = ((Float\_t)v.total) / v.samples;. do I need to calculate also the Standard error of the mean (SEM)? does root compensated for that while it does the fit? how do I add it, or I add error bars? an example or a plot below. Thank you very much in advance 🙂

 ![example](https://root-forum.cern.ch/uploads/default/original/3X/9/5/95df6aea32e4bc731a4fcc5030482d8f27093fe8.png)
