LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
TFileDirectory.h
Go to the documentation of this file.
1 #ifndef art_Framework_Services_Optional_TFileDirectory_h
2 #define art_Framework_Services_Optional_TFileDirectory_h
3 
4 /* class TFileDirectory
5  *
6  * Original author: Luca Lista, INFN
7  *
8  */
9 
11 
12 #include "TDirectory.h"
13 
14 class TFile;
15 class TGraph;
16 
17 #include <functional>
18 #include <map>
19 #include <string>
20 #include <utility>
21 
22 namespace art {
23 
25  public:
26  virtual ~TFileDirectory() = default;
27 
31  template <typename T, typename... ARGS>
32  T* make(ARGS... args) const;
33 
38  template <typename T, typename... ARGS>
39  T* makeAndRegister(char const* name, char const* title, ARGS... args) const;
40 
41  template <typename T, typename... ARGS>
42  T* makeAndRegister(std::string const& name,
43  std::string const& title,
44  ARGS... args) const;
45 
49  TFileDirectory mkdir(std::string const& dir, std::string const& descr = "");
50 
51  protected:
52  using Callback_t = std::function<void()>;
53 
55  TFileDirectory(std::string const& dir,
56  std::string const& descr,
57  TFile* file,
58  std::string const& path);
59 
60  void invokeCallbacks();
62 
65  std::string fullPath() const;
66 
67  TFile* file_;
68  std::string dir_;
69  std::string descr_;
70  bool requireCallback_{false};
71 
72  private:
73  std::string path_;
76  void cd() const;
77  std::map<std::string, std::vector<Callback_t>> callbacks_{};
78  };
79 
80  template <typename T, typename... ARGS>
81  T*
82  TFileDirectory::make(ARGS... args) const
83  {
85  cd();
86  return new T(args...);
87  }
88 
89  template <typename T, typename... ARGS>
90  T*
92  char const* title,
93  ARGS... args) const
94  {
96  cd();
97  T* p = new T(args...);
98  p->SetName(name);
99  p->SetTitle(title);
100  gDirectory->Append(p);
101  return p;
102  }
103 
104  template <typename T, typename... ARGS>
105  T*
106  TFileDirectory::makeAndRegister(std::string const& name,
107  std::string const& title,
108  ARGS... args) const
109  {
110  return makeAndRegister(name.c_str(), title.c_str(), args...);
111  }
112 }
113 
114 #endif /* art_Framework_Services_Optional_TFileDirectory_h */
115 
117 
virtual ~TFileDirectory()=default
std::function< void()> Callback_t
TFileDirectory mkdir(std::string const &dir, std::string const &descr="")
void registerCallback(Callback_t)
T * makeAndRegister(char const *name, char const *title, ARGS...args) const
T * make(ARGS...args) const
TDirectory * dir
Definition: macro.C:5
TFile * file
HLT enums.
std::string fullPath() const
std::map< std::string, std::vector< Callback_t > > callbacks_
TFileDirectory(std::string const &dir, std::string const &descr, TFile *file, std::string const &path)
Create a new TFileDirectory object.