|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +import ROOT |
| 4 | + |
| 5 | +RFile = ROOT.Experimental.RFile |
| 6 | + |
| 7 | + |
| 8 | +class RFileTests(unittest.TestCase): |
| 9 | + def test_open_for_reading(self): |
| 10 | + """A RFile can read a ROOT file created by TFile""" |
| 11 | + |
| 12 | + fileName = "test_rfile_read_py.root" |
| 13 | + |
| 14 | + # Create a root file to open |
| 15 | + with ROOT.TFile.Open(fileName, "RECREATE") as tfile: |
| 16 | + hist = ROOT.TH1D("hist", "", 100, -10, 10) |
| 17 | + hist.FillRandom("gaus", 100) |
| 18 | + tfile.WriteObject(hist, "hist") |
| 19 | + |
| 20 | + with RFile.Open(fileName) as rfile: |
| 21 | + hist = rfile.Get("hist") |
| 22 | + self.assertNotEqual(hist, None) |
| 23 | + self.assertEqual(rfile.Get[ROOT.TH1D]("inexistent"), None) |
| 24 | + self.assertEqual(rfile.Get[ROOT.TH1F]("hist"), None) |
| 25 | + self.assertNotEqual(rfile.Get[ROOT.TH1]("hist"), None) |
| 26 | + |
| 27 | + with self.assertRaises(ROOT.RException): |
| 28 | + # This should fail because the file was opened as read-only |
| 29 | + rfile.Put("foo", hist) |
| 30 | + |
| 31 | + os.remove(fileName) |
| 32 | + |
| 33 | + def test_writing_reading(self): |
| 34 | + """A RFile can be written into and read from""" |
| 35 | + |
| 36 | + fileName = "test_rfile_writeread_py.root" |
| 37 | + |
| 38 | + with RFile.Recreate(fileName) as rfile: |
| 39 | + hist = ROOT.TH1D("hist", "", 100, -10, 10) |
| 40 | + hist.FillRandom("gaus", 10) |
| 41 | + rfile.Put("hist", hist) |
| 42 | + with self.assertRaises(ROOT.RException): |
| 43 | + rfile.Put("hist/2", hist) |
| 44 | + |
| 45 | + with RFile.Open(fileName) as rfile: |
| 46 | + hist = rfile.Get("hist") |
| 47 | + self.assertNotEqual(hist, None) |
| 48 | + |
| 49 | + os.remove(fileName) |
| 50 | + |
| 51 | + def test_getkeyinfo(self): |
| 52 | + """A RFile can query individual keys of its objects""" |
| 53 | + |
| 54 | + fileName = "test_rfile_getkeyinfo_py.root" |
| 55 | + |
| 56 | + with RFile.Recreate(fileName) as rfile: |
| 57 | + hist = ROOT.TH1D("hist", "", 100, -10, 10) |
| 58 | + hist.FillRandom("gaus", 10) |
| 59 | + rfile.Put("hist", hist) |
| 60 | + rfile.Put("foo/hist", hist) |
| 61 | + rfile.Put("foo/bar/hist", hist) |
| 62 | + rfile.Put("foo/bar/hist2", hist) |
| 63 | + rfile.Put("foo/hist2", hist) |
| 64 | + |
| 65 | + with RFile.Open(fileName) as rfile: |
| 66 | + key = rfile.GetKeyInfo("hist") |
| 67 | + self.assertEqual(key.GetPath(), "hist") |
| 68 | + self.assertEqual(key.GetClassName(), "TH1D") |
| 69 | + |
| 70 | + key = rfile.GetKeyInfo("does_not_exist") |
| 71 | + self.assertEqual(key, None) |
| 72 | + |
| 73 | + def test_listkeys(self): |
| 74 | + """A RFile can query the keys of its objects and directories""" |
| 75 | + |
| 76 | + fileName = "test_rfile_listkeys_py.root" |
| 77 | + |
| 78 | + with RFile.Recreate(fileName) as rfile: |
| 79 | + hist = ROOT.TH1D("hist", "", 100, -10, 10) |
| 80 | + hist.FillRandom("gaus", 10) |
| 81 | + rfile.Put("hist", hist) |
| 82 | + rfile.Put("foo/hist", hist) |
| 83 | + rfile.Put("foo/bar/hist", hist) |
| 84 | + rfile.Put("foo/bar/hist2", hist) |
| 85 | + rfile.Put("foo/hist2", hist) |
| 86 | + |
| 87 | + with RFile.Open(fileName) as rfile: |
| 88 | + keys = [key.GetPath() for key in rfile.ListKeys()] |
| 89 | + self.assertEqual(keys, ["hist", "foo/hist", "foo/bar/hist", "foo/bar/hist2", "foo/hist2"]) |
| 90 | + |
| 91 | + keys = [key.GetClassName() for key in rfile.ListKeys()] |
| 92 | + self.assertEqual(keys, ["TH1D"] * len(keys)) |
| 93 | + |
| 94 | + self.assertEqual( |
| 95 | + [key.GetPath() for key in rfile.ListKeys("foo")], |
| 96 | + ["foo/hist", "foo/bar/hist", "foo/bar/hist2", "foo/hist2"], |
| 97 | + ) |
| 98 | + |
| 99 | + self.assertEqual([key.GetPath() for key in rfile.ListKeys("foo/bar")], ["foo/bar/hist", "foo/bar/hist2"]) |
| 100 | + |
| 101 | + self.assertEqual( |
| 102 | + [key.GetPath() for key in rfile.ListKeys("", listDirs=True, listObjects=False)], ["foo", "foo/bar"] |
| 103 | + ) |
| 104 | + self.assertEqual( |
| 105 | + [key.GetPath() for key in rfile.ListKeys("", listDirs=True, listObjects=False, listRecursive=False)], |
| 106 | + ["foo"], |
| 107 | + ) |
| 108 | + self.assertEqual( |
| 109 | + [key.GetPath() for key in rfile.ListKeys("", listDirs=True, listRecursive=False)], ["hist", "foo"] |
| 110 | + ) |
| 111 | + |
| 112 | + os.remove(fileName) |
| 113 | + |
| 114 | + def test_putUnsupportedType(self): |
| 115 | + fileName = "test_rfile_putunsupported_py.root" |
| 116 | + |
| 117 | + with RFile.Recreate(fileName) as rfile: |
| 118 | + # Storing integers is unsupported |
| 119 | + with self.assertRaises(TypeError): |
| 120 | + rfile.Put("foo", 2) |
| 121 | + |
| 122 | + # Storing lists without an explicit template is unsupported |
| 123 | + with self.assertRaises(TypeError): |
| 124 | + rfile.Put("bar", [2, 3]) |
| 125 | + |
| 126 | + # Storing lists with an explicit template is supported |
| 127 | + rfile.Put["std::vector<int>"]("bar", [2, 3]) |
| 128 | + |
| 129 | + # Storing strings is supported |
| 130 | + rfile.Put("str", "foobar") |
| 131 | + |
| 132 | + with RFile.Open(fileName) as rfile: |
| 133 | + self.assertEqual(rfile.Get("str"), b"foobar") |
| 134 | + |
| 135 | + os.remove(fileName) |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + unittest.main() |
0 commit comments