ROOT
master
Reference Guide
Loading...
Searching...
No Matches
_tcontext.pyzdoc
Go to the documentation of this file.
1
/**
2
\class TDirectory::TContext
3
\brief \parblock \endparblock
4
\htmlonly
5
<div class="pyrootbox">
6
\endhtmlonly
7
## PyROOT
8
9
The functionality offered by TContext can be used in PyROOT with a context manager. Here are a few examples:
10
\code{.py}
11
import ROOT
12
from ROOT import TDirectory
13
14
with TDirectory.TContext():
15
# Open some file here
16
file = ROOT.TFile(...)
17
# Retrieve contents from the file
18
histo = file.Get("myhisto")
19
20
# After the 'with' statement, the current directory is restored to ROOT.gROOT
21
\endcode
22
\n
23
\code{.py}
24
import ROOT
25
from ROOT import TDirectory
26
27
file1 = ROOT.TFile("file1.root", "recreate")
28
#...
29
file2 = ROOT.TFile("file2.root", "recreate")
30
#...
31
file3 = ROOT.TFile("file3.root", "recreate")
32
33
# Before the 'with' statement, the current directory is file3 (the last file opened)
34
with TDirectory.TContext(file1):
35
# Inside the statement, the current directory is file1
36
histo = ROOT.TH1F(...)
37
histo.Write()
38
39
# After the statement, the current directory is restored to file3
40
\endcode
41
\n
42
\code{.py}
43
import ROOT
44
from ROOT import TDirectory
45
46
file1 = ROOT.TFile("file1.root")
47
file2 = ROOT.TFile("file2.root")
48
49
with TDirectory.TContext(file1, file2):
50
# Manage content in file2
51
histo = ROOT.TH1F(...)
52
# histo will be written to file2
53
histo.Write()
54
55
# Current directory is set to 'file1.root'
56
\endcode
57
58
Note that TContext restores the current directory to its status before the 'with'
59
statement, but does not change the status of any file that has been opened inside
60
the context (e.g. it does not automatically close the file).
61
\htmlonly
62
</div>
63
\endhtmlonly
64
*/
master_TMP
pyzdoc
_tcontext.pyzdoc
ROOT master - Reference Guide Generated on Wed Aug 14 2024 14:53:26 (GVA Time) using Doxygen 1.9.8