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

Classes

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

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 d = ROOT.gDirectory
83 if hasattr (d, '_resolve'):
84 # Handle case of CurrentDirectory() returning TDirectoryPythonAdapter.
85 d = d._resolve()
86 return d
87
88

◆ _setdir()

python.root_pickle._setdir ( d)
protected

Definition at line 89 of file root_pickle.py.

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

◆ 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 419 of file root_pickle.py.

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

◆ dump()

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

Definition at line 430 of file root_pickle.py.

430def dump (o, f, proto=0, key=None):
431 """Dump object O to the Root TFile F."""
432 return Pickler(f, proto).dump(o, key)
433
-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 438 of file root_pickle.py.

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

◆ load()

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

Definition at line 434 of file root_pickle.py.

434def load (f, use_proxy = 1, key=None):
435 """Load an object from the Root TFile F."""
436 return Unpickler(f, use_proxy).load(key)
437

◆ 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 445 of file root_pickle.py.

445def load_root (fname, use_proxy = 1, key=None):
446 """Load an object from the Root file named FNAME."""
447 return load (ROOT.TFile (fname), use_proxy, key)
448
449

Variable Documentation

◆ _compat_hooks

python.root_pickle._compat_hooks = None
protected

Definition at line 293 of file root_pickle.py.

◆ xsave

python.root_pickle.xsave = Saver()

Definition at line 294 of file root_pickle.py.