예제 #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 ##
예제 #2
0
    def array2data(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
        """
        data, bins = np.histogram(data,
                                  bins=binning,
                                  weights=weights,
                                  normed=normed)

        results = Hist()
        results.content = data
        results.bins = bins
        results.center = tools.midpoints(bins)
        results.width = tools.widths(bins)

        results.error = np.sqrt(data)
        if weights is not None:
            # numpy digitize to get sumw2
            results.error = results.sumw2_1D(xdata=data, values=weights)

        if reBin is not None:
            results.Rebin(reBin)
            if normed: results.normalize()  # normalize after re-binning

        return results
예제 #3
0
    def TEfficiency2data(self, histo):
        """Convert TEfficiency to internal format. No support for re-binning TEfficiencies."""
        h_histo = histo.GetPassedHistogram()

        bin_contents = []
        bin_errors_up = []
        bin_errors_dn = []
        bin_centers = []
        bin_widths = []
        bin_edges = [h_histo.GetXaxis().GetBinLowEdge(1)]

        for i in xrange(1, h_histo.GetNbinsX() + 1):
            bin_contents.append(histo.GetEfficiency(i))
            bin_errors_up.append(histo.GetEfficiencyErrorUp(i))
            bin_errors_dn.append(histo.GetEfficiencyErrorLow(i))
            bin_centers.append(h_histo.GetXaxis().GetBinCenter(i))
            bin_edges.append(h_histo.GetXaxis().GetBinUpEdge(i))
            bin_widths.append(h_histo.GetXaxis().GetBinWidth(1) / 2.)

        results = Hist()
        results.content = np.array(bin_contents)
        results.error = [bin_errors_dn, bin_errors_up]
        results.bins = np.array(bin_edges)
        results.center = bin_centers
        results.width = bin_widths

        return results
예제 #4
0
    def TEfficiency2data2D(self, histo):
        """Convert 2D TEfficiency to internal format. No support for re-binning TEfficiencies."""
        h_histo = histo.GetPassedHistogram()

        bin_centers = {'x': [], 'y': []}
        bin_contents = []
        bin_error_ups = []  # eff uncertainty up
        bin_error_dns = []  # eff uncertainty down
        bin_widths = {'x': [], 'y': []}

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

        for i in xrange(1, h_histo.GetNbinsX() + 1):
            for j in xrange(1, h_histo.GetNbinsY() + 1):
                bin_center['x'].append(h_histo.GetXaxis().GetBinCenter(i))
                bin_center['y'].append(h_histo.GetYaxis().GetBinCenter(j))

                this_bin = histo.GetGlobalBin(i, j)
                bin_contents.append(histo.GetEfficiency(this_bin))
                bin_error_ups.append(histo.GetEfficiencyErrorUp(this_bin))
                bin_error_dns.append(histo.GetEfficiencyErrorLow(this_bin))
                bin_widths['x'].append(h_histo.GetXaxis().GetBinWidth(1) / 2.)
                bin_widths['y'].append(h_histo.GetYaxis().GetBinWidth(1) / 2.)

        results = Hist()
        results.content = np.array(bin_contents)
        results.error = [np.array(bin_error_dns), np.array(bin_error_ups)]
        results.bins = {
            'x': np.array(bin_edges['x']),
            'y': np.array(bin_edges['y'])
        }
        results.center = bin_centers
        results.width = bin_widths

        return results


## THE END ##
예제 #5
0
    def hist2data(self, histo, reBin=None, normed=False):
        """Convert ROOT histogram for internal use."""
        bin_contents, bin_edges = histo.numpy()

        results = Hist()
        results.content = bin_contents
        results.bins = bin_edges
        results.center = tools.midpoints(bin_edges)
        results.width = tools.widths(bin_edges)

        if len(histo.variances) > 0:
            results.error = histo.variances
        else:
            results.error = np.sqrt(bin_contents)

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

        return results
예제 #6
0
    def hist2data(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
            try:
                histo.Rebin(reBin)
            except TypeError:
                newname = histo.GetName() + "_"
                histo.Rebin(len(reBin) - 1, newname, reBin)
                histo = ROOT.gROOT.FindObject(newname)

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

        bin_centers = []
        bin_contents = []
        bin_errors = []
        bin_widths = []
        bin_edges = [histo.GetXaxis().GetBinLowEdge(1)]

        for i in xrange(1, histo.GetNbinsX() + 1):
            bin_edges.append(histo.GetXaxis().GetBinUpEdge(i))
            bin_centers.append(histo.GetBinCenter(i))
            bin_contents.append(histo.GetBinContent(i))
            bin_errors.append(histo.GetBinError(i))
            bin_widths.append(histo.GetXaxis().GetBinWidth(i) * 0.5)

        results.content = np.array(bin_contents)
        results.error = np.array(bin_errors)
        results.bins = np.array(bin_edges)
        results.center = bin_centers
        results.width = bin_widths

        return results
예제 #7
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 ##
예제 #8
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