2 bugs in RooChi2Var with Ranges

Hello,

I believe I have discovered a bug in the RooChi2Var when used with ranges. It manifests itself in two ways:
(1) when using a weighted dataset, if the fit range is shorter than the range of the observable, a series of errors are reported and the fit fails.
(2) when not using a weighted dataset, iif multiple ranges are use, e.g. for side bands, the range is ignored.

the following script reproduces the problems:

#include "RooAbsData.h"
using namespace RooStats;
using namespace RooFit;
void chi2_test(){
RooRandom::randomGenerator()->SetSeed(0);
RooRealVar *x = new RooRealVar("x","x",100,300);
x->setRange("all",100,300);
x->setRange("short",100,250);
x->setRange("low",100,150);
x->setRange("hi",200,300);
RooRealVar *par = new RooRealVar("par","par",-0.02,-5.,-5E-7);
RooExponential *pdf = new RooExponential("pdf","pdf",*x,*par);
TF1 *f = pdf->asTF(*x,*par);
TH1F *h1 = new TH1F("h1","",50,100,300);
TH1F *h2 = new TH1F("h2","",50,100,300);
//weight A
for( int i=0;i<10000;i++ ){
   w = pow(10,i/1000-5);
   Double_t r = f->GetRandom();
   h1->Fill(r,w);
   if(r>150&&r<200) h2->Fill(r,1); //Double fill in window
   h2->Fill(r,1);
}

RooDataHist *dh1 = new RooDataHist("dh1","dh1",*x,h1);
RooDataHist *dh2 = new RooDataHist("dh2","dh2",*x,h2);

cout << "TEST 1" << endl;
//Test 1, try a range other than the full obserable range
cout << "  first fit:" << endl;
pdf->chi2FitTo(*dh1,DataError(RooAbsData::SumW2)); //RESULT: -0.0213 +/- 0.0007
cout << "  second fit:" << endl;
pdf->chi2FitTo(*dh1,DataError(RooAbsData::SumW2),Range("short")); //INFINITY ERROR!! Fit fails!!

cout << "TEST 2" << endl;
// Test 2, try multiple ranges
cout << "  first fit:" << endl;
pdf->chi2FitTo(*dh2,DataError(RooAbsData::Poisson),Range("all")); //RESULT: -2.55 +/- 0.02
cout << "  second fit:" << endl;
pdf->chi2FitTo(*dh2,DataError(RooAbsData::Poisson),Range("low,high")); //RESULT: -2.55 +/- 0.02

}
1 Like