SND@LHC Software
Loading...
Searching...
No Matches
rootpyPickler Namespace Reference

Classes

class  IO_Wrapper
 
class  Pickler
 
class  ROOT_Proxy
 
class  Unpickler
 

Functions

 _protect (s)
 
 _restore (s)
 
 compat_hooks (hooks)
 
 dump (obj, root_file, proto=0, key=None)
 
 load (root_file, use_proxy=1, key=None)
 

Variables

 string_types = basestring,
 
tuple integer_types = (int, long)
 
list __all__
 
 _compat_hooks = None
 
dict xdict = {}
 
int xserial = 0
 

Detailed Description

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 rootpyPickler import Pickler
pkl=Pickler(file)
pkl.dump(ShipGeo,'ShipGeo')

from rootpyPickler import Unpickler
upkl    = Unpickler(f)
ShipGeo = upkl.load('ShipGeo')

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=0 to disable this behavior.

Function Documentation

◆ _protect()

rootpyPickler._protect (   s)
protected

Definition at line 91 of file rootpyPickler.py.

91def _protect(s):
92 return s.replace(b'\377', b'\377\376').replace(b'\000', b'\377\001')
93
94

◆ _restore()

rootpyPickler._restore (   s)
protected

Definition at line 95 of file rootpyPickler.py.

95def _restore(s):
96 return s.replace(b'\377\001', b'\000').replace(b'\377\376', b'\377')
97
98

◆ compat_hooks()

rootpyPickler.compat_hooks (   hooks)
Set compatibility hooks.
If this is set, then hooks[0] is called before loading, and hooks[1] is
called after loading.  hooks[1] is called with the return value of hooks[0]
as an argument.  This is useful for backwards compatibility in some
situations.

Definition at line 341 of file rootpyPickler.py.

341def compat_hooks(hooks):
342 """Set compatibility hooks.
343 If this is set, then hooks[0] is called before loading, and hooks[1] is
344 called after loading. hooks[1] is called with the return value of hooks[0]
345 as an argument. This is useful for backwards compatibility in some
346 situations.
347 """
348 global _compat_hooks
349 _compat_hooks = hooks
350
351

◆ dump()

rootpyPickler.dump (   obj,
  root_file,
  proto = 0,
  key = None 
)
Dump an object into a ROOT TFile.

`root_file` may be an open ROOT file or directory, or a string path to an
existing ROOT file.

Definition at line 352 of file rootpyPickler.py.

352def dump(obj, root_file, proto=0, key=None):
353 """Dump an object into a ROOT TFile.
354
355 `root_file` may be an open ROOT file or directory, or a string path to an
356 existing ROOT file.
357 """
358 if isinstance(root_file, string_types):
359 root_file = ROOT.TFile.Open(root_file, 'recreate')
360 own_file = True
361 else:
362 own_file = False
363 ret = Pickler(root_file, proto).dump(obj, key)
364 if own_file:
365 root_file.Close()
366 return ret
367
368

◆ load()

rootpyPickler.load (   root_file,
  use_proxy = 1,
  key = None 
)
Load an object from a ROOT TFile.

`root_file` may be an open ROOT file or directory, or a string path to an
existing ROOT file.

Definition at line 369 of file rootpyPickler.py.

369def load(root_file, use_proxy=1, key=None):
370 """Load an object from a ROOT TFile.
371
372 `root_file` may be an open ROOT file or directory, or a string path to an
373 existing ROOT file.
374 """
375 if isinstance(root_file, string_types):
376 root_file = ROOT.TFile.Open(root_file)
377 own_file = True
378 else:
379 own_file = False
380 obj = Unpickler(root_file, use_proxy).load(key)
381 if own_file:
382 root_file.Close()
383 return obj

Variable Documentation

◆ __all__

list rootpyPickler.__all__
private
Initial value:
1= [
2 'dump',
3 'load',
4 'compat_hooks',
5]

Definition at line 68 of file rootpyPickler.py.

◆ _compat_hooks

rootpyPickler._compat_hooks = None
protected

Definition at line 75 of file rootpyPickler.py.

◆ integer_types

tuple rootpyPickler.integer_types = (int, long)

Definition at line 64 of file rootpyPickler.py.

◆ string_types

rootpyPickler.string_types = basestring,

Definition at line 62 of file rootpyPickler.py.

◆ xdict

dict rootpyPickler.xdict = {}

Definition at line 76 of file rootpyPickler.py.

◆ xserial

int rootpyPickler.xserial = 0

Definition at line 77 of file rootpyPickler.py.