Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:13:11

0001 #ifndef multififo_regionizer_elements_ref_h
0002 #define multififo_regionizer_elements_ref_h
0003 
0004 #include "DataFormats/L1TParticleFlow/interface/layer1_emulator.h"
0005 
0006 #include <list>
0007 #include <vector>
0008 #include <cassert>
0009 
0010 namespace l1ct {
0011   namespace multififo_regionizer {
0012     template <typename T>
0013     inline void shift(T& from, T& to) {
0014       to = from;
0015       from.clear();
0016     }
0017     template <typename TL, typename T>
0018     inline void pop_back(TL& from, T& to) {
0019       assert(!from.empty());
0020       to = from.back();
0021       from.pop_back();
0022     }
0023 
0024     inline int dphi_wrap(int local_phi) {
0025       if (local_phi > l1ct::Scales::INTPHI_PI)
0026         local_phi -= l1ct::Scales::INTPHI_TWOPI;
0027       else if (local_phi <= -l1ct::Scales::INTPHI_PI)
0028         local_phi += l1ct::Scales::INTPHI_TWOPI;
0029       return local_phi;
0030     }
0031 
0032     template <typename T>
0033     inline void push_to_fifo(const T& t, int local_eta, int local_phi, std::list<T>& fifo) {
0034       fifo.push_front(t);
0035       fifo.front().hwEta = local_eta;
0036       fifo.front().hwPhi = local_phi;
0037     }
0038 
0039     template <typename T>
0040     inline void maybe_push(const T& t,
0041                            const l1ct::PFRegionEmu& sector,
0042                            const l1ct::PFRegionEmu& region,
0043                            std::list<T>& fifo,
0044                            bool useAlsoVtxCoords);
0045     template <>
0046     inline void maybe_push<l1ct::TkObjEmu>(const l1ct::TkObjEmu& t,
0047                                            const l1ct::PFRegionEmu& sector,
0048                                            const l1ct::PFRegionEmu& region,
0049                                            std::list<l1ct::TkObjEmu>& fifo,
0050                                            bool useAlsoVtxCoords);
0051 
0052     template <typename T>
0053     class RegionBuffer {
0054     public:
0055       RegionBuffer() : nfifos_(0) {}
0056       void initFifos(unsigned int nfifos);
0057       void initRegion(const l1ct::PFRegionEmu& region, bool useAlsoVtxCoords) {
0058         region_ = region;
0059         useAlsoVtxCoords_ = useAlsoVtxCoords;
0060       }
0061       void flush();
0062       void maybe_push(int fifo, const T& t, const l1ct::PFRegionEmu& sector);
0063       T pop();
0064 
0065     private:
0066       unsigned int nfifos_;
0067       bool useAlsoVtxCoords_;
0068       l1ct::PFRegionEmu region_;
0069       std::vector<std::list<T>> fifos_;
0070       std::vector<std::pair<std::vector<T>, std::vector<T>>> queues_;
0071 
0072       T pop_next_trivial_();
0073       void fifos_to_stage_(std::vector<T>& staging_area);
0074       void queue_to_stage_(std::vector<T>& queue, std::vector<T>& staging_area);
0075       void stage_to_queue_(std::vector<T>& staging_area, std::vector<T>& queue);
0076       T pop_queue_(std::vector<T>& queue);
0077     };
0078 
0079     // forward decl for later
0080     template <typename T>
0081     class RegionMux;
0082 
0083     template <typename T>
0084     class RegionBuilder {
0085     public:
0086       RegionBuilder() {}
0087       RegionBuilder(unsigned int iregion, unsigned int nsort) : iregion_(iregion), sortbuffer_(nsort) {}
0088       void push(const T& in);
0089       void pop(RegionMux<T>& out);
0090 
0091     private:
0092       unsigned int iregion_;
0093       std::vector<T> sortbuffer_;
0094     };
0095 
0096     template <typename T>
0097     class RegionMux {
0098     public:
0099       RegionMux() : nregions_(0) {}
0100       RegionMux(unsigned int nregions,
0101                 unsigned int nsort,
0102                 unsigned int nout,
0103                 bool streaming,
0104                 unsigned int outii = 0,
0105                 unsigned int pauseii = 0)
0106           : nregions_(nregions),
0107             nsort_(nsort),
0108             nout_(nout),
0109             outii_(outii),
0110             pauseii_(pauseii),
0111             streaming_(streaming),
0112             buffer_(nregions * nsort),
0113             iter_(0),
0114             ireg_(nregions) {
0115         assert(streaming ? (outii * nout >= nsort) : (nout == nsort));
0116         for (auto& t : buffer_)
0117           t.clear();
0118       }
0119       void push(unsigned int region, std::vector<T>& in);
0120       bool stream(bool newevt, std::vector<T>& out);
0121 
0122     private:
0123       unsigned int nregions_, nsort_, nout_, outii_, pauseii_;
0124       bool streaming_;
0125       std::vector<T> buffer_;
0126       unsigned int iter_, ireg_;
0127     };
0128 
0129     // out of the Regionizer<T> since it doesn't depend on T and may be shared
0130     struct Route {
0131       unsigned short int sector, link, region, fifo;
0132       Route(unsigned short int from_sector,
0133             unsigned short int from_link,
0134             unsigned short int to_region,
0135             unsigned short int to_fifo)
0136           : sector(from_sector), link(from_link), region(to_region), fifo(to_fifo) {}
0137     };
0138 
0139     template <typename T>
0140     class Regionizer {
0141     public:
0142       Regionizer() {}
0143       Regionizer(unsigned int nsorted,
0144                  unsigned int nout,
0145                  bool streaming,
0146                  unsigned int outii = 0,
0147                  unsigned int pauseii = 0,
0148                  bool useAlsoVtxCoords = false);
0149       void initSectors(const std::vector<DetectorSector<T>>& sectors);
0150       void initSectors(const DetectorSector<T>& sector);
0151       void initRegions(const std::vector<PFInputRegion>& regions);
0152       void initRouting(const std::vector<Route> routes, bool validateRoutes = true);
0153 
0154       void reset() {
0155         flush();
0156         nevt_ = 0;
0157       }
0158 
0159       // single clock emulation
0160       bool step(bool newEvent, const std::vector<T>& links, std::vector<T>& out, bool mux = true);
0161 
0162       // single clock emulation
0163       bool muxonly_step(bool newEvent, bool mayFlush, const std::vector<T>& nomux_out, std::vector<T>& out);
0164 
0165       void destream(int iclock, const std::vector<T>& streams, std::vector<T>& out);
0166 
0167     private:
0168       unsigned int nsectors_, nregions_, nsorted_, nout_, outii_, pauseii_;
0169       bool streaming_, useAlsoVtxCoords_;
0170       std::vector<l1ct::PFRegionEmu> sectors_;
0171       std::vector<RegionBuffer<T>> buffers_;
0172       std::vector<RegionBuilder<T>> builders_;
0173       RegionMux<T> bigmux_;
0174       std::vector<Route> routes_;
0175       unsigned int nevt_;
0176 
0177       void flush() {
0178         for (auto& b : buffers_)
0179           b.flush();
0180       }
0181     };
0182 
0183   }  // namespace  multififo_regionizer
0184 }  // namespace l1ct
0185 
0186 #endif