10 """Pickle python data into a root file, preserving references to root objects.
12 This module allows pickling python
13 objects into a root file. The python objects may contain
14 references to named root objects. If one has set up a structure
15 of python objects to hold root histograms, this provides a
16 convenient way of saving and restoring your histograms.
17 The pickled python data are stored in an additional string
18 object in the root file; any root objects are stored as usual.
19 (Thus, root files written by root_pickle can be
20 read just like any other root file if you don't care about the
23 Here'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')
33 This writes a list of histograms to test.root.
34 The histograms may be read back like this:
37 from PyAnalysisUtils.root_pickle import load_root
38 hlist = load_root ('test.root')
41 The 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'):
82 return ROOT.gDirectory
86 ROOT.TDirectory.cd (d)
133 self.
_s = ROOT.TObjString()
145 slen = len(self.
_str)
146 while i != 0
and self.
_pos < slen:
150 if self.
_pos >= slen:
165 slen = len(self.
_str)
166 while self.
_pos < slen:
170 if self.
_pos >= slen:
198 """Create a root pickler.
199 FILE should be a Root TFile. PROTO is the python pickle protocol
200 version to use. The python part will be pickled to a Root
201 TObjString called _pickle; it will contain references to the
214 """Write a pickled representation of o to the open TFile."""
221 s = self.
__io.getvalue()
232 """Clears the pickler's internal memo."""
239 if hasattr (o,
'_Root_Proxy__obj'):
240 o = o._Root_Proxy__obj()
241 if (isinstance (o, ROOT.TObject)):
261 k = self.
__keys.FindObject(nm)
264 k = self.
__file.GetKey (nm)
265 pid =
"%s;%d" % (nm, k.GetCycle())
292 __slots__ = (
'__f',
'__pid',
'__o')
301 if self.
__o.__class__.__module__ !=
'ROOT':
302 self.
__o.__class__.__module__ =
'ROOT'
303 return getattr (self.
__o, a)
307 if self.
__o.__class__.__module__ !=
'ROOT':
308 self.
__o.__class__.__module__ =
'ROOT'
311 def __init__ (self, file, use_proxy = True, use_hash = False):
312 """Create a root unpickler.
313 FILE should be a Root TFile.
318 pickle.Unpickler.__init__ (self, self.
__io)
326 for k
in file.GetListOfKeys():
330 if cy > ctab.get(nm,0):
347 ret = htab.get ((nm,cy),
None)
349 print (
"did't find", nm, cy, len(htab))
360 """Read a pickled object representation from the open file."""
365 save = _compat_hooks[0]()
368 s = self.
__file.Get (key +
';%d' % self.
__n)
369 self.
__io.setvalue (s)
370 o = pickle.Unpickler.load(self)
374 save = _compat_hooks[1](save)
379 o = Root_Proxy (self.
__file, pid)
388 if module ==
'copy_reg':
390 elif module ==
'__builtin__':
395 mod = sys.modules[module]
397 print (
"Making dummy module %s" % (module))
401 sys.modules[module] = mod
402 klass = getattr(mod, name)
404 except AttributeError:
405 print (
"Making dummy class %s.%s" % (module, name))
406 mod = sys.modules[module]
409 setattr (mod, name, Dummy)
416 """Set compatibility hooks.
417 If this is set, then hooks[0] is called before loading,
418 and hooks[1] is called after loading. hooks[1] is called with
419 the return value of hooks[0] as an argument. This is useful
420 for backwards compatibility in some situations."""
422 _compat_hooks = hooks
426 def dump (o, f, proto=0, key=None):
427 """Dump object O to the Root TFile F."""
430 def load (f, use_proxy = 1, key=None):
431 """Load an object from the Root TFile F."""
435 """Dump object O to the Root file named FNAME."""
436 f = ROOT.TFile (fname ,
"RECREATE")
437 dump (o, f, proto, key)
442 """Load an object from the Root file named FNAME."""
443 return load (ROOT.TFile (fname), use_proxy, key)