File indexing completed on 2023-09-12 05:12:15
0001 #ifndef L1Trigger_Phase2L1ParticleFlow_egamma_pftkegsorter_barrel_ref_h
0002 #define L1Trigger_Phase2L1ParticleFlow_egamma_pftkegsorter_barrel_ref_h
0003
0004 #include <cstdio>
0005 #include <vector>
0006
0007 #include "pftkegsorter_ref.h"
0008 #include "DataFormats/L1TParticleFlow/interface/layer1_emulator.h"
0009 #include "L1Trigger/Phase2L1ParticleFlow/interface/common/bitonic_hybrid_sort_ref.h"
0010
0011 namespace l1ct {
0012 class PFTkEGSorterBarrelEmulator : public PFTkEGSorterEmulator {
0013 public:
0014 PFTkEGSorterBarrelEmulator(const unsigned int nObjToSort = 10, const unsigned int nObjSorted = 16)
0015 : PFTkEGSorterEmulator(nObjToSort, nObjSorted) {}
0016
0017 #ifdef CMSSW_GIT_HASH
0018 PFTkEGSorterBarrelEmulator(const edm::ParameterSet& iConfig)
0019 : PFTkEGSorterEmulator(iConfig.getParameter<uint32_t>("nObjToSort"),
0020 iConfig.getParameter<uint32_t>("nObjSorted")) {}
0021
0022 static edm::ParameterSetDescription getParameterSetDescription() {
0023 return PFTkEGSorterEmulator::getParameterSetDescription();
0024 }
0025 #endif
0026
0027 ~PFTkEGSorterBarrelEmulator() override {}
0028
0029 void toFirmware_pho(const OutputRegion& outregions, EGIsoObj photons_in[]) const {
0030 for (unsigned int i = 0; i < nObjToSort_; i++) {
0031 EGIsoObj pho;
0032 if (i < outregions.egphoton.size()) {
0033 pho = outregions.egphoton[i];
0034 } else
0035 pho.clear();
0036
0037 photons_in[i] = pho;
0038 }
0039 }
0040
0041 void toFirmware_ele(const OutputRegion& outregions, EGIsoEleObj eles_in[]) const {
0042 for (unsigned int i = 0; i < nObjToSort_; i++) {
0043 EGIsoEleObj ele;
0044 if (i < outregions.egelectron.size()) {
0045 ele = outregions.egelectron[i];
0046 } else
0047 ele.clear();
0048
0049 eles_in[i] = ele;
0050 }
0051 }
0052
0053 void runPho(const std::vector<l1ct::PFInputRegion>& pfregions,
0054 const std::vector<l1ct::OutputRegion>& outregions,
0055 const std::vector<unsigned int>& region_index,
0056 std::vector<l1ct::EGIsoObjEmu>& eg_sorted_inBoard) override {
0057 run<l1ct::EGIsoObjEmu>(pfregions, outregions, region_index, eg_sorted_inBoard);
0058 }
0059 void runEle(const std::vector<l1ct::PFInputRegion>& pfregions,
0060 const std::vector<l1ct::OutputRegion>& outregions,
0061 const std::vector<unsigned int>& region_index,
0062 std::vector<l1ct::EGIsoEleObjEmu>& eg_sorted_inBoard) override {
0063 run<l1ct::EGIsoEleObjEmu>(pfregions, outregions, region_index, eg_sorted_inBoard);
0064 }
0065
0066 template <typename T>
0067 void run(const std::vector<PFInputRegion>& pfregions,
0068 const std::vector<OutputRegion>& outregions,
0069 const std::vector<unsigned int>& region_index,
0070 std::vector<T>& eg_sorted_inBoard) {
0071
0072 std::vector<std::vector<T>> objs_in;
0073 objs_in.reserve(nObjToSort_);
0074 for (unsigned int i : region_index) {
0075 std::vector<T> objs;
0076 extractEGObjEmu(pfregions[i].region, outregions[i], objs);
0077 if (debug_)
0078 dbgCout() << "objs size " << objs.size() << "\n";
0079 resize_input(objs);
0080 objs_in.push_back(objs);
0081 if (debug_)
0082 dbgCout() << "objs (re)size and total objs size " << objs.size() << " " << objs_in.size() << "\n";
0083 }
0084
0085 merge(objs_in, eg_sorted_inBoard);
0086
0087 if (debug_) {
0088 dbgCout() << "objs.size() size " << eg_sorted_inBoard.size() << "\n";
0089 for (const auto& out : eg_sorted_inBoard)
0090 dbgCout() << "kinematics of sorted objects " << out.hwPt << " " << out.hwEta << " " << out.hwPhi << "\n";
0091 }
0092 }
0093
0094 private:
0095 void extractEGObjEmu(const PFRegionEmu& region,
0096 const l1ct::OutputRegion& outregion,
0097 std::vector<l1ct::EGIsoObjEmu>& eg) {
0098 extractEGObjEmu(region, outregion.egphoton, eg);
0099 }
0100 void extractEGObjEmu(const PFRegionEmu& region,
0101 const l1ct::OutputRegion& outregion,
0102 std::vector<l1ct::EGIsoEleObjEmu>& eg) {
0103 extractEGObjEmu(region, outregion.egelectron, eg);
0104 }
0105 template <typename T>
0106 void extractEGObjEmu(const PFRegionEmu& region,
0107 const std::vector<T>& regional_objects,
0108 std::vector<T>& global_objects) {
0109 for (const auto& reg_obj : regional_objects) {
0110 global_objects.emplace_back(reg_obj);
0111 global_objects.back().hwEta = region.hwGlbEta(reg_obj.hwEta);
0112 global_objects.back().hwPhi = region.hwGlbPhi(reg_obj.hwPhi);
0113 }
0114 }
0115
0116 template <typename T>
0117 void resize_input(std::vector<T>& in) const {
0118 if (in.size() > nObjToSort_) {
0119 in.resize(nObjToSort_);
0120 } else if (in.size() < nObjToSort_) {
0121 for (unsigned int i = 0, diff = (nObjToSort_ - in.size()); i < diff; ++i) {
0122 in.push_back(T());
0123 in.back().clear();
0124 }
0125 }
0126 }
0127
0128 template <typename T>
0129 void merge_regions(const std::vector<T>& in_region1,
0130 const std::vector<T>& in_region2,
0131 std::vector<T>& out,
0132 unsigned int nOut) const {
0133
0134 out = in_region1;
0135 if (debug_)
0136 for (const auto& tmp : out)
0137 dbgCout() << "out " << tmp.hwPt << " " << tmp.hwEta << " " << tmp.hwPhi << "\n";
0138 std::reverse(out.begin(), out.end());
0139 if (debug_)
0140 for (const auto& tmp : out)
0141 dbgCout() << "out reverse " << tmp.hwPt << " " << tmp.hwEta << " " << tmp.hwPhi << "\n";
0142 std::copy(in_region2.begin(), in_region2.end(), std::back_inserter(out));
0143 if (debug_)
0144 for (const auto& tmp : out)
0145 dbgCout() << "out inserted " << tmp.hwPt << " " << tmp.hwEta << " " << tmp.hwPhi << "\n";
0146
0147 hybridBitonicMergeRef(&out[0], out.size(), 0, false);
0148
0149 if (out.size() > nOut) {
0150 out.resize(nOut);
0151 if (debug_)
0152 for (const auto& tmp : out)
0153 dbgCout() << "final " << tmp.hwPt << " " << tmp.hwEta << " " << tmp.hwPhi << "\n";
0154 }
0155 }
0156
0157 template <typename T>
0158 void merge(const std::vector<std::vector<T>>& in_objs, std::vector<T>& out) const {
0159 unsigned int nregions = in_objs.size();
0160 std::vector<T> pair_merge(nObjSorted_);
0161 if (nregions == 18) {
0162 for (unsigned int i = 0; i < nregions; i += 2) {
0163 merge_regions(in_objs[i + 0], in_objs[i + 1], pair_merge, nObjSorted_);
0164 if (i == 0)
0165 out = pair_merge;
0166 else
0167 merge_regions(out, pair_merge, out, nObjSorted_);
0168 }
0169 } else if (nregions == 9) {
0170 for (unsigned int i = 0; i < nregions; ++i) {
0171 for (unsigned int j = 0, nj = in_objs[i].size(); j < nObjSorted_; ++j) {
0172 if (j < nj)
0173 pair_merge[j] = in_objs[i][j];
0174 else
0175 pair_merge[j].clear();
0176 }
0177 if (i == 0)
0178 out = pair_merge;
0179 else
0180 merge_regions(out, pair_merge, out, nObjSorted_);
0181 }
0182 } else
0183 throw std::runtime_error("This sorter requires 18 or 9 regions");
0184 }
0185 };
0186 }
0187
0188 #endif