CMS 3D CMS Logo

L1MuGMTLUT.h
Go to the documentation of this file.
1 //-------------------------------------------------
2 //
34 //
35 //
36 // Author :
37 // H. Sakulin HEPHY Vienna
38 //
39 // Migrated to CMSSW:
40 // I. Mikulec
41 //
42 //--------------------------------------------------
43 #ifndef L1TriggerGlobalMuonTrigger_L1MuGMTLUT_h
44 #define L1TriggerGlobalMuonTrigger_L1MuGMTLUT_h
45 
46 //---------------
47 // C++ Headers --
48 //---------------
49 
50 #include <vector>
51 #include <iostream>
52 //#include <sstream>
53 #include <string>
54 #include <cstdlib>
55 #include <cstdio>
56 
57 //----------------------
58 // Base Class Headers --
59 //----------------------
60 class L1MuGMTLUTConverter;
61 
62 //------------------------------------
63 // Collaborating Class Declarations --
64 //------------------------------------
66 
68 
69 // ---------------------
70 // -- Class Interface --
71 // ---------------------
72 
73 class L1MuGMTLUT {
74 public:
75  typedef std::pair<std::string, unsigned> port;
76 
78 
81 
83  L1MuGMTLUT(const char* name,
84  const std::vector<std::string>& instances,
85  const std::vector<port>& in_widths,
86  const std::vector<port>& out_widths,
87  unsigned vme_addr_width = 0,
88  bool distrRAM = false)
90  Init(name, instances, in_widths, out_widths, vme_addr_width, distrRAM);
91  };
92 
93  L1MuGMTLUT(const char* name,
94  const std::string& instances,
95  const std::string& inputs,
96  const std::string& outputs,
97  unsigned vme_addr_width = 0,
98  bool distrRAM = false)
100  Init(name,
101  L1MuGMTLUTHelpers::Tokenizer(" ", instances),
102  PortDecoder(inputs),
103  PortDecoder(outputs),
104  vme_addr_width,
105  distrRAM);
106  };
107 
109  virtual ~L1MuGMTLUT();
110 
113  inline unsigned LookupPacked(int idx, unsigned) const;
114 
116  inline unsigned LookupPacked(int idx, const std::vector<unsigned>& address) const {
117  return LookupPacked(idx, vec2u(address, m_Inputs));
118  };
119 
121  inline std::vector<unsigned> Lookup(int idx, const std::vector<unsigned>& address) const {
122  return Lookup(idx, vec2u(address, m_Inputs));
123  };
124 
126  inline std::vector<unsigned> Lookup(int idx, unsigned address) const {
127  return u2vec(LookupPacked(idx, address), m_Outputs);
128  };
129 
131 
133  void Load(const char* path);
134 
136  void Save(const char* path);
137 
140  virtual unsigned LookupFunctionPacked(int idx, unsigned address) const { return 0; };
141 
143  void MakeSubClass(const char* fname = "",
144  const char* template_file_h = "../interface/L1MuGMTLUT_SubClass.h_template",
145  const char* template_file_cc = "../interface/L1MuGMTLUT_SubClass.cc_template");
146 
147  std::string Name() { return m_name; };
148 
149  friend class L1MuGMTLUTConverter;
150 
152  int numberOfInstances() { return m_NLUTS; };
153 
154 protected:
156  void Init(const char* name,
157  const std::vector<std::string>& instances,
158  const std::vector<port>& in_widths,
159  const std::vector<port>& out_widths,
160  unsigned vme_addr_width = 0,
161  bool distrRAM = false);
162 
164  inline unsigned vec2u(const std::vector<unsigned>& vec, const std::vector<port>& widths) const;
165 
167  inline std::vector<unsigned> u2vec(unsigned value, const std::vector<port>& widths) const;
168 
170  void Set(int idx, unsigned address, unsigned value);
171 
172  class PortDecoder : public std::vector<port> {
173  typedef std::vector<port> base;
174 
175  public:
176  PortDecoder(const std::vector<port>& pt) : base(pt){};
177 
179  // decode std::string of style "phi(2) eta(4)"
180  L1MuGMTLUTHelpers::Tokenizer tok(" ", input);
181  for (unsigned int i = 0; i < tok.size(); i++) {
182  size_type obrace = tok[i].find("("), cbrace = tok[i].find(")");
183  if (obrace != std::string::npos && cbrace != std::string::npos)
184  push_back(
185  port(tok[i].substr(0, obrace), (unsigned)atoi(tok[i].substr(obrace + 1, cbrace - obrace - 1).c_str())));
186  else
187  edm::LogWarning("LUTMismatch") << "L1MuGMTLUT::PortDecoder: error decoding port " << tok[i];
188  }
189  };
192  for (unsigned int i = 0; i < size(); i++) {
193  // ostd::stringstream os; os << (*this)[i].second << ends;
194  // temp += (*this)[i].first + "(" + std::string( os.str() ) + ")";
195 
196  char buf[100];
197  sprintf(buf, "(%d)", (*this)[i].second);
198  temp += (*this)[i].first + std::string(buf);
199 
200  if (i != size() - 1)
201  temp += " ";
202  }
203  return temp;
204  };
205 
206  private:
207  };
208 
210  int m_NLUTS;
212  std::vector<std::string> m_InstNames;
213  std::vector<std::vector<unsigned> > m_Contents;
214  std::vector<port> m_Inputs; // first port in vector is most significant bits
215  std::vector<port> m_Outputs;
216  unsigned m_TotalInWidth;
217  unsigned m_TotalOutWidth;
223 };
224 
225 //--------------------------------------------------------------------------------
226 
227 unsigned L1MuGMTLUT::vec2u(const std::vector<unsigned>& vec, const std::vector<port>& widths) const {
228  if (vec.size() != widths.size()) {
229  edm::LogWarning("LUTMismatch")
230  << "Error in L1MuGMTLUT::vec2u: number of LUT inputs/outputs does not match definition";
231  return (0);
232  }
233 
234  unsigned value = 0;
235  unsigned start_ofs = 0;
236 
237  for (int i = vec.size() - 1; i >= 0; i--) {
238  if (vec[i] >= (unsigned)(1 << widths[i].second)) {
239  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::vec2u: LUT input/output number " << i
240  << " exceeds range (0 to " << ((1 << widths[i].second) - 1) << ").";
241  } else
242  value |= vec[i] << start_ofs;
243  start_ofs += widths[i].second;
244  }
245 
246  return (value);
247 }
248 
249 //--------------------------------------------------------------------------------
250 
251 std::vector<unsigned> L1MuGMTLUT::u2vec(unsigned value, const std::vector<port>& widths) const {
252  std::vector<unsigned> output(widths.size(), 0);
253 
254  unsigned start_ofs = 0;
255 
256  for (int i = widths.size() - 1; i >= 0; i--) {
257  int mask = ((1 << widths[i].second) - 1) << start_ofs;
258  output[i] = (value & mask) >> start_ofs;
259  start_ofs += widths[i].second;
260  }
261 
262  return output;
263 }
264 
265 //--------------------------------------------------------------------------------
266 
267 //
268 // the main lookup function
269 // looks up either from the function or the table
270 // checks the input and result ranges
271 //
272 unsigned L1MuGMTLUT::LookupPacked(int idx, unsigned address) const {
273  if (!m_initialized) {
274  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::LookupPacked: LUT not initialized. ";
275  return 0;
276  }
277  if (idx >= m_NLUTS) {
278  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::LookupPacked: LUT index exceeds range (0 to "
279  << (m_NLUTS - 1) << ").";
280  return 0;
281  }
282  if (address >= (unsigned)(1 << m_TotalInWidth)) {
283  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::LookupPacked: LUT input exceeds range (0 to "
284  << ((1 << m_TotalInWidth) - 1) << ").";
285  return 0;
286  }
287 
288  unsigned value = 0;
289  if (m_UseLookupFunction) {
290  value = LookupFunctionPacked(idx, address);
291  } else {
292  value = m_Contents[idx][address];
293  }
294 
295  // check range of output
296  if (value >= (unsigned)(1 << m_TotalOutWidth)) {
297  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::LookupPacked(): LUT output value " << value
298  << " exceeds range (0 to " << ((1 << m_TotalOutWidth) - 1) << ").";
299  edm::LogWarning("LUTMismatch") << " LUT name: " << m_name;
301  edm::LogWarning("LUTMismatch") << " Lookup Function has to be corrected!!!";
302  else
303  edm::LogWarning("LUTMismatch") << " LUT File has to be corrected!!!";
304  return (1 << m_TotalOutWidth) - 1;
305  }
306  return value;
307 }
308 
309 #endif
size
Write out results.
L1MuGMTLUT(const char *name, const std::string &instances, const std::string &inputs, const std::string &outputs, unsigned vme_addr_width=0, bool distrRAM=false)
Definition: L1MuGMTLUT.h:93
virtual ~L1MuGMTLUT()
destructor
Definition: L1MuGMTLUT.cc:35
void Set(int idx, unsigned address, unsigned value)
set with single address and value
Definition: L1MuGMTLUT.cc:177
std::vector< std::vector< unsigned > > m_Contents
Definition: L1MuGMTLUT.h:213
unsigned vec2u(const std::vector< unsigned > &vec, const std::vector< port > &widths) const
generate address or value from composite address or value
Definition: L1MuGMTLUT.h:227
std::string Name()
Definition: L1MuGMTLUT.h:147
void MakeSubClass(const char *fname="", const char *template_file_h="../interface/L1MuGMTLUT_SubClass.h_template", const char *template_file_cc="../interface/L1MuGMTLUT_SubClass.cc_template")
Add Generate SubClass method.
Definition: L1MuGMTLUT.cc:333
std::vector< unsigned > Lookup(int idx, const std::vector< unsigned > &address) const
additional lookup function (std::vector -> vector)
Definition: L1MuGMTLUT.h:121
uint16_t size_type
virtual unsigned LookupFunctionPacked(int idx, unsigned address) const
Definition: L1MuGMTLUT.h:140
static std::string const input
Definition: EdmProvDump.cc:48
friend class L1MuGMTLUTConverter
Definition: L1MuGMTLUT.h:147
bool m_UseLookupFunction
Definition: L1MuGMTLUT.h:211
PortDecoder(const std::string &input)
Definition: L1MuGMTLUT.h:178
std::pair< std::string, unsigned > port
Definition: L1MuGMTLUT.h:75
U second(std::pair< T, U > const &p)
std::vector< port > m_Outputs
Definition: L1MuGMTLUT.h:215
unsigned m_TotalInWidth
Definition: L1MuGMTLUT.h:216
void Save(const char *path)
save to LUT file
Definition: L1MuGMTLUT.cc:78
L1MuGMTLUT()
Init and Destruct.
Definition: L1MuGMTLUT.h:80
std::vector< port > m_Inputs
Definition: L1MuGMTLUT.h:214
bool m_initialized
Definition: L1MuGMTLUT.h:209
Definition: value.py:1
unsigned m_TotalOutWidth
Definition: L1MuGMTLUT.h:217
std::vector< std::string > m_InstNames
Definition: L1MuGMTLUT.h:212
void Load(const char *path)
I/O functions.
Definition: L1MuGMTLUT.cc:200
std::string m_name
Definition: L1MuGMTLUT.h:220
bool m_distrRAM
Definition: L1MuGMTLUT.h:219
int numberOfInstances()
get the number of Instances
Definition: L1MuGMTLUT.h:152
L1MuGMTLUT(const char *name, const std::vector< std::string > &instances, const std::vector< port > &in_widths, const std::vector< port > &out_widths, unsigned vme_addr_width=0, bool distrRAM=false)
constructor with init
Definition: L1MuGMTLUT.h:83
std::vector< unsigned > Lookup(int idx, unsigned address) const
additional lookup function (unsigned -> std::vector)
Definition: L1MuGMTLUT.h:126
string fname
main script
std::vector< port > base
Definition: L1MuGMTLUT.h:173
bool m_saveFlag
Definition: L1MuGMTLUT.h:221
std::vector< unsigned > u2vec(unsigned value, const std::vector< port > &widths) const
generate composite address or value from compact unsigned
Definition: L1MuGMTLUT.h:251
unsigned m_GeneralLUTVersion
Definition: L1MuGMTLUT.h:222
PortDecoder(const std::vector< port > &pt)
Definition: L1MuGMTLUT.h:176
unsigned LookupPacked(int idx, unsigned) const
Definition: L1MuGMTLUT.h:272
unsigned m_vme_addr_width
Definition: L1MuGMTLUT.h:218
void Init(const char *name, const std::vector< std::string > &instances, const std::vector< port > &in_widths, const std::vector< port > &out_widths, unsigned vme_addr_width=0, bool distrRAM=false)
Initialize the LUT.
Definition: L1MuGMTLUT.cc:46
unsigned LookupPacked(int idx, const std::vector< unsigned > &address) const
additional lookup function (std::vector -> unisgned)
Definition: L1MuGMTLUT.h:116