CMS 3D CMS Logo

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