Reading out RooDataSet from RooWorkspace

Hi,

I’m stuck on a problem with RooFit workspaces. I’m trying to read a RooDataSet previously imported in a RooWorkspace. RooWorkspace does have a .data() which give access to RooAbsData object through a pointer, but there is no similar method for RooDataSet (neither some Get<T> method like TDirectory).
I’ve seen some solution based on raw c-like pointer cast, but it doesn’t sound like a solution for me.

Exemple:

[...includes...]

RooWorkspace generate() {
    RooWorkspace wksp {"MinimalExemple", false};
    RooRealVar x {"x", "x", 0, 100};
    RooRealVar y {"y", "y", -1, 100};

    RooDataSet dataSet {"foo", "foo", RooArgSet {x, y}};
    for (int i = 0; i < 100; i++) {
        dataSet.add(RooArgSet(i, i + gRandom->Rndm()));
    }

    wksp.import(dataSet);
    return wksp;
}

void test() {
    auto wksp = generate();
    [...?...]
}

Last thing that I’ve tried is to use WSDir nested class to get access to Get<RooDataSet>method.

void test() {
    auto wksp = generate();
    auto wkspDir = RooWorkspace::WSDir("dir", "dir", &wksp);
    std::unique_ptr<RooDataSet> dataSet {wkspDir.Get<RooDataSet>("foo")};
    if (!dataSet) {
        std::cout << ":(" << std::endl;
    } else {
        dataSet->write("xy_data.dat");
    }
}

But it just does not work, I always get a null ptr (tested with cling/gcc).

root [0] 
Processing test.C...

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby 
                Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
                All rights reserved, please read http://roofit.sourceforge.net/license.txt

[#1] INFO:ObjectHandling -- RooWorkspace::import(MinimalExemple) importing dataset foo
[#1] INFO:ObjectHandling -- RooWorkspace::import(MinimalExemple) importing RooRealVar::x
[#1] INFO:ObjectHandling -- RooWorkspace::import(MinimalExemple) importing RooRealVar::y
:(

Any hint on what I’ve done wrong will be helpful :slight_smile:

Thanks for taking the time to read this message,

Cheers.

Yann.


_ROOT Version: 6.26/10
_Platform: Linux Mint 21 x86_64
_Compiler: gcc 11.3.0 / cling


Hi @zazbone,

I am inviting @jonas to the topic; maybe he can help here.

Cheers,
J.

1 Like

HI @zazbone, welcome to the ROOT forum!

Right, there is no type-safe way to get RooDataSets or RooDataHists from a RooWorkspace.
You always get a base RooAbsData pointer via RooWorkspace::data().

Indeed, you should avoid C-style casts. Using static_cast is safer:

RooDataSet *data = static_cast<RooDataSet*>(wksp.data("foo"));

Remember that this will be a nullptr if there is no dataset named “foo” in the workspace. And if it’s a RooDataHist, you get undefined behavior :slight_smile:

Let me know if you have more questions!

Cheers,
Jonas

Hi !

Ok, that’s what I was afraid of :sweat_smile: . I will probably try to find a better way to manage my datasets, keeping it all the way long in dataframes may be.

Thank you for the clarification :slight_smile:

Have a nice day.

Yann.

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