Exemplo n.º 1
0
    def test_read_hist_2d_asymmetric_errors(self):
        """Test the read_hist_2d function with asymmetric errors
        forcing symmetric errors to be used."""
        # pylint: disable-msg=too-many-locals
        _fpath = "testfile.root"

        # Create test histogram
        NX = 17
        NY = 17
        n_fill = 1000

        hist = ROOT.TH2D("test2d_asym", "test2d_asym", NX, 0, 1, NY, 0, 1)
        hist.SetBinErrorOption(ROOT.TH1.kPoisson)
        for val in np.random.normal(loc=0.5, scale=0.15, size=(n_fill, 2)):
            hist.Fill(*val)

        backup_hist = hist.Clone("backup")

        # Write to file
        testfile = make_tmp_root_file(testcase=self)

        hist.SetDirectory(testfile)
        hist.Write("test2d_asym")
        testfile.Close()

        # Read back
        reader = RootFileReader(testfile.GetName())
        points = reader.read_hist_2d("test2d_asym")

        # Check keys
        self.assertTrue(
            set(["x", "y", "z", "x_edges", "y_edges", "dz"]) == set(
                points.keys()))

        # Check length
        for v in points.values():
            self.assertTrue(len(v) == NX * NY)

        # Look up original bins and compare
        for x, y, z, dz in zip(points["x"], points["y"], points["z"],
                               points["dz"]):
            ibin = backup_hist.Fill(x, y, 0)
            ibinx = ctypes.c_int()
            ibiny = ctypes.c_int()
            ibinz = ctypes.c_int()
            backup_hist.GetBinXYZ(ibin, ibinx, ibiny, ibinz)
            self.assertTrue(
                float_compare(backup_hist.GetXaxis().GetBinCenter(ibinx.value),
                              x))
            self.assertTrue(
                float_compare(backup_hist.GetYaxis().GetBinCenter(ibiny.value),
                              y))
            self.assertTrue(float_compare(backup_hist.GetBinContent(ibin), z))
            self.assertTrue(
                tuple_compare(
                    (-backup_hist.GetBinErrorLow(ibinx.value, ibiny.value),
                     backup_hist.GetBinErrorUp(ibinx.value, ibiny.value)), dz))

        # Clean up
        self.doCleanups()
Exemplo n.º 2
0
    def test_read_hist_2d_symmetric_errors(self):
        """Test the read_hist_2d function with symmetric errors."""
        fpath = "testfile.root"

        # Create test histogram
        NX = 100
        NY = 100
        x_values = [0.5 + x for x in range(NX)]
        y_values = [0.5 + x for x in range(NY)]
        z_values = np.random.uniform(-1e3, 1e3, (NX, NY))
        dz_values = np.random.uniform(0, 1e3, (NX, NY))

        hist = ROOT.TH2D("test2d_sym", "test2d_sym", NX, 0, NX, NY, 0, NY)

        for ix in range(1, hist.GetNbinsX() + 1):
            for iy in range(1, hist.GetNbinsY() + 1):
                ibin = hist.GetBin(ix, iy)
                hist.SetBinContent(ibin, z_values[ix - 1][iy - 1])
                hist.SetBinError(ibin, dz_values[ix - 1][iy - 1])

        backup_hist = hist.Clone("backup")
        rfile = ROOT.TFile(fpath, "RECREATE")
        hist.SetDirectory(rfile)
        hist.Write("test2d_sym")
        rfile.Close()

        reader = RootFileReader(fpath)
        points = reader.read_hist_2d("test2d_sym")

        # Check keys
        self.assertTrue(
            set(["x", "y", "z", "x_edges", "y_edges", "dz"]) == set(
                points.keys()))

        # Check length
        for v in points.values():
            self.assertTrue(len(v) == NX * NY)

        # Check unordered contents
        self.assertTrue(set(points["x"]) == set(x_values))
        self.assertTrue(set(points["y"]) == set(y_values))

        # Look up original bins and compare
        for x, y, z, dz in zip(points["x"], points["y"], points["z"],
                               points["dz"]):
            ibin = backup_hist.Fill(x, y, 0)
            ibinx = ctypes.c_int()
            ibiny = ctypes.c_int()
            ibinz = ctypes.c_int()
            backup_hist.GetBinXYZ(ibin, ibinx, ibiny, ibinz)
            self.assertTrue(
                float_compare(backup_hist.GetXaxis().GetBinCenter(ibinx.value),
                              x))
            self.assertTrue(
                float_compare(backup_hist.GetYaxis().GetBinCenter(ibiny.value),
                              y))
            self.assertTrue(float_compare(backup_hist.GetBinContent(ibin), z))
            self.assertTrue(float_compare(backup_hist.GetBinError(ibin), dz))
        # Clean up
        os.remove(fpath)
