File indexing completed on 2023-03-17 11:13:10
0001 #ifndef L2EgSorter_REF_H
0002 #define L2EgSorter_REF_H
0003
0004 #include "DataFormats/L1TParticleFlow/interface/layer1_emulator.h"
0005 #include "DataFormats/L1TParticleFlow/interface/egamma.h"
0006 #include "DataFormats/L1TParticleFlow/interface/pf.h"
0007 #include "L1Trigger/Phase2L1ParticleFlow/interface/common/bitonic_hybrid_sort_ref.h"
0008 #include "L1Trigger/Phase2L1ParticleFlow/interface/dbgPrintf.h"
0009
0010 #include <algorithm>
0011
0012 namespace edm {
0013 class ParameterSet;
0014 }
0015
0016 namespace l1ct {
0017
0018 class L2EgSorterEmulator {
0019 public:
0020 L2EgSorterEmulator(unsigned int nBoards, unsigned int nEGPerBoard, unsigned int nEGOut, bool debug)
0021 : nBOARDS(nBoards), nEGPerBoard(nEGPerBoard), nEGOut(nEGOut), debug_(debug) {}
0022
0023 L2EgSorterEmulator(const edm::ParameterSet &iConfig);
0024
0025 virtual ~L2EgSorterEmulator() {}
0026
0027 template <int NBoards, int NObjs>
0028 void toFirmware(const std::vector<l1ct::OutputBoard> &in,
0029 EGIsoObj (&photons_in)[NBoards][NObjs],
0030 EGIsoEleObj (&eles_in)[NBoards][NObjs]) const {
0031 for (unsigned int ib = 0; ib < NBoards; ib++) {
0032 const auto &board = in[ib];
0033 for (unsigned int io = 0; io < NObjs; io++) {
0034 EGIsoObj pho;
0035 EGIsoEleObj ele;
0036 if (io < board.egphoton.size())
0037 pho = board.egphoton[io];
0038 else
0039 pho.clear();
0040 if (io < board.egelectron.size())
0041 ele = board.egelectron[io];
0042 else
0043 ele.clear();
0044
0045 photons_in[ib][io] = pho;
0046 eles_in[ib][io] = ele;
0047 }
0048 }
0049 }
0050
0051 void toFirmware(const std::vector<EGIsoObjEmu> &out_photons,
0052 const std::vector<EGIsoEleObjEmu> &out_eles,
0053 EGIsoObj out_egphs[],
0054 EGIsoEleObj out_egeles[]) const;
0055
0056 void run(const std::vector<l1ct::OutputBoard> &in,
0057 std::vector<EGIsoObjEmu> &out_photons,
0058 std::vector<EGIsoEleObjEmu> &out_eles) const;
0059
0060 void setDebug(int verbose) { debug_ = verbose; }
0061
0062 unsigned int nInputBoards() const { return nBOARDS; }
0063 unsigned int nInputObjPerBoard() const { return nEGPerBoard; }
0064 unsigned int nOutputObj() const { return nEGOut; }
0065
0066 private:
0067 template <typename T>
0068 void resize_input(std::vector<T> &in) const {
0069 if (in.size() > nEGPerBoard) {
0070 in.resize(nEGPerBoard);
0071 } else if (in.size() < nEGPerBoard) {
0072 for (unsigned int i = 0, diff = (nEGPerBoard - in.size()); i < diff; ++i) {
0073 in.push_back(T());
0074 in.back().clear();
0075 }
0076 }
0077 }
0078
0079 template <typename T>
0080 static bool comparePt(const T &obj1, const T &obj2) {
0081 return (obj1.hwPt > obj2.hwPt);
0082 }
0083
0084 template <typename T>
0085 void print_objects(const std::vector<T> &objs, const std::string &label) const {
0086 for (unsigned int i = 0; i < objs.size(); ++i) {
0087 dbgCout() << label << " [" << i << "] pt: " << objs[i].hwPt << " eta: " << objs[i].hwEta
0088 << " phi: " << objs[i].hwPhi << " qual: " << objs[i].hwQual.to_string(2) << std::endl;
0089 }
0090 }
0091
0092 template <typename T>
0093 void merge_boards(const std::vector<T> &in_board1,
0094 const std::vector<T> &in_board2,
0095 std::vector<T> &out,
0096 unsigned int nOut) const {
0097
0098 out = in_board1;
0099 std::reverse(out.begin(), out.end());
0100 std::copy(in_board2.begin(), in_board2.end(), std::back_inserter(out));
0101 hybridBitonicMergeRef(&out[0], out.size(), 0, false);
0102
0103
0104 if (out.size() > nOut)
0105 out.resize(nOut);
0106 }
0107
0108 template <typename T>
0109 void merge(const std::vector<std::vector<T>> &in_objs, std::vector<T> &out) const {
0110 if (in_objs.size() == 1) {
0111 std::copy(in_objs[0].begin(), in_objs[0].end(), std::back_inserter(out));
0112 if (out.size() > nEGOut)
0113 out.resize(nEGOut);
0114 } else if (in_objs.size() == 2) {
0115 merge_boards(in_objs[0], in_objs[1], out, nEGOut);
0116 } else {
0117 std::vector<std::vector<T>> to_merge;
0118 for (unsigned int id = 0, idn = 1; id < in_objs.size(); id += 2, idn = id + 1) {
0119 if (idn >= in_objs.size()) {
0120 to_merge.push_back(in_objs[id]);
0121 } else {
0122 std::vector<T> pair_merge;
0123 merge_boards(in_objs[id], in_objs[idn], pair_merge, nEGPerBoard);
0124 to_merge.push_back(pair_merge);
0125 }
0126 }
0127 merge(to_merge, out);
0128 }
0129 }
0130
0131 const unsigned int nBOARDS;
0132 const unsigned int nEGPerBoard;
0133 const unsigned int nEGOut;
0134 int debug_;
0135 };
0136 }
0137
0138 #endif