Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-13 23:00:18

0001 // -*- C++ -*-
0002 //
0003 // Package:    SimTracker/TrackAssociation
0004 // Class:      DigiSimLinkPruner
0005 //
0006 /**\class DigiSimLinkPruner DigiSimLinkPruner.cc SimTracker/TrackAssociation/plugins/DigiSimLinkPruner.cc
0007 
0008  Description: Produce a pruned version of the DigiSimLinks collection based on the association to a collection of TrackingParticles
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Enrico Lusiani
0015 //         Created:  Fri, 14 May 2021 08:46:10 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/global/EDProducer.h"
0025 
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028 
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Utilities/interface/StreamID.h"
0031 
0032 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
0033 #include "DataFormats/Common/interface/DetSetVector.h"
0034 #include "SimDataFormats/TrackerDigiSimLink/interface/PixelDigiSimLink.h"
0035 #include "SimDataFormats/TrackerDigiSimLink/interface/StripDigiSimLink.h"
0036 #include "SimDataFormats/Track/interface/UniqueSimTrackId.h"
0037 
0038 #include <unordered_set>
0039 
0040 namespace {
0041 
0042   template <typename T>
0043   std::vector<edm::DetSet<T>> pruneByTpAssoc(
0044       const edm::DetSetVector<T>& simLinkColl,
0045       const std::unordered_set<UniqueSimTrackId, UniqueSimTrackIdHash>& selectedIds) {
0046     std::vector<edm::DetSet<T>> linkVector;
0047 
0048     for (auto&& detSet : simLinkColl) {
0049       edm::DetSet<T> newDetSet(detSet.detId());
0050 
0051       for (auto&& simLink : detSet) {
0052         UniqueSimTrackId trkid(simLink.SimTrackId(), simLink.eventId());
0053         if (selectedIds.count(trkid) > 0) {
0054           newDetSet.push_back(simLink);
0055         }
0056       }
0057 
0058       linkVector.push_back(std::move(newDetSet));
0059     }
0060 
0061     return linkVector;
0062   }
0063 
0064 }  // namespace
0065 
0066 //
0067 // class declaration
0068 //
0069 
0070 class DigiSimLinkPruner : public edm::global::EDProducer<> {
0071 public:
0072   explicit DigiSimLinkPruner(const edm::ParameterSet&);
0073 
0074   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0075 
0076 private:
0077   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0078 
0079   // ----------member data ---------------------------
0080   edm::EDGetTokenT<TrackingParticleCollection> trackingParticleToken_;
0081   edm::EDGetTokenT<edm::DetSetVector<PixelDigiSimLink>> sipixelSimLinksToken_;
0082   edm::EDGetTokenT<edm::DetSetVector<StripDigiSimLink>> sistripSimLinksToken_;
0083   edm::EDGetTokenT<edm::DetSetVector<PixelDigiSimLink>> siphase2OTSimLinksToken_;
0084 };
0085 
0086 DigiSimLinkPruner::DigiSimLinkPruner(const edm::ParameterSet& iConfig)
0087     : trackingParticleToken_(
0088           consumes<TrackingParticleCollection>(iConfig.getParameter<edm::InputTag>("trackingParticles"))),
0089       sipixelSimLinksToken_(
0090           consumes<edm::DetSetVector<PixelDigiSimLink>>(iConfig.getParameter<edm::InputTag>("pixelSimLinkSrc"))) {
0091   produces<edm::DetSetVector<PixelDigiSimLink>>("siPixel");
0092 
0093   if (iConfig.existsAs<edm::InputTag>("stripSimLinkSrc")) {
0094     sistripSimLinksToken_ =
0095         consumes<edm::DetSetVector<StripDigiSimLink>>(iConfig.getParameter<edm::InputTag>("stripSimLinkSrc"));
0096     produces<edm::DetSetVector<StripDigiSimLink>>("siStrip");
0097   }
0098 
0099   if (iConfig.existsAs<edm::InputTag>("phase2OTSimLinkSrc")) {
0100     siphase2OTSimLinksToken_ =
0101         consumes<edm::DetSetVector<PixelDigiSimLink>>(iConfig.getParameter<edm::InputTag>("phase2OTSimLinkSrc"));
0102     produces<edm::DetSetVector<PixelDigiSimLink>>("siphase2OT");
0103   }
0104 }
0105 
0106 // ------------ method called to produce the data  ------------
0107 void DigiSimLinkPruner::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0108   using namespace edm;
0109 
0110   auto const& tpColl = iEvent.get(trackingParticleToken_);
0111 
0112   std::unordered_set<UniqueSimTrackId, UniqueSimTrackIdHash> selectedIds;
0113   for (TrackingParticleCollection::size_type itp = 0; itp < tpColl.size(); ++itp) {
0114     auto const& trackingParticle = tpColl[itp];
0115 
0116     // SimTracks inside TrackingParticle
0117 
0118     EncodedEventId eid(trackingParticle.eventId());
0119 
0120     for (auto const& trk : trackingParticle.g4Tracks()) {
0121       selectedIds.emplace(trk.trackId(), eid);
0122     }
0123   }
0124 
0125   auto const& sipixelLinkColl = iEvent.get(sipixelSimLinksToken_);
0126 
0127   auto sipixelLinkVector = pruneByTpAssoc(sipixelLinkColl, selectedIds);
0128 
0129   auto sipixelOut = std::make_unique<edm::DetSetVector<PixelDigiSimLink>>(sipixelLinkVector);
0130   iEvent.put(std::move(sipixelOut), "siPixel");
0131 
0132   if (not sistripSimLinksToken_.isUninitialized()) {
0133     auto const& sistripLinkColl = iEvent.get(sistripSimLinksToken_);
0134 
0135     auto sistripLinkVector = pruneByTpAssoc(sistripLinkColl, selectedIds);
0136 
0137     auto sistripOut = std::make_unique<edm::DetSetVector<StripDigiSimLink>>(sistripLinkVector);
0138     iEvent.put(std::move(sistripOut), "siStrip");
0139   }
0140 
0141   if (not siphase2OTSimLinksToken_.isUninitialized()) {
0142     auto const& siphase2OTLinkColl = iEvent.get(siphase2OTSimLinksToken_);
0143 
0144     auto siphase2OTLinkVector = pruneByTpAssoc(siphase2OTLinkColl, selectedIds);
0145 
0146     auto siphase2OTOut = std::make_unique<edm::DetSetVector<PixelDigiSimLink>>(siphase2OTLinkVector);
0147     iEvent.put(std::move(siphase2OTOut), "siphase2OT");
0148   }
0149 }
0150 
0151 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0152 void DigiSimLinkPruner::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0153   edm::ParameterSetDescription desc;
0154   desc.add<edm::InputTag>("trackingParticles");
0155   desc.add<edm::InputTag>("pixelSimLinkSrc", edm::InputTag("simSiPixelDigis"));
0156   desc.addOptional<edm::InputTag>("stripSimLinkSrc");
0157   desc.addOptional<edm::InputTag>("phase2OTSimLinkSrc");
0158 
0159   descriptions.add("digiSimLinkPrunerDefault", desc);
0160 }
0161 
0162 //define this as a plug-in
0163 DEFINE_FWK_MODULE(DigiSimLinkPruner);