CMS 3D CMS Logo

MuonCleanerBySegments.cc
Go to the documentation of this file.
1 
2 //
3 //
4 
27 
29 
34 
35 namespace modules {
36 
37  template <typename T>
39  public:
40  explicit MuonCleanerBySegmentsT(const edm::ParameterSet &iConfig);
42 
43  void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override;
44 
45  bool isSameMuon(const T &mu1, const T &mu2) const {
46  return (&mu1 == &mu2) ||
47  (mu1.reco::Muon::innerTrack().isNonnull() ? mu1.reco::Muon::innerTrack() == mu2.reco::Muon::innerTrack()
48  : mu1.reco::Muon::outerTrack() == mu2.reco::Muon::outerTrack());
49  }
50  bool isBetterMuon(const T &mu1, const T &mu2) const;
51 
52  private:
55 
60 
63 
67  typedef std::pair<const reco::Muon *, const reco::Muon *> MuonPointerPair;
69  };
70 
71  template <>
72  bool MuonCleanerBySegmentsT<pat::Muon>::isSameMuon(const pat::Muon &mu1, const pat::Muon &mu2) const;
73 } // namespace modules
74 
75 template <typename T>
77  : srcToken_(consumes<edm::View<T> >(iConfig.getParameter<edm::InputTag>("src"))),
78  preselection_(iConfig.existsAs<std::string>("preselection") ? iConfig.getParameter<std::string>("preselection")
79  : ""),
80  passthrough_(iConfig.existsAs<std::string>("passthrough") ? iConfig.getParameter<std::string>("passthrough")
81  : "0"),
82  sharedFraction_(iConfig.getParameter<double>("fractionOfSharedSegments")),
83  defaultBestMuon_(!iConfig.existsAs<std::string>("customArbitration")),
84  bestMuonSelector_(defaultBestMuon_ ? std::string("") : iConfig.getParameter<std::string>("customArbitration")) {
85  // this is the basic output (edm::Association is not generic)
86  produces<std::vector<T> >();
87 }
88 
89 template <typename T>
91  using namespace edm;
92  using namespace std;
93 
95  unique_ptr<vector<T> > out(new vector<T>());
96 
97  iEvent.getByToken(srcToken_, src);
98  unsigned int nsrc = src->size();
99  out->reserve(nsrc);
100  std::vector<int> good(nsrc, true);
101  for (unsigned int i = 0; i < nsrc; ++i) {
102  const T &mu1 = (*src)[i];
103  if (!preselection_(mu1))
104  good[i] = false;
105  if (!good[i])
106  continue;
107  int nSegments1 = mu1.numberOfMatches(reco::Muon::SegmentArbitration);
108  for (unsigned int j = i + 1; j < nsrc; ++j) {
109  const T &mu2 = (*src)[j];
110  if (isSameMuon(mu1, mu2))
111  continue;
112  if (!good[j] || !preselection_(mu2))
113  continue;
114  int nSegments2 = mu2.numberOfMatches(reco::Muon::SegmentArbitration);
115  if (nSegments2 == 0 || nSegments1 == 0)
116  continue;
117  double sf = muon::sharedSegments(mu1, mu2) / std::min<double>(nSegments1, nSegments2);
118  if (sf > sharedFraction_) {
119  if (isBetterMuon(mu1, mu2)) {
120  good[j] = false;
121  } else {
122  good[i] = false;
123  }
124  }
125  }
126  }
127  for (unsigned int i = 0; i < nsrc; ++i) {
128  const T &mu1 = (*src)[i];
129  if (good[i] || passthrough_(mu1))
130  out->push_back(mu1);
131  }
132  iEvent.put(std::move(out));
133 }
134 
135 template <typename T>
136 bool modules::MuonCleanerBySegmentsT<T>::isBetterMuon(const T &mu1, const T &mu2) const {
137  if (!defaultBestMuon_) {
138  MuonPointerPair pair = {&mu1, &mu2};
139  return bestMuonSelector_(pair);
140  }
141  if (mu2.track().isNull())
142  return true;
143  if (mu1.track().isNull())
144  return false;
145  if (mu1.isPFMuon() != mu2.isPFMuon())
146  return mu1.isPFMuon();
147  if (mu1.isGlobalMuon() != mu2.isGlobalMuon())
148  return mu1.isGlobalMuon();
149  if (mu1.charge() == mu2.charge() && deltaR2(mu1, mu2) < 0.0009) {
150  return mu1.track()->ptError() / mu1.track()->pt() < mu2.track()->ptError() / mu2.track()->pt();
151  } else {
152  int nm1 = mu1.numberOfMatches(reco::Muon::SegmentArbitration);
153  int nm2 = mu2.numberOfMatches(reco::Muon::SegmentArbitration);
154  return (nm1 != nm2 ? nm1 > nm2 : mu1.pt() > mu2.pt());
155  }
156 }
157 
158 template <>
160  return (&mu1 == &mu2) || (mu1.originalObjectRef() == mu2.originalObjectRef()) ||
161  (mu1.reco::Muon::innerTrack().isNonnull() ? mu1.reco::Muon::innerTrack() == mu2.reco::Muon::innerTrack()
162  : mu1.reco::Muon::outerTrack() == mu2.reco::Muon::outerTrack());
163 }
164 
165 namespace modules {
168 } // namespace modules
169 
171 using namespace modules;
modules::MuonCleanerBySegmentsT< reco::Muon > MuonCleanerBySegments
double sharedFraction_
Fraction of shared segments.
StringCutObjectSelector< T > preselection_
Preselection cut.
modules::MuonCleanerBySegmentsT< pat::Muon > PATMuonCleanerBySegments
StringCutObjectSelector< T > passthrough_
Always-accept cut.
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::pair< const reco::Muon *, const reco::Muon * > MuonPointerPair
Cut on the pair of objects together.
bool isBetterMuon(const T &mu1, const T &mu2) const
int iEvent
Definition: GenABIO.cc:224
bool isSameMuon(const T &mu1, const T &mu2) const
edm::EDGetTokenT< edm::View< T > > srcToken_
Tokens for input collections.
MuonCleanerBySegmentsT(const edm::ParameterSet &iConfig)
int sharedSegments(const reco::Muon &muon1, const reco::Muon &muon2, unsigned int segmentArbitrationMask=reco::MuonSegmentMatch::BestInChamberByDR)
auto const good
min quality of good
HLT enums.
const edm::Ptr< reco::Candidate > & originalObjectRef() const
reference to original object. Returns a null reference if not available
Definition: PATObject.h:537
Removes duplicates from a muon collection using segment references.
StringCutObjectSelector< MuonPointerPair, true > bestMuonSelector_
long double T
Analysis-level muon class.
Definition: Muon.h:51
bool defaultBestMuon_
Use default criteria to choose the best muon.
def move(src, dest)
Definition: eostools.py:511
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override