TFileService

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#ifndef UtilAlgos_TFileService_h
#define UtilAlgos_TFileService_h
/* \class TFileService
 *
 * \author Luca Lista, INFN
 *
 * Modified to run properly in the multithreaded Framework.
 * Any modules that use this service in multithreaded mode
 * must be type "One" modules that declared a shared resource
 * of type TFileService::kSharedResource.
 *
 */

#include "CommonTools/Utils/interface/TFileDirectory.h"

#include <string>

class TDirectory;
class TFile;

namespace edm {
  class ActivityRegistry;
  class GlobalContext;
  class ModuleCallingContext;
  class ModuleDescription;
  class ParameterSet;
  class StreamContext;
}  // namespace edm

class TFileService {
public:
  /// constructor
  TFileService(const edm::ParameterSet &, edm::ActivityRegistry &);
  /// destructor
  ~TFileService();
  /// return opened TFile
  TFile &file() const { return *file_; }

  /// Hook for writing info into JR
  void afterBeginJob();

  TFileDirectory &tFileDirectory() { return tFileDirectory_; }

  // The next 6 functions do nothing more than forward function calls
  // to the TFileDirectory data member.

  // cd()s to requested directory and returns true (if it is not
  // able to cd, it throws exception).
  bool cd() const { return tFileDirectory_.cd(); }

  // returns a TDirectory pointer
  TDirectory *getBareDirectory(const std::string &subdir = "") const {
    return tFileDirectory_.getBareDirectory(subdir);
  }

  // reutrns a "T" pointer matched to objname
  template <typename T>
  T *getObject(const std::string &objname, const std::string &subdir = "") {
    return tFileDirectory_.getObject<T>(objname, subdir);
  }

  /// make new ROOT object
  template <typename T, typename... Args>
  T *make(const Args &...args) const {
    return tFileDirectory_.make<T>(args...);
  }

  /// create a new subdirectory
  TFileDirectory mkdir(const std::string &dir, const std::string &descr = "") {
    return tFileDirectory_.mkdir(dir, descr);
  }

  /// return the full path of the stored histograms
  std::string fullPath() const { return tFileDirectory_.fullPath(); }

  static const std::string kSharedResource;

private:
  static thread_local TFileDirectory tFileDirectory_;
  /// pointer to opened TFile
  TFile *file_;
  std::string fileName_;
  bool fileNameRecorded_;
  bool closeFileFast_;

  // set current directory according to module name and prepair to create directory
  void setDirectoryName(const edm::ModuleDescription &desc);
  void preModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &);
  void postModuleEvent(edm::StreamContext const &, edm::ModuleCallingContext const &);
  void preModuleGlobal(edm::GlobalContext const &, edm::ModuleCallingContext const &);
  void postModuleGlobal(edm::GlobalContext const &, edm::ModuleCallingContext const &);
};

namespace edm {
  namespace service {
    // This function is needed so that there will be only one instance
    // of this service per process when "subprocesses" are being used.
    inline bool isProcessWideService(TFileService const *) { return true; }
  }  // namespace service
}  // namespace edm
#endif