CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/MuonAnalysis/MuonAssociators/plugins/MuonCleanerBySegments.cc

Go to the documentation of this file.
00001 
00002 //
00003 // $Id: MuonCleanerBySegments.cc,v 1.1 2012/08/11 13:00:50 gpetrucc Exp $
00004 //
00005 
00025 #include "FWCore/Framework/interface/EDProducer.h"
00026 #include "FWCore/Framework/interface/Event.h"
00027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00028 #include "FWCore/Utilities/interface/InputTag.h"
00029 
00030 #include "DataFormats/Math/interface/deltaR.h"
00031 
00032 #include "DataFormats/MuonReco/interface/Muon.h"
00033 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
00034 #include "DataFormats/PatCandidates/interface/Muon.h"
00035 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
00036 
00037 
00038 namespace modules {
00039 
00040   template<typename T>
00041   class MuonCleanerBySegmentsT : public edm::EDProducer {
00042     public:
00043       explicit MuonCleanerBySegmentsT(const edm::ParameterSet & iConfig);
00044       virtual ~MuonCleanerBySegmentsT() { }
00045 
00046       virtual void produce(edm::Event & iEvent, const edm::EventSetup & iSetup);
00047 
00048       bool isSameMuon(const T &mu1, const T &mu2) const {
00049         return (& mu1 == & mu2)  ||
00050                (mu1.reco::Muon::innerTrack().isNonnull() ?
00051                        mu1.reco::Muon::innerTrack() == mu2.reco::Muon::innerTrack() :
00052                        mu1.reco::Muon::outerTrack() == mu2.reco::Muon::outerTrack());
00053       }
00054       bool isBetterMuon(const T &mu1, const T &mu2) const ;
00055     private:
00057       edm::InputTag src_;
00058 
00060       StringCutObjectSelector<T> preselection_;
00062       StringCutObjectSelector<T> passthrough_;
00063    
00065       double sharedFraction_;
00066 
00068       bool defaultBestMuon_;
00070       typedef std::pair<const reco::Muon *, const reco::Muon *> MuonPointerPair;
00071       StringCutObjectSelector<MuonPointerPair, true> bestMuonSelector_;
00072   };
00073 
00074   template<>
00075   bool
00076   MuonCleanerBySegmentsT<pat::Muon>::isSameMuon(const pat::Muon &mu1, const pat::Muon &mu2) const ;
00077 } // namespace
00078 
00079 template<typename T>
00080 modules::MuonCleanerBySegmentsT<T>::MuonCleanerBySegmentsT(const edm::ParameterSet & iConfig) :
00081     src_(iConfig.getParameter<edm::InputTag>("src")),
00082     preselection_(iConfig.existsAs<std::string>("preselection") ? iConfig.getParameter<std::string>("preselection") : ""),
00083     passthrough_(iConfig.existsAs<std::string>("passthrough") ? iConfig.getParameter<std::string>("passthrough") : "0"),
00084     sharedFraction_(iConfig.getParameter<double>("fractionOfSharedSegments")),
00085     defaultBestMuon_(!iConfig.existsAs<std::string>("customArbitration")),
00086     bestMuonSelector_(defaultBestMuon_ ? std::string("") : iConfig.getParameter<std::string>("customArbitration"))
00087 {
00088     // this is the basic output (edm::Association is not generic)
00089     produces<std::vector<T> >(); 
00090 }
00091 
00092 template<typename T>
00093 void 
00094 modules::MuonCleanerBySegmentsT<T>::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00095     using namespace edm;
00096     using namespace std;
00097 
00098     Handle<View<T> > src;
00099     auto_ptr<vector<T> > out(new vector<T>());
00100 
00101     iEvent.getByLabel(src_, src);
00102     unsigned int nsrc = src->size();
00103     out->reserve(nsrc);
00104     std::vector<int> good(nsrc, true);
00105     for (unsigned int i = 0; i < nsrc; ++i) {
00106         const T &mu1 = (*src)[i];
00107         if (!preselection_(mu1)) good[i] = false; 
00108         if (!good[i]) continue;
00109         int  nSegments1 = mu1.numberOfMatches(reco::Muon::SegmentArbitration);
00110         for (unsigned int j = i+1; j < nsrc; ++j) {
00111             const T &mu2 = (*src)[j];
00112             if (isSameMuon(mu1,mu2)) continue;
00113             if (!good[j] || !preselection_(mu2)) continue;
00114             int nSegments2 = mu2.numberOfMatches(reco::Muon::SegmentArbitration);
00115             if (nSegments2 == 0 || nSegments1 == 0) continue;
00116             double sf = muon::sharedSegments(mu1,mu2)/std::min<double>(nSegments1,nSegments2);
00117             if (sf > sharedFraction_) {
00118                 if (isBetterMuon(mu1,mu2)) {
00119                     good[j] = false;
00120                 } else {
00121                     good[i] = false;
00122                 }
00123             }
00124         }
00125     }
00126     for (unsigned int i = 0; i < nsrc; ++i) {
00127         const T &mu1 = (*src)[i];
00128         if (good[i] || passthrough_(mu1)) out->push_back(mu1);
00129     }
00130     iEvent.put(out);
00131 }
00132 
00133 template<typename T>
00134 bool
00135 modules::MuonCleanerBySegmentsT<T>::isBetterMuon(const T &mu1, const T &mu2) const {
00136     if (!defaultBestMuon_) {
00137         MuonPointerPair pair = { &mu1, &mu2 };
00138         return bestMuonSelector_(pair);
00139     }
00140     if (mu2.track().isNull()) return true;
00141     if (mu1.track().isNull()) return false;
00142     if (mu1.isPFMuon()     != mu2.isPFMuon())     return mu1.isPFMuon();
00143     if (mu1.isGlobalMuon() != mu2.isGlobalMuon()) return mu1.isGlobalMuon();
00144     if (mu1.charge() == mu2.charge() && deltaR2(mu1,mu2) < 0.0009) {
00145         return mu1.track()->ptError()/mu1.track()->pt() < mu2.track()->ptError()/mu2.track()->pt();
00146     } else {
00147         int nm1 = mu1.numberOfMatches(reco::Muon::SegmentArbitration);
00148         int nm2 = mu2.numberOfMatches(reco::Muon::SegmentArbitration);
00149         return (nm1 != nm2 ? nm1 > nm2 : mu1.pt() > mu2.pt());
00150     }
00151 }
00152 
00153 template<>
00154 bool
00155 modules::MuonCleanerBySegmentsT<pat::Muon>::isSameMuon(const pat::Muon &mu1, const pat::Muon &mu2) const {
00156     return (& mu1 == & mu2)  ||
00157            (mu1.originalObjectRef() == mu2.originalObjectRef()) ||
00158            (mu1.reco::Muon::innerTrack().isNonnull() ?
00159                    mu1.reco::Muon::innerTrack() == mu2.reco::Muon::innerTrack() :
00160                    mu1.reco::Muon::outerTrack() == mu2.reco::Muon::outerTrack());
00161 }
00162 
00163 namespace modules {
00164     typedef modules::MuonCleanerBySegmentsT<reco::Muon>  MuonCleanerBySegments;
00165     typedef modules::MuonCleanerBySegmentsT<pat::Muon>   PATMuonCleanerBySegments;
00166 }
00167 
00168 #include "FWCore/Framework/interface/MakerMacros.h"
00169 using namespace modules;
00170 DEFINE_FWK_MODULE(MuonCleanerBySegments);
00171 DEFINE_FWK_MODULE(PATMuonCleanerBySegments);