Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "oncsSub_idcaenv1742.h"
0002 #include <cstring>
0003 
0004 
0005 oncsSub_idcaenv1742::oncsSub_idcaenv1742(subevtdata_ptr data)
0006   :oncsSubevent_w4 (data)
0007 {
0008   samples = 0;
0009   dlength = 0;
0010   EvtTimeTag = 0;
0011   LVDSPattern = 0;
0012   evnr = 0;
0013   freq=3;
0014   group_mask=0;
0015   int i;
0016   for ( i=0; i< 4; i++)
0017     {
0018       index_cell[i] = 0;
0019       tr_present[i] = 0;
0020       GroupTriggerTime[i] = 0;
0021     }
0022 
0023 }
0024   
0025 int *oncsSub_idcaenv1742::decode ( int *nwout)
0026 {
0027   int *p;
0028 
0029 
0030   unsigned int *SubeventData = (unsigned int *) &SubeventHdr->data;
0031 
0032 
0033   // the first word must have  0xa in the MSB, and
0034   // has the total payload size in bits 0-27  
0035   if ( (( *SubeventData >> 28)  & 0xf ) != 0xa )
0036     {
0037       std::cout << "error in data structure" << std::endl;
0038       return 0;
0039     }
0040   dlength  = *SubeventData  & 0x0fffffff; 
0041  
0042   // word 1 has the group enable pattern
0043   group_mask = SubeventData[1] & 0xf;
0044 
0045   LVDSPattern = (SubeventData[1]>>8) & 0xffff;
0046   
0047   // word 2 has the event counter in bits 0-21
0048   evnr = SubeventData[2] & 0x3fffff;
0049   //  std::cout << "Evnt nr: " << evnr << std::endl;
0050 
0051   EvtTimeTag = (unsigned int) SubeventData[3];
0052       
0053   // before we go through the groups indexed by group_index,
0054   // we take a peek into the first group to figure out how many samples we have.
0055   // the sample count is the same for all groups, although each group encodes this  
0056   // value again. We use this value here up front to allocate the right amount of memory
0057   // for the decoded waveforms. 
0058 
0059   int size = SubeventData[4] & 0xfff;
0060   samples = size / 3;
0061 
0062   p = new int [ samples * 8 * 4];
0063   memset(p, 0, samples * 8 * 4 * sizeof(int));
0064 
0065   // the trigger waveform, if any
0066   int *p_tr = new int [ samples  * 4];
0067   memset(p_tr, 0, samples * 4 * sizeof(int));
0068   decoded_data2 = p_tr;
0069   data2_length = samples *4;
0070 
0071 
0072   // we also extract the sampling frequency here (encoded in each group but 
0073   // the same for all)
0074   freq = (SubeventData[4] >> 16) & 3;
0075 
0076   
0077   //now we go through the groups
0078   int group_offset = 4;
0079   int group_nr;
0080 
0081   for ( group_nr=0; group_nr < 4; group_nr++)
0082     {
0083       int pos = 0;
0084 
0085       if  ( (group_mask >> group_nr) &1) // we have that group present
0086     {
0087       unsigned int *groupdata = &(SubeventData[group_offset]); // first group
0088       tr_present[group_nr] = (groupdata[0] >> 12) & 1;
0089       index_cell[group_nr] = (groupdata[0] >> 20) & 0x3ff;
0090 
0091 
0092       // std::cout << "group " << group_nr << " size:  " << size << std::endl;
0093       // std::cout << "contains_tr: " << tr_present[group_nr] << std::endl;
0094       // std::cout << "frequency  : " << freq << std::endl;
0095       // std::cout << "index cell : " << index_cell[group_nr] << std::endl;
0096 
0097       int s, ch;
0098       pos = 1;
0099 
0100       for ( s = 0; s < samples; s++ )
0101         {
0102           ch = 0;
0103           p[group_nr*samples*8 + samples * ch++ + s] =( groupdata[pos] & 0xfff);
0104           p[group_nr*samples*8 + samples * ch++ + s] =(groupdata[pos] >> 12) & 0xfff;
0105           p[group_nr*samples*8 + samples * ch++ + s] = ((groupdata[pos] >> 24) & 0xff) + ( (groupdata[pos+1] & 0xf)<<8);
0106           
0107           p[group_nr*samples*8 + samples * ch++ + s] =(groupdata[pos+1] >> 4) & 0xfff;
0108           p[group_nr*samples*8 + samples * ch++ + s] =(groupdata[pos+1] >> 16) & 0xfff;
0109           p[group_nr*samples*8 + samples * ch++ + s] =((groupdata[pos+1] >> 28) & 0xf) + ( (groupdata[pos+2] & 0xff)<<4);
0110           
0111           p[group_nr*samples*8 + samples * ch++ + s] =(groupdata[pos+2] >> 8) & 0xfff;
0112           p[group_nr*samples*8 + samples * ch++ + s] =(groupdata[pos+2] >> 20) & 0xfff;
0113           pos +=3;
0114 
0115         }
0116       if ( tr_present[group_nr])
0117         {
0118           s = 0;
0119           while (s < samples)
0120         {
0121           p_tr[group_nr*samples + s++] = ( groupdata[pos] & 0xfff);
0122           p_tr[group_nr*samples + s++] =(groupdata[pos] >> 12) & 0xfff;
0123           p_tr[group_nr*samples + s++] = ((groupdata[pos] >> 24) & 0xff) + ( (groupdata[pos+1] & 0xf)<<8);
0124           
0125           p_tr[group_nr*samples + s++] =(groupdata[pos+1] >> 4) & 0xfff;
0126           p_tr[group_nr*samples + s++] =(groupdata[pos+1] >> 16) & 0xfff;
0127           p_tr[group_nr*samples + s++] =((groupdata[pos+1] >> 28) & 0xf) + ( (groupdata[pos+2] & 0xff)<<4);
0128           
0129           p_tr[group_nr*samples + s++] =(groupdata[pos+2] >> 8) & 0xfff;
0130           p_tr[group_nr*samples + s++] =(groupdata[pos+2] >> 20) & 0xfff;
0131           pos +=3;
0132         }
0133         }
0134       GroupTriggerTime[group_nr] = groupdata[pos] & 0x3fffffff;
0135       group_offset += pos + 1;
0136       
0137     }
0138     }
0139 
0140   // now see if we have an extended trigger time setting
0141   if ( GroupTriggerTime[0] != GroupTriggerTime[1])
0142     {
0143       GroupTriggerTime[0] |= ( GroupTriggerTime[1] << 30);
0144       GroupTriggerTime[1] = 0;
0145     }
0146   if ( GroupTriggerTime[2] != GroupTriggerTime[3])
0147     {
0148       GroupTriggerTime[2] |= ( GroupTriggerTime[3] << 30);
0149       GroupTriggerTime[3] = 0;
0150     }
0151   
0152   *nwout = samples*8 *4;
0153 
0154   return p;
0155 
0156 }
0157 
0158 int oncsSub_idcaenv1742::iValue(const int ch)
0159 {
0160 
0161   if ( decoded_data1 == 0 ) decoded_data1 = decode(&data1_length);
0162 
0163   if ( ch < 0 || ch >= data1_length ) return 0;
0164 
0165   return decoded_data1[ch];
0166 
0167 }
0168 
0169 int oncsSub_idcaenv1742::iValue(const int sample, const int ch)
0170 {
0171 
0172   if ( decoded_data1 == 0 ) decoded_data1 = decode(&data1_length);
0173 
0174   //if ( ch < 0 || ch >= 32 ) return 0;
0175   if ( ch < 0 || ch >= 36 ) return 0;
0176   if ( sample < 0 || sample >= samples ) return 0;
0177 
0178   //return decoded_data1[ch*samples + sample];
0179   return (ch < 32 ? decoded_data1[ch*samples + sample] : decoded_data2[(ch-32)*samples + sample]);
0180 
0181 }
0182 
0183 int oncsSub_idcaenv1742::iValue(const int n,const char *what)
0184 {
0185 
0186   if ( decoded_data1 == 0 ) decoded_data1 = decode(&data1_length);
0187 
0188   if ( strcmp(what,"SAMPLES") == 0 )
0189   {
0190     return samples;
0191   }
0192 
0193   if ( strcmp(what,"EVNR") == 0 )
0194   {
0195     return evnr;
0196   }
0197 
0198   if ( strcmp(what,"TR0") == 0 )
0199   {
0200     if ( n <0 || n >=samples) return 0;
0201 
0202     return decoded_data2[n];
0203   }
0204 
0205   if ( strcmp(what,"TR1") == 0 )
0206   {
0207     if ( n <0 || n >=samples) return 0;
0208 
0209     return decoded_data2[2*samples + n];
0210   }
0211 
0212   if ( strcmp(what,"TR0-0") == 0 )
0213   {
0214     if ( n <0 || n >=samples) return 0;
0215 
0216     return decoded_data2[            n];
0217   }
0218   if ( strcmp(what,"TR0-1") == 0 )
0219   {
0220     if ( n <0 || n >=samples) return 0;
0221 
0222     return decoded_data2[1*samples + n];
0223   }
0224   if ( strcmp(what,"TR1-0") == 0 )
0225   {
0226     if ( n <0 || n >=samples) return 0;
0227 
0228     return decoded_data2[2*samples + n];
0229   }
0230   if ( strcmp(what,"TR1-1") == 0 )
0231   {
0232     if ( n <0 || n >=samples) return 0;
0233 
0234     return decoded_data2[3*samples + n];
0235   }
0236 
0237   if ( strcmp(what,"INDEXCELL") == 0 )
0238   {
0239     if ( n <0 || n >=4) return 0;
0240 
0241     return index_cell[n];
0242   }
0243 
0244   if ( strcmp(what,"TRPRESENT") == 0 )
0245   {
0246     if ( n <0 || n >=4) return 0;
0247 
0248     return tr_present[n];
0249   }
0250 
0251   if ( strcmp(what,"FREQUENCY") == 0 )
0252   {
0253     return freq;
0254   }
0255 
0256   if ( strcmp(what,"GROUPPRESENT") == 0 )
0257   {
0258     if ( n <0 || n >=4) return 0;
0259     return (group_mask >> n) &1;
0260   }
0261 
0262   if ( strcmp(what,"LVDSPATTERN") == 0 )
0263   {
0264     return LVDSPattern;
0265   }
0266 
0267   if ( strcmp(what,"EVENTTIME") == 0 )
0268   {
0269     return EvtTimeTag;
0270   }
0271 
0272   // if ( strcmp(what,"GROUPTRIGGERTIME") == 0 )
0273   // {
0274   //   if ( n <0 || n >=4) return 0;
0275   //   return GroupTriggerTime[n];
0276   // }
0277 
0278 
0279   return 0;
0280 
0281 }
0282 
0283 long long oncsSub_idcaenv1742::lValue(const int n, const char *what)
0284 {
0285   if ( decoded_data1 == 0 ) decoded_data1 = decode(&data1_length);
0286   
0287   if ( strcmp(what,"GROUPTRIGGERTIME") == 0 )
0288   {
0289     if ( n <0 || n >=4) return 0;
0290     return GroupTriggerTime[n];
0291   }
0292 
0293   return 0;
0294 }
0295 
0296 
0297 
0298 void  oncsSub_idcaenv1742::dump ( OSTREAM& os )
0299 {
0300   int i,j;
0301   //  int *SubeventData = &SubeventHdr->data;
0302   
0303   os << "Samples:  " << iValue(0,"SAMPLES") << std::endl;
0304   os << "Evt Nr:   " << iValue(0,"EVNR") << std::endl;
0305   os << "Evt Time: " << iValue(0,"EVENTTIME") << std::endl;
0306   //os << "LVDS:      " << iValue(0,"LVDSPATTERN") << std::endl;
0307   int f = iValue(0,"FREQUENCY") ;
0308   os << "Sample Frequency ";
0309   switch (f)
0310     {
0311     case 0:
0312       os << " 5 GS/s ";
0313       break;
0314 
0315     case 1:
0316       os << " 2.5 GS/s ";
0317       break;
0318 
0319     case 2:
0320       os << " 1 GS/s ";
0321       break;
0322 
0323     default:
0324       os << " Unknown ";
0325       break;
0326     }
0327   os << "("<< f << ")" << std::endl;
0328 
0329   os << "Group present:           ";
0330   for ( i = 0; i < 4; i++)
0331     {
0332       os<< iValue(i, "GROUPPRESENT") << "  ";
0333     }
0334   os << std::endl;
0335 
0336 
0337   os << "contains trigger sample: ";
0338   for ( i = 0; i < 4; i++)
0339     {
0340       os<< iValue(i, "TRPRESENT") << "  ";
0341     }
0342   os << std::endl;
0343 
0344   os << "index cell:              ";
0345   for ( i = 0; i < 4; i++)
0346     {
0347       os<<  iValue(i, "INDEXCELL") << "  ";
0348     }
0349   os << std::endl;
0350 
0351   os << "Group Trigger time       ";
0352   for ( i = 0; i < 4; i++)
0353     {
0354       os<<  std::setw(10) << lValue(i, "GROUPTRIGGERTIME") << "  ";
0355     }
0356   os << std::endl;
0357 
0358   
0359   for ( i = 0; i < samples ; i++)
0360     {
0361       os  << std::setw(4) << i << " |  ";
0362 
0363       for ( j = 0; j < 32 ; j++)
0364     {
0365       
0366       os << std::setw(4) << iValue(i,j) << " ";
0367     }
0368       os << " tr: " << std::setw(5) << iValue(i, "TR0") << " " << std::setw(5) << iValue(i, "TR1")<< std::endl;
0369     }
0370   
0371   os << std::endl;
0372 }