forked from cms-sw/cmssw
/
MVAComputer.cc
248 lines (200 loc) · 6.08 KB
/
MVAComputer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// -*- C++ -*-
//
// Package: PhysicsToolsObjects
// Class : MVAComputer
//
// Implementation:
// getProcessor() and addProcessor() methods to add processors to
// the discriminator computer calibration object. POOL doesn't support
// polymorph pointers, so this is implemented using multiple containers
// for each possible sub-class and an index array from which the
// array of pointers can be reconstructed.
//
// Author: Christophe Saout
// Created: Sat Apr 24 15:18 CEST 2007
//
#include <functional>
#include <algorithm>
#include <typeinfo>
#include <iostream>
#include <cstring>
#include <cstddef>
#include <atomic>
#include <Reflex/Reflex.h>
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Utilities/interface/TypeID.h"
#include "CondFormats/PhysicsToolsObjects/interface/MVAComputer.h"
namespace PhysicsTools {
namespace Calibration {
std::string VarProcessor::getInstanceName() const
{
static const char prefix[] = "PhysicsTools::Calibration::";
edm::TypeID typeID(typeid(*this));
std::string type(typeID.className());
if (type.size() <= sizeof prefix - 1 ||
type.substr(0, sizeof prefix - 1) != prefix)
throw cms::Exception("MVAComputerCalibration")
<< "getInstanceName failed for "
<< typeid(*this).name() << "." << std::endl;
return type.substr(sizeof prefix - 1);
}
std::unique_ptr<VarProcessor>
VarProcessor::clone() const {
return(std::unique_ptr<VarProcessor>(new VarProcessor(*this)));
}
std::unique_ptr<VarProcessor>
ProcOptional::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcOptional(*this)));
}
std::unique_ptr<VarProcessor>
ProcCount::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcCount(*this)));
}
std::unique_ptr<VarProcessor>
ProcClassed::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcClassed(*this)));
}
std::unique_ptr<VarProcessor>
ProcSplitter::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcSplitter(*this)));
}
std::unique_ptr<VarProcessor>
ProcForeach::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcForeach(*this)));
}
std::unique_ptr<VarProcessor>
ProcSort::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcSort(*this)));
}
std::unique_ptr<VarProcessor>
ProcCategory::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcCategory(*this)));
}
std::unique_ptr<VarProcessor>
ProcNormalize::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcNormalize(*this)));
}
std::unique_ptr<VarProcessor>
ProcLikelihood::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcLikelihood(*this)));
}
std::unique_ptr<VarProcessor>
ProcLinear::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcLinear(*this)));
}
std::unique_ptr<VarProcessor>
ProcMultiply::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcMultiply(*this)));
}
std::unique_ptr<VarProcessor>
ProcMatrix::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcMatrix(*this)));
}
std::unique_ptr<VarProcessor>
ProcExternal::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcExternal(*this)));
}
std::unique_ptr<VarProcessor>
ProcMLP::clone() const {
return(std::unique_ptr<VarProcessor>(new ProcMLP(*this)));
}
std::string ProcExternal::getInstanceName() const
{
return method;
}
static MVAComputer::CacheId getNextMVAComputerCacheId()
{
static std::atomic<MVAComputer::CacheId> nextCacheId{0};
return ++nextCacheId;
}
MVAComputer::MVAComputer() :
cacheId(getNextMVAComputerCacheId())
{
}
MVAComputer::MVAComputer(const MVAComputer &orig) :
inputSet(orig.inputSet),
output(orig.output),
cacheId(orig.cacheId)
{
for(std::vector<VarProcessor*>::const_iterator iter =
orig.processors.begin();
iter != orig.processors.end(); ++iter)
addProcessor(*iter);
}
MVAComputer::~MVAComputer()
{
for(std::vector<VarProcessor*>::iterator iter = processors.begin();
iter != processors.end(); ++iter)
delete *iter;
processors.clear();
}
MVAComputer &MVAComputer::operator = (const MVAComputer &orig)
{
inputSet = orig.inputSet;
output = orig.output;
cacheId = orig.cacheId;
for(std::vector<VarProcessor*>::iterator iter = processors.begin();
iter != processors.end(); ++iter)
delete *iter;
processors.clear();
for(std::vector<VarProcessor*>::const_iterator iter =
orig.processors.begin();
iter != orig.processors.end(); ++iter)
addProcessor(*iter);
return *this;
}
std::vector<VarProcessor*> MVAComputer::getProcessors() const
{
return processors;
}
void MVAComputer::addProcessor(const VarProcessor *proc)
{
cacheId = getNextMVAComputerCacheId();
processors.push_back(proc->clone().release());
}
static MVAComputerContainer::CacheId getNextMVAComputerContainerCacheId()
{
static MVAComputerContainer::CacheId nextCacheId = 0;
return ++nextCacheId;
}
MVAComputerContainer::MVAComputerContainer() :
cacheId(getNextMVAComputerContainerCacheId())
{
}
MVAComputer &MVAComputerContainer::add(const std::string &label)
{
cacheId = getNextMVAComputerContainerCacheId();
entries.push_back(std::make_pair(label, MVAComputer()));
return entries.back().second;
}
namespace {
struct Comparator :
public std::unary_function<const std::string&, bool> {
inline Comparator(const std::string &label) : label(label) {}
inline bool
operator () (const MVAComputerContainer::Entry &entry) const
{ return entry.first == label; }
const std::string &label;
};
}
const MVAComputer &MVAComputerContainer::find(const std::string &label) const
{
std::vector<Entry>::const_iterator pos =
std::find_if(entries.begin(), entries.end(),
Comparator(label));
if (pos == entries.end())
throw cms::Exception("MVAComputerCalibration")
<< "Calibration record " << label
<< " not found in MVAComputerContainer." << std::endl;
return pos->second;
}
bool MVAComputerContainer::contains(const std::string &label) const
{
std::vector<Entry>::const_iterator pos =
std::find_if(entries.begin(), entries.end(),
Comparator(label));
if (pos == entries.end()) return false;
return true;
}
} // namespace Calibration
} // namespace PhysicsTools