Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "oncsSub_idsrs_v01.h"
0002 #include <cstring>
0003 
0004 #include <arpa/inet.h>
0005 
0006 using namespace std;
0007 
0008 oncsSub_idsrs_v01::oncsSub_idsrs_v01(subevtdata_ptr data)
0009   :oncsSubevent_w4 (data)
0010 {
0011   //  samples = 0;
0012   // nsamples = 0;
0013   //  hybrid = 0;
0014   nhybrids = 0;
0015   
0016 }
0017   
0018 oncsSub_idsrs_v01::~oncsSub_idsrs_v01()
0019 {
0020   //  if ( samples )
0021   //  {
0022   //    delete [] samples;
0023   //    samples = 0;
0024   //    nsamples = 0;
0025   //  }
0026 
0027   std::vector<hybriddata*>::iterator it;
0028   std::vector<report *>::iterator reportit;
0029 
0030 
0031   for ( it = hybridlist.begin(); it != hybridlist.end(); ++it)
0032     {
0033       for ( reportit = (*it)->rowdata.begin(); reportit != (*it)->rowdata.end(); ++reportit)
0034     {
0035       delete (*reportit);
0036     }
0037       delete (*it);
0038     }
0039 
0040 }
0041 
0042 
0043 int *oncsSub_idsrs_v01::decode ( int *nwout)
0044 {
0045   int i;
0046 
0047   unsigned int *d = (unsigned int *) &SubeventHdr->data;  // here begins the payload
0048 
0049   int dlength = getLength()-4 - getPadding();
0050 
0051   for ( i = 0; i< dlength; i++)
0052     {
0053       hybriddata *hd = new hybriddata;
0054       if ( d[i] == 0xfafafafa ) break;  // end of payload
0055       unsigned int dh = ntohl(d[i]);
0056       int framecounter = (dh & 0xff);
0057       //cout << "framecounter: " << framecounter << endl;
0058       
0059       hd->framecounter = framecounter;
0060       
0061       dh = ntohl(d[i+1]);
0062       int header = dh;
0063 
0064       unsigned char c = (header >>24) & 0xff;
0065       hd->desc[0] = c;
0066       c = (header >>16) & 0xff;
0067       hd->desc[1] = c;
0068       c = (header >>8) & 0xff;
0069       hd->desc[2] = c;
0070       hd->desc[3] = 0;
0071 
0072       unsigned int channel = header & 0xff;
0073       //cout << " channel nr: " << channel << endl;
0074       hd->hdmi = channel;
0075 
0076       unsigned int w = ntohl(d[i+2]);
0077       int words = w & 0xffff;
0078       //cout << " words: " << words << endl;
0079 
0080       int j = 0;  // here begin the samples;
0081 
0082       while ( d[j + i +3] != 0xf000f000 && j <  words )
0083     {
0084       hd->adc.push_back(d[j+i+3] & 0x0fff);
0085       hd->adc.push_back( (d[j+i+3] >> 16) & 0x0fff);
0086       j++;
0087     }
0088 
0089 
0090       //cout << " EOP marker " << hex <<  d[j + i +3] << dec << endl;
0091       hd->words = hd->adc.size();
0092       //cout << " words: " << hd->words << "  " << words << endl;
0093 
0094       analyze ( hd);
0095 
0096       hybridlist.push_back(hd);
0097       nhybrids++;
0098       i+= 3+j;
0099     }
0100 
0101   *nwout = 0;
0102   return 0;
0103 }
0104 
0105 
0106 int oncsSub_idsrs_v01::analyze (hybriddata * hd)
0107 {
0108   int i;
0109 
0110   int firstfound = 0;
0111   int onecount = 0;
0112 
0113   int current_report = 0;
0114 
0115   for ( i = 0; i < hd->words-( 128+8); i++)
0116     {
0117 
0118       if ( firstfound) 
0119     {
0120       add_report ( hd, i, current_report++);
0121       i += 11+128;
0122     }
0123       else
0124     {
0125       if ( hd->adc[i] < 1220) onecount++;
0126       if ( onecount == 3) 
0127         {
0128           // cout << " sequence start at " << i -2 << endl;
0129           add_report ( hd, i-2, current_report++);
0130           onecount = 0;
0131 
0132           i += (  9 + 128);
0133           firstfound = 1;
0134         
0135         }
0136       if ( onecount && hd->adc[i] >= 1220) 
0137         {
0138           onecount = 0;
0139         }
0140     }
0141     }
0142   return 0;
0143 }
0144 
0145 int oncsSub_idsrs_v01::add_report ( hybriddata * hd, const int start, const int nreport)
0146 {
0147   int i;
0148 
0149   report *r = new report;
0150 
0151   r->error = 1;
0152   r->n = nreport;
0153 
0154   if ( hd->adc[start + 11] < 1220) 
0155     {
0156       r->error = 0;
0157     }
0158   else if ( hd->adc[start + 11] > 3000)
0159     {
0160       r->error = 1;
0161     }
0162   else 
0163     {
0164       r->error = -1;  // funny bit
0165     }
0166 
0167   int acode = 0;
0168   for ( i = 0; i < 8; i++)
0169     {
0170       if (hd->adc[start + 3 + i] < 1220)
0171     {
0172       acode = ( acode << 1)+1;
0173     }
0174       else if (hd->adc[ start + 3 + i] > 2980)
0175     {
0176       acode = ( acode << 1);
0177     }
0178       else 
0179     {
0180       // cout << "bit expected but got " << hd->adc[start + 3 + i] << " as bit " << i << endl;
0181       acode = ( acode << 1);
0182     }
0183     }
0184   r->address = acode;
0185 
0186 
0187    for (i = 0; i < 128; i++)
0188    {
0189      int channel = 32 * ( i % 4 ) + 8 * ( i >> 2) - 31 * ( i >> 4);
0190      r->adc[channel] = hd->adc[start + 12+i];
0191    }
0192   hd->rowdata.push_back(r);
0193 
0194   return 0;
0195 }
0196 
0197 
0198   
0199 int oncsSub_idsrs_v01::iValue(const int hybrid,const char *what)
0200 {
0201 
0202   std::vector<hybriddata*>::iterator it;
0203   if ( nhybrids == 0 ) decoded_data1 = decode(&data1_length);
0204 
0205   if ( strcmp(what,"NHYBRIDS") == 0 )
0206   {
0207     return hybridlist.size();
0208   }
0209 
0210   else if ( strcmp(what,"NSAMPLES") == 0 )
0211   {
0212 
0213     for ( it = hybridlist.begin(); it != hybridlist.end(); ++it)
0214       {
0215     if ( (*it)->hdmi == hybrid )
0216       {
0217         return (*it)->rowdata.size();;
0218       }
0219       }
0220 
0221 
0222   }
0223 
0224   return 0;
0225 
0226 }
0227 
0228 
0229 int oncsSub_idsrs_v01::iValue(const int ich,const int hybrid, const char *what)
0230 {
0231 
0232   if ( nhybrids == 0 ) decoded_data1 = decode(&data1_length);
0233 
0234   std::vector<hybriddata*>::iterator it;
0235 
0236   if ( ich < 0) return 0;
0237 
0238   if ( strcmp(what,"RAWSAMPLES") == 0 )
0239   {
0240         
0241     for ( it = hybridlist.begin(); it != hybridlist.end(); ++it)
0242       {
0243     if ( (*it)->hdmi == hybrid )
0244       {
0245         if ( ich >= (*it)->words) return 0;
0246         return (*it)->adc[ich];
0247       }
0248       }
0249   }
0250 
0251     return 0;
0252 }
0253 
0254 int oncsSub_idsrs_v01::iValue(const int ich, const int tsample, const int hybrid)
0255 {
0256 
0257   if ( ich < 0 || ich > 127) return 0;
0258   if ( nhybrids == 0 ) decoded_data1 = decode(&data1_length);
0259  
0260   std::vector<hybriddata*>::iterator it;
0261 //  std::vector<report *>::iterator reportit;
0262 
0263   for ( it = hybridlist.begin(); it != hybridlist.end(); ++it)
0264     {
0265 
0266       if ( (*it)->hdmi == hybrid)
0267     {
0268 
0269       //cout << __LINE__ << "  " << __FILE__ << " tsample " << tsample << "  " << (*it)->rowdata.size() << endl;
0270 
0271       if ( (*it)->rowdata.size()  < (unsigned int)(tsample +1)) return 0;
0272 
0273       report *r  = (*it)->rowdata[tsample];
0274       return r->adc[ich];
0275 
0276     }
0277     }
0278   return 0;
0279 }
0280 
0281 
0282 
0283 void  oncsSub_idsrs_v01::dump ( OSTREAM& os )
0284 {
0285 
0286   identify(os);
0287 
0288   int i;
0289   int is =  iValue(0,"NHYBRIDS");
0290 
0291 
0292   os << "Number of Hybrids: " << is << std::endl;
0293  
0294   std::vector<hybriddata*>::iterator it;
0295   std::vector<report *>::iterator reportit;
0296 
0297   for ( it = hybridlist.begin(); it != hybridlist.end(); ++it)
0298     {
0299       os << std::endl;
0300       os << "Framecounter: " << (*it)->framecounter << std::endl;
0301       os << "HDMI Channel: " << (*it)->hdmi << std::endl;
0302       os << "Description:  " << (*it)->desc << std::endl;
0303       os << "Words:        " << (*it)->words << std::endl;
0304       os << "Time samples: " << (*it)->rowdata.size() << std::endl;
0305 
0306 //       for ( i = 0; i < (*it)->words; i++) 
0307 //  {
0308 //    os << setw(6) << i << setw(6) << (*it)->adc[i];
0309 //    if ( (*it)->adc[i] < 1200 ) cout << "  D1";
0310 //    else if ( (*it)->adc[i] > 3000 ) cout << "  D0";
0311 //    os << endl;
0312 //  }
0313 
0314       if ( (*it)->rowdata.size() > 0) 
0315     {
0316       os << "Sample ";
0317       for ( reportit = (*it)->rowdata.begin(); reportit != (*it)->rowdata.end(); ++reportit)
0318         {
0319           os << setw(5) << (*reportit)->n;
0320         }
0321       os << endl;
0322       os << "Addr   ";
0323 
0324       for ( reportit = (*it)->rowdata.begin(); reportit != (*it)->rowdata.end(); ++reportit)
0325         {
0326           os << setw(5) << (*reportit)->address;
0327         }
0328       os << endl;
0329       os << "Error  ";
0330       
0331       for ( reportit = (*it)->rowdata.begin(); reportit != (*it)->rowdata.end(); ++reportit)
0332         {
0333           os << setw(5) << (*reportit)->error;
0334         }
0335       os << endl;
0336       os << "       ----------------------------------------------------------------------------------------" << endl;
0337 
0338       
0339       for ( i = 0; i < 128; i++)
0340         { 
0341           os << setw(4) << i << " | ";
0342           
0343           for ( reportit = (*it)->rowdata.begin(); reportit != (*it)->rowdata.end(); ++reportit)
0344         {
0345           os << setw(5) << (*reportit)->adc[i];
0346         }
0347           os << endl;
0348         }
0349     }
0350     }
0351       
0352 }
0353