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