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 
67 
69 
70 
71 // ---------------------
72 // -- Class Interface --
73 // ---------------------
74 
75 class L1MuGMTLUT {
76 
77  public:
78  typedef std::pair<std::string, unsigned> port;
79 
81 
84 
86  L1MuGMTLUT(const char* name,
87  const std::vector<std::string>& instances,
88  const std::vector<port>& in_widths,
89  const std::vector<port>& out_widths,
90  unsigned vme_addr_width=0, bool distrRAM=false) : m_initialized(false), m_NLUTS(0), m_UseLookupFunction(true), m_saveFlag(false) {
91  Init (name, instances, in_widths, out_widths, vme_addr_width, distrRAM);
92  };
93 
94  L1MuGMTLUT(const char* name,
95  const std::string& instances,
96  const std::string& inputs,
97  const std::string& outputs,
98  unsigned vme_addr_width=0, bool distrRAM=false) : m_initialized(false), m_NLUTS(0), m_UseLookupFunction(true), m_saveFlag(false) {
99  Init (name, L1MuGMTLUTHelpers::Tokenizer(" ",instances), PortDecoder(inputs), PortDecoder(outputs),
100  vme_addr_width, distrRAM);
101  };
102 
104  virtual ~L1MuGMTLUT();
105 
108  inline unsigned LookupPacked (int idx, unsigned ) const;
109 
111  inline unsigned LookupPacked (int idx, const std::vector<unsigned>& address) const {
112  return LookupPacked (idx, vec2u ( address, m_Inputs ) ); };
113 
115  inline std::vector<unsigned> Lookup (int idx, const std::vector<unsigned>& address) const {
116  return Lookup (idx, vec2u ( address, m_Inputs ) ); };
117 
119  inline std::vector<unsigned> Lookup (int idx, unsigned address) const {
120  return u2vec ( LookupPacked(idx, address), m_Outputs );
121  };
122 
123 
125 
127  void Load (const char* path);
128 
130  void Save (const char* path);
131 
134  virtual unsigned LookupFunctionPacked (int idx, unsigned address) const { return 0; };
135 
137  void MakeSubClass (const char* fname = "", const char* template_file_h = "../interface/L1MuGMTLUT_SubClass.h_template",
138  const char* template_file_cc = "../interface/L1MuGMTLUT_SubClass.cc_template");
139 
140  std::string Name() {return m_name;};
141 
142  friend class L1MuGMTLUTConverter;
143 
145  int numberOfInstances() { return m_NLUTS; };
146 
147  protected:
148 
150  void Init(const char* name, const std::vector<std::string>& instances,
151  const std::vector<port>& in_widths, const std::vector<port>& out_widths,
152  unsigned vme_addr_width=0, bool distrRAM=false);
153 
155  inline unsigned vec2u (const std::vector <unsigned>& vec, const std::vector<port>& widths) const;
156 
158  inline std::vector<unsigned> u2vec (unsigned value, const std::vector<port>& widths) const;
159 
161  void Set (int idx, unsigned address, unsigned value);
162 
163  class PortDecoder : public std::vector<port> {
164  typedef std::vector<port> base;
165  public:
166  PortDecoder(const std::vector<port> &pt) : base(pt) {};
167 
169  // decode std::string of style "phi(2) eta(4)"
170  L1MuGMTLUTHelpers::Tokenizer tok(" ", input);
171  for (unsigned int i=0;i<tok.size(); i++) {
172  size_type obrace=tok[i].find("("), cbrace=tok[i].find(")");
173  if (obrace != std::string::npos && cbrace != std::string::npos)
174  push_back( port ( tok[i].substr(0,obrace), (unsigned) atoi (tok[i].substr(obrace+1,cbrace-obrace-1).c_str() ) ) );
175  else
176  edm::LogWarning("LUTMismatch") << "L1MuGMTLUT::PortDecoder: error decoding port " << tok[i];
177  }
178  };
181  for (unsigned int i=0; i<size();i++) {
182  // ostd::stringstream os; os << (*this)[i].second << ends;
183  // temp += (*this)[i].first + "(" + std::string( os.str() ) + ")";
184 
185  char buf[100]; sprintf(buf,"(%d)",(*this)[i].second);
186  temp += (*this)[i].first + std::string(buf);
187 
188  if (i!=size()-1) temp += " ";
189  }
190  return temp;
191  };
192 
193  private:
194  };
195 
197  int m_NLUTS;
199  std::vector <std::string> m_InstNames;
200  std::vector <std::vector <unsigned> > m_Contents;
201  std::vector <port> m_Inputs; // first port in vector is most significant bits
202  std::vector <port> m_Outputs;
203  unsigned m_TotalInWidth;
204  unsigned m_TotalOutWidth;
210 };
211 
212 //--------------------------------------------------------------------------------
213 
214 unsigned L1MuGMTLUT::vec2u (const std::vector <unsigned>& vec, const std::vector<port>& widths) const{
215  if (vec.size() != widths.size()) {
216  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::vec2u: number of LUT inputs/outputs does not match definition";
217  return (0);
218  }
219 
220  unsigned value = 0;
221  unsigned start_ofs=0;
222 
223  for (int i=vec.size()-1; i>=0; i--) {
224  if ( vec[i] >= (unsigned) (1 << widths[i].second) ) {
225  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::vec2u: LUT input/output number " << i
226  << " exceeds range (0 to " << ( (1 << widths[i].second) -1 ) << ")."
227  ;
228  }
229  else
230  value |= vec[i] << start_ofs;
231  start_ofs += widths[i].second;
232  }
233 
234  return (value);
235 }
236 
237 //--------------------------------------------------------------------------------
238 
239 std::vector <unsigned> L1MuGMTLUT::u2vec (unsigned value, const std::vector<port>& widths) const {
240  std::vector<unsigned> output( widths.size(), 0);
241 
242  unsigned start_ofs=0;
243 
244  for (int i=widths.size()-1; i>=0; i--) {
245  int mask = ( (1 << widths[i].second) - 1 ) << start_ofs;
246  output[i] = ( value & mask ) >> start_ofs;
247  start_ofs += widths[i].second;
248  }
249 
250  return output;
251 }
252 
253 //--------------------------------------------------------------------------------
254 
255 //
256 // the main lookup function
257 // looks up either from the function or the table
258 // checks the input and result ranges
259 //
260 unsigned L1MuGMTLUT::LookupPacked (int idx, unsigned address) const {
261  if (! m_initialized) {
262  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::LookupPacked: LUT not initialized. ";
263  return 0;
264  }
265  if ( idx >= m_NLUTS ) {
266  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::LookupPacked: LUT index exceeds range (0 to " << ( m_NLUTS -1 ) << ")."
267  ;
268  return 0;
269  }
270  if ( address >= (unsigned) (1 << m_TotalInWidth) ) {
271  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::LookupPacked: LUT input exceeds range (0 to " << ( (1 << m_TotalInWidth) -1 ) << ")."
272  ;
273  return 0;
274  }
275 
276  unsigned value = 0;
277  if (m_UseLookupFunction) {
278  value = LookupFunctionPacked (idx, address);
279  } else {
280  value = m_Contents[idx][address];
281  }
282 
283  // check range of output
284  if ( value >= (unsigned) (1 << m_TotalOutWidth) ) {
285  edm::LogWarning("LUTMismatch") << "Error in L1MuGMTLUT::LookupPacked(): LUT output value " << value
286  << " exceeds range (0 to " << ( (1 << m_TotalOutWidth) -1 ) << ")."
287  ;
288  edm::LogWarning("LUTMismatch") << " LUT name: " << m_name;
289  if (m_UseLookupFunction)
290  edm::LogWarning("LUTMismatch") << " Lookup Function has to be corrected!!!";
291  else
292  edm::LogWarning("LUTMismatch") << " LUT File has to be corrected!!!";
293  return (1 << m_TotalOutWidth) - 1;
294  }
295  return value;
296 }
297 
298 #endif
299 
300 
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
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:94
virtual ~L1MuGMTLUT()
destructor
Definition: L1MuGMTLUT.cc:35
void Set(int idx, unsigned address, unsigned value)
set with single address and value
Definition: L1MuGMTLUT.cc:171
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:214
std::string Name()
Definition: L1MuGMTLUT.h:140
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:326
std::vector< std::vector< unsigned > > m_Contents
Definition: L1MuGMTLUT.h:200
std::vector< unsigned > Lookup(int idx, const std::vector< unsigned > &address) const
additional lookup function (std::vector -> vector)
Definition: L1MuGMTLUT.h:115
uint16_t size_type
virtual unsigned LookupFunctionPacked(int idx, unsigned address) const
Definition: L1MuGMTLUT.h:134
static std::string const input
Definition: EdmProvDump.cc:44
friend class L1MuGMTLUTConverter
Definition: L1MuGMTLUT.h:140
bool m_UseLookupFunction
Definition: L1MuGMTLUT.h:198
PortDecoder(const std::string &input)
Definition: L1MuGMTLUT.h:168
std::pair< std::string, unsigned > port
Definition: L1MuGMTLUT.h:78
U second(std::pair< T, U > const &p)
std::vector< port > m_Outputs
Definition: L1MuGMTLUT.h:202
unsigned m_TotalInWidth
Definition: L1MuGMTLUT.h:203
void Save(const char *path)
save to LUT file
Definition: L1MuGMTLUT.cc:77
L1MuGMTLUT()
Init and Destruct.
Definition: L1MuGMTLUT.h:83
std::vector< port > m_Inputs
Definition: L1MuGMTLUT.h:201
bool m_initialized
Definition: L1MuGMTLUT.h:196
Definition: value.py:1
unsigned m_TotalOutWidth
Definition: L1MuGMTLUT.h:204
std::vector< std::string > m_InstNames
Definition: L1MuGMTLUT.h:199
void Load(const char *path)
I/O functions.
Definition: L1MuGMTLUT.cc:194
std::string m_name
Definition: L1MuGMTLUT.h:207
bool m_distrRAM
Definition: L1MuGMTLUT.h:206
int numberOfInstances()
get the number of Instances
Definition: L1MuGMTLUT.h:145
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:86
std::vector< unsigned > Lookup(int idx, unsigned address) const
additional lookup function (unsigned -> std::vector)
Definition: L1MuGMTLUT.h:119
string fname
main script
std::vector< port > base
Definition: L1MuGMTLUT.h:164
bool m_saveFlag
Definition: L1MuGMTLUT.h:208
std::vector< unsigned > u2vec(unsigned value, const std::vector< port > &widths) const
generate composite address or value from compact unsigned
Definition: L1MuGMTLUT.h:239
unsigned m_GeneralLUTVersion
Definition: L1MuGMTLUT.h:209
PortDecoder(const std::vector< port > &pt)
Definition: L1MuGMTLUT.h:166
unsigned LookupPacked(int idx, unsigned) const
Definition: L1MuGMTLUT.h:260
unsigned m_vme_addr_width
Definition: L1MuGMTLUT.h:205
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:47
unsigned LookupPacked(int idx, const std::vector< unsigned > &address) const
additional lookup function (std::vector -> unisgned)
Definition: L1MuGMTLUT.h:111