コード例 #1
0
    def test_scale_values(self):
        '''Test behavior of Variable.scale_values function'''
        values = list(zip(range(0, 5, 1), range(1, 6, 1)))

        testvar = Variable("testvar")
        testvar.is_binned = True
        testvar.units = "GeV"
        testvar.values = values

        self.assertTrue(testvar.values == values)

        for factor in [random.uniform(0, 10000) for x in range(100)]:
            # Check that scaling works
            testvar.scale_values(factor)
            scaled_values = [(factor * x[0], factor * x[1]) for x in values]
            assert (all(
                test_utilities.tuple_compare(x, y)
                for x, y in zip(testvar.values, scaled_values)))

            # Check that inverse also works
            testvar.scale_values(1. / factor)
            self.assertTrue(
                all(
                    test_utilities.tuple_compare(x, y)
                    for x, y in zip(testvar.values, values)))
コード例 #2
0
    def test_set_values_from_intervals(self):
        '''Test behavior of Uncertainy.test_set_values_from_intervals function'''

        # Dummy central values and variatons relative to central value
        npoints = 100
        values = list(range(0, npoints, 1))
        uncertainty = [(-random.uniform(0, 1), random.uniform(0, 1)) for _ in range(100)]

        # Convert +- error to interval bounds
        intervals = [(val + unc_minus, val + unc_plus)
                     for val, (unc_minus, unc_plus) in zip(values, uncertainty)]

        # Reference uses "normal" assignment
        refunc = Uncertainty("reference_unc")
        refunc.is_symmetric = False
        refunc.values = uncertainty

        # Test uses new function
        testunc = Uncertainty("test_unc")
        testunc.is_symmetric = False
        testunc.set_values_from_intervals(intervals, nominal=values)

        # Check that both agree
        self.assertTrue(all((test_utilities.tuple_compare(tup1, tup2) \
                        for tup1, tup2 in zip(testunc.values, refunc.values))))
コード例 #3
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()
コード例 #4
0
    def test_read_hist_1d_asymmetric_errors(self):
        """Test the read_hist_1d function for a histogram with asymmetric errors."""
        _fpath = "testfile.root"

        # Create test histogram
        n_bin = 17
        n_fill = 1000
        testname = "test1d_asymm"
        hist = ROOT.TH1D(testname, testname, n_bin, 0, 1)
        hist.SetBinErrorOption(ROOT.TH1.kPoisson)

        for val in np.random.normal(loc=0.5, scale=0.15, size=n_fill):
            hist.Fill(val)

        # Extract values
        x_values = []
        y_values = []
        dy_values = []
        for i in range(1, hist.GetNbinsX()):
            x_values.append(hist.GetBinCenter(i))
            y_values.append(hist.GetBinContent(i))
            dy_values.append((-hist.GetBinErrorLow(i), hist.GetBinErrorUp(i)))

        # 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_1d(testname)

        # Check consistency
        self.assertTrue(set(["x", "y", "x_edges", "dy"]) == set(points.keys()))

        self.assertTrue(
            all(float_compare(*tup) for tup in zip(points["x"], x_values)))
        self.assertTrue(
            all(float_compare(*tup) for tup in zip(points["y"], y_values)))
        self.assertTrue(
            all(tuple_compare(*tup) for tup in zip(points["dy"], dy_values)))

        # Clean up
        self.doCleanups()
コード例 #5
0
    def test_read_hist_1d_asymmetric_errors(self):
        """Test the read_hist_1d function for a histogram with asymmetric errors."""
        fpath = "testfile.root"

        # Create test histogram
        Nbin = 17
        Nfill = 1000
        hist = ROOT.TH1D("test1d_asymm", "test1d_asymm", Nbin, 0, 1)
        hist.SetBinErrorOption(ROOT.TH1.kPoisson)

        for val in np.random.normal(loc=0.5, scale=0.15, size=Nfill):
            hist.Fill(val)

        # Extract values
        x_values = []
        y_values = []
        dy_values = []
        for i in range(1, hist.GetNbinsX()):
            x_values.append(hist.GetBinCenter(i))
            y_values.append(hist.GetBinContent(i))
            dy_values.append((-hist.GetBinErrorLow(i), hist.GetBinErrorUp(i)))

        # Write to file
        f = ROOT.TFile(fpath, "RECREATE")
        hist.SetDirectory(f)
        hist.Write("test")
        f.Close()

        # Read back
        reader = RootFileReader(fpath)
        points = reader.read_hist_1d("test")

        # Check consistency
        self.assertTrue(set(["x", "y", "x_edges", "dy"]) == set(points.keys()))

        self.assertTrue(
            all(float_compare(*tup) for tup in zip(points["x"], x_values)))
        self.assertTrue(
            all(float_compare(*tup) for tup in zip(points["y"], y_values)))
        self.assertTrue(
            all(tuple_compare(*tup) for tup in zip(points["dy"], dy_values)))

        # Clean up
        os.remove(fpath)
コード例 #6
0
    def test_read_hist_1d_symmetric_errors(self):
        """Test the read_hist_1d function for a histogram with symmetric errors."""
        name = "test"

        # Create test histogram
        N = 100
        x_values = [0.5 + x for x in range(N)]
        y_values = list(np.random.uniform(-1e3, 1e3, N))
        dy_values = list(np.random.uniform(0, 1e3, N))
        x_edges = []
        hist = ROOT.TH1D("test1d_symm", "test1d_symm", N, 0, N)
        for i in range(1, hist.GetNbinsX() + 1):
            hist.SetBinContent(i, y_values[i - 1])
            hist.SetBinError(i, dy_values[i - 1])

            center = hist.GetBinCenter(i)
            width = hist.GetBinWidth(i)
            x_edges.append((center - 0.5 * width, center + 0.5 * width))

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

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

        # Check consistency
        for key in ["x", "y", "dy", "x_edges"]:
            self.assertTrue(key in set(points.keys()))
        self.assertTrue(
            all(float_compare(*tup) for tup in zip(points["x"], x_values)))
        self.assertTrue(
            all(float_compare(*tup) for tup in zip(points["y"], y_values)))
        self.assertTrue(
            all(float_compare(*tup) for tup in zip(points["dy"], dy_values)))
        self.assertTrue(
            all(
                tuple_compare(*tup)
                for tup in zip(points["x_edges"], x_edges)))

        # Clean up
        self.doCleanups()