Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-05 22:26:26

0001 // -*- C++ -*-
0002 //
0003 // Package:     SimMuon/MCTruth
0004 // Class  :     MuonToTrackingParticleAssociatorByHitsImpl
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  Christopher Jones
0010 //         Created:  Wed, 07 Jan 2015 21:35:56 GMT
0011 //
0012 
0013 // system include files
0014 
0015 // user include files
0016 #include "MuonToTrackingParticleAssociatorByHitsImpl.h"
0017 #include "SimMuon/MCTruth/interface/TrackerMuonHitExtractor.h"
0018 
0019 //
0020 // constants, enums and typedefs
0021 //
0022 
0023 //
0024 // static data member definitions
0025 //
0026 
0027 //
0028 // constructors and destructor
0029 //
0030 MuonToTrackingParticleAssociatorByHitsImpl::MuonToTrackingParticleAssociatorByHitsImpl(
0031     TrackerMuonHitExtractor const &iHitExtractor,
0032     TrackerHitAssociator::Config const &iTracker,
0033     CSCHitAssociator::Config const &iCSC,
0034     DTHitAssociator::Config const &iDT,
0035     RPCHitAssociator::Config const &iRPC,
0036     GEMHitAssociator::Config const &iGEM,
0037     edm::Event const &iEvent,
0038     edm::EventSetup const &iSetup,
0039     const TrackerTopology *iTopo,
0040     std::function<void(const TrackHitsCollection &, const TrackingParticleCollection &)> iDiagnostics,
0041     MuonAssociatorByHitsHelper const *iHelper)
0042     : m_hitExtractor(&iHitExtractor),
0043       m_tracker(iEvent, iTracker),
0044       m_csc(iEvent, iSetup, iCSC),
0045       m_dt(iEvent, iSetup, iDT, true),
0046       m_rpc(iEvent, iRPC),
0047       m_gem(iEvent, iGEM),
0048       m_resources(iTopo, &m_tracker, &m_csc, &m_dt, &m_rpc, &m_gem, iDiagnostics),
0049       m_helper(iHelper) {}
0050 
0051 //
0052 // member functions
0053 //
0054 
0055 //
0056 // const member functions
0057 //
0058 void MuonToTrackingParticleAssociatorByHitsImpl::associateMuons(
0059     reco::MuonToSimCollection &recToSim,
0060     reco::SimToMuonCollection &simToRec,
0061     const edm::RefToBaseVector<reco::Muon> &muons,
0062     reco::MuonTrackType type,
0063     const edm::RefVector<TrackingParticleCollection> &tPC) const {
0064   /// PART 1: Fill MuonToSimAssociatorByHits::TrackHitsCollection
0065   MuonAssociatorByHitsHelper::TrackHitsCollection muonHitRefs;
0066   edm::OwnVector<TrackingRecHit> allTMRecHits;  // this I will fill in only for tracker muon hits from
0067                                                 // segments
0068 
0069   std::vector<edm::OwnVector<TrackingRecHit>> TMRecHits;  // used for case GlbOrTrk
0070   edm::OwnVector<TrackingRecHit> noTM;
0071 
0072   switch (type) {
0073     case reco::InnerTk:
0074       for (edm::RefToBaseVector<reco::Muon>::const_iterator it = muons.begin(), ed = muons.end(); it != ed; ++it) {
0075         edm::RefToBase<reco::Muon> mur = *it;
0076         if (mur->track().isNonnull()) {
0077           muonHitRefs.push_back(std::make_pair(mur->track()->recHitsBegin(), mur->track()->recHitsEnd()));
0078         } else {
0079           muonHitRefs.push_back(std::make_pair(allTMRecHits.data().end(), allTMRecHits.data().end()));
0080         }
0081       }
0082       break;
0083     case reco::OuterTk:
0084       for (edm::RefToBaseVector<reco::Muon>::const_iterator it = muons.begin(), ed = muons.end(); it != ed; ++it) {
0085         edm::RefToBase<reco::Muon> mur = *it;
0086         if (mur->outerTrack().isNonnull()) {
0087           muonHitRefs.push_back(std::make_pair(mur->outerTrack()->recHitsBegin(), mur->outerTrack()->recHitsEnd()));
0088         } else {
0089           muonHitRefs.push_back(std::make_pair(allTMRecHits.data().end(), allTMRecHits.data().end()));
0090         }
0091       }
0092       break;
0093     case reco::GlobalTk:
0094       for (edm::RefToBaseVector<reco::Muon>::const_iterator it = muons.begin(), ed = muons.end(); it != ed; ++it) {
0095         edm::RefToBase<reco::Muon> mur = *it;
0096         if (mur->globalTrack().isNonnull()) {
0097           muonHitRefs.push_back(std::make_pair(mur->globalTrack()->recHitsBegin(), mur->globalTrack()->recHitsEnd()));
0098         } else {
0099           muonHitRefs.push_back(std::make_pair(allTMRecHits.data().end(), allTMRecHits.data().end()));
0100         }
0101       }
0102       break;
0103     case reco::Segments: {
0104       // puts hits in the vector, and record indices
0105       std::vector<std::pair<size_t, size_t>> muonHitIndices;
0106       for (edm::RefToBaseVector<reco::Muon>::const_iterator it = muons.begin(), ed = muons.end(); it != ed; ++it) {
0107         edm::RefToBase<reco::Muon> mur = *it;
0108         std::pair<size_t, size_t> indices(allTMRecHits.size(), allTMRecHits.size());
0109         if (mur->isTrackerMuon()) {
0110           std::vector<const TrackingRecHit *> hits = m_hitExtractor->getMuonHits(*mur);
0111           for (std::vector<const TrackingRecHit *>::const_iterator ith = hits.begin(), edh = hits.end(); ith != edh;
0112                ++ith) {
0113             allTMRecHits.push_back(**ith);
0114           }
0115           indices.second += hits.size();
0116         }
0117         muonHitIndices.push_back(indices);
0118       }
0119       // convert indices into pairs of iterators to references
0120       typedef std::pair<size_t, size_t> index_pair;
0121       trackingRecHit_iterator hitRefBegin = allTMRecHits.data().begin();
0122       for (std::vector<std::pair<size_t, size_t>>::const_iterator idxs = muonHitIndices.begin(),
0123                                                                   idxend = muonHitIndices.end();
0124            idxs != idxend;
0125            ++idxs) {
0126         muonHitRefs.push_back(std::make_pair(hitRefBegin + idxs->first, hitRefBegin + idxs->second));
0127       }
0128 
0129     } break;
0130     case reco::GlbOrTrk: {
0131       edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl")
0132           << "\n"
0133           << "There are " << muons.size() << " selected reco::Muons.";
0134 
0135       int isel = 0;
0136       for (edm::RefToBaseVector<reco::Muon>::const_iterator it = muons.begin(), ed = muons.end(); it != ed; ++it) {
0137         edm::RefToBase<reco::Muon> mur = *it;
0138 
0139         edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl")
0140             << " #" << isel << ", reco::Muon key = " << mur.key() << ", q*p = " << mur->charge() * mur->p()
0141             << ", pT = " << mur->pt() << ", eta = " << mur->eta() << ", phi = " << mur->phi();
0142 
0143         // Global muon with valid muon hits
0144         if (mur->isGlobalMuon() && mur->globalTrack()->hitPattern().numberOfValidMuonHits() > 0) {
0145           edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl")
0146               << "\t this is a Global Muon with valid muon hits";
0147           muonHitRefs.push_back(std::make_pair(mur->globalTrack()->recHitsBegin(), mur->globalTrack()->recHitsEnd()));
0148         }
0149 
0150         // Tracker Muon
0151         else if (mur->isTrackerMuon()) {
0152           edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl") << "\t this is a Tracker Muon";
0153           edm::OwnVector<TrackingRecHit> TMvec;
0154 
0155           std::vector<const TrackingRecHit *> hits = m_hitExtractor->getMuonHits(*mur);
0156           for (std::vector<const TrackingRecHit *>::const_iterator ith = hits.begin(), edh = hits.end(); ith != edh;
0157                ++ith) {
0158             TMvec.push_back(**ith);
0159           }
0160 
0161           TMRecHits.push_back(TMvec);
0162 
0163           muonHitRefs.push_back(std::make_pair(TMRecHits.rbegin()->data().begin(), TMRecHits.rbegin()->data().end()));
0164         }
0165 
0166         // Standalone muon or Global without valid muon hits
0167         else if (mur->outerTrack().isNonnull()) {
0168           edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl") << "\t this is a Standalone muon";
0169           muonHitRefs.push_back(std::make_pair(mur->outerTrack()->recHitsBegin(), mur->outerTrack()->recHitsEnd()));
0170         }
0171 
0172         else {
0173           edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl") << "\t what muon is this ?";
0174           edm::LogVerbatim("MuonToTrackingParticleAssociatorByHitsImpl")
0175               << "isMuon : " << mur->isMuon() << ", isPFMuon : " << mur->isPFMuon()
0176               << ", isTrackerMuon : " << mur->isTrackerMuon() << ", isStandAloneMuon : " << mur->isStandAloneMuon()
0177               << ", isGlobalMuon : " << mur->isGlobalMuon() << ", isRPCMuon : " << mur->isRPCMuon()
0178               << ", isGEMMuon : " << mur->isGEMMuon() << ", isME0Muon : " << mur->isME0Muon();
0179           muonHitRefs.push_back(std::make_pair(noTM.data().end(), noTM.data().end()));
0180         }
0181 
0182         isel++;
0183       }  // loop on muons
0184 
0185     } break;
0186   }
0187 
0188   /// PART 2: call the association routines
0189   auto recSimColl = m_helper->associateRecoToSimIndices(muonHitRefs, tPC, m_resources);
0190   for (auto it = recSimColl.begin(), ed = recSimColl.end(); it != ed; ++it) {
0191     edm::RefToBase<reco::Muon> rec = muons[it->first];
0192     std::vector<std::pair<TrackingParticleRef, double>> &tpAss = recToSim[rec];
0193     for (auto const &a : it->second) {
0194       tpAss.push_back(std::make_pair(tPC[a.idx], a.quality));
0195     }
0196   }
0197   auto simRecColl = m_helper->associateSimToRecoIndices(muonHitRefs, tPC, m_resources);
0198   for (auto it = simRecColl.begin(), ed = simRecColl.end(); it != ed; ++it) {
0199     TrackingParticleRef sim = tPC[it->first];
0200     std::vector<std::pair<edm::RefToBase<reco::Muon>, double>> &recAss = simToRec[sim];
0201     for (auto const &a : it->second) {
0202       recAss.push_back(std::make_pair(muons[a.idx], a.quality));
0203     }
0204   }
0205 }
0206 
0207 void MuonToTrackingParticleAssociatorByHitsImpl::associateMuons(
0208     reco::MuonToSimCollection &recToSim,
0209     reco::SimToMuonCollection &simToRec,
0210     const edm::Handle<edm::View<reco::Muon>> &tCH,
0211     reco::MuonTrackType type,
0212     const edm::Handle<TrackingParticleCollection> &tPCH) const {
0213   edm::RefVector<TrackingParticleCollection> tpc(tPCH.id());
0214   for (unsigned int j = 0; j < tPCH->size(); j++)
0215     tpc.push_back(edm::Ref<TrackingParticleCollection>(tPCH, j));
0216 
0217   edm::RefToBaseVector<reco::Muon> muonBaseRefVector;
0218   for (size_t i = 0; i < tCH->size(); ++i)
0219     muonBaseRefVector.push_back(tCH->refAt(i));
0220 
0221   associateMuons(recToSim, simToRec, muonBaseRefVector, type, tpc);
0222 }
0223 
0224 //
0225 // static member functions
0226 //