Example #1
0
    def array2data2D(self,
                     data,
                     weights=None,
                     normed=False,
                     binning=1,
                     reBin=None):
        """
        Convert array of data to internal format
        - Designed for arrays of raw, un-binned data.
        - If you pass values here from an existing histogram ('weights' is not None
          and the 'data' param is just bin centers), it is possible to re-bin
          this histogram using the 'reBin' keyword
        """
        try:
            x = data['x']
            y = data['y']
        except TypeError:
            x = data[0]
            y = data[1]

        data, bins_x, bins_y = np.histogram2d(x,
                                              y,
                                              bins=binning,
                                              normed=normed,
                                              weights=weights)

        results = Hist()
        results.content = data.flatten()  # data is a ndarray (nxbins,nybins)
        results.bins = {'x': bins_x, 'y': bins_y}

        xcenter, ycenter = tools.dummy_bins2D(tools.midpoints(bins_x),
                                              tools.midpoints(bins_y))
        xwidth, ywidth = tools.dummy_bins2D(tools.widths(bins_x),
                                            tools.widths(bins_y))

        results.center = {'x': xcenter, 'y': ycenter}
        results.width = {'x': xwidth, 'y': ywidth}

        results.error = np.sqrt(data)
        if weights is not None:
            # scipy.stats to get sumw2 (x,y,weights should have the same shape)
            results.error = results.sumw2_2D(xdata=x, ydata=y, values=weights)

        if reBin is not None:
            # re-binning after making data from array, likely that the user
            # passed in binned data and wants to re-bin.
            results.Rebin2D(reBin)
            if normed: results.normalize()  # normalize after re-binning

        results.xbins = results.bins['x']
        results.ybins = results.bins['y']
        results.xcenter = results.center['x']
        results.ycenter = results.center['y']
        results.xwidth = results.width['x']
        results.ywidth = results.width['y']

        return results


## THE END ##
Example #2
0
    def hist2data2D(self, histo, reBin=None, normed=False):
        """Convert ROOT histogram for internal use."""
        bin_contents, (xbin_edges, ybin_edges) = histo.numpy()
        bin_contents = bin_contents.T

        if len(histo.allvariances) > 0:
            bin_errors = histo.allvariances[
                1:-1, 1:
                -1]  # variances() doesn't produce correct values in 2D right now
        else:
            bin_errors = np.sqrt(bin_contents)

        xbin_centers, ybin_centers = tools.dummy_bins2D(
            tools.midpoints(xbin_edges), tools.midpoints(ybin_edges))
        xbin_widths, ybin_widths = tools.dummy_bins2D(tools.widths(xbin_edges),
                                                      tools.widths(ybin_edges))

        results = Hist()
        results.content = bin_contents.flatten()
        results.error = bin_errors.flatten()
        results.bins = {'x': xbin_edges, 'y': ybin_edges}
        results.center = {'x': xbin_centers, 'y': ybin_centers}
        results.width = {'x': xbin_widths, 'y': ybin_widths}

        if reBin is not None:
            results.Rebin2D(reBin)
        if normed: results.normalize()

        # Set extra attributes (placeholders for the moment)
        results.xbins = results.bins['x']
        results.ybins = results.bins['y']
        results.xcenter = results.center['x']
        results.ycenter = results.center['y']
        results.xwidth = results.width['x']
        results.ywidth = results.width['y']

        return results


## THE END ##
Example #3
0
    def hist2data2D(self, histogram, normed=False, reBin=None):
        """Convert ROOT histogram for internal use."""
        results = Hist()

        histo = histogram.Clone()
        if not histo.GetSumw2N():
            histo.Sumw2()

        if reBin is not None:
            # Use ROOT functionality to re-bin histogram
            if isinstance(reBin, (int, long)):
                histo.Rebin2D(reBin, reBin)
            else:
                newname = histo.GetName() + "_"
                old_histo = histo.Clone()  # special rebinning, redefine histo
                try:
                    new_x = reBin['x']
                    new_y = reBin['y']
                except TypeError:
                    new_x = reBin[0]
                    new_y = reBin[1]
                histo = ROOT.TH2F(old_histo.GetName() + newname,
                                  old_histo.GetTitle() + newname,
                                  len(new_x) - 1, new_x,
                                  len(new_y) - 1, new_y)
                xaxis = old_histo.GetXaxis()
                yaxis = old_histo.GetYaxis()
                for i in xrange(1, xaxis.GetNbins()):
                    for j in xrange(1, yaxis.GetNbins()):
                        histo.Fill(xaxis.GetBinCenter(i),
                                   yaxis.GetBinCenter(j),
                                   old_histo.GetBinContent(i, j))

        if normed:
            integral = histo.Integral()
            histo.Scale(1. / integral)

        bin_centers = {'x': [], 'y': []}
        bin_contents = []
        bin_errors = []
        bin_widths = {'x': [], 'y': []}
        bin_edges    = {'x':[histo.GetXaxis().GetBinLowEdge(1)],\
                        'y':[histo.GetYaxis().GetBinLowEdge(1)]}
        bin_edges['x'] += [
            histo.GetXaxis().GetBinUpEdge(i)
            for i in xrange(1,
                            histo.GetNbinsX() + 1)
        ]
        bin_edges['y'] += [
            histo.GetYaxis().GetBinUpEdge(j)
            for j in xrange(1,
                            histo.GetNbinsY() + 1)
        ]

        for i in xrange(1, histo.GetNbinsX() + 1):
            for j in xrange(1, histo.GetNbinsY() + 1):
                bin_contents.append(histo.GetBinContent(i, j))
                bin_errors.append(histo.GetBinError(i, j))
                bin_centers['x'].append(histo.GetXaxis().GetBinCenter(i))
                bin_centers['y'].append(histo.GetYaxis().GetBinCenter(j))
                bin_widths['x'].append(histo.GetXaxis().GetBinWidth(i) * 0.5)
                bin_widths['y'].append(histo.GetYaxis().GetBinWidth(i) * 0.5)

        results.content = np.array(bin_contents)
        results.error = np.array(bin_errors)

        results.xbins = np.array(bin_edges['x'])
        results.ybins = np.array(bin_edges['y'])
        results.xcenter = bin_centers['x']
        results.ycenter = bin_centers['y']
        results.xwidth = bin_widths['x']
        results.ywidth = bin_widths['y']

        results.bins = {'x': results.xbins, 'y': results.ybins}
        results.center = bin_centers
        results.width = bin_widths

        return results