Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:27

0001 // -*- C++ -*-
0002 //
0003 // Package:     PhysicsToolsObjects
0004 // Class  :     MVAComputer
0005 //
0006 
0007 // Implementation:
0008 //     getProcessor() and addProcessor() methods to add processors to
0009 //     the discriminator computer calibration object. POOL doesn't support
0010 //     polymorph pointers, so this is implemented using multiple containers
0011 //     for each possible sub-class and an index array from which the
0012 //     array of pointers can be reconstructed.
0013 //
0014 // Author:      Christophe Saout
0015 // Created:     Sat Apr 24 15:18 CEST 2007
0016 //
0017 #include <algorithm>
0018 #include <memory>
0019 
0020 #include <typeinfo>
0021 #include <iostream>
0022 #include <cstring>
0023 #include <cstddef>
0024 
0025 #include <atomic>
0026 
0027 #include "FWCore/Utilities/interface/Exception.h"
0028 #include "FWCore/Utilities/interface/TypeID.h"
0029 
0030 #include "CondFormats/PhysicsToolsObjects/interface/MVAComputer.h"
0031 
0032 namespace PhysicsTools {
0033   namespace Calibration {
0034 
0035     std::string VarProcessor::getInstanceName() const {
0036       static const char prefix[] = "PhysicsTools::Calibration::";
0037       edm::TypeID typeID(typeid(*this));
0038       const std::string &type(typeID.className());
0039       if (type.size() <= sizeof prefix - 1 || type.substr(0, sizeof prefix - 1) != prefix)
0040         throw cms::Exception("MVAComputerCalibration")
0041             << "getInstanceName failed for " << typeid(*this).name() << "." << std::endl;
0042 
0043       return type.substr(sizeof prefix - 1);
0044     }
0045 
0046     std::unique_ptr<VarProcessor> VarProcessor::clone() const { return (std::make_unique<VarProcessor>(*this)); }
0047 
0048     std::unique_ptr<VarProcessor> ProcOptional::clone() const {
0049       return (std::unique_ptr<VarProcessor>(new ProcOptional(*this)));
0050     }
0051 
0052     std::unique_ptr<VarProcessor> ProcCount::clone() const {
0053       return (std::unique_ptr<VarProcessor>(new ProcCount(*this)));
0054     }
0055 
0056     std::unique_ptr<VarProcessor> ProcClassed::clone() const {
0057       return (std::unique_ptr<VarProcessor>(new ProcClassed(*this)));
0058     }
0059 
0060     std::unique_ptr<VarProcessor> ProcSplitter::clone() const {
0061       return (std::unique_ptr<VarProcessor>(new ProcSplitter(*this)));
0062     }
0063 
0064     std::unique_ptr<VarProcessor> ProcForeach::clone() const {
0065       return (std::unique_ptr<VarProcessor>(new ProcForeach(*this)));
0066     }
0067 
0068     std::unique_ptr<VarProcessor> ProcSort::clone() const {
0069       return (std::unique_ptr<VarProcessor>(new ProcSort(*this)));
0070     }
0071 
0072     std::unique_ptr<VarProcessor> ProcCategory::clone() const {
0073       return (std::unique_ptr<VarProcessor>(new ProcCategory(*this)));
0074     }
0075 
0076     std::unique_ptr<VarProcessor> ProcNormalize::clone() const {
0077       return (std::unique_ptr<VarProcessor>(new ProcNormalize(*this)));
0078     }
0079 
0080     std::unique_ptr<VarProcessor> ProcLikelihood::clone() const {
0081       return (std::unique_ptr<VarProcessor>(new ProcLikelihood(*this)));
0082     }
0083 
0084     std::unique_ptr<VarProcessor> ProcLinear::clone() const {
0085       return (std::unique_ptr<VarProcessor>(new ProcLinear(*this)));
0086     }
0087 
0088     std::unique_ptr<VarProcessor> ProcMultiply::clone() const {
0089       return (std::unique_ptr<VarProcessor>(new ProcMultiply(*this)));
0090     }
0091 
0092     std::unique_ptr<VarProcessor> ProcMatrix::clone() const {
0093       return (std::unique_ptr<VarProcessor>(new ProcMatrix(*this)));
0094     }
0095 
0096     std::unique_ptr<VarProcessor> ProcExternal::clone() const {
0097       return (std::unique_ptr<VarProcessor>(new ProcExternal(*this)));
0098     }
0099 
0100     std::unique_ptr<VarProcessor> ProcMLP::clone() const { return (std::unique_ptr<VarProcessor>(new ProcMLP(*this))); }
0101 
0102     std::string ProcExternal::getInstanceName() const { return method; }
0103 
0104     static MVAComputer::CacheId getNextMVAComputerCacheId() {
0105       static std::atomic<MVAComputer::CacheId> nextCacheId{0};
0106 
0107       return ++nextCacheId;
0108     }
0109 
0110     MVAComputer::MVAComputer() : cacheId(getNextMVAComputerCacheId()) {}
0111 
0112     MVAComputer::MVAComputer(const MVAComputer &orig)
0113         : inputSet(orig.inputSet), output(orig.output), cacheId(orig.cacheId) {
0114       for (std::vector<VarProcessor *>::const_iterator iter = orig.processors.begin(); iter != orig.processors.end();
0115            ++iter)
0116         addProcessor(*iter);
0117     }
0118 
0119     MVAComputer::~MVAComputer() {
0120       for (std::vector<VarProcessor *>::iterator iter = processors.begin(); iter != processors.end(); ++iter)
0121         delete *iter;
0122       processors.clear();
0123     }
0124 
0125     MVAComputer &MVAComputer::operator=(const MVAComputer &orig) {
0126       inputSet = orig.inputSet;
0127       output = orig.output;
0128       cacheId = orig.cacheId;
0129 
0130       for (std::vector<VarProcessor *>::iterator iter = processors.begin(); iter != processors.end(); ++iter)
0131         delete *iter;
0132       processors.clear();
0133 
0134       for (std::vector<VarProcessor *>::const_iterator iter = orig.processors.begin(); iter != orig.processors.end();
0135            ++iter)
0136         addProcessor(*iter);
0137 
0138       return *this;
0139     }
0140 
0141     std::vector<VarProcessor *> MVAComputer::getProcessors() { return processors; }
0142 
0143     std::vector<const VarProcessor *> MVAComputer::getProcessors() const {
0144       std::vector<const VarProcessor *> ret(processors.size());
0145       std::copy(processors.begin(), processors.end(), ret.begin());
0146       return ret;
0147     }
0148 
0149     void MVAComputer::addProcessor(const VarProcessor *proc) {
0150       cacheId = getNextMVAComputerCacheId();
0151       processors.push_back(proc->clone().release());
0152     }
0153 
0154     static MVAComputerContainer::CacheId getNextMVAComputerContainerCacheId() {
0155       static MVAComputerContainer::CacheId nextCacheId = 0;
0156       return ++nextCacheId;
0157     }
0158 
0159     MVAComputerContainer::MVAComputerContainer() : cacheId(getNextMVAComputerContainerCacheId()) {}
0160 
0161     MVAComputer &MVAComputerContainer::add(const std::string &label) {
0162       cacheId = getNextMVAComputerContainerCacheId();
0163 
0164       entries.push_back(std::make_pair(label, MVAComputer()));
0165       return entries.back().second;
0166     }
0167 
0168     const MVAComputer &MVAComputerContainer::find(const std::string &label) const {
0169       std::vector<Entry>::const_iterator pos =
0170           std::find_if(entries.begin(), entries.end(), [&label](const MVAComputerContainer::Entry &entry) {
0171             return entry.first == label;
0172           });
0173 
0174       if (pos == entries.end())
0175         throw cms::Exception("MVAComputerCalibration")
0176             << "Calibration record " << label << " not found in MVAComputerContainer." << std::endl;
0177 
0178       return pos->second;
0179     }
0180 
0181     bool MVAComputerContainer::contains(const std::string &label) const {
0182       std::vector<Entry>::const_iterator pos =
0183           std::find_if(entries.begin(), entries.end(), [&label](const MVAComputerContainer::Entry &entry) {
0184             return entry.first == label;
0185           });
0186       if (pos == entries.end())
0187         return false;
0188       return true;
0189     }
0190 
0191   }  // namespace Calibration
0192 }  // namespace PhysicsTools