CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 :
37  public ProcessRegistry<VarProcessor,
38  Calibration::VarProcessor,
39  const MVAComputer>::Factory {
40  public:
50  struct Config {
51  inline Config() : mask(Variable::FLAG_NONE), origin(0) {}
52  inline Config(Variable::Flags mask, unsigned int origin) :
53  mask(mask), origin(origin) {}
54 
56  unsigned int origin;
57  };
58 
59  class ConfigCtx {
60  public:
61  typedef std::vector<Config> Config_t;
62 
65  typedef Config_t::iterator iterator;
66  typedef Config_t::const_iterator const_iterator;
67 
68  struct Context { virtual ~Context() {} };
69 
70  ConfigCtx(const std::vector<Variable::Flags>& flags);
71  ~ConfigCtx() { delete ctx; }
72 
73  inline size_type size() const { return configs.size(); }
74  inline const_iterator begin() const { return configs.begin(); }
75  inline iterator begin() { return configs.begin(); }
76  inline const_iterator end() const { return configs.end(); }
77  inline iterator end() { return configs.end(); }
78  inline void push_back(const Config &config) { configs.push_back(config); }
79  inline Config &operator [] (size_type i) { return configs[i]; }
80 
81  private:
82  friend class VarProcessor;
83 
87  };
88 
89 
98  class LoopCtx {
99  public:
100  LoopCtx(): index_(0), offset_(0), size_(0) {}
101  inline unsigned int& index() { return index_;}
102  inline unsigned int& offset() { return offset_;}
103  inline unsigned int& size() { return size_;}
104  private:
105  unsigned int index_;
106  unsigned int offset_;
107  unsigned int size_ ;
108  };
109 
110  virtual ~VarProcessor();
111 
113  void configure(ConfigCtx &config);
114 
116  inline void
117  eval(double *input, int *conf, double *output, int *outConf,
118  int *loop, LoopCtx& loopCtx, unsigned int offset) const
119  {
121  output, outConf, loop, loopCtx, offset);
122  eval(iter, nInputVars);
123  }
124 
126  void deriv(double *input, int *conf, double *output, int *outConf,
127  int *loop, LoopCtx& ctx, unsigned int offset, unsigned int in,
128  unsigned int out, std::vector<double> &deriv) const;
129 
131 
132  virtual LoopStatus loop(double *output, int *outConf,
133  unsigned int nOutput,
134  LoopCtx& ctx,
135  unsigned int &nOffset) const
136  { return kStop; }
137 
138  //used to create a PluginFactory
139  struct Dummy {};
141 
142  protected:
148  struct ConfIterator {
149  public:
152  {
153  config[cur()].mask =
154  (Variable::Flags)(config[cur()].mask & mask);
155  return *this;
156  }
157 
160  { config.push_back(config_); return *this; }
161 
164  { return *this << Config(mask, 0); }
165 
168  { return *this << Config(config[origin.cur()].mask, origin.cur()); }
169 
172  { return config[cur()].mask; }
173 
175  inline operator bool() const { return cur; }
176 
178  ConfIterator &operator ++ () { ++cur; return *this; }
179 
181  inline ConfIterator operator ++ (int dummy)
182  { ConfIterator orig = *this; operator ++ (); return orig; }
183 
184  protected:
185  friend class VarProcessor;
186 
188  cur(cur), config(config) {}
189 
190  private:
193  };
194 
200  struct ValueIterator {
201  public:
203  inline unsigned int size() const
204  { return loop ? (conf[1] - conf[0]) : 1; }
205 
207  inline double *begin() const { return values; }
208 
210  inline double *end() const { return values + size(); }
211 
213  inline bool empty() const { return begin() == end(); }
214 
216  inline double operator * ()
217  { return *values; }
218 
220  inline double operator [] (unsigned int idx)
221  { return values[idx]; }
222 
225  { *output++ = value; return *this; }
226 
228  inline void operator () ()
229  {
230  int pos = output - start;
231  if (*++outConf > pos)
232  output = start + *outConf;
233  else
234  *outConf = pos;
235  }
236 
238  inline void operator () (double value)
239  { *this << value; (*this)(); }
240 
242  inline operator bool() const { return cur; }
243 
244  inline LoopCtx& loopCtx() { return ctx;}
245 
248  {
249  BitSet::size_t orig = cur();
250  if (++cur) {
251  unsigned int prev = *conf;
252  conf += cur() - orig;
253  values += *conf - prev;
254  if (loop && conf >= loop) {
255  values += offset;
256  loop = 0;
257  }
258  }
259  return *this;
260  }
261 
263  inline ValueIterator operator ++ (int dummy)
264  { ValueIterator orig = *this; operator ++ (); return orig; }
265 
266  protected:
267  friend class VarProcessor;
268 
270  int *conf, double *output, int *outConf,
271  int *loop, LoopCtx& ctx, unsigned int offset) :
272  cur(cur), ctx(ctx), offset(offset), start(values + offset),
273  values(values), conf(conf), loop(loop),
274  output(output + offset), outConf(outConf)
275  {
276  this->conf += cur();
277  this->values += *this->conf;
278  if (loop && this->conf >= loop) {
279  this->values += offset;
280  this->loop = 0;
281  }
282  }
283 
284  private:
287  const unsigned int offset;
288  double *const start;
289  double *values;
290  const int *conf;
291  const int *loop;
292  double *output;
293  int *outConf;
294  };
295 
299 
300  VarProcessor(const char *name, const Calibration::VarProcessor *calib,
301  const MVAComputer *computer);
302 
304  virtual void configure(ConfIterator iter, unsigned int n) = 0;
305 
307  virtual ConfigCtx::Context *
310 
312  virtual void eval(ValueIterator iter, unsigned int n) const = 0;
313 
315  virtual std::vector<double> deriv(ValueIterator iter,
316  unsigned int n) const
317  { return std::vector<double>(); }
318 
319  protected:
321 
322  private:
325  unsigned int nInputVars;
326 };
327 
328 template<>
331  const char*, const Calibration::VarProcessor*, const MVAComputer*);
332 
333 } // namespace PhysicsTools
334 
335 
336 #endif // PhysicsTools_MVAComputer_VarProcessor_h
virtual ConfigCtx::Context * configureLoop(ConfigCtx::Context *ctx, ConfigCtx::iterator begin, ConfigCtx::iterator cur, ConfigCtx::iterator end)
virtual loop configure method
Definition: VarProcessor.cc:87
int i
Definition: DBlmapReader.cc:9
Config & operator[](size_type i)
Definition: VarProcessor.h:79
unsigned int size() const
number of values for current input variable
Definition: VarProcessor.h:203
void configure(ConfigCtx &config)
called from the discriminator computer to configure processor
Definition: VarProcessor.cc:53
Class describing an input variable.
Definition: Variable.h:33
void push_back(const Config &config)
Definition: VarProcessor.h:78
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
ConfIterator & operator()(Variable::Flags mask)
apply property flags mask to variable at current position
Definition: VarProcessor.h:151
ProcessRegistry< VarProcessor, Calibration::VarProcessor, const MVAComputer > Registry
Definition: VarProcessor.h:298
bool empty() const
checks for existence of values for current input variable
Definition: VarProcessor.h:213
uint16_t size_type
ConfigCtx(const std::vector< Variable::Flags > &flags)
Definition: VarProcessor.cc:78
static std::string const input
Definition: EdmProvDump.cc:43
double * end() const
end of value array for current input variable
Definition: VarProcessor.h:210
ValueIterator(BitSet::Iterator cur, double *values, int *conf, double *output, int *outConf, int *loop, LoopCtx &ctx, unsigned int offset)
Definition: VarProcessor.h:269
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
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:148
BitSet inputVars
bit set to select the input variables to be passed to this processor
Definition: VarProcessor.h:324
A compact container for storing single bits.
Definition: BitSet.h:29
Config_t::const_iterator const_iterator
Definition: VarProcessor.h:66
double operator*()
the (first or only) value for the current input variable
Definition: VarProcessor.h:216
#define end
Definition: vmac.h:37
Container::value_type value_type
double * begin() const
begin of value array for current input variable
Definition: VarProcessor.h:207
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
tuple conf
Definition: dbtoconf.py:185
tuple out
Definition: dbtoconf.py:99
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
std::size_t size_t
Definition: BitSet.h:31
const MVAComputer * computer
Definition: VarProcessor.h:320
ValueIterator & operator<<(double value)
add computed value to current output variable
Definition: VarProcessor.h:224
ConfIterator(BitSet::Iterator cur, ConfigCtx &config)
Definition: VarProcessor.h:187
void operator()()
finish current output variable, move to next slot
Definition: VarProcessor.h:228
VarProcessor(const char *name, const Calibration::VarProcessor *calib, const MVAComputer *computer)
Definition: VarProcessor.cc:38
#define begin
Definition: vmac.h:30
double operator[](unsigned int idx)
value idx of current input variable
Definition: VarProcessor.h:220
Iterator iter() const
create iterator over all set bits
Definition: BitSet.h:179
ValueIterator & operator++()
move to next input variable
Definition: VarProcessor.h:247
Hold context information for looping processors.
Definition: VarProcessor.h:98
virtual std::vector< double > deriv(ValueIterator iter, unsigned int n) const
virtual derivative evaluation method, implemented in actual processor
Definition: VarProcessor.h:315
tuple Config
Definition: helper.py:9
Iterates over all set bits of a BitSet.
Definition: BitSet.h:77
Generic registry template for polymorphic processor implementations.
Helper class for discriminator computer set-up procedure.
Definition: VarProcessor.h:50
Config(Variable::Flags mask, unsigned int origin)
Definition: VarProcessor.h:52
SurfaceDeformation * create(int type, const std::vector< double > &params)
ConfIterator & operator<<(Config config_)
add a new output variable configuration config_
Definition: VarProcessor.h:159
virtual LoopStatus loop(double *output, int *outConf, unsigned int nOutput, LoopCtx &ctx, unsigned int &nOffset) const
Definition: VarProcessor.h:132
Common base class for variable processors.
Definition: VarProcessor.h:36
Variable::Flags operator*() const
return the current input variable flags
Definition: VarProcessor.h:171
const_iterator begin() const
Definition: VarProcessor.h:74
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:117