CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/L1Trigger/DTTraco/src/Lut.cc

Go to the documentation of this file.
00001 //
00002 //   110208 SV TRACO hardware bug included
00003 //
00004 //
00005 //-----------------------
00006 // This Class's Header --
00007 //-----------------------
00008 #include "L1Trigger/DTTraco/interface/Lut.h"
00009 
00010 //----------------
00011 // Constructors --
00012 //----------------
00013 
00014 Lut::Lut(DTConfigLUTs* conf, int ntc, float SL_shift): _conf_luts(conf) {
00015 
00016   // set parameters from configuration
00017   m_d           = _conf_luts->D(); 
00018   m_ST          = _conf_luts->BTIC();
00019   m_wheel       = _conf_luts->Wheel();
00020 
00021   // 110208 SV TRACO hardware bug included: Xcn must be corrected because Xcn parameter has been
00022   // inserted in hardware strings with the shift included, because TRACO doesn't apply shift in positive cases
00023   float Xcn_corr    = _conf_luts->Xcn();
00024   if(SL_shift > 0){
00025     if(Xcn_corr > 0)
00026       Xcn_corr = Xcn_corr - SL_shift;
00027     if(Xcn_corr < 0)
00028       Xcn_corr = Xcn_corr + SL_shift;
00029   }
00030         
00031   m_Xcn         = Xcn_corr - (CELL_PITCH * 4.0 * (float)(ntc-1) * (float) m_wheel);
00032   m_pitch_d_ST  = CELL_PITCH / m_ST;
00033 
00034 
00035   //std::cout<< "Lut::Lut  ->  m_d " << m_d << " m_ST " << m_ST << " m_wheel " << m_wheel << " m_Xcn " << m_Xcn << " ntc " << ntc << std::endl;
00036   return;
00037 }
00038  
00039 Lut::~Lut() {}
00040 
00041 void Lut::setForTestBeam( int station, int board, int traco ) {
00042  // set parameters from fixed values for MB1 nd MB3 (used in Testbeams 2003 2004) 
00043   int nStat = station;
00044   int nBoard = board;
00045   int nTraco = traco;
00046   float tracoPos[50];
00047 
00048   if( nStat ==1 ){
00049 
00050     tracoPos[ 0] = -120.19;
00051     tracoPos[ 1] = -103.39;
00052     tracoPos[ 2] =  -86.59;
00053     tracoPos[ 3] =  -69.80;
00054     tracoPos[10] =  -52.99;
00055     tracoPos[11] =  -36.19;
00056     tracoPos[12] =  -19.39;
00057     tracoPos[13] =   -2.59;
00058     tracoPos[20] =   14.20;
00059     tracoPos[21] =   31.00;
00060     tracoPos[22] =   47.80;
00061     tracoPos[23] =   64.60;
00062     tracoPos[30] =   81.40;
00063 
00064     m_d  = 431.175;
00065     m_ST = 31;
00066     float m_Xc = tracoPos[ ( nBoard * 10 ) + nTraco ];
00067     float m_Xn = +39.0;
00068     m_Xcn =  m_Xn - m_Xc; 
00069     //m_stsize = m_CELL_PITCH / m_ST;
00070     //m_distp2 = 0.5 + ( 2.0 * m_CELL_H * m_ST / m_CELL_PITCH );
00071   }
00072 
00073   if( nStat ==3){
00074     tracoPos[ 0] = -165.45;
00075     tracoPos[ 1] = -148.65;
00076     tracoPos[ 2] = -131.85;
00077     tracoPos[ 3] = -115.05;
00078     tracoPos[10] =  -98.25;
00079     tracoPos[11] =  -81.45;
00080     tracoPos[12] =  -64.65;
00081     tracoPos[13] =  -47.85;
00082     tracoPos[20] =  -31.05;
00083     tracoPos[21] =  -14.25;
00084     tracoPos[22] =    2.54;
00085     tracoPos[23] =   19.34;
00086     tracoPos[30] =   36.14;
00087     tracoPos[31] =   52.94;
00088     tracoPos[32] =   69.74;
00089     tracoPos[33] =   86.54;
00090     tracoPos[40] =  103.34;
00091     tracoPos[41] =  120.14;
00092     tracoPos[42] =  136.94;
00093     tracoPos[43] =  153.74;
00094 
00095     m_d  = 512.47;
00096     m_ST = 31;
00097     float m_Xc = tracoPos[ ( nBoard * 10 ) + nTraco ];
00098     float m_Xn = -21.0;
00099     m_Xcn =  m_Xn - m_Xc; 
00100     //m_stsize = m_CELL_PITCH / m_ST;
00101     //m_distp2 = 0.5 + ( 2.0 * m_CELL_H * m_ST / m_CELL_PITCH );
00102   
00103   }
00104 
00105   return;
00106 }
00107 
00108 
00109 int Lut::get_k( int addr ) {
00110 //FIX attenzione controlla addr - 511 o -512???
00111   int i;
00112   float x;
00113   i = addr - 512;
00114   x = (float)i * CELL_PITCH / ( SL_D * m_ST );
00115   x = atanf(x);
00116   x = x * ANGRESOL;
00117   if(m_wheel<0)
00118     x = -x;
00119 
00120   return (int)x;
00121 }
00122 
00123 int Lut::get_x( int addr ) {
00124   int i;
00125   float a,b,x;
00126 
00127   if(addr<=511) //LUT outer
00128   {
00129         i=addr;
00130         b=m_d+SL_DIFF;
00131   }
00132   else if(addr<=1023) //LUT inner
00133   {
00134         i=addr-512;
00135         b=m_d-SL_DIFF;          
00136   }
00137   else  //LUT correlati
00138   {
00139         i=addr-1024;
00140         b=m_d;
00141   }
00142   a = m_Xcn - (m_pitch_d_ST * (float)i * (float)m_wheel);
00143   x = a/b;
00144 
00145   x = atanf(x);
00146   x = x * POSRESOL;
00147 
00148   return (int)x;
00149 }
00150 
00151 char exaDigit( int i ) {
00152   if ( i < 10 ) return (   i        + '0' );
00153   else          return ( ( i - 10 ) + 'A' );
00154 }
00155 
00156 std::string lutFmt( int i ) {
00157   char* buf = new char[6];
00158   buf[2] = ' ';
00159   buf[5] = '\0';
00160   int j4 = i % 16;
00161   i /= 16;
00162   int j3 = i % 16;
00163   i /= 16;
00164   int j2 = i % 16;
00165   i /= 16;
00166   int j1 = i % 16;
00167   buf[0] = exaDigit( j1 );
00168   buf[1] = exaDigit( j2 );
00169   buf[3] = exaDigit( j3 );
00170   buf[4] = exaDigit( j4 );
00171   std::string s( buf );
00172   return s;
00173 }
00174 
00175 
00176 
00177 /*  this for dumping luts in minicrate input format - for MB1  -- Testbeam 2004
00178 
00179 int main( int argn, char** argv ) {
00180 
00181 //  while ( 1 ) {
00182 //    int k;
00183 //    cin >> k;
00184 //    cout << lutFmt( k ) << endl;
00185 //  }
00186 
00187 //  cout << argn << endl;
00188   if ( argn != 3 ) return 1;
00189 //  cout << *argv[1] << endl;
00190 //  cout << *argv[2] << endl;
00191   if ( *argv[1] < '0' ) return 2;
00192   if ( *argv[1] > '9' ) return 2;
00193   if ( *argv[2] < '0' ) return 3;
00194   if ( *argv[2] > '9' ) return 3;
00195   int board = *argv[1] - '0';
00196   int traco = *argv[2] - '0';
00197   Lut lut( board, traco );
00198   int i;
00199   for ( i = 1; i <= 1536; i++ ) {
00200     cout << i << " " << lut.get( i ) << endl;
00201   }
00202 
00203   char* stri = "l1_  ";
00204   char* name = new char[10];
00205   char* s = stri;
00206   char* d = name;
00207   while ( *d++ = *s++ );
00208   int board;
00209   int traco;
00210   char winNewLine;
00211   winNewLine = 13;
00212   ofstream full( "l1_full" );
00213   for ( board = 0; board < 4; board++ ) {
00214     for ( traco = 0; traco < 4; traco++ ) {
00215       if ( ( board == 3 ) && ( traco != 0 ) ) continue;
00216       name[3] = '0' + board;
00217       name[4] = '0' + traco;
00218       cout << board << " " << traco << " " << name << endl;
00219       ofstream file( name );
00220       Lut lut( board, traco );
00221       cout << "loop" << endl;
00222       int i;
00223       int nfirst;
00224       int nwrite;
00225       nfirst = 0;
00226       nwrite = 0;
00227       for ( i = 0; i <= 1535; i++ ) {
00228 //        if ( i < 512 )
00229 //        if ( ( i > 512 ) && ( i < 1024 ) )
00230         int y = lut.get_x( i );
00231         int z = y;
00232         if ( z < 0 ) z += 65536;
00233         cout << board << " " << traco << " "
00234              << i << " " << y << endl;
00235         if ( nwrite == 0 ) {
00236           file << "4D " << board << " " << traco << " 0 "
00237                << lutFmt( nfirst );
00238           full << "4D " << board << " " << traco << " 0 "
00239                << lutFmt( nfirst );
00240 //               << nfirst << " ";
00241         }
00242 //        file   << lut.get( i ) << " ";
00243         file   << " " << lutFmt( z );
00244         full   << " " << lutFmt( z );
00245         nwrite++;
00246         if ( nwrite == 64 ) {
00247           file << winNewLine << endl;
00248           full << winNewLine << endl;
00249           nfirst += nwrite;
00250           nwrite = 0;
00251         }
00252       }
00253       nfirst = 0;
00254       nwrite = 0;
00255       for ( i = 0; i <= 1023; i++ ) {
00256 //        if ( i < 512 )
00257 //        if ( ( i > 512 ) && ( i < 1024 ) )
00258         int y = lut.get_k( i );
00259         int z = y;
00260         if ( z < 0 ) z += 65536;
00261         cout << board << " " << traco << " "
00262              << i << " " << y << endl;
00263         if ( nwrite == 0 ) {
00264           file << "4D " << board << " " << traco << " 1 "
00265                << lutFmt( nfirst );
00266           full << "4D " << board << " " << traco << " 1 "
00267                << lutFmt( nfirst );
00268 //               << nfirst << " ";
00269         }
00270 //        file   << lut.get( i ) << " ";
00271         file   << " " << lutFmt( z );
00272         full   << " " << lutFmt( z );
00273         nwrite++;
00274         if ( nwrite == 64 ) {
00275           file << winNewLine << endl;
00276           full << winNewLine << endl;
00277           nfirst += nwrite;
00278           nwrite = 0;
00279         }
00280       }
00281       file << "4E " << board << " " << traco << winNewLine << endl;
00282       full << "4E " << board << " " << traco << winNewLine << endl;
00283     }
00284   }
00285 
00286   return 0;
00287 
00288 }
00289 
00290 
00291 *** and for MB3  -- Testbeam 2004
00292 
00293 int main( int argn, char** argv ) {
00294 
00295 //  while ( 1 ) {
00296 //    int k;
00297 //    cin >> k;
00298 //    cout << lutFmt( k ) << endl;
00299 //  }
00300 
00301 //  cout << argn << endl;
00302   if ( argn != 3 ) return 1;
00303 //  cout << *argv[1] << endl;
00304 //  cout << *argv[2] << endl;
00305   if ( *argv[1] < '0' ) return 2;
00306   if ( *argv[1] > '9' ) return 2;
00307   if ( *argv[2] < '0' ) return 3;
00308   if ( *argv[2] > '9' ) return 3;
00309   int board = *argv[1] - '0';
00310   int traco = *argv[2] - '0';
00311   Lut lut( board, traco );
00312   int i;
00313   for ( i = 1; i <= 1536; i++ ) {
00314     cout << i << " " << lut.get( i ) << endl;
00315   }
00316 
00317   char* stri = "l3_  ";
00318   char* name = new char[10];
00319   char* s = stri;
00320   char* d = name;
00321   while ( *d++ = *s++ );
00322   int board;
00323   int traco;
00324   char winNewLine;
00325   winNewLine = 13;
00326   ofstream full( "l3_full" );
00327   for ( board = 0; board < 5; board++ ) {
00328     for ( traco = 0; traco < 4; traco++ ) {
00329       name[3] = '0' + board;
00330       name[4] = '0' + traco;
00331       cout << board << " " << traco << " " << name << endl;
00332       ofstream file( name );
00333       Lut lut( board, traco );
00334       cout << "loop" << endl;
00335       int i;
00336       int nfirst;
00337       int nwrite;
00338       nfirst = 0;
00339       nwrite = 0;
00340       for ( i = 0; i <= 1535; i++ ) {
00341 //        if ( i < 512 )
00342 //        if ( ( i > 512 ) && ( i < 1024 ) )
00343         int y = lut.get_x( i );
00344         int z = y;
00345         if ( z < 0 ) z += 65536;
00346         cout << board << " " << traco << " "
00347              << i << " " << y << endl;
00348         if ( nwrite == 0 ) {
00349           file << "4D " << board << " " << traco << " 0 "
00350                << lutFmt( nfirst );
00351           full << "4D " << board << " " << traco << " 0 "
00352                << lutFmt( nfirst );
00353 //               << nfirst << " ";
00354         }
00355 //        file   << lut.get( i ) << " ";
00356         file   << " " << lutFmt( z );
00357         full   << " " << lutFmt( z );
00358         nwrite++;
00359         if ( nwrite == 64 ) {
00360           file << winNewLine << endl;
00361           full << winNewLine << endl;
00362           nfirst += nwrite;
00363           nwrite = 0;
00364         }
00365       }
00366       nfirst = 0;
00367       nwrite = 0;
00368       for ( i = 0; i <= 1023; i++ ) {
00369 //        if ( i < 512 )
00370 //        if ( ( i > 512 ) && ( i < 1024 ) )
00371         int y = lut.get_k( i );
00372         int z = y;
00373         if ( z < 0 ) z += 65536;
00374         cout << board << " " << traco << " "
00375              << i << " " << y << endl;
00376         if ( nwrite == 0 ) {
00377           file << "4D " << board << " " << traco << " 1 "
00378                << lutFmt( nfirst );
00379           full << "4D " << board << " " << traco << " 1 "
00380                << lutFmt( nfirst );
00381 //               << nfirst << " ";
00382         }
00383 //        file   << lut.get( i ) << " ";
00384         file   << " " << lutFmt( z );
00385         full   << " " << lutFmt( z );
00386         nwrite++;
00387         if ( nwrite == 64 ) {
00388           file << winNewLine << endl;
00389           full << winNewLine << endl;
00390           nfirst += nwrite;
00391           nwrite = 0;
00392         }
00393       }
00394       file << "4E " << board << " " << traco << winNewLine << endl;
00395       full << "4E " << board << " " << traco << winNewLine << endl;
00396     }
00397   }
00398 
00399   return 0;
00400 
00401 }
00402 
00403 */
00404 
00405 
00406 
00407 void DSPtoIEEE32(short DSPmantissa, short DSPexp, float *f)
00408 {
00409   DSPexp -= 15;
00410   *f = DSPmantissa * (float)pow( 2.0, DSPexp );
00411   
00412   return;
00413 }
00414 
00415 
00416 void IEEE32toDSP(float f, short *DSPmantissa, short *DSPexp)
00417 {
00418   //long *pl, lm;
00419   uint32_t *pl = 0;
00420   uint32_t lm;
00421 
00422   //101104 SV convert float to int in safe way
00423   union { float f; uint32_t i; } u;
00424   u.f = f;
00425   *pl = u.i;
00426 
00427   bool sign=false;
00428   if( f==0.0 )
00429   {
00430     *DSPexp = 0;
00431     *DSPmantissa = 0;
00432   }
00433   else
00434   {
00435     //pl = reinterpret_cast<uint32_t*> (&f);
00436     //pl = (long*) (&f);
00437     if((*pl & 0x80000000)!=0)
00438       sign=true;
00439     lm =( 0x800000 | (*pl & 0x7FFFFF)); // [1][23bit mantissa]
00440     lm >>= 9; //reduce to 15bits
00441     lm &= 0x7FFF;
00442     *DSPexp = ((*pl>>23)&0xFF)-126;
00443     *DSPmantissa = (short)lm;
00444     if(sign)
00445       *DSPmantissa = - *DSPmantissa; // convert negative value in 2.s
00446     // complement
00447   }
00448   return;
00449 }
00450 
00451