Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-11 03:34:14

0001 // -*- C++ -*-
0002 //
0003 // Package:     <package>
0004 // Module:      EDLooperBase
0005 //
0006 // Author:      Valentin Kuznetsov
0007 // Created:     Wed Jul  5 11:44:26 EDT 2006
0008 
0009 #include "FWCore/Framework/interface/EDLooperBase.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/EventPrincipal.h"
0012 #include "FWCore/Framework/interface/LuminosityBlock.h"
0013 #include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
0014 #include "FWCore/Framework/interface/ModuleContextSentry.h"
0015 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0016 #include "FWCore/Framework/interface/Run.h"
0017 #include "FWCore/Framework/interface/RunPrincipal.h"
0018 #include "FWCore/Framework/interface/EventSetup.h"
0019 #include "FWCore/Framework/interface/EventSetupProvider.h"
0020 #include "FWCore/Utilities/interface/Algorithms.h"
0021 #include "FWCore/MessageLogger/interface/ExceptionMessages.h"
0022 #include "FWCore/Framework/interface/ExceptionActions.h"
0023 #include "FWCore/Utilities/interface/Exception.h"
0024 #include "FWCore/Framework/interface/ScheduleInfo.h"
0025 #include "FWCore/ServiceRegistry/interface/GlobalContext.h"
0026 #include "FWCore/ServiceRegistry/interface/ModuleCallingContext.h"
0027 #include "FWCore/ServiceRegistry/interface/ParentContext.h"
0028 #include "FWCore/ServiceRegistry/interface/StreamContext.h"
0029 #include "FWCore/ServiceRegistry/interface/ESParentContext.h"
0030 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
0031 #include "FWCore/Concurrency/interface/include_first_syncWait.h"
0032 #include "FWCore/Concurrency/interface/WaitingTask.h"
0033 #include "FWCore/Concurrency/interface/WaitingTaskHolder.h"
0034 
0035 namespace edm {
0036 
0037   EDLooperBase::EDLooperBase()
0038       : iCounter_(0),
0039         act_table_(nullptr),
0040         moduleChanger_(nullptr),
0041         moduleDescription_("Looper", "looper"),
0042         moduleCallingContext_(&moduleDescription_) {}
0043   EDLooperBase::~EDLooperBase() noexcept(false) {}
0044 
0045   void EDLooperBase::doStartingNewLoop() { startingNewLoop(iCounter_); }
0046 
0047   EDLooperBase::Status EDLooperBase::doDuringLoop(edm::EventPrincipal& eventPrincipal,
0048                                                   const edm::EventSetupImpl& esi,
0049                                                   edm::ProcessingController& ioController,
0050                                                   StreamContext* streamContext) {
0051     streamContext->setTransition(StreamContext::Transition::kEvent);
0052     streamContext->setEventID(eventPrincipal.id());
0053     streamContext->setRunIndex(eventPrincipal.luminosityBlockPrincipal().runPrincipal().index());
0054     streamContext->setLuminosityBlockIndex(eventPrincipal.luminosityBlockPrincipal().index());
0055     streamContext->setTimestamp(eventPrincipal.time());
0056     ParentContext parentContext(streamContext);
0057     ModuleContextSentry moduleContextSentry(&moduleCallingContext_, parentContext);
0058     Event event(eventPrincipal, moduleDescription_, &moduleCallingContext_);
0059     event.setConsumer(this);
0060 
0061     Status status = kContinue;
0062     try {
0063       ESParentContext parentC(&moduleCallingContext_);
0064       const EventSetup es{
0065           esi, static_cast<unsigned int>(Transition::Event), esGetTokenIndices(Transition::Event), parentC};
0066       status = duringLoop(event, es, ioController);
0067     } catch (cms::Exception& e) {
0068       e.addContext("Calling the 'duringLoop' method of a looper");
0069       exception_actions::ActionCodes action = (act_table_->find(e.category()));
0070       if (action != exception_actions::Rethrow) {
0071         edm::printCmsExceptionWarning("SkipEvent", e);
0072       } else {
0073         throw;
0074       }
0075     }
0076     return status;
0077   }
0078 
0079   EDLooperBase::Status EDLooperBase::doEndOfLoop(const edm::EventSetupImpl& esi) {
0080     ESParentContext parentC(&moduleCallingContext_);
0081     const EventSetup es{
0082         esi, static_cast<unsigned int>(Transition::EndRun), esGetTokenIndices(Transition::EndRun), parentC};
0083     return endOfLoop(es, iCounter_);
0084   }
0085 
0086   void EDLooperBase::prepareForNextLoop(eventsetup::EventSetupProvider* esp) {
0087     ++iCounter_;
0088 
0089     std::set<edm::eventsetup::EventSetupRecordKey> const& keys = modifyingRecords();
0090     for_all(keys,
0091             std::bind(&eventsetup::EventSetupProvider::resetRecordPlusDependentRecords, esp, std::placeholders::_1));
0092   }
0093 
0094   void EDLooperBase::beginOfJob(const edm::EventSetupImpl& iImpl) {
0095     ESParentContext parentC(&moduleCallingContext_);
0096     beginOfJob(EventSetup{
0097         iImpl, static_cast<unsigned int>(Transition::BeginRun), esGetTokenIndices(Transition::BeginRun), parentC});
0098   }
0099   void EDLooperBase::beginOfJob(const edm::EventSetup&) { beginOfJob(); }
0100   void EDLooperBase::beginOfJob() {}
0101 
0102   void EDLooperBase::endOfJob() {}
0103 
0104   void EDLooperBase::doBeginRun(RunPrincipal& iRP, EventSetupImpl const& iES, ProcessContext* processContext) {
0105     GlobalContext globalContext(GlobalContext::Transition::kBeginRun,
0106                                 LuminosityBlockID(iRP.run(), 0),
0107                                 iRP.index(),
0108                                 LuminosityBlockIndex::invalidLuminosityBlockIndex(),
0109                                 iRP.beginTime(),
0110                                 processContext);
0111     ParentContext parentContext(&globalContext);
0112     ModuleContextSentry moduleContextSentry(&moduleCallingContext_, parentContext);
0113     Run run(iRP, moduleDescription_, &moduleCallingContext_, false);
0114     run.setConsumer(this);
0115     ESParentContext parentC(&moduleCallingContext_);
0116     const EventSetup es{
0117         iES, static_cast<unsigned int>(Transition::BeginRun), esGetTokenIndices(Transition::BeginRun), parentC};
0118     beginRun(run, es);
0119   }
0120 
0121   void EDLooperBase::doEndRun(RunPrincipal& iRP, EventSetupImpl const& iES, ProcessContext* processContext) {
0122     GlobalContext globalContext(GlobalContext::Transition::kEndRun,
0123                                 LuminosityBlockID(iRP.run(), 0),
0124                                 iRP.index(),
0125                                 LuminosityBlockIndex::invalidLuminosityBlockIndex(),
0126                                 iRP.endTime(),
0127                                 processContext);
0128     ParentContext parentContext(&globalContext);
0129     ModuleContextSentry moduleContextSentry(&moduleCallingContext_, parentContext);
0130     Run run(iRP, moduleDescription_, &moduleCallingContext_, true);
0131     run.setConsumer(this);
0132     ESParentContext parentC(&moduleCallingContext_);
0133     const EventSetup es{
0134         iES, static_cast<unsigned int>(Transition::EndRun), esGetTokenIndices(Transition::EndRun), parentC};
0135     endRun(run, es);
0136   }
0137   void EDLooperBase::doBeginLuminosityBlock(LuminosityBlockPrincipal& iLB,
0138                                             EventSetupImpl const& iES,
0139                                             ProcessContext* processContext) {
0140     GlobalContext globalContext(GlobalContext::Transition::kBeginLuminosityBlock,
0141                                 iLB.id(),
0142                                 iLB.runPrincipal().index(),
0143                                 iLB.index(),
0144                                 iLB.beginTime(),
0145                                 processContext);
0146     ParentContext parentContext(&globalContext);
0147     ModuleContextSentry moduleContextSentry(&moduleCallingContext_, parentContext);
0148     LuminosityBlock luminosityBlock(iLB, moduleDescription_, &moduleCallingContext_, false);
0149     luminosityBlock.setConsumer(this);
0150     ESParentContext parentC(&moduleCallingContext_);
0151     const EventSetup es{iES,
0152                         static_cast<unsigned int>(Transition::BeginLuminosityBlock),
0153                         esGetTokenIndices(Transition::BeginLuminosityBlock),
0154                         parentC};
0155     beginLuminosityBlock(luminosityBlock, es);
0156   }
0157   void EDLooperBase::doEndLuminosityBlock(LuminosityBlockPrincipal& iLB,
0158                                           EventSetupImpl const& iES,
0159                                           ProcessContext* processContext) {
0160     GlobalContext globalContext(GlobalContext::Transition::kEndLuminosityBlock,
0161                                 iLB.id(),
0162                                 iLB.runPrincipal().index(),
0163                                 iLB.index(),
0164                                 iLB.beginTime(),
0165                                 processContext);
0166     ParentContext parentContext(&globalContext);
0167     ModuleContextSentry moduleContextSentry(&moduleCallingContext_, parentContext);
0168     LuminosityBlock luminosityBlock(iLB, moduleDescription_, &moduleCallingContext_, true);
0169     luminosityBlock.setConsumer(this);
0170     ESParentContext parentC(&moduleCallingContext_);
0171     const EventSetup es{iES,
0172                         static_cast<unsigned int>(Transition::EndLuminosityBlock),
0173                         esGetTokenIndices(Transition::EndLuminosityBlock),
0174                         parentC};
0175     endLuminosityBlock(luminosityBlock, es);
0176   }
0177 
0178   void EDLooperBase::beginRun(Run const&, EventSetup const&) {}
0179   void EDLooperBase::endRun(Run const&, EventSetup const&) {}
0180   void EDLooperBase::beginLuminosityBlock(LuminosityBlock const&, EventSetup const&) {}
0181   void EDLooperBase::endLuminosityBlock(LuminosityBlock const&, EventSetup const&) {}
0182 
0183   void EDLooperBase::attachTo(ActivityRegistry&) {}
0184 
0185   void EDLooperBase::prefetchAsync(WaitingTaskHolder iTask,
0186                                    ServiceToken const& token,
0187                                    Transition iTrans,
0188                                    Principal const& iPrincipal,
0189                                    EventSetupImpl const& iImpl) const noexcept {
0190     esPrefetchAsync(iTask, iImpl, iTrans, token);
0191     edPrefetchAsync(std::move(iTask), token, iPrincipal);
0192   }
0193 
0194   void EDLooperBase::edPrefetchAsync(WaitingTaskHolder iTask,
0195                                      ServiceToken const& token,
0196                                      Principal const& iPrincipal) const noexcept {
0197     //Based on Worker edPrefetchAsync
0198 
0199     // Prefetch products the module declares it consumes
0200     std::vector<ProductResolverIndexAndSkipBit> const& items = itemsToGetFrom(iPrincipal.branchType());
0201 
0202     for (auto const& item : items) {
0203       ProductResolverIndex productResolverIndex = item.productResolverIndex();
0204       bool skipCurrentProcess = item.skipCurrentProcess();
0205       if (productResolverIndex != ProductResolverIndexAmbiguous) {
0206         iPrincipal.prefetchAsync(iTask, productResolverIndex, skipCurrentProcess, token, &moduleCallingContext_);
0207       }
0208     }
0209   }
0210 
0211   void EDLooperBase::esPrefetchAsync(WaitingTaskHolder iTask,
0212                                      EventSetupImpl const& iImpl,
0213                                      Transition iTrans,
0214                                      ServiceToken const& iToken) const noexcept {
0215     //Based on Worker::esPrefetchAsync
0216     if (iTrans >= edm::Transition::NumberOfEventSetupTransitions) {
0217       return;
0218     }
0219     auto const& recs = esGetTokenRecordIndicesVector(iTrans);
0220     auto const& items = esGetTokenIndicesVector(iTrans);
0221 
0222     assert(items.size() == recs.size());
0223     if (items.empty()) {
0224       return;
0225     }
0226 
0227     for (size_t i = 0; i != items.size(); ++i) {
0228       if (recs[i] != ESRecordIndex{}) {
0229         auto rec = iImpl.findImpl(recs[i]);
0230         if (rec) {
0231           rec->prefetchAsync(iTask, items[i], &iImpl, iToken, ESParentContext(&moduleCallingContext_));
0232         }
0233       }
0234     }
0235   }
0236 
0237   std::set<eventsetup::EventSetupRecordKey> EDLooperBase::modifyingRecords() const {
0238     return std::set<eventsetup::EventSetupRecordKey>();
0239   }
0240 
0241   void EDLooperBase::copyInfo(const ScheduleInfo& iInfo) { scheduleInfo_ = std::make_unique<ScheduleInfo>(iInfo); }
0242   void EDLooperBase::setModuleChanger(ModuleChanger* iChanger) { moduleChanger_ = iChanger; }
0243 
0244   ModuleChanger* EDLooperBase::moduleChanger() { return moduleChanger_; }
0245   const ScheduleInfo* EDLooperBase::scheduleInfo() const { return scheduleInfo_.get(); }
0246 
0247 }  // namespace edm