Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-04-24 02:25:19

0001 #include <algorithm>
0002 #include <map>
0003 #include <vector>
0004 
0005 #include "FWCore/Utilities/interface/InputTag.h"
0006 #include "FWCore/Framework/interface/ESHandle.h"
0007 #include "FWCore/Framework/interface/EventSetup.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/Frameworkfwd.h"
0012 #include "FWCore/Framework/interface/global/EDProducer.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0015 #include "FWCore/Utilities/interface/EDGetToken.h"
0016 
0017 #include "DataFormats/Common/interface/Handle.h"
0018 #include "SimDataFormats/Track/interface/UniqueSimTrackId.h"
0019 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
0020 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticleFwd.h"
0021 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
0022 
0023 #include "SimGeneral/TrackingAnalysis/interface/SimHitTPAssociationProducer.h"
0024 
0025 SimHitTPAssociationProducer::SimHitTPAssociationProducer(const edm::ParameterSet &cfg)
0026     : _simHitSrc(),
0027       _trackingParticleSrc(
0028           consumes<TrackingParticleCollection>(cfg.getParameter<edm::InputTag>("trackingParticleSrc"))) {
0029   produces<SimHitTPAssociationList>();
0030   std::vector<edm::InputTag> tags = cfg.getParameter<std::vector<edm::InputTag>>("simHitSrc");
0031   _simHitSrc.reserve(tags.size());
0032   for (auto const &tag : tags) {
0033     _simHitSrc.emplace_back(consumes<edm::PSimHitContainer>(tag));
0034   }
0035 }
0036 
0037 SimHitTPAssociationProducer::~SimHitTPAssociationProducer() {}
0038 
0039 void SimHitTPAssociationProducer::produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &es) const {
0040   std::unique_ptr<SimHitTPAssociationList> simHitTPList(new SimHitTPAssociationList);
0041 
0042   // TrackingParticle
0043   edm::Handle<TrackingParticleCollection> TPCollectionH;
0044   iEvent.getByToken(_trackingParticleSrc, TPCollectionH);
0045 
0046   // prepare temporary map between SimTrackId and TrackingParticle index
0047   std::unordered_map<UniqueSimTrackId, TrackingParticleRef, UniqueSimTrackIdHash> mapping;
0048   auto const &tpColl = *TPCollectionH.product();
0049   for (TrackingParticleCollection::size_type itp = 0, size = tpColl.size(); itp < size; ++itp) {
0050     auto const &trackingParticle = tpColl[itp];
0051     TrackingParticleRef trackingParticleRef(TPCollectionH, itp);
0052     // SimTracks inside TrackingParticle
0053     EncodedEventId eid(trackingParticle.eventId());
0054     for (auto const &trk : trackingParticle.g4Tracks()) {
0055       UniqueSimTrackId trkid(trk.trackId(), eid);
0056       mapping.insert(std::make_pair(trkid, trackingParticleRef));
0057     }
0058   }
0059 
0060   // PSimHits
0061   for (auto const &psit : _simHitSrc) {
0062     edm::Handle<edm::PSimHitContainer> PSimHitCollectionH;
0063     iEvent.getByToken(psit, PSimHitCollectionH);
0064     auto const &pSimHitCollection = *PSimHitCollectionH;
0065     for (unsigned int psimHitI = 0, size = pSimHitCollection.size(); psimHitI < size; ++psimHitI) {
0066       TrackPSimHitRef pSimHitRef(PSimHitCollectionH, psimHitI);
0067       auto const &pSimHit = pSimHitCollection[psimHitI];
0068       UniqueSimTrackId simTkIds(pSimHit.trackId(), pSimHit.eventId());
0069       auto ipos = mapping.find(simTkIds);
0070       if (ipos != mapping.end()) {
0071         simHitTPList->emplace_back(ipos->second, pSimHitRef);
0072       }
0073     }
0074   }
0075 
0076   std::sort(simHitTPList->begin(), simHitTPList->end(), simHitTPAssociationListGreater);
0077   iEvent.put(std::move(simHitTPList));
0078 }
0079 
0080 #include "FWCore/Framework/interface/MakerMacros.h"
0081 #include "FWCore/PluginManager/interface/ModuleDef.h"
0082 
0083 DEFINE_FWK_MODULE(SimHitTPAssociationProducer);