Exemple #1
0
    def __init__(self, name, *args, **kwargs):
        r"""Initialize a 2-dimensional histograms.

        Create an instance of :class:`.Histo2D` with the specified **name** and binning
        (either with uniform or vairable bin widths). Can also be used to copy another
        histogram (or upgrade from a :class:`ROOT.TH2`).

        :param name: name of the histogram
        :type name: ``str``

        :param \*args: see below

        :param \**kwargs: :class:`.Histo2D` properties

        :Arguments:
            Depending on the number of arguments (besides **name**) there are three ways
            to initialize a :class:`.Histo2D` object\:

            * *one* argument\:

                #. **histo** (``Histo2D``, ``TH2``) -- histogram to be copied

            * *three* arguments\:

                #. **title** (``str``) -- histogram title that will be used by the
                   :class:`.Legend` class

                #. **xlowbinedges** (``list``, ``tuple``) -- list of lower bin-edges on
                   the x-axis (for a histogram with variable bin widths)

                #. **ylowbinedges** (``list``, ``tuple``) -- list of lower bin-edges on
                   the y-axis (for a histogram with variable bin widths)

            * *five* arguments (variable x-axis binning)\:

                #. **title** (``str``) -- histogram title that will be used by the
                   :class:`.Legend` class

                #. **xlowbinedges** (``list``, ``tuple``) -- list of lower bin-edges on
                   the x-axis (for a histogram with variable bin widths)

                #. **nbinsy** (``int``) -- number of bins on the y-axis (for a histogram
                   with equal widths)

                #. **ymin** (``float``) -- minimum y-axis value (lower bin-edge of first
                   bin)

                #. **ymax** (``float``) -- maximal y-axis value (upper bin-edge of last
                   bin)

            * *five* arguments (variable y-axis binning)\:

                #. **title** (``str``) -- histogram title that will be used by the
                   :class:`.Legend` class

                #. **nbinsx** (``int``) -- number of bins on the x-axis (for a histogram
                   with equal widths)

                #. **xmin** (``float``) -- minimum x-axis value (lower bin-edge of first
                   bin)

                #. **xmax** (``float``) -- maximal x-axis value (upper bin-edge of last
                   bin)

                #. **ylowbinedges** (``list``, ``tuple``) -- list of lower bin-edges on
                   the y-axis (for a histogram with variable bin widths)

            * *seven* arguments\:

                #. **title** (``str``) -- histogram title that will be used by the
                   :class:`.Legend` class

                #. **nbinsx** (``int``) -- number of bins on the x-axis (for a histogram
                   with equal widths)

                #. **xmin** (``float``) -- minimum x-axis value (lower bin-edge of first
                   bin)

                #. **xmax** (``float``) -- maximal x-axis value (upper bin-edge of last
                   bin)

                #. **nbinsy** (``int``) -- number of bins on the y-axis (for a histogram
                   with equal widths)

                #. **ymin** (``float``) -- minimum y-axis value (lower bin-edge of first
                   bin)

                #. **ymax** (``float``) -- maximal y-axis value (upper bin-edge of last
                   bin)
        """
        MethodProxy.__init__(self)
        self._varexp = None
        self._cuts = None
        self._weight = None
        self._drawoption = ""
        self._palette = 57  # ROOT default
        self._contours = []
        self._contourproperties = {}
        self._zmin = None
        self._zmax = None  # = max. bin content
        self._zaxisproperties = {}
        if len(args) == 1:
            if args[0].InheritsFrom("TH2"):
                ROOT.TH2D.__init__(self)
                args[0].Copy(self)
                self.SetDirectory(0)
                self.SetName(name)
            if isinstance(args[0], Histo2D):
                self._varexp = args[0]._varexp
                self._cuts = args[0]._cuts
                self._weight = args[0]._cuts
                self._contours = args[0]._contours
                self._contourproperties = args[0]._contourproperties
        elif len(args) == 3:
            assert isinstance(args[0], str)
            assert isinstance(args[1], (list, tuple))
            assert isinstance(args[2], (list, tuple))
            xlowbinedges = array("d", args[1])
            ylowbinedges = array("d", args[2])
            ROOT.TH2D.__init__(
                self,
                name,
                args[0],
                len(xlowbinedges) - 1,
                xlowbinedges,
                len(ylowbinedges) - 1,
                ylowbinedges,
            )
        elif len(args) == 5:
            assert isinstance(args[0], str)
            if isinstance(args[1], (list, tuple)):
                assert isinstance(args[2], int)
                xlowbinedges = array("d", args[1])
                ROOT.TH2D.__init__(
                    self, name, args[0], len(xlowbinedges) - 1, xlowbinedges, *args[2:]
                )
            elif isinstance(args[4], (list, tuple)):
                assert isinstance(args[1], int)
                ylowbinedges = array("d", args[4])
                ROOT.TH2D.__init__(
                    self,
                    name,
                    args[0],
                    args[1],
                    args[2],
                    args[3],
                    len(ylowbinedges) - 1,
                    ylowbinedges,
                )
        elif len(args) == 7:
            assert isinstance(args[0], str)
            assert isinstance(args[1], int)
            assert isinstance(args[4], int)
            ROOT.TH2D.__init__(self, name, *args)
        else:
            raise TypeError
        for key, value in self.GetTemplate(kwargs.get("template", "common")).items():
            kwargs.setdefault(key, value)
        self.DeclareProperties(**kwargs)
        self._xlowbinedges = IOManager._getBinning(self)["xbinning"]
        self._ylowbinedges = IOManager._getBinning(self)["ybinning"]
        self._nbinsx = len(self._xlowbinedges) - 1
        self._nbinsy = len(self._ylowbinedges) - 1
