Accumulator

BeginLuminosityBlockProducer

BeginProcessBlockProducer

BeginRunProducer

EmptyType

EndLuminosityBlockProducer

EndProcessBlockProducer

EndRunProducer

ExternalWork

GlobalCacheHolder

InputProcessBlockCacheHolder

LuminosityBlockCacheHolder

LuminosityBlockSummaryCacheHolder

RegistrationInfo

RunCacheHolder

RunSummaryCacheHolder

Transformer

WatchLuminosityBlocks

WatchProcessBlock

WatchRuns

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
#ifndef FWCore_Framework_stream_implementors_h
#define FWCore_Framework_stream_implementors_h
// -*- C++ -*-
//
// Package:     FWCore/Framework
// Class  :     implementors
//
/**\file implementors.h "FWCore/Framework/interface/stream/implementors.h"

 Description: Base classes used to implement the interfaces for the edm::stream::* module  abilities

 Usage:
    <usage>

*/
//
// Original Author:  Chris Jones
//         Created:  Fri, 02 Aug 2013 11:52:34 GMT
//

// system include files
#include <cstddef>
#include <functional>
#include <memory>
#include <tuple>
#include <utility>
#include <vector>

// user include files
#include "FWCore/Framework/interface/CacheHandle.h"
#include "FWCore/Framework/interface/stream/EDProducerBase.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/InputProcessBlockCacheImpl.h"
#include "FWCore/Framework/interface/TransformerBase.h"
#include "FWCore/Concurrency/interface/WaitingTaskWithArenaHolder.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "FWCore/Utilities/interface/RunIndex.h"
#include "FWCore/Utilities/interface/LuminosityBlockIndex.h"
#include "FWCore/Utilities/interface/TypeID.h"

// forward declarations
namespace edm {

  class WaitingTaskWithArenaHolder;

  namespace stream {
    namespace impl {
      class EmptyType {};

      template <typename C>
      class GlobalCacheHolder {
      public:
        GlobalCacheHolder() = default;
        GlobalCacheHolder(GlobalCacheHolder<C> const&) = delete;
        GlobalCacheHolder<C>& operator=(GlobalCacheHolder<C> const&) = delete;

        void setGlobalCache(C const* iCache) { cache_ = iCache; }

      protected:
        C const* globalCache() const { return cache_; }

      private:
        C const* cache_;
      };

      template <typename... CacheTypes>
      class InputProcessBlockCacheHolder {
      public:
        InputProcessBlockCacheHolder() = default;
        InputProcessBlockCacheHolder(InputProcessBlockCacheHolder const&) = delete;
        InputProcessBlockCacheHolder& operator=(InputProcessBlockCacheHolder const&) = delete;

        std::tuple<CacheHandle<CacheTypes>...> processBlockCaches(Event const& event) const {
          return cacheImpl_->processBlockCaches(event);
        }

        template <std::size_t N>
        using CacheTypeT = typename std::tuple_element<N, std::tuple<CacheTypes...>>::type;

        template <std::size_t ICacheType, typename DataType, typename Func>
        void registerProcessBlockCacheFiller(EDGetTokenT<DataType> const& token, Func&& cacheFiller) {
          registerProcessBlockCacheFiller<ICacheType, CacheTypeT<ICacheType>, DataType, Func>(
              token, std::forward<Func>(cacheFiller));
        }

        template <typename CacheType, typename DataType, typename Func>
        void registerProcessBlockCacheFiller(EDGetTokenT<DataType> const& token, Func&& cacheFiller) {
          static_assert(edm::impl::countTypeInParameterPack<CacheType, CacheTypes...>() == 1u,
                        "If registerProcessBlockCacheFiller is called with a type template parameter\n"
                        "then that type must appear exactly once in the template parameters of InputProcessBlockCache");

          // Find the index into the parameter pack from the CacheType
          constexpr unsigned int I = edm::impl::indexInputProcessBlockCache<CacheType, CacheTypes...>();

          registerProcessBlockCacheFiller<I, CacheType, DataType, Func>(token, std::forward<Func>(cacheFiller));
        }

      private:
        template <typename T, bool, bool>
        friend struct edm::stream::CallInputProcessBlockImpl;

