Pickle python data into a root file, preserving references to root objects.
This module allows pickling python
objects into a root file. The python objects may contain
references to named root objects. If one has set up a structure
of python objects to hold root histograms, this provides a
convenient way of saving and restoring your histograms.
The pickled python data are stored in an additional string
object in the root file; any root objects are stored as usual.
(Thus, root files written by root_pickle can be
read just like any other root file if you don't care about the
python data.)
Here's an example of writing a pickle:
import ROOT
from PyAnalysisUtils.root_pickle import dump_root
hlist = []
for i in range (10):
name = 'h%d' % i
hlist.append (ROOT.TH1F (name, name, 10, 0, 10))
dump_root (hlist, 'test.root')
This writes a list of histograms to test.root.
The histograms may be read back like this:
import ROOT
from PyAnalysisUtils.root_pickle import load_root
hlist = load_root ('test.root')
The following additional notes apply.
- In addition to dump_root and
load_root, the module also provides
dump and load functions, which
take root file objects instead of file names.
- The present version of root_pickle will
not work correctly for the case of python objects deriving
from root objects. It will probably also not work for the
case of root objects which do not derive from
TObject.
- When the pickled data are being read, if a class
doesn't exist, root_pickle will create a
dummy class with no methods and use that. This is
different from the standard pickle behavior (where it
would be an error), but it simplifies usage in the common
case where the class is being used to hold histograms, and
its methods are entirely concerned with filling the
histograms.
- When restoring a reference to a root object, the default behavior
is to not read the root object itself, but instead to create a proxy.
The root object will then be read the first time the proxy is accessed.
This can help significantly with time and memory usage if you're
only accessing a small fraction of the root objects, but it does
mean that you need to keep the root file open. Pass use_proxy=0
to disable this behavior.