Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:51

0001 /**
0002   Take as input:
0003      - the old PFCandidate collection (needed to setup the output ValueMap)
0004      - one ValueMap<reco::PFCandidateRef> that maps old PF candidates into new PF candidates (in multiple collections)
0005      - many edm::Association<pat::PackedCandidateCollection> that map new PF candidates into packed candidates
0006   Produce as output:
0007      - one ValueMap<reco::CandidatePtr> that maps the old PF candidates into the new packed PF candidates
0008 */
0009 
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/Framework/interface/stream/EDProducer.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "DataFormats/PatCandidates/interface/PackedCandidate.h"
0015 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
0016 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0017 #include "DataFormats/Common/interface/Association.h"
0018 #include "DataFormats/Common/interface/RefToPtr.h"
0019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0020 
0021 namespace pat {
0022   class PackedPFCandidateRefMixer : public edm::stream::EDProducer<> {
0023   public:
0024     explicit PackedPFCandidateRefMixer(const edm::ParameterSet& iConfig);
0025     ~PackedPFCandidateRefMixer() override {}
0026 
0027     void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0028 
0029   private:
0030     edm::EDGetTokenT<std::vector<reco::PFCandidate>> pf_;
0031     edm::EDGetTokenT<edm::ValueMap<reco::PFCandidateRef>> pf2pf_;
0032     std::vector<edm::EDGetTokenT<edm::Association<pat::PackedCandidateCollection>>> pf2pcs_;
0033   };
0034 
0035 }  // namespace pat
0036 
0037 pat::PackedPFCandidateRefMixer::PackedPFCandidateRefMixer(const edm::ParameterSet& iConfig)
0038     : pf_(consumes<std::vector<reco::PFCandidate>>(iConfig.getParameter<edm::InputTag>("pf"))),
0039       pf2pf_(consumes<edm::ValueMap<reco::PFCandidateRef>>(iConfig.getParameter<edm::InputTag>("pf2pf"))) {
0040   for (edm::InputTag const& tag : iConfig.getParameter<std::vector<edm::InputTag>>("pf2packed")) {
0041     pf2pcs_.push_back(consumes<edm::Association<pat::PackedCandidateCollection>>(tag));
0042   }
0043   produces<edm::ValueMap<reco::CandidatePtr>>();
0044 }
0045 
0046 void pat::PackedPFCandidateRefMixer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0047   edm::Handle<std::vector<reco::PFCandidate>> pf;
0048   edm::Handle<edm::ValueMap<reco::PFCandidateRef>> pf2pf;
0049   std::vector<edm::Handle<edm::Association<pat::PackedCandidateCollection>>> pf2pcs(pf2pcs_.size());
0050   iEvent.getByToken(pf_, pf);
0051   iEvent.getByToken(pf2pf_, pf2pf);
0052   for (unsigned int i = 0, n = pf2pcs.size(); i < n; ++i) {
0053     iEvent.getByToken(pf2pcs_[i], pf2pcs[i]);
0054   }
0055   std::vector<reco::CandidatePtr> outptrs;
0056   outptrs.reserve(pf->size());
0057   for (unsigned int i = 0, n = pf->size(); i < n; ++i) {
0058     reco::PFCandidateRef oldpfRef(pf, i);
0059     const auto& newpfRef = (*pf2pf)[oldpfRef];
0060     bool found = false;
0061     for (const auto& pf2pc : pf2pcs) {
0062       if (pf2pc->contains(newpfRef.id())) {
0063         outptrs.push_back(refToPtr((*pf2pc)[newpfRef]));
0064         found = true;
0065         break;
0066       }
0067     }
0068     if (!found) {
0069       edm::LogPrint("PackedPFCandidateRefMixer") << "oldpfRef: " << oldpfRef.id() << " / " << oldpfRef.key() << "\n";
0070       edm::LogPrint("PackedPFCandidateRefMixer") << "newpfRef: " << newpfRef.id() << " / " << newpfRef.key() << "\n";
0071       edm::LogPrint("PackedPFCandidateRefMixer") << "and I have " << pf2pcs.size() << " rekey maps."
0072                                                  << "\n";
0073       for (const auto& pf2pc : pf2pcs) {
0074         edm::LogPrint("PackedPFCandidateRefMixer") << "this map has keys in: "
0075                                                    << "\n";
0076         for (const auto& pair : pf2pc->ids()) {
0077           edm::LogPrint("PackedPFCandidateRefMixer") << "\t" << pair.first << "\n";
0078         }
0079       }
0080       throw cms::Exception("LogicError") << "A packed candidate has refs that we don't understand\n";
0081     }
0082   }
0083   std::unique_ptr<edm::ValueMap<reco::CandidatePtr>> out(new edm::ValueMap<reco::CandidatePtr>());
0084   edm::ValueMap<reco::CandidatePtr>::Filler filler(*out);
0085   filler.insert(pf, outptrs.begin(), outptrs.end());
0086   filler.fill();
0087   iEvent.put(std::move(out));
0088 }
0089 
0090 #include "FWCore/Framework/interface/MakerMacros.h"
0091 using namespace pat;
0092 DEFINE_FWK_MODULE(PackedPFCandidateRefMixer);