        void setProcessBlockCache(edm::impl::InputProcessBlockCacheImpl<CacheTypes...> const* cacheImpl) {
          cacheImpl_ = cacheImpl;
        }

        bool cacheFillersRegistered() const { return registrationInfo_ ? true : false; }
        std::vector<edm::impl::TokenInfo>& tokenInfos() { return registrationInfo_->tokenInfos_; }
        std::tuple<edm::impl::CacheFiller<CacheTypes>...>& cacheFillers() { return registrationInfo_->cacheFillers_; }

        void clearRegistration() { registrationInfo_.reset(); }

        // The next two functions exist so that it is optional whether modules
        // with this ability implement them.

        static void accessInputProcessBlock(edm::ProcessBlock const&) {}

        template <typename GlobalCacheType>
        static void accessInputProcessBlock(edm::ProcessBlock const&, GlobalCacheType*) {}

        template <std::size_t ICacheType, typename CacheType, typename DataType, typename Func>
        void registerProcessBlockCacheFiller(EDGetTokenT<DataType> const& token, Func&& cacheFiller) {
          if (!registrationInfo_) {
            registrationInfo_ = std::make_unique<RegistrationInfo>();
            tokenInfos().resize(sizeof...(CacheTypes));
          }

          if (!tokenInfos()[ICacheType].token_.isUninitialized()) {
            throw Exception(errors::LogicError)
                << "registerProcessBlockCacheFiller should only be called once per cache type";
          }

          tokenInfos()[ICacheType] = edm::impl::TokenInfo{EDGetToken(token), TypeID(typeid(DataType))};

          std::get<ICacheType>(cacheFillers()).func_ =
              std::function<std::shared_ptr<CacheType>(ProcessBlock const&, std::shared_ptr<CacheType> const&)>(
                  std::forward<Func>(cacheFiller));
        }

        // ------------ Data members --------------------

        edm::impl::InputProcessBlockCacheImpl<CacheTypes...> const* cacheImpl_;

        // The RegistrationInfo is filled while the module constructor runs.
        // Later this information is copied to the InputProcessBlockCacheImpl
        // object owned by the adaptor and then registrationInfo_ is cleared.
        // Note that this is really only needed for one of the stream instances,
        // but we fill for all streams so registerProcessBlockCacheFiller can
        // be called in the constructor. This keeps the interface as simple as
        // possible and makes it similar to the consumes interface.
        class RegistrationInfo {
        public:
          std::vector<edm::impl::TokenInfo> tokenInfos_;
          std::tuple<edm::impl::CacheFiller<CacheTypes>...> cacheFillers_;
        };
        std::unique_ptr<RegistrationInfo> registrationInfo_;
      };

      template <typename C>
      class RunCacheHolder {
      public:
        RunCacheHolder() = default;
        RunCacheHolder(RunCacheHolder<C> const&) = delete;
        RunCacheHolder<C>& operator=(RunCacheHolder<C> const&) = delete;
        void setRunCache(C const* iCache) { cache_ = iCache; }

      protected:
        C const* runCache() const { return cache_; }

      private:
        C const* cache_;
      };

      template <typename C>
      class LuminosityBlockCacheHolder {
      public:
        LuminosityBlockCacheHolder() = default;
        LuminosityBlockCacheHolder(LuminosityBlockCacheHolder<C> const&) = delete;
        LuminosityBlockCacheHolder<C>& operator=(LuminosityBlockCacheHolder<C> const&) = delete;
        void setLuminosityBlockCache(C const* iCache) { cache_ = iCache; }

      protected:
        C const* luminosityBlockCache() const { return cache_; }

      private:
        C const* cache_;
      };

      template <typename C>
      class RunSummaryCacheHolder {
      public:
        RunSummaryCacheHolder() = default;
        RunSummaryCacheHolder(RunSummaryCacheHolder<C> const&) = delete;
        RunSummaryCacheHolder<C>& operator=(RunSummaryCacheHolder<C> const&) = delete;
        virtual ~RunSummaryCacheHolder() noexcept(false) {}

      private:
        virtual void endRunSummary(edm::Run const&, edm::EventSetup const&, C*) const = 0;
      };

