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 
28  /// make new ROOT object of type T, using constructor parameters
29  /// args. It will be made in the 'current directory', as
30  /// established with a call to 'cd'.
31  template <typename T, typename... ARGS>
32  T* make(ARGS... args) const;
33 
34  /// make and register a new ROOT object of type T, giving it the
35  /// specified name and title, using constructor parameters
36  /// 'args'. The type must be registerable, and must support naming
37  /// and titling.
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 
46  /// Create a new TFileDirectory, sharing the same TFile as this
47  /// one, but with an additional 'dir', and with 'path' being the
48  /// fullPath() of this one.
49  TFileDirectory mkdir(std::string const& dir, std::string const& descr = "");
50 
51  protected:
52  using Callback_t = std::function<void()>;
53 
54  /// Create a new TFileDirectory object.
55  TFileDirectory(std::string const& dir,
56  std::string const& descr,
57  TFile* file,
58  std::string const& path);
59 
60  void invokeCallbacks();
62 
63  /// Return the full pathname of the current directory, formed from
64  /// appending 'dir' to the end of 'path'.
65  std::string fullPath() const;
66 
67  TFile* file_;
70  bool requireCallback_{false};
71 
72  private:
74  /// Make the current directory be the one implied by the state of
75  /// this TFileDirectory.
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*
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 
116 /// emacs configuration
117 
118 /// Local Variables: -
119 /// mode: c++ -
120 /// c-basic-offset: 2 -
121 /// indent-tabs-mode: nil -
122 /// End: -
virtual ~TFileDirectory()=default
const XML_Char * name
Definition: expat.h:151
std::function< void()> Callback_t
const char * p
Definition: xmltok.h:285
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
const std::string path
Definition: plot_BEN.C:43
T * make(ARGS...args) const
TDirectory * dir
Definition: macro.C:5
TFile * file
Definition: cellShifts.C:17
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
double T
Definition: Xdiff_gwt.C:5
std::string fullPath() const
std::map< std::string, std::vector< Callback_t > > callbacks_
void cd() const
TFileDirectory(std::string const &dir, std::string const &descr, TFile *file, std::string const &path)
Create a new TFileDirectory object.
enum BeamMode string