ROOT
master
Reference Guide
Loading...
Searching...
No Matches
_tcontext.pyzdoc
Go to the documentation of this file.
1
\pythondoc
TDirectory::TContext
2
3
The functionality offered by TContext can be used in
PyROOT
with
a
context manager. Here are
a
few examples:
4
\code{.py}
5
import
ROOT
6
from ROOT import TDirectory
7
8
with TDirectory.TContext():
9
# Open some file here
10
file = ROOT.TFile(...)
11
# Retrieve contents from the file
12
histo = file.Get("myhisto")
13
14
# After the
'with'
statement, the current directory is restored to
ROOT
.gROOT
15
\endcode
16
\n
17
\code{.py}
18
import
ROOT
19
from ROOT import TDirectory
20
21
file1 = ROOT.TFile("file1.root",
"recreate"
)
22
#...
23
file2 =
ROOT
.TFile(
"file2.root"
,
"recreate"
)
24
#...
25
file3 =
ROOT
.TFile(
"file3.root"
,
"recreate"
)
26
27
# Before the 'with' statement, the current directory is file3 (the last file opened)
28
with
TDirectory
.
TContext
(file1):
29
# Inside the statement, the current directory is file1
30
histo =
ROOT
.TH1F(...)
31
histo.Write()
32
33
# After the statement, the current directory is restored to file3
34
\endcode
35
\n
36
\code{.py}
37
import
ROOT
38
from ROOT import TDirectory
39
40
file1 = ROOT.TFile("file1.root")
41
file2 =
ROOT
.TFile(
"file2.root"
)
42
43
with
TDirectory
.
TContext
(file1, file2):
44
# Manage content in file2
45
histo =
ROOT
.TH1F(...)
46
# histo will be written to file2
47
histo.Write()
48
49
# Current directory is set to 'file1.root'
50
\endcode
51
52
Note that TContext restores the current directory to its status before the
'with'
53
statement, but does not change the status of any file that has been opened inside
54
the context (
e
.g. it does not automatically close the file).
55
56
\endpythondoc
a
#define a(i)
Definition
RSha256.hxx:99
e
#define e(i)
Definition
RSha256.hxx:103
TDirectory::TContext
TDirectory::TContext keeps track and restore the current directory.
Definition
TDirectory.h:89
TDirectory
Describe directory structure in memory.
Definition
TDirectory.h:45
TDirectory::TContext
friend class TContext
Definition
TDirectory.h:168
PyROOT
Definition
CPPInstancePyz.cxx:24
ROOT
Definition
EExecutionPolicy.hxx:4
pyzdoc
_tcontext.pyzdoc
ROOT master - Reference Guide Generated on Wed Sep 24 2025 04:33:08 (GVA Time) using Doxygen 1.10.0