test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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 <stdlib.h>
17 
20 
21 using namespace PhysicsTools;
22 
23 namespace { // anonymous
24 
25 class ProcMatrix : public VarProcessor {
26  public:
27  typedef VarProcessor::Registry::Registry<ProcMatrix,
28  Calibration::ProcMatrix> Registry;
29 
30  ProcMatrix(const char *name,
32  const MVAComputer *computer);
33  virtual ~ProcMatrix() {}
34 
35  virtual void configure(ConfIterator iter, unsigned int n) override;
36  virtual void eval(ValueIterator iter, unsigned int n) const override;
37  virtual std::vector<double> deriv(ValueIterator iter,
38  unsigned int n) const override;
39 
40  private:
41  class Matrix {
42  public:
43  inline Matrix(const Calibration::Matrix *calib) :
44  rows(calib->rows), cols(calib->columns),
45  coeffs(calib->elements) {}
46 
47  inline unsigned int getRows() const { return rows; }
48  inline unsigned int getCols() const { return cols; }
49 
50  inline double operator () (unsigned int row,
51  unsigned int col) const
52  { return coeffs[row * cols + col]; }
53 
54  private:
55  unsigned int rows;
56  unsigned int cols;
57  std::vector<double> coeffs;
58  };
59 
60  Matrix matrix;
61 };
62 
63 static ProcMatrix::Registry registry("ProcMatrix");
64 
65 ProcMatrix::ProcMatrix(const char *name,
67  const MVAComputer *computer) :
68  VarProcessor(name, calib, computer),
69  matrix(&calib->matrix)
70 {
71 }
72 
73 void ProcMatrix::configure(ConfIterator iter, unsigned int n)
74 {
75  if (n != matrix.getCols())
76  return;
77 
78  for(unsigned int col = 0; col < matrix.getCols(); col++)
79  iter++(Variable::FLAG_NONE);
80 
81  for(unsigned int row = 0; row < matrix.getRows(); row++)
82  iter << Variable::FLAG_NONE;
83 }
84 
85 void ProcMatrix::eval(ValueIterator iter, unsigned int n) const
86 {
87  double *sums = (double*)alloca(matrix.getRows() * sizeof(double));
88  for(unsigned int row = 0; row < matrix.getRows(); row++)
89  sums[row] = 0.0;
90 
91  for(unsigned int col = 0; col < matrix.getCols(); col++) {
92  double val = *iter++;
93  for(unsigned int row = 0; row < matrix.getRows(); row++)
94  sums[row] += matrix(row, col) * val;
95  }
96 
97  for(unsigned int row = 0; row < matrix.getRows(); row++)
98  iter(sums[row]);
99 }
100 
101 std::vector<double> ProcMatrix::deriv(ValueIterator iter,
102  unsigned int n) const
103 {
104  std::vector<double> result;
105  result.reserve(matrix.getRows() * matrix.getCols());
106 
107  for(unsigned int row = 0; row < matrix.getRows(); row++)
108  for(unsigned int col = 0; col < matrix.getCols(); col++)
109  result.push_back(matrix(row, col));
110 
111  return result;
112 }
113 
114 } // anonymous namespace
template to generate a registry singleton for a type.
CLHEP::HepMatrix Matrix
Definition: matutil.h:65
tuple result
Definition: mps_fire.py:84
dictionary elements
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:39
tuple columns
Definition: mps_check.py:210
static Interceptor::Registry registry("Interceptor")
int col
Definition: cuy.py:1008
Common base class for variable processors.
Definition: VarProcessor.h:36