CMS 3D CMS Logo

MVATrainerLooper.cc
Go to the documentation of this file.
1 #include <cassert>
2 #include <algorithm>
3 #include <string>
4 #include <memory>
5 
10 
12 
15 
16 namespace PhysicsTools {
17 
18 namespace {
19  template<typename T>
20  inline void deleter (T *ptr) { delete ptr; }
21 }
22 
23 // MVATrainerLooper::Trainer implementation
24 
26 {
27  const edm::Entry *entry = params.retrieveUntracked("trainDescription");
28  if (!entry)
30  "MissingParameter:")
31  << "The required parameter 'trainDescription' "
32  "was not specified." << std::endl;;
34  if (entry->typeCode() == 'F')
35  trainDescription = entry->getFileInPath().fullPath();
36  else
37  trainDescription = entry->getString();
38 
39  bool useXSLT = params.getUntrackedParameter<bool>("useXSLT", false);
40  bool doLoad = params.getUntrackedParameter<bool>("loadState", false);
41  bool doSave = params.getUntrackedParameter<bool>("saveState", false);
42  bool doMonitoring = params.getUntrackedParameter<bool>("monitoring", false);
43 
44  trainer.reset(new MVATrainer(trainDescription, useXSLT));
45 
46  if (doLoad)
47  trainer->loadState();
48 
49  trainer->setAutoSave(doSave);
50  trainer->setCleanup(!doSave);
51  trainer->setMonitoring(doMonitoring);
52 }
53 
54 // MVATrainerLooper::MVATrainerContainer implementation
55 
57 {
58  clear();
59 }
60 
62 {
63  std::for_each(begin(), end(), deleter<Trainer>);
64  content.clear();
65 }
66 
67 // MVATrainerLooper implementation
68 
71 {
72 }
73 
75 {
76 }
77 
79 {
80  dataProcessedInLoop = false;
81 
83  iter != trainers.end(); iter++) {
84  Trainer *trainer = *iter;
85 
86  trainer->trainCalib =
87  TrainObject(trainer->trainer->getTrainCalibration());
88  }
89 }
90 
93  const edm::EventSetup &es)
94 {
95  dataProcessedInLoop = true;
96 
97  if (trainers.empty())
98  return kStop;
99 
101  iter != trainers.end(); iter++)
102  if ((*iter)->getCalibration())
103  return kContinue;
104 
105  trainers.clear();
106  return kStop;
107 }
108 
110  unsigned int iteration)
111 {
112  if (!dataProcessedInLoop) {
113  cms::Exception ex("MVATrainerLooper");
114  ex << "No data processed during loop\n";
115  ex.addContext("Calling MVATrainerLooper::endOfLoop()");
116  throw ex;
117  }
118 
119  if (trainers.empty())
120  return kStop;
121 
123  iter != trainers.end(); iter++) {
124  Trainer *trainer = *iter;
125 
126  if (trainer->trainCalib)
127  trainer->trainer->doneTraining(
128  trainer->trainCalib.get());
129 
130  trainer->trainCalib.reset();
131  }
132 
133  return kContinue;
134 }
135 
136 } // namespace PhysicsTools
T getUntrackedParameter(std::string const &, T const &) const
MVATrainerLooper(const edm::ParameterSet &params)
Status duringLoop(const edm::Event &ev, const edm::EventSetup &es) override
void startingNewLoop(unsigned int iteration) override
std::unique_ptr< MVATrainer > trainer
void clear(CLHEP::HepGenMatrix &m)
Helper function: Reset all elements of a matrix to 0.
Definition: matutil.cc:167
#define end
Definition: vmac.h:39
Trainer(const edm::ParameterSet &params)
FileInPath getFileInPath() const
Definition: Entry.cc:818
void addContext(std::string const &context)
Definition: Exception.cc:165
Status endOfLoop(const edm::EventSetup &es, unsigned int iteration) override
std::vector< Trainer * >::const_iterator const_iterator
#define begin
Definition: vmac.h:32
std::shared_ptr< Calibration::MVAComputer > TrainObject
std::string fullPath() const
Definition: FileInPath.cc:163
char typeCode() const
Definition: Entry.h:176
Entry const * retrieveUntracked(char const *) const
long double T
std::string getString() const
Definition: Entry.cc:794
Definition: event.py:1