Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-16 00:33:15

0001 /* \class MCTruthCompositeMatcher
0002  *
0003  * \author Luca Lista, INFN
0004  *
0005  */
0006 
0007 #include "FWCore/Framework/interface/global/EDProducer.h"
0008 #include "FWCore/Utilities/interface/InputTag.h"
0009 #include "CommonTools/CandUtils/interface/CandMatcherNew.h"
0010 #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
0011 #include "DataFormats/Common/interface/Association.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0014 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0015 #include "FWCore/Utilities/interface/transform.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/makeRefToBaseProdFrom.h"
0018 #include "DataFormats/Common/interface/Handle.h"
0019 
0020 namespace reco {
0021   namespace modulesNew {
0022 
0023     class MCTruthCompositeMatcher : public edm::global::EDProducer<> {
0024     public:
0025       explicit MCTruthCompositeMatcher(const edm::ParameterSet &);
0026 
0027       static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0028 
0029     private:
0030       edm::EDGetTokenT<CandidateView> srcToken_;
0031       std::vector<edm::EDGetTokenT<reco::GenParticleMatch>> matchMapTokens_;
0032       std::vector<int> pdgId_;
0033       void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override;
0034     };
0035 
0036     MCTruthCompositeMatcher::MCTruthCompositeMatcher(const edm::ParameterSet &cfg)
0037         : srcToken_(consumes<CandidateView>(cfg.getParameter<edm::InputTag>("src"))),
0038           matchMapTokens_(edm::vector_transform(
0039               cfg.template getParameter<std::vector<edm::InputTag>>("matchMaps"),
0040               [this](edm::InputTag const &tag) { return consumes<reco::GenParticleMatch>(tag); })),
0041           pdgId_(cfg.getParameter<std::vector<int>>("matchPDGId")) {
0042       produces<reco::GenParticleMatch>();
0043     }
0044 
0045     void MCTruthCompositeMatcher::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0046       edm::ParameterSetDescription desc;
0047       desc.add<edm::InputTag>("src");
0048       desc.add<std::vector<edm::InputTag>>("matchMaps");
0049       desc.add<std::vector<int>>("matchPDGId");
0050 
0051       descriptions.addDefault(desc);
0052     }
0053 
0054     void MCTruthCompositeMatcher::produce(edm::StreamID, edm::Event &evt, const edm::EventSetup &) const {
0055       using namespace edm;
0056       using namespace std;
0057       Handle<CandidateView> cands;
0058       evt.getByToken(srcToken_, cands);
0059       size_t nMaps = matchMapTokens_.size();
0060       std::vector<const GenParticleMatch *> maps;
0061       maps.reserve(nMaps);
0062       for (size_t i = 0; i != nMaps; ++i) {
0063         Handle<reco::GenParticleMatch> matchMap;
0064         evt.getByToken(matchMapTokens_[i], matchMap);
0065         maps.push_back(&*matchMap);
0066       }
0067       utilsNew::CandMatcher<GenParticleCollection> match(maps);
0068       auto matchMap = std::make_unique<GenParticleMatch>(match.ref());
0069       int size = cands->size();
0070       vector<int>::const_iterator begin = pdgId_.begin(), end = pdgId_.end();
0071       if (size != 0) {
0072         GenParticleMatch::Filler filler(*matchMap);
0073         vector<int> indices(size);
0074         for (int i = 0; i != size; ++i) {
0075           const Candidate &cand = (*cands)[i];
0076           GenParticleRef mc = match[cand];
0077           if (mc.isNull()) {
0078             indices[i] = -1;
0079           } else {
0080             bool found = true;
0081             if (begin != end)
0082               found = find(begin, end, std::abs(mc->pdgId())) != end;
0083             indices[i] = found ? int(mc.key()) : -1;
0084           }
0085         }
0086         CandidateBaseRefProd ref(edm::makeRefToBaseProdFrom(cands->refAt(0), evt));
0087         filler.insert(ref, indices.begin(), indices.end());
0088         filler.fill();
0089       }
0090       evt.put(std::move(matchMap));
0091     }
0092 
0093   }  // namespace modulesNew
0094 }  // namespace reco
0095 
0096 #include "DataFormats/Candidate/interface/Candidate.h"
0097 #include "FWCore/Framework/interface/MakerMacros.h"
0098 
0099 namespace reco {
0100   namespace modulesNew {
0101 
0102     typedef MCTruthCompositeMatcher MCTruthCompositeMatcherNew;
0103 
0104     DEFINE_FWK_MODULE(MCTruthCompositeMatcherNew);
0105 
0106   }  // namespace modulesNew
0107 }  // namespace reco