Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-29 11:58:18

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/TrackingAnalysis/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   produces<SimTrackToTPMap>("simTrackToTP");
0031   std::vector<edm::InputTag> tags = cfg.getParameter<std::vector<edm::InputTag>>("simHitSrc");
0032   _simHitSrc.reserve(tags.size());
0033   for (auto const &tag : tags) {
0034     _simHitSrc.emplace_back(consumes<edm::PSimHitContainer>(tag));
0035   }
0036 }
0037 
0038 SimHitTPAssociationProducer::~SimHitTPAssociationProducer() {}
0039 
0040 void SimHitTPAssociationProducer::produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &es) const {
0041   std::unique_ptr<SimHitTPAssociationList> simHitTPList(new SimHitTPAssociationList);
0042 
0043   // TrackingParticle
0044   edm::Handle<TrackingParticleCollection> TPCollectionH;
0045   iEvent.getByToken(_trackingParticleSrc, TPCollectionH);
0046 
0047   // prepare temporary map between SimTrackId and TrackingParticle index
0048   auto simTrackToTPMap = std::make_unique<SimTrackToTPMap>();
0049   auto const &tpColl = *TPCollectionH.product();
0050   for (TrackingParticleCollection::size_type itp = 0, size = tpColl.size(); itp < size; ++itp) {
0051     auto const &trackingParticle = tpColl[itp];
0052     TrackingParticleRef trackingParticleRef(TPCollectionH, itp);
0053     // SimTracks inside TrackingParticle
0054     EncodedEventId eid(trackingParticle.eventId());
0055     for (auto const &trk : trackingParticle.g4Tracks()) {
0056       UniqueSimTrackId trkid(trk.trackId(), eid);
0057       simTrackToTPMap->mapping.insert(std::make_pair(trkid, trackingParticleRef));
0058     }
0059   }
0060 
0061   // PSimHits
0062   for (auto const &psit : _simHitSrc) {
0063     edm::Handle<edm::PSimHitContainer> PSimHitCollectionH;
0064     iEvent.getByToken(psit, PSimHitCollectionH);
0065     auto const &pSimHitCollection = *PSimHitCollectionH;
0066     for (unsigned int psimHitI = 0, size = pSimHitCollection.size(); psimHitI < size; ++psimHitI) {
0067       TrackPSimHitRef pSimHitRef(PSimHitCollectionH, psimHitI);
0068       auto const &pSimHit = pSimHitCollection[psimHitI];
0069       UniqueSimTrackId simTkIds(pSimHit.trackId(), pSimHit.eventId());
0070       auto ipos = simTrackToTPMap->mapping.find(simTkIds);
0071       if (ipos != simTrackToTPMap->mapping.end()) {
0072         simHitTPList->emplace_back(ipos->second, pSimHitRef);
0073       }
0074     }
0075   }
0076 
0077   std::sort(simHitTPList->begin(), simHitTPList->end(), simHitTPAssociationListGreater);
0078   iEvent.put(std::move(simHitTPList));
0079   iEvent.put(std::move(simTrackToTPMap), "simTrackToTP");
0080 }
0081 
0082 #include "FWCore/Framework/interface/MakerMacros.h"
0083 #include "FWCore/PluginManager/interface/ModuleDef.h"
0084 
0085 DEFINE_FWK_MODULE(SimHitTPAssociationProducer);