Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:21:28

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