Пример #1
0
def test_keepalive():
    class went_away:
        value = False

    def callback(*args):
        went_away.value = True

    with invisible_canvas() as c:
        c.cd()

        # No primitives to start with
        assert c.GetListOfPrimitives().GetSize() == 0

        h = R.TH1F()
        h.Draw()

        hproxy = weakref.proxy(h, callback)

        # Now we've got one primitive on the canvas
        assert c.GetListOfPrimitives().GetSize() == 1

        del h
        gc.collect()
        # We should still have it due to the keepalive
        assert c.GetListOfPrimitives().GetSize() == 1

    # Canvas should now have gone away
    assert not c

    # And so should the histogram object
    assert went_away.value
Пример #2
0
def test_keepalive():
    class went_away:
        value = False

    def callback(*args):
        went_away.value = True

    with invisible_canvas() as c:
        c.cd()

        # No primitives to start with
        assert c.GetListOfPrimitives().GetSize() == 0

        h = R.TH1F()
        h.Draw()

        hproxy = weakref.proxy(h, callback)

        # Now we've got one primitive on the canvas
        assert c.GetListOfPrimitives().GetSize() == 1

        del h
        gc.collect()
        # We should still have it due to the keepalive
        assert c.GetListOfPrimitives().GetSize() == 1

    # Canvas should now have gone away
    assert not c

    # And so should the histogram object
    assert went_away.value
Пример #3
0
def test_init():

    with invisible_canvas():
        l = Legend(2)
        h = Hist(10, 0, 1)
        l.AddEntry(h)
        hr = TH1D("test", "", 10, 0, 1)
        l.AddEntry(hr)
Пример #4
0
def test_canvas_divide():
    monitor, is_alive = monitor_deletion()

    with invisible_canvas() as c:
        monitor(c, "c")

        c.Divide(2)

        p = c.cd(1)

        monitor(p, "p")
        assert is_alive("p")

        h = R.TH1F()
        h.Draw()
        monitor(h, "h")

        assert is_alive("h")
        del h
        assert is_alive("h")

        del p
        # p should be kept alive because of the canvas
        assert is_alive("p")
        # h should still be alive because of the pad
        assert is_alive("h")

        c.Clear()

        # clearing the canvas means that the pad (and therefore the hist) should
        # be deleted.
        assert not is_alive("p")
        assert not is_alive("h")

        # -------------
        # Next test, check that when the canvas is deleted, everything goes away

        p = c.cd(2)
        h = R.TH1F()
        h.Draw()

        monitor(p, "p")
        monitor(p, "h")

        del p
        del h

        assert is_alive("p")
        assert is_alive("h")

    # The canvas is deleted by exiting the with statement.
    # Everything should go away.
    assert not is_alive("c")
    assert not is_alive("p")
    assert not is_alive("h")
Пример #5
0
def test_canvas_divide():
    monitor, is_alive = monitor_deletion()

    with invisible_canvas() as c:
        monitor(c, "c")

        c.Divide(2)

        p = c.cd(1)

        monitor(p, "p")
        assert is_alive("p")

        h = R.TH1F()
        h.Draw()
        monitor(h, "h")

        assert is_alive("h")
        del h
        assert is_alive("h")

        del p
        # p should be kept alive because of the canvas
        assert is_alive("p")
        # h should still be alive because of the pad
        assert is_alive("h")

        c.Clear()

        # clearing the canvas means that the pad (and therefore the hist) should
        # be deleted.
        assert not is_alive("p")
        assert not is_alive("h")

        # -------------
        # Next test, check that when the canvas is deleted, everything goes away

        p = c.cd(2)
        h = R.TH1F()
        h.Draw()

        monitor(p, "p")
        monitor(p, "h")

        del p
        del h

        assert is_alive("p")
        assert is_alive("h")

    # The canvas is deleted by exiting the with statement.
    # Everything should go away.
    assert not is_alive("c")
    assert not is_alive("p")
    assert not is_alive("h")
Пример #6
0
def test_keepalive_canvas():

    gc.collect()
    assert list(R.gROOT.GetListOfFiles()) == [], "There exist open ROOT files when there should not be"

    with invisible_canvas() as c:
        get_file().Get("means/hist1").Draw()
        gc.collect()
        assert list(R.gROOT.GetListOfFiles()) != [], "Expected an open ROOT file.."

    gc.collect()
    assert list(R.gROOT.GetListOfFiles()) == [], "There exist open ROOT files when there should not be"
Пример #7
0
def test_keepalive_canvas():

    gc.collect()
    assert list(R.gROOT.GetListOfFiles()) == [], "There exist open ROOT files when there should not be"

    with invisible_canvas() as c:
        get_file().Get("means/hist1").Draw()
        gc.collect()
        assert list(R.gROOT.GetListOfFiles()) != [], "Expected an open ROOT file.."

    gc.collect()
    assert list(R.gROOT.GetListOfFiles()) == [], "There exist open ROOT files when there should not be"
