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 // $Id: ProcMatrix.cc,v 1.4 2009/06/03 09:50:14 saout Exp $
15 //
16 
17 #include <stdlib.h>
18 
21 
22 using namespace PhysicsTools;
23 
24 namespace { // anonymous
25 
26 class ProcMatrix : public VarProcessor {
27  public:
28  typedef VarProcessor::Registry::Registry<ProcMatrix,
30 
31  ProcMatrix(const char *name,
33  const MVAComputer *computer);
34  virtual ~ProcMatrix() {}
35 
36  virtual void configure(ConfIterator iter, unsigned int n);
37  virtual void eval(ValueIterator iter, unsigned int n) const;
38  virtual std::vector<double> deriv(ValueIterator iter,
39  unsigned int n) const;
40 
41  private:
42  class Matrix {
43  public:
44  inline Matrix(const Calibration::Matrix *calib) :
45  rows(calib->rows), cols(calib->columns),
46  coeffs(calib->elements) {}
47 
48  inline unsigned int getRows() const { return rows; }
49  inline unsigned int getCols() const { return cols; }
50 
51  inline double operator () (unsigned int row,
52  unsigned int col) const
53  { return coeffs[row * cols + col]; }
54 
55  private:
56  unsigned int rows;
57  unsigned int cols;
58  std::vector<double> coeffs;
59  };
60 
61  Matrix matrix;
62 };
63 
64 static ProcMatrix::Registry registry("ProcMatrix");
65 
66 ProcMatrix::ProcMatrix(const char *name,
68  const MVAComputer *computer) :
69  VarProcessor(name, calib, computer),
70  matrix(&calib->matrix)
71 {
72 }
73 
74 void ProcMatrix::configure(ConfIterator iter, unsigned int n)
75 {
76  if (n != matrix.getCols())
77  return;
78 
79  for(unsigned int col = 0; col < matrix.getCols(); col++)
80  iter++(Variable::FLAG_NONE);
81 
82  for(unsigned int row = 0; row < matrix.getRows(); row++)
83  iter << Variable::FLAG_NONE;
84 }
85 
86 void ProcMatrix::eval(ValueIterator iter, unsigned int n) const
87 {
88  double *sums = (double*)alloca(matrix.getRows() * sizeof(double));
89  for(unsigned int row = 0; row < matrix.getRows(); row++)
90  sums[row] = 0.0;
91 
92  for(unsigned int col = 0; col < matrix.getCols(); col++) {
93  double val = *iter++;
94  for(unsigned int row = 0; row < matrix.getRows(); row++)
95  sums[row] += matrix(row, col) * val;
96  }
97 
98  for(unsigned int row = 0; row < matrix.getRows(); row++)
99  iter(sums[row]);
100 }
101 
102 std::vector<double> ProcMatrix::deriv(ValueIterator iter,
103  unsigned int n) const
104 {
105  std::vector<double> result;
106  result.reserve(matrix.getRows() * matrix.getCols());
107 
108  for(unsigned int row = 0; row < matrix.getRows(); row++)
109  for(unsigned int col = 0; col < matrix.getCols(); col++)
110  result.push_back(matrix(row, col));
111 
112  return result;
113 }
114 
115 } // anonymous namespace
list elements
Definition: asciidump.py:414
detail::ThreadSafeRegistry< ParameterSetID, ParameterSet, ProcessParameterSetIDCache > Registry
Definition: Registry.h:37
CLHEP::HepMatrix Matrix
Definition: matutil.h:66
MVATrainerComputer * calib
Definition: MVATrainer.cc:64
Main interface class to the generic discriminator computer framework.
Definition: MVAComputer.h:40
tuple result
Definition: query.py:137
template to generate a registry singleton for a type.
static Interceptor::Registry registry("Interceptor")
Common base class for variable processors.
Definition: VarProcessor.h:39