Exemplo n.º 3
0
    def test_read_hist_2d_bin_labels(self):
        """Test the read_hist_2d function with bin labels."""
        name = "test"

        # Create test histogram
        NX = 13
        NY = 37
        y_values = np.random.uniform(-1e3, 1e3, (NX, NY))
        xlabels = ["X{0}".format(i) for i in range(NX)]
        ylabels = ["Y{0}".format(i) for i in range(NY)]

        hist = ROOT.TH2D("test2d_labels", "test2d_labels", NX, 0, NX, NY, 0,
                         NY)
        for i in range(NX):
            for j in range(NY):
                hist.Fill(i, j, y_values[i, j])
                hist.GetXaxis().SetBinLabel(i + 1, xlabels[i])
                hist.GetYaxis().SetBinLabel(j + 1, ylabels[j])

        testfile = make_tmp_root_file(testcase=self)
        testfile.cd()
        hist.Write(name)
        testfile.Close()

        reader = RootFileReader(testfile.GetName())
        points = reader.read_hist_2d(name)

        self.assertTrue("x_labels" in points.keys())
        self.assertTrue("y_labels" in points.keys())

        # The output ordering is
        # [(x=0,y=0), (x=0,y=1), ...]
        for i in range(NX):
            for j in range(NY):
                index = i * NY + j
                self.assertTrue(points["x_labels"][index] == xlabels[i])
                self.assertTrue(points["y_labels"][index] == ylabels[j])
        # Clean up
        self.doCleanups()
Exemplo n.º 4
0
    def test_read_hist_2d_range(self):
        """Test the read_hist_2d function with symmetric errors."""
        # pylint: disable-msg=too-many-statements
        # pylint: disable-msg=too-many-locals
        # Create test histogram
        NX = 100
        NY = 100
        xmin = 20
        xmax = 80
        ymin = 30
        ymax = 90
        x_values = [0.5 + x for x in range(xmin, xmax)]
        y_values = [0.5 + x for x in range(ymin, ymax)]
        z_values = np.random.uniform(-1e3, 1e3, (xmax - xmin, ymax - ymin))
        dz_values = np.random.uniform(0, 1e3, (xmax - xmin, ymax - ymin))

        testname = "test2d_sym"
        hist = ROOT.TH2D(testname, testname, NX, 0, NX, NY, 0, NY)

        for ix in range(xmin, xmax):
            for iy in range(ymin, ymax):
                ibin = hist.GetBin(ix + 1, iy + 1)
                hist.SetBinContent(ibin, z_values[ix - xmin][iy - ymin])
                hist.SetBinError(ibin, dz_values[ix - xmin][iy - ymin])

        backup_hist = hist.Clone("backup")

        testfile = make_tmp_root_file(testcase=self)
        hist.SetDirectory(testfile)
        hist.Write("test2d_sym")
        testfile.Close()

        reader = RootFileReader(testfile.GetName())

        # pass too many axis limits
        with self.assertRaises(TypeError):
            reader.read_hist_2d(testname,
                                xlim=(xmin, xmax),
                                ylim=(ymin, ymax),
                                zlim=(ymin, ymax))
        # pass non-existing axis limit/parameter
        with self.assertRaises(TypeError):
            reader.read_hist_2d(testname, zlim=(xmin, xmax))
        # pass wrong order (xmax < xmin)
        with self.assertRaises(AssertionError):
            reader.read_hist_2d(testname, ylim=(xmax, xmin))
        # pass too many parameters as single axis limit
        with self.assertRaises(AssertionError):
            reader.read_hist_2d(testname, ylim=(xmin, xmax, 5))
        # pass wrong type as first item
        with self.assertRaises(AssertionError):
            reader.read_hist_2d(testname, ylim=("5", xmax))
        # pass wrong type as second item
        with self.assertRaises(AssertionError):
            reader.read_hist_2d(testname, ylim=(xmin, "12"))
        # pass set instance (needs ordered datatype: tuple or list)
        with self.assertRaises(AssertionError):
            reader.read_hist_2d(testname, xlim={xmin, xmax})
        # pass wrong instance
        with self.assertRaises(AssertionError):
            reader.read_hist_2d(testname, xlim="some string")

        points = reader.read_hist_2d(testname,
                                     xlim=(xmin, xmax),
                                     ylim=(ymin, ymax))

        # Check keys
        self.assertTrue(
            set(["x", "y", "z", "x_edges", "y_edges", "dz"]) == set(
                points.keys()))

        # Check length
        for v in points.values():
            self.assertTrue(len(v) == (xmax - xmin) * (ymax - ymin))

        # Check unordered contents
        self.assertTrue(set(points["x"]) == set(x_values))
        self.assertTrue(set(points["y"]) == set(y_values))

        # Look up original bins and compare
        for x, y, z, dz in zip(points["x"], points["y"], points["z"],
                               points["dz"]):
            ibin = backup_hist.Fill(x, y, 0)
            ibinx = ctypes.c_int()
            ibiny = ctypes.c_int()
            ibinz = ctypes.c_int()
            backup_hist.GetBinXYZ(ibin, ibinx, ibiny, ibinz)
            self.assertTrue(
                float_compare(backup_hist.GetXaxis().GetBinCenter(ibinx.value),
                              x))
            self.assertTrue(
                float_compare(backup_hist.GetYaxis().GetBinCenter(ibiny.value),
                              y))
            self.assertTrue(float_compare(backup_hist.GetBinContent(ibin), z))
            self.assertTrue(float_compare(backup_hist.GetBinError(ibin), dz))

        # Clean up
        self.doCleanups()
