Discussion:
[ROOT] How to register ROOT object read from python pickle
Benjamin Bannier
2011-01-11 21:33:31 UTC
Permalink
Dear ROOT experts,

I am trying to get a ROOT object loaded from a python pickle via name
from some global registry like gDirectory or gROOT, but somehow cannot
make it work.

If I write an object like

----(out.py)
import ROOT as r
import pickle

h = r.TH1D('h_test','', 100, -10, 10)

pickle.dump(h, file('h.pkl', 'w'))
----

and try to get it from gDirectory or gROOT like

----(in.py)
import ROOT as r
import pickle

h = pickle.load(file('h.pkl'))

for t in (r.gDirectory, r.gROOT):
print t.FindObjectAny('h_test')
print t.Get('h_test')
----

I get nothing:

----
$ python in.py
<ROOT.TObject object at 0x(nil)>
<ROOT.TObject object at 0x(nil)>
<ROOT.TObject object at 0x(nil)>
<ROOT.TObject object at 0x(nil)>
----

How would I make these objects known to the usual suspects or if they
are already know how do I get them?


Thanks,

Benjamin
Fine, Valeri
2011-01-12 01:22:17 UTC
Permalink
ROOT objects are C++ objects. You should use the ROOT I/O (for example,
TFile class) to read / write ROOT objects.


Best regards , Valeri Fine
-----------------------------------
Brookhaven National Laboratory
Upton, NY 11973
-----Original Message-----
On Behalf Of
Benjamin Bannier
Sent: Tuesday, January 11, 2011 4:34 PM
To: ROOT Talk
Subject: [ROOT] How to register ROOT object read from python pickle
Dear ROOT experts,
I am trying to get a ROOT object loaded from a python pickle via name
from some global
registry like gDirectory or gROOT, but somehow cannot make it work.
If I write an object like
----(out.py)
import ROOT as r
import pickle
h = r.TH1D('h_test','', 100, -10, 10)
pickle.dump(h, file('h.pkl', 'w'))
----
and try to get it from gDirectory or gROOT like
----(in.py)
import ROOT as r
import pickle
h = pickle.load(file('h.pkl'))
print t.FindObjectAny('h_test')
print t.Get('h_test')
----
----
$ python in.py
<ROOT.TObject object at 0x(nil)>
<ROOT.TObject object at 0x(nil)>
<ROOT.TObject object at 0x(nil)>
<ROOT.TObject object at 0x(nil)>
----
How would I make these objects known to the usual suspects or if they
are already know how
do I get them?
Thanks,
Benjamin
Benjamin Bannier
2011-01-12 02:52:10 UTC
Permalink
Dear Valeri,
Post by Benjamin Bannier
I am trying to get a ROOT object loaded from a python pickle via
name from some global registry like gDirectory or gROOT, but somehow
cannot make it work.
You should use the ROOT I/O (for example, TFile class) to read /
write ROOT objects.
I could do that, and maybe my example simplified what I wanted to do too
much. I will be streaming more complicated objects (e.g. tuples or
dictionaries containing ROOT objects). Python's pickle is very nice for
that since it requires minimal setup, both for writing and reading.
ROOT objects are C++ objects.
No, in python they are python objects and in ruby they are ruby objects
etc. ;)


Benjamin
Fine, Valeri
2011-01-13 20:07:55 UTC
Permalink
Post by Benjamin Bannier
Post by Fine, Valeri
ROOT objects are C++ objects.
No, in python they are python objects and in ruby they are ruby
objects etc. ;)

ROOT object remains the C++ object even though PyRoot hides this fact.
To use it you have to load PyRoot.
The PyRoot is to load the appropriated ROOT subsystem and C++ run-time
environment for you behind the scene.
However, I agree I expressed some narrow view. I overlooked the "use
case" when one wants to create the Python native container of the ROOT
C++ objects.
ROOT I/O cannot handle it alone. One needs pickle (json?) ( backed by
ROOT C++ object streamers to serialize /de-serialize the internal C++
objects )

