CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TFileDirectory.h
Go to the documentation of this file.
1 #ifndef Utils_TFileDirectory_h
2 #define Utils_TFileDirectory_h
3 /* \class TFileDirectory
4  *
5  * \author Luca Lista, INFN
6  *
7  */
8 #include <string>
11 #include "TFile.h"
12 #include "TDirectory.h"
13 #include "TClass.h"
14 #include "TH1.h"
15 
16 namespace fwlite {
17  class TFileService;
18 }
19 
20 class TFileService;
21 class TFile;
22 class TDirectory;
23 
25 public:
26  TFileDirectory() : file_(nullptr), dir_(), descr_(), path_() {}
27 
29  virtual ~TFileDirectory() {}
30 
31  // cd()s to requested directory and returns true (if it is not
32  // able to cd, it throws exception).
33  bool cd() const;
34 
35  // returns a TDirectory pointer
36  TDirectory *getBareDirectory(const std::string &subdir = "") const;
37 
38  // reutrns a "T" pointer matched to objname
39  template <typename T>
40  T *getObject(const std::string &objname, const std::string &subdir = "") {
41  TObject *objPtr = _getObj(objname, subdir);
42  // Ok, we've got it. Let's see if it's a histogram
43  T *retval = dynamic_cast<T *>(objPtr);
44  if (!retval) {
45  // object isn't a of class T
46  throw cms::Exception("ObjectNotCorrectlyTyped") << "Object named " << objname << " is not of correct type";
47  }
48  return retval;
49  }
50 
52  template <typename T, typename... Args>
53  T *make(const Args &...args) const {
54  TDirectory *d = _cd();
55  T *t = new T(args...);
56  ROOT::DirAutoAdd_t func = T::Class()->GetDirectoryAutoAdd();
57  if (func) {
58  TH1AddDirectorySentry sentry;
59  func(t, d);
60  } else {
61  d->Append(t);
62  }
63  return t;
64  }
65 
67  TFileDirectory mkdir(const std::string &dir, const std::string &descr = "");
69  std::string fullPath() const;
70 
71 private:
72  TObject *_getObj(const std::string &objname, const std::string &subdir = "") const;
73 
74  TDirectory *_cd(const std::string &subdir = "", bool createNeededDirectories = true) const;
75 
76  // it's completely insane that this needs to be const since
77  // 'mkdir' clearly changes things, but that's the way the cookie
78  // crumbles...
79  TDirectory *_mkdir(TDirectory *dirPtr, const std::string &dir, const std::string &description) const;
80 
81  TFileDirectory(const std::string &dir, const std::string &descr, TFile *file, const std::string &path)
82  : file_(file), dir_(dir), descr_(descr), path_(path) {}
83  friend class TFileService;
84  friend class fwlite::TFileService;
85  TFile *file_;
87 };
88 
89 #endif
TFileDirectory(const std::string &dir, const std::string &descr, TFile *file, const std::string &path)
TDirectory * getBareDirectory(const std::string &subdir="") const
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t Func __host__ __device__ V int Func func
TDirectory * _cd(const std::string &subdir="", bool createNeededDirectories=true) const
TObject * _getObj(const std::string &objname, const std::string &subdir="") const
tuple d
Definition: ztail.py:151
virtual ~TFileDirectory()
descructor
std::string path_
bool cd() const
TDirectory * _mkdir(TDirectory *dirPtr, const std::string &dir, const std::string &description) const
T * getObject(const std::string &objname, const std::string &subdir="")
T * make(const Args &...args) const
make new ROOT object
std::string fullPath() const
return the full path of the stored histograms
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
std::string dir_
long double T
std::string descr_