示例#1
0
文件: vector.py 项目: cdeil/rootpy
    def __mul__(self, other):

        if isinstance(other, self.__class__):
            prod = self.X() * other.X() + \
                   self.Y() * other.Y()
        elif isbasictype(other):
            prod = Vector2(other * self.X(), other * self.Y())
        else:
            raise TypeError("unsupported operand type(s) for *: '%s' and '%s'" %
                (self.__class__.__name__, other.__class__.__name__))
        return prod
示例#2
0
def draw(
    objects,
    pad=None,
    title=None,
    axislabels=None,
    legend=None,
    showlegend=True,
    greedylegend=False,
    textlabels=None,
    xscale="linear",
    yscale="linear",
    style2d="col",
    style3d="surf1",
    maxmin=(),
    minmax=None,
    minimum=0,
    maximum=None,
    use_global_margins=True,
):

    if type(objects) not in [list, tuple]:
        objects = [objects]

    objects = [hist.Clone() for hist in objects]

    dimension = None
    for thing in objects:
        if dimension is None:
            dimension = dim(thing)
        elif dim(thing) != dimension:
            raise TypeError("dimensions of histograms must all be the same")

    if axislabels is not None:
        if type(axislabels) not in [list, tuple]:
            axislabels = [axislabels]
    else:
        axislabels = []

    if textlabels is not None:
        if type(textlabels) not in [list, tuple]:
            textlabels = [textlabels]
    else:
        textlabels = []

    if pad is None:
        pad = Canvas()
    else:
        pad = asrootpy(pad)

    pad.Clear()
    pad.cd()

    if yscale == "log":
        pad.SetLogy(True)
    else:
        pad.SetLogy(False)
    if xscale == "log":
        pad.SetLogx(True)
    else:
        pad.SetLogx(False)

    if use_global_margins:
        pad.SetTopMargin(ROOT.gStyle.GetPadTopMargin())
        pad.SetRightMargin(ROOT.gStyle.GetPadRightMargin())
        pad.SetBottomMargin(ROOT.gStyle.GetPadBottomMargin())
        pad.SetLeftMargin(ROOT.gStyle.GetPadLeftMargin())

    if title:
        pad.SetTopMargin(0.1)
    else:
        title = ""

    for hist in objects:
        if isinstance(hist, HistStack):
            subobjects = hist
        else:
            subobjects = [hist]
        for subhist in subobjects:
            if "colz" in subhist.format.lower():
                if not title:
                    pad.SetTopMargin(0.06)
                pad.SetRightMargin(0.13)
                break

    nobjects = 0
    for hist in objects:
        if isinstance(hist, HistStack):
            nobjects += len(hist)
        else:
            nobjects += 1

    if not legend and showlegend:
        legend = Legend(nobjects, pad)
        legend.SetTextSize(20)

    for hist in objects:
        if hist.norm:
            if isinstance(hist.norm, _HistBase) or isinstance(hist.norm, HistStack):
                norm = hist.norm.Integral()
                integral = hist.Integral()
            elif type(hist.norm) is str:
                if hist.norm.lower() == "max":
                    norm = 1.0
                    integral = hist.GetMaximum()
                else:
                    raise ValueError("Normalization not understood: %s" % hist.norm)
            elif isbasictype(hist.norm):
                norm = hist.norm
                integral = hist.Integral()
            if integral > 0:
                hist.Scale(norm / integral)

    _max = None  # negative infinity
    _min = ()  # positive infinity
    for hist in objects:
        if dim(hist) == 1:
            lmax = hist.GetMaximum(include_error=True)
            lmin = hist.GetMinimum(include_error=True)
        else:
            lmax = hist.GetMaximum()
            lmin = hist.GetMinimum()
        if lmax > _max:
            _max = lmax
        if lmin < _min and not (yscale == "log" and lmin <= 0.0):
            _min = lmin

    __max, __min = _max, _min

    if maximum != None:
        if maximum > _max:
            _max = maximum
    if minimum != None:
        if minimum < _min and not (yscale == "log" and minimum <= 0.0):
            _min = minimum

    if _min > maxmin:
        _min = maxmin
    if _max < minmax:
        _max = minmax

    if legend and greedylegend:
        padding = 0.05
        plotheight = (1 - pad.GetTopMargin()) - pad.GetBottomMargin()
        legendheight = legend.Height() + padding
        if yscale == "linear":
            _max = (_max - (_min * legendheight / plotheight)) / (1.0 - (legendheight / plotheight))
        else:  # log
            if _max <= 0.0:
                raise ValueError("Attempted to plot log scale where max<=0: %f" % _max)
            if _min <= 0.0:
                raise ValueError("Attempted to plot log scale where min<=0: %f" % _min)
            _max = 10.0 ** (
                (math.log10(_max) - (math.log10(_min) * legendheight / plotheight))
                / (1.0 - (legendheight / plotheight))
            )
    else:
        if yscale == "linear":
            if maximum is None:
                _max += (_max - _min) * 0.1
            if _min != 0:
                _min -= (_max - _min) * 0.1
        else:
            height = math.log10(_max) - math.log10(_min)
            if maximum is None:
                _max *= 10 ** (height * 0.1)
            if _min != 0:
                _min *= 10 ** (height * -0.1)

    format = ""
    if len(axislabels) == 3:
        format += style2d
    elif len(axislabels) == 4:
        format += style3d

    axes_drawn = False
    for index, hist in enumerate(objects):
        if legend:
            legend.AddEntry(hist)
        if index == 0 or not axes_drawn:
            if title:
                hist.SetTitle(title)
            else:
                hist.SetTitle("")
            if isinstance(hist, Graph):
                hist.Draw("AP", format)
            else:
                hist.Draw(format)
            if hist.visible:
                axes_drawn = True
            if axislabels:
                hist.GetXaxis().SetTitle(axislabels[0])
                if len(axislabels) > 1:
                    hist.GetYaxis().SetTitle(axislabels[1])
                if len(axislabels) >= 3:
                    hist.GetZaxis().SetTitle(axislabels[2])
                    if len(axislabels) == 4:
                        hist.SetTitle(axislabels[3])
                        hist.GetZaxis().SetTitleOffset(1.8)
            if _max > _min and dimension in (1, 2):
                hist.GetYaxis().SetLimits(_min, _max)
                hist.GetYaxis().SetRangeUser(_min, _max)
            if _max > _min and dimension == 3:
                hist.GetZaxis().SetLimits(_min, _max)
                hist.GetZaxis().SetRangeUser(_min, _max)
            if hist.intmode:
                hist.GetXaxis().SetNdivisions(len(hist), True)
        else:
            hist.SetTitle("")
            if isinstance(hist, Graph):
                hist.Draw("P same", format)
            else:
                hist.Draw("same", format)

    if legend:
        legend.Draw()

    for label in textlabels:
        label.Draw()

    """
    for item in pad.GetListOfPrimitives():
        if isinstance(item, ROOT.TPaveText):
            text = item.GetLine(0)
            text.SetTextFont(63)
            text.SetTextSizePixels(20)
    """
    pad.OwnMembers()
    pad.Modified()
    pad.Update()
    return pad, __max, __min