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 // $Id: VarProcessor.h,v 1.11 2013/05/23 17:02:16 gartung Exp $
13 //
14 
15 #include <algorithm>
16 #include <vector>
17 
22 
23 namespace PhysicsTools {
24 
25 // forward declaration
26 class MVAComputer;
27 
37 class VarProcessor :
38  public ProcessRegistry<VarProcessor,
39  Calibration::VarProcessor,
40  const MVAComputer>::Factory {
41  public:
51  struct Config {
52  inline Config() : mask(Variable::FLAG_NONE), origin(0) {}
53  inline Config(Variable::Flags mask, unsigned int origin) :
54  mask(mask), origin(origin) {}
55 
57  unsigned int origin;
58  };
59 
60  class ConfigCtx {
61  public:
62  typedef std::vector<Config> Config_t;
63 
66  typedef Config_t::iterator iterator;
67  typedef Config_t::const_iterator const_iterator;
68 
69  struct Context { virtual ~Context() {} };
70 
71  ConfigCtx(const std::vector<Variable::Flags>& flags);
72  ~ConfigCtx() { delete ctx; }
73 
74  inline size_type size() const { return configs.size(); }
75  inline const_iterator begin() const { return configs.begin(); }
76  inline iterator begin() { return configs.begin(); }
77  inline const_iterator end() const { return configs.end(); }
78  inline iterator end() { return configs.end(); }
79  inline void push_back(const Config &config) { configs.push_back(config); }
80  inline Config &operator [] (size_type i) { return configs[i]; }
81 
82  private:
83  friend class VarProcessor;
84 
88  };
89 
90  virtual ~VarProcessor();
91 
93  void configure(ConfigCtx &config);
94 
96  inline void
97  eval(double *input, int *conf, double *output, int *outConf,
98  int *loop, unsigned int offset) const
99  {
101  output, outConf, loop, offset);
102  eval(iter, nInputVars);
103  }
104 
106  void deriv(double *input, int *conf, double *output, int *outConf,
107  int *loop, unsigned int offset, unsigned int in,
108  unsigned int out, std::vector<double> &deriv) const;
109 
111 
112  virtual LoopStatus loop(double *output, int *outConf,
113  unsigned int nOutput,
114  unsigned int &nOffset) const
115  { return kStop; }
116 
117  //used to create a PluginFactory
118  struct Dummy {};
120 
121  protected:
127  struct ConfIterator {
128  public:
131  {
132  config[cur()].mask =
133  (Variable::Flags)(config[cur()].mask & mask);
134  return *this;
135  }
136 
139  { config.push_back(config_); return *this; }
140 
143  { return *this << Config(mask, 0); }
144 
147  { return *this << Config(config[origin.cur()].mask, origin.cur()); }
148 
151  { return config[cur()].mask; }
152 
154  inline operator bool() const { return cur; }
155 
157  ConfIterator &operator ++ () { ++cur; return *this; }
158 
160  inline ConfIterator operator ++ (int dummy)
161  { ConfIterator orig = *this; operator ++ (); return orig; }
162 
163  protected:
164  friend class VarProcessor;
165 
167  cur(cur), config(config) {}
168 
169  private:
172  };
173 
179  struct ValueIterator {
180  public:
182  inline unsigned int size() const
183  { return loop ? (conf[1] - conf[0]) : 1; }
184 
186  inline double *begin() const { return values; }
187 
189  inline double *end() const { return values + size(); }
190 
192  inline bool empty() const { return begin() == end(); }
193 
195  inline double operator * ()
196  { return *values; }
197 
199  inline double operator [] (unsigned int idx)
200  { return values[idx]; }
201 
204  { *output++ = value; return *this; }
205 
207  inline void operator () ()
208  {
209  int pos = output - start;
210  if (*++outConf > pos)
211  output = start + *outConf;
212  else
213  *outConf = pos;
214  }
215 
217  inline void operator () (double value)
218  { *this << value; (*this)(); }
219 
221  inline operator bool() const { return cur; }
222 
225  {
226  BitSet::size_t orig = cur();
227  if (++cur) {
228  unsigned int prev = *conf;
229  conf += cur() - orig;
230  values += *conf - prev;
231  if (loop && conf >= loop) {
232  values += offset;
233  loop = 0;
234  }
235  }
236  return *this;
237  }
238 
240  inline ValueIterator operator ++ (int dummy)
241  { ValueIterator orig = *this; operator ++ (); return orig; }
242 
243  protected:
244  friend class VarProcessor;
245 
247  int *conf, double *output, int *outConf,
248  int *loop, unsigned int offset) :
249  cur(cur), offset(offset), start(values + offset),
250  values(values), conf(conf), loop(loop),
251  output(output + offset), outConf(outConf)
252  {
253  this->conf += cur();
254  this->values += *this->conf;
255  if (loop && this->conf >= loop) {
256  this->values += offset;
257  this->loop = 0;
258  }
259  }
260 
261  private:
263  const unsigned int offset;
264  double *const start;
265  double *values;
266  const int *conf;
267  const int *loop;
268  double *output;
269  int *outConf;
270  };
271 
275 
276  VarProcessor(const char *name, const Calibration::VarProcessor *calib,
277  const MVAComputer *computer);
278 
280  virtual void configure(ConfIterator iter, unsigned int n) = 0;
281 
283  virtual ConfigCtx::Context *
286 
288  virtual void eval(ValueIterator iter, unsigned int n) const = 0;
289 
291  virtual std::vector<double> deriv(ValueIterator iter,
292  unsigned int n) const
293  { return std::vector<double>(); }
294 
295  protected:
297 
298  private:
301  unsigned int nInputVars;
302 };
303 
304 template<>
307  const char*, const Calibration::VarProcessor*, const MVAComputer*);
308 
309 } // namespace PhysicsTools
310 
311 
312 #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:88
int i
Definition: DBlmapReader.cc:9
Config & operator[](size_type i)
Definition: VarProcessor.h:80
unsigned int size() const
number of values for current input variable
Definition: VarProcessor.h:182
void configure(ConfigCtx &config)
called from the discriminator computer to configure processor
Definition: VarProcessor.cc:54
Class describing an input variable.
Definition: Variable.h:34
void push_back(const Config &config)
Definition: VarProcessor.h:79
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
void deriv(double *input, int *conf, double *output, int *outConf, int *loop, 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
ConfIterator & operator()(Variable::Flags mask)
apply property flags mask to variable at current position
Definition: VarProcessor.h:130
void eval(double *input, int *conf, double *output, int *outConf, int *loop, unsigned int offset) const
run the processor evaluation pass on this processor
Definition: VarProcessor.h:97
ProcessRegistry< VarProcessor, Calibration::VarProcessor, const MVAComputer > Registry
Definition: VarProcessor.h:274
bool empty() const
checks for existence of values for current input variable
Definition: VarProcessor.h:192
uint16_t size_type
ConfigCtx(const std::vector< Variable::Flags > &flags)
Definition: VarProcessor.cc:79
double * end() const
end of value array for current input variable
Definition: VarProcessor.h:189
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:40
Iterator to loop over the input/output variable configuration.
Definition: VarProcessor.h:127
BitSet inputVars
bit set to select the input variables to be passed to this processor
Definition: VarProcessor.h:300
A compact container for storing single bits.
Definition: BitSet.h:30
Config_t::const_iterator const_iterator
Definition: VarProcessor.h:67
double operator*()
the (first or only) value for the current input variable
Definition: VarProcessor.h:195
#define end
Definition: vmac.h:38
unsigned int offset(bool)
Container::value_type value_type
double * begin() const
begin of value array for current input variable
Definition: VarProcessor.h:186
ConfIterator & operator++()
move to next input variable
Definition: VarProcessor.h:157
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:32
const MVAComputer * computer
Definition: VarProcessor.h:296
ValueIterator & operator<<(double value)
add computed value to current output variable
Definition: VarProcessor.h:203
virtual LoopStatus loop(double *output, int *outConf, unsigned int nOutput, unsigned int &nOffset) const
Definition: VarProcessor.h:112
ConfIterator(BitSet::Iterator cur, ConfigCtx &config)
Definition: VarProcessor.h:166
void operator()()
finish current output variable, move to next slot
Definition: VarProcessor.h:207
VarProcessor(const char *name, const Calibration::VarProcessor *calib, const MVAComputer *computer)
Definition: VarProcessor.cc:39
#define begin
Definition: vmac.h:31
double operator[](unsigned int idx)
value idx of current input variable
Definition: VarProcessor.h:199
Iterator iter() const
create iterator over all set bits
Definition: BitSet.h:180
ValueIterator & operator++()
move to next input variable
Definition: VarProcessor.h:224
virtual std::vector< double > deriv(ValueIterator iter, unsigned int n) const
virtual derivative evaluation method, implemented in actual processor
Definition: VarProcessor.h:291
Iterates over all set bits of a BitSet.
Definition: BitSet.h:78
Generic registry template for polymorphic processor implementations.
ValueIterator(BitSet::Iterator cur, double *values, int *conf, double *output, int *outConf, int *loop, unsigned int offset)
Definition: VarProcessor.h:246
Helper class for discriminator computer set-up procedure.
Definition: VarProcessor.h:51
Config(Variable::Flags mask, unsigned int origin)
Definition: VarProcessor.h:53
SurfaceDeformation * create(int type, const std::vector< double > &params)
ConfIterator & operator<<(Config config_)
add a new output variable configuration config_
Definition: VarProcessor.h:138
Common base class for variable processors.
Definition: VarProcessor.h:37
Variable::Flags operator*() const
return the current input variable flags
Definition: VarProcessor.h:150
const_iterator begin() const
Definition: VarProcessor.h:75