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()
def test_scale_values(self): '''Test behavior of Uncertainty.scale_values function''' values = list(range(0, 300, 1)) uncertainty = [x + random.uniform(0, 2) for x in values] testvar = Variable("testvar") testvar.is_binned = False testvar.units = "GeV" testvar.values = values testunc = Uncertainty("testunc") testunc.is_symmetric = True testunc.values = uncertainty testvar.uncertainties.append(testunc) assert testvar.values == values self.assertTrue(testunc.values == uncertainty) for factor in [random.uniform(0, 10000) for x in range(100)]: # Check that scaling works testvar.scale_values(factor) scaled_values = [factor * x for x in values] scaled_uncertainty = [factor * x for x in uncertainty] self.assertTrue(all(test_utilities.float_compare(x, y) for x, y in zip(testvar.values, scaled_values))) self.assertTrue(all(test_utilities.float_compare(x, y) for x, y in zip(testunc.values, scaled_uncertainty))) # Check that inverse also works testvar.scale_values(1. / factor) self.assertTrue(all(test_utilities.float_compare(x, y) for x, y in zip(testvar.values, values))) self.assertTrue(all(test_utilities.float_compare(x, y) for x, y in zip(testunc.values, uncertainty)))
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)
def test_read_hist_1d_symmetric_errors(self): """Test the read_hist_1d function for a histogram with symmetric errors.""" fpath = "testfile.root" # 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)) 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]) f = ROOT.TFile(fpath, "RECREATE") hist.SetDirectory(f) hist.Write("test") f.Close() reader = RootFileReader(fpath) points = reader.read_hist_1d("test") 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(float_compare(*tup) for tup in zip(points["dy"], dy_values))) # Clean up os.remove(fpath)
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)) 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]) testfile = make_tmp_root_file(testcase=self) testfile.cd() hist.Write(name) testfile.Close() reader = RootFileReader(testfile.GetName()) points = reader.read_hist_1d(name) 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(float_compare(*tup) for tup in zip(points["dy"], dy_values))) # Clean up self.doCleanups()
def test_read_hist_1d_range(self): """Test the read_hist_1d function for a histogram with symmetric errors.""" # Create test histogram N = 100 xmin = 20 xmax = 80 x_values = [0.5 + x for x in range(xmin, xmax)] y_values = list(np.random.uniform(-1e3, 1e3, xmax-xmin)) dy_values = list(np.random.uniform(0, 1e3, xmax-xmin)) testname = "test1d_symm" hist = ROOT.TH1D(testname, testname, N, 0, N) for i in range(xmin, xmax): hist.SetBinContent(i+1, y_values[i-xmin]) hist.SetBinError(i+1, dy_values[i-xmin]) testfile = make_tmp_root_file(testcase=self) testfile.cd() hist.Write(testname) testfile.Close() reader = RootFileReader(testfile.GetName()) # pass too many axis limits with self.assertRaises(TypeError): reader.read_hist_1d(testname, xlim=(xmin, xmax), ylim=(xmin, xmax)) # pass wrong axis limits or parameter with self.assertRaises(TypeError): reader.read_hist_1d(testname, ylim=(xmin, xmax)) # pass xmax < xmin (wrong ordering) with self.assertRaises(AssertionError): reader.read_hist_1d(testname, xlim=(xmax, xmin)) # pass too many parameters as single axis limit with self.assertRaises(AssertionError): reader.read_hist_1d(testname, xlim=(xmin, xmax, 5)) # pass non-float/-int as first item with self.assertRaises(AssertionError): reader.read_hist_1d(testname, xlim=("5", xmax)) # pass non-float/-int as second item with self.assertRaises(AssertionError): reader.read_hist_1d(testname, xlim=(xmin, "12")) # pass set instance (needs to be ordered tuple or list) with self.assertRaises(AssertionError): reader.read_hist_1d(testname, xlim={xmin, xmax}) # pass wrong instance with self.assertRaises(AssertionError): reader.read_hist_1d(testname, xlim="some string") points = reader.read_hist_1d(testname, xlim=(xmin, xmax)) 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(float_compare(*tup) for tup in zip(points["dy"], dy_values))) # Clean up self.doCleanups()
def test_read_hist_1d_asymmetric_force_symmetric_errors(self): """Test the read_hist_1d function for a histogram with asymmetric errors forcing symmetric errors to be used.""" _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.GetBinError(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, force_symmetric_errors=True) # Check consistency for key in ["x", "y", "dy"]: 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))) # Clean up self.doCleanups()
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)
def test_read_tree(self): """Test the read_tree function.""" n_fill = 1000 branchname = "testbranch" path_to_tree = "testpath" # Set up some test data, put into TTree and write to file data = np.random.normal(loc=0.5, scale=0.15, size=n_fill) number = array("f", [0]) tree = ROOT.TTree() tree.Branch(branchname, number, "test/F") for inumber in data: number[0] = inumber tree.Fill() testfile = make_tmp_root_file(testcase=self) tree.Write(path_to_tree) if testfile: testfile.Close() # Read data back and check consistency reader = RootFileReader(testfile.GetName()) try: data_readback = reader.read_tree(path_to_tree, branchname) except RuntimeError: self.fail( "RootFileReader.read_tree raised an unexpected RuntimeError!") self.assertIsInstance(data_readback, list) self.assertTrue( all([ float_compare(values[0], values[1]) for values in zip(data, data_readback) ])) # Try reading a nonexistant branch from an existing tree with self.assertRaises(RuntimeError): reader.read_tree(path_to_tree, "some_random_name") # Try reading a nonexistant tree with self.assertRaises(RuntimeError): reader.read_tree("some/random/path", "some_random_name") # Clean up self.doCleanups()
def test_read_hist_2d_range(self): """Test the read_hist_2d function with symmetric errors.""" fpath = "testfile.root" # 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)) hist = ROOT.TH2D("test2d_sym", "test2d_sym", 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") rfile = ROOT.TFile(fpath, "RECREATE") hist.SetDirectory(rfile) hist.Write("test2d_sym") rfile.Close() reader = RootFileReader(fpath) # pass too many axis limits with self.assertRaises(TypeError): reader.read_hist_2d("test", xlim=(xmin, xmax), ylim=(ymin, ymax), zlim=(ymin, ymax)) # pass non-existing axis limit/parameter with self.assertRaises(TypeError): reader.read_hist_2d("test", zlim=(xmin, xmax)) # pass wrong order (xmax < xmin) with self.assertRaises(AssertionError): reader.read_hist_2d("test", ylim=(xmax, xmin)) # pass too many parameters as single axis limit with self.assertRaises(AssertionError): reader.read_hist_2d("test", ylim=(xmin, xmax, 5)) # pass wrong type as first item with self.assertRaises(AssertionError): reader.read_hist_2d("test", ylim=("5", xmax)) # pass wrong type as second item with self.assertRaises(AssertionError): reader.read_hist_2d("test", ylim=(xmin, "12")) # pass set instance (needs ordered datatype: tuple or list) with self.assertRaises(AssertionError): reader.read_hist_2d("test", xlim={xmin, xmax}) # pass wrong instance with self.assertRaises(AssertionError): reader.read_hist_2d("test", xlim="some string") points = reader.read_hist_2d("test2d_sym", 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 os.remove(fpath)
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()