CMS 3D CMS Logo

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 <cstdlib>
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:
31 
32  ProcMultiply(const char *name, const Calibration::ProcMultiply *calib, const MVAComputer *computer);
33  ~ProcMultiply() override {}
34 
35  void configure(ConfIterator iter, unsigned int n) override;
36  void eval(ValueIterator iter, unsigned int n) const override;
37  std::vector<double> deriv(ValueIterator iter, unsigned int n) const override;
38 
39  private:
40  typedef std::vector<unsigned int> Config;
41 
42  unsigned int in;
43  std::vector<Config> out;
44  };
45 
46  ProcMultiply::Registry registry("ProcMultiply");
47 
48  ProcMultiply::ProcMultiply(const char *name, const Calibration::ProcMultiply *calib, const MVAComputer *computer)
50  std::copy(calib->out.begin(), calib->out.end(), std::back_inserter(out));
51  }
52 
53  void ProcMultiply::configure(ConfIterator iter, unsigned int n) {
54  if (in != n)
55  return;
56 
57  for (unsigned int i = 0; i < in; i++)
58  iter++(Variable::FLAG_NONE);
59 
60  for (unsigned int i = 0; i < out.size(); i++)
61  iter << Variable::FLAG_NONE;
62  }
63 
64  void ProcMultiply::eval(ValueIterator iter, unsigned int n) const {
65  double *values = (double *)alloca(in * sizeof(double));
66  for (double *pos = values; iter; iter++, pos++) {
67  if (iter.size() != 1)
68  throw cms::Exception("ProcMultiply") << "Special input variable encountered "
69  "at index "
70  << (pos - values) << "." << std::endl;
71  *pos = *iter;
72  }
73 
74  for (std::vector<Config>::const_iterator config = out.begin(); config != out.end(); ++config) {
75  double product = 1.0;
76  for (std::vector<unsigned int>::const_iterator var = config->begin(); var != config->end(); var++)
77  product *= values[*var];
78 
79  iter(product);
80  }
81  }
82 
83  std::vector<double> ProcMultiply::deriv(ValueIterator iter, unsigned int n) const {
84  std::vector<double> values;
85  std::vector<unsigned int> offsets;
86  unsigned int size = 0;
87  while (iter) {
88  offsets.push_back(size);
89  size += iter.size();
90  values.push_back(*iter++);
91  }
92 
93  std::vector<double> result(out.size() * size, 0.0);
94  unsigned int k = 0;
95  for (std::vector<Config>::const_iterator config = out.begin(); config != out.end(); ++config, k++) {
96  for (unsigned int i = 0; i < config->size(); i++) {
97  double product = 1.0;
98  for (unsigned int j = 0; j < config->size(); j++)
99  if (i != j)
100  product *= values[(*config)[j]];
101 
102  result[k * size + offsets[i]] = product;
103  }
104  }
105 
106  return result;
107  }
108 
109 } // anonymous namespace
size
Write out results.
template to generate a registry singleton for a type.
Definition: config.py:1
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
Config
Definition: helper.py:10
dictionary config
Read in AllInOne config in JSON format.
Definition: DiMuonV_cfg.py:30
Common base class for variable processors.
Definition: VarProcessor.h:36