      template <typename C>
      class LuminosityBlockSummaryCacheHolder {
      public:
        LuminosityBlockSummaryCacheHolder() = default;
        LuminosityBlockSummaryCacheHolder(LuminosityBlockSummaryCacheHolder<C> const&) = delete;
        LuminosityBlockSummaryCacheHolder<C>& operator=(LuminosityBlockSummaryCacheHolder<C> const&) = delete;
        virtual ~LuminosityBlockSummaryCacheHolder() noexcept(false) {}

      private:
        virtual void endLuminosityBlockSummary(edm::LuminosityBlock const&, edm::EventSetup const&, C*) const = 0;
      };

      class WatchProcessBlock {
      public:
        WatchProcessBlock() = default;
        WatchProcessBlock(WatchProcessBlock const&) = delete;
        WatchProcessBlock& operator=(WatchProcessBlock const&) = delete;

        ///requires the following be defined in the inheriting class
        ///static void beginProcessBlockProduce(edm::ProcessBlock const&, GlobalCache*);
      };

      class BeginProcessBlockProducer {
      public:
        BeginProcessBlockProducer() = default;
        BeginProcessBlockProducer(BeginProcessBlockProducer const&) = delete;
        BeginProcessBlockProducer& operator=(BeginProcessBlockProducer const&) = delete;

        ///requires the following be defined in the inheriting class
        ///static void beginProcessBlockProduce(edm::ProcessBlock&, GlobalCache*);
      };

      class EndProcessBlockProducer {
      public:
        EndProcessBlockProducer() = default;
        EndProcessBlockProducer(EndProcessBlockProducer const&) = delete;
        EndProcessBlockProducer& operator=(EndProcessBlockProducer const&) = delete;

        ///requires the following be defined in the inheriting class
        /// static void endProcessBlockProduce(edm::ProcessBlock&, GlobalCache*)
      };

      class BeginRunProducer {
      public:
        BeginRunProducer() = default;
        BeginRunProducer(BeginRunProducer const&) = delete;
        BeginRunProducer& operator=(BeginRunProducer const&) = delete;

        ///requires the following be defined in the inheriting class
        ///static void globalBeginRunProduce(edm::Run&, edm::EventSetup const&, RunContext const* );
      };

      class EndRunProducer {
      public:
        EndRunProducer() = default;
        EndRunProducer(EndRunProducer const&) = delete;
        EndRunProducer& operator=(EndRunProducer const&) = delete;

        ///requires the following be defined in the inheriting class
        /// static void globalEndRunProduce(edm::Run&, edm::EventSetup const&, RunContext const* )
      };

      class BeginLuminosityBlockProducer {
      public:
        BeginLuminosityBlockProducer() = default;
        BeginLuminosityBlockProducer(BeginLuminosityBlockProducer const&) = delete;
        BeginLuminosityBlockProducer& operator=(BeginLuminosityBlockProducer const&) = delete;

        ///requires the following be defined in the inheriting class
        ///static void globalBeginLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&, LuminosityBlockContext const*)
      };

      class EndLuminosityBlockProducer {
      public:
        EndLuminosityBlockProducer() = default;
        EndLuminosityBlockProducer(EndLuminosityBlockProducer const&) = delete;
        EndLuminosityBlockProducer& operator=(EndLuminosityBlockProducer const&) = delete;

        ///requires the following be defined in the inheriting class
        ///static void globalEndLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&, LuminosityBlockContext const*)
      };

      class ExternalWork {
      public:
        ExternalWork() = default;
        ExternalWork(ExternalWork const&) = delete;
        ExternalWork& operator=(ExternalWork const&) = delete;
        virtual ~ExternalWork() noexcept(false) {}

        virtual void acquire(Event const&, edm::EventSetup const&, WaitingTaskWithArenaHolder) = 0;
      };

      class WatchLuminosityBlocks {
      public:
        WatchLuminosityBlocks() = default;
        WatchLuminosityBlocks(WatchLuminosityBlocks const&) = delete;
        WatchLuminosityBlocks& operator=(WatchLuminosityBlocks const&) = delete;
        virtual ~WatchLuminosityBlocks() noexcept(false) {}

        // virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) = 0;
        // virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) {}
      };

      class WatchRuns {
      public:
        WatchRuns() = default;
        WatchRuns(WatchRuns const&) = delete;
        WatchRuns& operator=(WatchRuns const&) = delete;
        virtual ~WatchRuns() noexcept(false) {}

