How to use RDataFrame.Fill() on the same object more than once in pyROOT?

Hi all, I’m having trouble with trying to fill a TH1 multiple times with an RDataFrame.

Lets say I define an RDataFrame with two columns and a TH1 to fill

df = ROOT.RDataFrame(100)
df = df.Define('x', '1')
df = df.Define('y', '2')

h = ROOT.TH1I('', '', 2, 1, 3)

I can fill it with one column with no issue

h_filled = df.Fill(h, ['x'])

but If I try twice, only the last Fill will be registered

h_filled = df.Fill(h, ['x'])
h_filled = df.Fill(h, ['y'])
h_filled.Draw()  # results in only the '2' bin being filled

I’m not able to use the filled object as an argument to the second fill like this:

h_filled = df.Fill(h, ['x'])
h_filled = df.Fill(h_filled, ['y'])
h_filled.Draw()  # runtime error

even trying to make two histograms and adding them together doesn’t work since Add() doesn’t recognise the RResultsPtr as a Histogram:

h = ROOT.TH1I('test', 'test', 3, 0, 3)
h2 = ROOT.TH1I('test2', 'test2', 3, 0, 3)
h_filled1 = df.Fill(h, ['x'])
h_filled2 = df.Fill(h2, ['y'])
h_filled1.Add(h_filled2)   # TypeError

Is this just not yet possible or am I misunderstanding something here?

ROOT Version: 6.24/00

Hi @kghorban ,

and welcome to the ROOT forum! Fill fills a copy of the object you pass as argument, and returns that copy. Calling it twice produces 2 copies. You should be able to do this:

h = ROOT.TH1I('test', 'test', 3, 0, 3)
h_filled1 = df.Fill(h, ['x'])
h_filled2 = df.Fill(h, ['y'])

Let me know if that’s not the case.



and I made a note of clarifying, in the docs, that Fill returns a copy of the “model” of the object to fill that is passed to it

Thank you for clarifying! I managed to get it to work by using .GetPtr() rather than GetValue() but they both seem to work.

h_filled = df.Fill(h, ['x'])
h_filled = df.Fill(h_filled.GetPtr(), ['y'])
h_filled.Draw()  # works

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