Skip to content

Commit

Permalink
Merge pull request #760 from ndawe/master
Browse files Browse the repository at this point in the history
fix gPad access for ROOT 6.10
  • Loading branch information
ndawe committed Jul 7, 2017
2 parents e2afe19 + eec8a68 commit ee9ac62
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 82 deletions.
26 changes: 12 additions & 14 deletions rootpy/ROOT.py
Expand Up @@ -60,7 +60,7 @@
__all__ = []


def proxy_global(name, no_expand_macro=False, fname='func'):
def proxy_global(name, no_expand_macro=False, fname='func', args=()):
"""
Used to automatically asrootpy ROOT's thread local variables
"""
Expand All @@ -78,18 +78,14 @@ def func():

@property
def gSomething(self):
glob = getattr(ROOT, name)
orig_func = getattr(glob, fname)

def asrootpy_izing_func():
return self(orig_func())

# new_glob = copy(glob)
new_glob = glob.__class__.__new__(glob.__class__)
new_glob.func = asrootpy_izing_func
# Memoize
setattr(type(self), name, new_glob)
return new_glob
obj_func = getattr(getattr(ROOT, name), fname)
try:
obj = obj_func(*args)
except ReferenceError: # null pointer
return None
# asrootpy
return self(obj)

return gSomething


Expand Down Expand Up @@ -127,7 +123,9 @@ def __getattr__(self, what):
def R(self):
return ROOT

gPad = proxy_global("gPad")
gPad = proxy_global("gPad",
fname='GetPad' if ROOT_VERSION >= (6, 9, 2) else 'func',
args=(0,) if ROOT_VERSION >= (6, 9, 2) else ())
gVirtualX = proxy_global("gVirtualX")

