# How to get Chi squared values for a fit

**URL:** <https://root-forum.cern.ch/t/how-to-get-chi-squared-values-for-a-fit/53701>\
**Category:** Newbie\
**Created:** [February 20, 2023, 3:16pm UTC](https://root-forum.cern.ch/t/how-to-get-chi-squared-values-for-a-fit/53701 "2023-02-20T15:16:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [February 20, 2023, 3:16pm UTC](https://root-forum.cern.ch/t/how-to-get-chi-squared-values-for-a-fit/53701/1 "2023-02-20T15:16:50Z")

</div>

I am fitting a gaussian to the data, then in order to see that if mt fit is good or bad, what code I should use to get Chi squared value?  
[roomodel.cpp](https://root-forum.cern.ch/uploads/short-url/iAFAv5hbOf4g6Wo3UBfHlbG74YT.cpp) (1.4 KB)

you might not be able to execute it as you do not have that root file, but can you please tell me the code which I should use to get Chi squared value.

---

<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:** [February 21, 2023, 8:27am UTC](https://root-forum.cern.ch/t/how-to-get-chi-squared-values-for-a-fit/53701/2 "2023-02-21T08:27:59Z")

</div>

Hi,

In RoFit, you can use [`RooPlot::chiSquare`](https://root.cern.ch/doc/master/classRooPlot.html#a70290000399ccd22f58980dbbcead23c)

Lorenzo

---

<div class="post-metadata">

**Author:** ![Subhu18](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/cdc98d/32.png) [@Subhu18](https://root-forum.cern.ch/u/Subhu18)\
**Post date:** [February 21, 2023, 1:10pm UTC](https://root-forum.cern.ch/t/how-to-get-chi-squared-values-for-a-fit/53701/3 "2023-02-21T13:10:10Z")

</div>

Is it the code I should add to my macro?  
Ans where will it print the value of the ChiSquare?

---

<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:** [February 21, 2023, 3:23pm UTC](https://root-forum.cern.ch/t/how-to-get-chi-squared-values-for-a-fit/53701/4 "2023-02-21T15:23:31Z")

</div>

Hi,  
You can see the example from this tutorial, [ROOT: tutorials/roofit/rf109\_chi2residpull.C File Reference](https://root.cern.ch/doc/master/rf109__chi2residpull_8C.html), but you need after the fit to provide the number of fit parameters. In your case it is enough you add this line after fitting:

```auto
double chiSquare = frame->chiSquare(2);

```

Lorenzo

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 7, 2023, 3:24pm UTC](https://root-forum.cern.ch/t/how-to-get-chi-squared-values-for-a-fit/53701/5 "2023-03-07T15:24:09Z")

</div>

Closed on March 7, 2023, 3:24pm UTC

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