CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/PhysicsTools/MVATrainer/src/MVATrainerContainerSave.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <memory>
00003 #include <vector>
00004 #include <string>
00005 
00006 #include "FWCore/Utilities/interface/Exception.h"
00007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00008 #include "FWCore/Framework/interface/Event.h"
00009 #include "FWCore/Framework/interface/EventSetup.h"
00010 #include "FWCore/Framework/interface/ESHandle.h"
00011 #include "FWCore/Framework/interface/EDAnalyzer.h"
00012 #include "FWCore/Framework/interface/MakerMacros.h"
00013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00014 
00015 #include "FWCore/Framework/interface/IOVSyncValue.h"
00016 #include "FWCore/ServiceRegistry/interface/Service.h"
00017 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00018 
00019 #include "CondFormats/PhysicsToolsObjects/interface/MVAComputer.h"
00020 
00021 #include "PhysicsTools/MVATrainer/interface/MVATrainerLooper.h"
00022 #include "PhysicsTools/MVATrainer/interface/MVATrainerContainerSave.h"
00023 
00024 namespace PhysicsTools {
00025 
00026 MVATrainerContainerSave::MVATrainerContainerSave(
00027                                         const edm::ParameterSet &params) :
00028         toPut(params.getParameter<std::vector<std::string> >("toPut")),
00029         toCopy(params.getParameter<std::vector<std::string> >("toCopy")),
00030         saved(false)
00031 {
00032 }
00033 
00034 void MVATrainerContainerSave::analyze(const edm::Event& event,
00035                                       const edm::EventSetup& es)
00036 {
00037         if (calib.get() || saved)
00038                 return;
00039 
00040         const Calibration::MVAComputerContainer *toPutCalib = 0;
00041         if (!toPut.empty()) {
00042                 toPutCalib = getToPut(es);
00043                 if (MVATrainerLooper::isUntrained(toPutCalib))
00044                         return;
00045         }
00046 
00047         const Calibration::MVAComputerContainer *toCopyCalib = 0;
00048         if (!toCopy.empty())
00049                 toCopyCalib = getToCopy(es);
00050 
00051         edm::LogInfo("MVATrainerSave") << "Got the trained calibration data";
00052 
00053         std::auto_ptr<Calibration::MVAComputerContainer> calib(
00054                                         new Calibration::MVAComputerContainer);
00055 
00056         for(std::vector<std::string>::const_iterator iter = toCopy.begin();
00057             iter != toCopy.end(); iter++)
00058                 calib->add(*iter) = toCopyCalib->find(*iter);
00059 
00060         for(std::vector<std::string>::const_iterator iter = toPut.begin();
00061             iter != toPut.end(); iter++)
00062                 calib->add(*iter) = toPutCalib->find(*iter);
00063 
00064         this->calib = calib;
00065 }
00066 
00067 void MVATrainerContainerSave::endJob()
00068 {
00069         if (!calib.get() || saved)
00070                 return;
00071 
00072         edm::LogInfo("MVATrainerSave") << "Saving calibration data in CondDB.";
00073 
00074         edm::Service<cond::service::PoolDBOutputService> dbService;
00075         if (!dbService.isAvailable())
00076                 throw cms::Exception("MVATrainerContainerSave")
00077                         << "DBService unavailable" << std::endl;
00078 
00079         dbService->createNewIOV<Calibration::MVAComputerContainer>(
00080                 calib.release(), dbService->beginOfTime(),
00081                 dbService->endOfTime(), getRecordName().c_str());
00082 
00083         saved = true;
00084 }
00085 
00086 } // namespace PhysicsTools