Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:42

0001 // -*- C++ -*-
0002 //
0003 // Package:     MVAComputer
0004 // Class  :     ProcLinear
0005 //
0006 
0007 // Implementation:
0008 //     Variable processor to compute a simple linear discriminant using
0009 //     coefficients for each input variable.
0010 //
0011 // Author:      Christophe Saout
0012 // Created:     Sat Apr 24 15:18 CEST 2007
0013 //
0014 
0015 #include <vector>
0016 
0017 #include "PhysicsTools/MVAComputer/interface/VarProcessor.h"
0018 #include "PhysicsTools/MVAComputer/interface/Calibration.h"
0019 
0020 using namespace PhysicsTools;
0021 
0022 namespace {  // anonymous
0023 
0024   class ProcLinear : public VarProcessor {
0025   public:
0026     typedef VarProcessor::Registry::Registry<ProcLinear, Calibration::ProcLinear> Registry;
0027 
0028     ProcLinear(const char *name, const Calibration::ProcLinear *calib, const MVAComputer *computer);
0029     ~ProcLinear() override {}
0030 
0031     void configure(ConfIterator iter, unsigned int n) override;
0032     void eval(ValueIterator iter, unsigned int n) const override;
0033     std::vector<double> deriv(ValueIterator iter, unsigned int n) const override;
0034 
0035   private:
0036     std::vector<double> coeffs;
0037     double offset;
0038   };
0039 
0040   ProcLinear::Registry registry("ProcLinear");
0041 
0042   ProcLinear::ProcLinear(const char *name, const Calibration::ProcLinear *calib, const MVAComputer *computer)
0043       : VarProcessor(name, calib, computer), coeffs(calib->coeffs), offset(calib->offset) {}
0044 
0045   void ProcLinear::configure(ConfIterator iter, unsigned int n) {
0046     while (iter)
0047       iter++(Variable::FLAG_OPTIONAL);
0048 
0049     iter << Variable::FLAG_OPTIONAL;
0050   }
0051 
0052   void ProcLinear::eval(ValueIterator iter, unsigned int n) const {
0053     double sum = offset;
0054 
0055     for (std::vector<double>::const_iterator coeff = coeffs.begin(); coeff != coeffs.end(); coeff++, ++iter) {
0056       if (iter.empty()) {
0057         iter();
0058         return;
0059       }
0060       sum += *coeff * *iter;
0061     }
0062 
0063     iter(sum);
0064   }
0065 
0066   std::vector<double> ProcLinear::deriv(ValueIterator iter, unsigned int n) const {
0067     std::vector<double> result;
0068 
0069     for (std::vector<double>::const_iterator coeff = coeffs.begin(); coeff != coeffs.end(); coeff++, ++iter) {
0070       if (!iter.empty())
0071         result.push_back(*coeff);
0072     }
0073 
0074     return result;
0075   }
0076 
0077 }  // anonymous namespace