CMS 3D CMS Logo

VarProcessor.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_MVAComputer_VarProcessor_h
2 #define PhysicsTools_MVAComputer_VarProcessor_h
3 // -*- C++ -*-
4 //
5 // Package: MVAComputer
6 // Class : VarProcessor
7 //
8 
9 //
10 // Author: Christophe Saout <christophe.saout@cern.ch>
11 // Created: Sat Apr 24 15:18 CEST 2007
12 //
13 
14 #include <algorithm>
15 #include <vector>
16 
21 
22 namespace PhysicsTools {
23 
24  // forward declaration
25  class MVAComputer;
26 
36  class VarProcessor : public ProcessRegistry<VarProcessor, Calibration::VarProcessor, const MVAComputer>::Factory {
37  public:
47  struct Config {
48  inline Config() : mask(Variable::FLAG_NONE), origin(0) {}
49  inline Config(Variable::Flags mask, unsigned int origin) : mask(mask), origin(origin) {}
50 
52  unsigned int origin;
53  };
54 
55  class ConfigCtx {
56  public:
57  typedef std::vector<Config> Config_t;
58 
61  typedef Config_t::iterator iterator;
62  typedef Config_t::const_iterator const_iterator;
63 
64  struct Context {
65  virtual ~Context() {}
66  };
67 
68  ConfigCtx(const std::vector<Variable::Flags> &flags);
69  ~ConfigCtx() { delete ctx; }
70 
71  inline size_type size() const { return configs.size(); }
72  inline const_iterator begin() const { return configs.begin(); }
73  inline iterator begin() { return configs.begin(); }
74  inline const_iterator end() const { return configs.end(); }
75  inline iterator end() { return configs.end(); }
76  inline void push_back(const Config &config) { configs.push_back(config); }
77  inline Config &operator[](size_type i) { return configs[i]; }
78 
79  private:
80  friend class VarProcessor;
81 
85  };
86 
95  class LoopCtx {
96  public:
97  LoopCtx() : index_(0), offset_(0), size_(0) {}
98  inline unsigned int &index() { return index_; }
99  inline unsigned int &offset() { return offset_; }
100  inline unsigned int &size() { return size_; }
101 
102  private:
103  unsigned int index_;
104  unsigned int offset_;
105  unsigned int size_;
106  };
107 
108  virtual ~VarProcessor();
109 
111  void configure(ConfigCtx &config);
112 
114  inline void eval(
115  double *input, int *conf, double *output, int *outConf, int *loop, LoopCtx &loopCtx, unsigned int offset) const {
116  ValueIterator iter(inputVars.iter(), input, conf, output, outConf, loop, loopCtx, offset);
117  eval(iter, nInputVars);
118  }
119 
121  void deriv(double *input,
122  int *conf,
123  double *output,
124  int *outConf,
125  int *loop,
126  LoopCtx &ctx,
127  unsigned int offset,
128  unsigned int in,
129  unsigned int out,
130  std::vector<double> &deriv) const;
131 
133 
134  virtual LoopStatus loop(
135  double *output, int *outConf, unsigned int nOutput, LoopCtx &ctx, unsigned int &nOffset) const {
136  return kStop;
137  }
138 
139  //used to create a PluginFactory
140  struct Dummy {};
142 
143  protected:
149  struct ConfIterator {
150  public:
153  config[cur()].mask = (Variable::Flags)(config[cur()].mask & mask);
154  return *this;
155  }
156 
159  config.push_back(config_);
160  return *this;
161  }
162 
164  ConfIterator &operator<<(Variable::Flags mask) { return *this << Config(mask, 0); }
165 
168  return *this << Config(config[origin.cur()].mask, origin.cur());
169  }
170 
172  Variable::Flags operator*() const { return config[cur()].mask; }
173 
175  inline operator bool() const { return cur; }
176 
179  ++cur;
180  return *this;
181  }
182 
185  ConfIterator orig = *this;
186  operator++();
187  return orig;
188  }
189 
190  protected:
191  friend class VarProcessor;
192 
194 
195  private:
198  };
199 
205  struct ValueIterator {
206  public:
208  inline unsigned int size() const { return loop ? (conf[1] - conf[0]) : 1; }
209 
211  inline double *begin() const { return values; }
212 
214  inline double *end() const { return values + size(); }
215 
217  inline bool empty() const { return begin() == end(); }
218 
220  inline double operator*() { return *values; }
221 
223  inline double operator[](unsigned int idx) { return values[idx]; }
224 
226  inline ValueIterator &operator<<(double value) {
227  *output++ = value;
228  return *this;
229  }
230 
232  inline void operator()() {
233  int pos = output - start;
234  if (*++outConf > pos)
235  output = start + *outConf;
236  else
237  *outConf = pos;
238  }
239 
241  inline void operator()(double value) {
242  *this << value;
243  (*this)();
244  }
245 
247  inline operator bool() const { return cur; }
248 
249  inline LoopCtx &loopCtx() { return ctx; }
250 
253  BitSet::size_t orig = cur();
254  if (++cur) {
255  unsigned int prev = *conf;
256  conf += cur() - orig;
257  values += *conf - prev;
258  if (loop && conf >= loop) {
259  values += offset;
260  loop = nullptr;
261  }
262  }
263  return *this;
264  }
265 
268  ValueIterator orig = *this;
269  operator++();
270  return orig;
271  }
272 
273  protected:
274  friend class VarProcessor;
275 
277  double *values,
278  int *conf,
279  double *output,
280  int *outConf,
281  int *loop,
282  LoopCtx &ctx,
283  unsigned int offset)
284  : cur(cur),
285  ctx(ctx),
286  offset(offset),
287  start(values + offset),
288  values(values),
289  conf(conf),
290  loop(loop),
291  output(output + offset),
292  outConf(outConf) {
293  this->conf += cur();
294  this->values += *this->conf;
295  if (loop && this->conf >= loop) {
296  this->values += offset;
297  this->loop = nullptr;
298  }
299  }
300 
301  private:
304  const unsigned int offset;
305  double *const start;
306  double *values;
307  const int *conf;
308  const int *loop;
309  double *output;
310  int *outConf;
311  };
312 
314 
316 
318  virtual void configure(ConfIterator iter, unsigned int n) = 0;
319 
322  ConfigCtx::iterator begin,
325 
327  virtual void eval(ValueIterator iter, unsigned int n) const = 0;
328 
330  virtual std::vector<double> deriv(ValueIterator iter, unsigned int n) const { return std::vector<double>(); }
331 
332  protected:
334 
335  private:
338  unsigned int nInputVars;
339  };
340 
341  template <>
343  const char *, const Calibration::VarProcessor *, const MVAComputer *);
344 
345 } // namespace PhysicsTools
346 
347 #endif // PhysicsTools_MVAComputer_VarProcessor_h
Definition: start.py:1
virtual ConfigCtx::Context * configureLoop(ConfigCtx::Context *ctx, ConfigCtx::iterator begin, ConfigCtx::iterator cur, ConfigCtx::iterator end)
virtual loop configure method
Definition: VarProcessor.cc:74
ConfIterator & operator<<(const ConfIterator &origin)
add a new output variable that inherits values from origin
Definition: VarProcessor.h:167
Config & operator[](size_type i)
Definition: VarProcessor.h:77
Iterator iter() const
create iterator over all set bits
Definition: BitSet.h:179
Variable::Flags operator*() const
return the current input variable flags
Definition: VarProcessor.h:172
void operator()(double value)
add value as output variable and move to next slot
Definition: VarProcessor.h:241
void configure(ConfigCtx &config)
called from the discriminator computer to configure processor
Definition: VarProcessor.cc:47
Class describing an input variable.
Definition: Variable.h:33
void push_back(const Config &config)
Definition: VarProcessor.h:76
ConfIterator operator++(int dummy)
move to next input variable
Definition: VarProcessor.h:184
ConfIterator & operator()(Variable::Flags mask)
apply property flags mask to variable at current position
Definition: VarProcessor.h:152
Definition: config.py:1
ProcessRegistry< VarProcessor, Calibration::VarProcessor, const MVAComputer > Registry
Definition: VarProcessor.h:313
uint16_t size_type
ConfigCtx(const std::vector< Variable::Flags > &flags)
Definition: VarProcessor.cc:69
static std::string const input
Definition: EdmProvDump.cc:47
ValueIterator(BitSet::Iterator cur, double *values, int *conf, double *output, int *outConf, int *loop, LoopCtx &ctx, unsigned int offset)
Definition: VarProcessor.h:276
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
Iterator to loop over the input/output variable configuration.
Definition: VarProcessor.h:149
BitSet inputVars
bit set to select the input variables to be passed to this processor
Definition: VarProcessor.h:337
unsigned int size() const
number of values for current input variable
Definition: VarProcessor.h:208
Config
Definition: helper.py:10
A compact container for storing single bits.
Definition: BitSet.h:29
Config_t::const_iterator const_iterator
Definition: VarProcessor.h:62
double operator*()
the (first or only) value for the current input variable
Definition: VarProcessor.h:220
Definition: value.py:1
ConfIterator & operator++()
move to next input variable
Definition: VarProcessor.h:178
void deriv(double *input, int *conf, double *output, int *outConf, int *loop, LoopCtx &ctx, unsigned int offset, unsigned int in, unsigned int out, std::vector< double > &deriv) const
run the processor evaluation pass on this processor and compute derivatives
static Base_t * create(const char *name, const CalibBase_t *calib, Parent_t *parent=0)
bool empty() const
checks for existence of values for current input variable
Definition: VarProcessor.h:217
std::size_t size_t
Definition: BitSet.h:31
const MVAComputer * computer
Definition: VarProcessor.h:333
double * end() const
end of value array for current input variable
Definition: VarProcessor.h:214
ValueIterator & operator<<(double value)
add computed value to current output variable
Definition: VarProcessor.h:226
ConfIterator(BitSet::Iterator cur, ConfigCtx &config)
Definition: VarProcessor.h:193
void operator()()
finish current output variable, move to next slot
Definition: VarProcessor.h:232
VarProcessor(const char *name, const Calibration::VarProcessor *calib, const MVAComputer *computer)
Definition: VarProcessor.cc:39
virtual LoopStatus loop(double *output, int *outConf, unsigned int nOutput, LoopCtx &ctx, unsigned int &nOffset) const
Definition: VarProcessor.h:134
double operator[](unsigned int idx)
value idx of current input variable
Definition: VarProcessor.h:223
void eval(double *input, int *conf, double *output, int *outConf, int *loop, LoopCtx &loopCtx, unsigned int offset) const
run the processor evaluation pass on this processor
Definition: VarProcessor.h:114
Definition: Config.py:1
ValueIterator & operator++()
move to next input variable
Definition: VarProcessor.h:252
Hold context information for looping processors.
Definition: VarProcessor.h:95
ValueIterator operator++(int dummy)
move to next input variable
Definition: VarProcessor.h:267
ConfIterator & operator<<(Variable::Flags mask)
add a new output variable configuration with mask mask
Definition: VarProcessor.h:164
virtual std::vector< double > deriv(ValueIterator iter, unsigned int n) const
virtual derivative evaluation method, implemented in actual processor
Definition: VarProcessor.h:330
Iterates over all set bits of a BitSet.
Definition: BitSet.h:77
Generic registry template for polymorphic processor implementations.
double * begin() const
begin of value array for current input variable
Definition: VarProcessor.h:211
Config(Variable::Flags mask, unsigned int origin)
Definition: VarProcessor.h:49
ConfIterator & operator<<(Config config_)
add a new output variable configuration config_
Definition: VarProcessor.h:158
Common base class for variable processors.
Definition: VarProcessor.h:36