Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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 nRegions, unsigned int nEGPerRegion, unsigned int nEGOut, bool debug)
0021         : nREGIONS(nRegions), nEGPerRegion(nEGPerRegion), nEGOut(nEGOut), debug_(debug) {}
0022 
0023     L2EgSorterEmulator(const edm::ParameterSet &iConfig);
0024 
0025     virtual ~L2EgSorterEmulator() {}
0026 
0027     template <int NRegions, int NObjs>
0028     void toFirmware(const std::vector<l1ct::OutputBoard> &in,
0029                     EGIsoObj (&photons_in)[NRegions][NObjs],
0030                     EGIsoEleObj (&eles_in)[NRegions][NObjs]) const {
0031       for (unsigned int ib = 0; ib < NRegions; ib++) {
0032         const auto &region = in[ib];
0033         for (unsigned int io = 0; io < NObjs; io++) {
0034           EGIsoObj pho;
0035           EGIsoEleObj ele;
0036           if (io < region.egphoton.size())
0037             pho = region.egphoton[io];
0038           else
0039             pho.clear();
0040           if (io < region.egelectron.size())
0041             ele = region.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[/*nObjOut*/],
0054                     EGIsoEleObj out_egeles[/*nObjOut*/]) 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 nInputRegions() const { return nREGIONS; }
0063     unsigned int nInputObjPerRegion() const { return nEGPerRegion; }
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() > nEGPerRegion) {
0070         in.resize(nEGPerRegion);
0071       } else if (in.size() < nEGPerRegion) {
0072         for (unsigned int i = 0, diff = (nEGPerRegion - 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_regions(const std::vector<T> &in_region1,
0094                        const std::vector<T> &in_region2,
0095                        std::vector<T> &out,
0096                        unsigned int nOut) const {
0097       // we crate a bitonic list
0098       out = in_region1;
0099       std::reverse(out.begin(), out.end());
0100       std::copy(in_region2.begin(), in_region2.end(), std::back_inserter(out));
0101       hybridBitonicMergeRef(&out[0], out.size(), 0, false);
0102 
0103       if (out.size() > nOut)
0104         out.resize(nOut);
0105     }
0106 
0107     template <typename T>
0108     void merge(const std::vector<std::vector<T>> &in_objs, std::vector<T> &out) const {
0109       if (in_objs.size() == 1) {
0110         std::copy(in_objs[0].begin(), in_objs[0].end(), std::back_inserter(out));
0111         if (out.size() > nEGOut)
0112           out.resize(nEGOut);
0113       } else if (in_objs.size() == 2) {
0114         merge_regions(in_objs[0], in_objs[1], out, nEGOut);
0115       } else {
0116         std::vector<std::vector<T>> to_merge;
0117         for (unsigned int id = 0, idn = 1; id < in_objs.size(); id += 2, idn = id + 1) {
0118           if (idn >= in_objs.size()) {
0119             to_merge.push_back(in_objs[id]);
0120           } else {
0121             std::vector<T> pair_merge;
0122             merge_regions(in_objs[id], in_objs[idn], pair_merge, nEGPerRegion);
0123             to_merge.push_back(pair_merge);
0124           }
0125         }
0126         merge(to_merge, out);
0127       }
0128     }
0129 
0130     const unsigned int nREGIONS;
0131     const unsigned int nEGPerRegion;
0132     const unsigned int nEGOut;
0133     int debug_;
0134   };
0135 }  // namespace l1ct
0136 
0137 #endif