CMS 3D CMS Logo

BadGlobalMuonTagger.cc
Go to the documentation of this file.
5 
9 
13 
15  public:
16  explicit BadGlobalMuonTagger(const edm::ParameterSet & iConfig);
17  ~BadGlobalMuonTagger() override {}
18 
19  bool filter(edm::StreamID iID, edm::Event & iEvent, const edm::EventSetup & iSetup) const override;
20 
21  private:
24  double ptCut_;
26 
27  bool outInOnly(const reco::Muon &mu) const {
28  const reco::Track &tk = *mu.innerTrack();
29  return tk.algoMask().count() == 1 && tk.isAlgoInMask(reco::Track::muonSeededStepOutIn);
30  }
31  bool preselection(const reco::Muon &mu) const {
32  return (!selectClones_ || outInOnly(mu));
33  }
34  bool tighterId(const reco::Muon &mu) const {
35  return muon::isMediumMuon(mu) && mu.numberOfMatchedStations() >= 2;
36  }
37  bool tightGlobal(const reco::Muon &mu) const {
38  return mu.isGlobalMuon() && (mu.globalTrack()->hitPattern().muonStationsWithValidHits() >= 3 && mu.globalTrack()->normalizedChi2() <= 20);
39  }
40  bool safeId(const reco::Muon &mu) const {
41  if (mu.muonBestTrack()->ptError() > 0.2 * mu.muonBestTrack()->pt()) { return false; }
42  return mu.numberOfMatchedStations() >= 1 || tightGlobal(mu);
43  }
44  bool partnerId(const reco::Muon &mu) const {
45  return mu.pt() >= 10 && mu.numberOfMatchedStations() >= 1;
46  }
47 };
48 
50  muons_(consumes<edm::View<reco::Muon>>(iConfig.getParameter<edm::InputTag>("muons"))),
51  vtx_(consumes<std::vector<reco::Vertex>>(iConfig.getParameter<edm::InputTag>("vtx"))),
52  ptCut_(iConfig.getParameter<double>("muonPtCut")),
53  selectClones_(iConfig.getParameter<bool>("selectClones")),
54  taggingMode_(iConfig.getParameter<bool> ("taggingMode")),
55  verbose_(iConfig.getUntrackedParameter<bool> ("verbose",false))
56 {
57  produces<edm::PtrVector<reco::Muon>>("bad");
58  produces<bool>("notBadEvent");
59 }
60 
61 
62 bool
64  using namespace edm;
65 
66  // read input
69  std::vector<int> goodMuon;
70 
71  iEvent.getByToken(vtx_, vtx);
72  assert(!vtx->empty());
73  const auto &PV = vtx->front().position();
74 
75  std::unique_ptr<edm::PtrVector<reco::Muon>> out(new edm::PtrVector<reco::Muon>());
76  iEvent.getByToken(muons_, hmuons);
77  const edm::View<reco::Muon> & muons = *hmuons;
78  for (const reco::Muon & mu : muons) {
79  if (!mu.isPFMuon() || mu.innerTrack().isNull()) {
80  goodMuon.push_back(-1); // bad but we don't care
81  continue;
82  }
83  if (preselection(mu)) {
84  float dxypv = std::abs(mu.innerTrack()->dxy(PV));
85  float dzpv = std::abs(mu.innerTrack()->dz(PV));
86  if (tighterId(mu)) {
87  bool ipLoose = ((dxypv < 0.5 && dzpv < 2.0) || mu.innerTrack()->hitPattern().pixelLayersWithMeasurement() >= 2);
88  goodMuon.push_back(ipLoose || (!selectClones_ && tightGlobal(mu)));
89  } else if (safeId(mu)) {
90  bool ipTight = (dxypv < 0.2 && dzpv < 0.5);
91  goodMuon.push_back(ipTight);
92  } else {
93  goodMuon.push_back(0);
94  }
95  } else {
96  goodMuon.push_back(3); // maybe good, maybe bad, but we don't care
97  }
98  }
99 
100  bool found = false;
101  for (unsigned int i = 0, n = muons.size(); i < n; ++i) {
102  if (muons[i].pt() < ptCut_ || goodMuon[i] != 0) continue;
103  if (verbose_) printf("potentially bad muon %d of pt %.1f eta %+.3f phi %+.3f\n", int(i+1), muons[i].pt(), muons[i].eta(), muons[i].phi());
104  bool bad = true;
105  if (selectClones_) {
106  bad = false; // unless proven otherwise
107  unsigned int n1 = muons[i].numberOfMatches(reco::Muon::SegmentArbitration);
108  for (unsigned int j = 0; j < n; ++j) {
109  if (j == i || goodMuon[j] <= 0 || !partnerId(muons[j])) continue;
110  unsigned int n2 = muons[j].numberOfMatches(reco::Muon::SegmentArbitration);
111  if (deltaR2(muons[i],muons[j]) < 0.16 || (n1 > 0 && n2 > 0 && muon::sharedSegments(muons[i],muons[j]) >= 0.5*std::min(n1,n2))) {
112  if (verbose_) printf(" tagged as clone of muon %d of pt %.1f eta %+.3f phi %+.3f\n", int(j+1), muons[j].pt(), muons[j].eta(), muons[j].phi());
113  bad = true;
114  break;
115  }
116  }
117  }
118  if (bad) {
119  found = true;
120  out->push_back(muons.ptrAt(i));
121  }
122  }
123 
124  iEvent.put(std::move(out), "bad");
125  iEvent.put(std::unique_ptr<bool>(new bool(!found)), "notBadEvent");
126  return taggingMode_ || found;
127 }
128 
129 
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool tighterId(const reco::Muon &mu) const
bool safeId(const reco::Muon &mu) const
bool isMediumMuon(const reco::Muon &, bool run2016_hip_mitigation=false)
edm::EDGetTokenT< edm::View< reco::Muon > > muons_
virtual TrackRef innerTrack() const
Definition: Muon.h:48
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
double pt() const final
transverse momentum
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
bool outInOnly(const reco::Muon &mu) const
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
virtual TrackRef muonBestTrack() const
Definition: Muon.h:63
bool partnerId(const reco::Muon &mu) const
Definition: Muon.py:1
bool isGlobalMuon() const override
Definition: Muon.h:291
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const int mu
Definition: Constants.h:22
bool tightGlobal(const reco::Muon &mu) const
T min(T a, T b)
Definition: MathUtil.h:58
int numberOfMatchedStations(ArbitrationType type=SegmentAndTrackArbitration) const
edm::EDGetTokenT< std::vector< reco::Vertex > > vtx_
bool isAlgoInMask(TrackAlgorithm a) const
Definition: TrackBase.h:395
int sharedSegments(const reco::Muon &muon1, const reco::Muon &muon2, unsigned int segmentArbitrationMask=reco::MuonSegmentMatch::BestInChamberByDR)
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
bool preselection(const reco::Muon &mu) const
AlgoMask algoMask() const
Definition: TrackBase.h:393
fixed size matrix
HLT enums.
BadGlobalMuonTagger(const edm::ParameterSet &iConfig)
bool filter(edm::StreamID iID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
def move(src, dest)
Definition: eostools.py:511
virtual TrackRef globalTrack() const
reference to Track reconstructed in both tracked and muon detector
Definition: Muon.h:54