From 513f132d289c27110f9f80173f661690eaea10c1 Mon Sep 17 00:00:00 2001 From: IzaakWN Date: Fri, 5 Apr 2024 19:39:55 +0200 Subject: [PATCH 1/4] retrieve TH1 from THStack via RootFileReader.retrieve_object --- hepdata_lib/root_utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hepdata_lib/root_utils.py b/hepdata_lib/root_utils.py index 1192ae20..64159925 100644 --- a/hepdata_lib/root_utils.py +++ b/hepdata_lib/root_utils.py @@ -98,7 +98,10 @@ def retrieve_object(self, path_to_object): try: obj = self.tfile.Get(parts[0]) for part in parts[1:]: - obj = obj.GetPrimitive(part) + if isinstance(obj,r.THStack): + return obj.GetHists().FindObject(part) + else: + obj = obj.GetPrimitive(part) assert obj From cd93d57753b11642a30b66e4789c57374ad34e2b Mon Sep 17 00:00:00 2001 From: IzaakWN Date: Wed, 10 Apr 2024 08:09:34 +0200 Subject: [PATCH 2/4] add tester; do not return hist from stack immediately (to allow assertion error) --- hepdata_lib/root_utils.py | 2 +- tests/test_rootfilereader.py | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/hepdata_lib/root_utils.py b/hepdata_lib/root_utils.py index 64159925..baf2b62e 100644 --- a/hepdata_lib/root_utils.py +++ b/hepdata_lib/root_utils.py @@ -99,7 +99,7 @@ def retrieve_object(self, path_to_object): obj = self.tfile.Get(parts[0]) for part in parts[1:]: if isinstance(obj,r.THStack): - return obj.GetHists().FindObject(part) + obj = obj.GetHists().FindObject(part) else: obj = obj.GetPrimitive(part) diff --git a/tests/test_rootfilereader.py b/tests/test_rootfilereader.py index 7ede5f49..75fe92c6 100644 --- a/tests/test_rootfilereader.py +++ b/tests/test_rootfilereader.py @@ -776,6 +776,43 @@ def test_retrieve_object_canvas(self): # Clean up self.doCleanups() + def test_retrieve_object_stack(self): + '''Check that retrieve_object correctly reads from stack in canvas.''' + # Disable graphical output + ROOT.gROOT.SetBatch(ROOT.kTRUE) + + # Create test histogram, plot on canvas, save to file + tfile = make_tmp_root_file(testcase=self) + histogram = ROOT.TH1D("testhist", "testhist", 10, 0, 1) + stack = ROOT.THStack("teststack","teststack") + stack.Add(histogram) + path_to_file = tfile.GetName() + + canvas = ROOT.TCanvas() + stack.Draw("HIST") + canvas.Write("canvas") + + reference = histogram.Clone("reference") + reference.SetDirectory(0) + if tfile: + tfile.Close() + + # Read it back + reader = RootFileReader(path_to_file) + try: + readback = reader.retrieve_object("canvas/teststack/testhist") + except OSError: + print("RootFileReader.retrieve_object raised unexpected IOError!") + self.fail() + + self.assertTrue(readback) + self.assertTrue( + histogram_compare_1d(reference, readback) + ) + + # Clean up + self.doCleanups() + def test_retrieve_object_canvas_tpad(self): '''Check that retrieve_object correctly reads from canvas.''' # Disable graphical output From f81a0917d93f8e517847a90c8b4571f14b64dfa6 Mon Sep 17 00:00:00 2001 From: IzaakWN Date: Thu, 11 Apr 2024 10:33:42 +0200 Subject: [PATCH 3/4] disable Pylint for ROOT members --- hepdata_lib/root_utils.py | 2 +- tests/test_rootfilereader.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/hepdata_lib/root_utils.py b/hepdata_lib/root_utils.py index 36e29d13..f4e435f7 100644 --- a/hepdata_lib/root_utils.py +++ b/hepdata_lib/root_utils.py @@ -101,7 +101,7 @@ def retrieve_object(self, path_to_object): try: obj = self.tfile.Get(parts[0]) for part in parts[1:]: - if isinstance(obj,r.THStack): + if isinstance(obj,r.THStack): # pylint: disable=no-member obj = obj.GetHists().FindObject(part) else: obj = obj.GetPrimitive(part) diff --git a/tests/test_rootfilereader.py b/tests/test_rootfilereader.py index 5de3e614..45ec5fa7 100644 --- a/tests/test_rootfilereader.py +++ b/tests/test_rootfilereader.py @@ -787,16 +787,16 @@ def test_retrieve_object_canvas(self): def test_retrieve_object_stack(self): '''Check that retrieve_object correctly reads from stack in canvas.''' # Disable graphical output - ROOT.gROOT.SetBatch(ROOT.kTRUE) + ROOT.gROOT.SetBatch(ROOT.kTRUE) # pylint: disable=no-member # Create test histogram, plot on canvas, save to file tfile = make_tmp_root_file(testcase=self) - histogram = ROOT.TH1D("testhist", "testhist", 10, 0, 1) - stack = ROOT.THStack("teststack","teststack") + histogram = ROOT.TH1D("testhist", "testhist", 10, 0, 1) # pylint: disable=no-member + stack = ROOT.THStack("teststack","teststack") # pylint: disable=no-member stack.Add(histogram) path_to_file = tfile.GetName() - canvas = ROOT.TCanvas() + canvas = ROOT.TCanvas() # pylint: disable=no-member stack.Draw("HIST") canvas.Write("canvas") From 6ed3eeff348d6a7d772d75cab6915beffdce8a8c Mon Sep 17 00:00:00 2001 From: Clemens Lange Date: Fri, 12 Apr 2024 00:13:48 +0200 Subject: [PATCH 4/4] Disable too-many-public-methods for test_rootfilereader.py --- tests/test_rootfilereader.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_rootfilereader.py b/tests/test_rootfilereader.py index 45ec5fa7..02712886 100644 --- a/tests/test_rootfilereader.py +++ b/tests/test_rootfilereader.py @@ -17,6 +17,7 @@ @pytest.mark.needs_root class TestRootFileReader(TestCase): + # pylint: disable=R0904 """Test the RootFileReader class.""" def test_tfile_setter(self):