Consistent pointer to RooWorkspace when working interactively

I’ve encountered an unexpected behaviour when working with my RooWorkspace interactively. To reproduce, create a file like this:

RooWorkspace w("w","w");

Now open the file in an interactive root session (root /tmp/test.root) …

If, on the root prompt, I simply type “w” I get one pointer. But if I do gDirectory->Get("w") I get a different pointer. Additionally, if I repeat calls to this, I get different pointers each time. So each time a new copy of the workspace is being read from the file, yes? I wanted to make the behaviour more like other standard ROOT objects, so after reading the workspace I add it to the gDirectory. This resolves my issue with different pointers retrieved by the Get method, but the root prompt shortcut of using just the workspace name still refers to a different workspace.

root [1] .ls
TFile**         /tmp/test.root  
 TFile*         /tmp/test.root  
  KEY: RooWorkspace     w;1     w
root [2] w
(RooWorkspace *) 0x7f8284c55a00
root [3] gDirectory->Get("w")
(TObject *) 0x7f8284d46a00
root [4] gDirectory->Get("w")
(TObject *) 0x7f8284d80e00
root [5] gDirectory->Add(gDirectory->Get("w"))
root [6] gDirectory->Get("w")
(TObject *) 0x7f82839c0c00
root [7] gDirectory->Get("w")
(TObject *) 0x7f82839c0c00
root [8] w
(RooWorkspace *) 0x7f8284c55a00

How can I access the same instance of the workspace that the ROOT prompt is accessing here? Or rather, how can I assure the ROOT prompt accesses the same version as my C++ code would access through the Get method?

Hi @will_cern,

I assume that the workspace gets updated when you access it. Could you .ls again after playing with it a bit? Are you getting different name cycles? Such as

  KEY: RooWorkspace     w;2     w
  KEY: RooWorkspace     w;1     w

As you can see in TDirectory::Get, you get the in-memory object if there’s one, and the newest object in the file if there’s nothing in memory. To get a specific cycle, you have to mention the cycle number.
If you always want the latest cycle, irrespective of whether the object is already in memory, you have to read it via GetKey and then ReadObj, which will always return the latest cycle.

I can confirm no new cycles are created, no new keys appear when using TDirectory::Get , I just get a different pointer each time

Ok, I assume @pcanal can help here.

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