Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-14 03:16:32

0001 // -*- C++ -*-
0002 //
0003 // Package:     Framework
0004 // Class  :     ESRecordsToProductResolverIndices
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Author:      Chris Jones
0010 // Created:     Thu Mar 31 14:31:13 EST 2005
0011 //
0012 
0013 // system include files
0014 #include <cassert>
0015 #include <algorithm>
0016 
0017 // user include files
0018 #include "FWCore/Framework/interface/ESRecordsToProductResolverIndices.h"
0019 #include "FWCore/Framework/interface/ComponentDescription.h"
0020 
0021 namespace edm::eventsetup {
0022   ESRecordsToProductResolverIndices::ESRecordsToProductResolverIndices(std::vector<EventSetupRecordKey> iRecords)
0023       : recordKeys_{std::move(iRecords)} {
0024     assert(std::is_sorted(recordKeys_.begin(), recordKeys_.end()));
0025     recordOffsets_.reserve(recordKeys_.size() + 1);
0026     recordOffsets_.push_back(0);
0027   }
0028 
0029   unsigned int ESRecordsToProductResolverIndices::dataKeysInRecord(
0030       unsigned int iRecordIndex,
0031       EventSetupRecordKey const& iRecord,
0032       std::vector<DataKey> const& iDataKeys,
0033       std::vector<ComponentDescription const*> const& iComponents,
0034       std::vector<unsigned int> const& iProduceMethodIDs) {
0035     assert(iRecord == recordKeys_[iRecordIndex]);
0036     assert(iDataKeys.size() == iComponents.size());
0037     assert(iDataKeys.size() == iProduceMethodIDs.size());
0038     assert(iRecordIndex + 1 == recordOffsets_.size());
0039     dataKeys_.insert(dataKeys_.end(), iDataKeys.begin(), iDataKeys.end());
0040     ++iRecordIndex;
0041     components_.insert(components_.end(), iComponents.begin(), iComponents.end());
0042     produceMethodIDs_.insert(produceMethodIDs_.end(), iProduceMethodIDs.begin(), iProduceMethodIDs.end());
0043     recordOffsets_.push_back(dataKeys_.size());
0044     return iRecordIndex;
0045   }
0046 
0047   //
0048   // const member functions
0049   //
0050   ESResolverIndex ESRecordsToProductResolverIndices::indexInRecord(EventSetupRecordKey const& iRK,
0051                                                                    DataKey const& iDK) const noexcept {
0052     auto it = std::lower_bound(recordKeys_.begin(), recordKeys_.end(), iRK);
0053     if (it == recordKeys_.end() or *it != iRK) {
0054       return ESResolverIndex::noResolverConfigured();
0055     }
0056 
0057     auto beginOffset = recordOffsets_[std::distance(recordKeys_.begin(), it)];
0058     ++it;
0059     auto endOffset = recordOffsets_[std::distance(recordKeys_.begin(), it)];
0060 
0061     auto itDK = std::lower_bound(dataKeys_.begin() + beginOffset, dataKeys_.begin() + endOffset, iDK);
0062     if (itDK == dataKeys_.begin() + endOffset or *itDK != iDK) {
0063       return ESResolverIndex::noResolverConfigured();
0064     }
0065 
0066     return ESResolverIndex{static_cast<int>(std::distance(dataKeys_.begin() + beginOffset, itDK))};
0067   }
0068 
0069   ESRecordIndex ESRecordsToProductResolverIndices::recordIndexFor(EventSetupRecordKey const& iRK) const noexcept {
0070     auto it = std::lower_bound(recordKeys_.begin(), recordKeys_.end(), iRK);
0071     if (it == recordKeys_.end() or *it != iRK) {
0072       return missingRecordIndex();
0073     }
0074     return ESRecordIndex{static_cast<ESRecordIndex::Value_t>(it - recordKeys_.begin())};
0075   }
0076 
0077   ComponentDescription const* ESRecordsToProductResolverIndices::component(EventSetupRecordKey const& iRK,
0078                                                                            DataKey const& iDK) const noexcept {
0079     auto it = std::lower_bound(recordKeys_.begin(), recordKeys_.end(), iRK);
0080     if (it == recordKeys_.end() or *it != iRK) {
0081       return nullptr;
0082     }
0083 
0084     auto beginOffset = recordOffsets_[std::distance(recordKeys_.begin(), it)];
0085     ++it;
0086     auto endOffset = recordOffsets_[std::distance(recordKeys_.begin(), it)];
0087 
0088     auto itDK = std::lower_bound(dataKeys_.begin() + beginOffset, dataKeys_.begin() + endOffset, iDK);
0089     if (itDK == dataKeys_.begin() + endOffset or *itDK != iDK) {
0090       return nullptr;
0091     }
0092     return components_[std::distance(dataKeys_.begin(), itDK)];
0093   }
0094 
0095   std::tuple<ComponentDescription const*, unsigned int> ESRecordsToProductResolverIndices::componentAndProduceMethodID(
0096       EventSetupRecordKey const& iRK, ESResolverIndex esResolverIndex) const noexcept {
0097     auto const recIndex = recordIndexFor(iRK);
0098     if (recIndex == missingRecordIndex()) {
0099       return {nullptr, 0};
0100     }
0101     auto const beginIndex = recordOffsets_[recIndex.value()];
0102     auto const endIndex = recordOffsets_[recIndex.value() + 1];
0103 
0104     int resolverIndex = esResolverIndex.value();
0105     if (resolverIndex < 0 || static_cast<unsigned int>(resolverIndex) >= endIndex - beginIndex) {
0106       return {nullptr, 0};
0107     }
0108     auto index = beginIndex + resolverIndex;
0109     return {components_[index], produceMethodIDs_[index]};
0110   }
0111 
0112   ESTagGetter ESRecordsToProductResolverIndices::makeTagGetter(EventSetupRecordKey const& iRK,
0113                                                                TypeTag const& iTT) const {
0114     auto recIndex = recordIndexFor(iRK);
0115     if (recIndex == missingRecordIndex()) {
0116       return ESTagGetter();
0117     }
0118 
0119     auto const beginIndex = recordOffsets_[recIndex.value()];
0120     auto const endIndex = recordOffsets_[recIndex.value() + 1];
0121     auto keyIndex = beginIndex;
0122 
0123     std::vector<ESTagGetter::Info> returnValue;
0124     returnValue.reserve(endIndex - beginIndex);
0125     bool foundFirstIndex = false;
0126     while (keyIndex < endIndex) {
0127       if (dataKeys_[keyIndex].type() != iTT) {
0128         if (foundFirstIndex) {
0129           //we are now past any further matches
0130           break;
0131         }
0132       } else {
0133         foundFirstIndex = true;
0134         returnValue.emplace_back(keyIndex - beginIndex,
0135                                  dataKeys_[keyIndex].name().value(),
0136                                  components_[keyIndex] ? components_[keyIndex]->label_.empty()
0137                                                              ? std::string_view(components_[keyIndex]->type_)
0138                                                              : std::string_view(components_[keyIndex]->label_)
0139                                                        : std::string_view());
0140       }
0141       ++keyIndex;
0142     }
0143     return returnValue;
0144   }
0145 
0146   std::pair<std::vector<DataKey>::const_iterator, std::vector<DataKey>::const_iterator>
0147   ESRecordsToProductResolverIndices::keysForRecord(EventSetupRecordKey const& iRK) const noexcept {
0148     auto recIndex = recordIndexFor(iRK);
0149     if (recIndex == missingRecordIndex()) {
0150       return std::make_pair(dataKeys_.end(), dataKeys_.end());
0151     }
0152     auto const beginIndex = recordOffsets_[recIndex.value()];
0153     auto const endIndex = recordOffsets_[recIndex.value() + 1];
0154     return std::make_pair(dataKeys_.begin() + beginIndex, dataKeys_.begin() + endIndex);
0155   }
0156 
0157 }  // namespace edm::eventsetup