# Extended Chi^2 in RooFit

Good Morning!
I am using RooFit to estimate the total number of events in a distribution, using an extended fit.

1. Is it correct to extend the pdf normalized to 1 to an extended pdf in which the total number of events is a free parameter of the fit with the following code:

``` RooRealVar n("n","number of events",10000, 9000, 11000) ; RooExtendPdf gen_ext("gen_ext","extended PDF",*genpdf,n);```
2) Does the argument `DataError(RooAbsData::Expected)` create a Pearson chi^2 when passed to the createChi2 method?
3) Why `DataError(RooAbsData::Expected)` doesn’t draw the errors while `DataError(RooAbsData::Poisson)` does?
Here is the full code (sorry for the Italian comments)

[code]#ifndef CINT
#include “RooGlobalFunc.h”
#endif
#include “RooRealVar.h”
#include “RooDataSet.h”
#include “RooGaussian.h”
#include “TCanvas.h”
#include “TAxis.h”
#include “RooPlot.h”
#include “RooClassFactory.h”
#include “TROOT.h”

#ifndef CINT
#include “MyPdfV3.h”
#endif

using namespace RooFit ;

void ChiFit () {

// Declare observable x
RooRealVar x(“x”,“x”,0,2) ;
x.setBins(40);

// The RooClassFactory::makePdfInstance() function performs code writing, compiling, linking
// and object instantiation in one go and can serve as a straight replacement of RooGenericPdf

RooRealVar mean(“mean”,“mean”,1, .5, 1.5) ;
RooRealVar sigma(“sigma”,“sigma”,.4, .3, .5) ;
RooRealVar tau(“tau”, “tau”, 3, 1, 4);
RooAbsPdf* genpdf = RooClassFactory::makePdfInstance(“GenPdf”,".5TMath::Gaus(x,mean,sigma)+.5TMath::Exp(-x/tau)",RooArgSet(x,mean,sigma,tau)) ; //fin qui normalizza da solo a 1

// Make extended pdf from GenPdf

RooRealVar n(“n”,“number of events”,10000, 9000, 11000) ;
RooExtendPdf gen_ext(“gen_ext”,“extended PDF”,*genpdf,n) ; //estendo genpdf al caso in cui la sua normalizzazione sia un parametero libero del fit (n)

// Generate a toy dataset from the interpreted p.d.f
RooDataSet* data2 = gen_ext.generate(x,10000) ;
RooDataHist* binnedData = data2->binnedClone() ;

// Fit the interpreted p.d.f to the generated data

RooAbsReal* chi2 = gen_ext.createChi2((RooDataHist&)*binnedData, Extended(true), DataError(RooAbsData::Expected), PrintLevel(1)); //definisce il chi^2 di Pearson rispetto alla pdf (estesa)

RooMinuit m(chi2) ; //minimizzo il chi^2 rispetto ai parameteri
m.hesse() ;
RooFitResult
r = m.save();

// Make a plot of the data and the p.d.f overlaid
RooPlot* frame2 = x.frame(Title(“My PDF”)) ;
binnedData->plotOn(frame2,DataError(RooAbsData::Expected)) ;
gen_ext.plotOn(frame2) ;
gen_ext.paramOn(frame2, Layout(0.6), Format(“NEU”, AutoPrecision(1)), Parameters(RooArgList(n,mean,sigma,tau)));
frame2->Draw();

double chired = chi2->getVal()/(40-4); //chi^ min / dof
std::cout << "Chi2 Val: " << chired << endl;

r->Print(“v”);

} [/code]
Thank you!!

Hi,

1. yes is correct

2)yes correct. You create a Pearson chi2

1. Are you sure the errors are not drawn ? They are maybe very small ?
In case of small bin content you might have in this case a very small expected error. For example in case of a bin with Nobserved = 1 you might have the function value = 10^-4 which gives an expected error of 10^-2, so it is not visible in the plot.

Lorenzo