Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-10 01:53:48

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