Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:16

0001 #ifndef FWCore_Sources_VectorInputSource_h
0002 #define FWCore_Sources_VectorInputSource_h
0003 
0004 /*----------------------------------------------------------------------
0005 VectorInputSource: Abstract interface for vector input sources.
0006 ----------------------------------------------------------------------*/
0007 
0008 #include "DataFormats/Common/interface/SecondaryEventIDAndFileInfo.h"
0009 #include "DataFormats/Provenance/interface/ProcessHistoryRegistry.h"
0010 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "FWCore/Utilities/interface/get_underlying_safe.h"
0013 
0014 #include <memory>
0015 #include <string>
0016 #include <vector>
0017 
0018 namespace CLHEP {
0019   class HepRandomEngine;
0020 }
0021 
0022 namespace edm {
0023   class EventPrincipal;
0024   struct VectorInputSourceDescription;
0025   class EventID;
0026   class ParameterSet;
0027   class VectorInputSource {
0028   public:
0029     explicit VectorInputSource(ParameterSet const& pset, VectorInputSourceDescription const& desc);
0030     virtual ~VectorInputSource();
0031 
0032     template <typename T>
0033     size_t loopOverEvents(EventPrincipal& cache,
0034                           size_t& fileNameHash,
0035                           size_t number,
0036                           T eventOperator,
0037                           CLHEP::HepRandomEngine* = nullptr,
0038                           EventID const* id = nullptr,
0039                           bool recycleFiles = true);
0040 
0041     template <typename T, typename Iterator>
0042     size_t loopSpecified(
0043         EventPrincipal& cache, size_t& fileNameHash, Iterator const& begin, Iterator const& end, T eventOperator);
0044 
0045     void dropUnwantedBranches(std::vector<std::string> const& wantedBranches);
0046     //
0047     /// Called at beginning of job
0048     void doBeginJob();
0049 
0050     /// Called at end of job
0051     void doEndJob();
0052 
0053     std::shared_ptr<ProductRegistry const> productRegistry() const { return get_underlying_safe(productRegistry_); }
0054     std::shared_ptr<ProductRegistry>& productRegistry() { return get_underlying_safe(productRegistry_); }
0055     ProductRegistry& productRegistryUpdate() { return *productRegistry_; }
0056     ProcessHistoryRegistry const& processHistoryRegistry() const { return *processHistoryRegistry_; }
0057     ProcessHistoryRegistry& processHistoryRegistryForUpdate() { return *processHistoryRegistry_; }
0058 
0059   private:
0060     void clearEventPrincipal(EventPrincipal& cache);
0061 
0062   private:
0063     virtual bool readOneEvent(
0064         EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine*, EventID const* id, bool recycleFiles) = 0;
0065     virtual void readOneSpecified(EventPrincipal& cache,
0066                                   size_t& fileNameHash,
0067                                   SecondaryEventIDAndFileInfo const& event) = 0;
0068     void readOneSpecified(EventPrincipal& cache, size_t& fileNameHash, EventID const& event) {
0069       SecondaryEventIDAndFileInfo info(event, fileNameHash);
0070       readOneSpecified(cache, fileNameHash, info);
0071     }
0072 
0073     virtual void dropUnwantedBranches_(std::vector<std::string> const& wantedBranches) = 0;
0074     virtual void beginJob() = 0;
0075     virtual void endJob() = 0;
0076 
0077     void throwIfOverLimit(unsigned int consecutiveRejections) const;
0078 
0079     edm::propagate_const<std::shared_ptr<ProductRegistry>> productRegistry_;
0080     edm::propagate_const<std::unique_ptr<ProcessHistoryRegistry>> processHistoryRegistry_;
0081     unsigned int consecutiveRejectionsLimit_;
0082   };
0083 
0084   template <typename T>
0085   size_t VectorInputSource::loopOverEvents(EventPrincipal& cache,
0086                                            size_t& fileNameHash,
0087                                            size_t number,
0088                                            T eventOperator,
0089                                            CLHEP::HepRandomEngine* engine,
0090                                            EventID const* id,
0091                                            bool recycleFiles) {
0092     size_t i = 0U;
0093     unsigned int consecutiveRejections = 0U;
0094     while (i < number) {
0095       clearEventPrincipal(cache);
0096       bool found = readOneEvent(cache, fileNameHash, engine, id, recycleFiles);
0097       if (!found)
0098         break;
0099       bool used = eventOperator(cache, fileNameHash);
0100       if (used) {
0101         ++i;
0102         consecutiveRejections = 0U;
0103       } else if (consecutiveRejectionsLimit_ > 0) {
0104         ++consecutiveRejections;
0105         throwIfOverLimit(consecutiveRejections);
0106       }
0107     }
0108     return i;
0109   }
0110 
0111   template <typename T, typename Iterator>
0112   size_t VectorInputSource::loopSpecified(
0113       EventPrincipal& cache, size_t& fileNameHash, Iterator const& begin, Iterator const& end, T eventOperator) {
0114     size_t i = 0U;
0115     for (Iterator iter = begin; iter != end; ++iter) {
0116       clearEventPrincipal(cache);
0117       readOneSpecified(cache, fileNameHash, *iter);
0118       eventOperator(cache, fileNameHash);
0119       ++i;
0120     }
0121     return i;
0122   }
0123 }  // namespace edm
0124 #endif