Exemplo n.º 5
0
    def test_read_hist_2d_symmetric_errors(self):
        """Test the read_hist_2d function with symmetric errors."""
        # pylint: disable-msg=too-many-locals
        _fpath = "testfile.root"

        # Create test histogram
        NX = 100
        NY = 100
        x_values = [0.5 + x for x in range(NX)]
        y_values = [0.5 + x for x in range(NY)]
        z_values = np.random.uniform(-1e3, 1e3, (NX, NY))
        dz_values = np.random.uniform(0, 1e3, (NX, NY))

        testname = "test2d_sym"
        hist = ROOT.TH2D(testname, testname, NX, 0, NX, NY, 0, NY)

        for ix in range(1, hist.GetNbinsX() + 1):
            for iy in range(1, hist.GetNbinsY() + 1):
                ibin = hist.GetBin(ix, iy)
                hist.SetBinContent(ibin, z_values[ix - 1][iy - 1])
                hist.SetBinError(ibin, dz_values[ix - 1][iy - 1])

        backup_hist = hist.Clone("backup")

        # Write to file
        testfile = make_tmp_root_file(testcase=self)
        testfile.cd()
        hist.Write(testname)
        testfile.Close()

        # Read back
        reader = RootFileReader(testfile.GetName())
        points = reader.read_hist_2d(testname)

        # Check keys
        self.assertTrue(
            set(["x", "y", "z", "x_edges", "y_edges", "dz"]) == set(
                points.keys()))

        # Check length
        for v in points.values():
            self.assertTrue(len(v) == NX * NY)

        # Check unordered contents
        self.assertTrue(set(points["x"]) == set(x_values))
        self.assertTrue(set(points["y"]) == set(y_values))

        # Look up original bins and compare
        for x, y, z, dz in zip(points["x"], points["y"], points["z"],
                               points["dz"]):
            ibin = backup_hist.Fill(x, y, 0)
            ibinx = ctypes.c_int()
            ibiny = ctypes.c_int()
            ibinz = ctypes.c_int()
            backup_hist.GetBinXYZ(ibin, ibinx, ibiny, ibinz)
            self.assertTrue(
                float_compare(backup_hist.GetXaxis().GetBinCenter(ibinx.value),
                              x))
            self.assertTrue(
                float_compare(backup_hist.GetYaxis().GetBinCenter(ibiny.value),
                              y))
            self.assertTrue(float_compare(backup_hist.GetBinContent(ibin), z))
            self.assertTrue(float_compare(backup_hist.GetBinError(ibin), dz))
        # Clean up
        self.doCleanups()