Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:30:31

0001 #ifndef Watcher_SimProducer_h
0002 #define Watcher_SimProducer_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     Watcher
0006 // Class  :     SimProducer
0007 //
0008 /**\class SimProducer SimProducer.h SimG4Core/Watcher/interface/SimProducer.h
0009 
0010  Description: a SimWatcher which puts data into the edm::Event
0011 
0012  Usage:
0013     <usage>
0014 
0015 */
0016 //
0017 // Original Author:  Chris D. Jones
0018 //         Created:  Mon Nov 28 16:02:21 EST 2005
0019 //
0020 
0021 // system include files
0022 #include <algorithm>
0023 #include <functional>
0024 #include <memory>
0025 #include <string>
0026 #include <vector>
0027 
0028 // user include files
0029 #include "FWCore/Framework/interface/ProducesCollector.h"
0030 #include "SimG4Core/Watcher/interface/SimWatcher.h"
0031 
0032 namespace edm {
0033   class Event;
0034   class EventSetup;
0035 }  // namespace edm
0036 
0037 // forward declarations
0038 namespace simproducer {
0039   class ProductInfoBase {
0040   public:
0041     ProductInfoBase(const std::string &iInstanceName) : m_instanceName(iInstanceName) {}
0042 
0043     virtual ~ProductInfoBase() {}
0044 
0045     const std::string &instanceName() const { return m_instanceName; }
0046 
0047     virtual void registerProduct(edm::ProducesCollector) const = 0;
0048 
0049   private:
0050     std::string m_instanceName;
0051   };
0052 
0053   template <class T>
0054   class ProductInfo : public ProductInfoBase {
0055   public:
0056     ProductInfo(const std::string &iInstanceName) : ProductInfoBase(iInstanceName) {}
0057 
0058     void registerProduct(edm::ProducesCollector producesCollector) const override {
0059       producesCollector.produces<T>(this->instanceName());
0060     }
0061   };
0062 }  // namespace simproducer
0063 
0064 class SimProducer : public SimWatcher {
0065 public:
0066   SimProducer() {}
0067 
0068   virtual void produce(edm::Event &, const edm::EventSetup &) = 0;
0069 
0070   void registerProducts(edm::ProducesCollector producesCollector) {
0071     std::for_each(m_info.begin(),
0072                   m_info.end(),
0073                   [&producesCollector](std::shared_ptr<simproducer::ProductInfoBase> const &ptr) mutable {
0074                     ptr->registerProduct(producesCollector);
0075                   });
0076   }
0077 
0078   SimProducer(const SimProducer &) = delete;
0079   const SimProducer &operator=(const SimProducer &) = delete;
0080 
0081 protected:
0082   template <class T>
0083   void produces() {
0084     produces<T>("");
0085   }
0086 
0087   template <class T>
0088   void produces(const std::string &instanceName) {
0089     m_info.push_back(std::make_shared<simproducer::ProductInfo<T>>(instanceName));
0090   }
0091 
0092 private:
0093   std::vector<std::shared_ptr<simproducer::ProductInfoBase>> m_info;
0094 };
0095 
0096 #endif