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 // MVATrainerLooper::Trainer implementation 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 bool doLoad = params.getUntrackedParameter<bool>("loadState", false); 00043 bool doSave = params.getUntrackedParameter<bool>("saveState", false); 00044 bool doMonitoring = params.getUntrackedParameter<bool>("monitoring", false); 00045 00046 trainer = std::auto_ptr<MVATrainer>(new MVATrainer(trainDescription)); 00047 00048 if (doLoad) 00049 trainer->loadState(); 00050 00051 trainer->setAutoSave(doSave); 00052 trainer->setCleanup(!doSave); 00053 trainer->setMonitoring(doMonitoring); 00054 } 00055 00056 // MVATrainerLooper::MVATrainerContainer implementation 00057 00058 MVATrainerLooper::TrainerContainer::~TrainerContainer() 00059 { 00060 clear(); 00061 } 00062 00063 void MVATrainerLooper::TrainerContainer::clear() 00064 { 00065 std::for_each(begin(), end(), deleter<Trainer>()); 00066 content.clear(); 00067 } 00068 00069 // MVATrainerLooper implementation 00070 00071 MVATrainerLooper::MVATrainerLooper(const edm::ParameterSet& iConfig) 00072 { 00073 } 00074 00075 MVATrainerLooper::~MVATrainerLooper() 00076 { 00077 } 00078 00079 void MVATrainerLooper::startingNewLoop(unsigned int iteration) 00080 { 00081 for(TrainerContainer::const_iterator iter = trainers.begin(); 00082 iter != trainers.end(); iter++) { 00083 Trainer *trainer = *iter; 00084 00085 trainer->trainCalib = 00086 TrainObject(trainer->trainer->getTrainCalibration()); 00087 } 00088 } 00089 00090 edm::EDLooper::Status 00091 MVATrainerLooper::duringLoop(const edm::Event &event, 00092 const edm::EventSetup &es) 00093 { 00094 if (trainers.empty()) 00095 return kStop; 00096 00097 for(TrainerContainer::const_iterator iter = trainers.begin(); 00098 iter != trainers.end(); iter++) 00099 if ((*iter)->getCalibration()) 00100 return kContinue; 00101 00102 trainers.clear(); 00103 return kStop; 00104 } 00105 00106 edm::EDLooper::Status MVATrainerLooper::endOfLoop(const edm::EventSetup &es, 00107 unsigned int iteration) 00108 { 00109 if (trainers.empty()) 00110 return kStop; 00111 00112 for(TrainerContainer::const_iterator iter = trainers.begin(); 00113 iter != trainers.end(); iter++) { 00114 Trainer *trainer = *iter; 00115 00116 if (trainer->trainCalib) 00117 trainer->trainer->doneTraining( 00118 trainer->trainCalib.get()); 00119 00120 trainer->trainCalib.reset(); 00121 } 00122 00123 return kContinue; 00124 } 00125 00126 } // namespace PhysicsTools