# Pythonization of RooDataSetHelper with weights not working?

**URL:** <https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413>\
**Category:** Roofit and RooStats\
**Created:** [November 17, 2025, 4:46pm UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413 "2025-11-17T16:46:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rquaglia](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rquaglia/32/35548_2.png) [@rquaglia](https://root-forum.cern.ch/u/rquaglia)\
**Post date:** [November 17, 2025, 4:46pm UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/1 "2025-11-17T16:46:18Z")

</div>

Dear experts,

Here is a reproducer of the problem. ( basically a tutorial script with the weight var for RDataFrame → RooDataSet making)

```python
import ROOT
import math
 
 
# Set up
# ------------------------
 
# We create an RDataFrame with two columns filled with 2 million random numbers.
d = ROOT.RDataFrame(2000000)
dd = d.Define("x", "gRandom->Uniform(-5., 5.)").Define("y", "gRandom->Gaus(1., 3.)").Define("weight", "gRandom->Uniform(0.1, 1.0)")

 
 
# We create RooFit variables that will represent the dataset.
x = ROOT.RooRealVar("x", "x", -5.0, 5.0)
y = ROOT.RooRealVar("y", "y", -50.0, 50.0)
x.setBins(10)
y.setBins(20) 
 
# Booking the creation of RooDataSet / RooDataHist in RDataFrame
# ----------------------------------------------------------------
 
# Method 1:
# ---------
# We directly book the RooDataSetHelper action.
# We need to pass
# - the RDataFrame column types as template parameters
# - the constructor arguments for RooDataSet (they follow the same syntax as the usual RooDataSet constructors)
# - the column names that RDataFrame should fill into the dataset
#
# NOTE: RDataFrame columns are matched to RooFit variables by position, *not by name*!
rooDataSet = dd.Book(
    ROOT.std.move(ROOT.RooDataSetHelper("dataset", "Title of dataset", ROOT.RooArgSet(x, y))), ("x", "y", "weight")
)

def printData(data):
    print("")
    data.Print()
    for i in range(min(data.numEntries(), 20)):
        print(
            "("
            + ", ".join(["{0:8.3f}".format(var.getVal()) for var in data.get(i)])
            + ", ) weight={0:10.3f}".format(data.weight())
        )
 
    print("mean(x) = {0:.3f}".format(data.mean(x)) + "\tsigma(x) = {0:.3f}".format(math.sqrt(data.moment(x, 2.0))))
    print("mean(y) = {0:.3f}".format(data.mean(y)) + "\tsigma(y) = {0:.3f}\n".format(math.sqrt(data.moment(y, 2.0))))
 
 
printData(rooDataSet)

```

I added to the tutorial here [ROOT: tutorials/roofit/rf408\_RDataFrameToRooFit.py File Reference](https://root.cern/doc/v630/rf408__RDataFrameToRooFit_8py.html) the case for weighted dataset construction and the code segfault.

```auto
rooDataSet = dd.Book(
    ROOT.std.move(ROOT.RooDataSetHelper("dataset", "Title of dataset", ROOT.RooArgSet(x, y))), ("x", "y", "weight")
)

```

following what suggested in [ROOT: RooAbsDataHelper\< DataSet\_t \> Class Template Reference](https://root.cern.ch/doc/master/classRooAbsDataHelper.html) but apparently the Book call is unhappy when Argset content do not match columns length in python.  
Is there a pythonization which is not correctly loaded here?

Thanks in advance renato

* * *

## ROOT Version: 6.34.04 Built for linuxx8664gcc on Feb 26 2025, 15:54:54 From tags/6-34-04@6-34-04

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [November 17, 2025, 8:40pm UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/2 "2025-11-17T20:40:58Z")

</div>

I think @jonas can help here

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [November 17, 2025, 10:27pm UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/3 "2025-11-17T22:27:28Z")

</div>

Thanks for the report!

Indeed, the documentation was wrong, but I’m fixing this:

> <https://github.com/root-project/root/pull/20447>
>
> Fix the example to correctly construct a weighted RooDataSet.
> 
> Thanks to the f…ollowing forum post for spotting this: https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/

If you create a weighted RooDataSet, you need to explicitly specify the `WeightVar("weight")` in the constructor, so that the weighted RooDataSet can be constructed correctly.

Let me know if it works now!

Cheers,  
Jonas

---

<div class="post-metadata">

**Author:** ![rquaglia](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rquaglia/32/35548_2.png) [@rquaglia](https://root-forum.cern.ch/u/rquaglia)\
**Post date:** [November 18, 2025, 12:02am UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/4 "2025-11-18T00:02:10Z")

</div>

Thanks a lot for the hint, it is much clearer now which if i got right imply that you forward the RooCmdArg.

With this

```auto
rooDataSet = dd.Book(
    ROOT.std.move(ROOT.RooDataSetHelper("dataset", "Tit\
le of dataset", ROOT.RooArgSet(x, y), ROOT.RooFit.Weigh\
tVar("weight"))), ("x", "y", "weight")
)

```

It works , however i guess that for the weightvar one should match always the column name to pass at the end with the WeightVar argument value, correct?

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [November 18, 2025, 10:15am UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/5 "2025-11-18T10:15:44Z")

</div>

No, the variable names don’t need to match. As the docs say, the variables are mapped by position and not by name. And if there is one more variable taken from the RDF than in the `RooArgSet` that specifies the RooFit dataset, it is assumed to be the weight column.

---

<div class="post-metadata">

**Author:** ![rquaglia](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rquaglia/32/35548_2.png) [@rquaglia](https://root-forum.cern.ch/u/rquaglia)\
**Post date:** [November 18, 2025, 1:39pm UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/6 "2025-11-18T13:39:56Z")

</div>

Thanks @jonas

Just to be sure :  
Is what i posted before correct then?

```auto
rooDataSet = dd.Book(
    ROOT.std.move(ROOT.RooDataSetHelper("dataset", "Tit\
le of dataset", ROOT.RooArgSet(x, y), ROOT.RooFit.Weigh\
tVar("weight"))), ("x", "y", "weight")
)

```

i see my dataset plotted with weights later on so i assumed it is correct, but are you saying that

```python
rooDataSet = dd.Book(
    ROOT.std.move(ROOT.RooDataSetHelper("dataset", "Tit\
le of dataset", ROOT.RooArgSet(x, y), ROOT.RooFit.Weigh\
tVar("weight"))), ("x", "y", "weightcolumnname")
)

```

would work even if “weight” is not a column in rdataframe? it just remap it to “weight” ?  
Or do i need to supply a RooRealVar weight in the RooArgSet instead?

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [November 18, 2025, 2:18pm UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/7 "2025-11-18T14:18:01Z")

</div>

Yes, what you posted before is correct.

Sorry, I got your follow up question wrong then! What I said was that the names of the RooRealVars in the RooDataSet don’t have the match the names of the columns you take from the RDataFrame. But the names of the columns you take from the RDataFrame need to correspond to actual columns in the RDataFrame.

---

<div class="post-metadata">

**Author:** ![rquaglia](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rquaglia/32/35548_2.png) [@rquaglia](https://root-forum.cern.ch/u/rquaglia)\
**Post date:** [November 18, 2025, 2:31pm UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/8 "2025-11-18T14:31:33Z")

</div>

thanks a lot for the clarification, this was clear , my doubt was mostly about the

```auto
RooArgSet( var1,var2), WeightVar("xxx") )), ["col1","col2","colweight"])

```

and wether in `WeightVar("xxx")` , xxx musth match the column name or “colweight” is auto-interpreted as Weight Var and assigned to “xxx” label.

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [November 18, 2025, 2:46pm UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/9 "2025-11-18T14:46:17Z")

</div>

> and wether in `WeightVar("xxx")` , xxx musth match the column name or “colweight” is auto-interpreted as Weight Var and assigned to “xxx” label.

No, the names don’t have to mach. I think [the documentation](https://root.cern.ch/doc/master/classRooAbsDataHelper.html) tries to be clear about this with this warning:

> ```
> Variables in the dataset and columns in RDataFrame are matched by position, not by name. This enables the easy exchanging of columns that should be filled into the dataset. 
> 
> ```

You have a suggestion to make this clearer? Sorry for the confusion! 🙂

---

<div class="post-metadata">

**Author:** ![rquaglia](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rquaglia/32/35548_2.png) [@rquaglia](https://root-forum.cern.ch/u/rquaglia)\
**Post date:** [November 18, 2025, 2:51pm UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/10 "2025-11-18T14:51:23Z")

</div>

Thanks a lot, i think the best is to actually have the example with weights in the tutorials, as i suspect this to be quite useful to have as reference. Anyway all clear now. Thank you

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [December 2, 2025, 2:51pm UTC](https://root-forum.cern.ch/t/pythonization-of-roodatasethelper-with-weights-not-working/64413/11 "2025-12-02T14:51:47Z")

</div>

Closed on December 2, 2025, 2:51pm UTC

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