CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuonCleanerBySegments.cc
Go to the documentation of this file.
1 
2 //
3 //
4 
28 
30 
35 
36 
37 namespace modules {
38 
39  template<typename T>
41  public:
42  explicit MuonCleanerBySegmentsT(const edm::ParameterSet & iConfig);
44 
45  virtual void produce(edm::Event & iEvent, const edm::EventSetup& iSetup) override;
46 
47  bool isSameMuon(const T &mu1, const T &mu2) const {
48  return (& mu1 == & mu2) ||
49  (mu1.reco::Muon::innerTrack().isNonnull() ?
50  mu1.reco::Muon::innerTrack() == mu2.reco::Muon::innerTrack() :
51  mu1.reco::Muon::outerTrack() == mu2.reco::Muon::outerTrack());
52  }
53  bool isBetterMuon(const T &mu1, const T &mu2) const ;
54  private:
57 
62 
65 
69  typedef std::pair<const reco::Muon *, const reco::Muon *> MuonPointerPair;
71  };
72 
73  template<>
74  bool
76 } // namespace
77 
78 template<typename T>
80  srcToken_(consumes<edm::View<T> >(iConfig.getParameter<edm::InputTag>("src"))),
81  preselection_(iConfig.existsAs<std::string>("preselection") ? iConfig.getParameter<std::string>("preselection") : ""),
82  passthrough_(iConfig.existsAs<std::string>("passthrough") ? iConfig.getParameter<std::string>("passthrough") : "0"),
83  sharedFraction_(iConfig.getParameter<double>("fractionOfSharedSegments")),
84  defaultBestMuon_(!iConfig.existsAs<std::string>("customArbitration")),
85  bestMuonSelector_(defaultBestMuon_ ? std::string("") : iConfig.getParameter<std::string>("customArbitration"))
86 {
87  // this is the basic output (edm::Association is not generic)
88  produces<std::vector<T> >();
89 }
90 
91 template<typename T>
92 void
94  using namespace edm;
95  using namespace std;
96 
98  auto_ptr<vector<T> > out(new vector<T>());
99 
100  iEvent.getByToken(srcToken_, src);
101  unsigned int nsrc = src->size();
102  out->reserve(nsrc);
103  std::vector<int> good(nsrc, true);
104  for (unsigned int i = 0; i < nsrc; ++i) {
105  const T &mu1 = (*src)[i];
106  if (!preselection_(mu1)) good[i] = false;
107  if (!good[i]) continue;
108  int nSegments1 = mu1.numberOfMatches(reco::Muon::SegmentArbitration);
109  for (unsigned int j = i+1; j < nsrc; ++j) {
110  const T &mu2 = (*src)[j];
111  if (isSameMuon(mu1,mu2)) continue;
112  if (!good[j] || !preselection_(mu2)) continue;
113  int nSegments2 = mu2.numberOfMatches(reco::Muon::SegmentArbitration);
114  if (nSegments2 == 0 || nSegments1 == 0) continue;
115  double sf = muon::sharedSegments(mu1,mu2)/std::min<double>(nSegments1,nSegments2);
116  if (sf > sharedFraction_) {
117  if (isBetterMuon(mu1,mu2)) {
118  good[j] = false;
119  } else {
120  good[i] = false;
121  }
122  }
123  }
124  }
125  for (unsigned int i = 0; i < nsrc; ++i) {
126  const T &mu1 = (*src)[i];
127  if (good[i] || passthrough_(mu1)) out->push_back(mu1);
128  }
129  iEvent.put(out);
130 }
131 
132 template<typename T>
133 bool
134 modules::MuonCleanerBySegmentsT<T>::isBetterMuon(const T &mu1, const T &mu2) const {
135  if (!defaultBestMuon_) {
136  MuonPointerPair pair = { &mu1, &mu2 };
137  return bestMuonSelector_(pair);
138  }
139  if (mu2.track().isNull()) return true;
140  if (mu1.track().isNull()) return false;
141  if (mu1.isPFMuon() != mu2.isPFMuon()) return mu1.isPFMuon();
142  if (mu1.isGlobalMuon() != mu2.isGlobalMuon()) return mu1.isGlobalMuon();
143  if (mu1.charge() == mu2.charge() && deltaR2(mu1,mu2) < 0.0009) {
144  return mu1.track()->ptError()/mu1.track()->pt() < mu2.track()->ptError()/mu2.track()->pt();
145  } else {
146  int nm1 = mu1.numberOfMatches(reco::Muon::SegmentArbitration);
147  int nm2 = mu2.numberOfMatches(reco::Muon::SegmentArbitration);
148  return (nm1 != nm2 ? nm1 > nm2 : mu1.pt() > mu2.pt());
149  }
150 }
151 
152 template<>
153 bool
155  return (& mu1 == & mu2) ||
156  (mu1.originalObjectRef() == mu2.originalObjectRef()) ||
157  (mu1.reco::Muon::innerTrack().isNonnull() ?
158  mu1.reco::Muon::innerTrack() == mu2.reco::Muon::innerTrack() :
159  mu1.reco::Muon::outerTrack() == mu2.reco::Muon::outerTrack());
160 }
161 
162 namespace modules {
165 }
166 
168 using namespace modules;
modules::MuonCleanerBySegmentsT< reco::Muon > MuonCleanerBySegments
int i
Definition: DBlmapReader.cc:9
double sharedFraction_
Fraction of shared segments.
StringCutObjectSelector< T > preselection_
Preselection cut.
modules::MuonCleanerBySegmentsT< pat::Muon > PATMuonCleanerBySegments
StringCutObjectSelector< T > passthrough_
Always-accept cut.
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::pair< const reco::Muon *, const reco::Muon * > MuonPointerPair
Cut on the pair of objects together.
int iEvent
Definition: GenABIO.cc:230
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:115
bool isSameMuon(const T &mu1, const T &mu2) const
int j
Definition: DBlmapReader.cc:9
double deltaR2(const T1 &t1, const T2 &t2)
Definition: deltaR.h:36
edm::EDGetTokenT< edm::View< T > > srcToken_
Tokens for input collections.
bool isBetterMuon(const T &mu1, const T &mu2) const
const edm::Ptr< reco::Candidate > & originalObjectRef() const
reference to original object. Returns a null reference if not available
Definition: PATObject.h:484
MuonCleanerBySegmentsT(const edm::ParameterSet &iConfig)
tuple out
Definition: dbtoconf.py:99
int sharedSegments(const reco::Muon &muon1, const reco::Muon &muon2, unsigned int segmentArbitrationMask=reco::MuonSegmentMatch::BestInChamberByDR)
Removes duplicates from a muon collection using segment references.
StringCutObjectSelector< MuonPointerPair, true > bestMuonSelector_
long double T
Analysis-level muon class.
Definition: Muon.h:49
bool defaultBestMuon_
Use default criteria to choose the best muon.
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override