Project

General

Profile

TFileDirectory.h

Kyle Knoepfel, 11/02/2017 09:06 AM

 
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

    
10
#include <functional>
11
#include <map>
12
#include <string>
13
#include <utility>
14

    
15
#include "art/Framework/Services/Optional/detail/RootDirectorySentry.h"
16
#include "TDirectory.h"
17

    
18
class TFile;
19
class TGraph;
20

    
21
namespace art {
22

    
23
  class TFileDirectory {
24
  public:
25

    
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, std::string const& title, ARGS... args) const;
43

    
44
    /// Create a new TFileDirectory, sharing the same TFile as this
45
    /// one, but with an additional 'dir', and with 'path' being the
46
    /// fullPath() of this one.
47
    TFileDirectory mkdir(std::string const& dir, std::string const& descr = "");
48

    
49
  protected:
50
    using Callback_t = std::function<void()>;
51

    
52
    /// Create a new TFileDirectory object.
53
    TFileDirectory(std::string const& dir,
54
                   std::string const& descr,
55
                   TFile* file,
56
                   std::string const& path);
57

    
58
    void invokeCallbacks();
59
    void registerCallback(Callback_t);
60

    
61
    /// Return the full pathname of the current directory, formed from
62
    /// appending 'dir' to the end of 'path'.
63
    std::string fullPath() const;
64

    
65
    TFile* file_;
66
    std::string dir_;
67
    std::string descr_;
68
    std::string path_;
69
    bool requireCallback_{false};
70

    
71
  private:
72
    /// Make the current directory be the one implied by the state of
73
    /// this TFileDirectory.
74
    void cd() const;
75
    std::map<std::string, std::vector<Callback_t>> callbacks_{};
76
  };
77

    
78
  template <typename T, typename... ARGS>
79
  T*
80
  TFileDirectory::make(ARGS... args) const
81
  {
82
    detail::RootDirectorySentry sentry;
83
    cd();
84
    return new T(args...);
85
  }
86

    
87
  template <typename T, typename... ARGS>
88
  T*
89
  TFileDirectory::makeAndRegister(char const* name,
90
                                  char const* title,
91
                                  ARGS... args) const
92
  {
93
    detail::RootDirectorySentry sentry;
94
    cd();
95
    T* p = new T(args...);
96
    p->SetName(name);
97
    p->SetTitle(title);
98
    gDirectory->Append(p);
99
    return p;
100
  }
101

    
102
  template <typename T, typename... ARGS>
103
  T*
104
  TFileDirectory::makeAndRegister(std::string const& name,
105
                                  std::string const& title,
106
                                  ARGS... args) const
107
  {
108
    return makeAndRegister(name.c_str(),
109
                           title.c_str(),
110
                           args...);
111
  }
112

    
113
}
114

    
115
#endif /* art_Framework_Services_Optional_TFileDirectory_h */
116

    
117
/// emacs configuration
118

    
119
/// Local Variables: -
120
/// mode: c++ -
121
/// c-basic-offset: 2 -
122
/// indent-tabs-mode: nil -
123
/// End: -