createChi2 of binned and weighted data set

Hello,

I am using ROOT 5.27/01 (trunk@32187, Feb 02 2010, 16:13:55 on linux).

I would like to do a chi2 fit to a weighted and binned data set. I start with unbinned data from a tree, then create the binned clone.

If I do it this way:[code]

RooDataSet tmp(“tmp”,“tmp”,dataVarSet,Import(tree1));
RooDataSet
data = new RooDataSet(“my_data”, “my_data”, dataVarSet, WeightVar(“weight”),Import(tmp), StoreError(dataVarSet) );

RooAbsData data_binned = (RooAbsData)data->binnedClone();
data = (RooDataSet*)data_binned;

//—cut out some code here—

RooAbsReal* chi2 = model->createChi2((RooDataHist&)*data, Extended(true),SumW2Error(true),PrintLevel(1),Range(“cutrange”) );[/code]

Then I get these runtime errors:

[#0] ERROR:Eval -- RooChi2Var::RooChi2Var(chi2_model_my_data_binned) INFINITY ERROR: bin 0 has zero error, but function is not zero (0.0347996) [#0] ERROR:Eval -- RooChi2Var::RooChi2Var(chi2_model_my_data_binned) INFINITY ERROR: bin 0 has zero error, but function is not zero (0.0347996)

And when I do it this way:

RooDataHist *data_binned   = data->binnedClone();
RooDataSet  *data_binned_w = (RooDataSet*) data->emptyClone();
for (int i=0 ; i<data_binned->numEntries() ; i++) {
data_binned_w->add(*data_binned->get(i),data_binned->weight(*data_binned->get(i),1,true,false) ) ;
}
RooDataSet* data = (RooDataSet*)data_binned_w;

//---cut out some code here---

RooAbsReal* chi2 = model->createChi2((RooDataHist&)*data, Extended(true),SumW2Error(true),PrintLevel(1),Range("cutrange") );

I get these runtime errors:

[#0] ERROR:Eval -- RooChi2Var::RooChi2Var(chi2_model_my_data) INFINITY ERROR: bin 3 has zero error, but function is not zero (0) [#0] ERROR:Eval -- RooChi2Var::RooChi2Var(chi2_model_my_data) INFINITY ERROR: bin 3 has zero error, but function is not zero (0)

Can someone explain these errors, or give an example of a chi2 fit to a weighted and binned data set?

Many thanks,
Anthony

I’ll ask again.
Can anyone explain these errors, or give an example of a chi2 fit to a weighted and binned data set?

The RooFit chi2 fit does not work when the bins have zero entries.
You should either use a binned likelihood fit or use the standard chi2 fit provided by ROOT.
In this case bins with zero entries are excluded from the fit

Best Regards

Lorenzo

Ok, good to know. I will use another method. Can someone add that to the documentation? It would have saved me a lot of time.

[quote=“moneta”]
You should either use a binned likelihood fit or use the standard chi2 fit provided by ROOT. [/quote]

Ok, so I’ve tried a binned likelihood fit via a RooAbsPdf::createNLL()
but I have a weighted and binned data set.
There is no option SumW2Error(Bool_t flag) for createNLL(). Doing this:

RooAbsReal* nll;
nll = model->createNLL(*datahist, Extended(true),Range("cutrange"), CloneData(true) ) ;
RooMinuit m(*nll);
m.migrad();
m.hesse();
m.minos();//new
result = m.save();

gives wrong error estimates.
I find it hard to believe there is no solution yet to fit a weighted AND binned dataset and get the correct error calculation.

I found this bug report, which may be relevent. Any progress?
savannah.cern.ch/bugs/?63997

I am using today’s version of ROOT (trunk@33086, Apr 19 2010).

I welcome any suggestions for fitting weighted and binned data.

Many thanks for your help,
Anthony

This bug is not yet been fixed. It could be that it affects your case.

You can use plain ROOT, make a TH1 and fit it with your function.
If your model is described as a RooAbsPdf you can get the TF1 simply with the method RooAbsPdf::asTF

TH1 * h1 = datahist->createHistogram(x);
TF1 * f = model->asTF(RooArgList(x) , parameters ); 
h1->Fit(f);

Best Regards

Lorenzo

[quote=“moneta”]This bug is not yet been fixed. It could be that it affects your case.
[/quote]

Ok, I’ve added myself to the mailing list for that bug.

Thanks for the suggestion, but this will not work. I have more than 3 observables.
createNLL looks the most promising for me, but it would require a SumW2Error(true) argument that doesn’t exist.

It will work but you need to create a THNSparse and fit it or use directly the ROOT::Fit::BinData class to create a ROOT::Fit::Chi2Function to minimize.
If you are interested I can send you an example.
However, due to the curse of dimensionality, I suspect you will have low statistics in many bins. In this case, you need to use a likelihood fit.
Why don’t you transform the binned data set in a weighted unbinned data sets using as first approximation the bin center values ?

Best regards

Lorenzo