Exemple #2
0
    def __init__(self, name, *args, **kwargs):
        r"""Initialize a 1-dimensional histograms.

        Create an instance of :class:`.Histo1D` with the specified **name** and binning
        (either with uniform or vairable bin widths). Can also be used to copy another
        histogram (or upgrade from a :class:`ROOT.TH1`).

        :param name: name of the histogram
        :type name: ``str``

        :param \*args: see below

        :param \**kwargs: :class:`.Histo1D` properties

        :Arguments:
            Depending on the number of arguments (besides **name**) there are three ways
            to initialize a :class:`.Histo1D` object\:

            * *one* argument\:

                #. **histo** (``Histo1D``, ``TH1``) -- histogram to be copied

            * *two* arguments\:

                #. **title** (``str``) -- histogram title that will be used by the
                   :class:`.Legend` class

                #. **xlowbinedges** (``list``, ``tuple``) -- list of lower bin-edges on
                   the x-axis (for a histogram with variable bin widths)

            * *four* arguments\:

                #. **title** (``str``) -- histogram title that will be used by the
                   :class:`.Legend` class

                #. **nbinsx** (``int``) -- number of bins on the x-axis (for a histogram
                   with equal widths)

                #. **xmin** (``float``) -- minimum x-axis value (lower bin-edge of first
                   bin)

                #. **xmax** (``float``) -- maximal x-axis value (upper bin-edge of last
                   bin)
        """
        MethodProxy.__init__(self)
        self._varexp = None
        self._cuts = None
        self._weight = None
        self._errorband = None
        self._drawoption = ""
        self._drawerrorband = False
        self._addtolegend = True
        self._legenddrawoption = ""
        self._stack = False  # Stack property!
        self._attalpha = defaultdict(lambda: 1.0)
        self._includeoverflow = False
        self._includeunderflow = False
        if len(args) == 1:
            if args[0].InheritsFrom("TH1"):
                ROOT.TH1D.__init__(self)
                args[0].Copy(self)
                self.SetDirectory(0)
                self.SetName(name)
            if isinstance(args[0], Histo1D):
                self._varexp = args[0]._varexp
                self._cuts = args[0]._cuts
                self._weight = args[0]._cuts
                self._stack = args[0]._stack
                if args[0]._errorband is not None:
                    self._errorband = Histo1D("{}_errorband".format(name),
                                              args[0]._errorband)
                if not name.endswith("_errorband"):
                    self.DeclareProperties(**args[0].GetProperties())
                    self.DeclareProperties(**args[0]._errorband.GetProperties(
                        prefix="errorband"))
        elif len(args) == 2:
            assert isinstance(args[0], str)
            assert isinstance(args[1], (list, tuple))
            lowbinedges = array("d", args[1])
            ROOT.TH1D.__init__(self, name, args[0],
                               len(lowbinedges) - 1, lowbinedges)
        elif len(args) == 4:
            assert isinstance(args[0], str)
            assert isinstance(args[1], int)
            ROOT.TH1D.__init__(self, name, *args)
        else:
            raise TypeError
        if not name.endswith("_errorband") and self._errorband is None:
            self._errorband = Histo1D("{}_errorband".format(self.GetName()),
                                      self)
            for key, value in self.GetTemplate(kwargs.get(
                    "template", "common")).items():
                kwargs.setdefault(key, value)
        self.DeclareProperties(**kwargs)
        self._lowbinedges = IOManager._getBinning(self)["xbinning"]
        self._nbins = len(self._lowbinedges) - 1