Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:47

0001 // -*- C++ -*-
0002 //
0003 // Package:    MuonAnalysis/MuonAssociators
0004 // Class:      MatcherByPulls
0005 //
0006 /**\class pat::MatcherByPulls MatcherByPulls.cc MuonAnalysis/MuonAssociators/src/MatcherByPulls.cc
0007 
0008  Description: Matches RecoCandidates to (Gen)Particles by looking at the pulls fo the track parameters.
0009               Produces as output an edm::Association to GenParticles, and a ValueMap with the pull values.
0010 
0011  Implementation: uses MatcherByPullsAlgorithm
0012                  the module is in the pat namespace, but it doesn't have any PAT dependency.
0013 */
0014 //
0015 // Original Author:  Giovanni Petrucciani (SNS Pisa and CERN PH-CMG)
0016 //         Created:  Sun Nov 16 16:14:09 CET 2008
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/stream/EDProducer.h"
0025 
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028 
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 
0031 #include "DataFormats/Common/interface/Association.h"
0032 #include "DataFormats/Common/interface/View.h"
0033 #include "DataFormats/Common/interface/RefProd.h"
0034 
0035 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0036 
0037 #include "MuonAnalysis/MuonAssociators/interface/MatcherByPullsAlgorithm.h"
0038 
0039 /*     ____ _                     _           _                 _   _
0040  *    / ___| | __ _ ___ ___    __| | ___  ___| | __ _ _ __ __ _| |_(_) ___  _ __
0041  *   | |   | |/ _` / __/ __|  / _` |/ _ \/ __| |/ _` | '__/ _` | __| |/ _ \| '_ \
0042  *   | |___| | (_| \__ \__ \ | (_| |  __/ (__| | (_| | | | (_| | |_| | (_) | | | |
0043  *    \____|_|\__,_|___/___/  \__,_|\___|\___|_|\__,_|_|  \__,_|\__|_|\___/|_| |_|
0044  */
0045 namespace pat {
0046   template <typename T>
0047   class MatcherByPulls : public edm::stream::EDProducer<> {
0048   public:
0049     explicit MatcherByPulls(const edm::ParameterSet&);
0050     ~MatcherByPulls() override;
0051 
0052   private:
0053     void produce(edm::Event&, const edm::EventSetup&) override;
0054 
0055     /// The RECO objects
0056     edm::EDGetTokenT<edm::View<T> > srcToken_;
0057 
0058     /// The MC objects to match against
0059     edm::EDGetTokenT<std::vector<reco::GenParticle> > matchedToken_;
0060 
0061     /// Preselection cut on MC objects
0062     StringCutObjectSelector<reco::GenParticle> mcSel_;
0063 
0064     MatcherByPullsAlgorithm algo_;
0065   };
0066 }  // namespace pat
0067 
0068 /*     ____                _                   _
0069  *    / ___|___  _ __  ___| |_ _ __ _   _  ___| |_ ___  _ __
0070  *   | |   / _ \| '_ \/ __| __| '__| | | |/ __| __/ _ \| '__|
0071  *   | |__| (_) | | | \__ \ |_| |  | |_| | (__| || (_) | |
0072  *    \____\___/|_| |_|___/\__|_|   \__,_|\___|\__\___/|_|
0073  *
0074  */
0075 template <typename T>
0076 pat::MatcherByPulls<T>::MatcherByPulls(const edm::ParameterSet& iConfig)
0077     : srcToken_(consumes<edm::View<T> >(iConfig.getParameter<edm::InputTag>("src"))),
0078       matchedToken_(consumes<std::vector<reco::GenParticle> >(iConfig.getParameter<edm::InputTag>("matched"))),
0079       mcSel_(iConfig.getParameter<std::string>("matchedSelector")),
0080       algo_(iConfig) {
0081   produces<edm::Association<std::vector<reco::GenParticle> > >();
0082   produces<edm::ValueMap<float> >("pulls");
0083 }
0084 
0085 template <typename T>
0086 pat::MatcherByPulls<T>::~MatcherByPulls() {}
0087 
0088 /*    ____                _
0089  *   |  _ \ _ __ ___   __| |_   _  ___ ___
0090  *   | |_) | '__/ _ \ / _` | | | |/ __/ _ \
0091  *   |  __/| | | (_) | (_| | |_| | (_|  __/
0092  *   |_|   |_|  \___/ \__,_|\__,_|\___\___|
0093  *
0094  */
0095 
0096 template <typename T>
0097 void pat::MatcherByPulls<T>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0098   typedef std::vector<reco::GenParticle> MCColl;
0099   edm::Handle<edm::View<T> > src;
0100   edm::Handle<MCColl> cands;
0101   iEvent.getByToken(srcToken_, src);
0102   iEvent.getByToken(matchedToken_, cands);
0103 
0104   std::vector<uint8_t> candGood(cands->size(), 1);
0105   std::transform(cands->begin(), cands->end(), candGood.begin(), mcSel_);
0106 
0107   std::vector<int> matches(src->size(), -1);
0108   std::vector<float> pulls(src->size(), 1e39);
0109   for (size_t i = 0, n = src->size(); i < n; ++i) {
0110     const T& tk = (*src)[i];
0111     std::pair<int, float> m = algo_.match(tk, *cands, candGood);
0112     matches[i] = m.first;
0113     pulls[i] = m.second;
0114   }
0115 
0116   typedef edm::Association<MCColl> MCAsso;
0117   std::unique_ptr<MCAsso> matchesMap(new MCAsso(edm::RefProd<MCColl>(cands)));
0118   MCAsso::Filler matchesFiller(*matchesMap);
0119   matchesFiller.insert(src, matches.begin(), matches.end());
0120   matchesFiller.fill();
0121   iEvent.put(std::move(matchesMap));
0122 
0123   std::unique_ptr<edm::ValueMap<float> > pullsMap(new edm::ValueMap<float>());
0124   edm::ValueMap<float>::Filler pullsFiller(*pullsMap);
0125   pullsFiller.insert(src, pulls.begin(), pulls.end());
0126   pullsFiller.fill();
0127   iEvent.put(std::move(pullsMap), "pulls");
0128 }
0129 
0130 //define this as a plug-in
0131 
0132 typedef pat::MatcherByPulls<reco::RecoCandidate> MatcherByPulls;
0133 typedef pat::MatcherByPulls<reco::Track> TrackMatcherByPulls;
0134 DEFINE_FWK_MODULE(MatcherByPulls);
0135 DEFINE_FWK_MODULE(TrackMatcherByPulls);