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 dataProcessedInLoop(false)
00075 {
00076 }
00077
00078 MVATrainerLooper::~MVATrainerLooper()
00079 {
00080 }
00081
00082 void MVATrainerLooper::startingNewLoop(unsigned int iteration)
00083 {
00084 dataProcessedInLoop = false;
00085
00086 for(TrainerContainer::const_iterator iter = trainers.begin();
00087 iter != trainers.end(); iter++) {
00088 Trainer *trainer = *iter;
00089
00090 trainer->trainCalib =
00091 TrainObject(trainer->trainer->getTrainCalibration());
00092 }
00093 }
00094
00095 edm::EDLooper::Status
00096 MVATrainerLooper::duringLoop(const edm::Event &event,
00097 const edm::EventSetup &es)
00098 {
00099 dataProcessedInLoop = true;
00100
00101 if (trainers.empty())
00102 return kStop;
00103
00104 for(TrainerContainer::const_iterator iter = trainers.begin();
00105 iter != trainers.end(); iter++)
00106 if ((*iter)->getCalibration())
00107 return kContinue;
00108
00109 trainers.clear();
00110 return kStop;
00111 }
00112
00113 edm::EDLooper::Status MVATrainerLooper::endOfLoop(const edm::EventSetup &es,
00114 unsigned int iteration)
00115 {
00116 if (!dataProcessedInLoop) {
00117 cms::Exception ex("MVATrainerLooper");
00118 ex << "No data processed during loop\n";
00119 ex.addContext("Calling MVATrainerLooper::endOfLoop()");
00120 throw ex;
00121 }
00122
00123 if (trainers.empty())
00124 return kStop;
00125
00126 for(TrainerContainer::const_iterator iter = trainers.begin();
00127 iter != trainers.end(); iter++) {
00128 Trainer *trainer = *iter;
00129
00130 if (trainer->trainCalib)
00131 trainer->trainer->doneTraining(
00132 trainer->trainCalib.get());
00133
00134 trainer->trainCalib.reset();
00135 }
00136
00137 return kContinue;
00138 }
00139
00140 }