How to use weighted unbinned dataset in roofit

Dear Expert,

I have the variables md0, md0pi, and weight in my ntuple. I want to create a weighted dataset of md0 and md0pi variables using weight. and do a 2d fit of md0 and md0pi.
I am trying like, below:
RooDataSet * dataxy = new RooDataSet(“dataxy”,“dataxy”, tree, RooArgSet(mD0, mD0pi, weight));

and in the fit results:
RooFitResult *fitresult = model.fitTo(*dataxy, Save(true), Strategy(2), Extended(true), RooFit::SumW2Error(true));

But its do not seem to be weighted.

Here are the scripts that I am using:
fit_2d_WS_check_unbinned.C (14.9 KB)

Can you please suggest it?


When creating a weighted dataset, you need to specify the weight variable in the constructor of RooDataSet. However, it seems like you might not have declared the weight as a RooRealVar and included it in your RooArgSet.

Try something like this:

RooRealVar weight("weight", "weight", 0, 100); // Adjust the range as needed
RooDataSet *dataxy = new RooDataSet("dataxy", "dataxy", RooArgSet(mD0, mD0pi, weight), WeightVar(weight));

Hi @EmberEnigma,

Thanks for your reply.
But its gives the following error:

Can you please have a look?


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

The answer by @EmberEnigma is almost correct :slight_smile:

You need to pass the WeightVar() as a command argument to this constructor of RooDataSet.

However, this constructor doesn’t take the tree directly, but you need to use the Import(tree) command argument as explained in the docs that I linked.

So this should work:

RooDataSet("dataxy", "dataxy", {mD0, mD0pi, weight},
           WeightVar(weight), Import(tree));

Does it?


Actually, it looks like the documentation of Import() is wrong. It actually takes the TTree by reference, and not by pointer:

So you need to do Import(*tree), dereferencing the pointer. I will open a PR in ROOT to fix the documentation.