Пример #8
0
def test_hooks():
    h = ROOT.TH1D()

    newtitle = "Hello, world"
    assert h.SetTitle(newtitle) == "SUCCESS"
    assert h.GetTitle() == newtitle

    with invisible_canvas() as c:
        assert c.GetListOfPrimitives().GetSize() == 0
        assert h._rootpy_test_super_draw() == "SUCCESS"
        assert c.GetListOfPrimitives().GetSize() == 1

    assert h._rootpy_hook_test_prop == "SUCCESS"
    assert h._rootpy_hook_test_method() == "SUCCESS"
    assert h._rootpy_hook_test_static() == "SUCCESS"
    assert h._rootpy_hook_test_clsmeth() == "SUCCESS"
Пример #9
0
def test_hooks():
    h = ROOT.TH1D()

    newtitle = "Hello, world"
    assert h.SetTitle(newtitle) == "SUCCESS"
    assert h.GetTitle() == newtitle

    with invisible_canvas() as c:
        assert c.GetListOfPrimitives().GetSize() == 0
        assert h._rootpy_test_super_draw() == "SUCCESS"
        assert c.GetListOfPrimitives().GetSize() == 1

    assert h._rootpy_hook_test_prop == "SUCCESS"
    assert h._rootpy_hook_test_method() == "SUCCESS"
    assert h._rootpy_hook_test_static() == "SUCCESS"
    assert h._rootpy_hook_test_clsmeth() == "SUCCESS"
Пример #10
0
def test_nokeepalive():
    with invisible_canvas() as c:

        assert c.GetListOfPrimitives().GetSize() == 0

        h = R.TH1F()
        h.Draw()

        assert c.GetListOfPrimitives().GetSize() == 1
        del h
        from rootpy.memory import KEEPALIVE
        KEEPALIVE.clear()

        # ROOT automatically cleans things up like this on deletion, and since
        # we cleared the keepalive dictionary, they should have gone away.
        assert c.GetListOfPrimitives().GetSize() == 0
Пример #11
0
def test_nokeepalive():
    with invisible_canvas() as c:

        assert c.GetListOfPrimitives().GetSize() == 0

        h = R.TH1F()
        h.Draw()

        assert c.GetListOfPrimitives().GetSize() == 1
        del h
        from rootpy.memory import KEEPALIVE
        KEEPALIVE.clear()

        # ROOT automatically cleans things up like this on deletion, and since
        # we cleared the keepalive dictionary, they should have gone away.
        assert c.GetListOfPrimitives().GetSize() == 0
Пример #12
0
def draw_contours(hist,
                  n_contours=3,
                  contours=None,
                  linecolors=None,
                  linestyles=None,
                  linewidths=None,
                  labelcontours=True,
                  labelcolors=None,
                  labelsizes=None,
                  labelformats=None,
                  same=False,
                  min_points=5):
    if contours is None:
        contours = np.linspace(hist.min(),
                               hist.max(),
                               n_contours + 1,
                               endpoint=False)[1:]
    hist = hist.Clone()
    hist.SetContour(len(contours), np.asarray(contours, dtype=float))
    graphs = []
    levels = []
    with invisible_canvas() as c:
        hist.Draw('CONT LIST')
        c.Update()
        conts = asrootpy(ROOT.gROOT.GetListOfSpecials().FindObject('contours'))
        for i, cont in enumerate(conts):
            for curve in cont:
                if len(curve) < min_points:
                    continue
                graphs.append(curve.Clone())
                levels.append(contours[i])
    if not same:
        axes = hist.Clone()
        axes.Draw('AXIS')
    if linecolors is None:
        linecolors = ['black']
    elif not isinstance(linecolors, list):
        linecolors = [linecolors]
    if linestyles is None:
        linestyles = linestyles_text2root.keys()
    elif not isinstance(linestyles, list):
        linestyles = [linestyles]
    if linewidths is None:
        linewidths = [1]
    elif not isinstance(linewidths, list):
        linewidths = [linewidths]
    if labelsizes is not None:
        if not isinstance(labelsizes, list):
            labelsizes = [labelsizes]
        labelsizes = cycle(labelsizes)
    if labelcolors is not None:
        if not isinstance(labelcolors, list):
            labelcolors = [labelcolors]
        labelcolors = cycle(labelcolors)
    if labelformats is None:
        labelformats = ['%0.2g']
    elif not isinstance(labelformats, list):
        labelformats = [labelformats]
    linecolors = cycle(linecolors)
    linestyles = cycle(linestyles)
    linewidths = cycle(linewidths)
    labelformats = cycle(labelformats)
    label = ROOT.TLatex()
    xmin, xmax = hist.bounds(axis=0)
    ymin, ymax = hist.bounds(axis=1)
    stepx = (xmax - xmin) / 100
    stepy = (ymax - ymin) / 100
    for level, graph in zip(levels, graphs):
        graph.linecolor = linecolors.next()
        graph.linewidth = linewidths.next()
        graph.linestyle = linestyles.next()
        graph.Draw('C')
        if labelcontours:
            if labelsizes is not None:
                label.SetTextSize(labelsizes.next())
            if labelcolors is not None:
                label.SetTextColor(convert_color(labelcolors.next(), 'ROOT'))
            point_idx = len(graph) / 2
            point = graph[point_idx]
            padx, pady = 0, 0
            if len(graph) > 5:
                # use derivative to get text angle
                x1, y1 = graph[point_idx - 2]
                x2, y2 = graph[point_idx + 2]
                dx = (x2 - x1) / stepx
                dy = (y2 - y1) / stepy
                if dx == 0:
                    label.SetTextAngle(0)
                    label.SetTextAlign(12)
                else:
                    padx = copysign(1, -dy) * stepx
                    pady = copysign(1, dx) * stepy
                    if pady < 0:
                        align = 23
                    else:
                        align = 21
                    angle = atan(dy / dx) * 180 / pi
                    label.SetTextAngle(angle)
                    label.SetTextAlign(align)
            else:
                label.SetTextAngle(0)
                label.SetTextAlign(21)
            label.DrawLatex(point[0] + padx, point[1] + pady,
                            (labelformats.next()) % level)
