CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/PhysicsTools/MVATrainer/src/MVATrainerFileSave.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 "CondFormats/PhysicsToolsObjects/interface/MVAComputer.h"
00016 
00017 #include "PhysicsTools/MVAComputer/interface/MVAComputer.h"
00018 #include "PhysicsTools/MVATrainer/interface/MVATrainerLooper.h"
00019 #include "PhysicsTools/MVATrainer/interface/MVATrainerFileSave.h"
00020 
00021 namespace PhysicsTools {
00022 
00023 MVATrainerFileSave::MVATrainerFileSave(const edm::ParameterSet &params) :
00024         trained(params.getUntrackedParameter<bool>("trained", true)),
00025         saved(false)
00026 {
00027         std::vector<std::string> names = params.getParameterNames();
00028         for(std::vector<std::string>::const_iterator iter = names.begin();
00029             iter != names.end(); iter++) {
00030                 if (iter->c_str()[0] == '@' || *iter == "trained")
00031                         continue;
00032 
00033                 toPut[*iter] = params.getParameter<std::string>(*iter);
00034         }
00035 
00036 }
00037 
00038 void MVATrainerFileSave::analyze(const edm::Event& event,
00039                                  const edm::EventSetup& es)
00040 {
00041         if (calib.get() || saved)
00042                 return;
00043 
00044         const Calibration::MVAComputerContainer *toPutCalib = getToPut(es);
00045         if (MVATrainerLooper::isUntrained(toPutCalib))
00046                 return;
00047 
00048         edm::LogInfo("MVATrainerFileSave")
00049                 << "Got the trained calibration data";
00050 
00051         std::auto_ptr<Calibration::MVAComputerContainer> calib(
00052                                         new Calibration::MVAComputerContainer);
00053         *calib = *toPutCalib;
00054 
00055         this->calib = calib;
00056 }
00057 
00058 void MVATrainerFileSave::endJob()
00059 {
00060         if (!calib.get() || saved)
00061                 return;
00062 
00063         edm::LogInfo("MVATrainerFileSave")
00064                 << "Saving calibration data into plain MVA files.";
00065 
00066         for(LabelFileMap::const_iterator iter = toPut.begin();
00067             iter != toPut.end(); iter++) {
00068                 const Calibration::MVAComputer *calibration =
00069                                                 &calib->find(iter->first);
00070 
00071                 MVAComputer::writeCalibration(iter->second.c_str(),
00072                                               calibration);
00073         }
00074 
00075         saved = true;
00076 }
00077 
00078 } // namespace PhysicsTools