Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup muon validation #33610

Merged
merged 5 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SimMuon/MCTruth/interface/MuonAssociatorByHitsHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class MuonAssociatorByHitsHelper {

const bool includeZeroHitMuons;
const bool acceptOneStubMatchings;
const bool rejectBadGlobal;
bool UseTracker;
bool UseMuon;
const bool AbsoluteNumberOfHits_track;
Expand Down
56 changes: 41 additions & 15 deletions SimMuon/MCTruth/plugins/MuonAssociatorEDProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@
MuonAssociatorEDProducer::MuonAssociatorEDProducer(const edm::ParameterSet &parset)
: tracksTag(parset.getParameter<edm::InputTag>("tracksTag")),
tpTag(parset.getParameter<edm::InputTag>("tpTag")),
tpRefVector(parset.getParameter<bool>("tpRefVector")),
ignoreMissingTrackCollection(parset.getUntrackedParameter<bool>("ignoreMissingTrackCollection", false)),
parset_(parset) {
edm::LogVerbatim("MuonAssociatorEDProducer") << "constructing MuonAssociatorEDProducer";
produces<reco::RecoToSimCollection>();
produces<reco::SimToRecoCollection>();
tpToken_ = consumes<TrackingParticleCollection>(tpTag);
if (tpRefVector)
tpRefVectorToken_ = consumes<TrackingParticleRefVector>(tpTag);
else
tpToken_ = consumes<TrackingParticleCollection>(tpTag);
tracksToken_ = consumes<edm::View<reco::Track>>(tracksTag);

/// Perform some sanity checks of the configuration
LogTrace("MuonAssociatorEDProducer") << "constructing MuonAssociatorByHits" << parset_.dump();
edm::LogVerbatim("MuonAssociatorEDProducer") << "\n MuonAssociatorByHits will associate reco::Tracks with "
<< tracksTag << "\n\t\t and TrackingParticles with " << tpTag;
const std::string recoTracksLabel = tracksTag.label();
const std::string recoTracksInstance = tracksTag.instance();

// check and fix inconsistent input settings
// tracks with hits only on muon detectors
if (recoTracksLabel == "standAloneMuons" || recoTracksLabel == "standAloneSETMuons" ||
recoTracksLabel == "cosmicMuons" || recoTracksLabel == "hltL2Muons") {
if (recoTracksLabel == "seedsOfSTAmuons" || recoTracksLabel == "standAloneMuons" ||
recoTracksLabel == "refittedStandAloneMuons" || recoTracksLabel == "seedsOfDisplacedSTAmuons" ||
recoTracksLabel == "displacedStandAloneMuons" || recoTracksLabel == "cosmicMuons" ||
recoTracksLabel == "cosmicMuons1Leg" || recoTracksLabel == "hltL2Muons") {
if (parset_.getParameter<bool>("UseTracker")) {
edm::LogWarning("MuonAssociatorEDProducer")
<< "\n*** WARNING : inconsistent input tracksTag = " << tracksTag << "\n with UseTracker = true"
Expand All @@ -40,9 +45,11 @@ MuonAssociatorEDProducer::MuonAssociatorEDProducer(const edm::ParameterSet &pars
}
}
// tracks with hits only on tracker
if (recoTracksLabel == "generalTracks" || recoTracksLabel == "ctfWithMaterialTracksP5LHCNavigation" ||
recoTracksLabel == "hltL3TkTracksFromL2" ||
(recoTracksLabel == "hltL3Muons" && recoTracksInstance == "L2Seeded")) {
if (recoTracksLabel == "generalTracks" || recoTracksLabel == "probeTracks" || recoTracksLabel == "displacedTracks" ||
recoTracksLabel == "extractGemMuons" || recoTracksLabel == "extractMe0Muons" ||
recoTracksLabel == "ctfWithMaterialTracksP5LHCNavigation" || recoTracksLabel == "ctfWithMaterialTracksP5" ||
recoTracksLabel == "hltIterL3OIMuonTrackSelectionHighPurity" || recoTracksLabel == "hltIterL3MuonMerged" ||
recoTracksLabel == "hltIterL3MuonAndMuonFromL1Merged") {
if (parset_.getParameter<bool>("UseMuon")) {
edm::LogWarning("MuonAssociatorEDProducer")
<< "\n*** WARNING : inconsistent input tracksTag = " << tracksTag << "\n with UseMuon = true"
Expand Down Expand Up @@ -71,10 +78,27 @@ void MuonAssociatorEDProducer::endJob() {}
void MuonAssociatorEDProducer::produce(edm::Event &event, const edm::EventSetup &setup) {
using namespace edm;

TrackingParticleRefVector tmpTP;
const TrackingParticleRefVector *tmpTPptr = nullptr;
Handle<TrackingParticleCollection> TPCollection;
Handle<TrackingParticleRefVector> TPCollectionRefVector;

if (tpRefVector) {
event.getByToken(tpRefVectorToken_, TPCollectionRefVector);
tmpTPptr = TPCollectionRefVector.product();
//
tmpTP = *tmpTPptr;
} else {
event.getByToken(tpToken_, TPCollection);
size_t nTP = TPCollection->size();
for (size_t i = 0; i < nTP; ++i) {
tmpTP.push_back(TrackingParticleRef(TPCollection, i));
}
tmpTPptr = &tmpTP;
}

LogTrace("MuonAssociatorEDProducer") << "getting TrackingParticle collection - " << tpTag;
event.getByToken(tpToken_, TPCollection);
LogTrace("MuonAssociatorEDProducer") << "\t... size = " << TPCollection->size();
LogTrace("MuonAssociatorEDProducer") << "\t... size = " << tmpTPptr->size();

Handle<edm::View<reco::Track>> trackCollection;
LogTrace("MuonAssociatorEDProducer") << "getting reco::Track collection - " << tracksTag;
Expand All @@ -84,6 +108,10 @@ void MuonAssociatorEDProducer::produce(edm::Event &event, const edm::EventSetup
else
LogTrace("MuonAssociatorEDProducer") << "\t... NOT FOUND.";

edm::RefToBaseVector<reco::Track> tmpT;
for (size_t i = 0; i < trackCollection->size(); ++i)
tmpT.push_back(trackCollection->refAt(i));

std::unique_ptr<reco::RecoToSimCollection> rts;
std::unique_ptr<reco::SimToRecoCollection> str;

Expand All @@ -99,20 +127,18 @@ void MuonAssociatorEDProducer::produce(edm::Event &event, const edm::EventSetup
<< " Track collection : " << tracksTag.label() << ":" << tracksTag.instance()
<< " (size = " << trackCollection->size() << ") \n"
<< " TrackingParticle collection : " << tpTag.label() << ":" << tpTag.instance()
<< " (size = " << TPCollection->size() << ")";
<< " (size = " << tmpTPptr->size() << ")";

reco::RecoToSimCollection recSimColl =
associatorByHits->associateRecoToSim(trackCollection, TPCollection, &event, &setup);
reco::RecoToSimCollection recSimColl = associatorByHits->associateRecoToSim(tmpT, tmpTP, &event, &setup);

edm::LogVerbatim("MuonAssociatorEDProducer")
<< "\n >>> SimToReco association <<< \n"
<< " TrackingParticle collection : " << tpTag.label() << ":" << tpTag.instance()
<< " (size = " << TPCollection->size() << ") \n"
<< " (size = " << tmpTPptr->size() << ") \n"
<< " Track collection : " << tracksTag.label() << ":" << tracksTag.instance()
<< " (size = " << trackCollection->size() << ")";

reco::SimToRecoCollection simRecColl =
associatorByHits->associateSimToReco(trackCollection, TPCollection, &event, &setup);
reco::SimToRecoCollection simRecColl = associatorByHits->associateSimToReco(tmpT, tmpTP, &event, &setup);

rts = std::make_unique<reco::RecoToSimCollection>(recSimColl);
str = std::make_unique<reco::SimToRecoCollection>(simRecColl);
Expand Down
2 changes: 2 additions & 0 deletions SimMuon/MCTruth/plugins/MuonAssociatorEDProducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class MuonAssociatorEDProducer : public edm::stream::EDProducer<> {
edm::InputTag tracksTag;
edm::InputTag tpTag;
edm::EDGetTokenT<TrackingParticleCollection> tpToken_;
edm::EDGetTokenT<TrackingParticleRefVector> tpRefVectorToken_;
edm::EDGetTokenT<edm::View<reco::Track>> tracksToken_;

bool tpRefVector;
bool ignoreMissingTrackCollection;
edm::ParameterSet parset_;
MuonAssociatorByHits *associatorByHits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ MuonToTrackingParticleAssociatorEDProducer::MuonToTrackingParticleAssociatorEDPr
DTHitAssociator dttruth(iConfig, consumesCollector());
CSCHitAssociator cscruth(iConfig, consumesCollector());

edm::LogVerbatim("MuonToTrackingParticleAssociatorEDProducer")
<< "\n constructing MuonToTrackingParticleAssociatorEDProducer"
<< "\n";

if (iConfig.getUntrackedParameter<bool>("dumpInputCollections")) {
diagnostics_ = std::make_unique<InputDumper>(iConfig, consumesCollector());
}
Expand Down
34 changes: 18 additions & 16 deletions SimMuon/MCTruth/plugins/MuonTrackProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,6 @@ void MuonTrackProducer::produce(edm::StreamID, edm::Event &iEvent, const edm::Ev
else
continue;
} else if (trackType == "recomuonTrack") {
if (muon->isGlobalMuon())
trackref = muon->globalTrack();
else if (muon->isTrackerMuon()) {
trackref = muon->innerTrack();
addMatchedMuonSegments = true;
} else if (muon->isStandAloneMuon())
trackref = muon->outerTrack();
else if (muon->isRPCMuon())
trackref = muon->innerTrack();
else if (muon->isGEMMuon())
trackref = muon->innerTrack();
else if (muon->isME0Muon())
trackref = muon->innerTrack();
else
trackref = muon->muonBestTrack();

if (muon->muonBestTrackType() != muon->tunePMuonBestTrackType())
edm::LogVerbatim("MuonTrackProducer") << "\n *** PF != TuneP *** \n" << std::endl;

Expand Down Expand Up @@ -290,6 +274,24 @@ void MuonTrackProducer::produce(edm::StreamID, edm::Event &iEvent, const edm::Ev
<< ", phi = " << muon->dytTrack()->phi()
<< ", N mu hits = " << muon->dytTrack()->hitPattern().numberOfValidMuonHits()
<< ", N trk hits = " << muon->dytTrack()->hitPattern().numberOfValidTrackerHits() << std::endl;

if (muon->isGlobalMuon() && muon->globalTrack()->hitPattern().numberOfValidMuonHits() > 0)
trackref = muon->globalTrack();
else if (muon->isTrackerMuon()) {
trackref = muon->innerTrack();
addMatchedMuonSegments = true;
} else if (muon->isPFMuon())
trackref = muon->muonBestTrack();
else if (muon->isStandAloneMuon())
trackref = muon->outerTrack();
else if (muon->isRPCMuon())
trackref = muon->innerTrack();
else if (muon->isGEMMuon())
trackref = muon->innerTrack();
else if (muon->isME0Muon())
trackref = muon->innerTrack();
else
trackref = muon->muonBestTrack();
}

edm::LogVerbatim("MuonTrackProducer") << "\t *** Selected *** ";
Expand Down
86 changes: 86 additions & 0 deletions SimMuon/MCTruth/plugins/TrackingParticleRefMuonProducer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/PluginManager/interface/ModuleDef.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "DataFormats/Common/interface/EDProductfwd.h"
#include "SimDataFormats/TrackingAnalysis/interface/TrackingParticle.h"
#include "SimDataFormats/TrackingAnalysis/interface/TrackingParticleFwd.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

class TrackingParticleRefMuonProducer : public edm::stream::EDProducer<> {
public:
TrackingParticleRefMuonProducer(const edm::ParameterSet &iConfig);

void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;

private:
edm::EDGetTokenT<TrackingParticleCollection> tpToken_;
std::string skim_;
double ptmin_;
double pmin_;
};

TrackingParticleRefMuonProducer::TrackingParticleRefMuonProducer(const edm::ParameterSet &iConfig)
: tpToken_(consumes<TrackingParticleCollection>(iConfig.getParameter<edm::InputTag>("src"))),
skim_(iConfig.getParameter<std::string>("skim")),
ptmin_(iConfig.getParameter<double>("ptmin")),
pmin_(iConfig.getParameter<double>("pmin")) {
edm::LogVerbatim("TrackingParticleRefMuonProducer")
<< "\n constructing TrackingParticleRefMuonProducer: skim = " << skim_;
if (skim_ == "mu")
edm::LogVerbatim("TrackingParticleRefMuonProducer") << "\t ptmin = " << ptmin_ << ", pmin = " << pmin_ << "\n";
else if (skim_ == "track")
edm::LogVerbatim("TrackingParticleRefMuonProducer") << "\t ptmin = " << ptmin_ << "\n";
else if (skim_ == "pf")
edm::LogVerbatim("TrackingParticleRefMuonProducer") << "\t ptmin = " << ptmin_ << ", pmin = " << pmin_ << "\n";
else
edm::LogError("TrackingParticleRefMuonProducer") << "\t undefined skim = " << skim_ << "\n";

produces<TrackingParticleRefVector>();
}

void TrackingParticleRefMuonProducer::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
edm::Handle<TrackingParticleCollection> tpH;
iEvent.getByToken(tpToken_, tpH);

auto tpskim = std::make_unique<TrackingParticleRefVector>();

if (skim_ == "mu") {
for (size_t i = 0, end = tpH->size(); i < end; ++i) {
auto tp = TrackingParticleRef(tpH, i);

// test if the TP is a muon with pt and p above minimum thresholds
bool isMu = (std::abs(tp->pdgId()) == 13);
bool ptpOk = (tp->pt() > ptmin_) && (tp->p() > pmin_);
if (isMu && ptpOk)
tpskim->push_back(tp);
else {
// test if the TP has muon hits
int n_muon_hits = tp->numberOfHits() - tp->numberOfTrackerHits();
if (n_muon_hits > 0)
tpskim->push_back(tp);
}
}
} else if (skim_ == "track") {
for (size_t i = 0, end = tpH->size(); i < end; ++i) {
auto tp = TrackingParticleRef(tpH, i);

// test if the pt is above a minimum cut
if (tp->pt() > ptmin_)
tpskim->push_back(tp);
}
} else if (skim_ == "pf") {
for (size_t i = 0, end = tpH->size(); i < end; ++i) {
auto tp = TrackingParticleRef(tpH, i);

// test if p and pt are above minimum cuts
if ((tp->pt() > ptmin_) && (tp->p() > pmin_))
tpskim->push_back(tp);
}
}

iEvent.put(std::move(tpskim));
}

DEFINE_FWK_MODULE(TrackingParticleRefMuonProducer);
24 changes: 5 additions & 19 deletions SimMuon/MCTruth/python/MuonAssociatorByHits_cfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
# include invalid muon hits
includeZeroHitMuons = cms.bool(True),
#
# accept to match only tracker/muon stub of globalMuons
# accept mismatched association of tracker/muon stub of global hit patterns
acceptOneStubMatchings = cms.bool(False),
# reject bad global muons made of only tracker hits
rejectBadGlobal = cms.bool(True),
#
# switches to be set according to the input Track collection
UseTracker = cms.bool(True),
Expand Down Expand Up @@ -130,27 +132,11 @@
# input collections
#
# ... reco::Track collection
# tracksTag = cms.InputTag("standAloneMuons"),
# tracksTag = cms.InputTag("standAloneMuons","UpdatedAtVtx"),
# tracksTag = cms.InputTag("standAloneSETMuons"),
# tracksTag = cms.InputTag("standAloneSETMuons","UpdatedAtVtx"),
# tracksTag = cms.InputTag("cosmicMuons"),
tracksTag = cms.InputTag("globalMuons"),
# tracksTag = cms.InputTag("tevMuons","firstHit"),
# tracksTag = cms.InputTag("tevMuons","picky"),
# tracksTag = cms.InputTag("globalSETMuons"),
# tracksTag = cms.InputTag("globalCosmicMuons"),
# tracksTag = cms.InputTag("generalTracks"),
# tracksTag = cms.InputTag("ctfWithMaterialTracksP5LHCNavigation"),
# tracksTag = cms.InputTag("hltL2Muons"),
# tracksTag = cms.InputTag("hltL2Muons","UpdatedAtVtx"),
# tracksTag = cms.InputTag("hltL3Muons")
# tracksTag = cms.InputTag("hltL3Muons","L2Seeded")
# tracksTag = cms.InputTag("hltL3TkTracksFromL2")
#
ignoreMissingTrackCollection = cms.untracked.bool(False),
# ... TrackingParticle collection
tpTag = cms.InputTag("mix","MergedTrackTruth"),
ignoreMissingTrackCollection = cms.untracked.bool(False),
tpRefVector = cms.bool(False)
)

premix_stage2.toModify(muonAssociatorByHits, tpTag = "mixData:MergedTrackTruth")
14 changes: 14 additions & 0 deletions SimMuon/MCTruth/python/trackingParticleMuon_cfi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import FWCore.ParameterSet.Config as cms

trackingParticleMuon = cms.EDProducer("TrackingParticleRefMuonProducer",
mightGet = cms.optional.untracked.vstring,
src = cms.InputTag("mix","MergedTrackTruth"),
skim = cms.string('mu'),
# skim = cms.string('track'),
# skim = cms.string('pf'),
ptmin = cms.double(0.5),
pmin = cms.double(2.5)
)

from Configuration.ProcessModifiers.premix_stage2_cff import premix_stage2
premix_stage2.toModify(trackingParticleMuon, src = "mixData:MergedTrackTruth")
Loading