Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-10 02:50:56

0001 #ifndef FWCore_Framework_SubProcess_h
0002 #define FWCore_Framework_SubProcess_h
0003 
0004 #include "DataFormats/Provenance/interface/BranchID.h"
0005 #include "FWCore/Common/interface/FWCoreCommonFwd.h"
0006 #include "FWCore/Framework/interface/EventSetupProvider.h"
0007 #include "FWCore/Framework/interface/EDConsumerBase.h"
0008 #include "FWCore/Framework/interface/PathsAndConsumesOfModules.h"
0009 #include "FWCore/Framework/interface/PrincipalCache.h"
0010 #include "FWCore/Framework/interface/Schedule.h"
0011 #include "FWCore/Framework/interface/TriggerResultsBasedEventSelector.h"
0012 #include "FWCore/Framework/interface/ProductSelectorRules.h"
0013 #include "FWCore/Framework/interface/ProductSelector.h"
0014 #include "FWCore/ServiceRegistry/interface/ProcessContext.h"
0015 #include "FWCore/ServiceRegistry/interface/ServiceLegacy.h"
0016 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
0017 #include "FWCore/Utilities/interface/Algorithms.h"
0018 #include "FWCore/Utilities/interface/BranchType.h"
0019 #include "FWCore/Utilities/interface/get_underlying_safe.h"
0020 #include "FWCore/Utilities/interface/propagate_const.h"
0021 
0022 #include "DataFormats/Provenance/interface/SelectedProducts.h"
0023 
0024 #include <map>
0025 #include <memory>
0026 #include <set>
0027 #include <vector>
0028 
0029 namespace edm {
0030   class ActivityRegistry;
0031   class BranchDescription;
0032   class BranchIDListHelper;
0033   class EventPrincipal;
0034   class EventSetupImpl;
0035   class HistoryAppender;
0036   class LuminosityBlockPrincipal;
0037   class LumiTransitionInfo;
0038   class MergeableRunProductMetadata;
0039   class ParameterSet;
0040   class Principal;
0041   class ProcessBlockTransitionInfo;
0042   class ProductRegistry;
0043   class PreallocationConfiguration;
0044   class RunTransitionInfo;
0045   class ThinnedAssociationsHelper;
0046   class SubProcessParentageHelper;
0047   class WaitingTaskHolder;
0048 
0049   namespace eventsetup {
0050     class EventSetupsController;
0051   }
0052   class SubProcess : public EDConsumerBase {
0053   public:
0054     SubProcess(ParameterSet& parameterSet,
0055                ParameterSet const& topLevelParameterSet,
0056                std::shared_ptr<ProductRegistry const> parentProductRegistry,
0057                std::shared_ptr<BranchIDListHelper const> parentBranchIDListHelper,
0058                ProcessBlockHelperBase const& parentProcessBlockHelper,
0059                ThinnedAssociationsHelper const& parentThinnedAssociationsHelper,
0060                SubProcessParentageHelper const& parentSubProcessParentageHelper,
0061                eventsetup::EventSetupsController& esController,
0062                ActivityRegistry& parentActReg,
0063                ServiceToken const& token,
0064                serviceregistry::ServiceLegacy iLegacy,
0065                PreallocationConfiguration const& preallocConfig,
0066                ProcessContext const* parentProcessContext);
0067 
0068     ~SubProcess() override;
0069 
0070     SubProcess(SubProcess const&) = delete;             // Disallow copying
0071     SubProcess& operator=(SubProcess const&) = delete;  // Disallow copying
0072     SubProcess(SubProcess&&) = default;                 // Allow Moving
0073     SubProcess& operator=(SubProcess&&) = delete;       // Move not supported by PrincipalCache
0074 
0075     //From OutputModule
0076     void selectProducts(ProductRegistry const& preg,
0077                         ThinnedAssociationsHelper const& parentThinnedAssociationsHelper,
0078                         std::map<BranchID, bool>& keepAssociation);
0079 
0080     SelectedProductsForBranchType const& keptProducts() const { return keptProducts_; }
0081 
0082     // Returns the set of modules whose products may be consumed by
0083     // modules in this SubProcess or its child SubProcesses
0084     std::vector<ModuleProcessName> keepOnlyConsumedUnscheduledModules(bool deleteModules);
0085 
0086     void doBeginJob();
0087     void doEndJob();
0088 
0089     void doEventAsync(WaitingTaskHolder iHolder,
0090                       EventPrincipal const& principal,
0091                       std::vector<std::shared_ptr<const EventSetupImpl>> const*);
0092 
0093     template <typename Traits>
0094     void doBeginProcessBlockAsync(WaitingTaskHolder iHolder,
0095                                   ProcessBlockTransitionInfo const& iTransitionInfo,
0096                                   bool cleaningUpAfterException);
0097 
0098     void doEndProcessBlockAsync(WaitingTaskHolder iHolder,
0099                                 ProcessBlockTransitionInfo const& iTransitionInfo,
0100                                 bool cleaningUpAfterException);
0101 
0102     void doBeginRunAsync(WaitingTaskHolder iHolder, RunTransitionInfo const& iTransitionInfo);
0103 
0104     void doEndRunAsync(WaitingTaskHolder iHolder,
0105                        RunTransitionInfo const& iTransitionInfo,
0106                        bool cleaningUpAfterException);
0107 
0108     void doBeginLuminosityBlockAsync(WaitingTaskHolder iHolder, LumiTransitionInfo const& iTransitionInfo);
0109 
0110     void doEndLuminosityBlockAsync(WaitingTaskHolder iHolder,
0111                                    LumiTransitionInfo const& iTransitionInfo,
0112                                    bool cleaningUpAfterException);
0113 
0114     void doBeginStream(unsigned int);
0115     void doEndStream(unsigned int);
0116     void doStreamBeginRunAsync(WaitingTaskHolder iHolder, unsigned int iID, RunTransitionInfo const&);
0117 
0118     void doStreamEndRunAsync(WaitingTaskHolder iHolder,
0119                              unsigned int iID,
0120                              RunTransitionInfo const&,
0121                              bool cleaningUpAfterException);
0122 
0123     void doStreamBeginLuminosityBlockAsync(WaitingTaskHolder iHolder, unsigned int iID, LumiTransitionInfo const&);
0124 
0125     void doStreamEndLuminosityBlockAsync(WaitingTaskHolder iHolder,
0126                                          unsigned int iID,
0127                                          LumiTransitionInfo const&,
0128                                          bool cleaningUpAfterException);
0129 
0130     void writeLumiAsync(WaitingTaskHolder, LuminosityBlockPrincipal&);
0131 
0132     void deleteLumiFromCache(LuminosityBlockPrincipal&);
0133 
0134     using ProcessBlockType = PrincipalCache::ProcessBlockType;
0135     void writeProcessBlockAsync(edm::WaitingTaskHolder task, ProcessBlockType);
0136 
0137     void writeRunAsync(WaitingTaskHolder,
0138                        ProcessHistoryID const& parentPhID,
0139                        int runNumber,
0140                        MergeableRunProductMetadata const*);
0141 
0142     void deleteRunFromCache(ProcessHistoryID const& parentPhID, int runNumber);
0143 
0144     void clearProcessBlockPrincipal(ProcessBlockType);
0145 
0146     // Call closeFile() on all OutputModules.
0147     void closeOutputFiles() {
0148       ServiceRegistry::Operate operate(serviceToken_);
0149       schedule_->closeOutputFiles();
0150       for_all(subProcesses_, [](auto& subProcess) { subProcess.closeOutputFiles(); });
0151     }
0152 
0153     // Call openFiles() on all OutputModules
0154     void openOutputFiles(FileBlock& fb) {
0155       ServiceRegistry::Operate operate(serviceToken_);
0156       schedule_->openOutputFiles(fb);
0157       for_all(subProcesses_, [&fb](auto& subProcess) { subProcess.openOutputFiles(fb); });
0158     }
0159 
0160     void updateBranchIDListHelper(BranchIDLists const&);
0161 
0162     // Call respondToOpenInputFile() on all Modules
0163     void respondToOpenInputFile(FileBlock const& fb);
0164 
0165     // Call respondToCloseInputFile() on all Modules
0166     void respondToCloseInputFile(FileBlock const& fb) {
0167       ServiceRegistry::Operate operate(serviceToken_);
0168       schedule_->respondToCloseInputFile(fb);
0169       for_all(subProcesses_, [&fb](auto& subProcess) { subProcess.respondToCloseInputFile(fb); });
0170     }
0171 
0172     // Call shouldWeCloseFile() on all OutputModules.
0173     bool shouldWeCloseOutput() const {
0174       ServiceRegistry::Operate operate(serviceToken_);
0175       if (schedule_->shouldWeCloseOutput()) {
0176         return true;
0177       }
0178       for (auto const& subProcess : subProcesses_) {
0179         if (subProcess.shouldWeCloseOutput()) {
0180           return true;
0181         }
0182       }
0183       return false;
0184     }
0185 
0186     /// Return a vector allowing const access to all the ModuleDescriptions for this SubProcess
0187 
0188     /// *** N.B. *** Ownership of the ModuleDescriptions is *not*
0189     /// *** passed to the caller. Do not call delete on these
0190     /// *** pointers!
0191     std::vector<ModuleDescription const*> getAllModuleDescriptions() const;
0192 
0193     /// Return the number of events this SubProcess has tried to process
0194     /// (inclues both successes and failures, including failures due
0195     /// to exceptions during processing).
0196     int totalEvents() const { return schedule_->totalEvents(); }
0197 
0198     /// Return the number of events which have been passed by one or more trigger paths.
0199     int totalEventsPassed() const {
0200       ServiceRegistry::Operate operate(serviceToken_);
0201       return schedule_->totalEventsPassed();
0202     }
0203 
0204     /// Return the number of events that have not passed any trigger.
0205     /// (N.B. totalEventsFailed() + totalEventsPassed() == totalEvents()
0206     int totalEventsFailed() const {
0207       ServiceRegistry::Operate operate(serviceToken_);
0208       return schedule_->totalEventsFailed();
0209     }
0210 
0211     /// Return the trigger report information on paths,
0212     /// modules-in-path, modules-in-endpath, and modules.
0213     void getTriggerReport(TriggerReport& rep) const {
0214       ServiceRegistry::Operate operate(serviceToken_);
0215       schedule_->getTriggerReport(rep);
0216     }
0217 
0218     /// Return whether each output module has reached its maximum count.
0219     /// If there is a subprocess, get this information from the subprocess.
0220     bool terminate() const {
0221       ServiceRegistry::Operate operate(serviceToken_);
0222       if (schedule_->terminate()) {
0223         return true;
0224       }
0225       for (auto const& subProcess : subProcesses_) {
0226         if (subProcess.terminate()) {
0227           return true;
0228         }
0229       }
0230       return false;
0231     }
0232 
0233     ///  Clear all the counters in the trigger report.
0234     void clearCounters() {
0235       ServiceRegistry::Operate operate(serviceToken_);
0236       schedule_->clearCounters();
0237       for_all(subProcesses_, [](auto& subProcess) { subProcess.clearCounters(); });
0238     }
0239 
0240   private:
0241     void beginJob();
0242     void endJob();
0243     void processAsync(WaitingTaskHolder iHolder,
0244                       EventPrincipal const& e,
0245                       std::vector<std::shared_ptr<const EventSetupImpl>> const*);
0246 
0247     void propagateProducts(BranchType type, Principal const& parentPrincipal, Principal& principal) const;
0248     bool parentProducedProductIsKept(Principal const& parentPrincipal, Principal& principal) const;
0249     void fixBranchIDListsForEDAliases(
0250         std::map<BranchID::value_type, BranchID::value_type> const& droppedBranchIDToKeptBranchID);
0251     void keepThisBranch(BranchDescription const& desc,
0252                         std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
0253                         std::set<BranchID>& keptProductsInEvent);
0254 
0255     std::map<BranchID::value_type, BranchID::value_type> const& droppedBranchIDToKeptBranchID() {
0256       return droppedBranchIDToKeptBranchID_;
0257     }
0258 
0259     std::shared_ptr<BranchIDListHelper const> branchIDListHelper() const {
0260       return get_underlying_safe(branchIDListHelper_);
0261     }
0262     std::shared_ptr<BranchIDListHelper>& branchIDListHelper() { return get_underlying_safe(branchIDListHelper_); }
0263     std::shared_ptr<ThinnedAssociationsHelper const> thinnedAssociationsHelper() const {
0264       return get_underlying_safe(thinnedAssociationsHelper_);
0265     }
0266     std::shared_ptr<ThinnedAssociationsHelper> thinnedAssociationsHelper() {
0267       return get_underlying_safe(thinnedAssociationsHelper_);
0268     }
0269 
0270     std::shared_ptr<ActivityRegistry> actReg_;  // We do not use propagate_const because the registry itself is mutable.
0271     ServiceToken serviceToken_;
0272     std::shared_ptr<ProductRegistry const> parentPreg_;
0273     std::shared_ptr<ProductRegistry const> preg_;
0274     edm::propagate_const<std::shared_ptr<BranchIDListHelper>> branchIDListHelper_;
0275     edm::propagate_const<std::shared_ptr<SubProcessBlockHelper>> processBlockHelper_;
0276     edm::propagate_const<std::shared_ptr<ThinnedAssociationsHelper>> thinnedAssociationsHelper_;
0277     edm::propagate_const<std::shared_ptr<SubProcessParentageHelper>> subProcessParentageHelper_;
0278     std::unique_ptr<ExceptionToActionTable const> act_table_;
0279     std::shared_ptr<ProcessConfiguration const> processConfiguration_;
0280     ProcessContext processContext_;
0281     PathsAndConsumesOfModules pathsAndConsumesOfModules_;
0282     //We require 1 history for each Run, Lumi and Stream
0283     // The vectors first hold Stream info, then Lumi then Run
0284     unsigned int historyLumiOffset_;
0285     unsigned int historyRunOffset_;
0286     std::vector<ProcessHistoryRegistry> processHistoryRegistries_;
0287     std::vector<HistoryAppender> historyAppenders_;
0288     PrincipalCache principalCache_;
0289     //vector index is principal lumi's index value
0290     std::vector<std::shared_ptr<LuminosityBlockPrincipal>> inUseLumiPrincipals_;
0291     edm::propagate_const<std::shared_ptr<eventsetup::EventSetupProvider>> esp_;
0292     edm::propagate_const<std::unique_ptr<Schedule>> schedule_;
0293     std::map<ProcessHistoryID, ProcessHistoryID> parentToChildPhID_;
0294     std::vector<SubProcess> subProcesses_;
0295     edm::propagate_const<std::unique_ptr<ParameterSet>> processParameterSet_;
0296 
0297     // keptProducts_ are pointers to the BranchDescription objects describing
0298     // the branches we are to write.
0299     //
0300     // We do not own the BranchDescriptions to which we point.
0301     SelectedProductsForBranchType keptProducts_;
0302     ProductSelectorRules productSelectorRules_;
0303     ProductSelector productSelector_;
0304 
0305     //EventSelection
0306     bool wantAllEvents_;
0307     ParameterSetID selector_config_id_;
0308     mutable detail::TriggerResultsBasedEventSelector selectors_;
0309 
0310     // needed because of possible EDAliases.
0311     // filled in only if key and value are different.
0312     std::map<BranchID::value_type, BranchID::value_type> droppedBranchIDToKeptBranchID_;
0313   };
0314 
0315   // free function
0316   std::vector<ParameterSet> popSubProcessVParameterSet(ParameterSet& parameterSet);
0317 }  // namespace edm
0318 #endif