CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrainerMonitoring.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_MVATrainer_TrainerMonitoring_h
2 #define PhysicsTools_MVATrainer_TrainerMonitoring_h
3 
4 #include <string>
5 #include <vector>
6 #include <memory>
7 #include <map>
8 
9 #include <boost/type_traits.hpp>
10 #include <boost/mpl/bool.hpp>
11 #include <boost/mpl/or.hpp>
12 
13 #include <TFile.h>
14 #include <TTree.h>
15 #include <TClass.h>
16 #include <TDirectory.h>
17 #include <TObject.h>
18 
19 namespace PhysicsTools {
20 
22  public:
25 
26  void write();
27  void close();
28 
29  public:
30  class Module;
31 
32  protected:
33  friend class Module;
34 
35  class Object;
36 
37  template<typename T>
38  class RootObject;
39 
40  public:
41  class Module {
42  public:
43  ~Module();
44 
45  template<typename T>
46  void book(const std::string &name, T *object)
47  { add(new RootObject<T>(name, object)); }
48 
49  template<typename T>
50  T *book(const std::string &name) {
51  T *obj = new T();
52  this->reg(name, obj);
53  return obj;
54  }
55 
56  template<typename T, typename T1>
57  T *book(const std::string &name, T1 a1) {
58  T *obj = new T(a1);
59  this->reg(name, obj);
60  return obj;
61  }
62 
63  template<typename T, typename T1, typename T2>
64  T *book(const std::string &name, T1 a1, T2 a2) {
65  T *obj = new T(a1, a2);
66  this->reg(name, obj);
67  return obj;
68  }
69 
70  template<typename T, typename T1, typename T2, typename T3>
71  T *book(const std::string &name, T1 a1, T2 a2, T3 a3) {
72  T *obj = new T(a1, a2, a3);
73  this->reg(name, obj);
74  return obj;
75  }
76 
77  template<typename T, typename T1, typename T2, typename T3,
78  typename T4>
79  T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4) {
80  T *obj = new T(a1, a2, a3, a4);
81  this->reg(name, obj);
82  return obj;
83  }
84 
85  template<typename T, typename T1, typename T2, typename T3,
86  typename T4, typename T5>
87  T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) {
88  T *obj = new T(a1, a2, a3, a4, a5);
89  this->reg(name, obj);
90  return obj;
91  }
92 
93  template<typename T, typename T1, typename T2, typename T3,
94  typename T4, typename T5, typename T6>
95  T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) {
96  T *obj = new T(a1, a2, a3, a4, a5, a6);
97  this->reg(name, obj);
98  return obj;
99  }
100 
101  template<typename T, typename T1, typename T2, typename T3,
102  typename T4, typename T5, typename T6, typename T7>
103  T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) {
104  T *obj = new T(a1, a2, a3, a4, a5, a6, a7);
105  this->reg(name, obj);
106  return obj;
107  }
108 
109  template<typename T, typename T1, typename T2, typename T3,
110  typename T4, typename T5, typename T6, typename T7,
111  typename T8>
112  T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8) {
113  T *obj = new T(a1, a2, a3, a4, a5, a6, a7, a8);
114  this->reg(name, obj);
115  return obj;
116  }
117 
118  template<typename T, typename T1, typename T2, typename T3,
119  typename T4, typename T5, typename T6, typename T7,
120  typename T8, typename T9>
121  T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8, T9 a9) {
122  T *obj = new T(a1, a2, a3, a4, a5, a6, a7, a8, a9);
123  this->reg(name, obj);
124  return obj;
125  }
126 
127  protected:
128  friend class TrainerMonitoring;
129 
130  Module();
131 
132  void write(TDirectory *dir);
133 
134  private:
135  void add(Object *object);
136 
137  template<typename T>
138  inline void reg(const std::string &name, T *object);
139 
140  TDirectory *dir;
141  std::map<std::string, std::shared_ptr<Object> > data;
142  };
143 
144  Module *book(const std::string &name);
145 
146  protected:
147  class Object {
148  public:
149  Object(const std::string &name) : name(name) {}
150  virtual ~Object() {}
151 
152  const std::string &getName() const { return name; }
153 
154  virtual void write(TDirectory *dir) = 0;
155 
156  private:
158  };
159 
160  template<typename T>
161  class RootObject : public Object {
162  public:
163  RootObject(const std::string &name, T *object) :
164  Object(name), object(object) {}
165  virtual ~RootObject() {}
166 
167  virtual void write(TDirectory *dir)
168  {
169  dir->WriteObjectAny(object.get(),
170  TClass::GetClass(typeid(T)),
171  getName().c_str());
172  }
173 
174  private:
175  std::unique_ptr<T> object;
176  };
177 
178  private:
179  std::unique_ptr<TFile> rootFile;
180  std::map<std::string, std::shared_ptr<Module> > modules;
181 };
182 
183 namespace helper {
184  template<typename T, bool B>
185  void trainerMonitoringRootClear(T *object, const boost::mpl::bool_<B>&)
186  {}
187 
188  template<typename T>
189  void trainerMonitoringRootClear(T *object, const boost::mpl::true_&)
190  { object->SetDirectory(0); }
191 }
192 
193 template<typename T>
194 inline void TrainerMonitoring::Module::reg(const std::string &name, T *object)
195 {
197  boost::mpl::or_<
198  boost::is_base_of<TH1, T>,
199  boost::is_base_of<TTree, T> >());
200  this->book(name, object);
201 }
202 
203 } // namespace PhysicsTools
204 
205 #endif // PhysicsTools_MVATrainer_TrainerMonitoring_h
const std::string & getName() const
T * book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
std::unique_ptr< TFile > rootFile
T * book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4)
T * book(const std::string &name)
T * book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8)
T * book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
void trainerMonitoringRootClear(T *object, const boost::mpl::bool_< B > &)
RootObject(const std::string &name, T *object)
Module * book(const std::string &name)
std::map< std::string, std::shared_ptr< Object > > data
T * book(const std::string &name, T1 a1, T2 a2)
void book(const std::string &name, T *object)
virtual void write(TDirectory *dir)=0
T * book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
TrainerMonitoring(const std::string &fileName)
T * book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8, T9 a9)
void reg(const std::string &name, T *object)
dbl *** dir
Definition: mlp_gen.cc:35
std::map< std::string, std::shared_ptr< Module > > modules
long double T
T * book(const std::string &name, T1 a1)
T * book(const std::string &name, T1 a1, T2 a2, T3 a3)