Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:51

0001 #include <packet_fvtx_dcm0.h>
0002 
0003 Packet_fvtx_dcm0::Packet_fvtx_dcm0()
0004 {
0005 
0006   calculated_parity=0;
0007   parity_comparison=0; 
0008   number_of_hits = 0;
0009   error_code = 0;
0010 }
0011 
0012 Packet_fvtx_dcm0::Packet_fvtx_dcm0(PACKET_ptr data)
0013   : Packet_w4 (data)
0014 {
0015   calculated_parity=0;
0016   parity_comparison=0; 
0017   number_of_hits = 0;
0018   error_code = 0;
0019 
0020 }
0021 
0022 Packet_fvtx_dcm0::~Packet_fvtx_dcm0()
0023 {
0024 }
0025 
0026 // ------------------------------------------------------
0027 
0028 int  Packet_fvtx_dcm0::iValue(const int ich, const int what)
0029 {
0030   // we check that "row" is within range
0031   if ( what < 0 || what >3 ) return 0;
0032 
0033   // now we must make sure we have decoded first so that the number_of_hits used
0034   // in a moment is valid
0035 
0036   if (decoded_data1 == NULL )
0037     {
0038       if ( (decoded_data1 = decode(&data1_length))==NULL)
0039     return 0;
0040     }
0041 
0042   if ( ich < 0 || ich >= number_of_hits) return 0;
0043   if ( error_code ) return 0;
0044 
0045   switch (what)
0046     {
0047     case 0:     // FEMID
0048       return decoded_data2[ich];
0049       break;
0050       
0051     case 1:     // CHIPID
0052       return decoded_data3[ich];
0053       break;
0054 
0055     case 2:    // CHANNNEL
0056       return decoded_data4[ich];
0057       break;
0058 
0059     case 3:    // ADC
0060       return decoded_data1[ich];
0061       break;
0062 
0063     default:
0064       break;
0065 
0066     }
0067 
0068   return 0;
0069 }
0070 
0071 int  Packet_fvtx_dcm0::iValue(const int ich, const char *what)
0072 {
0073   // now let's derefence the proxy array. If we didn't decode
0074   // the data until now, we do it now
0075 
0076 
0077   if ( strcmp(what,"HITS")==0)
0078     {
0079       //we need to decode the main data structures now
0080       if (decoded_data1 == NULL )
0081     {
0082       if ( (decoded_data1 = decode(&data1_length))==NULL)
0083         return 0;
0084     }
0085       if ( error_code ) return 0;
0086       return number_of_hits;
0087     }
0088 
0089   else if ( strcmp(what,"FLAG")==0)
0090     {
0091       if (decoded_data5 == NULL )
0092     {
0093       if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
0094         return 0;
0095     }
0096       
0097       return decoded_data5[1];
0098     }
0099 
0100   else if ( strcmp(what,"EVTNR")==0)
0101     {
0102       if (decoded_data5 == NULL )
0103     {
0104       if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
0105         return 0;
0106     }
0107       
0108       return decoded_data5[0];
0109     }
0110 
0111   else if ( strcmp(what,"DETID")==0)
0112     {
0113       if (decoded_data5 == NULL )
0114     {
0115       if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
0116         return 0;
0117     }
0118       
0119       return decoded_data5[2];
0120     }
0121 
0122   else if ( strcmp(what,"ADC")==0)
0123     {
0124       if (decoded_data1 == NULL )
0125     {
0126       if ( (decoded_data1 = decode(&data1_length))==NULL)
0127         return 0;
0128     }
0129       if ( ich < 0 || ich >= number_of_hits) return 0;      
0130       return decoded_data1[ich];
0131     }
0132 
0133 
0134   else if ( strcmp(what,"FEMID")==0)
0135     {
0136       if (decoded_data1 == NULL )
0137     {
0138       if ( (decoded_data1 = decode(&data1_length))==NULL)
0139         return 0;
0140     }
0141       
0142       if ( ich < 0 || ich >= number_of_hits) return 0;      
0143       return decoded_data2[ich];
0144     }
0145 
0146   else if ( strcmp(what,"CHIPID")==0)
0147     {
0148       if (decoded_data1 == NULL )
0149     {
0150       if ( (decoded_data1 = decode(&data1_length))==NULL)
0151         return 0;
0152     }
0153       
0154       if ( ich < 0 || ich >= number_of_hits) return 0;      
0155       return decoded_data3[ich];
0156     }
0157 
0158   else if ( strcmp(what,"CHANNEL")==0)
0159     {
0160       if (decoded_data1 == NULL )
0161     {
0162       if ( (decoded_data1 = decode(&data1_length))==NULL)
0163         return 0;
0164     }
0165       
0166       if ( ich < 0 || ich >= number_of_hits) return 0;      
0167       return decoded_data4[ich];
0168     }
0169 
0170 
0171   else if ( strcmp(what,"MODADDR")==0)
0172     {
0173       if (decoded_data5 == NULL )
0174     {
0175       if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
0176         return 0;
0177     }
0178       
0179       return decoded_data5[3];
0180     }
0181 
0182 
0183   else if ( strcmp(what,"BCLCK")==0)
0184     {
0185       if (decoded_data5 == NULL )
0186     {
0187       if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
0188         return 0;
0189     }
0190       
0191       return decoded_data5[4];
0192     }
0193 
0194   else if ( strcmp(what,"PARITY")==0)
0195     {
0196       if (decoded_data5 == NULL )
0197     {
0198       if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
0199         return 0;
0200     }
0201       
0202       return decoded_data5[5];
0203     }
0204 
0205   else if ( strcmp(what,"FEMERROR")==0)
0206     {
0207       if (decoded_data5 == NULL )
0208     {
0209       if ( (decoded_data5 = decode_misc(&data5_length))==NULL)
0210         return 0;
0211     }
0212       
0213       return decoded_data5[6];
0214     }
0215 
0216   else if ( strcmp(what,"CHECKPARITY")==0)
0217     {
0218       if (! parity_comparison  )
0219     {
0220       calculate_parity();
0221     }
0222       return parity_comparison;
0223     }
0224   
0225   else if ( strcmp(what,"ERRORCODE")==0)
0226     {
0227       return error_code;
0228     }
0229   
0230   
0231   return 0;
0232 }
0233 
0234 
0235 
0236 // ------------------------------------------------------
0237 
0238 int *Packet_fvtx_dcm0::decode (int *nwout)
0239 {
0240   int *p,*k;
0241 
0242     
0243   int dlength = getDataLength();
0244 
0245   p = (int *) findPacketDataStart(packet);
0246   if (p == 0) 
0247     {
0248       *nwout = 0;
0249       return 0;
0250     }
0251   k = &p[5]; // skip the header info
0252   int payloadlength = dlength -5; 
0253 
0254   // this routine is designed to return the "main" array of values which gets 
0255   // hooked up to decoded_data1 internally. 
0256   // in addition, we fill decoded_data{2,3,4} here as well. In this way, we don't
0257   // need to bother with deleting the allocated memory in the end. 
0258 
0259   // we use 
0260   // decoded_data1 -> adc values
0261   // decoded_data2 -> FEM ID
0262   // decoded_data3 -> CHIP ID
0263   // decoded_data4 -> CHANNEL NUMBER
0264 
0265   p =             new int [ 4*512];  // 4 FEM ids (wedges), 512 max hits per wedge
0266   decoded_data2 = new int [ 4*512];  // 4 FEM ids (wedges), 512 max hits per wedge
0267   decoded_data3 = new int [ 4*512];  // 4 FEM ids (wedges), 512 max hits per wedge
0268   decoded_data4 = new int [ 4*512];  // 4 FEM ids (wedges), 512 max hits per wedge
0269 
0270   data2_length = 4*512;
0271   data3_length = 4*512;
0272   data4_length = 4*512;
0273   
0274   memset (p, 0, 4*512*sizeof(int));
0275   memset (decoded_data2, 0, 4*512*sizeof(int));
0276   memset (decoded_data3, 0, 4*512*sizeof(int));
0277   memset (decoded_data4, 0, 4*512*sizeof(int));
0278 
0279   // let's confirm that k[payloadlength-1] has the MSB set (this holds the CRS)
0280   if ( (k[payloadlength-1] & 0x80000000) == 0)
0281     {
0282       std::cout << __FILE__ << "  " << __LINE__ << " Error in trailer word " << std::hex << k[payloadlength-1] << std::dec << "  ";
0283       this->identify();
0284       // for now we let this pass
0285     }
0286 
0287   // and let's confirm that k[0] is an actual FEMID field ( must have all 12 LSBs 0) 
0288   if ( (k[0] & 0xFFF) != 0)
0289     {
0290       std::cout << __FILE__ << "  " << __LINE__ << " Error: 1st word is not a FEM ID " << std::hex << k[0] << std::dec << "  ";
0291       this->identify();
0292       // for now we let this pass
0293     }
0294 
0295   int i;
0296   int last_femid = -1;
0297   int number_of_datawords = 0;
0298   int adc, channelnumber, chipid;
0299   int array_pos = 0;
0300 
0301   for ( i = 0; i < payloadlength -2; i++)
0302     {
0303 
0304       if ( (k[i] & 0xFFF) == 0) // found a FEM ID
0305     {
0306       // the simulated data have thee femid encoded differently
0307       if (  getHitFormat() == IDFVTX_SIM)
0308         {
0309           last_femid = ((k[i] >> 12) & 0xf) -1;
0310         }
0311 
0312       else
0313         {
0314           last_femid = -1;  // we reset to "invalid"
0315           int f = k[i] >> 12;
0316           if ( f & 1 ) last_femid = 0;
0317           if ( f & 2 ) last_femid = 1;
0318           if ( f & 4 ) last_femid = 2;
0319           if ( f & 8 ) last_femid = 3;
0320         }
0321       number_of_datawords = 0;     // we reset the dataword count, just a sanity check if we get more than 512
0322     }
0323 
0324       else  // now this should be a data word
0325     {
0326       
0327       // raise hell if either
0328       //  - we still don't have a valid FEM ID
0329       //  - the number_of_datawords has reached 512 
0330       //  - the array_pos has reached 2048 
0331 
0332       
0333       if ( last_femid < 0)
0334         {
0335           error_code = 1;
0336           //          std::cout << __FILE__ << "  " << __LINE__ << " Error: got data words without FEM ID at position " << i+5 << "   ";
0337           //this->identify();
0338         }
0339       else if ( number_of_datawords >= 512) 
0340         {
0341           error_code = 2;
0342           //      std::cout << __FILE__ << "  " << __LINE__ << " Error: we aleady have reached the max hit count for FEMID " << last_femid << "    ";
0343           //this->identify();
0344         }
0345       else if ( array_pos >= 4*512) 
0346         {
0347           error_code = 3;
0348           //          std::cout << __FILE__ << "  " << __LINE__ << " Error: we aleady have reached the max total hit count    " ;
0349           //this->identify();
0350         }
0351       else
0352         {
0353 
0354           adc           =         k[i] & 0x07;
0355           chipid        = (k[i]>>3) & 0x3f;
0356           channelnumber = (k[i]>>9) & 0x7f;
0357 
0358           p[array_pos]             = adc;
0359           decoded_data2[array_pos] = last_femid;
0360           decoded_data3[array_pos] = chipid;
0361           decoded_data4[array_pos] = channelnumber;
0362           array_pos++;
0363           number_of_datawords++;
0364         }
0365     }
0366     }
0367   number_of_hits = array_pos;  // we remember how many hits we actually got
0368   *nwout = 4*512;
0369   return p;
0370 }
0371 
0372 // ------------------------------------------------------
0373 
0374 int Packet_fvtx_dcm0::calculate_parity ()
0375 {
0376   int *k;
0377 
0378   k = (int *) findPacketDataStart(packet);
0379   if (k == 0) 
0380     {
0381       return 0;
0382     }
0383 
0384   int dlength = getDataLength();
0385 
0386   int i;
0387   int p = 0;
0388 
0389   // the simulated data calculate the hitformat
0390   // from word 0; real data from word 5
0391   if (  getHitFormat() == IDFVTX_SIM)
0392     {
0393       i = 0;
0394     }
0395   else
0396     {
0397       i = 5;
0398     }
0399   for (; i< dlength-1; i++)
0400     {
0401       p = p ^ (k[i] & 0xffff);
0402     }
0403 
0404   calculated_parity = p;
0405 
0406   if( p == iValue(0, "PARITY") )
0407     {
0408       parity_comparison = 1;
0409     }
0410   else
0411     {
0412       parity_comparison = -1;
0413     }
0414 
0415   return 0;
0416 }
0417 
0418 
0419 
0420 // ------------------------------------------------------
0421 
0422 int *Packet_fvtx_dcm0::decode_misc (int *nwout)
0423 {
0424   int *p,*k;
0425 
0426 
0427   k = (int *) findPacketDataStart(packet);
0428   if (k == 0) 
0429     {
0430       *nwout = 0;
0431       return 0;
0432     }
0433 
0434   int dlength = getDataLength();
0435   
0436   p = new int[7];
0437 
0438   p[0] = k[0] & 0xffff;  // event number
0439   p[1] = k[1] & 0xffff;  // Flag word
0440   p[2] = k[2] & 0xffff;  // DET ID
0441   p[3] = k[3] & 0xffff;  // module address
0442   p[4] = k[4] & 0xffff;  // beam clock counter
0443   p[5] = k[dlength-1] & 0xffff;  // PARITY
0444   p[6] = k[dlength-2] & 0xffff;  // FEM_ERROR_WORD
0445 
0446 
0447   *nwout = 7;
0448   return p;
0449 
0450 }
0451 
0452 // ------------------------------------------------------
0453 
0454 void Packet_fvtx_dcm0::dump ( OSTREAM &os) 
0455 {
0456 
0457   this->identify(os);
0458   
0459   os << " Number of hits                   " << iValue(0,"HITS")  << std::endl;
0460   os << " Event number:                    " << iValue(0,"EVTNR") << std::endl; 
0461   os << " Det id                           " << iValue(0,"DETID") << std::endl;
0462   if ( iValue(0,"ERRORCODE"))
0463     {
0464       os << " Error Code  *********            " << iValue(0,"ERRORCODE");
0465       switch ( iValue(0,"ERRORCODE") )
0466     {
0467     case 1:
0468       os << "  (no FEM ID found)";
0469       break;
0470 
0471     case 2:
0472       os << "  (Max hit count per FEM ID reached)";
0473       break;
0474 
0475     case 3:
0476       os << "  (Max hit count per packet reached)";
0477       break;
0478 
0479     default :
0480       break;
0481     }
0482       os<< std::endl;
0483     }
0484   else 
0485     {
0486       os << " Error Code                       " << iValue(0,"ERRORCODE") << std::endl;
0487     }
0488   os << " module address                   " << iValue(0,"MODADDR") << std::endl;
0489   os << " Flag word                      0x" << std::hex << iValue(0,"FLAG") <<std::dec << std::endl;
0490   os << " beam clock counter               " << iValue(0,"BCLCK") << std::endl;
0491   os << " FEM Error word                   " << iValue(0,"FEMERROR") << std::endl;
0492   os << " Parity                           " << std::hex << iValue(0,"PARITY") << std::dec <<std::endl;
0493   os << " Parity Ok                        ";
0494 
0495   if ( iValue(0,"CHECKPARITY") ==1 )
0496     {
0497       os << "Yes" <<std::endl;
0498     }
0499   else
0500     {
0501       os << "No    (0x" << std::hex << calculated_parity << ")" << std::dec << std::endl;
0502     }
0503 
0504   int hit;
0505 
0506   if  ( iValue(0,"HITS") )  os << " hit # |  FemId  Chip   Chan    ADC" << std::endl;
0507 
0508   for ( hit = 0; hit  < iValue(0,"HITS"); hit++)
0509     {
0510       os << std::setw(6) << hit << " |" 
0511      << std::setw(5) << iValue(hit, 0)  
0512      << std::setw(7) << iValue(hit, 1)  
0513      << std::setw(7) << iValue(hit, 2)  
0514      << std::setw(7) << iValue(hit, 3)  
0515      << std::endl;
0516 
0517 
0518 
0519     }
0520   std::cout << std::endl;
0521   
0522   //   dumpErrorBlock(os);
0523   //  dumpDebugBlock(os);
0524 
0525 }
0526 
0527 
0528 
0529 
0530 
0531 
0532 
0533