Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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