diff --git a/RecoTauTag/RecoTau/interface/DeepTauBase.h b/RecoTauTag/RecoTau/interface/DeepTauBase.h index be6c22690aec3..b29113bb6bc9d 100644 --- a/RecoTauTag/RecoTau/interface/DeepTauBase.h +++ b/RecoTauTag/RecoTau/interface/DeepTauBase.h @@ -21,7 +21,6 @@ #include "DataFormats/PatCandidates/interface/PATTauDiscriminator.h" #include "CommonTools/Utils/interface/StringObjectFunction.h" #include "RecoTauTag/RecoTau/interface/PFRecoTauClusterVariables.h" -#include "RecoTauTag/RecoTau/interface/MuonHitMatch.h" #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h" #include "FWCore/ParameterSet/interface/ParameterSetDescription.h" #include diff --git a/RecoTauTag/RecoTau/interface/MuonHitMatch.h b/RecoTauTag/RecoTau/interface/MuonHitMatch.h deleted file mode 100644 index c621dc952299a..0000000000000 --- a/RecoTauTag/RecoTau/interface/MuonHitMatch.h +++ /dev/null @@ -1,40 +0,0 @@ -/*! Match hits in the muon system. -*/ - -#pragma once - -#include "DataFormats/PatCandidates/interface/Muon.h" - -namespace tau_analysis { - -namespace MuonSubdetId { -enum { DT = 1, CSC = 2, RPC = 3, GEM = 4, ME0 = 5 }; -} - -struct MuonHitMatch { - static constexpr size_t n_muon_stations = 4; - static constexpr int first_station_id = 1; - static constexpr int last_station_id = first_station_id + n_muon_stations - 1; - using CountArray = std::array; - using CountMap = std::map; - - static const std::vector& ConsideredSubdets(); - static const std::string& SubdetName(int subdet); - - static size_t GetStationIndex(int station, bool throw_exception); - static void CountMatches(const pat::Muon& muon, CountMap& n_matches); - static void CountHits(const pat::Muon& muon, CountMap& n_hits); - - MuonHitMatch(const pat::Muon& muon); - - unsigned CountMuonStationsWithMatches(int first_station, int last_station) const; - unsigned CountMuonStationsWithHits(int first_station, int last_station) const; - - unsigned NMatches(int subdet, int station) const; - unsigned NHits(int subdet, int station) const; - -private: - CountMap n_matches, n_hits; -}; - -} // namespace tau_analysis diff --git a/RecoTauTag/RecoTau/plugins/DeepTauId.cc b/RecoTauTag/RecoTau/plugins/DeepTauId.cc index 0ca73f486251f..34a36a9f641fb 100644 --- a/RecoTauTag/RecoTau/plugins/DeepTauId.cc +++ b/RecoTauTag/RecoTau/plugins/DeepTauId.cc @@ -306,51 +306,76 @@ struct MuonHitMatchV1 { } }; + + struct MuonHitMatchV2 { - static constexpr int n_muon_stations = 4; - std::map> n_matches, n_hits; - unsigned n_muons{0}; - const pat::Muon* best_matched_muon{nullptr}; - double deltaR2_best_match{-1}; + static constexpr size_t n_muon_stations = 4; + static constexpr int first_station_id = 1; + static constexpr int last_station_id = first_station_id + n_muon_stations - 1; + using CountArray = std::array; + using CountMap = std::map; - MuonHitMatchV2() + const std::vector& ConsideredSubdets() { - n_matches[MuonSubdetId::DT].assign(n_muon_stations, 0); - n_matches[MuonSubdetId::CSC].assign(n_muon_stations, 0); - n_matches[MuonSubdetId::RPC].assign(n_muon_stations, 0); - n_hits[MuonSubdetId::DT].assign(n_muon_stations, 0); - n_hits[MuonSubdetId::CSC].assign(n_muon_stations, 0); - n_hits[MuonSubdetId::RPC].assign(n_muon_stations, 0); + static const std::vector subdets = { MuonSubdetId::DT, MuonSubdetId::CSC, MuonSubdetId::RPC }; + return subdets; } - void addMatchedMuon(const pat::Muon& muon, const pat::Tau& tau) + const std::string& SubdetName(int subdet) { - static constexpr int n_stations = 4; + static const std::map subdet_names = { + { MuonSubdetId::DT, "DT" }, { MuonSubdetId::CSC, "CSC" }, { MuonSubdetId::RPC, "RPC" } + }; + if(!subdet_names.count(subdet)) + throw cms::Exception("MuonHitMatch") << "Subdet name for subdet id " << subdet << " not found."; + return subdet_names.at(subdet); + } - ++n_muons; - const double dR2 = reco::deltaR2(tau.p4(), muon.p4()); - if(!best_matched_muon || dR2 < deltaR2_best_match) { - best_matched_muon = &muon; - deltaR2_best_match = dR2; + size_t GetStationIndex(int station, bool throw_exception) const + { + if(station < first_station_id || station > last_station_id) { + if(throw_exception) + throw cms::Exception("MuonHitMatch") << "Station id is out of range"; + return std::numeric_limits::max(); } + return static_cast(station - 1); + } + MuonHitMatchV2(const pat::Muon& muon) + { + for(int subdet : ConsideredSubdets()) { + n_matches[subdet].fill(0); + n_hits[subdet].fill(0); + } + + CountMatches(muon, n_matches); + CountHits(muon, n_hits); + } + + void CountMatches(const pat::Muon& muon, CountMap& n_matches) + { for(const auto& segment : muon.matches()) { - if(segment.segmentMatches.empty()) continue; - if(n_matches.count(segment.detector())) - ++n_matches.at(segment.detector()).at(segment.station() - 1); + if(segment.segmentMatches.empty() && segment.rpcMatches.empty()) continue; + if(n_matches.count(segment.detector())) { + const size_t station_index = GetStationIndex(segment.station(), true); + ++n_matches.at(segment.detector()).at(station_index); + } } + } + void CountHits(const pat::Muon& muon, CountMap& n_hits) + { if(muon.outerTrack().isNonnull()) { const auto& hit_pattern = muon.outerTrack()->hitPattern(); for(int hit_index = 0; hit_index < hit_pattern.numberOfAllHits(reco::HitPattern::TRACK_HITS); ++hit_index) { auto hit_id = hit_pattern.getHitPattern(reco::HitPattern::TRACK_HITS, hit_index); if(hit_id == 0) break; if(hit_pattern.muonHitFilter(hit_id) && (hit_pattern.getHitType(hit_id) == TrackingRecHit::valid - || hit_pattern.getHitType(hit_id == TrackingRecHit::bad))) { - const int station = hit_pattern.getMuonStation(hit_id) - 1; - if(station > 0 && station < n_stations) { - std::vector* muon_n_hits = nullptr; + || hit_pattern.getHitType(hit_id) == TrackingRecHit::bad)) { + const size_t station_index = GetStationIndex(hit_pattern.getMuonStation(hit_id), false); + if(station_index < n_muon_stations) { + CountArray* muon_n_hits = nullptr; if(hit_pattern.muonDTHitFilter(hit_id)) muon_n_hits = &n_hits.at(MuonSubdetId::DT); else if(hit_pattern.muonCSCHitFilter(hit_id)) @@ -359,61 +384,40 @@ struct MuonHitMatchV2 { muon_n_hits = &n_hits.at(MuonSubdetId::RPC); if(muon_n_hits) - ++muon_n_hits->at(station); + ++muon_n_hits->at(station_index); } } } } } - static std::vector findMatchedMuons(const pat::Tau& tau, const pat::MuonCollection& muons, - double deltaR, double minPt) + unsigned NMatches(int subdet, int station) const { - const reco::Muon* hadr_cand_muon = nullptr; - if(tau.leadPFChargedHadrCand().isNonnull() && tau.leadPFChargedHadrCand()->muonRef().isNonnull()) - hadr_cand_muon = tau.leadPFChargedHadrCand()->muonRef().get(); - std::vector matched_muons; - const double dR2 = deltaR*deltaR; - for(const pat::Muon& muon : muons) { - const reco::Muon* reco_muon = &muon; - if(muon.pt() <= minPt) continue; - if(reco_muon == hadr_cand_muon) continue; - if(reco::deltaR2(tau.p4(), muon.p4()) >= dR2) continue; - matched_muons.push_back(&muon); - } - return matched_muons; + if(!n_matches.count(subdet)) + throw cms::Exception("MuonHitMatch") << "Subdet " << subdet << " not found."; + const size_t station_index = GetStationIndex(station, true); + return n_matches.at(subdet).at(station_index); } - template - // // void fillTensor(const TensorElemGet& get, const pat::Tau& tau, float default_value) const - void fillTensor(const TensorElemGet& get, const pat::Muon& muons) const + unsigned NHits(int subdet, int station) const { - const tau_analysis::MuonHitMatch hit_match(muons); - for(int subdet : tau_analysis::MuonHitMatch::ConsideredSubdets()) { - const std::string& subdetName = tau_analysis::MuonHitMatch::SubdetName(subdet); - for(int station = tau_analysis::MuonHitMatch::first_station_id; station <= tau_analysis::MuonHitMatch::last_station_id; ++station) { - const std::string matches_branch_name = "muon_n_matches_" + subdetName + "_" + std::to_string(station); - const std::string hits_branch_name = "muon_n_hits_" + subdetName + "_" + std::to_string(station); - const unsigned n_matches = hit_match.NMatches(subdet, station); - const unsigned n_hits = hit_match.NHits(subdet, station); - get(matches_branch_name) = static_cast(n_matches); - // get(matches_branch_name).push_back(static_cast(n_matches)); - // get(hits_branch_name).push_back(static_cast(n_hits)); - } - } + if(!n_hits.count(subdet)) + throw cms::Exception("MuonHitMatch") << "Subdet " << subdet << " not found."; + const size_t station_index = GetStationIndex(station, true); + return n_hits.at(subdet).at(station_index); } - -private: - unsigned countMuonStationsWithMatches(size_t first_station, size_t last_station) const + unsigned CountMuonStationsWithMatches(int first_station, int last_station) const { static const std::map> masks = { { MuonSubdetId::DT, { false, false, false, false } }, { MuonSubdetId::CSC, { true, false, false, false } }, { MuonSubdetId::RPC, { false, false, false, false } }, }; + const size_t first_station_index = GetStationIndex(first_station, true); + const size_t last_station_index = GetStationIndex(last_station, true); unsigned cnt = 0; - for(unsigned n = first_station; n <= last_station; ++n) { + for(size_t n = first_station_index; n <= last_station_index; ++n) { for(const auto& match : n_matches) { if(!masks.at(match.first).at(n) && match.second.at(n) > 0) ++cnt; } @@ -421,7 +425,7 @@ struct MuonHitMatchV2 { return cnt; } - unsigned countMuonStationsWithHits(size_t first_station, size_t last_station) const + unsigned CountMuonStationsWithHits(int first_station, int last_station) const { static const std::map> masks = { { MuonSubdetId::DT, { false, false, false, false } }, @@ -429,14 +433,19 @@ struct MuonHitMatchV2 { { MuonSubdetId::RPC, { false, false, false, false } }, }; + const size_t first_station_index = GetStationIndex(first_station, true); + const size_t last_station_index = GetStationIndex(last_station, true); unsigned cnt = 0; - for(unsigned n = first_station; n <= last_station; ++n) { + for(size_t n = first_station_index; n <= last_station_index; ++n) { for(const auto& hit : n_hits) { if(!masks.at(hit.first).at(n) && hit.second.at(n) > 0) ++cnt; } } return cnt; } + + private: + CountMap n_matches, n_hits; }; enum class CellObjectType { PfCand_electron, PfCand_muon, PfCand_chargedHadron, PfCand_neutralHadron, @@ -1127,32 +1136,37 @@ class DeepTauId : public deep_tau::DeepTauBase { get(muon_rel_pfEcalEnergy) = getValueNorm(muons.at(index_muon).pfEcalEnergy() / muons.at(index_muon).polarP4().pt(), 0.2273f, 0.4865f); } - MuonHitMatchV2 muon_hit_match; - muon_hit_match.fillTensor(get, muons.at(index_muon)); - // get(muon_n_matches_DT_1) = getValueLinear(n_matches.at(MuonSubdetId::DT).at(0), 0, 2, true) - // get(muon_n_matches_DT_2) = getValueLinear(tau.muon_n_matches_DT_2.at(idx), 0, 2, true) - // get(muon_n_matches_DT_3) = getValueLinear(tau.muon_n_matches_DT_3.at(idx), 0, 2, true) - // get(muon_n_matches_DT_4) = getValueLinear(tau.muon_n_matches_DT_4.at(idx), 0, 2, true) - // get(muon_n_matches_CSC_1) = getValueLinear(tau.muon_n_matches_CSC_1.at(idx), 0, 6, true) - // get(muon_n_matches_CSC_2) = getValueLinear(tau.muon_n_matches_CSC_2.at(idx), 0, 2, true) - // get(muon_n_matches_CSC_3) = getValueLinear(tau.muon_n_matches_CSC_3.at(idx), 0, 2, true) - // get(muon_n_matches_CSC_4) = getValueLinear(tau.muon_n_matches_CSC_4.at(idx), 0, 2, true) - // get(muon_n_matches_RPC_1) = getValueLinear(tau.muon_n_matches_RPC_1.at(idx), 0, 7, true) - // get(muon_n_matches_RPC_2) = getValueLinear(tau.muon_n_matches_RPC_2.at(idx), 0, 6, true) - // get(muon_n_matches_RPC_3) = getValueLinear(tau.muon_n_matches_RPC_3.at(idx), 0, 4, true) - // get(muon_n_matches_RPC_4) = getValueLinear(tau.muon_n_matches_RPC_4.at(idx), 0, 4, true) - // get(muon_n_hits_DT_1) = getValueLinear(tau.muon_n_hits_DT_1.at(idx), 0, 12, true) - // get(muon_n_hits_DT_2) = getValueLinear(tau.muon_n_hits_DT_2.at(idx), 0, 12, true) - // get(muon_n_hits_DT_3) = getValueLinear(tau.muon_n_hits_DT_3.at(idx), 0, 12, true) - // get(muon_n_hits_DT_4) = getValueLinear(tau.muon_n_hits_DT_4.at(idx), 0, 8, true) - // get(muon_n_hits_CSC_1) = getValueLinear(tau.muon_n_hits_CSC_1.at(idx), 0, 24, true) - // get(muon_n_hits_CSC_2) = getValueLinear(tau.muon_n_hits_CSC_2.at(idx), 0, 12, true) - // get(muon_n_hits_CSC_3) = getValueLinear(tau.muon_n_hits_CSC_3.at(idx), 0, 12, true) - // get(muon_n_hits_CSC_4) = getValueLinear(tau.muon_n_hits_CSC_4.at(idx), 0, 12, true) - // get(muon_n_hits_RPC_1) = getValueLinear(tau.muon_n_hits_RPC_1.at(idx), 0, 4, true) - // get(muon_n_hits_RPC_2) = getValueLinear(tau.muon_n_hits_RPC_2.at(idx), 0, 4, true) - // get(muon_n_hits_RPC_3) = getValueLinear(tau.muon_n_hits_RPC_3.at(idx), 0, 2, true) - // get(muon_n_hits_RPC_4) = getValueLinear(tau.muon_n_hits_RPC_4.at(idx), 0, 2, true) + + MuonHitMatchV2 hit_match(muons.at(index_muon)); + static const std::map> muonMatchHitVars = { + { MuonSubdetId::DT, { muon_n_matches_DT_1, muon_n_hits_DT_1 } }, + { MuonSubdetId::CSC, { muon_n_matches_CSC_1, muon_n_hits_CSC_1 } }, + { MuonSubdetId::RPC, { muon_n_matches_RPC_1, muon_n_hits_RPC_1 } } + }; + + static const std::map> muonMatchVarLimits = { + { MuonSubdetId::DT, { 2, 2, 2, 2 } }, + { MuonSubdetId::CSC, { 6, 2, 2, 2 } }, + { MuonSubdetId::RPC, { 7, 6, 4, 4 } } + }; + + static const std::map> muonHitVarLimits = { + { MuonSubdetId::DT, { 12, 12, 12, 8 } }, + { MuonSubdetId::CSC, { 24, 12, 12, 12 } }, + { MuonSubdetId::RPC, { 4, 4, 2, 2 } } + }; + + for(int subdet : hit_match.MuonHitMatchV2::ConsideredSubdets()) { + const auto& matchHitVar = muonMatchHitVars.at(subdet); + const auto& matchLimits = muonMatchVarLimits.at(subdet); + const auto& hitLimits = muonHitVarLimits.at(subdet); + for(int station = MuonHitMatchV2::first_station_id; station <= MuonHitMatchV2::last_station_id; ++station) { + const unsigned n_matches = hit_match.NMatches(subdet, station); + const unsigned n_hits = hit_match.NHits(subdet, station); + get(matchHitVar.first + station - 1) = getValueLinear(n_matches, 0, matchLimits.at(station - 1), true); + get(matchHitVar.second + station - 1) = getValueLinear(n_hits, 0, hitLimits.at(station - 1), true); + } + } } } return inputs; diff --git a/RecoTauTag/RecoTau/plugins/MuonHitMatch.cc b/RecoTauTag/RecoTau/plugins/MuonHitMatch.cc deleted file mode 100644 index 1b1be725f7564..0000000000000 --- a/RecoTauTag/RecoTau/plugins/MuonHitMatch.cc +++ /dev/null @@ -1,136 +0,0 @@ -/*! Match hits in the muon system. -*/ - -#include "RecoTauTag/RecoTau/interface/MuonHitMatch.h" - -namespace tau_analysis { - -const std::vector& MuonHitMatch::ConsideredSubdets() -{ - static const std::vector subdets = { MuonSubdetId::DT, MuonSubdetId::CSC, MuonSubdetId::RPC }; - return subdets; -} - -const std::string& MuonHitMatch::SubdetName(int subdet) -{ - static const std::map subdet_names = { - { MuonSubdetId::DT, "DT" }, { MuonSubdetId::CSC, "CSC" }, { MuonSubdetId::RPC, "RPC" } - }; - if(!subdet_names.count(subdet)) - throw cms::Exception("MuonHitMatch") << "Subdet name for subdet id " << subdet << " not found."; - return subdet_names.at(subdet); -} - -size_t MuonHitMatch::GetStationIndex(int station, bool throw_exception) -{ - if(station < first_station_id || station > last_station_id) { - if(throw_exception) - throw cms::Exception("MuonHitMatch") << "Station id is out of range"; - return std::numeric_limits::max(); - } - return static_cast(station - 1); -} - -MuonHitMatch::MuonHitMatch(const pat::Muon& muon) -{ - for(int subdet : ConsideredSubdets()) { - n_matches[subdet].fill(0); - n_hits[subdet].fill(0); - } - - CountMatches(muon, n_matches); - CountHits(muon, n_hits); -} - -void MuonHitMatch::CountMatches(const pat::Muon& muon, CountMap& n_matches) -{ - for(const auto& segment : muon.matches()) { - if(segment.segmentMatches.empty() && segment.rpcMatches.empty()) continue; - if(n_matches.count(segment.detector())) { - const size_t station_index = GetStationIndex(segment.station(), true); - ++n_matches.at(segment.detector()).at(station_index); - } - } -} - -void MuonHitMatch::CountHits(const pat::Muon& muon, CountMap& n_hits) -{ - if(muon.outerTrack().isNonnull()) { - const auto& hit_pattern = muon.outerTrack()->hitPattern(); - for(int hit_index = 0; hit_index < hit_pattern.numberOfAllHits(reco::HitPattern::TRACK_HITS); ++hit_index) { - auto hit_id = hit_pattern.getHitPattern(reco::HitPattern::TRACK_HITS, hit_index); - if(hit_id == 0) break; - if(hit_pattern.muonHitFilter(hit_id) && (hit_pattern.getHitType(hit_id) == TrackingRecHit::valid - || hit_pattern.getHitType(hit_id) == TrackingRecHit::bad)) { - const size_t station_index = GetStationIndex(hit_pattern.getMuonStation(hit_id), false); - if(station_index < n_muon_stations) { - CountArray* muon_n_hits = nullptr; - if(hit_pattern.muonDTHitFilter(hit_id)) - muon_n_hits = &n_hits.at(MuonSubdetId::DT); - else if(hit_pattern.muonCSCHitFilter(hit_id)) - muon_n_hits = &n_hits.at(MuonSubdetId::CSC); - else if(hit_pattern.muonRPCHitFilter(hit_id)) - muon_n_hits = &n_hits.at(MuonSubdetId::RPC); - - if(muon_n_hits) - ++muon_n_hits->at(station_index); - } - } - } - } -} - -unsigned MuonHitMatch::NMatches(int subdet, int station) const -{ - if(!n_matches.count(subdet)) - throw cms::Exception("MuonHitMatch") << "Subdet " << subdet << " not found."; - const size_t station_index = GetStationIndex(station, true); - return n_matches.at(subdet).at(station_index); -} - -unsigned MuonHitMatch::NHits(int subdet, int station) const -{ - if(!n_hits.count(subdet)) - throw cms::Exception("MuonHitMatch") << "Subdet " << subdet << " not found."; - const size_t station_index = GetStationIndex(station, true); - return n_hits.at(subdet).at(station_index); -} - -unsigned MuonHitMatch::CountMuonStationsWithMatches(int first_station, int last_station) const -{ - static const std::map> masks = { - { MuonSubdetId::DT, { false, false, false, false } }, - { MuonSubdetId::CSC, { true, false, false, false } }, - { MuonSubdetId::RPC, { false, false, false, false } }, - }; - const size_t first_station_index = GetStationIndex(first_station, true); - const size_t last_station_index = GetStationIndex(last_station, true); - unsigned cnt = 0; - for(size_t n = first_station_index; n <= last_station_index; ++n) { - for(const auto& match : n_matches) { - if(!masks.at(match.first).at(n) && match.second.at(n) > 0) ++cnt; - } - } - return cnt; -} - -unsigned MuonHitMatch::CountMuonStationsWithHits(int first_station, int last_station) const -{ - static const std::map> masks = { - { MuonSubdetId::DT, { false, false, false, false } }, - { MuonSubdetId::CSC, { false, false, false, false } }, - { MuonSubdetId::RPC, { false, false, false, false } }, - }; - - const size_t first_station_index = GetStationIndex(first_station, true); - const size_t last_station_index = GetStationIndex(last_station, true); - unsigned cnt = 0; - for(size_t n = first_station_index; n <= last_station_index; ++n) { - for(const auto& hit : n_hits) { - if(!masks.at(hit.first).at(n) && hit.second.at(n) > 0) ++cnt; - } - } - return cnt; -} - -} // namespace tau_analysis