File indexing completed on 2022-10-13 03:38:20
0001 #include <unordered_map>
0002
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "FWCore/Framework/interface/stream/EDProducer.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/Utilities/interface/InputTag.h"
0007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0008 #include "FWCore/Framework/interface/Frameworkfwd.h"
0009 #include "FWCore/Framework/interface/MakerMacros.h"
0010
0011 #include "DataFormats/L1TParticleFlow/interface/PFCandidate.h"
0012
0013 #include "L1Trigger/Phase2L1ParticleFlow/interface/deregionizer/deregionizer_input.h"
0014 #include "L1Trigger/Phase2L1ParticleFlow/interface/deregionizer/deregionizer_ref.h"
0015
0016 class DeregionizerProducer : public edm::stream::EDProducer<> {
0017 public:
0018 explicit DeregionizerProducer(const edm::ParameterSet &);
0019 ~DeregionizerProducer() override;
0020 static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
0021
0022 private:
0023 edm::ParameterSet config_;
0024 edm::EDGetTokenT<l1t::PFCandidateRegionalOutput> token_;
0025 l1ct::DeregionizerEmulator emulator_;
0026
0027 std::unordered_map<const l1t::PFCandidate *, l1t::PFClusterRef> clusterRefMap_;
0028 std::unordered_map<const l1t::PFCandidate *, l1t::PFTrackRef> trackRefMap_;
0029 std::unordered_map<const l1t::PFCandidate *, l1t::PFCandidate::MuonRef> muonRefMap_;
0030
0031 void produce(edm::Event &, const edm::EventSetup &) override;
0032 void hwToEdm_(const std::vector<l1ct::PuppiObjEmu> &hwOut, std::vector<l1t::PFCandidate> &edmOut) const;
0033 void setRefs_(l1t::PFCandidate &pf, const l1ct::PuppiObjEmu &p) const;
0034 };
0035
0036 DeregionizerProducer::DeregionizerProducer(const edm::ParameterSet &iConfig)
0037 : config_(iConfig),
0038 token_(consumes<l1t::PFCandidateRegionalOutput>(iConfig.getParameter<edm::InputTag>("RegionalPuppiCands"))),
0039 emulator_(iConfig) {
0040 produces<l1t::PFCandidateCollection>("Puppi");
0041 produces<l1t::PFCandidateCollection>("TruncatedPuppi");
0042 }
0043
0044 DeregionizerProducer::~DeregionizerProducer() {}
0045
0046 void DeregionizerProducer::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0047 clusterRefMap_.clear();
0048 trackRefMap_.clear();
0049 muonRefMap_.clear();
0050
0051 auto deregColl = std::make_unique<l1t::PFCandidateCollection>();
0052 auto truncColl = std::make_unique<l1t::PFCandidateCollection>();
0053
0054 edm::Handle<l1t::PFCandidateRegionalOutput> src;
0055
0056 iEvent.getByToken(token_, src);
0057
0058 std::vector<float> regionEtas, regionPhis;
0059 std::vector<l1ct::OutputRegion> outputRegions;
0060 std::vector<l1ct::PuppiObjEmu> hwOut;
0061 std::vector<l1t::PFCandidate> edmOut;
0062 std::vector<l1ct::PuppiObjEmu> hwTruncOut;
0063 std::vector<l1t::PFCandidate> edmTruncOut;
0064
0065 LogDebug("DeregionizerProducer") << "\nRegional Puppi Candidates";
0066 for (unsigned int iReg = 0, nReg = src->nRegions(); iReg < nReg; ++iReg) {
0067 l1ct::OutputRegion tempOutputRegion;
0068
0069 auto region = src->region(iReg);
0070 float eta = src->eta(iReg);
0071 float phi = src->phi(iReg);
0072 LogDebug("DeregionizerProducer") << "\nRegion " << iReg << "\n"
0073 << "Eta = " << eta << " and Phi = " << phi << "\n"
0074 << "###########";
0075 for (int i = 0, n = region.size(); i < n; ++i) {
0076 l1ct::PuppiObjEmu tempPuppi;
0077 const l1t::PFCandidate &cand = region[i];
0078 clusterRefMap_[&cand] = cand.pfCluster();
0079 trackRefMap_[&cand] = cand.pfTrack();
0080 muonRefMap_[&cand] = cand.muon();
0081
0082 tempPuppi.initFromBits(cand.encodedPuppi64());
0083 tempPuppi.srcCand = &cand;
0084 tempOutputRegion.puppi.push_back(tempPuppi);
0085 LogDebug("DeregionizerProducer") << "pt[" << i << "] = " << tempOutputRegion.puppi.back().hwPt << ", eta[" << i
0086 << "] = " << tempOutputRegion.puppi.back().floatEta() << ", phi[" << i
0087 << "] = " << tempOutputRegion.puppi.back().floatPhi();
0088 }
0089 if (!tempOutputRegion.puppi.empty()) {
0090 regionEtas.push_back(eta);
0091 regionPhis.push_back(phi);
0092 outputRegions.push_back(tempOutputRegion);
0093 }
0094 }
0095
0096 l1ct::DeregionizerInput in = l1ct::DeregionizerInput(regionEtas, regionPhis, outputRegions);
0097
0098 emulator_.run(in, hwOut, hwTruncOut);
0099
0100 DeregionizerProducer::hwToEdm_(hwOut, edmOut);
0101 DeregionizerProducer::hwToEdm_(hwTruncOut, edmTruncOut);
0102
0103 deregColl->swap(edmOut);
0104 truncColl->swap(edmTruncOut);
0105
0106 iEvent.put(std::move(deregColl), "Puppi");
0107 iEvent.put(std::move(truncColl), "TruncatedPuppi");
0108 }
0109
0110 void DeregionizerProducer::hwToEdm_(const std::vector<l1ct::PuppiObjEmu> &hwOut,
0111 std::vector<l1t::PFCandidate> &edmOut) const {
0112 for (const auto &hwPuppi : hwOut) {
0113 l1t::PFCandidate::ParticleType type;
0114 float mass = 0.13f;
0115 if (hwPuppi.hwId.charged()) {
0116 if (hwPuppi.hwId.isMuon()) {
0117 type = l1t::PFCandidate::Muon;
0118 mass = 0.105;
0119 } else if (hwPuppi.hwId.isElectron()) {
0120 type = l1t::PFCandidate::Electron;
0121 mass = 0.005;
0122 } else
0123 type = l1t::PFCandidate::ChargedHadron;
0124 } else {
0125 type = hwPuppi.hwId.isPhoton() ? l1t::PFCandidate::Photon : l1t::PFCandidate::NeutralHadron;
0126 mass = hwPuppi.hwId.isPhoton() ? 0.0 : 0.5;
0127 }
0128 reco::Particle::PolarLorentzVector p4(hwPuppi.floatPt(), hwPuppi.floatEta(), hwPuppi.floatPhi(), mass);
0129 edmOut.emplace_back(
0130 type, hwPuppi.intCharge(), p4, hwPuppi.floatPuppiW(), hwPuppi.intPt(), hwPuppi.intEta(), hwPuppi.intPhi());
0131 if (hwPuppi.hwId.charged()) {
0132 edmOut.back().setZ0(hwPuppi.floatZ0());
0133 edmOut.back().setDxy(hwPuppi.floatDxy());
0134 edmOut.back().setHwZ0(hwPuppi.hwZ0());
0135 edmOut.back().setHwDxy(hwPuppi.hwDxy());
0136 edmOut.back().setHwTkQuality(hwPuppi.hwTkQuality());
0137 } else {
0138 edmOut.back().setHwPuppiWeight(hwPuppi.hwPuppiW());
0139 edmOut.back().setHwEmID(hwPuppi.hwEmID());
0140 }
0141 edmOut.back().setEncodedPuppi64(hwPuppi.pack().to_uint64());
0142 setRefs_(edmOut.back(), hwPuppi);
0143 }
0144 }
0145
0146 void DeregionizerProducer::setRefs_(l1t::PFCandidate &pf, const l1ct::PuppiObjEmu &p) const {
0147 if (p.srcCand) {
0148 auto match = clusterRefMap_.find(p.srcCand);
0149 if (match == clusterRefMap_.end()) {
0150 throw cms::Exception("CorruptData") << "Invalid cluster pointer in PF candidate id " << p.intId() << " pt "
0151 << p.floatPt() << " eta " << p.floatEta() << " phi " << p.floatPhi();
0152 }
0153 pf.setPFCluster(match->second);
0154 }
0155 if (p.srcCand) {
0156 auto match = trackRefMap_.find(p.srcCand);
0157 if (match == trackRefMap_.end()) {
0158 throw cms::Exception("CorruptData") << "Invalid track pointer in PF candidate id " << p.intId() << " pt "
0159 << p.floatPt() << " eta " << p.floatEta() << " phi " << p.floatPhi();
0160 }
0161 pf.setPFTrack(match->second);
0162 }
0163 if (p.srcCand) {
0164 auto match = muonRefMap_.find(p.srcCand);
0165 if (match == muonRefMap_.end()) {
0166 throw cms::Exception("CorruptData") << "Invalid muon pointer in PF candidate id " << p.intId() << " pt "
0167 << p.floatPt() << " eta " << p.floatEta() << " phi " << p.floatPhi();
0168 }
0169 pf.setMuon(match->second);
0170 }
0171 }
0172
0173 void DeregionizerProducer::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
0174
0175 edm::ParameterSetDescription desc;
0176 desc.add<edm::InputTag>("RegionalPuppiCands", edm::InputTag("l1tLayer1", "PuppiRegional"));
0177 desc.add<unsigned int>("nPuppiFinalBuffer", 128);
0178 desc.add<unsigned int>("nPuppiPerClk", 6);
0179 desc.add<unsigned int>("nPuppiFirstBuffers", 12);
0180 desc.add<unsigned int>("nPuppiSecondBuffers", 32);
0181 desc.add<unsigned int>("nPuppiThirdBuffers", 64);
0182 descriptions.add("DeregionizerProducer", desc);
0183
0184
0185 }
0186
0187 #include "FWCore/Framework/interface/MakerMacros.h"
0188 DEFINE_FWK_MODULE(DeregionizerProducer);