Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef UtilAlgos_TFileService_h
0002 #define UtilAlgos_TFileService_h
0003 /* \class TFileService
0004  *
0005  * \author Luca Lista, INFN
0006  *
0007  * Modified to run properly in the multithreaded Framework.
0008  * Any modules that use this service in multithreaded mode
0009  * must be type "One" modules that declared a shared resource
0010  * of type TFileService::kSharedResource.
0011  *
0012  */
0013 
0014 #include "CommonTools/Utils/interface/TFileDirectory.h"
0015 
0016 #include <string>
0017 
0018 class TDirectory;
0019 class TFile;
0020 
0021 namespace edm {
0022   class ActivityRegistry;
0023   class GlobalContext;
0024   class ModuleCallingContext;
0025   class ModuleDescription;
0026   class ParameterSet;
0027   class StreamContext;
0028 }  // namespace edm
0029 
0030 class TFileService {
0031 public:
0032   /// constructor
0033   TFileService(const edm::ParameterSet &, edm::ActivityRegistry &);
0034   /// destructor
0035   ~TFileService();
0036   /// return opened TFile
0037   TFile &file() const { return *file_; }
0038 
0039   /// Hook for writing info into JR
0040   void afterBeginJob();
0041 
0042   TFileDirectory &tFileDirectory() { return tFileDirectory_; }
0043 
0044   // The next 6 functions do nothing more than forward function calls
0045   // to the TFileDirectory data member.
0046 
0047   // cd()s to requested directory and returns true (if it is not
0048   // able to cd, it throws exception).
0049   bool cd() const { return tFileDirectory_.cd(); }
0050 
0051   // returns a TDirectory pointer
0052   TDirectory *getBareDirectory(const std::string &subdir = "") const {
0053     return tFileDirectory_.getBareDirectory(subdir);
0054   }
0055 
0056   // reutrns a "T" pointer matched to objname
0057   template <typename T>
0058   T *getObject(const std::string &objname, const std::string &subdir = "") {
0059     return tFileDirectory_.getObject<T>(objname, subdir);
0060   }
0061 
0062   /// make new ROOT object
0063   template <typename T, typename... Args>
0064   T *make(const Args &...args) const {
0065     return tFileDirectory_.make<T>(args...);
0066   }
0067 
0068   /// create a new subdirectory
0069   TFileDirectory mkdir(const std::string &dir, const std::string &descr = "") {
0070     return tFileDirectory_.mkdir(dir, descr);
0071   }
0072 
0073   /// return the full path of the stored histograms
0074   std::string fullPath() const { return tFileDirectory_.fullPath(); }
0075 
0076   static const std::string kSharedResource;
0077 
0078 private:
0079   static thread_local TFileDirectory tFileDirectory_;
0080   /// pointer to opened TFile
0081   TFile *file_;
0082   std::string fileName_;
0083   bool fileNameRecorded_;
0084   bool closeFileFast_;
0085 
0086   // set current directory according to module name and prepair to create directory
0087   void setDirectoryName(const edm::ModuleDescription &desc);
0088   void preModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &);
0089   void postModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &);
0090   void preModuleGlobal(edm::GlobalContext const &, edm::ModuleCallingContext const &);
0091   void postModuleGlobal(edm::GlobalContext const &, edm::ModuleCallingContext const &);
0092 };
0093 
0094 namespace edm {
0095   namespace service {
0096     // This function is needed so that there will be only one instance
0097     // of this service per process when "subprocesses" are being used.
0098     inline bool isProcessWideService(TFileService const *) { return true; }
0099   }  // namespace service
0100 }  // namespace edm
0101 #endif