Best regards , Valeri Fine
-----------------------------------
Brookhaven National Laboratory
Upton, NY 11973
Post by Benjamin Bannier
-----Original Message-----
Sent: Tuesday, January 11, 2011 9:52 PM
To: Fine, Valeri
Cc: ROOT Talk
Subject: Re: [ROOT] How to register ROOT object read from python
pickle
Post by Benjamin Bannier
Dear Valeri,
Post by Fine, Valeri
Post by Benjamin Bannier
I am trying to get a ROOT object loaded from a python pickle via
name from some global registry like gDirectory or gROOT, but somehow
cannot make it work.
You should use the ROOT I/O (for example, TFile class) to read /
write ROOT objects.
I could do that, and maybe my example simplified what I wanted to do
too much. I will be
Post by Benjamin Bannier
streaming more complicated objects (e.g. tuples or dictionaries
containing ROOT objects).
Post by Benjamin Bannier
Python's pickle is very nice for that since it requires minimal setup,
both for writing and
Post by Benjamin Bannier
reading.
Post by Fine, Valeri
ROOT objects are C++ objects.
No, in python they are python objects and in ruby they are ruby
objects etc. ;)
Post by Benjamin Bannier
Benjamin
BIAN Jianming
2011-01-12 03:30:51 UTC
Permalink
Dear ROOTers,

We defined a RooDataHist like following. Bin errors are set by an array. But when perform the fit we find that errors is set to poisson errors automatic. That means the results will not change when we change the input bin errors. Who can tell me how to fit RooDataHist with our input errors? Thanks!

double costheta[19] = {-0.9, -0.8, -0.7, -0.6, -0.5, -0.4, -0.3, -0.2, -0.1, 0.0,
0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9};
double num[19] = {97.9303,79.9721,64.0475,47.2125,67.8041,66.9418,39.2421,15.5514,34.8678,40.4425,52.4124,68.5636,59.2562,63.1224,41.115,43.1246,61.5914,67.7323,77.1489};
double numerr[19] = {19.0372,15.4138,15.8855,13.4445,16.1578,15.5069,14.0996,12.5128,13.6687,16.7594,15.2767,16.2664,15.6253,15.44,14.6369,14.215,14.6403,14.466,17.7352};
TH1F *DaHknew = new TH1F("DaHknew", "DaHknew", Nbin, up, lo);
double nsum=0;
for (int ibin=0; ibin<19; ibin++) {
DaHknew->SetBinContent((ibin+1), num[ibin]);
DaHknew->SetBinError((ibin+1), numerr[ibin]);
nsum+=num[ibin];
}
...
RooDataHist* rHKnew = new RooDataHist("rHKnew", "Knew mass(GeV)", costm, DaHknew);
...
RooAddPdf model("model", "model", RooArgList(helilam2, helilam0), ratio);
model.fitTo(*rHKnew,Save());
...


--
With my best wishes,

Jianming Bian
Experimental Physics Center,
Institute of High Energy Physics(IHEP),
Chinese Academy of Sciences
P.O.Box 918-1
Beijing, 100049
P.R.China
Tim Head
2011-01-12 03:23:05 UTC
Permalink
Post by Benjamin Bannier
and try to get it from gDirectory or gROOT like
----(in.py)
import ROOT as r
import pickle
h = pickle.load(file('h.pkl'))
 print t.FindObjectAny('h_test')
 print t.Get('h_test')
----
<snip>
Post by Benjamin Bannier
How would I make these objects known to the usual suspects or if they
are already know how do I get them?
Try:

h.SetDirectory(r.gDirectory)

after doing that both gROOT.Get() and gDirectory.Get() return the
histogram. But I am not sure if this is the "correct" way of doing
this, if it has unpleasant side effects or is missing some side
effects. It is very hard to know with ROOT ...

When the histogram is first unpickled its directory is set to nothing,
check out h.GetDirectory() after unpickling it.

Out of curiosity, why do you bother with being able to retrieve things
using the root gDirectory etc mechanism and don't store things in a
dictionary?

Tim
--
http://tim.jottit.com/
Benjamin Bannier
2011-01-12 04:54:55 UTC
Permalink
Hi Tim,
Post by Tim Head
h.SetDirectory(r.gDirectory)
Great, that's what I was looking for.
Post by Tim Head
Out of curiosity, why do you bother with being able to retrieve things
using the root gDirectory etc mechanism and don't store things in a
dictionary?
I am invoking CINT macros beyond my control via gROOT.ProcessLine in
python. I need them to know about the unpickled objects.


Thank you for your help,

Benjamin
Loading...