ATLAS Offline Software
Loading...
Searching...
No Matches
python.root_pickle Namespace Reference

Classes

class  Pickler
class  Read_Wrapper
class  Root_Proxy
class  Saver
class  Unpickler
class  Write_Wrapper

Functions

 _getdir ()
 _setdir (d)
 compat_hooks (hooks)
 dump (o, f, proto=0, key=None)
 load (f, use_proxy=1, key=None)
 dump_root (o, fname, proto=0, key=None)
 load_root (fname, use_proxy=1, key=None)

Variables

 _compat_hooks = None
 xsave = Saver()

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 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.

Function Documentation

◆ _getdir()

python.root_pickle._getdir ( )
protected

Definition at line 76 of file root_pickle.py.

76def _getdir():
77 if hasattr (ROOT.TDirectory, 'CurrentDirectory'):
78 d = ROOT.TDirectory.CurrentDirectory()
79 if hasattr (d, 'load'):
80 # Handle case of CurrentDirectory() returning an atomic.
81 d = d.load()
82 return ROOT.gDirectory
83
84

◆ _setdir()

python.root_pickle._setdir ( d)
protected

Definition at line 85 of file root_pickle.py.

85def _setdir (d):
86 ROOT.TDirectory.cd (d)
87
88
89#
90# This stuff was originally written in terms of an stringIO stream.
91# But with py3, i couldn't find a better way of getting bytes objects
92# into and out of a TString.
93#
94# Argh! We can't store NULs in TObjStrings.
95# But pickle protocols > 0 are binary protocols, and will get corrupted
96# if we truncate at a NUL.
97# So, when we save the pickle data, make the mappings:
98# 0x00 -> 0xff 0x01
99# 0xff -> 0xff 0xfe
100# ... This may actually be obsolete --- looks like we can have NULs
101# in TObjString now, if we access the TString direectly. But retain
102# for compatibility with existing pickles.
103#
104

◆ compat_hooks()

python.root_pickle.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 415 of file root_pickle.py.

415def compat_hooks (hooks):
416 """Set compatibility hooks.
417If this is set, then hooks[0] is called before loading,
418and hooks[1] is called after loading. hooks[1] is called with
419the return value of hooks[0] as an argument. This is useful
420for backwards compatibility in some situations."""
421 global _compat_hooks
422 _compat_hooks = hooks
423 return
424
425

◆ dump()

python.root_pickle.dump ( o,
f,
proto = 0,
key = None )
Dump object O to the Root TFile F.

Definition at line 426 of file root_pickle.py.

426def dump (o, f, proto=0, key=None):
427 """Dump object O to the Root TFile F."""
428 return Pickler(f, proto).dump(o, key)
429
-event-from-file

◆ dump_root()

python.root_pickle.dump_root ( o,
fname,
proto = 0,
key = None )
Dump object O to the Root file named FNAME.

Definition at line 434 of file root_pickle.py.

434def dump_root (o, fname, proto=0, key=None):
435 """Dump object O to the Root file named FNAME."""
436 f = ROOT.TFile (fname , "RECREATE")
437 dump (o, f, proto, key)
438 f.Close()
439 return
440

◆ load()

python.root_pickle.load ( f,
use_proxy = 1,
key = None )
Load an object from the Root TFile F.

Definition at line 430 of file root_pickle.py.

430def load (f, use_proxy = 1, key=None):
431 """Load an object from the Root TFile F."""
432 return Unpickler(f, use_proxy).load(key)
433

◆ load_root()

python.root_pickle.load_root ( fname,
use_proxy = 1,
key = None )
Load an object from the Root file named FNAME.

Definition at line 441 of file root_pickle.py.

441def load_root (fname, use_proxy = 1, key=None):
442 """Load an object from the Root file named FNAME."""
443 return load (ROOT.TFile (fname), use_proxy, key)
444
445

Variable Documentation

◆ _compat_hooks

python.root_pickle._compat_hooks = None
protected

Definition at line 289 of file root_pickle.py.

◆ xsave

python.root_pickle.xsave = Saver()

Definition at line 290 of file root_pickle.py.