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 // $Id: MuonCleanerBySegments.cc,v 1.2 2013/02/27 20:42:45 wmtan Exp $
4 //
5 
29 
31 
36 
37 
38 namespace modules {
39 
40  template<typename T>
42  public:
43  explicit MuonCleanerBySegmentsT(const edm::ParameterSet & iConfig);
45 
46  virtual void produce(edm::Event & iEvent, const edm::EventSetup& iSetup) override;
47 
48  bool isSameMuon(const T &mu1, const T &mu2) const {
49  return (& mu1 == & mu2) ||
50  (mu1.reco::Muon::innerTrack().isNonnull() ?
51  mu1.reco::Muon::innerTrack() == mu2.reco::Muon::innerTrack() :
52  mu1.reco::Muon::outerTrack() == mu2.reco::Muon::outerTrack());
53  }
54  bool isBetterMuon(const T &mu1, const T &mu2) const ;
55  private:
58 
63 
66 
70  typedef std::pair<const reco::Muon *, const reco::Muon *> MuonPointerPair;
72  };
73 
74  template<>
75  bool
77 } // namespace
78 
79 template<typename T>
81  src_(iConfig.getParameter<edm::InputTag>("src")),
82  preselection_(iConfig.existsAs<std::string>("preselection") ? iConfig.getParameter<std::string>("preselection") : ""),
83  passthrough_(iConfig.existsAs<std::string>("passthrough") ? iConfig.getParameter<std::string>("passthrough") : "0"),
84  sharedFraction_(iConfig.getParameter<double>("fractionOfSharedSegments")),
85  defaultBestMuon_(!iConfig.existsAs<std::string>("customArbitration")),
86  bestMuonSelector_(defaultBestMuon_ ? std::string("") : iConfig.getParameter<std::string>("customArbitration"))
87 {
88  // this is the basic output (edm::Association is not generic)
89  produces<std::vector<T> >();
90 }
91 
92 template<typename T>
93 void
95  using namespace edm;
96  using namespace std;
97 
99  auto_ptr<vector<T> > out(new vector<T>());
100 
101  iEvent.getByLabel(src_, src);
102  unsigned int nsrc = src->size();
103  out->reserve(nsrc);
104  std::vector<int> good(nsrc, true);
105  for (unsigned int i = 0; i < nsrc; ++i) {
106  const T &mu1 = (*src)[i];
107  if (!preselection_(mu1)) good[i] = false;
108  if (!good[i]) continue;
109  int nSegments1 = mu1.numberOfMatches(reco::Muon::SegmentArbitration);
110  for (unsigned int j = i+1; j < nsrc; ++j) {
111  const T &mu2 = (*src)[j];
112  if (isSameMuon(mu1,mu2)) continue;
113  if (!good[j] || !preselection_(mu2)) continue;
114  int nSegments2 = mu2.numberOfMatches(reco::Muon::SegmentArbitration);
115  if (nSegments2 == 0 || nSegments1 == 0) continue;
116  double sf = muon::sharedSegments(mu1,mu2)/std::min<double>(nSegments1,nSegments2);
117  if (sf > sharedFraction_) {
118  if (isBetterMuon(mu1,mu2)) {
119  good[j] = false;
120  } else {
121  good[i] = false;
122  }
123  }
124  }
125  }
126  for (unsigned int i = 0; i < nsrc; ++i) {
127  const T &mu1 = (*src)[i];
128  if (good[i] || passthrough_(mu1)) out->push_back(mu1);
129  }
130  iEvent.put(out);
131 }
132 
133 template<typename T>
134 bool
135 modules::MuonCleanerBySegmentsT<T>::isBetterMuon(const T &mu1, const T &mu2) const {
136  if (!defaultBestMuon_) {
137  MuonPointerPair pair = { &mu1, &mu2 };
138  return bestMuonSelector_(pair);
139  }
140  if (mu2.track().isNull()) return true;
141  if (mu1.track().isNull()) return false;
142  if (mu1.isPFMuon() != mu2.isPFMuon()) return mu1.isPFMuon();
143  if (mu1.isGlobalMuon() != mu2.isGlobalMuon()) return mu1.isGlobalMuon();
144  if (mu1.charge() == mu2.charge() && deltaR2(mu1,mu2) < 0.0009) {
145  return mu1.track()->ptError()/mu1.track()->pt() < mu2.track()->ptError()/mu2.track()->pt();
146  } else {
147  int nm1 = mu1.numberOfMatches(reco::Muon::SegmentArbitration);
148  int nm2 = mu2.numberOfMatches(reco::Muon::SegmentArbitration);
149  return (nm1 != nm2 ? nm1 > nm2 : mu1.pt() > mu2.pt());
150  }
151 }
152 
153 template<>
154 bool
156  return (& mu1 == & mu2) ||
157  (mu1.originalObjectRef() == mu2.originalObjectRef()) ||
158  (mu1.reco::Muon::innerTrack().isNonnull() ?
159  mu1.reco::Muon::innerTrack() == mu2.reco::Muon::innerTrack() :
160  mu1.reco::Muon::outerTrack() == mu2.reco::Muon::outerTrack());
161 }
162 
163 namespace modules {
166 }
167 
169 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.
#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:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:94
bool isSameMuon(const T &mu1, const T &mu2) const
int j
Definition: DBlmapReader.cc:9
edm::InputTag src_
Labels 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:482
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
double deltaR2(const Vector1 &v1, const Vector2 &v2)
Definition: VectorUtil.h:78
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:51
bool defaultBestMuon_
Use default criteria to choose the best muon.
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override