Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TDirectory::TContext Class Reference

TDirectory::TContext keeps track and restore the current directory.

With this tool C++ exceptions will be guaranteed to properly restore the current directory pointer.

For example code like:

mydirectory->cd();
if (...) {
....
sav->cd();
return;
} else if (...) {
....
sav->cd();
return;
}
sav->cd;
return;
#define gDirectory
Definition TDirectory.h:384
Describe directory structure in memory.
Definition TDirectory.h:45
virtual Bool_t cd()
Change current directory to "this" directory.

can be replaced with the simpler and exception safe:

TDirectory::TContext context(gDirectory, mydirectory);
if (...) {
....
return;
} else if (...) {
....
return;
}
return;
TDirectory::TContext keeps track and restore the current directory.
Definition TDirectory.h:89

PyROOT

The functionality offered by TContext can be used in PyROOT with a context manager. Here are a few examples:

import ROOT
from ROOT import TDirectory
# Open some file here
file = ROOT.TFile(...)
# Retrieve contents from the file
histo = file.Get("myhisto")
# After the 'with' statement, the current directory is restored to ROOT.gROOT


import ROOT
from ROOT import TDirectory
file1 = ROOT.TFile("file1.root", "recreate")
#...
file2 = ROOT.TFile("file2.root", "recreate")
#...
file3 = ROOT.TFile("file3.root", "recreate")
# Before the 'with' statement, the current directory is file3 (the last file opened)
with TDirectory.TContext(file1):
# Inside the statement, the current directory is file1
histo = ROOT.TH1F(...)
histo.Write()
# After the statement, the current directory is restored to file3


import ROOT
from ROOT import TDirectory
file1 = ROOT.TFile("file1.root")
file2 = ROOT.TFile("file2.root")
with TDirectory.TContext(file1, file2):
# Manage content in file2
histo = ROOT.TH1F(...)
# histo will be written to file2
histo.Write()
# Current directory is set to 'file1.root'

Note that TContext restores the current directory to its status before the 'with' statement, but does not change the status of any file that has been opened inside the context (e.g. it does not automatically close the file).

Definition at line 89 of file TDirectory.h.

Public Member Functions

 TContext ()
 
 TContext (TDirectory *newCurrent)
 
 TContext (TDirectory *previous, TDirectory *newCurrent)
 
 ~TContext ()
 Destructor.
 

Private Member Functions

 TContext (TContext &)=delete
 Pointer to the next TContext in the implied list of context pointing to fPrevious.
 
void CdNull ()
 Set the current directory to null.
 
TContextoperator= (TContext &)=delete
 
void RegisterCurrentDirectory ()
 

Private Attributes

std::atomic< boolfActiveDestructor {false}
 Pointer to the previous current directory.
 
std::atomic< TDirectory * > fDirectory {nullptr}
 
std::atomic< boolfDirectoryWait {false}
 Set to true during the destructor execution.
 
TContextfNext {nullptr}
 Pointer to the next TContext in the implied list of context pointing to fPrevious.
 
TContextfPrevious {nullptr}
 Set to true if a TDirectory might still access this object.
 

Friends

class TDirectory
 

#include <TDirectory.h>

Constructor & Destructor Documentation

◆ TContext() [1/4]

TDirectory::TContext::TContext ( TContext )
privatedelete

Pointer to the next TContext in the implied list of context pointing to fPrevious.

◆ TContext() [2/4]

TDirectory::TContext::TContext ( TDirectory previous,
TDirectory newCurrent 
)
inline

Definition at line 108 of file TDirectory.h.

◆ TContext() [3/4]

TDirectory::TContext::TContext ( )
inline

Definition at line 120 of file TDirectory.h.

◆ TContext() [4/4]

TDirectory::TContext::TContext ( TDirectory newCurrent)
inline

Definition at line 126 of file TDirectory.h.

◆ ~TContext()

TDirectory::TContext::~TContext ( )

Destructor.

Reset the current directory to its previous state.

Definition at line 143 of file TDirectory.cxx.

Member Function Documentation

◆ CdNull()

void TDirectory::TContext::CdNull ( )
private

Set the current directory to null.

This is called from the TContext destructor. Since the destructor is inline, we do not want to have it directly use a global variable.

Definition at line 133 of file TDirectory.cxx.

◆ operator=()

TContext & TDirectory::TContext::operator= ( TContext )
privatedelete

◆ RegisterCurrentDirectory()

void TDirectory::TContext::RegisterCurrentDirectory ( )
private

Definition at line 1344 of file TDirectory.cxx.

Friends And Related Symbol Documentation

◆ TDirectory

friend class TDirectory
friend

Definition at line 101 of file TDirectory.h.

Member Data Documentation

◆ fActiveDestructor

std::atomic<bool> TDirectory::TContext::fActiveDestructor {false}
private

Pointer to the previous current directory.

Definition at line 92 of file TDirectory.h.

◆ fDirectory

std::atomic<TDirectory*> TDirectory::TContext::fDirectory {nullptr}
private

Definition at line 91 of file TDirectory.h.

◆ fDirectoryWait

std::atomic<bool> TDirectory::TContext::fDirectoryWait {false}
private

Set to true during the destructor execution.

Definition at line 93 of file TDirectory.h.

◆ fNext

TContext* TDirectory::TContext::fNext {nullptr}
private

Pointer to the next TContext in the implied list of context pointing to fPrevious.

Definition at line 95 of file TDirectory.h.

◆ fPrevious

TContext* TDirectory::TContext::fPrevious {nullptr}
private

Set to true if a TDirectory might still access this object.

Definition at line 94 of file TDirectory.h.

Libraries for TDirectory::TContext:

The documentation for this class was generated from the following files: