Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:18

0001 #ifndef Utils_TFileDirectory_h
0002 #define Utils_TFileDirectory_h
0003 /* \class TFileDirectory
0004  *
0005  * \author Luca Lista, INFN
0006  *
0007  */
0008 #include <string>
0009 #include "FWCore/Utilities/interface/Exception.h"
0010 #include "CommonTools/Utils/interface/TH1AddDirectorySentry.h"
0011 #include "TFile.h"
0012 #include "TDirectory.h"
0013 #include "TClass.h"
0014 #include "TH1.h"
0015 
0016 namespace fwlite {
0017   class TFileService;
0018 }
0019 
0020 class TFileService;
0021 class TFile;
0022 class TDirectory;
0023 
0024 class TFileDirectory {
0025 public:
0026   TFileDirectory() : file_(nullptr), dir_(), descr_(), path_() {}
0027 
0028   /// descructor
0029   virtual ~TFileDirectory() {}
0030 
0031   // cd()s to requested directory and returns true (if it is not
0032   // able to cd, it throws exception).
0033   bool cd() const;
0034 
0035   // returns a TDirectory pointer
0036   TDirectory *getBareDirectory(const std::string &subdir = "") const;
0037 
0038   // reutrns a "T" pointer matched to objname
0039   template <typename T>
0040   T *getObject(const std::string &objname, const std::string &subdir = "") {
0041     TObject *objPtr = _getObj(objname, subdir);
0042     // Ok, we've got it.  Let's see if it's a histogram
0043     T *retval = dynamic_cast<T *>(objPtr);
0044     if (!retval) {
0045       // object isn't a of class T
0046       throw cms::Exception("ObjectNotCorrectlyTyped") << "Object named " << objname << " is not of correct type";
0047     }
0048     return retval;
0049   }
0050 
0051   /// make new ROOT object
0052   template <typename T, typename... Args>
0053   T *make(const Args &...args) const {
0054     TDirectory *d = _cd();
0055     T *t = new T(args...);
0056     ROOT::DirAutoAdd_t func = T::Class()->GetDirectoryAutoAdd();
0057     if (func) {
0058       TH1AddDirectorySentry sentry;
0059       func(t, d);
0060     } else {
0061       d->Append(t);
0062     }
0063     return t;
0064   }
0065 
0066   /// create a new subdirectory
0067   TFileDirectory mkdir(const std::string &dir, const std::string &descr = "");
0068   /// return the full path of the stored histograms
0069   std::string fullPath() const;
0070 
0071 private:
0072   TObject *_getObj(const std::string &objname, const std::string &subdir = "") const;
0073 
0074   TDirectory *_cd(const std::string &subdir = "", bool createNeededDirectories = true) const;
0075 
0076   // it's completely insane that this needs to be const since
0077   // 'mkdir' clearly changes things, but that's the way the cookie
0078   // crumbles...
0079   TDirectory *_mkdir(TDirectory *dirPtr, const std::string &dir, const std::string &description) const;
0080 
0081   TFileDirectory(const std::string &dir, const std::string &descr, TFile *file, const std::string &path)
0082       : file_(file), dir_(dir), descr_(descr), path_(path) {}
0083   friend class TFileService;
0084   friend class fwlite::TFileService;
0085   TFile *file_;
0086   std::string dir_, descr_, path_;
0087 };
0088 
0089 #endif