10"""Pickle python data into a root file, preserving references to root objects.
12This module allows pickling python
13objects into a root file. The python objects may contain
14references to named root objects. If one has set up a structure
15of python objects to hold root histograms, this provides a
16convenient way of saving and restoring your histograms.
17The pickled python data are stored in an additional string
18object in the root file; any root objects are stored as usual.
19(Thus, root files written by root_pickle can be
20read just like any other root file if you don't care about the
23Here's an example of writing a pickle:
26 from PyAnalysisUtils.root_pickle import dump_root
30 hlist.append (ROOT.TH1F (name, name, 10, 0, 10))
31 dump_root (hlist, 'test.root')
33This writes a list of histograms to test.root.
34The histograms may be read back like this:
37 from PyAnalysisUtils.root_pickle import load_root
38 hlist = load_root ('test.root')
41The following additional notes apply.
43 - In addition to dump_root and
44 load_root, the module also provides
45 dump and load functions, which
46 take root file objects instead of file names.
48 - The present version of root_pickle will
49 not work correctly for the case of python objects deriving
50 from root objects. It will probably also not work for the
51 case of root objects which do not derive from
54 - When the pickled data are being read, if a class
55 doesn't exist, root_pickle will create a
56 dummy class with no methods and use that. This is
57 different from the standard pickle behavior (where it
58 would be an error), but it simplifies usage in the common
59 case where the class is being used to hold histograms, and
60 its methods are entirely concerned with filling the
63 - When restoring a reference to a root object, the default behavior
64 is to not read the root object itself, but instead to create a proxy.
65 The root object will then be read the first time the proxy is accessed.
66 This can help significantly with time and memory usage if you're
67 only accessing a small fraction of the root objects, but it does
68 mean that you need to keep the root file open. Pass use_proxy=0
69 to disable this behavior.
77 if hasattr (ROOT.TDirectory,
'CurrentDirectory'):
78 d = ROOT.TDirectory.CurrentDirectory()
79 if hasattr (d,
'load'):
83 if hasattr (d,
'_resolve'):
90 ROOT.TDirectory.cd (d)
137 self.
_s = ROOT.TObjString()
149 slen = len(self.
_str)
150 while i != 0
and self.
_pos < slen:
154 if self.
_pos >= slen:
169 slen = len(self.
_str)
170 while self.
_pos < slen:
174 if self.
_pos >= slen:
202 """Create a root pickler.
203FILE should be a Root TFile. PROTO is the python pickle protocol
204version to use. The python part will be pickled to a Root
205TObjString called _pickle; it will contain references to the
218 """Write a pickled representation of o to the open TFile."""
225 s = self.
__io.getvalue()
236 """Clears the pickler's internal memo."""
243 if hasattr (o,
'_Root_Proxy__obj'):
244 o = o._Root_Proxy__obj()
245 if (isinstance (o, ROOT.TObject)):
265 k = self.
__keys.FindObject(nm)
268 k = self.
__file.GetKey (nm)
269 pid =
"%s;%d" % (nm, k.GetCycle())
296 __slots__ = (
'__f',
'__pid',
'__o')
305 if self.
__o.__class__.__module__ !=
'ROOT':
306 self.
__o.__class__.__module__ =
'ROOT'
307 return getattr (self.
__o, a)
311 if self.
__o.__class__.__module__ !=
'ROOT':
312 self.
__o.__class__.__module__ =
'ROOT'
315 def __init__ (self, file, use_proxy = True, use_hash = False):
316 """Create a root unpickler.
317FILE should be a Root TFile.
322 pickle.Unpickler.__init__ (self, self.
__io)
330 for k
in file.GetListOfKeys():
334 if cy > ctab.get(nm,0):
351 ret = htab.get ((nm,cy),
None)
353 print (
"did't find", nm, cy, len(htab))
364 """Read a pickled object representation from the open file."""
369 save = _compat_hooks[0]()
372 s = self.
__file.Get (key +
';%d' % self.
__n)
373 self.
__io.setvalue (s)
374 o = pickle.Unpickler.load(self)
378 save = _compat_hooks[1](save)
383 o = Root_Proxy (self.
__file, pid)
392 if module ==
'copy_reg':
394 elif module ==
'__builtin__':
399 mod = sys.modules[module]
401 print (
"Making dummy module %s" % (module))
405 sys.modules[module] = mod
406 klass = getattr(mod, name)
408 except AttributeError:
409 print (
"Making dummy class %s.%s" % (module, name))
410 mod = sys.modules[module]
413 setattr (mod, name, Dummy)
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."""
426 _compat_hooks = hooks
430def dump (o, f, proto=0, key=None):
431 """Dump object O to the Root TFile F."""
434def load (f, use_proxy = 1, key=None):
435 """Load an object from the Root TFile F."""
439 """Dump object O to the Root file named FNAME."""
440 f = ROOT.TFile (fname ,
"RECREATE")
441 dump (o, f, proto, key)
446 """Load an object from the Root file named FNAME."""
447 return load (ROOT.TFile (fname), use_proxy, key)
void clear()
Empty the pool.
__init__(self, file, proto=0)
find_class(self, module, name)
persistent_load(self, pid)
__init__(self, file, use_proxy=True, use_hash=False)
load_root(fname, use_proxy=1, key=None)
load(f, use_proxy=1, key=None)
dump_root(o, fname, proto=0, key=None)