Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:37:04

0001 #include <vector>
0002 #include <numeric>
0003 
0004 ////////////////////
0005 // FRAMEWORK HEADERS
0006 #include "FWCore/Framework/interface/global/EDProducer.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Utilities/interface/InputTag.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 
0011 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0013 
0014 #include "DataFormats/L1TParticleFlow/interface/PFCandidate.h"
0015 #include "DataFormats/L1TParticleFlow/interface/PFTau.h"
0016 #include "DataFormats/JetReco/interface/CaloJet.h"
0017 // bitwise emulation headers
0018 #include "L1Trigger/Phase2L1ParticleFlow/interface/taus/L1HPSPFTauEmulator.h"
0019 
0020 class L1HPSPFTauProducer : public edm::global::EDProducer<> {
0021 public:
0022   explicit L1HPSPFTauProducer(const edm::ParameterSet&);
0023   ~L1HPSPFTauProducer() override {}
0024   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0025 
0026 private:
0027   void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
0028 
0029   //various needed vars
0030   int nTaus_;
0031   bool HW_;
0032   bool fUseJets_;
0033   bool debug_;
0034 
0035   edm::EDGetTokenT<l1t::PFCandidateCollection> tokenL1PFCands_;
0036   //jets
0037   edm::EDGetTokenT<std::vector<reco::CaloJet>> tokenL1PFJets_;
0038   //functions
0039   std::vector<l1t::PFTau> processEvent_HW(std::vector<edm::Ptr<l1t::PFCandidate>>& parts,
0040                                           std::vector<edm::Ptr<reco::CaloJet>>& jets) const;
0041 
0042   static std::vector<l1HPSPFTauEmu::Particle> convertJetsToHW(std::vector<edm::Ptr<reco::CaloJet>>& edmJets);
0043   static std::vector<l1HPSPFTauEmu::Particle> convertEDMToHW(std::vector<edm::Ptr<l1t::PFCandidate>>& edmParticles);
0044   static std::vector<l1t::PFTau> convertHWToEDM(std::vector<l1HPSPFTauEmu::Tau> hwTaus);
0045   //ADD
0046   edm::EDPutTokenT<l1t::PFTauCollection> tauToken_;
0047 };
0048 
0049 L1HPSPFTauProducer::L1HPSPFTauProducer(const edm::ParameterSet& cfg)
0050     : nTaus_(cfg.getParameter<int>("nTaus")),
0051       HW_(cfg.getParameter<bool>("HW")),
0052       fUseJets_(cfg.getParameter<bool>("useJets")),
0053       debug_(cfg.getParameter<bool>("debug")),
0054       tokenL1PFCands_(consumes(cfg.getParameter<edm::InputTag>("srcL1PFCands"))),
0055       tokenL1PFJets_(consumes(cfg.getParameter<edm::InputTag>("srcL1PFJets"))),
0056       tauToken_{produces()} {}
0057 
0058 void L1HPSPFTauProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0059   edm::ParameterSetDescription desc;
0060 
0061   desc.add<edm::InputTag>("srcL1PFCands", edm::InputTag("l1tLayer1", "Puppi"));
0062   desc.add<int>("nTaus", 16);
0063   desc.add<bool>("HW", true);
0064   desc.add<bool>("useJets", false);
0065   desc.add<bool>("debug", false);
0066   desc.add<edm::InputTag>("srcL1PFJets",
0067                           edm::InputTag("l1tPhase1JetCalibrator9x9trimmed", "Phase1L1TJetFromPfCandidates"));
0068   descriptions.add("l1tHPSPFTauProducer", desc);
0069 }
0070 
0071 void L1HPSPFTauProducer::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0072   auto l1PFCandidates = iEvent.getHandle(tokenL1PFCands_);
0073   //add jets even if not used, for simplicity
0074   auto l1PFJets = iEvent.getHandle(tokenL1PFJets_);
0075   //
0076 
0077   //adding collection
0078   std::vector<edm::Ptr<l1t::PFCandidate>> particles;
0079   for (unsigned i = 0; i < (*l1PFCandidates).size(); i++) {
0080     particles.push_back(edm::Ptr<l1t::PFCandidate>(l1PFCandidates, i));
0081   }
0082 
0083   //get the jets
0084   std::vector<edm::Ptr<reco::CaloJet>> jets;
0085   for (unsigned int i = 0; i < (*l1PFJets).size(); i++) {
0086     jets.push_back(edm::Ptr<reco::CaloJet>(l1PFJets, i));
0087     //
0088   }
0089 
0090   std::vector<l1t::PFTau> taus;
0091 
0092   taus = processEvent_HW(particles, jets);
0093 
0094   std::sort(taus.begin(), taus.end(), [](const l1t::PFTau& i, const l1t::PFTau& j) { return (i.pt() > j.pt()); });
0095 
0096   iEvent.emplace(tauToken_, std::move(taus));
0097 }
0098 
0099 std::vector<l1t::PFTau> L1HPSPFTauProducer::processEvent_HW(std::vector<edm::Ptr<l1t::PFCandidate>>& work,
0100                                                             std::vector<edm::Ptr<reco::CaloJet>>& jwork) const {
0101   //convert and call emulator
0102 
0103   using namespace l1HPSPFTauEmu;
0104 
0105   std::vector<Particle> particles = convertEDMToHW(work);
0106 
0107   std::vector<Particle> jets = convertJetsToHW(jwork);
0108   //also need to pass the jet enabler
0109 
0110   bool jEnable = fUseJets_;
0111 
0112   std::vector<Tau> taus = emulateEvent(particles, jets, jEnable);
0113 
0114   return convertHWToEDM(taus);
0115 }
0116 
0117 std::vector<l1HPSPFTauEmu::Particle> L1HPSPFTauProducer::convertJetsToHW(std::vector<edm::Ptr<reco::CaloJet>>& edmJets) {
0118   using namespace l1HPSPFTauEmu;
0119   std::vector<Particle> hwJets;
0120   std::for_each(edmJets.begin(), edmJets.end(), [&](edm::Ptr<reco::CaloJet>& edmJet) {
0121     l1HPSPFTauEmu::Particle jPart;
0122     jPart.hwPt = l1ct::Scales::makePtFromFloat(edmJet->pt());
0123     jPart.hwEta = edmJet->eta() * etaphi_base;
0124     jPart.hwPhi = edmJet->phi() * etaphi_base;
0125     jPart.tempZ0 = 0.;
0126     hwJets.push_back(jPart);
0127   });
0128   return hwJets;
0129 }
0130 
0131 //conversion to and from HW bitwise
0132 std::vector<l1HPSPFTauEmu::Particle> L1HPSPFTauProducer::convertEDMToHW(
0133     std::vector<edm::Ptr<l1t::PFCandidate>>& edmParticles) {
0134   using namespace l1HPSPFTauEmu;
0135   std::vector<Particle> hwParticles;
0136 
0137   std::for_each(edmParticles.begin(), edmParticles.end(), [&](edm::Ptr<l1t::PFCandidate>& edmParticle) {
0138     Particle hwPart;
0139     hwPart.hwPt = l1ct::Scales::makePtFromFloat(edmParticle->pt());
0140     hwPart.hwEta = edmParticle->eta() * etaphi_base;
0141     hwPart.hwPhi = edmParticle->phi() * etaphi_base;
0142     hwPart.pID = edmParticle->id();
0143     if (edmParticle->z0()) {
0144       hwPart.tempZ0 = edmParticle->z0() / dz_base;
0145     }
0146     hwParticles.push_back(hwPart);
0147   });
0148   return hwParticles;
0149 }
0150 
0151 std::vector<l1t::PFTau> L1HPSPFTauProducer::convertHWToEDM(std::vector<l1HPSPFTauEmu::Tau> hwTaus) {
0152   using namespace l1HPSPFTauEmu;
0153   std::vector<l1t::PFTau> edmTaus;
0154 
0155   //empty array for the PFTau format, since it's used for PuppiTaus but not here
0156   float tauArray[80] = {0};
0157   std::for_each(hwTaus.begin(), hwTaus.end(), [&](Tau tau) {
0158     l1gt::Tau gtTau = tau.toGT();
0159     l1gt::PackedTau packTau = gtTau.pack();
0160 
0161     l1t::PFTau pTau(
0162         reco::Candidate::PolarLorentzVector(
0163             l1ct::Scales::floatPt(tau.hwPt), float(tau.hwEta) / etaphi_base, float(tau.hwPhi) / etaphi_base, 0),
0164         tauArray,
0165         0,
0166         0,
0167         0,
0168         tau.hwPt,
0169         tau.hwEta,
0170         tau.hwPhi);
0171     pTau.set_encodedTau(packTau);
0172     edmTaus.push_back(pTau);
0173   });
0174   return edmTaus;
0175 }
0176 
0177 #include "FWCore/Framework/interface/MakerMacros.h"
0178 DEFINE_FWK_MODULE(L1HPSPFTauProducer);