SND@LHC Software
Loading...
Searching...
No Matches
rootpyPickler.Pickler Class Reference
Inheritance diagram for rootpyPickler.Pickler:
Collaboration diagram for rootpyPickler.Pickler:

Public Member Functions

 __init__ (self, file, proto=0)
 
 dump (self, obj, key=None)
 
 clear_memo (self)
 
 persistent_id (self, obj)
 

Private Attributes

 __file
 
 __keys
 
 __io
 
 __pmap
 

Detailed Description

Definition at line 145 of file rootpyPickler.py.

Constructor & Destructor Documentation

◆ __init__()

rootpyPickler.Pickler.__init__ (   self,
  file,
  proto = 0 
)
Create a root pickler.
`file` should be a ROOT TFile. `proto` is the python pickle protocol
version to use.  The python part will be pickled to a ROOT
TObjString called _pickle; it will contain references to the
ROOT objects.

Definition at line 146 of file rootpyPickler.py.

146 def __init__(self, file, proto=0):
147 """Create a root pickler.
148 `file` should be a ROOT TFile. `proto` is the python pickle protocol
149 version to use. The python part will be pickled to a ROOT
150 TObjString called _pickle; it will contain references to the
151 ROOT objects.
152 """
153 self.__file = file
154 self.__keys = file.GetListOfKeys()
155 self.__io = IO_Wrapper()
156 self.__pmap = {}
157 if sys.version_info[0] < 3:
158 # 2.X old-style classobj
159 pickle.Pickler.__init__(self, self.__io, proto)
160 else:
161 super(Pickler, self).__init__(self.__io, proto)
162

Member Function Documentation

◆ clear_memo()

rootpyPickler.Pickler.clear_memo (   self)
Clears the pickler's internal memo.

Definition at line 179 of file rootpyPickler.py.

179 def clear_memo(self):
180 """Clears the pickler's internal memo."""
181 self.__pickle.memo.clear()
182

◆ dump()

rootpyPickler.Pickler.dump (   self,
  obj,
  key = None 
)
Write a pickled representation of obj to the open TFile.

Definition at line 163 of file rootpyPickler.py.

163 def dump(self, obj, key=None):
164 """Write a pickled representation of obj to the open TFile."""
165 if key is None:
166 key = '_pickle'
167 if 1>0:
168 self.__file.cd()
169 if sys.version_info[0] < 3:
170 pickle.Pickler.dump(self, obj)
171 else:
172 super(Pickler, self).dump(obj)
173 s = ROOT.TObjString(self.__io.getvalue())
174 self.__io.reopen()
175 s.Write(key)
176 self.__file.GetFile().Flush()
177 self.__pmap.clear()
178

◆ persistent_id()

rootpyPickler.Pickler.persistent_id (   self,
  obj 
)

Definition at line 183 of file rootpyPickler.py.

183 def persistent_id(self, obj):
184 if hasattr(obj, '_ROOT_Proxy__obj'):
185 obj = obj._ROOT_Proxy__obj()
186 if isinstance(obj, ROOT.TObject):
187 """
188 Write the object, and return the resulting NAME;CYCLE.
189 We used to do this::
190
191 o.Write()
192 k = self.__file.GetKey(o.GetName())
193 pid = "{0};{1:d}".format(k.GetName(), k.GetCycle())
194
195 It turns out, though, that destroying the python objects
196 referencing the TKeys is quite expensive (O(logN) where N is the
197 total number of pyroot objects?). Although we want to allow for
198 the case of saving multiple objects with the same name, the most
199 common case is that the name has not already been written to the
200 file. So we optimize for that case, doing the key lookup before we
201 write the object, not after. (Note further: GetKey() is very slow
202 if the key does not actually exist, as it does a linear search of
203 the key list. We use FindObject instead for the initial
204 lookup, which is a hashed lookup, but it is not guaranteed to
205 find the highest cycle. So if we do find an existing key, we
206 need to look up again using GetKey.
207 """
208 nm = obj.GetName()
209 key = self.__keys.FindObject(nm)
210 obj.Write()
211 if key:
212 key = self.__file.GetKey(nm)
213 pid = '{0};{1:d}'.format(nm, key.GetCycle())
214 else:
215 pid = nm + ';1'
216 return pid
217
218

Member Data Documentation

◆ __file

rootpyPickler.Pickler.__file
private

Definition at line 153 of file rootpyPickler.py.

◆ __io

rootpyPickler.Pickler.__io
private

Definition at line 155 of file rootpyPickler.py.

◆ __keys

rootpyPickler.Pickler.__keys
private

Definition at line 154 of file rootpyPickler.py.

◆ __pmap

rootpyPickler.Pickler.__pmap
private

Definition at line 156 of file rootpyPickler.py.


The documentation for this class was generated from the following file: