CMS 3D CMS Logo

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
writedatasetfile.args
args
Definition: writedatasetfile.py:18
TFileDirectory::mkdir
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
Definition: TFileDirectory.cc:112
TFileDirectory::~TFileDirectory
virtual ~TFileDirectory()
descructor
Definition: TFileDirectory.h:29
TFileDirectory::fullPath
std::string fullPath() const
return the full path of the stored histograms
Definition: TFileDirectory.cc:110
TFileDirectory::dir_
std::string dir_
Definition: TFileDirectory.h:86
fwlite
Definition: TFileDirectory.h:16
TFileDirectory::make
T * make(const Args &... args) const
make new ROOT object
Definition: TFileDirectory.h:53
edmLumisInFiles.description
description
Definition: edmLumisInFiles.py:11
TFileDirectory::path_
std::string path_
Definition: TFileDirectory.h:86
TFileDirectory::cd
bool cd() const
Definition: TFileDirectory.cc:12
TFileDirectory::getObject
T * getObject(const std::string &objname, const std::string &subdir="")
Definition: TFileDirectory.h:40
HLT_2018_cff.Class
Class
Definition: HLT_2018_cff.py:7110
TFileDirectory
Definition: TFileDirectory.h:24
TFileDirectory::file_
TFile * file_
Definition: TFileDirectory.h:85
TFileDirectory::TFileDirectory
TFileDirectory()
Definition: TFileDirectory.h:26
TFileDirectory::descr_
std::string descr_
Definition: TFileDirectory.h:86
TFileService
Definition: TFileService.h:30
OrderedSet.t
t
Definition: OrderedSet.py:90
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TFileDirectory::_getObj
TObject * _getObj(const std::string &objname, const std::string &subdir="") const
Definition: TFileDirectory.cc:97
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
TFileDirectory::getBareDirectory
TDirectory * getBareDirectory(const std::string &subdir="") const
Definition: TFileDirectory.cc:10
TrackCollections2monitor_cff.func
func
Definition: TrackCollections2monitor_cff.py:359
TFileDirectory::_cd
TDirectory * _cd(const std::string &subdir="", bool createNeededDirectories=true) const
Definition: TFileDirectory.cc:17
T
long double T
Definition: Basic3DVectorLD.h:48
TH1AddDirectorySentry.h
Exception
Definition: hltDiff.cc:246
TFileDirectory::TFileDirectory
TFileDirectory(const std::string &dir, const std::string &descr, TFile *file, const std::string &path)
Definition: TFileDirectory.h:81
TFileDirectory::_mkdir
TDirectory * _mkdir(TDirectory *dirPtr, const std::string &dir, const std::string &description) const
Definition: TFileDirectory.cc:69
fwlite::TFileService
Definition: TFileService.h:12
Exception.h
ztail.d
d
Definition: ztail.py:151
castor_dqm_sourceclient_file_cfg.path
path
Definition: castor_dqm_sourceclient_file_cfg.py:37
TH1AddDirectorySentry
Definition: TH1AddDirectorySentry.h:24
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23