I want to do a fit to a weighted data set, and I’d like clarification on something that doesn’t quite make sense to me.
It seems I have to both declare a weight variable (and assign its values) and tell add
the value of the weight:
import ROOT
x = ROOT.RooRealVar('x', 'x', 0, 10)
w = ROOT.RooRealVar('w', 'w', 0, 1)
obs = ROOT.RooArgSet(x, w)
data = ROOT.RooDataSet('data', 'data', obs, ROOT.RooFit.WeightVar(w))
for i in xrange(1, 101):
x.setVal(float(i) / 10)
w.setVal(float(i) / 100)
data.add(obs, w.getVal())
In this case, data.Print()
gives
RooDataSet::data[x,weight:w] = 100 entries (50.5 weighted)
It seems like I should only have to do one or the other of these. Why do I have to tell RooFit the value of the weight twice (both in the value of w
and again when I add
it)?
Elaboration:
In rf403_weightedevts.C
, it looks like it is enough to add a variable to a RooDataSet
and declare that it is a weight, e.g.:
obs = ROOT.RooArgSet(x, w)
data = ROOT.RooDataSet('data', 'data', obs, ROOT.RooFit.WeightVar(w))
for i in xrange(1, 101):
x.setVal(float(i) / 10)
w.setVal(float(i) / 100)
data.add(obs)
but after I do this, each entry seems to have a weight of 1. data.Print()
gives
RooDataSet::data[x,weight:w] = 100 entries (100 weighted)
If I specify a weight value instead of a variable, however, no weight seems to be assigned at all:
obs = ROOT.RooArgSet(x)
data = ROOT.RooDataSet('data', 'data', obs)
for i in xrange(1, 101):
x.setVal(float(i) / 10)
data.add(obs, float(i) / 100)
and data.Print()
gives
RooDataSet::data[x] = 100 entries
So it looks like I have to tell RooFit
the value twice: once in the value of w
and again in the call to add
.