Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #114 from scikit-hep/issue-107
Browse files Browse the repository at this point in the history
Issue 107
  • Loading branch information
jpivarski authored Aug 7, 2018
2 parents 59b23cc + a595e42 commit f5bf675
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
9 changes: 9 additions & 0 deletions uproot/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,15 @@ def show(self, width=80, minimum=None, maximum=None, stream=sys.stdout):

uproot.rootio.methods["TH2"] = TH2Methods

class THnSparseMethods(object):
# makes __doc__ attribute mutable before Python 3.3
__metaclass__ = type.__new__(type, "type", (TH1Methods.__metaclass__,), {})

def hello(self):
return "world", len(dir(self))

uproot.rootio.methods["THnSparse"] = THnSparseMethods

class TH1(TH1Methods, list):
def _type(self):
if all(isinstance(x, numbers.Integral) for x in self):
Expand Down
67 changes: 31 additions & 36 deletions uproot/rootio.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,9 @@ def showstreamers(self, filtername=nofilter, stream=sys.stdout):
if filtername(x.fName):
x.show(stream=stream)

def _classof(self, classname):
if classname == b"TDirectory" or classname == b"TDirectoryFile":
cls = ROOTDirectory
else:
cls = self._context.classes.get(classname, None)
if cls is None:
cls = ROOTObject.__metaclass__("Undefined_" + str(_safename(classname)), (Undefined,), {"_classname": classname})
return cls

def iterkeys(self, recursive=False, filtername=nofilter, filterclass=nofilter):
for key in self._keys:
cls = self._classof(key.fClassName)
cls = _classof(self._context, key.fClassName)
if filtername(key.fName) and filterclass(cls):
yield self._withcycle(key)

Expand All @@ -272,7 +263,7 @@ def iterkeys(self, recursive=False, filtername=nofilter, filterclass=nofilter):

def itervalues(self, recursive=False, filtername=nofilter, filterclass=nofilter):
for key in self._keys:
cls = self._classof(key.fClassName)
cls = _classof(self._context, key.fClassName)
if filtername(key.fName) and filterclass(cls):
yield key.get()

Expand All @@ -282,7 +273,7 @@ def itervalues(self, recursive=False, filtername=nofilter, filterclass=nofilter)

def iteritems(self, recursive=False, filtername=nofilter, filterclass=nofilter):
for key in self._keys:
cls = self._classof(key.fClassName)
cls = _classof(self._context, key.fClassName)
if filtername(key.fName) and filterclass(cls):
yield self._withcycle(key), key.get()

Expand All @@ -292,7 +283,7 @@ def iteritems(self, recursive=False, filtername=nofilter, filterclass=nofilter):

def iterclasses(self, recursive=False, filtername=nofilter, filterclass=nofilter):
for key in self._keys:
cls = self._classof(key.fClassName)
cls = _classof(self._context, key.fClassName)
if filtername(key.fName) and filterclass(cls):
yield self._withcycle(key), cls

Expand Down Expand Up @@ -488,6 +479,15 @@ def _readobjany(source, cursor, context, parent, asclass=None):

return obj # return object

def _classof(context, classname):
if classname == b"TDirectory" or classname == b"TDirectoryFile":
cls = ROOTDirectory
else:
cls = context.classes.get(_safename(classname), None)
if cls is None:
cls = ROOTObject.__metaclass__("Undefined_" + str(_safename(classname)), (Undefined,), {"_classname": classname})
return cls

def _readstreamers(source, cursor, context, parent):
tlist = TList.read(source, cursor, context, parent)

Expand Down Expand Up @@ -692,17 +692,14 @@ def _defineclasses(streamerinfos, classes):
" for index in range(self.{0}):".format(_safename(element.fCountName)),
" self.{0} = {1}.read(source, cursor, context, self)".format(_safename(element.fName), _safename(element.fTypeName.rstrip(b"*")))])