        // virtual void beginRun(edm::Run const&, edm::EventSetup const&) = 0;
        // virtual void endRun(edm::Run const&, edm::EventSetup const&) {}
      };
      class Transformer : private TransformerBase, public EDProducerBase {
      public:
        Transformer() = default;
        Transformer(Transformer const&) = delete;
        Transformer& operator=(Transformer const&) = delete;
        ~Transformer() noexcept(false) override {}

        template <typename G, typename F>
        void registerTransform(ProducerBase::BranchAliasSetterT<G> iSetter,
                               F&& iF,
                               std::string productInstance = std::string()) {
          registerTransform(edm::EDPutTokenT<G>(iSetter), std::forward<F>(iF), std::move(productInstance));
        }

        template <typename G, typename F>
        void registerTransform(edm::EDPutTokenT<G> iToken, F iF, std::string productInstance = std::string()) {
          using ReturnTypeT = decltype(iF(std::declval<edm::StreamID>(), std::declval<G>()));
          TypeID returnType(typeid(ReturnTypeT));
          TransformerBase::registerTransformImp(
              *this,
              EDPutToken(iToken),
              returnType,
              std::move(productInstance),
              [f = std::move(iF)](edm::StreamID id, std::any const& iGotProduct) {
                auto pGotProduct = std::any_cast<edm::WrapperBase const*>(iGotProduct);
                return std::make_unique<edm::Wrapper<ReturnTypeT>>(
                    WrapperBase::Emplace{}, f(id, *static_cast<edm::Wrapper<G> const*>(pGotProduct)->product()));
              });
        }

        template <typename G, typename P, typename F>
        void registerTransformAsync(edm::EDPutTokenT<G> iToken,
                                    P iPre,
                                    F iF,
                                    std::string productInstance = std::string()) {
          using CacheTypeT =
              decltype(iPre(std::declval<edm::StreamID>(), std::declval<G>(), WaitingTaskWithArenaHolder()));
          using ReturnTypeT = decltype(iF(std::declval<edm::StreamID>(), std::declval<CacheTypeT>()));
          TypeID returnType(typeid(ReturnTypeT));
          TransformerBase::registerTransformAsyncImp(
              *this,
              EDPutToken(iToken),
              returnType,
              std::move(productInstance),
              [p = std::move(iPre)](
                  edm::StreamID id, edm::WrapperBase const& iGotProduct, WaitingTaskWithArenaHolder iHolder) {
                return std::any(p(id, *static_cast<edm::Wrapper<G> const&>(iGotProduct).product(), std::move(iHolder)));
              },
              [f = std::move(iF)](edm::StreamID id, std::any const& iCache) {
                auto cache = std::any_cast<CacheTypeT>(iCache);
                return std::make_unique<edm::Wrapper<ReturnTypeT>>(WrapperBase::Emplace{}, f(id, cache));
              });
        }

      private:
        size_t transformIndex_(edm::ProductDescription const& iBranch) const noexcept final {
          return TransformerBase::findMatchingIndex(*this, iBranch);
        }
        ProductResolverIndex transformPrefetch_(std::size_t iIndex) const noexcept final {
          return TransformerBase::prefetchImp(iIndex);
        }
        void transformAsync_(WaitingTaskHolder iTask,
                             std::size_t iIndex,
                             edm::EventForTransformer& iEvent,
                             edm::ActivityRegistry* iAct,
                             ServiceWeakToken const& iToken) const noexcept final {
          return TransformerBase::transformImpAsync(std::move(iTask), iIndex, iAct, *this, iEvent);
        }
        void extendUpdateLookup(BranchType iBranchType, ProductResolverIndexHelper const& iHelper) override {
          if (iBranchType == InEvent) {
            TransformerBase::extendUpdateLookup(*this, this->moduleDescription(), iHelper);
          }
        }
      };

      class Accumulator : public EDProducerBase {
      public:
        Accumulator() = default;
        Accumulator(Accumulator const&) = delete;
        Accumulator& operator=(Accumulator const&) = delete;
        ~Accumulator() noexcept(false) override {}

        virtual void accumulate(Event const& ev, EventSetup const& es) = 0;

        void produce(Event& ev, EventSetup const& es) final { accumulate(ev, es); }
      };
    }  // namespace impl
  }  // namespace stream
}  // namespace edm

#endif