CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ProcMultiply.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MVAComputer
4 // Class :
5 //
6 
7 // Implementation:
8 // Multiplies n input variables to produce one output variable.
9 //
10 // Author: Christophe Saout
11 // Created: Sat Apr 24 15:18 CEST 2007
12 //
13 
14 #include <stdlib.h>
15 #include <algorithm>
16 #include <iterator>
17 #include <vector>
18 
20 
23 
24 using namespace PhysicsTools;
25 
26 namespace { // anonymous
27 
28 class ProcMultiply : public VarProcessor {
29  public:
30  typedef VarProcessor::Registry::Registry<ProcMultiply,
31  Calibration::ProcMultiply> Registry;
32 
33  ProcMultiply(const char *name,
35  const MVAComputer *computer);
36  virtual ~ProcMultiply() {}
37 
38  virtual void configure(ConfIterator iter, unsigned int n) override;
39  virtual void eval(ValueIterator iter, unsigned int n) const override;
40  virtual std::vector<double> deriv(
41  ValueIterator iter, unsigned int n) const override;
42 
43  private:
44  typedef std::vector<unsigned int> Config;
45 
46  unsigned int in;
47  std::vector<Config> out;
48 };
49 
50 static ProcMultiply::Registry registry("ProcMultiply");
51 
52 ProcMultiply::ProcMultiply(const char *name,
54  const MVAComputer *computer) :
55  VarProcessor(name, calib, computer),
56  in(calib->in)
57 {
58  std::copy(calib->out.begin(), calib->out.end(),
59  std::back_inserter(out));
60 }
61 
62 void ProcMultiply::configure(ConfIterator iter, unsigned int n)
63 {
64  if (in != n)
65  return;
66 
67  for(unsigned int i = 0; i < in; i++)
68  iter++(Variable::FLAG_NONE);
69 
70  for(unsigned int i = 0; i < out.size(); i++)
71  iter << Variable::FLAG_NONE;
72 }
73 
74 void ProcMultiply::eval(ValueIterator iter, unsigned int n) const
75 {
76  double *values = (double*)alloca(in * sizeof(double));
77  for(double *pos = values; iter; iter++, pos++) {
78  if (iter.size() != 1)
79  throw cms::Exception("ProcMultiply")
80  << "Special input variable encountered "
81  "at index " << (pos - values) << "."
82  << std::endl;
83  *pos = *iter;
84  }
85 
86  for(std::vector<Config>::const_iterator config = out.begin();
87  config != out.end(); ++config) {
88  double product = 1.0;
89  for(std::vector<unsigned int>::const_iterator var =
90  config->begin();
91  var != config->end(); var++)
92  product *= values[*var];
93 
94  iter(product);
95  }
96 }
97 
98 std::vector<double> ProcMultiply::deriv(
99  ValueIterator iter, unsigned int n) const
100 {
101  std::vector<double> values;
102  std::vector<unsigned int> offsets;
103  unsigned int size = 0;
104  while(iter) {
105  offsets.push_back(size);
106  size += iter.size();
107  values.push_back(*iter++);
108  }
109 
110  std::vector<double> result(out.size() * size, 0.0);
111  unsigned int k = 0;
112  for(std::vector<Config>::const_iterator config = out.begin();
113  config != out.end(); ++config, k++) {
114  for(unsigned int i = 0; i < config->size(); i++) {
115  double product = 1.0;
116  for(unsigned int j = 0; j < config->size(); j++)
117  if (i != j)
118  product *= values[(*config)[j]];
119 
120  result[k * size + offsets[i]] = product;
121  }
122  }
123 
124  return result;
125 }
126 
127 } // anonymous namespace
int i
Definition: DBlmapReader.cc:9
template to generate a registry singleton for a type.
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
tuple result
Definition: query.py:137
int j
Definition: DBlmapReader.cc:9
tuple out
Definition: dbtoconf.py:99
tuple Config
Definition: helper.py:9
static Interceptor::Registry registry("Interceptor")
tuple size
Write out results.
Common base class for variable processors.
Definition: VarProcessor.h:36