Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-01 01:02:11

0001 #ifndef FIRMWARE_PFTKEGSORTER_REF_H
0002 #define FIRMWARE_PFTKEGSORTER_REF_H
0003 
0004 #include <cstdio>
0005 #include <vector>
0006 
0007 #include "DataFormats/L1TParticleFlow/interface/layer1_emulator.h"
0008 #include "L1Trigger/Phase2L1ParticleFlow/interface/dbgPrintf.h"
0009 
0010 #ifdef CMSSW_GIT_HASH
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #endif
0013 
0014 namespace edm {
0015   class ParameterSet;
0016 }
0017 
0018 namespace l1ct {
0019   class PFTkEGSorterEmulator {
0020   public:
0021     PFTkEGSorterEmulator(const unsigned int nObjToSort = 6, const unsigned int nObjSorted = 16)
0022         : nObjToSort_(nObjToSort), nObjSorted_(nObjSorted), debug_(false) {}
0023 
0024 #ifdef CMSSW_GIT_HASH
0025     PFTkEGSorterEmulator(const edm::ParameterSet& iConfig)
0026         : PFTkEGSorterEmulator(iConfig.getParameter<uint32_t>("nObjToSort"),
0027                                iConfig.getParameter<uint32_t>("nObjSorted")) {}
0028 
0029 #endif
0030 
0031     ~PFTkEGSorterEmulator(){};
0032 
0033     void setDebug(bool debug = true) { debug_ = debug; };
0034 
0035     template <typename T>
0036     void run(const std::vector<l1ct::PFInputRegion>& pfregions,
0037              const std::vector<l1ct::OutputRegion>& outregions,
0038              const std::vector<unsigned int>& region_index,
0039              std::vector<T>& eg_sorted_inBoard) {
0040       std::vector<T> eg_unsorted_inBoard = eg_sorted_inBoard;
0041       mergeEGObjFromRegions<T>(pfregions, outregions, region_index, eg_unsorted_inBoard);
0042 
0043       if (debug_) {
0044         dbgCout() << "\nUNSORTED\n";
0045         for (int j = 0, nj = eg_unsorted_inBoard.size(); j < nj; j++)
0046           dbgCout() << "EG[" << j << "]: pt = " << eg_unsorted_inBoard[j].hwPt
0047                     << ",\t eta = " << eg_unsorted_inBoard[j].hwEta << ",\t phi = " << eg_unsorted_inBoard[j].hwPhi
0048                     << "\n";
0049       }
0050 
0051       if (debug_)
0052         dbgCout() << "\nSORTED\n";
0053 
0054       eg_sorted_inBoard = eg_unsorted_inBoard;
0055       std::reverse(eg_sorted_inBoard.begin(), eg_sorted_inBoard.end());
0056       std::stable_sort(eg_sorted_inBoard.begin(), eg_sorted_inBoard.end(), comparePt<T>);
0057       if (eg_sorted_inBoard.size() > nObjSorted_)
0058         eg_sorted_inBoard.resize(nObjSorted_);
0059 
0060       if (debug_) {
0061         for (int j = 0, nj = eg_sorted_inBoard.size(); j < nj; j++)
0062           dbgCout() << "EG[" << j << "]: pt = " << eg_sorted_inBoard[j].hwPt
0063                     << ",\t eta = " << eg_sorted_inBoard[j].hwEta << ",\t phi = " << eg_sorted_inBoard[j].hwPhi << "\n";
0064       }
0065     }
0066 
0067   private:
0068     unsigned int nObjToSort_, nObjSorted_;
0069     bool debug_;
0070 
0071     void extractEGObjEmu(const PFRegionEmu& region,
0072                          const l1ct::OutputRegion& outregion,
0073                          std::vector<l1ct::EGIsoObjEmu>& eg) {
0074       extractEGObjEmu(region, outregion.egphoton, eg);
0075     }
0076     void extractEGObjEmu(const PFRegionEmu& region,
0077                          const l1ct::OutputRegion& outregion,
0078                          std::vector<l1ct::EGIsoEleObjEmu>& eg) {
0079       extractEGObjEmu(region, outregion.egelectron, eg);
0080     }
0081 
0082     template <typename T>
0083     void extractEGObjEmu(const PFRegionEmu& region,
0084                          const std::vector<T>& regional_objects,
0085                          std::vector<T>& global_objects) {
0086       for (const auto& reg_obj : regional_objects) {
0087         global_objects.emplace_back(reg_obj);
0088         global_objects.back().hwEta = region.hwGlbEta(reg_obj.hwEta);
0089         global_objects.back().hwPhi = region.hwGlbPhi(reg_obj.hwPhi);
0090       }
0091     }
0092 
0093     template <typename T>
0094     static bool comparePt(T obj1, T obj2) {
0095       return (obj1.hwPt > obj2.hwPt);
0096     }
0097 
0098     template <typename T>
0099     void mergeEGObjFromRegions(const std::vector<l1ct::PFInputRegion>& pfregions,
0100                                const std::vector<l1ct::OutputRegion>& outregions,
0101                                const std::vector<unsigned int>& region_index,
0102                                std::vector<T>& eg_unsorted_inBoard) {
0103       for (unsigned int i : region_index) {
0104         const auto& region = pfregions[i].region;
0105 
0106         if (debug_)
0107           dbgCout() << "\nOutput Region " << i << ": eta = " << region.floatEtaCenter()
0108                     << " and phi = " << region.floatPhiCenter() << " \n";
0109 
0110         std::vector<T> eg_tmp;
0111         extractEGObjEmu(region, outregions[i], eg_tmp);
0112         for (int j = 0, nj = (eg_tmp.size() > nObjToSort_ ? nObjToSort_ : eg_tmp.size()); j < nj; j++) {
0113           if (debug_)
0114             dbgCout() << "EG[" << j << "] pt = " << eg_tmp[j].hwPt << ",\t eta = " << eg_tmp[j].hwEta
0115                       << ",\t phi = " << eg_tmp[j].hwPhi << "\n";
0116           eg_unsorted_inBoard.push_back(eg_tmp[j]);
0117         }
0118         if (debug_)
0119           dbgCout() << "\n";
0120       }
0121     }
0122   };
0123 }  // namespace l1ct
0124 
0125 #endif