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  struct deleter : public std::unary_function<T*, void> {
21  inline void operator() (T *ptr) const { delete ptr; }
22  };
23 }
24 
25 // MVATrainerLooper::Trainer implementation
26 
28 {
29  const edm::Entry *entry = params.retrieveUntracked("trainDescription");
30  if (!entry)
32  "MissingParameter:")
33  << "The required parameter 'trainDescription' "
34  "was not specified." << std::endl;;
36  if (entry->typeCode() == 'F')
37  trainDescription = entry->getFileInPath().fullPath();
38  else
39  trainDescription = entry->getString();
40 
41  bool useXSLT = params.getUntrackedParameter<bool>("useXSLT", false);
42  bool doLoad = params.getUntrackedParameter<bool>("loadState", false);
43  bool doSave = params.getUntrackedParameter<bool>("saveState", false);
44  bool doMonitoring = params.getUntrackedParameter<bool>("monitoring", false);
45 
46  trainer.reset(new MVATrainer(trainDescription, useXSLT));
47 
48  if (doLoad)
49  trainer->loadState();
50 
51  trainer->setAutoSave(doSave);
52  trainer->setCleanup(!doSave);
53  trainer->setMonitoring(doMonitoring);
54 }
55 
56 // MVATrainerLooper::MVATrainerContainer implementation
57 
59 {
60  clear();
61 }
62 
64 {
65  std::for_each(begin(), end(), deleter<Trainer>());
66  content.clear();
67 }
68 
69 // MVATrainerLooper implementation
70 
72  dataProcessedInLoop(false)
73 {
74 }
75 
77 {
78 }
79 
81 {
82  dataProcessedInLoop = false;
83 
85  iter != trainers.end(); iter++) {
86  Trainer *trainer = *iter;
87 
88  trainer->trainCalib =
89  TrainObject(trainer->trainer->getTrainCalibration());
90  }
91 }
92 
95  const edm::EventSetup &es)
96 {
97  dataProcessedInLoop = true;
98 
99  if (trainers.empty())
100  return kStop;
101 
103  iter != trainers.end(); iter++)
104  if ((*iter)->getCalibration())
105  return kContinue;
106 
107  trainers.clear();
108  return kStop;
109 }
110 
112  unsigned int iteration)
113 {
114  if (!dataProcessedInLoop) {
115  cms::Exception ex("MVATrainerLooper");
116  ex << "No data processed during loop\n";
117  ex.addContext("Calling MVATrainerLooper::endOfLoop()");
118  throw ex;
119  }
120 
121  if (trainers.empty())
122  return kStop;
123 
125  iter != trainers.end(); iter++) {
126  Trainer *trainer = *iter;
127 
128  if (trainer->trainCalib)
129  trainer->trainer->doneTraining(
130  trainer->trainCalib.get());
131 
132  trainer->trainCalib.reset();
133  }
134 
135  return kContinue;
136 }
137 
138 } // 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:785
void addContext(std::string const &context)
Definition: Exception.cc:227
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:197
char typeCode() const
Definition: Entry.h:179
Entry const * retrieveUntracked(char const *) const
long double T
std::string getString() const
Definition: Entry.cc:762
Definition: event.py:1