Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_tcontext.pyzdoc
Go to the documentation of this file.
2
3The functionality offered by TContext can be used in PyROOT with a context manager. Here are a few examples:
4\code{.py}
5import ROOT
6from ROOT import TDirectory
7
8with 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}
18import ROOT
19from ROOT import TDirectory
20
21file1 = ROOT.TFile("file1.root", "recreate")
22#...
23file2 = ROOT.TFile("file2.root", "recreate")
24#...
25file3 = ROOT.TFile("file3.root", "recreate")
26
27# Before the 'with' statement, the current directory is file3 (the last file opened)
28with 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}
37import ROOT
38from ROOT import TDirectory
39
40file1 = ROOT.TFile("file1.root")
41file2 = ROOT.TFile("file2.root")
42
43with 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
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).
55
56\endpythondoc
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
TDirectory::TContext keeps track and restore the current directory.
Definition TDirectory.h:89
Describe directory structure in memory.
Definition TDirectory.h:45
friend class TContext
Definition TDirectory.h:168
Namespace for new ROOT classes and functions.