if ROOT_VERSION < (5, 32, 0): # pragma: no cover
Expand Down
8 changes: 4 additions & 4 deletions rootpy/context.py
Expand Up @@ -50,13 +50,13 @@ def preserve_current_canvas():
Context manager which ensures that the current canvas remains the current
canvas when the context is left.
"""
old = ROOT.gPad.func()
old = ROOT.gPad
try:
yield
finally:
if old:
old.cd()
elif ROOT.gPad.func():
elif ROOT.gPad:
# Put things back how they were before.
with invisible_canvas():
# This is a round-about way of resetting gPad to None.
Expand All @@ -70,7 +70,7 @@ def preserve_current_directory():
Context manager which ensures that the current directory remains the
current directory when the context is left.
"""
old = ROOT.gDirectory.func()
old = ROOT.gDirectory
try:
yield
finally:
Expand Down Expand Up @@ -159,7 +159,7 @@ def set_directory(robject):
else:
old_dir = robject.GetDirectory()
try:
robject.SetDirectory(ROOT.gDirectory.func())
robject.SetDirectory(ROOT.gDirectory)
yield
finally:
robject.SetDirectory(old_dir)
Expand Down
4 changes: 2 additions & 2 deletions rootpy/decorators.py
Expand Up @@ -74,8 +74,8 @@ def method_file_check(f):
"""
@wraps(f)
def wrapper(self, *args, **kwargs):
curr_dir = ROOT.gDirectory.func()
if isinstance(curr_dir, ROOT.TROOT):
curr_dir = ROOT.gDirectory
if isinstance(curr_dir, ROOT.TROOT) or not curr_dir:
raise RuntimeError(
"You must first create a File before calling {0}.{1}".format(
self.__class__.__name__, _get_qualified_name(f)))
Expand Down
8 changes: 4 additions & 4 deletions rootpy/io/file.py
Expand Up @@ -134,7 +134,7 @@ def root_open(filename, mode=''):
mode = mode_map[mode]

filename = expand_path(filename)
prev_dir = ROOT.gDirectory.func()
prev_dir = ROOT.gDirectory
root_file = ROOT.R.TFile.Open(filename, mode)
if not root_file:
raise IOError("could not open file: '{0}'".format(filename))
Expand Down Expand Up @@ -215,7 +215,7 @@ def __iter__(self):
return self.objects()

def __enter__(self):
curr_dir = ROOT.gDirectory.func()
curr_dir = ROOT.gDirectory
if curr_dir != self:
self._prev_dir = curr_dir
self.cd()
Expand Down Expand Up @@ -630,7 +630,7 @@ def __init__(self, name, title=None, classname='', parent=None):
if title is None:
title = name
# grab previous directory before creating self
self._prev_dir = ROOT.gDirectory.func()
self._prev_dir = ROOT.gDirectory
self._parent = parent or self._prev_dir
super(Directory, self).__init__(name, title, classname, parent or 0)
self._post_init()
Expand All @@ -649,7 +649,7 @@ def __init__(self, name, *args, **kwargs):
# trigger finalSetup
ROOT.R.kTRUE
# grab previous directory before creating self
self._prev_dir = ROOT.gDirectory.func()
self._prev_dir = ROOT.gDirectory
super(_FileBase, self).__init__(name, *args, **kwargs)
self._post_init()

Expand Down
16 changes: 8 additions & 8 deletions rootpy/io/tests/test_file.py
Expand Up @@ -52,20 +52,20 @@ def test_file_open():

def test_context():
with MemFile() as a:
assert_equal(ROOT.gDirectory.func(), a)
assert_equal(ROOT.gDirectory, a)
with MemFile() as b:
d = Directory('test')
with d:
assert_equal(ROOT.gDirectory.func(), d)
assert_equal(ROOT.gDirectory.func(), b)
assert_equal(ROOT.gDirectory.func(), a)
assert_equal(ROOT.gDirectory, d)
assert_equal(ROOT.gDirectory, b)
assert_equal(ROOT.gDirectory, a)

# test out of order
f1 = MemFile()
f2 = MemFile()
with f1:
assert_equal(ROOT.gDirectory.func(), f1)
assert_equal(ROOT.gDirectory.func(), f2)
assert_equal(ROOT.gDirectory, f1)
assert_equal(ROOT.gDirectory, f2)
f1.Close()
f2.Close()

Expand All @@ -75,9 +75,9 @@ def test_context():
# test without with statement
f1 = MemFile()
f2 = TemporaryFile()
assert_equal(ROOT.gDirectory.func(), f2)
assert_equal(ROOT.gDirectory, f2)
f2.Close()
assert_equal(ROOT.gDirectory.func(), f1)
assert_equal(ROOT.gDirectory, f1)
f1.Close()


Expand Down
2 changes: 1 addition & 1 deletion rootpy/plotting/__init__.py
Expand Up @@ -38,5 +38,5 @@
@super_overridden
class DrawableKeepAlive(object):
def Draw(self, *args, **kwargs):
keepalive(ROOT.gPad.func(), self)
keepalive(ROOT.gPad, self)
return super(DrawableKeepAlive, self).Draw(*args, **kwargs)
2 changes: 1 addition & 1 deletion rootpy/plotting/base.py
Expand Up @@ -502,7 +502,7 @@ def Draw(self, *args, **kwargs):
if kwargs:
return self.DrawCopy(*args, **kwargs)

pad = ROOT.gPad.func()
pad = ROOT.gPad
own_pad = False
if not pad:
# avoid circular import by delaying import until needed here
Expand Down
32 changes: 15 additions & 17 deletions rootpy/plotting/box.py
Expand Up @@ -2,9 +2,7 @@
# distributed under the terms of the GNU General Public License
from __future__ import absolute_import

import ROOT

from .. import QROOT, asrootpy
from .. import ROOT, QROOT, asrootpy
from ..base import Object
from .utils import canvases_with

Expand Down Expand Up @@ -66,7 +64,7 @@ def y2(self, value):

@property
def x1_pixels(self):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before setting position in pixels")
Expand All @@ -75,7 +73,7 @@ def x1_pixels(self):

@property
def x2_pixels(self):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before setting position in pixels")
Expand All @@ -84,7 +82,7 @@ def x2_pixels(self):

@property
def y1_pixels(self):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before setting position in pixels")
Expand All @@ -93,7 +91,7 @@ def y1_pixels(self):

@property
def y2_pixels(self):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before setting position in pixels")
Expand All @@ -102,7 +100,7 @@ def y2_pixels(self):

@x1_pixels.setter
def x1_pixels(self, value):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before setting position in pixels")
Expand All @@ -111,7 +109,7 @@ def x1_pixels(self, value):

@x2_pixels.setter
def x2_pixels(self, value):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before setting position in pixels")
Expand All @@ -120,7 +118,7 @@ def x2_pixels(self, value):

@y1_pixels.setter
def y1_pixels(self, value):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before setting position in pixels")
Expand All @@ -129,7 +127,7 @@ def y1_pixels(self, value):

@y2_pixels.setter
def y2_pixels(self, value):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before setting position in pixels")
Expand All @@ -154,7 +152,7 @@ def position(self, value):
@property
def position_pixels(self):
x1, y1, x2, y2 = self.position
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before setting position in pixels")
Expand All @@ -166,7 +164,7 @@ def position_pixels(self):
@position_pixels.setter
def position_pixels(self, value):
x1, y1, x2, y2 = value
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before getting position in pixels")
Expand Down Expand Up @@ -199,23 +197,23 @@ def width(self, value):

@property
def height_pixels(self):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before getting position in pixels")
return int(self.height * pad.height_pixels)

@property
def width_pixels(self):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before getting position in pixels")
return int(self.width * pad.width_pixels)

@height_pixels.setter
def height_pixels(self, value):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before getting position in pixels")
Expand All @@ -226,7 +224,7 @@ def height_pixels(self, value):

@width_pixels.setter
def width_pixels(self, value):
pad = asrootpy(ROOT.gPad.func())
pad = ROOT.gPad
if not pad:
raise RuntimeError(
"create a pad before getting position in pixels")
Expand Down
8 changes: 3 additions & 5 deletions rootpy/plotting/canvas.py
Expand Up @@ -6,13 +6,11 @@
"""
from __future__ import absolute_import

