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  :     ProcOptional
0005 //
0006 
0007 // Implementation:
0008 //     Variable processor to set empty input variables to a default value.
0009 //
0010 // Author:      Christophe Saout
0011 // Created:     Sat Apr 24 15:18 CEST 2007
0012 //
0013 
0014 #include "FWCore/Utilities/interface/Exception.h"
0015 
0016 #include "PhysicsTools/MVAComputer/interface/VarProcessor.h"
0017 #include "PhysicsTools/MVAComputer/interface/Calibration.h"
0018 
0019 using namespace PhysicsTools;
0020 
0021 namespace {  // anonymous
0022 
0023   class ProcOptional : public VarProcessor {
0024   public:
0025     typedef VarProcessor::Registry::Registry<ProcOptional, Calibration::ProcOptional> Registry;
0026 
0027     ProcOptional(const char *name, const Calibration::ProcOptional *calib, const MVAComputer *computer);
0028     ~ProcOptional() override {}
0029 
0030     void configure(ConfIterator iter, unsigned int n) override;
0031     void eval(ValueIterator iter, unsigned int n) const override;
0032     std::vector<double> deriv(ValueIterator iter, unsigned int n) const override;
0033 
0034   private:
0035     std::vector<double> neutralPos;
0036   };
0037 
0038   ProcOptional::Registry registry("ProcOptional");
0039 
0040   ProcOptional::ProcOptional(const char *name, const Calibration::ProcOptional *calib, const MVAComputer *computer)
0041       : VarProcessor(name, calib, computer), neutralPos(calib->neutralPos) {}
0042 
0043   void ProcOptional::configure(ConfIterator iter, unsigned int n) {
0044     if (n != neutralPos.size())
0045       return;
0046 
0047     while (iter)
0048       iter++(Variable::FLAG_OPTIONAL) << Variable::FLAG_NONE;
0049   }
0050 
0051   void ProcOptional::eval(ValueIterator iter, unsigned int n) const {
0052     for (std::vector<double>::const_iterator pos = neutralPos.begin(); pos != neutralPos.end(); pos++, ++iter) {
0053       switch (iter.size()) {
0054         case 0:
0055           iter(*pos);
0056           break;
0057         case 1:
0058           iter(*iter);
0059           break;
0060         default:
0061           throw cms::Exception("ProcOptional") << "Multiple input variables encountered." << std::endl;
0062       }
0063     }
0064   }
0065 
0066   std::vector<double> ProcOptional::deriv(ValueIterator iter, unsigned int n) const {
0067     unsigned int size = 0;
0068     for (ValueIterator iter2 = iter; iter2; ++iter2)
0069       size += iter2.size();
0070 
0071     std::vector<double> result;
0072 
0073     unsigned int column = 0;
0074     for (std::vector<double>::const_iterator pos = neutralPos.begin(); pos != neutralPos.end(); pos++, ++iter) {
0075       unsigned int row = result.size();
0076       result.resize(row + size);
0077       switch (iter.size()) {
0078         case 0:
0079           break;
0080         case 1:
0081           result[row + column++] = 1.0;
0082           break;
0083         default:
0084           throw cms::Exception("ProcOptionalError") << "Multiple input variables encountered." << std::endl;
0085       }
0086     }
0087 
0088     return result;
0089   }
0090 
0091 }  // anonymous namespace