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
PhysicsTools::VarProcessor::Config::Config
Config()
Definition: VarProcessor.h:48
PhysicsTools::VarProcessor::PluginFunctionPrototype
Dummy * PluginFunctionPrototype()
Definition: VarProcessor.h:141
PhysicsTools::Variable::Flags
Flags
Definition: Variable.h:35
electrons_cff.bool
bool
Definition: electrons_cff.py:366
mps_fire.i
i
Definition: mps_fire.py:428
start
Definition: start.py:1
input
static const std::string input
Definition: EdmProvDump.cc:48
PhysicsTools::VarProcessor::deriv
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
Definition: VarProcessor.cc:105
PhysicsTools::VarProcessor::ValueIterator::conf
const int * conf
Definition: VarProcessor.h:307
PhysicsTools::VarProcessor::Config::Config
Config(Variable::Flags mask, unsigned int origin)
Definition: VarProcessor.h:49
PhysicsTools::VarProcessor::LoopStatus
LoopStatus
Definition: VarProcessor.h:132
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
PhysicsTools::VarProcessor::LoopCtx::size_
unsigned int size_
Definition: VarProcessor.h:105
PhysicsTools::VarProcessor::Dummy
Definition: VarProcessor.h:140
PhysicsTools::VarProcessor::ConfigCtx::Context
Definition: VarProcessor.h:64
PhysicsTools::VarProcessor::ValueIterator::operator()
void operator()()
finish current output variable, move to next slot
Definition: VarProcessor.h:232
PhysicsTools::VarProcessor::ConfigCtx::push_back
void push_back(const Config &config)
Definition: VarProcessor.h:76
PhysicsTools::VarProcessor::ConfigCtx::loop
VarProcessor * loop
Definition: VarProcessor.h:83
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:72
pos
Definition: PixelAliasList.h:18
PhysicsTools::VarProcessor::computer
const MVAComputer * computer
Definition: VarProcessor.h:333
PhysicsTools::VarProcessor::ConfIterator::operator()
ConfIterator & operator()(Variable::Flags mask)
apply property flags mask to variable at current position
Definition: VarProcessor.h:152
PhysicsTools::VarProcessor::ValueIterator::operator++
ValueIterator & operator++()
move to next input variable
Definition: VarProcessor.h:252
PhysicsTools::VarProcessor::ValueIterator::operator*
double operator*()
the (first or only) value for the current input variable
Definition: VarProcessor.h:220
PhysicsTools::VarProcessor::ValueIterator::operator++
ValueIterator operator++(int dummy)
move to next input variable
Definition: VarProcessor.h:267
PhysicsTools::VarProcessor::ValueIterator::size
unsigned int size() const
number of values for current input variable
Definition: VarProcessor.h:208
PhysicsTools::VarProcessor::ConfigCtx::configs
Config_t configs
Definition: VarProcessor.h:82
PhysicsTools::VarProcessor::LoopCtx::index
unsigned int & index()
Definition: VarProcessor.h:98
PhysicsTools::VarProcessor::ConfIterator::operator<<
ConfIterator & operator<<(const ConfIterator &origin)
add a new output variable that inherits values from origin
Definition: VarProcessor.h:167
PhysicsTools::BitSet::size_t
std::size_t size_t
Definition: BitSet.h:31
PhysicsTools::VarProcessor::eval
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
PhysicsTools::VarProcessor::ConfIterator::operator<<
ConfIterator & operator<<(Config config_)
add a new output variable configuration config_
Definition: VarProcessor.h:158
PhysicsTools::VarProcessor::ValueIterator::ctx
LoopCtx & ctx
Definition: VarProcessor.h:303
PhysicsTools::VarProcessor::ConfigCtx::Context::~Context
virtual ~Context()
Definition: VarProcessor.h:65
PhysicsTools::MVAComputer
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
PhysicsTools::VarProcessor::kReset
Definition: VarProcessor.h:132
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
PhysicsTools::VarProcessor::~VarProcessor
virtual ~VarProcessor()
Definition: VarProcessor.cc:42
PhysicsTools::VarProcessor::LoopCtx
Hold context information for looping processors.
Definition: VarProcessor.h:95
ProcessRegistry.h
config
Definition: config.py:1
PhysicsTools::VarProcessor::LoopCtx::index_
unsigned int index_
Definition: VarProcessor.h:103
trigger::size_type
uint16_t size_type
Definition: TriggerTypeDefs.h:18
PhysicsTools::VarProcessor::ConfIterator
Iterator to loop over the input/output variable configuration.
Definition: VarProcessor.h:149
PhysicsTools::Variable
Class describing an input variable.
Definition: Variable.h:33
PhysicsTools::VarProcessor::ValueIterator::operator<<
ValueIterator & operator<<(double value)
add computed value to current output variable
Definition: VarProcessor.h:226
PhysicsTools::VarProcessor
Common base class for variable processors.
Definition: VarProcessor.h:36
PhysicsTools::VarProcessor::VarProcessor
VarProcessor(const char *name, const Calibration::VarProcessor *calib, const MVAComputer *computer)
Definition: VarProcessor.cc:39
PhysicsTools::VarProcessor::LoopCtx::LoopCtx
LoopCtx()
Definition: VarProcessor.h:97
PhysicsTools::VarProcessor::ConfigCtx::~ConfigCtx
~ConfigCtx()
Definition: VarProcessor.h:69
PhysicsTools::VarProcessor::deriv
virtual std::vector< double > deriv(ValueIterator iter, unsigned int n) const
virtual derivative evaluation method, implemented in actual processor
Definition: VarProcessor.h:330
mps_fire.end
end
Definition: mps_fire.py:242
PhysicsTools::VarProcessor::LoopCtx::offset
unsigned int & offset()
Definition: VarProcessor.h:99
BitSet.h
PhysicsTools::BitSet::iter
Iterator iter() const
create iterator over all set bits
Definition: BitSet.h:179
PhysicsTools::VarProcessor::loop
virtual LoopStatus loop(double *output, int *outConf, unsigned int nOutput, LoopCtx &ctx, unsigned int &nOffset) const
Definition: VarProcessor.h:134
PhysicsTools::VarProcessor::Config::mask
Variable::Flags mask
Definition: VarProcessor.h:51
PhysicsTools::VarProcessor::ValueIterator::end
double * end() const
end of value array for current input variable
Definition: VarProcessor.h:214
PhysicsTools::VarProcessor::ConfigCtx::begin
const_iterator begin() const
Definition: VarProcessor.h:72
calib
Definition: CalibElectron.h:12
PhysicsTools::VarProcessor::ValueIterator::offset
const unsigned int offset
Definition: VarProcessor.h:304
PhysicsTools::VarProcessor::ValueIterator::operator[]
double operator[](unsigned int idx)
value idx of current input variable
Definition: VarProcessor.h:223
PhysicsTools::VarProcessor::nInputVars
unsigned int nInputVars
Definition: VarProcessor.h:338
PhysicsTools::VarProcessor::ValueIterator::begin
double * begin() const
begin of value array for current input variable
Definition: VarProcessor.h:211
PhysicsTools::VarProcessor::ValueIterator::outConf
int * outConf
Definition: VarProcessor.h:310
PhysicsTools::ProcessRegistry::Factory::create
static Base_t * create(const char *name, const CalibBase_t *calib, Parent_t *parent=0)
PhysicsTools::VarProcessor::ConfigCtx::ctx
Context * ctx
Definition: VarProcessor.h:84
PhysicsTools::VarProcessor::kSkip
Definition: VarProcessor.h:132
recoMuon::in
Definition: RecoMuonEnumerators.h:6
PhysicsTools
Definition: Histogram.h:13
PhysicsTools::VarProcessor::LoopCtx::size
unsigned int & size()
Definition: VarProcessor.h:100
PhysicsTools::VarProcessor::ConfigCtx::size
size_type size() const
Definition: VarProcessor.h:71
PhysicsTools::VarProcessor::ConfigCtx::end
iterator end()
Definition: VarProcessor.h:75
PhysicsTools::VarProcessor::Config::origin
unsigned int origin
Definition: VarProcessor.h:52
PhysicsTools::VarProcessor::ValueIterator::values
double * values
Definition: VarProcessor.h:306
PhysicsTools::VarProcessor::ConfigCtx::const_iterator
Config_t::const_iterator const_iterator
Definition: VarProcessor.h:62
PhysicsTools::VarProcessor::ConfigCtx::size_type
Config_t::size_type size_type
Definition: VarProcessor.h:60
PhysicsTools::VarProcessor::ConfIterator::operator*
Variable::Flags operator*() const
return the current input variable flags
Definition: VarProcessor.h:172
PhysicsTools::VarProcessor::LoopCtx::offset_
unsigned int offset_
Definition: VarProcessor.h:104
value
Definition: value.py:1
PhysicsTools::VarProcessor::configure
void configure(ConfigCtx &config)
called from the discriminator computer to configure processor
Definition: VarProcessor.cc:47
PhysicsTools::VarProcessor::ValueIterator::loop
const int * loop
Definition: VarProcessor.h:308
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
PhysicsTools::VarProcessor::ConfIterator::operator++
ConfIterator & operator++()
move to next input variable
Definition: VarProcessor.h:178
PhysicsTools::VarProcessor::configureLoop
virtual ConfigCtx::Context * configureLoop(ConfigCtx::Context *ctx, ConfigCtx::iterator begin, ConfigCtx::iterator cur, ConfigCtx::iterator end)
virtual loop configure method
Definition: VarProcessor.cc:74
PhysicsTools::BitSet
A compact container for storing single bits.
Definition: BitSet.h:29
PhysicsTools::VarProcessor::ConfigCtx::value_type
Config_t::value_type value_type
Definition: VarProcessor.h:59
PhysicsTools::VarProcessor::ConfIterator::cur
BitSet::Iterator cur
Definition: VarProcessor.h:196
PhysicsTools::VarProcessor::kStop
Definition: VarProcessor.h:132
PhysicsTools::VarProcessor::ValueIterator::output
double * output
Definition: VarProcessor.h:309
CalibrationFwd.h
PhysicsTools::VarProcessor::ValueIterator::start
double *const start
Definition: VarProcessor.h:305
PhysicsTools::VarProcessor::ConfigCtx::end
const_iterator end() const
Definition: VarProcessor.h:74
PhysicsTools::VarProcessor::ValueIterator::loopCtx
LoopCtx & loopCtx()
Definition: VarProcessor.h:249
PhysicsTools::VarProcessor::ConfigCtx::Config_t
std::vector< Config > Config_t
Definition: VarProcessor.h:57
PhysicsTools::VarProcessor::ValueIterator::ValueIterator
ValueIterator(BitSet::Iterator cur, double *values, int *conf, double *output, int *outConf, int *loop, LoopCtx &ctx, unsigned int offset)
Definition: VarProcessor.h:276
PhysicsTools::VarProcessor::ValueIterator
Definition: VarProcessor.h:205
PhysicsTools::VarProcessor::ConfIterator::operator<<
ConfIterator & operator<<(Variable::Flags mask)
add a new output variable configuration with mask mask
Definition: VarProcessor.h:164
PhysicsTools::ProcessRegistry
Generic registry template for polymorphic processor implementations.
Definition: ProcessRegistry.h:39
relativeConstraints.value
value
Definition: relativeConstraints.py:53
PhysicsTools::VarProcessor::kNext
Definition: VarProcessor.h:132
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
PhysicsTools::VarProcessor::ConfigCtx::ConfigCtx
ConfigCtx(const std::vector< Variable::Flags > &flags)
Definition: VarProcessor.cc:69
PhysicsTools::VarProcessor::ConfIterator::config
ConfigCtx & config
Definition: VarProcessor.h:197
PhysicsTools::VarProcessor::ConfigCtx
Definition: VarProcessor.h:55
PhysicsTools::VarProcessor::ValueIterator::cur
BitSet::Iterator cur
Definition: VarProcessor.h:302
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
PhysicsTools::VarProcessor::ConfigCtx::iterator
Config_t::iterator iterator
Definition: VarProcessor.h:61
PhysicsTools::VarProcessor::inputVars
BitSet inputVars
bit set to select the input variables to be passed to this processor
Definition: VarProcessor.h:337
PhysicsTools::VarProcessor::ConfigCtx::operator[]
Config & operator[](size_type i)
Definition: VarProcessor.h:77
dummy
Definition: DummySelector.h:38
Config
Definition: Config.py:1
PhysicsTools::VarProcessor::ConfigCtx::begin
iterator begin()
Definition: VarProcessor.h:73
Variable.h
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
PhysicsTools::VarProcessor::Registry
ProcessRegistry< VarProcessor, Calibration::VarProcessor, const MVAComputer > Registry
Definition: VarProcessor.h:313
HLT_FULL_cff.flags
flags
Definition: HLT_FULL_cff.py:13168
PhysicsTools::BitSet::Iterator
Iterates over all set bits of a BitSet.
Definition: BitSet.h:77
PhysicsTools::Calibration::VarProcessor
Definition: MVAComputer.h:55
PhysicsTools::VarProcessor::ValueIterator::empty
bool empty() const
checks for existence of values for current input variable
Definition: VarProcessor.h:217
PhysicsTools::VarProcessor::ValueIterator::operator()
void operator()(double value)
add value as output variable and move to next slot
Definition: VarProcessor.h:241
PhysicsTools::VarProcessor::ConfIterator::operator++
ConfIterator operator++(int dummy)
move to next input variable
Definition: VarProcessor.h:184
helper.Config
Config
Definition: helper.py:10
PhysicsTools::VarProcessor::ConfIterator::ConfIterator
ConfIterator(BitSet::Iterator cur, ConfigCtx &config)
Definition: VarProcessor.h:193