CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/PhysicsTools/MVATrainer/interface/TrainerMonitoring.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_MVATrainer_TrainerMonitoring_h
00002 #define PhysicsTools_MVATrainer_TrainerMonitoring_h
00003 
00004 #include <string>
00005 #include <vector>
00006 #include <memory>
00007 #include <map>
00008 
00009 #include <boost/shared_ptr.hpp>
00010 #include <boost/type_traits.hpp>
00011 #include <boost/mpl/bool.hpp>
00012 #include <boost/mpl/or.hpp>
00013 
00014 #include <TFile.h>
00015 #include <TTree.h>
00016 #include <TClass.h>
00017 #include <TDirectory.h>
00018 #include <TObject.h>
00019 
00020 namespace PhysicsTools {
00021 
00022 class TrainerMonitoring {
00023     public:
00024         TrainerMonitoring(const std::string &fileName);
00025         ~TrainerMonitoring();
00026 
00027         void write();
00028         void close();
00029 
00030     public:
00031         class Module;
00032 
00033     protected:
00034         friend class Module;
00035 
00036         class Object;
00037 
00038         template<typename T>
00039         class RootObject;
00040 
00041     public:
00042         class Module {
00043             public:
00044                 ~Module();
00045 
00046                 template<typename T>
00047                 void book(const std::string &name, T *object)
00048                 { add(new RootObject<T>(name, object)); }
00049 
00050                 template<typename T>
00051                 T *book(const std::string &name) {
00052                         T *obj = new T();
00053                         this->reg(name, obj);
00054                         return obj;
00055                 }
00056 
00057                 template<typename T, typename T1>
00058                 T *book(const std::string &name, T1 a1) {
00059                         T *obj = new T(a1);
00060                         this->reg(name, obj);
00061                         return obj;
00062                 }
00063 
00064                 template<typename T, typename T1, typename T2>
00065                 T *book(const std::string &name, T1 a1, T2 a2) {
00066                         T *obj = new T(a1, a2);
00067                         this->reg(name, obj);
00068                         return obj;
00069                 }
00070 
00071                 template<typename T, typename T1, typename T2, typename T3>
00072                 T *book(const std::string &name, T1 a1, T2 a2, T3 a3) {
00073                         T *obj = new T(a1, a2, a3);
00074                         this->reg(name, obj);
00075                         return obj;
00076                 }
00077 
00078                 template<typename T, typename T1, typename T2, typename T3,
00079                          typename T4>
00080                 T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4) {
00081                         T *obj = new T(a1, a2, a3, a4);
00082                         this->reg(name, obj);
00083                         return obj;
00084                 }
00085 
00086                 template<typename T, typename T1, typename T2, typename T3,
00087                          typename T4, typename T5>
00088                 T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) {
00089                         T *obj = new T(a1, a2, a3, a4, a5);
00090                         this->reg(name, obj);
00091                         return obj;
00092                 }
00093 
00094                 template<typename T, typename T1, typename T2, typename T3,
00095                          typename T4, typename T5, typename T6>
00096                 T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) {
00097                         T *obj = new T(a1, a2, a3, a4, a5, a6);
00098                         this->reg(name, obj);
00099                         return obj;
00100                 }
00101 
00102                 template<typename T, typename T1, typename T2, typename T3,
00103                          typename T4, typename T5, typename T6, typename T7>
00104                 T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7) {
00105                         T *obj = new T(a1, a2, a3, a4, a5, a6, a7);
00106                         this->reg(name, obj);
00107                         return obj;
00108                 }
00109 
00110                 template<typename T, typename T1, typename T2, typename T3,
00111                          typename T4, typename T5, typename T6, typename T7,
00112                          typename T8>
00113                 T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8) {
00114                         T *obj = new T(a1, a2, a3, a4, a5, a6, a7, a8);
00115                         this->reg(name, obj);
00116                         return obj;
00117                 }
00118 
00119                 template<typename T, typename T1, typename T2, typename T3,
00120                          typename T4, typename T5, typename T6, typename T7,
00121                          typename T8, typename T9>
00122                 T *book(const std::string &name, T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7, T8 a8, T9 a9) {
00123                         T *obj = new T(a1, a2, a3, a4, a5, a6, a7, a8, a9);
00124                         this->reg(name, obj);
00125                         return obj;
00126                 }
00127 
00128             protected:
00129                 friend class TrainerMonitoring;
00130 
00131                 Module();
00132 
00133                 void write(TDirectory *dir);
00134 
00135             private:
00136                 void add(Object *object);
00137 
00138                 template<typename T>
00139                 inline void reg(const std::string &name, T *object);
00140 
00141                 TDirectory                                              *dir;
00142                 std::map<std::string, boost::shared_ptr<Object> >       data;
00143         };
00144 
00145         Module *book(const std::string &name);
00146 
00147     protected:
00148         class Object {
00149             public:
00150                 Object(const std::string &name) : name(name) {}
00151                 virtual ~Object() {}
00152 
00153                 const std::string &getName() const { return name; }
00154 
00155                 virtual void write(TDirectory *dir) = 0;
00156 
00157             private:
00158                 std::string     name;
00159         };
00160 
00161         template<typename T>
00162         class RootObject : public Object {
00163             public:
00164                 RootObject(const std::string &name, T *object) :
00165                         Object(name), object(object) {}
00166                 virtual ~RootObject() {}
00167 
00168                 virtual void write(TDirectory *dir)
00169                 {
00170                         dir->WriteObjectAny(object.get(),
00171                                             TClass::GetClass(typeid(T)),
00172                                             getName().c_str());
00173                 }
00174 
00175             private:
00176                 std::auto_ptr<T>        object;
00177         };
00178 
00179     private:
00180         std::auto_ptr<TFile>                                    rootFile;
00181         std::map<std::string, boost::shared_ptr<Module> >       modules;
00182 };
00183 
00184 namespace helper {
00185         template<typename T, bool B>
00186         void trainerMonitoringRootClear(T *object, const boost::mpl::bool_<B>&)
00187         {}
00188 
00189         template<typename T>
00190         void trainerMonitoringRootClear(T *object, const boost::mpl::true_&)
00191         { object->SetDirectory(0); }
00192 }
00193 
00194 template<typename T>
00195 inline void TrainerMonitoring::Module::reg(const std::string &name, T *object)
00196 {
00197         helper::trainerMonitoringRootClear(object,
00198                         boost::mpl::or_<
00199                                 boost::is_base_of<TH1, T>,
00200                                 boost::is_base_of<TTree, T> >());
00201         this->book(name, object);
00202 }
00203 
00204 } // namespace PhysicsTools
00205 
00206 #endif // PhysicsTools_MVATrainer_TrainerMonitoring_h