-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtfile_context_manager.py
More file actions
100 lines (79 loc) · 3.27 KB
/
tfile_context_manager.py
File metadata and controls
100 lines (79 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os
import unittest
import ROOT
from ROOT import TFile
class TFileContextManager(unittest.TestCase):
"""
Test of TFile used as context manager
"""
NBINS = 123
XMIN = 10
XMAX = 242
def check_file_data(self, tfile, filename, histoname):
"""
Check status of the TFile after the context manager and correctness of
the data it contains.
"""
self.assertTrue(tfile) # The TFile object is still there
self.assertFalse(tfile.IsOpen()) # And it is correctly closed
with TFile(filename, "read") as infile:
hin = infile.Get(histoname)
xaxis = hin.GetXaxis()
self.assertEqual(self.NBINS, hin.GetNbinsX())
self.assertEqual(self.XMIN, xaxis.GetXmin())
self.assertEqual(self.XMAX, xaxis.GetXmax())
os.remove(filename)
def test_writeobject(self):
"""
Write a histogram in a file within a context manager, using TDirectory::WriteObject.
"""
filename = "TFileContextManager_test_writeobject.root"
histoname = "myhisto"
with TFile(filename, "recreate") as outfile:
hout = ROOT.TH1F(histoname, histoname, self.NBINS, self.XMIN, self.XMAX)
outfile.WriteObject(hout, "myhisto")
self.check_file_data(outfile, filename, histoname)
def test_histowrite(self):
"""
Write a histogram in a file within a context manager, using TH1::Write.
"""
filename = "TFileContextManager_test_histowrite.root"
histoname = "myhisto_2"
with TFile(filename, "recreate") as outfile:
hout = ROOT.TH1F(histoname, histoname, self.NBINS, self.XMIN, self.XMAX)
hout.Write()
self.check_file_data(outfile, filename, histoname)
def test_filewrite(self):
"""
Write a histogram in a file within a context manager, using TFile::Write.
"""
filename = "TFileContextManager_test_filewrite.root"
histoname = "myhisto_3"
with TFile(filename, "recreate") as outfile:
hout = ROOT.TH1F(histoname, histoname, self.NBINS, self.XMIN, self.XMAX)
hout.SetDirectory(outfile)
outfile.Write()
self.check_file_data(outfile, filename, histoname)
def test_detachhisto(self):
"""
Detach histogram from file and access it outside of the context, both when writing and reading.
"""
filename = "TFileContextManager_test_detachhisto.root"
with TFile(filename, "recreate") as outfile:
hout = ROOT.TH1F("myhisto", "myhisto", self.NBINS, self.XMIN, self.XMAX)
hout.SetDirectory(ROOT.nullptr)
outfile.WriteObject(hout, "myhisto")
self.assertTrue(hout)
self.assertEqual(hout.GetName(), "myhisto")
with TFile(filename, "read") as infile:
hin = infile.Get("myhisto")
hin.SetDirectory(ROOT.nullptr)
xaxis = hin.GetXaxis()
self.assertEqual(self.NBINS, hin.GetNbinsX())
self.assertEqual(self.XMIN, xaxis.GetXmin())
self.assertEqual(self.XMAX, xaxis.GetXmax())
self.assertTrue(hin)
self.assertEqual(hin.GetName(), "myhisto")
os.remove(filename)
if __name__ == '__main__':
unittest.main()