Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-08-18 22:30:18

0001 #include "FWCore/Framework/interface/Event.h"
0002 
0003 #include "DataFormats/Common/interface/TriggerResults.h"
0004 #include "DataFormats/Provenance/interface/Provenance.h"
0005 #include "DataFormats/Provenance/interface/StableProvenance.h"
0006 #include "DataFormats/Provenance/interface/ParentageRegistry.h"
0007 #include "FWCore/Common/interface/TriggerResultsByName.h"
0008 #include "FWCore/Framework/interface/EventPrincipal.h"
0009 #include "FWCore/Framework/interface/LuminosityBlock.h"
0010 #include "FWCore/Framework/interface/TransitionInfoTypes.h"
0011 #include "FWCore/Framework/interface/ProductPutterBase.h"
0012 #include "FWCore/ParameterSet/interface/Registry.h"
0013 #include "FWCore/Utilities/interface/Algorithms.h"
0014 #include "FWCore/Utilities/interface/InputTag.h"
0015 
0016 namespace {
0017   const edm::ParentageID s_emptyParentage;
0018 }
0019 namespace edm {
0020 
0021   std::string const Event::emptyString_;
0022 
0023   Event::Event(EventTransitionInfo const& info, ModuleDescription const& md, ModuleCallingContext const* mcc)
0024       : Event(info.principal(), md, mcc) {}
0025 
0026   Event::Event(EventPrincipal const& ep, ModuleDescription const& md, ModuleCallingContext const* moduleCallingContext)
0027       : provRecorder_(ep, md, true /*always at end*/),
0028         aux_(ep.aux()),
0029         luminosityBlock_(),
0030         gotBranchIDs_(),
0031         gotViews_(),
0032         streamID_(ep.streamID()),
0033         moduleCallingContext_(moduleCallingContext) {}
0034 
0035   Event::~Event() {}
0036 
0037   Event::CacheIdentifier_t Event::cacheIdentifier() const { return eventPrincipal().cacheIdentifier(); }
0038 
0039   void Event::setConsumer(EDConsumerBase const* iConsumer) {
0040     provRecorder_.setConsumer(iConsumer);
0041     gotBranchIDs_.reserve(provRecorder_.numberOfProductsConsumed());
0042     if (luminosityBlock_) {
0043       luminosityBlock_->setConsumer(iConsumer);
0044     }
0045   }
0046 
0047   void Event::setSharedResourcesAcquirer(SharedResourcesAcquirer* iResourceAcquirer) {
0048     provRecorder_.setSharedResourcesAcquirer(iResourceAcquirer);
0049     if (luminosityBlock_) {
0050       luminosityBlock_->setSharedResourcesAcquirer(iResourceAcquirer);
0051     }
0052   }
0053 
0054   void Event::fillLuminosityBlock() const {
0055     luminosityBlock_.emplace(
0056         eventPrincipal().luminosityBlockPrincipal(), provRecorder_.moduleDescription(), moduleCallingContext_, false);
0057     luminosityBlock_->setConsumer(provRecorder_.getConsumer());
0058     luminosityBlock_->setSharedResourcesAcquirer(provRecorder_.getSharedResourcesAcquirer());
0059   }
0060 
0061   void Event::setProducerCommon(ProducerBase const* iProd, std::vector<BranchID>* previousParentage) {
0062     provRecorder_.setProducer(iProd);
0063     //set appropriate size
0064     putProducts_.resize(provRecorder_.putTokenIndexToProductResolverIndex().size());
0065     previousBranchIDs_ = previousParentage;
0066   }
0067 
0068   void Event::setProducer(ProducerBase const* iProd,
0069                           std::vector<BranchID>* previousParentage,
0070                           std::vector<BranchID>* gotBranchIDsFromAcquire) {
0071     setProducerCommon(iProd, previousParentage);
0072     if (previousParentage) {
0073       //are we supposed to record parentage for at least one item?
0074       bool record_parents = false;
0075       for (auto v : provRecorder_.recordProvenanceList()) {
0076         if (v) {
0077           record_parents = true;
0078           break;
0079         }
0080       }
0081       if (not record_parents) {
0082         previousBranchIDs_ = nullptr;
0083         return;
0084       }
0085       gotBranchIDsFromPrevious_.resize(previousParentage->size(), false);
0086       if (gotBranchIDsFromAcquire) {
0087         for (auto const& branchID : *gotBranchIDsFromAcquire) {
0088           addToGotBranchIDs(branchID);
0089         }
0090       }
0091     }
0092   }
0093 
0094   void Event::setProducerForAcquire(ProducerBase const* iProd,
0095                                     std::vector<BranchID>* previousParentage,
0096                                     std::vector<BranchID>& gotBranchIDsFromAcquire) {
0097     setProducerCommon(iProd, previousParentage);
0098     gotBranchIDsFromAcquire_ = &gotBranchIDsFromAcquire;
0099     gotBranchIDsFromAcquire_->clear();
0100   }
0101 
0102   EventPrincipal const& Event::eventPrincipal() const {
0103     return dynamic_cast<EventPrincipal const&>(provRecorder_.principal());
0104   }
0105 
0106   EDProductGetter const& Event::productGetter() const { return provRecorder_.principal(); }
0107 
0108   ProductID Event::makeProductID(BranchDescription const& desc) const {
0109     return eventPrincipal().branchIDToProductID(desc.originalBranchID());
0110   }
0111 
0112   Run const& Event::getRun() const { return getLuminosityBlock().getRun(); }
0113 
0114   EventSelectionIDVector const& Event::eventSelectionIDs() const { return eventPrincipal().eventSelectionIDs(); }
0115 
0116   ProcessHistoryID const& Event::processHistoryID() const { return eventPrincipal().processHistoryID(); }
0117 
0118   Provenance const& Event::getProvenance(BranchID const& bid) const {
0119     return provRecorder_.principal().getProvenance(bid);
0120   }
0121 
0122   Provenance const& Event::getProvenance(ProductID const& pid) const { return eventPrincipal().getProvenance(pid); }
0123 
0124   StableProvenance const& Event::getStableProvenance(BranchID const& bid) const {
0125     return provRecorder_.principal().getStableProvenance(bid);
0126   }
0127 
0128   StableProvenance const& Event::getStableProvenance(ProductID const& pid) const {
0129     return eventPrincipal().getStableProvenance(pid);
0130   }
0131 
0132   void Event::getAllProvenance(std::vector<Provenance const*>& provenances) const {
0133     provRecorder_.principal().getAllProvenance(provenances);
0134   }
0135 
0136   void Event::getAllStableProvenance(std::vector<StableProvenance const*>& provenances) const {
0137     provRecorder_.principal().getAllStableProvenance(provenances);
0138   }
0139 
0140   bool Event::getProcessParameterSet(std::string const& processName, ParameterSet& ps) const {
0141     ProcessConfiguration config;
0142     bool process_found = processHistory().getConfigurationForProcess(processName, config);
0143     if (process_found) {
0144       pset::Registry::instance()->getMapped(config.parameterSetID(), ps);
0145       assert(!ps.empty());
0146     }
0147     return process_found;
0148   }
0149 
0150   edm::ParameterSet const* Event::parameterSet(edm::ParameterSetID const& psID) const {
0151     return parameterSetForID_(psID);
0152   }
0153 
0154   BasicHandle Event::getByProductID_(ProductID const& oid) const { return eventPrincipal().getByProductID(oid); }
0155 
0156   void Event::commit_(std::vector<edm::ProductResolverIndex> const& iShouldPut, ParentageID* previousParentageId) {
0157     size_t nPut = 0;
0158     for (auto const& p : putProducts()) {
0159       if (p) {
0160         ++nPut;
0161       }
0162     }
0163     if (nPut > 0) {
0164       commit_aux(putProducts(), previousParentageId);
0165     }
0166     auto sz = iShouldPut.size();
0167     if (sz != 0 and sz != nPut) {
0168       //some were missed
0169       auto& p = provRecorder_.principal();
0170       for (auto index : iShouldPut) {
0171         auto resolver = p.getProductResolverByIndex(index);
0172         if (not resolver->productResolved()) {
0173           dynamic_cast<ProductPutterBase const*>(resolver)->putProduct(std::unique_ptr<WrapperBase>());
0174         }
0175       }
0176     }
0177   }
0178 
0179   void Event::commit_aux(Event::ProductPtrVec& products, ParentageID* previousParentageId) {
0180     // fill in guts of provenance here
0181     auto& ep = eventPrincipal();
0182 
0183     //If we don't have a valid previousParentage then we want to use a temp value in order to
0184     // avoid constantly recalculating the ParentageID which is a time consuming operation
0185     ParentageID const* presentParentageId;
0186 
0187     if (previousBranchIDs_) {
0188       bool sameAsPrevious = gotBranchIDs_.empty();
0189       if (sameAsPrevious) {
0190         for (auto i : gotBranchIDsFromPrevious_) {
0191           if (not i) {
0192             sameAsPrevious = false;
0193             break;
0194           }
0195         }
0196       }
0197       if (not sameAsPrevious) {
0198         std::vector<BranchID> gotBranchIDVector{gotBranchIDs_.begin(), gotBranchIDs_.end()};
0199         //add items in common from previous
0200         auto n = gotBranchIDsFromPrevious_.size();
0201         for (size_t i = 0; i < n; ++i) {
0202           if (gotBranchIDsFromPrevious_[i]) {
0203             gotBranchIDVector.push_back((*previousBranchIDs_)[i]);
0204           }
0205         }
0206         std::sort(gotBranchIDVector.begin(), gotBranchIDVector.end());
0207         previousBranchIDs_->assign(gotBranchIDVector.begin(), gotBranchIDVector.end());
0208 
0209         Parentage p;
0210         p.setParents(std::move(gotBranchIDVector));
0211         *previousParentageId = p.id();
0212         ParentageRegistry::instance()->insertMapped(p);
0213       }
0214       presentParentageId = previousParentageId;
0215     } else {
0216       presentParentageId = &s_emptyParentage;
0217     }
0218 
0219     auto const& recordProv = provRecorder_.recordProvenanceList();
0220     for (unsigned int i = 0; i < products.size(); ++i) {
0221       auto& p = get_underlying_safe(products[i]);
0222       if (p) {
0223         if (recordProv[i]) {
0224           ep.put(provRecorder_.putTokenIndexToProductResolverIndex()[i], std::move(p), *presentParentageId);
0225         } else {
0226           ep.put(provRecorder_.putTokenIndexToProductResolverIndex()[i], std::move(p), s_emptyParentage);
0227         }
0228       }
0229     }
0230 
0231     // the cleanup is all or none
0232     products.clear();
0233   }
0234 
0235   void Event::addToGotBranchIDs(Provenance const& prov) const { addToGotBranchIDs(prov.originalBranchID()); }
0236 
0237   void Event::addToGotBranchIDs(BranchID const& branchID) const {
0238     if (previousBranchIDs_) {
0239       auto range = std::equal_range(previousBranchIDs_->begin(), previousBranchIDs_->end(), branchID);
0240       if (range.first == range.second) {
0241         gotBranchIDs_.insert(branchID.id());
0242       } else {
0243         gotBranchIDsFromPrevious_[range.first - previousBranchIDs_->begin()] = true;
0244       }
0245     } else if (gotBranchIDsFromAcquire_) {
0246       gotBranchIDsFromAcquire_->push_back(branchID);
0247     }
0248   }
0249 
0250   ProcessHistory const& Event::processHistory() const { return provRecorder_.processHistory(); }
0251 
0252   size_t Event::size() const {
0253     return std::count_if(putProducts().begin(), putProducts().end(), [](auto const& i) { return bool(i); }) +
0254            provRecorder_.principal().size();
0255   }
0256 
0257   BasicHandle Event::getByLabelImpl(std::type_info const&,
0258                                     std::type_info const& iProductType,
0259                                     const InputTag& iTag) const {
0260     BasicHandle h = provRecorder_.getByLabel_(TypeID(iProductType), iTag, moduleCallingContext_);
0261     if (h.isValid()) {
0262       addToGotBranchIDs(*(h.provenance()));
0263     }
0264     return h;
0265   }
0266 
0267   BasicHandle Event::getImpl(std::type_info const&, ProductID const& pid) const {
0268     BasicHandle h = this->getByProductID_(pid);
0269     if (h.isValid()) {
0270       addToGotBranchIDs(*(h.provenance()));
0271     }
0272     return h;
0273   }
0274 
0275   TriggerNames const& Event::triggerNames(edm::TriggerResults const& triggerResults) const {
0276     edm::TriggerNames const* names = triggerNames_(triggerResults);
0277     if (names != nullptr)
0278       return *names;
0279 
0280     throw cms::Exception("TriggerNamesNotFound") << "TriggerNames not found in ParameterSet registry";
0281     return *names;
0282   }
0283 
0284   TriggerResultsByName Event::triggerResultsByName(edm::TriggerResults const& triggerResults) const {
0285     edm::TriggerNames const* names = triggerNames_(triggerResults);
0286     return TriggerResultsByName(&triggerResults, names);
0287   }
0288 }  // namespace edm