CMS 3D CMS Logo

ProcMatrix.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     MVAComputer
00004 // Class  :     ProcMatrix
00005 // 
00006 
00007 // Implementation:
00008 //     Variable processor to apply a matrix transformation to the input
00009 //     variables. An n x m matrix applied to n input variables results in
00010 //     m output variables.
00011 //
00012 // Author:      Christophe Saout
00013 // Created:     Sat Apr 24 15:18 CEST 2007
00014 // $Id: ProcMatrix.cc,v 1.3 2007/07/15 22:31:46 saout Exp $
00015 //
00016 
00017 #include <stdlib.h>
00018 
00019 #include "PhysicsTools/MVAComputer/interface/VarProcessor.h"
00020 #include "PhysicsTools/MVAComputer/interface/Calibration.h"
00021 
00022 using namespace PhysicsTools;
00023 
00024 namespace { // anonymous
00025 
00026 class ProcMatrix : public VarProcessor {
00027     public:
00028         typedef VarProcessor::Registry::Registry<ProcMatrix,
00029                                         Calibration::ProcMatrix> Registry;
00030 
00031         ProcMatrix(const char *name,
00032                    const Calibration::ProcMatrix *calib,
00033                    const MVAComputer *computer);
00034         virtual ~ProcMatrix() {}      
00035 
00036         virtual void configure(ConfIterator iter, unsigned int n);
00037         virtual void eval(ValueIterator iter, unsigned int n) const;
00038 
00039     private:
00040         class Matrix {
00041             public:
00042                 inline Matrix(const Calibration::Matrix *calib) :
00043                         rows(calib->rows), cols(calib->columns),
00044                         coeffs(calib->elements) {}
00045 
00046                 inline unsigned int getRows() const { return rows; }
00047                 inline unsigned int getCols() const { return cols; }
00048 
00049                 inline double operator () (unsigned int row,
00050                                            unsigned int col) const
00051                 { return coeffs[row * cols + col]; }
00052 
00053             private:
00054                 unsigned int            rows;
00055                 unsigned int            cols;
00056                 std::vector<double>     coeffs;
00057         };
00058 
00059         Matrix  matrix;
00060 };
00061 
00062 static ProcMatrix::Registry registry("ProcMatrix");
00063 
00064 ProcMatrix::ProcMatrix(const char *name,
00065                        const Calibration::ProcMatrix *calib,
00066                        const MVAComputer *computer) :
00067         VarProcessor(name, calib, computer),
00068         matrix(&calib->matrix)
00069 {
00070 }
00071 
00072 void ProcMatrix::configure(ConfIterator iter, unsigned int n)
00073 {
00074         if (n != matrix.getCols())
00075                 return;
00076 
00077         for(unsigned int col = 0; col < matrix.getCols(); col++)
00078                 iter++(Variable::FLAG_NONE);
00079 
00080         for(unsigned int row = 0; row < matrix.getRows(); row++)
00081                 iter << Variable::FLAG_NONE;
00082 }
00083 
00084 void ProcMatrix::eval(ValueIterator iter, unsigned int n) const
00085 {
00086         double *sums = (double*)alloca(matrix.getRows() * sizeof(double));
00087         for(unsigned int row = 0; row < matrix.getRows(); row++)
00088                 sums[row] = 0.0;
00089 
00090         for(unsigned int col = 0; col < matrix.getCols(); col++) {
00091                 double val = *iter++;
00092                 for(unsigned int row = 0; row < matrix.getRows(); row++)
00093                         sums[row] += matrix(row, col) * val;
00094         }
00095 
00096         for(unsigned int row = 0; row < matrix.getRows(); row++)
00097                 iter(sums[row]);
00098 }
00099 
00100 } // anonymous namespace

Generated on Tue Jun 9 17:41:28 2009 for CMSSW by  doxygen 1.5.4