Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:22:15

0001 //-------------------------------------------------
0002 //
0003 /** \class L1MuGMTMatrix
0004  *  Matrix.
0005  *
0006  *  general matrix
0007 */
0008 //
0009 //
0010 //   Author :
0011 //   N. Neumeister            CERN EP
0012 //
0013 //   Migrated to CMSSW:
0014 //   I. Mikulec
0015 //
0016 //--------------------------------------------------
0017 #ifndef L1TriggerGlobalMuonTrigger_L1MuGMTMatrix_h
0018 #define L1TriggerGlobalMuonTrigger_L1MuGMTMatrix_h
0019 
0020 //---------------
0021 // C++ Headers --
0022 //---------------
0023 
0024 #include <iostream>
0025 #include <cassert>
0026 #include <string>
0027 #include <sstream>
0028 #include <memory>
0029 
0030 //----------------------
0031 // Base Class Headers --
0032 //----------------------
0033 
0034 //------------------------------------
0035 // Collaborating Class Declarations --
0036 //------------------------------------
0037 
0038 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0039 
0040 //              ---------------------
0041 //              -- Class Interface --
0042 //              ---------------------
0043 
0044 template <class T>
0045 class L1MuGMTMatrix {
0046 public:
0047   /// constructor
0048   L1MuGMTMatrix(int r, int c, T v);
0049 
0050   /// copy constructor
0051   L1MuGMTMatrix(const L1MuGMTMatrix<T>&);
0052 
0053   L1MuGMTMatrix(L1MuGMTMatrix<T>&&) = default;
0054 
0055   /// destructor
0056   virtual ~L1MuGMTMatrix() = default;
0057 
0058   ///
0059   T& operator()(int r, int c);
0060 
0061   ///
0062   const T& operator()(int r, int c) const;
0063 
0064   /// reset all elements
0065   void reset(T v);
0066 
0067   /// set matrix element
0068   void set(int r, int c, T v);
0069 
0070   /// assignment operator
0071   L1MuGMTMatrix& operator=(const L1MuGMTMatrix& m);
0072 
0073   /// matrix addition
0074   L1MuGMTMatrix& operator+=(const L1MuGMTMatrix& m);
0075 
0076   /// scalar addition
0077   L1MuGMTMatrix& operator+=(const T& s);
0078 
0079   /// scalar multiplication
0080   L1MuGMTMatrix& operator*=(const T& s);
0081 
0082   /// is the element (r,c) the max. entry in its row and column?
0083   bool isMax(int r, int c) const;
0084 
0085   /// is the element (r,c) the min. entry in its row and column?
0086   bool isMin(int r, int c) const;
0087 
0088   /// is any element in column c > 0 ? return index or -1
0089   int colAny(int c) const;
0090 
0091   /// is any element in row r > 0 ? return index or -1
0092   int rowAny(int r) const;
0093 
0094   /// print matrix
0095   void print() const;
0096 
0097 private:
0098   T& get(int r, int c) { return p_[r * c_size + c]; }
0099 
0100   T const& get(int r, int c) const { return p_[r * c_size + c]; }
0101 
0102   int r_size, c_size;
0103 
0104   std::unique_ptr<T[]> p_;
0105 };
0106 
0107 template <class T>
0108 L1MuGMTMatrix<T>::L1MuGMTMatrix(int r, int c, T v) : r_size(r), c_size(c), p_(new T[r * c]) {
0109   for (int i = 0; i < r_size * c_size; ++i) {
0110     p_[i] = v;
0111   }
0112 }
0113 
0114 // copy constructor
0115 
0116 template <class T>
0117 L1MuGMTMatrix<T>::L1MuGMTMatrix(const L1MuGMTMatrix<T>& mat)
0118     : r_size(mat.r_size), c_size(mat.c_size), p_(new T[r_size * c_size]) {
0119   for (int i = 0; i < r_size * c_size; ++i) {
0120     p_[i] = mat.p_[i];
0121   }
0122 }
0123 
0124 //
0125 //
0126 //
0127 template <class T>
0128 T& L1MuGMTMatrix<T>::operator()(int r, int c) {
0129   assert(r >= 0 && r < r_size && c >= 0 && c < c_size);
0130 
0131   return get(r, c);
0132 }
0133 
0134 //
0135 //
0136 //
0137 template <class T>
0138 const T& L1MuGMTMatrix<T>::operator()(int r, int c) const {
0139   assert(r >= 0 && r < r_size && c >= 0 && c < c_size);
0140 
0141   return get(r, c);
0142 }
0143 
0144 //
0145 // reset elements
0146 //
0147 template <class T>
0148 void L1MuGMTMatrix<T>::reset(T v) {
0149   for (int i = 0; i < r_size * c_size; ++i) {
0150     p_[i] = v;
0151   }
0152 }
0153 
0154 //
0155 // set matrix element
0156 //
0157 template <class T>
0158 void L1MuGMTMatrix<T>::set(int r, int c, T v) {
0159   assert(r >= 0 && r < r_size && c >= 0 && c < c_size);
0160 
0161   get(r, c) = v;
0162 }
0163 
0164 //
0165 // assignment operator
0166 //
0167 template <class T>
0168 L1MuGMTMatrix<T>& L1MuGMTMatrix<T>::operator=(const L1MuGMTMatrix& m) {
0169   if (this != &m) {
0170     assert(m.c_size == c_size && m.r_size == r_size);
0171 
0172     for (int i = 0; i < r_size * c_size; ++i) {
0173       p_[i] = m.p_[i];
0174     }
0175   }
0176 
0177   return (*this);
0178 }
0179 
0180 //
0181 // matrix addition
0182 //
0183 template <class T>
0184 L1MuGMTMatrix<T>& L1MuGMTMatrix<T>::operator+=(const L1MuGMTMatrix& m) {
0185   assert(m.c_size == c_size && m.r_size == r_size);
0186 
0187   for (int i = 0; i < r_size * c_size; ++i) {
0188     p_[i] += m.p_[i];
0189   }
0190 
0191   return *this;
0192 }
0193 
0194 //
0195 // scalar addition
0196 //
0197 template <class T>
0198 L1MuGMTMatrix<T>& L1MuGMTMatrix<T>::operator+=(const T& s) {
0199   for (int i = 0; i < r_size * c_size; ++i) {
0200     p_[i] += s;
0201   }
0202 
0203   return *this;
0204 }
0205 
0206 //
0207 // scalar multiplication
0208 //
0209 template <class T>
0210 L1MuGMTMatrix<T>& L1MuGMTMatrix<T>::operator*=(const T& s) {
0211   for (int i = 0; i < r_size * c_size; ++i) {
0212     p_[i] *= s;
0213   }
0214 
0215   return *this;
0216 }
0217 
0218 //
0219 // is the element (r,c) the max. entry in its row and column?
0220 //
0221 template <class T>
0222 bool L1MuGMTMatrix<T>::isMax(int r, int c) const {
0223   bool max = true;
0224 
0225   for (int i = 0; i < c; i++) {
0226     max = max && (this->get(r, c) > this->get(r, i));
0227   }
0228   for (int i = c + 1; i < c_size; i++) {
0229     max = max && (this->get(r, c) >= this->get(r, i));
0230   }
0231 
0232   for (int j = 0; j < r; j++) {
0233     max = max && (this->get(r, c) > this->get(j, c));
0234   }
0235   for (int j = r + 1; j < r_size; j++) {
0236     max = max && (this->get(r, c) >= this->get(j, c));
0237   }
0238 
0239   return max;
0240 }
0241 
0242 //
0243 // is the element (r,c) the min. entry in its row and column?
0244 //
0245 template <class T>
0246 bool L1MuGMTMatrix<T>::isMin(int r, int c) const {
0247   bool min = true;
0248   for (int i = 0; i < c_size; i++) {
0249     min = min && (this->get(r, c) <= this->get(r, i));
0250   }
0251   for (int j = 0; j < r_size; j++) {
0252     min = min && (this->get(r, c) <= this->get(j, c));
0253   }
0254 
0255   return min;
0256 }
0257 
0258 //
0259 // is any element in column c > 0 ? return index or -1
0260 //
0261 template <class T>
0262 int L1MuGMTMatrix<T>::colAny(int c) const {
0263   int stat = -1;
0264   for (int i = 0; i < r_size; i++) {
0265     if (this->get(i, c) > 0)
0266       stat = i;
0267   }
0268 
0269   return stat;
0270 }
0271 
0272 //
0273 // is any element in row r > 0 ? return index or -1
0274 //
0275 template <class T>
0276 int L1MuGMTMatrix<T>::rowAny(int r) const {
0277   int stat = -1;
0278   for (int i = 0; i < c_size; i++) {
0279     if (this->get(r, i) > 0)
0280       stat = i;
0281   }
0282 
0283   return stat;
0284 }
0285 
0286 //
0287 // print matrix
0288 //
0289 template <class T>
0290 void L1MuGMTMatrix<T>::print() const {
0291   for (int r = 0; r < r_size; r++) {
0292     std::stringstream output;
0293     for (int c = 0; c < c_size; c++)
0294       output << get(r, c) << "\t";
0295     edm::LogVerbatim("GMTMatrix_print") << output.str();
0296   }
0297   edm::LogVerbatim("GMTMatrix_print");
0298 }
0299 
0300 #endif