Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "oncsSub_iduppetdata.h"
0002 #include <cstring>
0003 
0004 #include <arpa/inet.h>
0005 
0006 using namespace std;
0007 
0008 oncsSub_iduppetdata::oncsSub_iduppetdata(subevtdata_ptr data)
0009   :oncsSubevent_w4 (data)
0010 {
0011   samples = 0;
0012   d_time = 0;
0013   d_timelength = 0;
0014   lval = 0;
0015   tval = 0;
0016   serialnumber = 0;
0017   udpheader1 = 0;
0018   udpheader2 = 0;
0019 
0020 }
0021   
0022 oncsSub_iduppetdata::~oncsSub_iduppetdata()
0023 {
0024   if ( d_timelength )
0025     {
0026       delete [] d_time;
0027       d_time = 0;
0028     }
0029   if ( lval)
0030     {
0031       delete [] lval;
0032       lval = 0;
0033     }
0034   if ( tval)
0035     {
0036       delete [] tval;
0037       tval = 0;
0038     }
0039 }
0040 
0041 
0042 int *oncsSub_iduppetdata::decode ( int *nwout)
0043 {
0044   int *p;
0045 
0046 
0047   int i;
0048 
0049   unsigned int *SubeventData = (unsigned int *) &SubeventHdr->data;
0050 
0051   unsigned short *sdata = ( unsigned short *) &SubeventData[3];
0052 
0053   int dlength = getLength()-4 - getPadding();
0054 
0055   samples = (dlength - 3) /2;
0056 
0057   p = new int [samples];             // block id
0058 
0059   decoded_data2 = new int [samples]; //  crystal id
0060   data2_length = samples;
0061 
0062   decoded_data3 = new int [samples];  // rfgate
0063   data3_length = samples;
0064 
0065   decoded_data4 = new int [samples];  // cardiac
0066   data4_length = samples;
0067 
0068   lval  = new long long [samples];  // raw samples
0069   tval  = new long long [samples];  // binary time
0070 
0071   d_time = new double [samples];     // time
0072   d_timelength = samples;
0073 
0074   serialnumber = ntohl(SubeventData[0]);
0075   udpheader1 = ntohl(SubeventData[1]);
0076   udpheader2 = ntohl(SubeventData[2]);
0077 
0078   int k = 0;
0079 
0080   for ( i = 0; i < 2*(dlength-4); i+=4 ) 
0081     {
0082       
0083       unsigned long long y;
0084       unsigned long long x64 = ntohs( sdata[i+3]);
0085       x64 <<= 48;
0086 
0087       y =  ntohs( sdata[i+2]);
0088       y <<=32;
0089       x64 |= y;
0090 
0091       y =  ntohs( sdata[i+1]);
0092       y <<=16;
0093       x64 |= y;
0094 
0095       y =  ntohs( sdata[i]);
0096       x64 |= y;
0097 
0098 
0099       unsigned long long t = x64 & 0xfffffffffffL;
0100       unsigned int crystalid = ( x64 >> 52) & 0x1f;
0101       unsigned int block = ( x64 >> 57) & 0x1f;
0102       unsigned int rfgate =   ( x64 >> 62) & 0x1;
0103       unsigned int cardiac =   ( x64 >> 63) & 0x1;
0104 
0105       p[k] = block;
0106       decoded_data2[k] = crystalid;
0107       lval[k] = x64;
0108       tval[k] = t;
0109       d_time[k] = t;
0110       decoded_data3[k] = rfgate;
0111       decoded_data4[k] = cardiac;
0112       k++;
0113 
0114       //      cout << k<< endl;
0115 
0116       // cout << hex << x << dec << "   "  
0117       //       << " time:  " << t 
0118       //       << " block: " << block 
0119       //       << " chan:  " << channel 
0120       //       << " rfgate " << rfgate
0121       //       << " Cardiac " << cardiac
0122       //       <<  endl;
0123     }
0124   
0125   //  samples = k;
0126   *nwout = k;
0127   return p;
0128 }
0129   
0130 int oncsSub_iduppetdata::iValue(const int ich,const char *what)
0131 {
0132 
0133   if ( decoded_data1 == 0 ) decoded_data1 = decode(&data1_length);
0134 
0135   if ( strcmp(what,"SAMPLES") == 0 )
0136   {
0137     return samples;
0138   }
0139 
0140   if ( strcmp(what,"BLOCKID") == 0 )
0141   {
0142     if ( ich < 0 || ich >= samples) return 0;
0143     return decoded_data1[ich];
0144   }
0145 
0146   if ( strcmp(what,"CRYSTALID") == 0 )
0147   {
0148     if ( ich < 0 || ich >= samples) return 0;
0149     return decoded_data2[ich];
0150   }
0151 
0152   if ( strcmp(what,"RFGATE") == 0 )
0153   {
0154     if ( ich < 0 || ich >= samples) return 0;
0155     return decoded_data3[ich];
0156   }
0157 
0158   if ( strcmp(what,"CARDIAC") == 0 )
0159   {
0160     if ( ich < 0 || ich >= samples) return 0;
0161     return decoded_data4[ich];
0162   }
0163 
0164   return 0;
0165 
0166 }
0167 
0168 double oncsSub_iduppetdata::dValue(const int ich)
0169 {
0170 
0171   if ( decoded_data1 == 0 ) decoded_data1 = decode(&data1_length);
0172 
0173   if ( ich < 0 || ich >= samples) return 0;
0174     return d_time[ich];
0175 }
0176 
0177 long long  oncsSub_iduppetdata::lValue(const int ich)
0178 {
0179 
0180   if ( decoded_data1 == 0 ) decoded_data1 = decode(&data1_length);
0181 
0182   if ( ich < 0 || ich >= samples) return 0;
0183     return lval[ich];
0184 }
0185 
0186 void  oncsSub_iduppetdata::dump ( OSTREAM& os )
0187 {
0188 
0189  
0190   int i;
0191   int is =  iValue(0,"SAMPLES");
0192 
0193   //  os << "Samples: " << iValue(0,"SAMPLES") << std::endl;
0194 
0195   os << " sample  B id    C id   time   ( " << is << " Samples )" << std::endl;
0196   
0197   for ( i = 0; i < is ; i++)
0198     {
0199       int prec = os.precision();
0200 
0201       os << std::setw(5) << i << "  |  "; 
0202  
0203       os << std::setw(4) << iValue(i,"BLOCKID") << "  ";
0204       os << std::setw(4) << iValue(i,"CRYSTALID") << "  ";
0205       os << std::setw(24) << std::setprecision(24) << hex << tval[i]
0206      << dec      << std::setprecision(prec) << "   " ;
0207       os << std::setw(24) << std::setprecision(24) << hex  << lValue(i) << dec
0208      << std::setprecision(prec) ;
0209     
0210       os << std::endl;
0211     }
0212   os << std::endl;
0213 }
0214