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  virtual ~VarProcessor();
90 
92  void configure(ConfigCtx &config);
93 
95  inline void
96  eval(double *input, int *conf, double *output, int *outConf,
97  int *loop, unsigned int offset) const
98  {
100  output, outConf, loop, offset);
101  eval(iter, nInputVars);
102  }
103 
105  void deriv(double *input, int *conf, double *output, int *outConf,
106  int *loop, unsigned int offset, unsigned int in,
107  unsigned int out, std::vector<double> &deriv) const;
108 
110 
111  virtual LoopStatus loop(double *output, int *outConf,
112  unsigned int nOutput,
113  unsigned int &nOffset) const
114  { return kStop; }
115 
116  //used to create a PluginFactory
117  struct Dummy {};
119 
120  protected:
126  struct ConfIterator {
127  public:
130  {
131  config[cur()].mask =
132  (Variable::Flags)(config[cur()].mask & mask);
133  return *this;
134  }
135 
138  { config.push_back(config_); return *this; }
139 
142  { return *this << Config(mask, 0); }
143 
146  { return *this << Config(config[origin.cur()].mask, origin.cur()); }
147 
150  { return config[cur()].mask; }
151 
153  inline operator bool() const { return cur; }
154 
156  ConfIterator &operator ++ () { ++cur; return *this; }
157 
159  inline ConfIterator operator ++ (int dummy)
160  { ConfIterator orig = *this; operator ++ (); return orig; }
161 
162  protected:
163  friend class VarProcessor;
164 
166  cur(cur), config(config) {}
167 
168  private:
171  };
172 
178  struct ValueIterator {
179  public:
181  inline unsigned int size() const
182  { return loop ? (conf[1] - conf[0]) : 1; }
183 
185  inline double *begin() const { return values; }
186 
188  inline double *end() const { return values + size(); }
189 
191  inline bool empty() const { return begin() == end(); }
192 
194  inline double operator * ()
195  { return *values; }
196 
198  inline double operator [] (unsigned int idx)
199  { return values[idx]; }
200 
203  { *output++ = value; return *this; }
204 
206  inline void operator () ()
207  {
208  int pos = output - start;
209  if (*++outConf > pos)
210  output = start + *outConf;
211  else
212  *outConf = pos;
213  }
214 
216  inline void operator () (double value)
217  { *this << value; (*this)(); }
218 
220  inline operator bool() const { return cur; }
221 
224  {
225  BitSet::size_t orig = cur();
226  if (++cur) {
227  unsigned int prev = *conf;
228  conf += cur() - orig;
229  values += *conf - prev;
230  if (loop && conf >= loop) {
231  values += offset;
232  loop = 0;
233  }
234  }
235  return *this;
236  }
237 
239  inline ValueIterator operator ++ (int dummy)
240  { ValueIterator orig = *this; operator ++ (); return orig; }
241 
242  protected:
243  friend class VarProcessor;
244 
246  int *conf, double *output, int *outConf,
247  int *loop, unsigned int offset) :
248  cur(cur), offset(offset), start(values + offset),
249  values(values), conf(conf), loop(loop),
250  output(output + offset), outConf(outConf)
251  {
252  this->conf += cur();
253  this->values += *this->conf;
254  if (loop && this->conf >= loop) {
255  this->values += offset;
256  this->loop = 0;
257  }
258  }
259 
260  private:
262  const unsigned int offset;
263  double *const start;
264  double *values;
265  const int *conf;
266  const int *loop;
267  double *output;
268  int *outConf;
269  };
270 
274 
275  VarProcessor(const char *name, const Calibration::VarProcessor *calib,
276  const MVAComputer *computer);
277 
279  virtual void configure(ConfIterator iter, unsigned int n) = 0;
280 
282  virtual ConfigCtx::Context *
285 
287  virtual void eval(ValueIterator iter, unsigned int n) const = 0;
288 
290  virtual std::vector<double> deriv(ValueIterator iter,
291  unsigned int n) const
292  { return std::vector<double>(); }
293 
294  protected:
296 
297  private:
300  unsigned int nInputVars;
301 };
302 
303 template<>
306  const char*, const Calibration::VarProcessor*, const MVAComputer*);
307 
308 } // namespace PhysicsTools
309 
310 
311 #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:181
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
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:129
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:96
ProcessRegistry< VarProcessor, Calibration::VarProcessor, const MVAComputer > Registry
Definition: VarProcessor.h:273
bool empty() const
checks for existence of values for current input variable
Definition: VarProcessor.h:191
uint16_t size_type
ConfigCtx(const std::vector< Variable::Flags > &flags)
Definition: VarProcessor.cc:78
static std::string const input
Definition: EdmProvDump.cc:44
double * end() const
end of value array for current input variable
Definition: VarProcessor.h:188
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:126
BitSet inputVars
bit set to select the input variables to be passed to this processor
Definition: VarProcessor.h:299
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:194
#define end
Definition: vmac.h:37
unsigned int offset(bool)
Container::value_type value_type
double * begin() const
begin of value array for current input variable
Definition: VarProcessor.h:185
ConfIterator & operator++()
move to next input variable
Definition: VarProcessor.h:156
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:295
ValueIterator & operator<<(double value)
add computed value to current output variable
Definition: VarProcessor.h:202
virtual LoopStatus loop(double *output, int *outConf, unsigned int nOutput, unsigned int &nOffset) const
Definition: VarProcessor.h:111
ConfIterator(BitSet::Iterator cur, ConfigCtx &config)
Definition: VarProcessor.h:165
void operator()()
finish current output variable, move to next slot
Definition: VarProcessor.h:206
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:198
Iterator iter() const
create iterator over all set bits
Definition: BitSet.h:179
ValueIterator & operator++()
move to next input variable
Definition: VarProcessor.h:223
virtual std::vector< double > deriv(ValueIterator iter, unsigned int n) const
virtual derivative evaluation method, implemented in actual processor
Definition: VarProcessor.h:290
Iterates over all set bits of a BitSet.
Definition: BitSet.h:77
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:245
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:137
Common base class for variable processors.
Definition: VarProcessor.h:36
Variable::Flags operator*() const
return the current input variable flags
Definition: VarProcessor.h:149
const_iterator begin() const
Definition: VarProcessor.h:74