Combining RooDataSets in pyROOT

Is there a streamlined way to combine categorized RooDataSets in pyROOT? There is a partial answer from years ago in the forum, but this necessitates the extremely awkward

import ROOT as r

# create workspace
ws = r.RooWorkspace("ws")
# create variable
x = ws.factory("x[0, 100]")

# get datasets and declare categories
dsdict = {category: dataset, ...}
cat = ws.factory(f"infile[{' ,'.join(dsdict.keys())}]")

# a lot of ROOT magic to convert from a dictionary to a map
r.gInterpreter.GenerateDictionary("std::pair<std::string, RooDataSet*>", "map;string;RooDataSet.h")
r.gInterpreter.GenerateDictionary("std::map<std::string, RooDataSet*>", "map;string;RooDataSet.h")
r.gInterpreter.GenerateDictionary("std::pair<std::map<string,RooDataSet*>::iterator, bool>", "map;string;RooDataSet.h")
dsmap = r.std.map('string, RooDataSet*')()
dsmap.keepalive = list()
for c, d in dsdict.items():
    dsmap.keepalive.append(d)
    dsmap.insert(dsmap.begin(), r.std.pair("const string,RooDataSet*")(c, d))
# combine
ds = r.RooDataSet(
    "data",
    "data",
    r.RooArgSet(x),
    r.RooFit.Index(cat),
    r.RooFit.Import(dsmap),
)

Are there any pythonizations or easier workarounds I have missed?

Hi! As far as I know there is no way of doing this in a nicer way in pyROOT.

That’s why the RooDataSet::RooDataSet constructor is on our hit list of functions to pythonize in the future: [RF] Pythonisations for RooFit · Issue #7217 · root-project/root · GitHub.

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