Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:33:51

0001 #include <memory>
0002 #include <string>
0003 #include <vector>
0004 
0005 #include "CondFormats/PhysicsToolsObjects/interface/MVAComputer.h"
0006 #include "RecoBTau/JetTagComputer/interface/GenericMVAComputer.h"
0007 #include "RecoBTau/JetTagComputer/interface/GenericMVAComputerCache.h"
0008 #include "FWCore/Utilities/interface/Exception.h"
0009 
0010 using namespace PhysicsTools::Calibration;
0011 
0012 inline GenericMVAComputerCache::IndividualComputer::IndividualComputer() {}
0013 
0014 inline GenericMVAComputerCache::IndividualComputer::IndividualComputer(const IndividualComputer &orig)
0015     : label(orig.label) {}
0016 
0017 inline GenericMVAComputerCache::IndividualComputer::~IndividualComputer() {}
0018 
0019 GenericMVAComputerCache::GenericMVAComputerCache(const std::vector<std::string> &labels)
0020     : computers(labels.size()),
0021       cacheId(MVAComputerContainer::CacheId()),
0022       initialized(false),
0023       empty(true),
0024       errorUpdatingLabel() {
0025   std::vector<IndividualComputer>::iterator computer = computers.begin();
0026   for (std::vector<std::string>::const_iterator iter = labels.begin(); iter != labels.end(); iter++) {
0027     computer->label = *iter;
0028     computer->cacheId = MVAComputer::CacheId();
0029     computer++;
0030   }
0031 }
0032 
0033 GenericMVAComputerCache::~GenericMVAComputerCache() {}
0034 
0035 GenericMVAComputer const *GenericMVAComputerCache::getComputer(int index) const {
0036   if (!errorUpdatingLabel.empty()) {
0037     throw cms::Exception("MVAComputerCalibration")
0038         << "GenericMVAComputerCache::getComputer Error occurred during update.\n"
0039         << "Calibration record " << errorUpdatingLabel << " not found in MVAComputerContainer." << std::endl;
0040   }
0041   return index >= 0 ? computers[index].computer.get() : nullptr;
0042 }
0043 
0044 bool GenericMVAComputerCache::isEmpty() const {
0045   if (!errorUpdatingLabel.empty()) {
0046     throw cms::Exception("MVAComputerCalibration")
0047         << "GenericMVAComputerCache::isEmpty Error occurred during update.\n"
0048         << "Calibration record " << errorUpdatingLabel << " not found in MVAComputerContainer." << std::endl;
0049   }
0050   return empty;
0051 }
0052 
0053 bool GenericMVAComputerCache::update(const MVAComputerContainer *calib) {
0054   // check container for changes
0055   if (initialized && !calib->changed(cacheId))
0056     return false;
0057 
0058   empty = true;
0059 
0060   bool changed = false;
0061   for (std::vector<IndividualComputer>::iterator iter = computers.begin(); iter != computers.end(); iter++) {
0062     // empty labels means we don't want a computer
0063     if (iter->label.empty())
0064       continue;
0065 
0066     // Delay throwing if the label cannot be found until getComputer is called
0067     // Sometimes this cache is updated and never used.
0068     if (!calib->contains(iter->label)) {
0069       errorUpdatingLabel = iter->label;
0070       continue;
0071     }
0072 
0073     const MVAComputer *computerCalib = &calib->find(iter->label);
0074     if (!computerCalib) {
0075       iter->computer.reset();
0076       continue;
0077     }
0078 
0079     // check container content for changes
0080     if (iter->computer.get() && !computerCalib->changed(iter->cacheId)) {
0081       empty = false;
0082       continue;
0083     }
0084 
0085     // drop old computer
0086     iter->computer.reset();
0087 
0088     if (!computerCalib)
0089       continue;
0090 
0091     // instantiate new MVAComputer with uptodate calibration
0092     iter->computer = std::make_unique<GenericMVAComputer>(computerCalib);
0093 
0094     iter->cacheId = computerCalib->getCacheId();
0095 
0096     changed = true;
0097     empty = false;
0098   }
0099 
0100   cacheId = calib->getCacheId();
0101   initialized = true;
0102 
0103   return changed;
0104 }