Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:56

0001 #include <string>
0002 
0003 #include "DataFormats/Candidate/interface/Candidate.h"
0004 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0005 #include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
0006 #include "DataFormats/VertexReco/interface/Vertex.h"
0007 #include "DataFormats/VertexReco/interface/VertexFwd.h"
0008 #include "DataFormats/PatCandidates/interface/PackedGenParticle.h"
0009 #include "DataFormats/PatCandidates/interface/Jet.h"
0010 #include "DataFormats/Common/interface/Association.h"
0011 #include "FWCore/Framework/interface/global/EDProducer.h"
0012 #include "DataFormats/Common/interface/View.h"
0013 #include "FWCore/Framework/interface/ESHandle.h"
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include "FWCore/Framework/interface/EventSetup.h"
0016 #include "FWCore/Framework/interface/Frameworkfwd.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "FWCore/Utilities/interface/Exception.h"
0019 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
0020 #include "DataFormats/MuonReco/interface/Muon.h"
0021 
0022 #include "TrackingTools/IPTools/interface/IPTools.h"
0023 #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
0024 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
0025 
0026 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
0027 #include "TrackingTools/GeomPropagators/interface/AnalyticalTrajectoryExtrapolatorToLine.h"
0028 #include "TrackingTools/GeomPropagators/interface/AnalyticalImpactPointExtrapolator.h"
0029 #include "DataFormats/GeometryCommonDetAlgo/interface/Measurement1D.h"
0030 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
0031 #include "TrackingTools/IPTools/interface/IPTools.h"
0032 #include "CLHEP/Vector/ThreeVector.h"
0033 #include "CLHEP/Vector/LorentzVector.h"
0034 #include "CLHEP/Matrix/Vector.h"
0035 #include <string>
0036 
0037 namespace pat {
0038   class PATPackedGenParticleProducer : public edm::global::EDProducer<> {
0039   public:
0040     explicit PATPackedGenParticleProducer(const edm::ParameterSet&);
0041     ~PATPackedGenParticleProducer() override;
0042 
0043     void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0044 
0045   private:
0046     const edm::EDGetTokenT<reco::GenParticleCollection> Cands_;
0047     const edm::EDGetTokenT<reco::GenParticleCollection> GenOrigs_;
0048     const edm::EDGetTokenT<edm::Association<reco::GenParticleCollection>> Asso_;
0049     const edm::EDGetTokenT<edm::Association<reco::GenParticleCollection>> AssoOriginal_;
0050     const double maxRapidity_;
0051   };
0052 }  // namespace pat
0053 
0054 pat::PATPackedGenParticleProducer::PATPackedGenParticleProducer(const edm::ParameterSet& iConfig)
0055     : Cands_(consumes<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("inputCollection"))),
0056       GenOrigs_(consumes<reco::GenParticleCollection>(iConfig.getParameter<edm::InputTag>("inputOriginal"))),
0057       Asso_(consumes<edm::Association<reco::GenParticleCollection>>(iConfig.getParameter<edm::InputTag>("map"))),
0058       AssoOriginal_(consumes<edm::Association<reco::GenParticleCollection>>(
0059           iConfig.getParameter<edm::InputTag>("inputCollection"))),
0060       maxRapidity_(iConfig.getParameter<double>("maxRapidity")) {
0061   produces<std::vector<pat::PackedGenParticle>>();
0062   produces<edm::Association<std::vector<pat::PackedGenParticle>>>();
0063 }
0064 
0065 pat::PATPackedGenParticleProducer::~PATPackedGenParticleProducer() {}
0066 
0067 void pat::PATPackedGenParticleProducer::produce(edm::StreamID,
0068                                                 edm::Event& iEvent,
0069                                                 const edm::EventSetup& iSetup) const {
0070   edm::Handle<reco::GenParticleCollection> cands;
0071   iEvent.getByToken(Cands_, cands);
0072 
0073   //from prunedGenParticlesWithStatusOne to prunedGenParticles
0074   edm::Handle<edm::Association<reco::GenParticleCollection>> asso;
0075   iEvent.getByToken(Asso_, asso);
0076 
0077   edm::Handle<edm::Association<reco::GenParticleCollection>> assoOriginal;
0078   iEvent.getByToken(AssoOriginal_, assoOriginal);
0079 
0080   edm::Handle<reco::GenParticleCollection> genOrigs;
0081   iEvent.getByToken(GenOrigs_, genOrigs);
0082   std::vector<int> mapping(genOrigs->size(), -1);
0083 
0084   //invert the value map from Orig2New to New2Orig
0085   std::map<edm::Ref<reco::GenParticleCollection>, edm::Ref<reco::GenParticleCollection>> reverseMap;
0086   for (unsigned int ic = 0, nc = genOrigs->size(); ic < nc; ++ic) {
0087     edm::Ref<reco::GenParticleCollection> originalRef = edm::Ref<reco::GenParticleCollection>(genOrigs, ic);
0088     edm::Ref<reco::GenParticleCollection> newRef = (*assoOriginal)[originalRef];
0089     reverseMap.insert(
0090         std::pair<edm::Ref<reco::GenParticleCollection>, edm::Ref<reco::GenParticleCollection>>(newRef, originalRef));
0091   }
0092 
0093   auto outPtrP = std::make_unique<std::vector<pat::PackedGenParticle>>();
0094 
0095   unsigned int packed = 0;
0096   for (unsigned int ic = 0, nc = cands->size(); ic < nc; ++ic) {
0097     const reco::GenParticle& cand = (*cands)[ic];
0098     if (cand.status() == 1 && std::abs(cand.y()) < maxRapidity_) {
0099       // Obtain original gen particle collection reference from input reference and map
0100       edm::Ref<reco::GenParticleCollection> inputRef = edm::Ref<reco::GenParticleCollection>(cands, ic);
0101       edm::Ref<reco::GenParticleCollection> originalRef = reverseMap[inputRef];
0102       edm::Ref<reco::GenParticleCollection> finalPrunedRef = (*asso)[inputRef];
0103       mapping[originalRef.key()] = packed;
0104       packed++;
0105       if (finalPrunedRef.isNonnull()) {  //this particle exists also in the final pruned
0106         outPtrP->push_back(pat::PackedGenParticle(cand, finalPrunedRef));
0107       } else {
0108         if (cand.numberOfMothers() > 0) {
0109           edm::Ref<reco::GenParticleCollection> newRef = (*asso)[cand.motherRef(0)];
0110           outPtrP->push_back(pat::PackedGenParticle(cand, newRef));
0111         } else {
0112           outPtrP->push_back(pat::PackedGenParticle(cand, edm::Ref<reco::GenParticleCollection>()));
0113         }
0114       }
0115     }
0116   }
0117 
0118   edm::OrphanHandle<std::vector<pat::PackedGenParticle>> oh = iEvent.put(std::move(outPtrP));
0119 
0120   auto gp2pgp = std::make_unique<edm::Association<std::vector<pat::PackedGenParticle>>>(oh);
0121   edm::Association<std::vector<pat::PackedGenParticle>>::Filler gp2pgpFiller(*gp2pgp);
0122   gp2pgpFiller.insert(genOrigs, mapping.begin(), mapping.end());
0123   gp2pgpFiller.fill();
0124   iEvent.put(std::move(gp2pgp));
0125 }
0126 
0127 using pat::PATPackedGenParticleProducer;
0128 #include "FWCore/Framework/interface/MakerMacros.h"
0129 DEFINE_FWK_MODULE(PATPackedGenParticleProducer);