How to open RooWorkspace without any name?

How to open a root file which has a workspace in it but has no name about this workspace?

Hi @zebing_wang,

welcome to the ROOT forum!

I think I don’t need to tell you that it would have been best to create the workspace with a name that is not empty to begin with. So, if you have the opportunity to recreate that file, you should just do that.

In case you are forced to read exactly that file, let’s say it’s called file.root, you can’t use f.Get("") because it initially checks that the name is not empty.

However, you can use FindObjectAny(), which doesn’t do this check. Usually there should be no need to use that, but it’s a public function that will help you to get a pointer to this workspace:

std::unique_ptr<TFile> f{TFile::Open("file.root", "READ")};
auto ws = static_cast<RooWorkspace*>(f->FindObjectAny(""));

or in Python:

import ROOT

f = ROOT.TFile("file.root")

ws = f.FindObjectAny("")

I checked that this works with a file that I created like this:

RooWorkspace ws{""}; // RooWorkspace with an empty name
ws.factory("Gaussian::gauss(x[-4,4],mu[0,-4,4],sigma[1, 0.1, 10.])");

I hope this helps!



Thanks! That works!

Thanks! That works!

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