Fit and plot of weighted data sets

Dear all,
starting from ROOT version 5.32, I’ve a problem with fits and plots of weighted data sets.

The fit does not work properly if I try to fit a weighted data set with a RooAddPdf and conditional observables. The problem does not show up if simpler models are used. The fit works much better if the default optimization behavior is deactivated, by choosing Optimize(0) or Optimize(1).

In the same situation, the PDF is not correctly plotted (and I could not find a way to deactivate the default optimization for plotting).

Everything worked properly with previous ROOT versions.

You can find below a simplified example (in this specific case, the covariance matrix by HESSE is not posdef even in the “good” case, but the different behavior of Optimize(1) and Optimize(2) is nonetheless clear).

Does anybody have some idea to get also the good plots?

Thank you in advance for your help,
Francesco

weighted_fit(){

  RooRealVar x("x","x",0.,-10.,10.);
  RooRealVar w("w","w",0.,0.,1000.);
  RooRealVar mean("mean","mean",0.,-10.,10.);
  RooRealVar frac("frac","frac",0.3,0.,1.);
  RooRealVar sigma("sigma","sigma",1.,0.,10.);
  RooRealVar sigma2("sigma2","sigma2",2.,0.,10.);

  RooGaussian gaus1("gaus1","gaus1",x,mean,sigma);
  RooGaussian gaus2("gaus2","gaus2",x,mean,sigma2);

  RooAddPdf gaus("gaus","gaus",RooArgList(gaus1,gaus2),frac);

  RooDataSet data("data","data",RooArgSet(x,w,mean));

  for(Int_t i=0;i<100000;i++){

    x.setVal(gRandom->Uniform(-10.,10.));
    mean.setVal(gRandom->Gaus(-1.,1.));
    w.setVal(0.6*TMath::Gaus(x.getVal(),mean.getVal(),1.,1) + 0.4*TMath::Gaus(x.getVal(),mean.getVal(),4.,1));

    data.add(RooArgSet(x,w,mean));

  }

  RooDataSet dataw("dataw","Weighted Data",&data,*data.get(),0,"w");

  gaus.fitTo(dataw,RooFit::SumW2Error(kTRUE),RooFit::ConditionalObservables(RooArgSet(mean)),RooFit::Optimize(1));
  //gaus.fitTo(dataw,RooFit::SumW2Error(kTRUE),RooFit::ConditionalObservables(RooArgSet(mean)),RooFit::Optimize(2));

  RooPlot *frame = x.frame();
  dataw.plotOn(frame,RooFit::DataError(RooAbsData::SumW2),RooFit::ProjWData(mean,dataw));
  gaus.plotOn(frame);

  frame->Draw();

}

Hi,

I did not have any problem with different optimize values in 5.33.02.
To Draw the correct projection you need to add RooFit::ProjWData(obs, data) to the call of RooAbsPdf::plotOn and not on the dataset. Do

 gaus.plotOn(frame, RooFit::ProjWData(mean,dataw));

Best Regards

Lorenzo