From d458e9588a39df25ecd146065709baa76e9bcef9 Mon Sep 17 00:00:00 2001 From: mmusich Date: Tue, 28 Mar 2023 13:13:57 +0200 Subject: [PATCH 1/5] modernize loops in DisappearingMuonsSkimming --- .../interface/DisappearingMuonsSkimming.h | 35 +++-- .../Skimming/src/DisappearingMuonsSkimming.cc | 126 +++++++++--------- 2 files changed, 77 insertions(+), 84 deletions(-) diff --git a/Configuration/Skimming/interface/DisappearingMuonsSkimming.h b/Configuration/Skimming/interface/DisappearingMuonsSkimming.h index 750ef843753e2..76247902f3143 100644 --- a/Configuration/Skimming/interface/DisappearingMuonsSkimming.h +++ b/Configuration/Skimming/interface/DisappearingMuonsSkimming.h @@ -22,19 +22,21 @@ #include // user include filter -#include "FWCore/Utilities/interface/EDGetToken.h" -#include "FWCore/Framework/interface/one/EDFilter.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" -#include "DataFormats/TrackReco/interface/Track.h" -#include "TrackingTools/TransientTrack/interface/TransientTrack.h" -#include "DataFormats/VertexReco/interface/Vertex.h" #include "DataFormats/Common/interface/TriggerResults.h" #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h" -#include "TrackingTools/Records/interface/TransientTrackRecord.h" -#include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h" +#include "DataFormats/MuonReco/interface/Muon.h" +#include "DataFormats/MuonReco/interface/MuonFwd.h" +#include "DataFormats/TrackReco/interface/Track.h" +#include "DataFormats/VertexReco/interface/Vertex.h" +#include "FWCore/Framework/interface/one/EDFilter.h" +#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/Utilities/interface/EDGetToken.h" #include "Geometry/CaloGeometry/interface/CaloGeometry.h" #include "Geometry/Records/interface/CaloGeometryRecord.h" +#include "TrackingTools/Records/interface/TransientTrackRecord.h" +#include "TrackingTools/TransientTrack/interface/TransientTrack.h" +#include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h" // // class declaration @@ -53,22 +55,17 @@ class DisappearingMuonsSkimming : public edm::one::EDFilter<> { bool passTriggers(const edm::Event& iEvent, edm::EDGetToken m_trigResultsToken, std::vector m_muonPathsToPass); - double getTrackIsolation(const edm::Event&, - edm::Handle vtxHandle, - std::vector::const_iterator& iTrack); + double getTrackIsolation(const edm::Event&, const reco::VertexCollection& vtxHandle, const reco::Track& iTrack); double getECALIsolation(const edm::Event&, const edm::EventSetup&, const reco::TransientTrack track); // ----------member data --------------------------- - - const edm::EDGetToken recoMuonToken_; - const edm::EDGetToken standaloneMuonToken_; - const edm::EDGetTokenT> trackCollectionToken_; - const edm::EDGetTokenT> primaryVerticesToken_; + const edm::EDGetTokenT recoMuonToken_; + const edm::EDGetTokenT standaloneMuonToken_; + const edm::EDGetTokenT trackCollectionToken_; + const edm::EDGetTokenT primaryVerticesToken_; const edm::EDGetTokenT reducedEndcapRecHitCollectionToken_; const edm::EDGetTokenT reducedBarrelRecHitCollectionToken_; const edm::EDGetTokenT trigResultsToken_; - const edm::EDGetToken genParticleToken_; - const edm::EDGetToken genInfoToken_; const edm::ESGetToken transientTrackToken_; const edm::ESGetToken geometryToken_; const std::vector muonPathsToPass_; diff --git a/Configuration/Skimming/src/DisappearingMuonsSkimming.cc b/Configuration/Skimming/src/DisappearingMuonsSkimming.cc index a1fed76d152dd..a98336bed0878 100644 --- a/Configuration/Skimming/src/DisappearingMuonsSkimming.cc +++ b/Configuration/Skimming/src/DisappearingMuonsSkimming.cc @@ -17,38 +17,37 @@ // // system include files #include - #include "Math/VectorUtil.h" + // user include files #include "Configuration/Skimming/interface/DisappearingMuonsSkimming.h" -#include "FWCore/Framework/interface/Frameworkfwd.h" -#include "FWCore/Framework/interface/one/EDFilter.h" -#include "FWCore/Framework/interface/Event.h" -#include "FWCore/Framework/interface/MakerMacros.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" #include "DataFormats/Common/interface/TriggerResults.h" -#include "FWCore/Common/interface/TriggerNames.h" +#include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h" +#include "DataFormats/Math/interface/deltaR.h" #include "DataFormats/PatCandidates/interface/Muon.h" +#include "DataFormats/TrackReco/interface/Track.h" #include "DataFormats/VertexReco/interface/Vertex.h" -#include "FWCore/Utilities/interface/InputTag.h" +#include "FWCore/Common/interface/TriggerNames.h" +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/MakerMacros.h" +#include "FWCore/Framework/interface/one/EDFilter.h" +#include "FWCore/ParameterSet/interface/ParameterSet.h" #include "FWCore/Utilities/interface/EDGetToken.h" -#include "DataFormats/Math/interface/deltaR.h" -#include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h" -#include "DataFormats/TrackReco/interface/Track.h" +#include "FWCore/Utilities/interface/InputTag.h" #include "Geometry/CaloGeometry/interface/CaloGeometry.h" #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h" #include "Geometry/Records/interface/CaloGeometryRecord.h" +#include "RecoVertex/KalmanVertexFit/interface/KalmanVertexFitter.h" +#include "TrackingTools/Records/interface/TransientTrackRecord.h" #include "TrackingTools/TransientTrack/interface/TransientTrack.h" #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h" -#include "TrackingTools/Records/interface/TransientTrackRecord.h" -#include "RecoVertex/KalmanVertexFit/interface/KalmanVertexFitter.h" DisappearingMuonsSkimming::DisappearingMuonsSkimming(const edm::ParameterSet& iConfig) - : recoMuonToken_(consumes>(iConfig.getParameter("recoMuons"))), - standaloneMuonToken_(consumes>(iConfig.getParameter("StandaloneTracks"))), - trackCollectionToken_(consumes>(iConfig.getParameter("tracks"))), - primaryVerticesToken_( - consumes>(iConfig.getParameter("primaryVertices"))), + : recoMuonToken_(consumes(iConfig.getParameter("recoMuons"))), + standaloneMuonToken_(consumes(iConfig.getParameter("StandaloneTracks"))), + trackCollectionToken_(consumes(iConfig.getParameter("tracks"))), + primaryVerticesToken_(consumes(iConfig.getParameter("primaryVertices"))), reducedEndcapRecHitCollectionToken_( consumes(iConfig.getParameter("EERecHits"))), reducedBarrelRecHitCollectionToken_( @@ -98,14 +97,11 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup bool offPeak = false; bool partialRegion = false; - edm::Handle staTracks; - iEvent.getByToken(standaloneMuonToken_, staTracks); - edm::Handle> vertices; - iEvent.getByToken(primaryVerticesToken_, vertices); - edm::Handle> recoMuons; - iEvent.getByToken(recoMuonToken_, recoMuons); - edm::Handle> thePATTrackHandle; - iEvent.getByToken(trackCollectionToken_, thePATTrackHandle); + const auto& staTracks = iEvent.get(standaloneMuonToken_); + const auto& vertices = iEvent.get(primaryVerticesToken_); + const auto& recoMuons = iEvent.get(recoMuonToken_); + const auto& thePATTrackHandle = iEvent.get(trackCollectionToken_); + // this wraps tracks with additional methods that are used in vertex-calculation const TransientTrackBuilder* transientTrackBuilder = &iSetup.getData(transientTrackToken_); @@ -116,39 +112,39 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup float MuonTrackMass = 0.; //Looping over the reconstructed Muons - for (std::vector::const_iterator iMuon = recoMuons->begin(); iMuon != recoMuons->end(); iMuon++) { - if (!(iMuon->isPFMuon() && iMuon->isGlobalMuon())) + for (const auto& iMuon : recoMuons) { + if (!(iMuon.isPFMuon() && iMuon.isGlobalMuon())) continue; - if (!(iMuon->passed(reco::Muon::CutBasedIdTight))) + if (!(iMuon.passed(reco::Muon::CutBasedIdTight))) continue; - if (!(iMuon->passed(reco::Muon::PFIsoTight))) + if (!(iMuon.passed(reco::Muon::PFIsoTight))) continue; - if (iMuon->pt() < minMuPt_ || fabs(iMuon->eta()) > maxMuEta_) + if (iMuon.pt() < minMuPt_ || std::abs(iMuon.eta()) > maxMuEta_) continue; //Looping over tracks for any good muon - for (std::vector::const_iterator iTrack = thePATTrackHandle->begin(); - iTrack != thePATTrackHandle->end(); - ++iTrack) { - if (!iTrack->quality(reco::Track::qualityByName("highPurity"))) + for (const auto& iTrack : thePATTrackHandle) { + + if (!iTrack.quality(reco::Track::qualityByName("highPurity"))) continue; - if (fabs(iTrack->eta()) > maxTrackEta_ || fabs(iTrack->eta()) < minTrackEta_) + if (std::abs(iTrack.eta()) > maxTrackEta_ || std::abs(iTrack.eta()) < minTrackEta_) continue; - if (iTrack->pt() < minTrackPt_) + if (iTrack.pt() < minTrackPt_) continue; //Check if the track belongs to a primary vertex for isolation calculation + bool foundtrack = false; GlobalPoint tkVtx; - for (unsigned int i = 0; i < vertices->size(); i++) { - reco::VertexRef vtx(vertices, i); + for (unsigned int i = 0; i < vertices.size(); i++) { + reco::VertexRef vtx(&vertices, i); if (!vtx->isValid()) { continue; } for (unsigned int j = 0; j < vtx->tracksSize(); j++) { - double dPt = fabs(vtx->trackRefAt(j)->pt() - iTrack->pt()) / iTrack->pt(); + double dPt = std::abs(vtx->trackRefAt(j)->pt() - iTrack.pt()) / iTrack.pt(); //Find the vertex track that is the same as the probe if (dPt < 0.001) { - double dR2 = deltaR2(vtx->trackRefAt(j)->eta(), vtx->trackRefAt(j)->phi(), iTrack->eta(), iTrack->phi()); + double dR2 = deltaR2(vtx->trackRefAt(j)->eta(), vtx->trackRefAt(j)->phi(), iTrack.eta(), iTrack.phi()); if (dR2 < 0.001 * 0.001) { foundtrack = true; GlobalPoint vert(vtx->x(), vtx->y(), vtx->z()); @@ -158,20 +154,21 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup } } } + if (!foundtrack) continue; - reco::TransientTrack tk = transientTrackBuilder->build(*iTrack); + reco::TransientTrack tk = transientTrackBuilder->build(iTrack); TrajectoryStateClosestToPoint traj = tk.trajectoryStateClosestToPoint(tkVtx); double transDCA = traj.perigeeParameters().transverseImpactParameter(); double longDCA = traj.perigeeParameters().longitudinalImpactParameter(); - if (fabs(longDCA) > maxLongDCA_) + if (std::abs(longDCA) > maxLongDCA_) continue; - if (fabs(transDCA) > maxTransDCA_) + if (std::abs(transDCA) > maxTransDCA_) continue; // make a pair of TransientTracks to feed the vertexer std::vector tracksToVertex; - tracksToVertex.push_back(transientTrackBuilder->build(*iTrack)); - tracksToVertex.push_back(transientTrackBuilder->build(iMuon->globalTrack())); + tracksToVertex.push_back(transientTrackBuilder->build(iTrack)); + tracksToVertex.push_back(transientTrackBuilder->build(iMuon.globalTrack())); // try to fit these two tracks to a common vertex KalmanVertexFitter vertexFitter; CachingVertex<5> fittedVertex = vertexFitter.vertex(tracksToVertex); @@ -208,27 +205,26 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup //Track iso returns -1 when it fails to find the vertex containing the track (already checked in track selection, but might as well make sure) if (trackIso < 0) continue; - double ecalIso = getECALIsolation(iEvent, iSetup, transientTrackBuilder->build(*iTrack)); + double ecalIso = getECALIsolation(iEvent, iSetup, transientTrackBuilder->build(iTrack)); if (trackIso > maxTrackIso_ || ecalIso > maxEcalIso_) continue; //A good tag/probe pair has been selected, now check for control or signal regions - if (iMuon->charge() == iTrack->charge()) { + if (iMuon.charge() == iTrack.charge()) { sameSign = true; } //If not same sign CR, need to check standalone muons for signal regions double staMinDr2 = 1000; double staMinDEoverE = -10; - if (!staTracks->empty()) { - for (reco::TrackCollection::const_iterator staTrack = staTracks->begin(); staTrack != staTracks->end(); - ++staTrack) { - reco::TransientTrack track = transientTrackBuilder->build(*staTrack); + if (!staTracks.empty()) { + for (const auto& staTrack : staTracks) { + reco::TransientTrack track = transientTrackBuilder->build(staTrack); double dR2 = deltaR2(track.impactPointTSCP().momentum().eta(), track.impactPointTSCP().momentum().phi(), - (*iTrack).eta(), - (*iTrack).phi()); - double staDE = (std::sqrt(track.impactPointTSCP().momentum().mag2()) - (*iTrack).p()) / (*iTrack).p(); + iTrack.eta(), + iTrack.phi()); + double staDE = (std::sqrt(track.impactPointTSCP().momentum().mag2()) - iTrack.p()) / iTrack.p(); if (dR2 < staMinDr2) { staMinDr2 = dR2; } @@ -301,22 +297,22 @@ bool DisappearingMuonsSkimming::passTriggers(const edm::Event& iEvent, } double DisappearingMuonsSkimming::getTrackIsolation(const edm::Event& iEvent, - edm::Handle vtxHandle, - std::vector::const_iterator& iTrack) { + const reco::VertexCollection& vtxHandle, + const reco::Track& iTrack) { bool foundtrack = false; unsigned int vtxindex = -1; unsigned int trackindex = -1; double Isolation = 0; - for (unsigned int i = 0; i < vtxHandle->size(); i++) { - reco::VertexRef vtx(vtxHandle, i); + for (unsigned int i = 0; i < vtxHandle.size(); i++) { + reco::VertexRef vtx(&vtxHandle, i); if (!vtx->isValid()) { continue; } for (unsigned int j = 0; j < vtx->tracksSize(); j++) { - double dPt = fabs(vtx->trackRefAt(j)->pt() - iTrack->pt()) / iTrack->pt(); + double dPt = std::abs(vtx->trackRefAt(j)->pt() - iTrack.pt()) / iTrack.pt(); //Find the vertex track that is the same as the probe if (dPt < 0.001) { - double dR2 = deltaR2(vtx->trackRefAt(j)->eta(), vtx->trackRefAt(j)->phi(), iTrack->eta(), iTrack->phi()); + double dR2 = deltaR2(vtx->trackRefAt(j)->eta(), vtx->trackRefAt(j)->phi(), iTrack.eta(), iTrack.phi()); if (dR2 < 0.001 * 0.001) { vtxindex = i; trackindex = j; @@ -331,21 +327,21 @@ double DisappearingMuonsSkimming::getTrackIsolation(const edm::Event& iEvent, return -1; } - reco::VertexRef primaryVtx(vtxHandle, vtxindex); + reco::VertexRef primaryVtx(&vtxHandle, vtxindex); for (unsigned int i = 0; i < primaryVtx->tracksSize(); i++) { if (i == trackindex) continue; reco::TrackBaseRef secondarytrack = primaryVtx->trackRefAt(i); - if (deltaR2(iTrack->eta(), iTrack->phi(), secondarytrack->eta(), secondarytrack->phi()) > + if (deltaR2(iTrack.eta(), iTrack.phi(), secondarytrack->eta(), secondarytrack->phi()) > trackIsoConesize_ * trackIsoConesize_ || - deltaR2(iTrack->eta(), iTrack->phi(), secondarytrack->eta(), secondarytrack->phi()) < + deltaR2(iTrack.eta(), iTrack.phi(), secondarytrack->eta(), secondarytrack->phi()) < trackIsoInnerCone_ * trackIsoInnerCone_) continue; Isolation += secondarytrack->pt(); } - return Isolation / iTrack->pt(); + return Isolation / iTrack.pt(); } double DisappearingMuonsSkimming::getECALIsolation(const edm::Event& iEvent, From 104988601419ad271ba16201c4e24ddc3a352db4 Mon Sep 17 00:00:00 2001 From: mmusich Date: Tue, 28 Mar 2023 15:13:09 +0200 Subject: [PATCH 2/5] simplify / optimize DisappearingMuonsSkimming --- .../interface/DisappearingMuonsSkimming.h | 13 +- .../Skimming/src/DisappearingMuonsSkimming.cc | 132 +++++++++--------- 2 files changed, 75 insertions(+), 70 deletions(-) diff --git a/Configuration/Skimming/interface/DisappearingMuonsSkimming.h b/Configuration/Skimming/interface/DisappearingMuonsSkimming.h index 76247902f3143..b6fd8ae0bc4b4 100644 --- a/Configuration/Skimming/interface/DisappearingMuonsSkimming.h +++ b/Configuration/Skimming/interface/DisappearingMuonsSkimming.h @@ -49,13 +49,18 @@ class DisappearingMuonsSkimming : public edm::one::EDFilter<> { static void fillDescriptions(edm::ConfigurationDescriptions& descriptions); private: - void beginJob() override; bool filter(edm::Event&, const edm::EventSetup&) override; - void endJob() override; + bool passTriggers(const edm::Event& iEvent, - edm::EDGetToken m_trigResultsToken, + const edm::TriggerResults& results, std::vector m_muonPathsToPass); - double getTrackIsolation(const edm::Event&, const reco::VertexCollection& vtxHandle, const reco::Track& iTrack); + + bool findTrackInVertices(const reco::TrackRef& tkToMatch, + const reco::VertexCollection& vertices, + unsigned int& vtxIndex, + unsigned int& trackIndex); + + double getTrackIsolation(const reco::TrackRef& tkToMatch, const reco::VertexCollection& vertices); double getECALIsolation(const edm::Event&, const edm::EventSetup&, const reco::TransientTrack track); // ----------member data --------------------------- diff --git a/Configuration/Skimming/src/DisappearingMuonsSkimming.cc b/Configuration/Skimming/src/DisappearingMuonsSkimming.cc index a98336bed0878..3ec747f783610 100644 --- a/Configuration/Skimming/src/DisappearingMuonsSkimming.cc +++ b/Configuration/Skimming/src/DisappearingMuonsSkimming.cc @@ -16,6 +16,7 @@ // // // system include files +#include #include #include "Math/VectorUtil.h" @@ -100,12 +101,13 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup const auto& staTracks = iEvent.get(standaloneMuonToken_); const auto& vertices = iEvent.get(primaryVerticesToken_); const auto& recoMuons = iEvent.get(recoMuonToken_); - const auto& thePATTrackHandle = iEvent.get(trackCollectionToken_); + const auto& thePATTracks = iEvent.get(trackCollectionToken_); + const auto& triggerResults = iEvent.get(trigResultsToken_); // this wraps tracks with additional methods that are used in vertex-calculation const TransientTrackBuilder* transientTrackBuilder = &iSetup.getData(transientTrackToken_); - if (!passTriggers(iEvent, trigResultsToken_, muonPathsToPass_)) + if (!passTriggers(iEvent, triggerResults, muonPathsToPass_)) return false; int nMuonTrackCand = 0; @@ -123,7 +125,10 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup continue; //Looping over tracks for any good muon - for (const auto& iTrack : thePATTrackHandle) { + int indx(0); + for (const auto& iTrack : thePATTracks) { + reco::TrackRef trackRef = reco::TrackRef(&thePATTracks, indx); + indx++; if (!iTrack.quality(reco::Track::qualityByName("highPurity"))) continue; @@ -133,30 +138,16 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup continue; //Check if the track belongs to a primary vertex for isolation calculation - bool foundtrack = false; - GlobalPoint tkVtx; - for (unsigned int i = 0; i < vertices.size(); i++) { - reco::VertexRef vtx(&vertices, i); - if (!vtx->isValid()) { - continue; - } - for (unsigned int j = 0; j < vtx->tracksSize(); j++) { - double dPt = std::abs(vtx->trackRefAt(j)->pt() - iTrack.pt()) / iTrack.pt(); - //Find the vertex track that is the same as the probe - if (dPt < 0.001) { - double dR2 = deltaR2(vtx->trackRefAt(j)->eta(), vtx->trackRefAt(j)->phi(), iTrack.eta(), iTrack.phi()); - if (dR2 < 0.001 * 0.001) { - foundtrack = true; - GlobalPoint vert(vtx->x(), vtx->y(), vtx->z()); - tkVtx = vert; - break; - } - } - } - } + unsigned int vtxIndex; + unsigned int tkIndex; + bool foundtrack = this->findTrackInVertices(trackRef, vertices, vtxIndex, tkIndex); if (!foundtrack) continue; + + reco::VertexRef vtx(&vertices, vtxIndex); + GlobalPoint tkVtx = GlobalPoint(vtx->x(), vtx->y(), vtx->z()); + reco::TransientTrack tk = transientTrackBuilder->build(iTrack); TrajectoryStateClosestToPoint traj = tk.trajectoryStateClosestToPoint(tkVtx); double transDCA = traj.perigeeParameters().transverseImpactParameter(); @@ -167,7 +158,7 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup continue; // make a pair of TransientTracks to feed the vertexer std::vector tracksToVertex; - tracksToVertex.push_back(transientTrackBuilder->build(iTrack)); + tracksToVertex.push_back(tk); tracksToVertex.push_back(transientTrackBuilder->build(iMuon.globalTrack())); // try to fit these two tracks to a common vertex KalmanVertexFitter vertexFitter; @@ -201,7 +192,7 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup if (MuonTrackMass < minInvMass_ || MuonTrackMass > maxInvMass_) continue; - double trackIso = getTrackIsolation(iEvent, vertices, iTrack); + double trackIso = getTrackIsolation(trackRef, vertices); //Track iso returns -1 when it fails to find the vertex containing the track (already checked in track selection, but might as well make sure) if (trackIso < 0) continue; @@ -276,17 +267,15 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup } bool DisappearingMuonsSkimming::passTriggers(const edm::Event& iEvent, - edm::EDGetToken m_trigResultsToken, + const edm::TriggerResults& triggerResults, std::vector m_muonPathsToPass) { bool passTriggers = false; - edm::Handle triggerResults; - iEvent.getByToken(m_trigResultsToken, triggerResults); - const edm::TriggerNames& trigNames = iEvent.triggerNames(*triggerResults); + const edm::TriggerNames& trigNames = iEvent.triggerNames(triggerResults); for (size_t i = 0; i < trigNames.size(); ++i) { const std::string& name = trigNames.triggerName(i); for (auto& pathName : m_muonPathsToPass) { if ((name.find(pathName) != std::string::npos)) { - if (triggerResults->accept(i)) { + if (triggerResults.accept(i)) { passTriggers = true; break; } @@ -296,52 +285,69 @@ bool DisappearingMuonsSkimming::passTriggers(const edm::Event& iEvent, return passTriggers; } -double DisappearingMuonsSkimming::getTrackIsolation(const edm::Event& iEvent, - const reco::VertexCollection& vtxHandle, - const reco::Track& iTrack) { - bool foundtrack = false; - unsigned int vtxindex = -1; - unsigned int trackindex = -1; - double Isolation = 0; - for (unsigned int i = 0; i < vtxHandle.size(); i++) { - reco::VertexRef vtx(&vtxHandle, i); - if (!vtx->isValid()) { +bool DisappearingMuonsSkimming::findTrackInVertices(const reco::TrackRef& tkToMatch, + const reco::VertexCollection& vertices, + unsigned int& vtxIndex, + unsigned int& trackIndex) { + // initialize indices + vtxIndex = -1; + trackIndex = -1; + + bool foundtrack{false}; + for (const auto& vtx : vertices | boost::adaptors::indexed(0)) { + if (!vtx.value().isValid()) { continue; } - for (unsigned int j = 0; j < vtx->tracksSize(); j++) { - double dPt = std::abs(vtx->trackRefAt(j)->pt() - iTrack.pt()) / iTrack.pt(); - //Find the vertex track that is the same as the probe - if (dPt < 0.001) { - double dR2 = deltaR2(vtx->trackRefAt(j)->eta(), vtx->trackRefAt(j)->phi(), iTrack.eta(), iTrack.phi()); - if (dR2 < 0.001 * 0.001) { - vtxindex = i; - trackindex = j; - foundtrack = true; - break; - } + + std::vector thePVkeys; + for (auto tv = vtx.value().tracks_begin(); tv != vtx.value().tracks_end(); tv++) { + if (tv->isNonnull()) { + const reco::TrackRef tkToMatch = tv->castTo(); + thePVkeys.push_back(tkToMatch.key()); } } + + auto result = std::find(thePVkeys.begin(), thePVkeys.end(), tkToMatch.key()); + if (result != thePVkeys.end()) { + foundtrack = true; + trackIndex = std::distance(thePVkeys.begin(), result); + vtxIndex = vtx.index(); + } + if (foundtrack) + break; } + return foundtrack; +} + +double DisappearingMuonsSkimming::getTrackIsolation(const reco::TrackRef& tkToMatch, + const reco::VertexCollection& vertices) { + unsigned int vtxindex; + unsigned int trackIndex; + bool foundtrack = this->findTrackInVertices(tkToMatch, vertices, vtxindex, trackIndex); + + LogDebug("DisappearingMuonsSkimming") << "getTrackIsolation vtx Index: " << vtxindex + << " track Index: " << trackIndex; if (!foundtrack) { - return -1; + return -1.; } - reco::VertexRef primaryVtx(&vtxHandle, vtxindex); + reco::VertexRef primaryVtx(&vertices, vtxindex); + double Isolation = 0; for (unsigned int i = 0; i < primaryVtx->tracksSize(); i++) { - if (i == trackindex) + if (i == trackIndex) continue; reco::TrackBaseRef secondarytrack = primaryVtx->trackRefAt(i); - if (deltaR2(iTrack.eta(), iTrack.phi(), secondarytrack->eta(), secondarytrack->phi()) > + if (deltaR2(tkToMatch.get()->eta(), tkToMatch.get()->phi(), secondarytrack->eta(), secondarytrack->phi()) > trackIsoConesize_ * trackIsoConesize_ || - deltaR2(iTrack.eta(), iTrack.phi(), secondarytrack->eta(), secondarytrack->phi()) < + deltaR2(tkToMatch.get()->eta(), tkToMatch.get()->phi(), secondarytrack->eta(), secondarytrack->phi()) < trackIsoInnerCone_ * trackIsoInnerCone_) continue; Isolation += secondarytrack->pt(); } - return Isolation / iTrack.pt(); + return Isolation / tkToMatch.get()->pt(); } double DisappearingMuonsSkimming::getECALIsolation(const edm::Event& iEvent, @@ -367,7 +373,7 @@ double DisappearingMuonsSkimming::getECALIsolation(const edm::Event& iEvent, TrajectoryStateClosestToPoint traj = track.trajectoryStateClosestToPoint(hitPos); math::XYZVector idPositionRoot(hitPos.x(), hitPos.y(), hitPos.z()); math::XYZVector trajRoot(traj.position().x(), traj.position().y(), traj.position().z()); - if (ROOT::Math::VectorUtil::DeltaR(idPositionRoot, trajRoot) < ecalIsoConesize_ && (*hit).energy() > minEcalHitE_) { + if (deltaR2(idPositionRoot, trajRoot) < ecalIsoConesize_ * ecalIsoConesize_ && (*hit).energy() > minEcalHitE_) { eDR += (*hit).energy(); } } @@ -380,7 +386,7 @@ double DisappearingMuonsSkimming::getECALIsolation(const edm::Event& iEvent, TrajectoryStateClosestToPoint traj = track.trajectoryStateClosestToPoint(hitPos); math::XYZVector idPositionRoot(hitPos.x(), hitPos.y(), hitPos.z()); math::XYZVector trajRoot(traj.position().x(), traj.position().y(), traj.position().z()); - if (ROOT::Math::VectorUtil::DeltaR(idPositionRoot, trajRoot) < ecalIsoConesize_ && (*hit).energy() > minEcalHitE_) { + if (deltaR2(idPositionRoot, trajRoot) < ecalIsoConesize_ * ecalIsoConesize_ && (*hit).energy() > minEcalHitE_) { eDR += (*hit).energy(); } } @@ -430,11 +436,5 @@ void DisappearingMuonsSkimming::fillDescriptions(edm::ConfigurationDescriptions& descriptions.addWithDefaultLabel(desc); } -// ------------ method called once each job just before starting event loop ------------ -void DisappearingMuonsSkimming::beginJob() {} - -// ------------ method called once each job just after ending the event loop ------------ -void DisappearingMuonsSkimming::endJob() {} - //define this as a plug-in DEFINE_FWK_MODULE(DisappearingMuonsSkimming); From 4cf4254f3614758bf1d689c838529a95c69af490 Mon Sep 17 00:00:00 2001 From: mmusich Date: Wed, 29 Mar 2023 17:07:20 +0200 Subject: [PATCH 3/5] add testing configuration for disappearing muons --- .../Skimming/test/test_DisMuon_cfg.py | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Configuration/Skimming/test/test_DisMuon_cfg.py diff --git a/Configuration/Skimming/test/test_DisMuon_cfg.py b/Configuration/Skimming/test/test_DisMuon_cfg.py new file mode 100644 index 0000000000000..d9d7d9dcc1ffa --- /dev/null +++ b/Configuration/Skimming/test/test_DisMuon_cfg.py @@ -0,0 +1,59 @@ +import FWCore.ParameterSet.Config as cms + +from Configuration.Eras.Era_Run3_cff import Run3 +process = cms.Process("testSkimming", Run3) + +# Message logger service + +process.load("FWCore.MessageLogger.MessageLogger_cfi") +process.MessageLogger.cerr.enable = False +process.MessageLogger.DisappearingMuonsSkimming=dict() +process.MessageLogger.cout = cms.untracked.PSet( + enable = cms.untracked.bool(True), + threshold = cms.untracked.string("INFO"), + default = cms.untracked.PSet(limit = cms.untracked.int32(0)), + FwkReport = cms.untracked.PSet(limit = cms.untracked.int32(-1), + reportEvery = cms.untracked.int32(100) + ), + DMRChecker = cms.untracked.PSet( limit = cms.untracked.int32(-1)), + DisappearingMuonsSkimming = cms.untracked.PSet( limit = cms.untracked.int32(-1)) + ) + +readFiles = cms.untracked.vstring( + '/store/relval/CMSSW_13_0_0_pre3/RelValZMM_14/GEN-SIM-RECO/PU_130X_mcRun3_2022_realistic_v2-v1/00000/04f597b5-14d3-44c8-851e-c5f46657d92a.root', +) + +process.source = cms.Source("PoolSource",fileNames = readFiles) +process.maxEvents = cms.untracked.PSet(input = cms.untracked.int32((800))) + +process.load('Configuration.StandardSequences.Services_cff') +process.load('Configuration.EventContent.EventContent_cff') +process.load('Configuration.StandardSequences.GeometryRecoDB_cff') +process.load('Configuration.StandardSequences.MagneticField_cff') +process.load('Configuration.StandardSequences.FrontierConditions_GlobalTag_cff') +process.load("TrackingTools.TransientTrack.TransientTrackBuilder_cfi") + +from Configuration.AlCa.GlobalTag import GlobalTag +process.GlobalTag = GlobalTag(process.GlobalTag, '130X_mcRun3_2022_realistic_v2', '') + +process.load("Configuration.Skimming.PDWG_EXODisappMuon_cff") +# Additional output definition +process.SKIMStreamEXODisappMuon = cms.OutputModule("PoolOutputModule", + SelectEvents = cms.untracked.PSet( + SelectEvents = cms.vstring('EXODisappMuonPath') + ), + dataset = cms.untracked.PSet( + dataTier = cms.untracked.string('AODSIM'), + filterName = cms.untracked.string('EXODisappMuon') + #filterName = cms.untracked.string('') + ), + eventAutoFlushCompressedSize = cms.untracked.int32(5242880), + fileName = cms.untracked.string('EXODisappMuon.root'), + outputCommands = process.EXODisappMuonSkimContent.outputCommands +) + +process.EXODisappMuonPath = cms.Path(process.EXODisappMuonSkimSequence) +process.SKIMStreamEXODisappMuonOutPath = cms.EndPath(process.SKIMStreamEXODisappMuon) + +# Schedule definition +process.schedule = cms.Schedule(process.EXODisappMuonPath,process.SKIMStreamEXODisappMuonOutPath) From 1e43aff44fddee0af700398c50cc3de651203923 Mon Sep 17 00:00:00 2001 From: mmusich Date: Fri, 31 Mar 2023 18:07:49 +0200 Subject: [PATCH 4/5] further optimization of DisappearingMuonsSkimming::findTrackInVertices --- .../Skimming/src/DisappearingMuonsSkimming.cc | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Configuration/Skimming/src/DisappearingMuonsSkimming.cc b/Configuration/Skimming/src/DisappearingMuonsSkimming.cc index 3ec747f783610..97d17f57c39c1 100644 --- a/Configuration/Skimming/src/DisappearingMuonsSkimming.cc +++ b/Configuration/Skimming/src/DisappearingMuonsSkimming.cc @@ -16,7 +16,6 @@ // // // system include files -#include #include #include "Math/VectorUtil.h" @@ -294,27 +293,31 @@ bool DisappearingMuonsSkimming::findTrackInVertices(const reco::TrackRef& tkToMa trackIndex = -1; bool foundtrack{false}; - for (const auto& vtx : vertices | boost::adaptors::indexed(0)) { - if (!vtx.value().isValid()) { + unsigned int idx = 0; + for (const auto& vtx : vertices) { + if (!vtx.isValid()) { + idx++; continue; } + const auto& thePVtracks{vtx.tracks()}; std::vector thePVkeys; - for (auto tv = vtx.value().tracks_begin(); tv != vtx.value().tracks_end(); tv++) { - if (tv->isNonnull()) { - const reco::TrackRef tkToMatch = tv->castTo(); - thePVkeys.push_back(tkToMatch.key()); - } + thePVkeys.reserve(thePVtracks.size()); + for (const auto& tv : thePVtracks) { + thePVkeys.push_back(tv.key()); } - auto result = std::find(thePVkeys.begin(), thePVkeys.end(), tkToMatch.key()); + auto result = std::find_if( + thePVkeys.begin(), thePVkeys.end(), [tkToMatch](const auto& tkRef) { return tkRef == tkToMatch.key(); }); if (result != thePVkeys.end()) { foundtrack = true; trackIndex = std::distance(thePVkeys.begin(), result); - vtxIndex = vtx.index(); + vtxIndex = idx; } if (foundtrack) break; + + idx++; } return foundtrack; } From 559f2c274ddfeda22c1074701c485b738833ade3 Mon Sep 17 00:00:00 2001 From: mmusich Date: Thu, 6 Apr 2023 14:17:29 +0200 Subject: [PATCH 5/5] address pedantic comment from release management review --- .../interface/DisappearingMuonsSkimming.h | 4 ++-- .../Skimming/src/DisappearingMuonsSkimming.cc | 18 ++++-------------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/Configuration/Skimming/interface/DisappearingMuonsSkimming.h b/Configuration/Skimming/interface/DisappearingMuonsSkimming.h index b6fd8ae0bc4b4..2b37c48b8f392 100644 --- a/Configuration/Skimming/interface/DisappearingMuonsSkimming.h +++ b/Configuration/Skimming/interface/DisappearingMuonsSkimming.h @@ -53,7 +53,7 @@ class DisappearingMuonsSkimming : public edm::one::EDFilter<> { bool passTriggers(const edm::Event& iEvent, const edm::TriggerResults& results, - std::vector m_muonPathsToPass); + const std::vector& m_muonPathsToPass); bool findTrackInVertices(const reco::TrackRef& tkToMatch, const reco::VertexCollection& vertices, @@ -61,7 +61,7 @@ class DisappearingMuonsSkimming : public edm::one::EDFilter<> { unsigned int& trackIndex); double getTrackIsolation(const reco::TrackRef& tkToMatch, const reco::VertexCollection& vertices); - double getECALIsolation(const edm::Event&, const edm::EventSetup&, const reco::TransientTrack track); + double getECALIsolation(const edm::Event&, const edm::EventSetup&, const reco::TransientTrack& track); // ----------member data --------------------------- const edm::EDGetTokenT recoMuonToken_; diff --git a/Configuration/Skimming/src/DisappearingMuonsSkimming.cc b/Configuration/Skimming/src/DisappearingMuonsSkimming.cc index 97d17f57c39c1..b30cf8a1736ba 100644 --- a/Configuration/Skimming/src/DisappearingMuonsSkimming.cc +++ b/Configuration/Skimming/src/DisappearingMuonsSkimming.cc @@ -21,27 +21,17 @@ // user include files #include "Configuration/Skimming/interface/DisappearingMuonsSkimming.h" -#include "DataFormats/Common/interface/TriggerResults.h" -#include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h" #include "DataFormats/Math/interface/deltaR.h" -#include "DataFormats/PatCandidates/interface/Muon.h" -#include "DataFormats/TrackReco/interface/Track.h" -#include "DataFormats/VertexReco/interface/Vertex.h" #include "FWCore/Common/interface/TriggerNames.h" +#include "FWCore/Framework/interface/MakerMacros.h" #include "FWCore/Framework/interface/Event.h" #include "FWCore/Framework/interface/Frameworkfwd.h" -#include "FWCore/Framework/interface/MakerMacros.h" -#include "FWCore/Framework/interface/one/EDFilter.h" -#include "FWCore/ParameterSet/interface/ParameterSet.h" -#include "FWCore/Utilities/interface/EDGetToken.h" +#include "FWCore/MessageLogger/interface/MessageLogger.h" #include "FWCore/Utilities/interface/InputTag.h" #include "Geometry/CaloGeometry/interface/CaloGeometry.h" #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h" #include "Geometry/Records/interface/CaloGeometryRecord.h" #include "RecoVertex/KalmanVertexFit/interface/KalmanVertexFitter.h" -#include "TrackingTools/Records/interface/TransientTrackRecord.h" -#include "TrackingTools/TransientTrack/interface/TransientTrack.h" -#include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h" DisappearingMuonsSkimming::DisappearingMuonsSkimming(const edm::ParameterSet& iConfig) : recoMuonToken_(consumes(iConfig.getParameter("recoMuons"))), @@ -267,7 +257,7 @@ bool DisappearingMuonsSkimming::filter(edm::Event& iEvent, const edm::EventSetup bool DisappearingMuonsSkimming::passTriggers(const edm::Event& iEvent, const edm::TriggerResults& triggerResults, - std::vector m_muonPathsToPass) { + const std::vector& m_muonPathsToPass) { bool passTriggers = false; const edm::TriggerNames& trigNames = iEvent.triggerNames(triggerResults); for (size_t i = 0; i < trigNames.size(); ++i) { @@ -355,7 +345,7 @@ double DisappearingMuonsSkimming::getTrackIsolation(const reco::TrackRef& tkToMa double DisappearingMuonsSkimming::getECALIsolation(const edm::Event& iEvent, const edm::EventSetup& iSetup, - const reco::TransientTrack track) { + const reco::TransientTrack& track) { edm::Handle rechitsEE; iEvent.getByToken(reducedEndcapRecHitCollectionToken_, rechitsEE);