CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ProcMatrix.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: MVAComputer
4 // Class : ProcMatrix
5 //
6 
7 // Implementation:
8 // Variable processor to apply a matrix transformation to the input
9 // variables. An n x m matrix applied to n input variables results in
10 // m output variables.
11 //
12 // Author: Christophe Saout
13 // Created: Sat Apr 24 15:18 CEST 2007
14 //
15 
16 #include <cstdlib>
17 
20 
21 using namespace PhysicsTools;
22 
23 namespace { // anonymous
24 
25  class ProcMatrix : public VarProcessor {
26  public:
28 
29  ProcMatrix(const char *name, const Calibration::ProcMatrix *calib, const MVAComputer *computer);
30  ~ProcMatrix() override {}
31 
32  void configure(ConfIterator iter, unsigned int n) override;
33  void eval(ValueIterator iter, unsigned int n) const override;
34  std::vector<double> deriv(ValueIterator iter, unsigned int n) const override;
35 
36  private:
37  class Matrix {
38  public:
39  inline Matrix(const Calibration::Matrix *calib)
40  : rows(calib->rows), cols(calib->columns), coeffs(calib->elements) {}
41 
42  inline unsigned int getRows() const { return rows; }
43  inline unsigned int getCols() const { return cols; }
44 
45  inline double operator()(unsigned int row, unsigned int col) const { return coeffs[row * cols + col]; }
46 
47  private:
48  unsigned int rows;
49  unsigned int cols;
50  std::vector<double> coeffs;
51  };
52 
53  Matrix matrix;
54  };
55 
56  ProcMatrix::Registry registry("ProcMatrix");
57 
58  ProcMatrix::ProcMatrix(const char *name, const Calibration::ProcMatrix *calib, const MVAComputer *computer)
59  : VarProcessor(name, calib, computer), matrix(&calib->matrix) {}
60 
61  void ProcMatrix::configure(ConfIterator iter, unsigned int n) {
62  if (n != matrix.getCols())
63  return;
64 
65  for (unsigned int col = 0; col < matrix.getCols(); col++)
66  iter++(Variable::FLAG_NONE);
67 
68  for (unsigned int row = 0; row < matrix.getRows(); row++)
69  iter << Variable::FLAG_NONE;
70  }
71 
72  void ProcMatrix::eval(ValueIterator iter, unsigned int n) const {
73  double *sums = (double *)alloca(matrix.getRows() * sizeof(double));
74  for (unsigned int row = 0; row < matrix.getRows(); row++)
75  sums[row] = 0.0;
76 
77  for (unsigned int col = 0; col < matrix.getCols(); col++) {
78  double val = *iter++;
79  for (unsigned int row = 0; row < matrix.getRows(); row++)
80  sums[row] += matrix(row, col) * val;
81  }
82 
83  for (unsigned int row = 0; row < matrix.getRows(); row++)
84  iter(sums[row]);
85  }
86 
87  std::vector<double> ProcMatrix::deriv(ValueIterator iter, unsigned int n) const {
88  std::vector<double> result;
89  result.reserve(matrix.getRows() * matrix.getCols());
90 
91  for (unsigned int row = 0; row < matrix.getRows(); row++)
92  for (unsigned int col = 0; col < matrix.getCols(); col++)
93  result.push_back(matrix(row, col));
94 
95  return result;
96  }
97 
98 } // anonymous namespace
template to generate a registry singleton for a type.
CLHEP::HepMatrix Matrix
Definition: matutil.h:62
tuple result
Definition: mps_fire.py:311
dictionary elements
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
tuple columns
Definition: mps_check.py:244
int col
Definition: cuy.py:1009
Common base class for variable processors.
Definition: VarProcessor.h:36