elif isinstance(element, TStreamerObjectAnyPointer):
code.append(" _raise_notimplemented({0}, {1}, source, cursor)".format(repr(element.__class__.__name__), repr(repr(element.__dict__))))

elif isinstance(element, TStreamerObjectPointer):
if element.fType == uproot.const.kObjectp:
elif isinstance(element, (TStreamerObjectAnyPointer, TStreamerObjectPointer)):
if element.fType == uproot.const.kObjectp or element.fType == uproot.const.kAnyp:
if pyclassname in skip and _safename(element.fName) in skip[pyclassname]:
code.append(" Undefined.read(source, cursor, context, self)")
else:
code.append(" self.{0} = {1}.read(source, cursor, context, self)".format(_safename(element.fName), _safename(element.fTypeName.rstrip(b"*"))))
fields.append(_safename(element.fName))
elif element.fType == uproot.const.kObjectP:
elif element.fType == uproot.const.kObjectP or element.fType == uproot.const.kAnyP:
if pyclassname in skip and _safename(element.fName) in skip[pyclassname]:
code.append(" _readobjany(source, cursor, context, parent, asclass=Undefined)")
else:
Expand All @@ -725,7 +722,7 @@ def _defineclasses(streamerinfos, classes):
fields.append(_safename(element.fName))

else:
raise AssertionError
raise AssertionError(element)

code.extend([" if self.__class__.__name__ == cls.__name__:",
" self.__class__ = cls._versions[classversion]",
Expand All @@ -752,7 +749,7 @@ def _defineclasses(streamerinfos, classes):
code.insert(0, " _classname = {0}".format(repr(streamerinfo.fName)))
else:
code.insert(0, " _classname = b{0}".format(repr(streamerinfo.fName)))
code.insert(0, " _fields = [{0}]".format(", ".join(repr(x) for x in fields)))
code.insert(0, " _fields = [{0}]".format(", ".join(repr(str(x)) for x in fields)))
code.insert(0, "class {0}({1}):".format(pyclassname, ", ".join(bases)))

if pyclassname in classes:
Expand Down Expand Up @@ -847,19 +844,8 @@ def get(self, dismiss=True):
Objects are not read or decompressed until this function is explicitly called.
"""

classname = self.fClassName.decode("ascii")
try:
if classname == "TDirectory" or classname == "TDirectoryFile":
return ROOTDirectory.read(self._source, self._cursor.copied(), self._context, self)

elif classname in self._context.classes:
return self._context.classes[classname].read(self._source, self._cursor.copied(), self._context, self)

else:
out = Undefined.read(self._source, self._cursor.copied(), self._context, self)
out._classname = classname
return out

return _classof(self._context, self.fClassName).read(self._source, self._cursor.copied(), self._context, self)
finally:
if dismiss:
self._source.dismiss()
Expand Down Expand Up @@ -1091,11 +1077,18 @@ def _readinto(cls, self, source, cursor, context, parent):
################################################################ streamed classes (with some overrides)

class ROOTStreamedObject(ROOTObject):
pass

class TObject(ROOTStreamedObject):
_fields = []

@classmethod
def _members(cls):
out = []
for t in cls.__bases__:
if issubclass(t, ROOTStreamedObject):
out.extend(t._members())
out.extend(cls._fields)
return out

class TObject(ROOTStreamedObject):
@classmethod
def _readinto(cls, self, source, cursor, context, parent):
_skiptobj(source, cursor)
Expand All @@ -1110,6 +1103,8 @@ def __str__(self):
return self.decode("utf-8", "replace")

class TNamed(TObject):
_fields = ["fName", "fTitle"]

@classmethod
def _readinto(cls, self, source, cursor, context, parent):
start, cnt, self._classversion = _startcheck(source, cursor)
Expand Down
2 changes: 1 addition & 1 deletion uproot/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import re

__version__ = "2.9.9"
__version__ = "2.9.10"
version = __version__
version_info = tuple(re.split(r"[-\.]", __version__))

Expand Down

0 comments on commit f5bf675

Please sign in to comment.