# Getting negative chi sq value, but the fit looks good?

I am getting this fit when I fit the combined model to the data.

this fit looks good to me but when I print the chi square value it always prints -1.
can you please correct my code?

roomodel.cpp (1.7 KB)

Hello,
How did you compute the chisquare ? Using `RooPlot::chiSquare` ?
See also How exactly does RooPlot::chisquare() compute the result? - #4 by Xuli

Lorenzo

Thanks for your suggestion, I just corrected my code a bit, and it gives Chi Sq = 142.046.
I did not use `RooPlot::chiSquare` I just used
double chiSquare = frame->chiSquare()
One more thing, that confuses me is that when I write
double chiSquare = frame->chiSquare(any integer)
it gives me little different ChiSq value.
What is the significance of this argument??

@moneta also If i increase the no. of bins to 10000 .
I get ChiSq = 23.18 , but my fit looks like this-

is there anything wrong in this?
Thanks.

Hi,

yes, you are using `RooPlot::chiSquare` (see the ROOT: RooPlot Class Reference), by calling `frame->chiSquare, since `frame` is a RooPlot object instance.

The function returns the reduced chiSquare (i.e. chisquare divided by the number of bins). If it is after a fit you should provide to the function the number of first parameters.
In addition, when you are not passing a curve name and a data name, `RooPlot::chiSquare` will compute the chi-square for the last curve you have plotted, in your case I think is the `sig3`. You should then provide the name of the data hist and of the curve. For doing this just ad this code to find these names:

``````for (int i = 0; i < frame->numItems(); i++) {
std::cout << "component " << frame->nameOf(i) << "  is a " << frame->getObject(i)->IsA()->GetName() << std::endl;
}
``````

and then call the correct one in `frame->chiSquare` , probably it should be: the data are the first (index 0) and the full pdf is second (with index 1) since this is the order how you have plotted them.

``````frame->chiSquare((frame->nameOf(1),frame->nameOf(0), numberOfFitParamters)
``````

The number of fit parameters seems to be 6, looking at your code.

If you increase the number of bins, since the function is returning the reduce chi-square it should still give a results around 1 if the fit is good.

Best,

Lorenzo

Dear @moneta

this is what it prints if I add your code and when I write

`frame->chiSquare((frame->model_Norm[X],frame->h_dh, numberOfFitParamters)`

it says these items are not in the RooPlot.
I am confused What exactly I should add to my code in order to get the chiSq value of model(not sig1 sig 2 sig3) fitted to the histogram.
I am sorry for the inconvenience
Im new to all these.
roomodel.cpp (2.1 KB)

Dear @Subhu18,