3The functionality offered by TContext can be used in
PyROOT with
a context manager. Here are
a few examples:
6from ROOT import TDirectory
8with TDirectory.TContext():
10 file = ROOT.TFile(...)
11 # Retrieve contents from the file
12 histo = file.Get("myhisto")
14# After the
'with' statement, the current directory is restored to
ROOT.gROOT
19from ROOT import TDirectory
21file1 = ROOT.TFile("file1.root",
"recreate")
23file2 =
ROOT.TFile(
"file2.root",
"recreate")
25file3 =
ROOT.TFile(
"file3.root",
"recreate")
27# Before the 'with' statement, the current directory is file3 (the last file opened)
29 # Inside the statement, the current directory is file1
30 histo =
ROOT.TH1F(...)
33# After the statement, the current directory is restored to file3
38from ROOT import TDirectory
40file1 = ROOT.TFile("file1.root")
41file2 =
ROOT.TFile(
"file2.root")
44 # Manage content in file2
45 histo =
ROOT.TH1F(...)
46 # histo will be written to file2
49# Current directory is set to 'file1.root'
52Note that TContext restores the current directory to its status before the
'with'
53statement, but does not change the status of any file that has been opened inside
54the context (
e.g. it does not automatically close the file).
TDirectory::TContext keeps track and restore the current directory.
Describe directory structure in memory.
Namespace for new ROOT classes and functions.