Пример #1
0
    def test_1d(self):
        bins = 1.0 * np.array([0, 3, 6, 9, 12, 15])
        vals = 1.0 * np.array([1, 2, 3, 4, 5, 10, 13])
        weights = 1.0 * np.array([1, 1, 1, 2, 2, 1, 1])
        hr_ = r.TH1F("hr", "hr", len(bins) - 1, bins)
        fill_fast(hr_, vals, weights=weights)
        hr = Hist1D(hr_)
        hn = Hist1D(vals, bins=bins, weights=weights)

        self.assertEqual(hn, hr)

        self.assertEqual(hn.get_integral(), np.sum(weights))
        self.assertEqual(hr.get_integral(), np.sum(weights))

        self.assertEqual(np.all(hn.edges == bins), True)
        self.assertEqual(np.all(hr.edges == bins), True)

        check = np.histogram(vals, bins=bins, weights=weights)[0]
        self.assertEqual(np.all(hn.counts == check), True)
        self.assertEqual(np.all(hr.counts == check), True)

        self.assertEqual(Hist1D(hr_ * 2), hn * 2)
        self.assertEqual(Hist1D(hr_ + hr_), hn + hn)

        self.assertEqual(Hist1D(hr_ + 0.5 * hr_), hn + 0.5 * hn)
Пример #2
0
 def test_1d_summing_weights(self):
     bins = 1.0*np.array([0,3,6,9,12,15])
     vals1 = 1.0*np.array([4,1,2,3,4,5,10,13])
     weights1 = 1.0*np.array([-1,1,1,1,2,2,1,1])
     vals2 = 1.0*np.array([4,0,2,3,4,-5,100,13])
     weights2 = 1.0*np.array([-1,2,-1,1,2,2,-1,1])
     hr1_ = r.TH1F("hr1","hr1", len(bins)-1, bins)
     hr2_ = r.TH1F("hr2","hr2", len(bins)-1, bins)
     fill_fast(hr1_, vals1, weights=weights1)
     fill_fast(hr2_, vals2, weights=weights2)
     hr1 = Hist1D(hr1_, no_overflow=True)
     hr2 = Hist1D(hr2_, no_overflow=True)
     hn1 = Hist1D(vals1,bins=bins, weights=weights1)
     hn2 = Hist1D(vals2,bins=bins, weights=weights2)
     self.assertEqual(hr1+hr2, hn1+hn2)
Пример #3
0
    def test_2d(self):
        vals2d = 1.0 * np.array([
            [1, 1],
            [1, 3],
            [1, 4],
            [1, 4],
            [3, 1],
            [3, 4],
        ])
        bins = [
            np.linspace(0., 4., 3),  # edges 0.0,2.0,4.0
            np.linspace(0., 5., 3),  # edges 0.0,2.5,5.0
        ]
        weights = 1.0 * np.array([1, 1, 2, 3, 1, 4])

        hr_ = r.TH2F("hr2d", "hr2d",
                     len(bins[0]) - 1, bins[0],
                     len(bins[1]) - 1, bins[1])
        fill_fast(hr_, vals2d[:, 0], vals2d[:, 1], weights=weights)
        hr = Hist2D(hr_)

        hn = Hist2D(vals2d, bins=bins, weights=weights)

        self.assertEqual(hn, hr)

        self.assertEqual(hn.get_integral(), hr.get_integral())

        self.assertEqual(np.all(hr.edges[0] == bins[0]), True)
        self.assertEqual(np.all(hr.edges[1] == bins[1]), True)
        self.assertEqual(np.all(hn.edges[0] == bins[0]), True)
        self.assertEqual(np.all(hn.edges[1] == bins[1]), True)

        hr2x_ = hr_.Clone("hr2x")
        hr2x_.Scale(2.0)
        self.assertEqual(Hist2D(hr2x_), hn * 2)

        hr2p_ = hr_.Clone("hr2p")
        hr2p_.Add(hr_)
        self.assertEqual(Hist2D(hr2p_), hn + hn)