Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:30:08

0001 #ifndef SimGeneral_PremixingModule_PreMixingDigiSimLinkWorker_h
0002 #define SimGeneral_PremixingModule_PreMixingDigiSimLinkWorker_h
0003 
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/EventPrincipal.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/Framework/interface/ConsumesCollector.h"
0008 #include "FWCore/Framework/interface/ProducesCollector.h"
0009 #include "SimGeneral/MixingModule/interface/PileUpEventPrincipal.h"
0010 
0011 #include "DataFormats/Common/interface/Handle.h"
0012 
0013 #include "SimGeneral/PreMixingModule/interface/PreMixingWorker.h"
0014 
0015 template <typename DigiSimLinkCollection>
0016 class PreMixingDigiSimLinkWorker : public PreMixingWorker {
0017 public:
0018   PreMixingDigiSimLinkWorker(const edm::ParameterSet& ps, edm::ProducesCollector, edm::ConsumesCollector&& iC);
0019   ~PreMixingDigiSimLinkWorker() override = default;
0020 
0021   void initializeEvent(edm::Event const& iEvent, edm::EventSetup const& iSetup) override {}
0022   void addSignals(edm::Event const& iEvent, edm::EventSetup const& iSetup) override;
0023   void addPileups(PileUpEventPrincipal const& pep, edm::EventSetup const& iSetup) override;
0024   void put(edm::Event& iEvent,
0025            edm::EventSetup const& iSetup,
0026            std::vector<PileupSummaryInfo> const& ps,
0027            int bunchSpacing) override;
0028 
0029 private:
0030   edm::EDGetTokenT<DigiSimLinkCollection> signalToken_;
0031   edm::InputTag pileupTag_;
0032   std::string collectionDM_;  // secondary name to be given to new digis
0033 
0034   std::unique_ptr<DigiSimLinkCollection> merged_;
0035 };
0036 
0037 template <typename DigiSimLinkCollection>
0038 PreMixingDigiSimLinkWorker<DigiSimLinkCollection>::PreMixingDigiSimLinkWorker(const edm::ParameterSet& ps,
0039                                                                               edm::ProducesCollector producesCollector,
0040                                                                               edm::ConsumesCollector&& iC)
0041     : signalToken_(iC.consumes<DigiSimLinkCollection>(ps.getParameter<edm::InputTag>("labelSig"))),
0042       pileupTag_(ps.getParameter<edm::InputTag>("pileInputTag")),
0043       collectionDM_(ps.getParameter<std::string>("collectionDM")) {
0044   producesCollector.produces<DigiSimLinkCollection>(collectionDM_);
0045 }
0046 
0047 template <typename DigiSimLinkCollection>
0048 void PreMixingDigiSimLinkWorker<DigiSimLinkCollection>::addSignals(edm::Event const& iEvent,
0049                                                                    edm::EventSetup const& iSetup) {
0050   edm::Handle<DigiSimLinkCollection> digis;
0051   iEvent.getByToken(signalToken_, digis);
0052 
0053   if (digis.isValid()) {
0054     merged_ = std::make_unique<DigiSimLinkCollection>(*digis);  // for signal we can just copy
0055   } else {
0056     merged_ = std::make_unique<DigiSimLinkCollection>();
0057   }
0058 }
0059 
0060 template <typename DigiSimLinkCollection>
0061 void PreMixingDigiSimLinkWorker<DigiSimLinkCollection>::addPileups(PileUpEventPrincipal const& pep,
0062                                                                    edm::EventSetup const& iSetup) {
0063   edm::Handle<DigiSimLinkCollection> digis;
0064   pep.getByLabel(pileupTag_, digis);
0065   if (digis.isValid()) {
0066     for (const auto& detsetSource : *digis) {
0067       auto& detsetTarget = merged_->find_or_insert(detsetSource.detId());
0068       std::copy(detsetSource.begin(), detsetSource.end(), std::back_inserter(detsetTarget));
0069     }
0070   }
0071 }
0072 
0073 template <typename DigiSimLinkCollection>
0074 void PreMixingDigiSimLinkWorker<DigiSimLinkCollection>::put(edm::Event& iEvent,
0075                                                             edm::EventSetup const& iSetup,
0076                                                             std::vector<PileupSummaryInfo> const& ps,
0077                                                             int bunchSpacing) {
0078   iEvent.put(std::move(merged_), collectionDM_);
0079 }
0080 
0081 #endif