import ROOT

from .. import ROOT, QROOT, asrootpy
from .base import convert_color
from ..base import NamedObject
from ..context import invisible_canvas
from ..decorators import snake_case_methods
from .. import QROOT, asrootpy
from ..memory.keepalive import keepalive

from array import array
Expand Down Expand Up @@ -164,15 +162,15 @@ def range_axis(self, bounds):
super(_PadBase, self).RangeAxis(x1, y1, x2, y2)

def __enter__(self):
self._prev_pad = ROOT.gPad.func()
self._prev_pad = ROOT.gPad
self.cd()
return self

def __exit__(self, type, value, traceback):
# similar to preserve_current_canvas in rootpy/context.py
if self._prev_pad:
self._prev_pad.cd()
elif ROOT.gPad.func():
elif ROOT.gPad:
# Put things back how they were before.
with invisible_canvas():
# This is a round-about way of resetting gPad to None.
Expand Down
2 changes: 1 addition & 1 deletion rootpy/plotting/legend.py
Expand Up @@ -30,7 +30,7 @@ def __init__(self, entries,
textsize=None,
header=None):
if pad is None:
pad = ROOT.gPad.func()
pad = ROOT.gPad
if not pad:
raise RuntimeError("create a pad before a legend")

Expand Down
5 changes: 2 additions & 3 deletions rootpy/plotting/style/atlas/labels.py
Expand Up @@ -2,8 +2,7 @@
# distributed under the terms of the GNU General Public License
from __future__ import absolute_import

import ROOT

from .... import ROOT
from ....context import preserve_current_canvas
from ....memory.keepalive import keepalive

Expand All @@ -18,7 +17,7 @@ def ATLAS_label(x, y, text="Preliminary 20XX", sqrts=8,
textsize=20, sep=None):

if pad is None:
pad = ROOT.gPad.func()
pad = ROOT.gPad
with preserve_current_canvas():
pad.cd()
l = ROOT.TLatex(x, y, "ATLAS")
Expand Down
5 changes: 2 additions & 3 deletions rootpy/plotting/style/cmstdr/labels.py
Expand Up @@ -5,8 +5,7 @@
"""
from __future__ import absolute_import

import ROOT

from .... import ROOT
from ....context import preserve_current_canvas
from ....memory.keepalive import keepalive

Expand All @@ -23,7 +22,7 @@ def CMS_label(text="Preliminary 2012", sqrts=8, pad=None):
will be drawn in the upper right.
"""
if pad is None:
pad = ROOT.gPad.func()
pad = ROOT.gPad
with preserve_current_canvas():
pad.cd()
left_margin = pad.GetLeftMargin()
Expand Down

0 comments on commit ee9ac62

Please sign in to comment.