Define new RDataFrame column using values from TH1F in PyROOT


I want define a new RDataFrame column using the values from a TH1F I have loaded from a .root file. Something like this:

df = ROOT.ROOT.RDataFrame(‘tree’,‘rootfile’)
weights = ROOT.TFile(“histrootfile”,“READ”)
w = weights.Get(“histname”)

Usually I would do this to get the value of a bin:
w.GetBinContent(w.FindBin(number from tbranch))

then I want to do:
df.Define(“new_column”, “w.GetBinContent(w.FindBin(number from tbranch))”)

but this syntax doesn’t work when I try to use it in Define. Does anyone know the appropriate syntax for this?



ROOT Version: 6.17/01
Platform: ]Ubuntu 18.04 LTS
Compiler: Not Provided

Hi Derek,

it is possible to access the histogram in the file from within a define. I wrote this simple example to demonstrate it:

import ROOT

# this file contains a histo called "hpx"
f = ROOT.TFile("hsimple.root")

# We use a feature called "dynamic scopes" to create a variable holding hpx
ROOT.gInterpreter.ProcessLine("auto histo = hpx;")

df = ROOT.ROOT.RDataFrame(3)
df2 = df.Define("hpx_bin", "histo->GetBinContent(rdfentry_)")
m = df2.Mean("hpx_bin")


Here I used the hsimple.root file in the tutorials directory of ROOT, the hpx histogram contained in it as well the deafult variable rdfentry_.
Let us know how it goes for your example.




Thanks so much! This is exactly what I was looking for.


1 Like

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