Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:48

0001 #include <memory>
0002 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0003 #include "DataFormats/PatCandidates/interface/PackedGenParticle.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/global/EDProducer.h"
0006 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "FWCore/Utilities/interface/InputTag.h"
0009 
0010 namespace pat {
0011 
0012   class PackedGenParticleSignalProducer : public edm::global::EDProducer<> {
0013   public:
0014     explicit PackedGenParticleSignalProducer(const edm::ParameterSet& iConfig)
0015         : genParticleToken_(consumes<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("genParticles"))),
0016           assoToken_(consumes<edm::Association<std::vector<pat::PackedGenParticle>>>(
0017               iConfig.getParameter<edm::InputTag>("packedGenParticles"))) {
0018       produces<pat::PackedGenParticleRefVector>();
0019     }
0020     ~PackedGenParticleSignalProducer() override = default;
0021 
0022     void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0023 
0024     static void fillDescriptions(edm::ConfigurationDescriptions&);
0025 
0026   private:
0027     const edm::EDGetTokenT<reco::GenParticleCollection> genParticleToken_;
0028     const edm::EDGetTokenT<edm::Association<std::vector<pat::PackedGenParticle>>> assoToken_;
0029   };
0030 
0031 }  // namespace pat
0032 
0033 void pat::PackedGenParticleSignalProducer::produce(edm::StreamID iID,
0034                                                    edm::Event& iEvent,
0035                                                    const edm::EventSetup& iSetup) const {
0036   const auto& genParticles = iEvent.getHandle(genParticleToken_);
0037   const auto& orig2packed = iEvent.get(assoToken_);
0038 
0039   auto signalGenParticleRefs = std::make_unique<pat::PackedGenParticleRefVector>();
0040 
0041   for (auto it = genParticles->begin(); it != genParticles->end(); ++it) {
0042     const auto& orig = reco::GenParticleRef(genParticles, it - genParticles->begin());
0043     if (orig->collisionId() != 0)
0044       continue;
0045     const auto& packed = orig2packed[orig];
0046     if (packed.isNonnull()) {
0047       signalGenParticleRefs->push_back(packed);
0048     }
0049   }
0050 
0051   iEvent.put(std::move(signalGenParticleRefs));
0052 }
0053 
0054 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0055 void pat::PackedGenParticleSignalProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0056   edm::ParameterSetDescription desc;
0057   desc.add<edm::InputTag>("genParticles", edm::InputTag("genParticles"))->setComment("genParticles input collection");
0058   desc.add<edm::InputTag>("packedGenParticles", edm::InputTag("packedGenParticles"))
0059       ->setComment("packedGenParticles input collection");
0060   descriptions.add("packedGenParticlesSignal", desc);
0061 }
0062 
0063 #include "FWCore/Framework/interface/MakerMacros.h"
0064 using namespace pat;
0065 DEFINE_FWK_MODULE(PackedGenParticleSignalProducer);