Пример #13
0
def draw_contours(hist, n_contours=3, contours=None,
                  linecolors=None, linestyles=None, linewidths=None,
                  labelcontours=True, labelcolors=None,
                  labelsizes=None, labelformats=None, same=False,
                  min_points=5):
    if contours is None:
        contours = np.linspace(hist.min(), hist.max(), n_contours + 1,
                               endpoint=False)[1:]
    hist = hist.Clone()
    hist.SetContour(len(contours), np.asarray(contours, dtype=float))
    graphs = []
    levels = []
    with invisible_canvas() as c:
        hist.Draw('CONT LIST')
        c.Update()
        conts = asrootpy(ROOT.gROOT.GetListOfSpecials().FindObject('contours'))
        for i, cont in enumerate(conts):
            for curve in cont:
                if len(curve) < min_points:
                    continue
                graphs.append(curve.Clone())
                levels.append(contours[i])
    if not same:
        axes = hist.Clone()
        axes.Draw('AXIS')
    if linecolors is None:
        linecolors = ['black']
    elif not isinstance(linecolors, list):
        linecolors = [linecolors]
    if linestyles is None:
        linestyles = linestyles_text2root.keys()
    elif not isinstance(linestyles, list):
        linestyles = [linestyles]
    if linewidths is None:
        linewidths = [1]
    elif not isinstance(linewidths, list):
        linewidths = [linewidths]
    if labelsizes is not None:
        if not isinstance(labelsizes, list):
            labelsizes = [labelsizes]
        labelsizes = cycle(labelsizes)
    if labelcolors is not None:
        if not isinstance(labelcolors, list):
            labelcolors = [labelcolors]
        labelcolors = cycle(labelcolors)
    if labelformats is None:
        labelformats = ['%0.2g']
    elif not isinstance(labelformats, list):
        labelformats = [labelformats]
    linecolors = cycle(linecolors)
    linestyles = cycle(linestyles)
    linewidths = cycle(linewidths)
    labelformats = cycle(labelformats)
    label = ROOT.TLatex()
    xmin, xmax = hist.bounds(axis=0)
    ymin, ymax = hist.bounds(axis=1)
    stepx = (xmax - xmin) / 100
    stepy = (ymax - ymin) / 100
    for level, graph in zip(levels, graphs):
        graph.linecolor = linecolors.next()
        graph.linewidth = linewidths.next()
        graph.linestyle = linestyles.next()
        graph.Draw('C')
        if labelcontours:
            if labelsizes is not None:
                label.SetTextSize(labelsizes.next())
            if labelcolors is not None:
                label.SetTextColor(convert_color(labelcolors.next(), 'ROOT'))
            point_idx = len(graph) / 2
            point = graph[point_idx]
            padx, pady = 0, 0
            if len(graph) > 5:
                # use derivative to get text angle
                x1, y1 = graph[point_idx - 2]
                x2, y2 = graph[point_idx + 2]
                dx = (x2 - x1) / stepx
                dy = (y2 - y1) / stepy
                if dx == 0:
                    label.SetTextAngle(0)
                    label.SetTextAlign(12)
                else:
                    padx = copysign(1, -dy) * stepx
                    pady = copysign(1, dx) * stepy
                    if pady < 0:
                        align = 23
                    else:
                        align = 21
                    angle = atan(dy / dx) * 180 / pi
                    label.SetTextAngle(angle)
                    label.SetTextAlign(align)
            else:
                label.SetTextAngle(0)
                label.SetTextAlign(21)
            label.DrawLatex(point[0] + padx, point[1] + pady,
                            (labelformats.next()) % level)