Logo ROOT  
Reference Guide
TDirectory.h
Go to the documentation of this file.
1// @(#)root/base:$Id$
2// Author: Rene Brun 28/11/94
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TDirectory
13#define ROOT_TDirectory
14
15
16//////////////////////////////////////////////////////////////////////////
17// //
18// TDirectory //
19// //
20// Describe directory structure in memory. //
21// //
22//////////////////////////////////////////////////////////////////////////
23
24#include "TNamed.h"
25#include "TClass.h"
26#include "TUUID.h"
27
28#ifdef R__LESS_INCLUDES
29class TList;
30#else
31#include "TList.h"
32#include "TBuffer.h"
33// #include "TDatime.h"
34#endif
35
36class TBrowser;
37class TKey;
38class TFile;
39
40class TDirectory : public TNamed {
41public:
42 /** @class TContext
43 *
44 * Small helper to keep current directory context.
45 * Automatically reverts to "old" directory
46 */
47 class TContext {
48 private:
49 std::atomic<TDirectory*> fDirectory{nullptr}; //! Pointer to the previous current directory.
50 std::atomic<bool> fActiveDestructor{false}; //! Set to true during the destructor execution
51 std::atomic<bool> fDirectoryWait{false}; //! Set to true if a TDirectory might still access this object.
52 TContext *fPrevious{nullptr}; //! Pointer to the next TContext in the implied list of context pointing to fPrevious.
53 TContext *fNext{nullptr}; //! Pointer to the next TContext in the implied list of context pointing to fPrevious.
54
55 TContext(TContext&) = delete;
57
58 void CdNull();
59 friend class TDirectory;
60 public:
61 TContext(TDirectory *previous, TDirectory *newCurrent) : fDirectory(previous)
62 {
63 // Store the current directory so we can restore it
64 // later and cd to the new directory.
65 if (fDirectory)
66 (*fDirectory).RegisterContext(this);
67 if (newCurrent)
68 newCurrent->cd();
69 else
70 CdNull();
71 }
73 {
74 // Store the current directory so we can restore it
75 // later and cd to the new directory.
76 if (fDirectory)
77 (*fDirectory).RegisterContext(this);
78 }
80 {
81 // Store the current directory so we can restore it
82 // later and cd to the new directory.
83 if (fDirectory)
84 (*fDirectory).RegisterContext(this);
85 if (newCurrent)
86 newCurrent->cd();
87 else
88 CdNull();
89 }
90 ~TContext();
91 };
92
93protected:
94
95 TObject *fMother{nullptr}; // pointer to mother of the directory
96 TList *fList{nullptr}; // List of objects in memory
97 TUUID fUUID; // Unique identifier
98 mutable TString fPathBuffer; //! Buffer for GetPath() function
99 TContext *fContext{nullptr}; //! Pointer to a list of TContext object pointing to this TDirectory
100
101 std::atomic<size_t> fContextPeg; //!Counter delaying the TDirectory destructor from finishing.
102 mutable std::atomic_flag fSpinLock; //! MSVC doesn't support = ATOMIC_FLAG_INIT;
103
104 static Bool_t fgAddDirectory; //!flag to add histograms, graphs,etc to the directory
105
106 Bool_t cd1(const char *path);
107 static Bool_t Cd1(const char *path);
108
109 void CleanTargets();
110 void FillFullPath(TString& buf) const;
111 void RegisterContext(TContext *ctxt);
112 void UnregisterContext(TContext *ctxt);
113 void BuildDirectory(TFile* motherFile, TDirectory* motherDir);
114
115 friend class TContext;
116
117protected:
118 TDirectory(const TDirectory &directory) = delete; //Directories cannot be copied
119 void operator=(const TDirectory &) = delete; //Directories cannot be copied
120
121public:
122
123 TDirectory();
124 TDirectory(const char *name, const char *title, Option_t *option = "", TDirectory* motherDir = nullptr);
125 virtual ~TDirectory();
126 static void AddDirectory(Bool_t add=kTRUE);
127 static Bool_t AddDirectoryStatus();
128 virtual void Append(TObject *obj, Bool_t replace = kFALSE);
129 virtual void Add(TObject *obj, Bool_t replace = kFALSE) { Append(obj,replace); }
130 virtual Int_t AppendKey(TKey *) {return 0;}
131 void Browse(TBrowser *b) override;
132 virtual void Build(TFile* motherFile = nullptr, TDirectory* motherDir = nullptr) { BuildDirectory(motherFile, motherDir); }
133 void Clear(Option_t *option="") override;
134 virtual TObject *CloneObject(const TObject *obj, Bool_t autoadd = kTRUE);
135 virtual void Close(Option_t *option="");
136 static TDirectory *&CurrentDirectory(); // Return the current directory for this thread.
137 void Copy(TObject &) const override { MayNotUse("Copy(TObject &)"); }
138 virtual Bool_t cd(const char *path = nullptr);
139 virtual void DeleteAll(Option_t *option="");
140 void Delete(const char *namecycle="") override;
141 void Draw(Option_t *option="") override;
142 virtual TKey *FindKey(const char * /*keyname*/) const {return nullptr;}
143 virtual TKey *FindKeyAny(const char * /*keyname*/) const {return nullptr;}
144 TObject *FindObject(const char *name) const override;
145 TObject *FindObject(const TObject *obj) const override;
146 virtual TObject *FindObjectAny(const char *name) const;
147 virtual TObject *FindObjectAnyFile(const char * /*name*/) const {return nullptr;}
148 virtual TObject *Get(const char *namecycle);
149 /// See documentation of TDirectoryFile::Get(const char *namecycle)
150 template <class T> inline T* Get(const char* namecycle)
151 {
152 return static_cast<T*>(GetObjectChecked(namecycle, TClass::GetClass<T>()));
153 }
154 virtual TDirectory *GetDirectory(const char *namecycle, Bool_t printError = false, const char *funcname = "GetDirectory");
155 template <class T> inline void GetObject(const char* namecycle, T*& ptr) // See TDirectory::Get for information
156 {
157 ptr = (T *)GetObjectChecked(namecycle, TClass::GetClass<T>());
158 }
159 virtual void *GetObjectChecked(const char *namecycle, const char* classname);
160 virtual void *GetObjectChecked(const char *namecycle, const TClass* cl);
161 virtual void *GetObjectUnchecked(const char *namecycle);
162 virtual Int_t GetBufferSize() const {return 0;}
163 virtual TFile *GetFile() const { return 0; }
164 virtual TKey *GetKey(const char * /*name */, Short_t /* cycle */=9999) const {return nullptr;}
165 virtual TList *GetList() const { return fList; }
166 virtual TList *GetListOfKeys() const { return nullptr; }
167 TObject *GetMother() const { return fMother; }
168 TDirectory *GetMotherDir() const { return !fMother ? nullptr : dynamic_cast<TDirectory*>(fMother); }
169 virtual Int_t GetNbytesKeys() const { return 0; }
170 virtual Int_t GetNkeys() const { return 0; }
171 virtual Long64_t GetSeekDir() const { return 0; }
172 virtual Long64_t GetSeekParent() const { return 0; }
173 virtual Long64_t GetSeekKeys() const { return 0; }
174 virtual const char *GetPathStatic() const;
175 virtual const char *GetPath() const;
176 TUUID GetUUID() const {return fUUID;}
177 Bool_t IsFolder() const override { return kTRUE; }
178 virtual Bool_t IsModified() const { return kFALSE; }
179 virtual Bool_t IsWritable() const { return kFALSE; }
180 void ls(Option_t *option="") const override;
181 virtual TDirectory *mkdir(const char *name, const char *title="", Bool_t returnExistingDirectory = kFALSE);
182 virtual TFile *OpenFile(const char * /*name*/, Option_t * /*option*/ = "",
183 const char * /*ftitle*/ = "", Int_t /*compress*/ = 1,
184 Int_t /*netopt*/ = 0) {return nullptr;}
185 void Paint(Option_t *option="") override;
186 void Print(Option_t *option="") const override;
187 virtual void Purge(Short_t /*nkeep*/=1) {}
188 virtual void pwd() const;
189 virtual void ReadAll(Option_t * /*option*/="") {}
190 virtual Int_t ReadKeys(Bool_t /*forceRead*/=kTRUE) {return 0;}
191 virtual Int_t ReadTObject(TObject * /*obj*/, const char * /*keyname*/) {return 0;}
192 virtual TObject *Remove(TObject*);
193 void RecursiveRemove(TObject *obj) override;
194 virtual void rmdir(const char *name);
195 virtual void Save() {}
196 virtual Int_t SaveObjectAs(const TObject * /*obj*/, const char * /*filename*/="", Option_t * /*option*/="") const;
197 virtual void SaveSelf(Bool_t /*force*/ = kFALSE) {}
198 virtual void SetBufferSize(Int_t /* bufsize */) {}
199 virtual void SetModified() {}
200 virtual void SetMother(TObject *mother) {fMother = (TObject*)mother;}
201 void SetName(const char* newname) override;
202 virtual void SetTRefAction(TObject * /*ref*/, TObject * /*parent*/) {}
203 virtual void SetSeekDir(Long64_t) {}
204 virtual void SetWritable(Bool_t) {}
205 Int_t Sizeof() const override {return 0;}
206 virtual Int_t Write(const char * /*name*/=nullptr, Int_t /*opt*/=0, Int_t /*bufsize*/=0) override {return 0;}
207 virtual Int_t Write(const char * /*name*/=nullptr, Int_t /*opt*/=0, Int_t /*bufsize*/=0) const override {return 0;}
208 virtual Int_t WriteTObject(const TObject *obj, const char *name =nullptr, Option_t * /*option*/="", Int_t /*bufsize*/ =0);
209private:
210 Int_t WriteObject(void *obj, const char* name, Option_t *option="", Int_t bufsize=0); // Intentionally not implemented.
211public:
212 template <class T> inline Int_t WriteObject(const T* obj, const char* name, Option_t *option="", Int_t bufsize=0) // see TDirectory::WriteTObject or TDirectoryWriteObjectAny for explanation
213 {
214 return WriteObjectAny(obj, TClass::GetClass<T>(), name, option, bufsize);
215 }
216 virtual Int_t WriteObjectAny(const void *, const char * /*classname*/, const char * /*name*/, Option_t * /*option*/="", Int_t /*bufsize*/ =0) {return 0;}
217 virtual Int_t WriteObjectAny(const void *, const TClass * /*cl*/, const char * /*name*/, Option_t * /*option*/="", Int_t /*bufsize*/ =0) {return 0;}
218 virtual void WriteDirHeader() {}
219 virtual void WriteKeys() {}
220
221 static Bool_t Cd(const char *path);
222 static void DecodeNameCycle(const char *namecycle, char *name, Short_t &cycle, const size_t namesize = 0);
223 static void EncodeNameCycle(char *buffer, const char *name, Short_t cycle);
224
225 ClassDefOverride(TDirectory,5) //Describe directory structure in memory
226};
227
228#ifndef __CINT__
229#define gDirectory (TDirectory::CurrentDirectory())
230
231#elif defined(__MAKECINT__)
232// To properly handle the use of gDirectory in header files (in static declarations)
234#endif
235
236#endif
int Int_t
Definition: CPyCppyy.h:43
#define R__EXTERN
Definition: DllImport.h:27
#define b(i)
Definition: RSha256.hxx:100
const Bool_t kFALSE
Definition: RtypesCore.h:90
bool Bool_t
Definition: RtypesCore.h:61
short Short_t
Definition: RtypesCore.h:37
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassDefOverride(name, id)
Definition: Rtypes.h:326
#define gDirectory
Definition: TDirectory.h:229
char name[80]
Definition: TGX11.cxx:109
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
Small helper to keep current directory context.
Definition: TDirectory.h:47
TContext(TDirectory *previous, TDirectory *newCurrent)
Definition: TDirectory.h:61
std::atomic< bool > fDirectoryWait
Set to true during the destructor execution.
Definition: TDirectory.h:51
void CdNull()
Set the current directory to null.
Definition: TDirectory.cxx:125
TContext(TDirectory *newCurrent)
Definition: TDirectory.h:79
TContext & operator=(TContext &)=delete
~TContext()
Destructor.
Definition: TDirectory.cxx:135
TContext * fPrevious
Set to true if a TDirectory might still access this object.
Definition: TDirectory.h:52
std::atomic< bool > fActiveDestructor
Pointer to the previous current directory.
Definition: TDirectory.h:50
TContext(TContext &)=delete
Pointer to the next TContext in the implied list of context pointing to fPrevious.
TContext * fNext
Pointer to the next TContext in the implied list of context pointing to fPrevious.
Definition: TDirectory.h:53
std::atomic< TDirectory * > fDirectory
Definition: TDirectory.h:49
Describe directory structure in memory.
Definition: TDirectory.h:40
virtual Long64_t GetSeekDir() const
Definition: TDirectory.h:171
virtual TKey * GetKey(const char *, Short_t=9999) const
Definition: TDirectory.h:164
Bool_t cd1(const char *path)
flag to add histograms, graphs,etc to the directory
Definition: TDirectory.cxx:516
void Delete(const char *namecycle="") override
Delete Objects or/and keys in a directory.
Definition: TDirectory.cxx:652
virtual void Close(Option_t *option="")
Delete all objects from memory and directory structure itself.
Definition: TDirectory.cxx:585
virtual TKey * FindKeyAny(const char *) const
Definition: TDirectory.h:143
virtual TKey * FindKey(const char *) const
Definition: TDirectory.h:142
virtual void Save()
Definition: TDirectory.h:195
virtual void SetSeekDir(Long64_t)
Definition: TDirectory.h:203
TDirectory(const TDirectory &directory)=delete
virtual Int_t Write(const char *=nullptr, Int_t=0, Int_t=0) override
Write this object to the current directory.
Definition: TDirectory.h:206
virtual TList * GetList() const
Definition: TDirectory.h:165
virtual Int_t AppendKey(TKey *)
Definition: TDirectory.h:130
virtual TDirectory * GetDirectory(const char *namecycle, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory using apath.
Definition: TDirectory.cxx:401
Bool_t IsFolder() const override
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects).
Definition: TDirectory.h:177
virtual Int_t GetNkeys() const
Definition: TDirectory.h:170
virtual void WriteKeys()
Definition: TDirectory.h:219
virtual Int_t GetNbytesKeys() const
Definition: TDirectory.h:169
virtual const char * GetPath() const
Returns the full path of the directory.
Definition: TDirectory.cxx:988
std::atomic_flag fSpinLock
Counter delaying the TDirectory destructor from finishing.
Definition: TDirectory.h:102
virtual void ReadAll(Option_t *="")
Definition: TDirectory.h:189
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:806
virtual void * GetObjectUnchecked(const char *namecycle)
Return pointer to object identified by namecycle.
Definition: TDirectory.cxx:861
void Draw(Option_t *option="") override
Fill Graphics Structure and Paint.
Definition: TDirectory.cxx:708
virtual void DeleteAll(Option_t *option="")
Delete all objects from memory.
Definition: TDirectory.cxx:625
std::atomic< size_t > fContextPeg
Pointer to a list of TContext object pointing to this TDirectory.
Definition: TDirectory.h:101
virtual void SetModified()
Definition: TDirectory.h:199
static TDirectory *& CurrentDirectory()
Return the current directory for the current thread.
Definition: TDirectory.cxx:381
virtual void rmdir(const char *name)
Removes subdirectory from the directory When directory is deleted, all keys in all subdirectories wil...
void Copy(TObject &) const override
Copy this to obj.
Definition: TDirectory.h:137
static Bool_t AddDirectoryStatus()
Static function: see TDirectory::AddDirectory for more comments.
Definition: TDirectory.cxx:180
void FillFullPath(TString &buf) const
Recursive method to fill full path for directory.
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add objects like histograms, TGraph2D, etc in memory.
Definition: TDirectory.cxx:172
void CleanTargets()
Clean the pointers to this object (gDirectory, TContext, etc.).
Definition: TDirectory.cxx:253
void ls(Option_t *option="") const override
List Directory contents.
TObject * GetMother() const
Definition: TDirectory.h:167
virtual Int_t WriteObjectAny(const void *, const TClass *, const char *, Option_t *="", Int_t=0)
Definition: TDirectory.h:217
TContext * fContext
Buffer for GetPath() function.
Definition: TDirectory.h:99
T * Get(const char *namecycle)
See documentation of TDirectoryFile::Get(const char *namecycle)
Definition: TDirectory.h:150
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
Definition: TDirectory.cxx:191
Int_t WriteObject(const T *obj, const char *name, Option_t *option="", Int_t bufsize=0)
Definition: TDirectory.h:212
virtual void WriteDirHeader()
Definition: TDirectory.h:218
Int_t Sizeof() const override
Return size of the TNamed part of the TObject.
Definition: TDirectory.h:205
virtual TFile * GetFile() const
Definition: TDirectory.h:163
virtual TFile * OpenFile(const char *, Option_t *="", const char *="", Int_t=1, Int_t=0)
Definition: TDirectory.h:182
TDirectory()
Directory default constructor.
Definition: TDirectory.cxx:47
static void DecodeNameCycle(const char *namecycle, char *name, Short_t &cycle, const size_t namesize=0)
Decode a namecycle "aap;2" into name "aap" and cycle "2".
static Bool_t Cd(const char *path)
Change current directory to "path".
Definition: TDirectory.cxx:542
void Clear(Option_t *option="") override
Delete all objects from a Directory list.
Definition: TDirectory.cxx:573
virtual Int_t WriteTObject(const TObject *obj, const char *name=nullptr, Option_t *="", Int_t=0)
See TDirectoryFile::WriteTObject for details.
virtual void Add(TObject *obj, Bool_t replace=kFALSE)
Definition: TDirectory.h:129
virtual Int_t WriteObjectAny(const void *, const char *, const char *, Option_t *="", Int_t=0)
Definition: TDirectory.h:216
TObject * FindObject(const char *name) const override
Find object by name in the list of memory objects.
Definition: TDirectory.cxx:724
virtual void Build(TFile *motherFile=nullptr, TDirectory *motherDir=nullptr)
Definition: TDirectory.h:132
Int_t WriteObject(void *obj, const char *name, Option_t *option="", Int_t bufsize=0)
void Print(Option_t *option="") const override
Print all objects in the directory.
virtual ~TDirectory()
Destructor.
Definition: TDirectory.cxx:91
virtual Int_t ReadKeys(Bool_t=kTRUE)
Definition: TDirectory.h:190
static Bool_t Cd1(const char *path)
Change current directory to "path".
Definition: TDirectory.cxx:555
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:166
virtual Bool_t IsWritable() const
Definition: TDirectory.h:179
virtual void Purge(Short_t=1)
Definition: TDirectory.h:187
TDirectory * GetMotherDir() const
Definition: TDirectory.h:168
void UnregisterContext(TContext *ctxt)
UnRegister a TContext pointing to this TDirectory object.
virtual Int_t ReadTObject(TObject *, const char *)
Definition: TDirectory.h:191
virtual void SetWritable(Bool_t)
Definition: TDirectory.h:204
virtual Bool_t IsModified() const
Definition: TDirectory.h:178
virtual void SetTRefAction(TObject *, TObject *)
Definition: TDirectory.h:202
virtual void SaveSelf(Bool_t=kFALSE)
Definition: TDirectory.h:197
void RecursiveRemove(TObject *obj) override
Recursively remove object from a Directory.
virtual Int_t SaveObjectAs(const TObject *, const char *="", Option_t *="") const
Save object in filename, if filename is 0 or "", a file with "objectname.root" is created.
TString fPathBuffer
Definition: TDirectory.h:98
virtual TDirectory * mkdir(const char *name, const char *title="", Bool_t returnExistingDirectory=kFALSE)
Create a sub-directory "a" or a hierarchy of sub-directories "a/b/c/...".
void SetName(const char *newname) override
Set the name for directory If the directory name is changed after the directory was written once,...
void BuildDirectory(TFile *motherFile, TDirectory *motherDir)
Initialise directory to defaults.
Definition: TDirectory.cxx:238
virtual Int_t GetBufferSize() const
Definition: TDirectory.h:162
virtual Long64_t GetSeekParent() const
Definition: TDirectory.h:172
virtual void SetMother(TObject *mother)
Definition: TDirectory.h:200
virtual Long64_t GetSeekKeys() const
Definition: TDirectory.h:173
TUUID fUUID
Definition: TDirectory.h:97
TUUID GetUUID() const
Definition: TDirectory.h:176
static Bool_t fgAddDirectory
MSVC doesn't support = ATOMIC_FLAG_INIT;.
Definition: TDirectory.h:104
static void EncodeNameCycle(char *buffer, const char *name, Short_t cycle)
Encode the name and cycle into buffer like: "aap;2".
virtual const char * GetPathStatic() const
Returns the full path of the directory.
Definition: TDirectory.cxx:950
virtual TObject * FindObjectAnyFile(const char *) const
Definition: TDirectory.h:147
TObject * fMother
Definition: TDirectory.h:95
void Browse(TBrowser *b) override
Browse the content of the directory.
Definition: TDirectory.cxx:216
void GetObject(const char *namecycle, T *&ptr)
Definition: TDirectory.h:155
virtual void pwd() const
Print the path of the directory.
virtual void * GetObjectChecked(const char *namecycle, const char *classname)
See documentation of TDirectory::GetObjectCheck(const char *namecycle, const TClass *cl)
Definition: TDirectory.cxx:869
virtual TObject * CloneObject(const TObject *obj, Bool_t autoadd=kTRUE)
Clone an object.
Definition: TDirectory.cxx:329
virtual TObject * Remove(TObject *)
Remove an object from the in-memory list.
void RegisterContext(TContext *ctxt)
Register a TContext pointing to this TDirectory object.
TList * fList
Definition: TDirectory.h:96
virtual Int_t Write(const char *=nullptr, Int_t=0, Int_t=0) const override
Write this object to the current directory.
Definition: TDirectory.h:207
virtual void SetBufferSize(Int_t)
Definition: TDirectory.h:198
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
Definition: TDirectory.cxx:498
void Paint(Option_t *option="") override
Paint all objects in the directory.
virtual TObject * FindObjectAny(const char *name) const
Find object by name in the list of memory objects of the current directory or its sub-directories.
Definition: TDirectory.cxx:736
void operator=(const TDirectory &)=delete
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:28
A doubly linked list.
Definition: TList.h:44
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
Mother of all ROOT objects.
Definition: TObject.h:37
void MayNotUse(const char *method) const
Use this method to signal that a method (defined in a base class) may not be called in a derived clas...
Definition: TObject.cxx:944
Basic string class.
Definition: TString.h:131
This class defines a UUID (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDent...
Definition: TUUID.h:42
long long Long64_t
Definition: cpp_cppyy.h:13
double T(double x)
Definition: ChebyshevPol.h:34