Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "oncsSubevent.h"
0002 #include <string.h>
0003 
0004 
0005 const char *oncs_get_mnemonic (const int structure,const int format);
0006 const char *get_type_mnemonic (const int id);
0007 const char *get_evt_mnemonic(const int id);
0008 
0009 
0010 oncsSubevent::oncsSubevent(subevtdata_ptr sevt_ptr)
0011 {
0012   SubeventHdr = sevt_ptr;
0013 
0014   data1_length = 0;
0015   data2_length = 0;
0016   data3_length = 0;
0017   data4_length = 0;
0018 
0019   decoded_data1 = NULL;
0020   decoded_data2 = NULL;
0021   decoded_data3 = NULL;
0022   decoded_data4 = NULL;
0023 
0024   is_data_type = 0;  //assume "p" type first.
0025 
0026 }
0027 
0028 oncsSubevent::~oncsSubevent()
0029 {
0030   if (decoded_data1 != NULL) delete [] decoded_data1;
0031   if (decoded_data2 != NULL) delete [] decoded_data2;
0032   if (decoded_data3 != NULL) delete [] decoded_data3;
0033   if (decoded_data4 != NULL) delete [] decoded_data4;
0034 
0035   if (is_data_type) delete [] (int *) SubeventHdr;
0036 }
0037 
0038 
0039 
0040 // ----------------------------------------------
0041 
0042 int oncsSubevent::getLength() const
0043 {
0044   return SubeventHdr->sub_length;
0045 }
0046 
0047 // ----------------------------------------------
0048 
0049 int oncsSubevent::getDataLength() const
0050 {
0051   return SubeventHdr->sub_length -4;
0052 }
0053 
0054 // ----------------------------------------------
0055 
0056 int oncsSubevent::getIdentifier() const
0057 {
0058   return SubeventHdr->sub_id;
0059 }
0060 
0061 // ----------------------------------------------
0062 
0063 int oncsSubevent::setIdentifier(const int newid) 
0064 {
0065    // check that we have short range; we want the parameter to be int
0066   if ( newid & 0xffff0000) return -1; 
0067   SubeventHdr->sub_id = newid;
0068   return 0;
0069 }
0070 
0071 // ----------------------------------------------
0072 //int oncsSubevent::get_type()
0073 //{
0074 //  return SubeventHdr->sub_type;
0075 //}
0076 
0077 // -----------------------------------------------
0078 int oncsSubevent::getHitFormat() const
0079 {
0080   return SubeventHdr->sub_decoding;
0081 }
0082 
0083 // -----------------------------------------------
0084 int oncsSubevent::getPadding() const
0085 {
0086   return SubeventHdr->sub_padding;
0087 }
0088 
0089 
0090 
0091 void  oncsSubevent::identify( OSTREAM& out ) const
0092 {
0093   out << std::dec
0094       << "Packet " << SETW(5) <<  getIdentifier() 
0095       << " " << SETW(5) << getLength() 
0096       << " -1"  << " (sPHENIX Packet) ";
0097 
0098   out << SETW(3) << getHitFormat() 
0099       << " (" << oncs_get_mnemonic( getStructure(), getHitFormat()) << ")" << std::endl;
0100 }
0101 
0102 
0103 int oncsSubevent::is_pointer_type() const
0104 {
0105   if (is_data_type) return 0;
0106   else return 1;
0107 }
0108 
0109 int oncsSubevent::convert()
0110 {
0111   if (is_data_type) return -1;
0112 
0113   int *tmp;
0114 
0115   tmp = new int[getLength()];
0116   int *from=  (int *) SubeventHdr;
0117   int *to=tmp;
0118   for (int k=0; k< getLength(); k++) 
0119     { 
0120       *to++ = *from++;
0121     }
0122   SubeventHdr = ( subevtdata_ptr )tmp;
0123   is_data_type = 1;
0124   return 0;
0125 
0126 }
0127 
0128 
0129 //------------------------------------------------------
0130 
0131 int   oncsSubevent::iValue(const int ich)
0132 {
0133   // now let's derefence the proxy array. If we didn't decode
0134   // the data until now, we do it now
0135   if (decoded_data1 == NULL )
0136     {
0137       if ( (decoded_data1 = decode(&data1_length))==NULL)
0138         return 0;
0139     }
0140 
0141   // see if our array is long enough
0142   if (ich >= data1_length) return 0;
0143 
0144   return decoded_data1[ich];
0145 }
0146 
0147 // ------------------------------------------------------
0148 
0149 int   oncsSubevent::iValue(const int ich, const char *what)
0150 {
0151   // now let's derefence the proxy array. If we didn't decode
0152   // the data until now, we do it now
0153   if (decoded_data1 == NULL )
0154     {
0155       if ( (decoded_data1 = decode(&data1_length))==NULL)
0156         return 0;
0157     }
0158 
0159   // see if our array is long enough
0160   if (ich >= data1_length) return 0;
0161 
0162   return decoded_data1[ich];
0163 }
0164 
0165 // ------------------------------------------------------
0166 
0167 int   oncsSubevent::iValue(const int ich, const int iy)
0168 {
0169   // now let's derefence the proxy array. If we didn't decode
0170   // the data until now, we do it now
0171   if (decoded_data1 == NULL )
0172     {
0173       if ( (decoded_data1 = decode(&data1_length))==NULL)
0174         return 0;
0175     }
0176 
0177   // see if our array is long enough
0178   if (ich >= data1_length) return 0;
0179 
0180   return decoded_data1[ich];
0181 }
0182 
0183 // ------------------------------------------------------
0184 
0185 float oncsSubevent::rValue(const int ich)
0186 {
0187   // now let's derefence the proxy array. If we didn't decode
0188   // the data until now, we do it now
0189   if (decoded_data1 == NULL )
0190     {
0191       if ( (decoded_data1 = decode(&data1_length))==NULL)
0192         return 0;
0193     }
0194 
0195   // see if our array is long enough
0196   if (ich >= data1_length) return 0;
0197 
0198   return float(decoded_data1[ich]);
0199 }
0200 
0201 // ------------------------------------------------------
0202 
0203 float oncsSubevent::rValue(const int ich, const char *what)
0204 {
0205   // now let's derefence the proxy array. If we didn't decode
0206   // the data until now, we do it now
0207   if (decoded_data1 == NULL )
0208     {
0209       if ( (decoded_data1 = decode(&data1_length))==NULL)
0210         return 0;
0211     }
0212 
0213   // see if our array is long enough
0214   if (ich >= data1_length) return 0;
0215 
0216   return float(decoded_data1[ich]);
0217 }
0218 
0219 // ------------------------------------------------------
0220 
0221 float oncsSubevent::rValue(const int ich, const int iy)
0222 {
0223   // now let's derefence the proxy array. If we didn't decode
0224   // the data until now, we do it now
0225   if (decoded_data1 == NULL )
0226     {
0227       if ( (decoded_data1 = decode(&data1_length))==NULL)
0228         return 0;
0229     }
0230 
0231   // see if our array is long enough
0232   if (ich >= data1_length) return 0;
0233 
0234   return float(decoded_data1[ich]);
0235 }
0236 
0237 // ----------------------------------------------
0238 
0239 int   
0240 oncsSubevent::getArraylength(const char *what)
0241 {
0242   // now let's derefence the proxy array. If we didn't decode
0243   // the data until now, we do it now
0244   if (decoded_data1 == NULL )
0245     {
0246       if ( (decoded_data1 = decode(&data1_length))==NULL)
0247     return -1;
0248     }
0249 
0250   return data1_length;
0251 }
0252 
0253 // ----------------------------------------------
0254 
0255 int
0256 oncsSubevent::fillIntArray (int iarr[],
0257                const int nlen, int *nwout,
0258                const char *what)
0259 {
0260 
0261   if (strcmp(what,"") ==0)
0262     {
0263       // now let's derefence the proxy array. If we didn't decode
0264       // the data until now, we do it now
0265       if (decoded_data1 == NULL )
0266     {
0267       if ( (decoded_data1 = decode(&data1_length))==NULL)
0268         {
0269           *nwout=0;
0270           return -1;
0271         }
0272     }
0273 
0274       // see if our array is long enough
0275       if (nlen < data1_length) return -2;
0276 
0277       // and copy the data to the output array
0278       int *from = decoded_data1;
0279       for (int i=0; i<data1_length; i++) *iarr++ = *from++;
0280 
0281       // tell how much we copied
0282       *nwout = data1_length;
0283     }
0284   
0285   if (strcmp(what,"RAW") ==0)
0286     {
0287       if (nlen < getLength() ) return -2;
0288       int *from = (int *) SubeventHdr;
0289       for (int i=0; i<getLength(); i++) *iarr++ = *from++;
0290 
0291       *nwout = getLength();
0292     }
0293 
0294   if (strcmp(what,"DATA") ==0)
0295     {
0296       if (nlen < getLength() -4 ) return -2;
0297       int *from = &SubeventHdr->data;
0298       for (int i=0; i<getLength() -4; i++) *iarr++ = *from++;
0299 
0300       *nwout = getLength()-4;
0301     }
0302   return 0;
0303 }
0304 
0305 // ------------------------------------------------------
0306 
0307 int
0308 oncsSubevent::fillFloatArray (float rarr[],
0309                  const int nlen, int *nwout,
0310                  const char *what)
0311 {
0312 
0313   // now let's derefence the proxy array. If we didn't decode
0314   // the data until now, we do it now
0315   if (decoded_data1 == NULL )
0316     {
0317       if ( (decoded_data1 = decode(&data1_length))==NULL)
0318     {
0319       *nwout=0;
0320       return -1;
0321     }
0322     }
0323 
0324   // see if our array is long enough
0325   if (nlen < data1_length) return -2;
0326 
0327   // and copy the data to the output array
0328   int *from = decoded_data1;
0329   for (int i=0; i<data1_length; i++) *rarr++ = float (*from++);
0330 
0331   // tell how much we copied
0332   *nwout = data1_length;
0333   return 0;
0334 }
0335 
0336 // ------------------------------------------------------
0337 int*   
0338 oncsSubevent::getIntArray (int *nwout, const char *what)
0339 {
0340 
0341   // now let's derefence the proxy array. If we didn't decode
0342   // the data until now, we do it now
0343   if (decoded_data1 == NULL )
0344     {
0345       if ( (decoded_data1 = decode(&data1_length))==NULL)
0346     {
0347       *nwout=0;
0348       return NULL;
0349     }
0350     }
0351 
0352   int *temp=new int[data1_length];
0353   int is = fillIntArray(temp, data1_length, nwout);
0354   if (is)
0355     {
0356       *nwout=0;
0357       return NULL;
0358     }
0359   return temp;
0360 }
0361 
0362 // ------------------------------------------------------
0363 float*   oncsSubevent::getFloatArray (int *nwout, const char *what)
0364 {
0365   // now let's derefence the proxy array. If we didn't decode
0366   // the data until now, we do it now
0367   if (decoded_data1 == NULL )
0368     {
0369       if ( (decoded_data1 = decode(&data1_length))==NULL)
0370     {
0371       *nwout=0;
0372       return NULL;
0373     }
0374     }
0375 
0376   float *temp=new float[data1_length];
0377   int is = fillFloatArray(temp, data1_length, nwout);
0378   if (is)
0379     {
0380       *nwout=0;
0381       return NULL;
0382     }
0383   return temp;
0384 }
0385 
0386 // ----------------------------------------------
0387 
0388 int oncsSubevent::copyMe(int dest[],  const int maxlength) const
0389 {
0390   
0391   if ( getLength() > maxlength )
0392     {
0393       return 0;
0394     }
0395 
0396   memcpy((void *) dest, (void *) SubeventHdr, 4*getLength() );
0397   return getLength();
0398 }
0399 
0400 
0401 
0402 // now the member functions common to all
0403 // the individial p1, p2, and p4 classes.
0404 
0405 // ------------Constructors first -----------------
0406 
0407 oncsSubevent_w1::oncsSubevent_w1(subevtdata_ptr sevt_ptr)
0408   : oncsSubevent(sevt_ptr)
0409 {}
0410 
0411 oncsSubevent_w2::oncsSubevent_w2(subevtdata_ptr sevt_ptr)
0412   : oncsSubevent(sevt_ptr)
0413 {}
0414 
0415 oncsSubevent_w4::oncsSubevent_w4(subevtdata_ptr sevt_ptr)
0416   : oncsSubevent(sevt_ptr)
0417 {}
0418 
0419 // ---- and the dump routines, which just call the 
0420 // ---- generic dump routines, but may be overloaded
0421 // ---- by the subclasses.
0422 
0423 void oncsSubevent_w1::dump(OSTREAM& out)  { gdump(2,out);}
0424 void oncsSubevent_w2::dump(OSTREAM& out)  { gdump(2,out);}
0425 void oncsSubevent_w4::dump(OSTREAM& out)  { gdump(2,out);}
0426 
0427 void oncsSubevent_w4::gdump(const int i, OSTREAM& out) const
0428 {
0429 
0430   int *SubeventData = &SubeventHdr->data;
0431 
0432   if ( i == EVT_RAW)
0433     {
0434       fwrite(SubeventData, sizeof(int), getDataLength(), stdout);
0435       return;
0436     }
0437 
0438   if ( i == EVT_RAW_WH)
0439     {
0440       fwrite(SubeventHdr, sizeof(int), getLength(), stdout);
0441       return;
0442     }
0443 
0444   unsigned int j;
0445   int l;
0446   identify(out);
0447   
0448   switch (i)
0449     {
0450     case (EVT_HEXADECIMAL):
0451       j = 0;
0452       while (1)
0453     {
0454       out << SETW(5) << j << " |  ";
0455       for (l=0;l<4;l++)
0456         {
0457           out << std::hex << SETW(8) << std::setfill ('0') << SubeventData[j++] << std::setfill(' ') << " " << std::dec;
0458           if (j>=SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) break;
0459         }
0460       out << std::endl;
0461       if (j>=SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) break;
0462     }
0463       break;
0464 
0465     case (EVT_DECIMAL):
0466       j = 0;
0467       while (1)
0468     {
0469       out << std::dec  << SETW(5) << j << " |  ";
0470 
0471       for (l=0;l<6;l++)
0472         {
0473           out << SETW(10) << SubeventData[j++] << " ";
0474           if (j>=SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) break;
0475         }
0476       out << std::endl;
0477       if (j>=SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) break;
0478     }
0479       break;
0480 
0481     default: 
0482       break;
0483     }
0484   out << std::endl;
0485 
0486 }
0487 
0488 // ---------------------------------------------------------------------
0489 
0490 void oncsSubevent_w2::gdump(const int i, OSTREAM& out) const
0491 {
0492   short *SubeventData = (short *) &SubeventHdr->data;
0493 
0494   if ( i == EVT_RAW)
0495     {
0496       fwrite(SubeventData, sizeof(int), getDataLength(), stdout);
0497       return;
0498     }
0499 
0500   if ( i == EVT_RAW_WH)
0501     {
0502       fwrite(SubeventHdr, sizeof(int), getLength(), stdout);
0503       return;
0504     }
0505 
0506   unsigned int j;
0507   int l;
0508 
0509   identify(out);
0510 
0511   switch (i)
0512     {
0513     case (EVT_HEXADECIMAL):
0514       j = 0;
0515       while (1)
0516     {
0517       out << std::dec << SETW(5) << j << " |  ";
0518       for (l=0;l<8;l++)
0519         {
0520           out << std::hex << SETW(4) << std::setfill ('0') << SubeventData[j++] << std::setfill(' ') << " " << std::dec;
0521           if (j>=2*(SubeventHdr->sub_length-SEVTHEADERLENGTH  - SubeventHdr->sub_padding) ) break;
0522         }
0523         out << std::endl;
0524 
0525       if (j>=2*(SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) ) break;
0526     }
0527       break;
0528 
0529     case (EVT_DECIMAL):
0530       j = 0;
0531       while (1)
0532     {
0533       out << std::dec << SETW(5) << j << " |  ";
0534       for (l=0;l<8;l++)
0535         {
0536           out << std::dec << SETW(6) << SubeventData[j++] << " ";
0537           if (j>=2*(SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) ) break;
0538         }
0539       out << std::endl;
0540       if (j>=2*(SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) ) break;
0541     }
0542       break;
0543 
0544     default: break;
0545     }
0546   out << std::endl; 
0547 }
0548 
0549 // ---------------------------------------------------------------------
0550 
0551 void oncsSubevent_w1::gdump(const int i, OSTREAM& out) const
0552 {
0553   char *SubeventData = (char *) &SubeventHdr->data;
0554 
0555 
0556   if ( i == EVT_RAW)
0557     {
0558       fwrite(SubeventData, sizeof(int), getDataLength(), stdout);
0559       return;
0560     }
0561 
0562   if ( i == EVT_RAW_WH)
0563     {
0564       fwrite(SubeventHdr, sizeof(int), getLength(), stdout);
0565       return;
0566     }
0567 
0568   unsigned int j;
0569   int l;
0570 
0571 
0572   char cstring[20];
0573   char *c;
0574   identify(out);
0575 
0576   j = 0;
0577   switch (i)
0578     {
0579     case (EVT_HEXADECIMAL):
0580       while (1)
0581     {
0582       c = cstring;
0583       out << std::dec <<  SETW(5) << j << " |  ";
0584       for (l=0;l<16;l++)
0585         {
0586           if (j < 4*(SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) ) 
0587         {
0588           out << std::hex << SETW(2) << (int) SubeventData[j] << " ";
0589           if (SubeventData[j] >=32 && SubeventData[j] <=127) 
0590             {
0591               *c++ = SubeventData[j];
0592             }
0593           else
0594             {
0595               *c++ = 0x20;
0596             }
0597           out << " ";
0598         }
0599           j++;
0600         }
0601       *c = 0;
0602       out << "  | " << cstring;
0603       out << std::endl;
0604       if (j >= 4*(SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) ) break;
0605     }
0606       break;
0607 
0608     case (EVT_DECIMAL):
0609       while (1)
0610     {
0611       c = cstring;
0612       out << std::dec <<  SETW(5) << j << " |  ";
0613       for (l=0;l<16;l++)
0614         {
0615           if (j < 4*(SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) ) 
0616         {
0617           out  << SETW(3) << (int) SubeventData[j] << " ";
0618           if (SubeventData[j] >=32 && SubeventData[j] <=127) 
0619             {
0620               *c++ = SubeventData[j];
0621             }
0622           else
0623             {
0624               *c++ = 0x20;
0625             }
0626           out << " ";
0627         }
0628           j++;
0629         }
0630       *c = 0;
0631       out << "  | " << cstring;
0632       out << std::endl;
0633       if (j >= 4*(SubeventHdr->sub_length-SEVTHEADERLENGTH - SubeventHdr->sub_padding) ) break;
0634     }
0635       break;
0636       
0637     default: break;
0638     }
0639   out << std::endl;
0640 }
0641