CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/PhysicsTools/MVATrainer/src/TrainerMonitoring.cc

Go to the documentation of this file.
00001 #include <string>
00002 #include <vector>
00003 #include <memory>
00004 #include <ostream>
00005 
00006 #include <boost/shared_ptr.hpp>
00007 
00008 #include <TFile.h>
00009 #include <TDirectory.h>
00010 #include <TObject.h>
00011 
00012 #include "FWCore/Utilities/interface/Exception.h"
00013 
00014 #include "PhysicsTools/MVATrainer/interface/TrainerMonitoring.h"
00015 
00016 namespace { // anonymous
00017 
00018 class ROOTContextSentinel {
00019     public:
00020         ROOTContextSentinel() : dir(gDirectory), file(gFile) {}
00021         ~ROOTContextSentinel() { gDirectory = dir; gFile = file; }
00022 
00023     private:
00024         TDirectory      *dir;
00025         TFile           *file;
00026 };
00027 
00028 } // anonymous namespace
00029 
00030 namespace PhysicsTools {
00031 
00032 TrainerMonitoring::TrainerMonitoring(const std::string &fileName)
00033 {
00034         ROOTContextSentinel ctx;
00035 
00036         rootFile.reset(TFile::Open(fileName.c_str(), "RECREATE"));
00037 }
00038 
00039 TrainerMonitoring::~TrainerMonitoring()
00040 {
00041 }
00042 
00043 void TrainerMonitoring::write()
00044 {
00045         ROOTContextSentinel ctx;
00046 
00047         typedef std::map<std::string, boost::shared_ptr<Module> > Map;
00048         for(Map::const_iterator iter = modules.begin();
00049             iter != modules.end(); ++iter) {
00050                 rootFile->cd();
00051                 TDirectory *dir = rootFile->mkdir(iter->first.c_str());
00052                 dir->cd();
00053                 iter->second->write(dir);
00054         }
00055 }
00056 
00057 TrainerMonitoring::Module::Module()
00058 {
00059 }
00060 
00061 TrainerMonitoring::Module::~Module()
00062 {
00063 }
00064 
00065 void TrainerMonitoring::Module::write(TDirectory *dir)
00066 {
00067         typedef std::map<std::string, boost::shared_ptr<Object> > Map;
00068         for(Map::const_iterator iter = data.begin();
00069             iter != data.end(); ++iter)
00070                 iter->second->write(dir);
00071 }
00072 
00073 void TrainerMonitoring::Module::add(Object *object)
00074 {
00075         boost::shared_ptr<Object> ptr(object);
00076         if (!data.insert(std::make_pair(object->getName(), ptr)).second)
00077                 throw cms::Exception("DuplicateNode")
00078                         << "Node \"" << object->getName() << "\" already"
00079                            " exists." << std::endl;
00080 }
00081 
00082 TrainerMonitoring::Module *TrainerMonitoring::book(const std::string &name)
00083 {
00084         boost::shared_ptr<Module> module(new Module);
00085         if (!modules.insert(std::make_pair(name, module)).second)
00086                 throw cms::Exception("DuplicateModule")
00087                         << "Module \"" << name << "\" already"
00088                            " exists." << std::endl;
00089 
00090         return module.get();
00091 }
00092 
00093 } // namespace PhysicsTools