Go to the documentation of this file.00001 #include <assert.h>
00002 #include <algorithm>
00003 #include <string>
00004 #include <memory>
00005
00006 #include <boost/shared_ptr.hpp>
00007
00008 #include "FWCore/Utilities/interface/Exception.h"
00009 #include "FWCore/Utilities/interface/EDMException.h"
00010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00011 #include "FWCore/ParameterSet/interface/FileInPath.h"
00012
00013 #include "CondFormats/PhysicsToolsObjects/interface/MVAComputer.h"
00014
00015 #include "PhysicsTools/MVATrainer/interface/MVATrainer.h"
00016 #include "PhysicsTools/MVATrainer/interface/MVATrainerLooper.h"
00017
00018 namespace PhysicsTools {
00019
00020 namespace {
00021 template<typename T>
00022 struct deleter : public std::unary_function<T*, void> {
00023 inline void operator() (T *ptr) const { delete ptr; }
00024 };
00025 }
00026
00027
00028
00029 MVATrainerLooper::Trainer::Trainer(const edm::ParameterSet ¶ms)
00030 {
00031 const edm::Entry *entry = params.retrieveUntracked("trainDescription");
00032 if (!entry)
00033 throw edm::Exception(edm::errors::Configuration,
00034 "MissingParameter:")
00035 << "The required parameter 'trainDescription' "
00036 "was not specified." << std::endl;;
00037 std::string trainDescription;
00038 if (entry->typeCode() == 'F')
00039 trainDescription = entry->getFileInPath().fullPath();
00040 else
00041 trainDescription = entry->getString();
00042
00043 bool useXSLT = params.getUntrackedParameter<bool>("useXSLT", false);
00044 bool doLoad = params.getUntrackedParameter<bool>("loadState", false);
00045 bool doSave = params.getUntrackedParameter<bool>("saveState", false);
00046 bool doMonitoring = params.getUntrackedParameter<bool>("monitoring", false);
00047
00048 trainer.reset(new MVATrainer(trainDescription, useXSLT));
00049
00050 if (doLoad)
00051 trainer->loadState();
00052
00053 trainer->setAutoSave(doSave);
00054 trainer->setCleanup(!doSave);
00055 trainer->setMonitoring(doMonitoring);
00056 }
00057
00058
00059
00060 MVATrainerLooper::TrainerContainer::~TrainerContainer()
00061 {
00062 clear();
00063 }
00064
00065 void MVATrainerLooper::TrainerContainer::clear()
00066 {
00067 std::for_each(begin(), end(), deleter<Trainer>());
00068 content.clear();
00069 }
00070
00071
00072
00073 MVATrainerLooper::MVATrainerLooper(const edm::ParameterSet& iConfig)
00074 {
00075 }
00076
00077 MVATrainerLooper::~MVATrainerLooper()
00078 {
00079 }
00080
00081 void MVATrainerLooper::startingNewLoop(unsigned int iteration)
00082 {
00083 for(TrainerContainer::const_iterator iter = trainers.begin();
00084 iter != trainers.end(); iter++) {
00085 Trainer *trainer = *iter;
00086
00087 trainer->trainCalib =
00088 TrainObject(trainer->trainer->getTrainCalibration());
00089 }
00090 }
00091
00092 edm::EDLooper::Status
00093 MVATrainerLooper::duringLoop(const edm::Event &event,
00094 const edm::EventSetup &es)
00095 {
00096 if (trainers.empty())
00097 return kStop;
00098
00099 for(TrainerContainer::const_iterator iter = trainers.begin();
00100 iter != trainers.end(); iter++)
00101 if ((*iter)->getCalibration())
00102 return kContinue;
00103
00104 trainers.clear();
00105 return kStop;
00106 }
00107
00108 edm::EDLooper::Status MVATrainerLooper::endOfLoop(const edm::EventSetup &es,
00109 unsigned int iteration)
00110 {
00111 if (trainers.empty())
00112 return kStop;
00113
00114 for(TrainerContainer::const_iterator iter = trainers.begin();
00115 iter != trainers.end(); iter++) {
00116 Trainer *trainer = *iter;
00117
00118 if (trainer->trainCalib)
00119 trainer->trainer->doneTraining(
00120 trainer->trainCalib.get());
00121
00122 trainer->trainCalib.reset();
00123 }
00124
00125 return kContinue;
00126 }
00127
00128 }