Pickle Python Objects into ROOT Files

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 the pickler 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:

from rootpy.plotting import Hist
from import dump
hlist = []
for i in range(10):
    hlist.append(Hist(10, 0, 10))
dump(hlist, 'test.root')

This writes a list of histograms to test.root. The histograms may be read back like this:

from import load
hlist = load('test.root')

The following additional notes apply:

  • Pickling may not always 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, a dummy class with no methods will be used instead. 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=False to disable this behavior.


io.pickler.dump(obj, root_file[, proto, key]) Dump an object into a ROOT TFile.
io.pickler.load(root_file[, use_proxy, key]) Load an object from a ROOT TFile.