Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include <packet_gl1p.h>
0002 #include <string.h>
0003 
0004 Packet_gl1p::Packet_gl1p(PACKET_ptr data)
0005   : Packet_w4(data)
0006 {
0007   sgl1p=0;
0008 }
0009 
0010 Packet_gl1p::~Packet_gl1p()
0011 {
0012   if(sgl1p)
0013     {
0014       delete [] sgl1p;
0015     }
0016 }
0017 
0018 int *Packet_gl1p::decode ( int *nwout)
0019 {
0020   int *p,*k;
0021   int i, olength;
0022   int temp[MAX_OUTLENGTH];
0023   
0024   int dlength = getDataLength();
0025 
0026   int status = decode_gl1p( temp
0027                   ,(int *)  findPacketDataStart(packet) 
0028                   ,dlength
0029                   ,MAX_OUTLENGTH, &olength);
0030 
0031   if (status || olength<=0 ) return NULL;
0032  
0033   p = new int[olength];
0034   k = p;
0035   for (i =0; i<olength; i++) *k++ = temp[i];
0036   *nwout = olength;
0037   return p;
0038 }
0039 int Packet_gl1p::iValue(const int ich, const int what)
0040 {// int what is used hear instead of char* what to reduce data taking time. 
0041   if(!sgl1p) demangle();
0042   if(!sgl1p){
0043     COUT<<"Failed to fill data structure"<<std::endl;
0044     return 0;
0045   }
0046   switch (what)
0047     {
0048     case GL1P_HEADER:
0049       {
0050     if(ich<0 || ich>= nGL1Pboards) return 0;
0051     else return sgl1p[ich].header;
0052       }
0053     case GL1P_EVNUMBER:
0054       {
0055     if(ich<0 || ich>= nGL1Pboards) return 0;
0056     else return sgl1p[ich].ev_number;
0057       }
0058     case GL1P_MODEBIT:
0059       {
0060     if(ich<0 || ich>= nGL1Pboards) return 0;
0061     else return sgl1p[ich].modebit;
0062       }
0063     case GL1P_CLOCK:    
0064       {// it is supposed that the number of "clock counters" is 4*nGL1Pboards.
0065     //For example, to get Clock Counter for Scaler A of GL1P board 2 call iValue(5,GL1P_CLOCK)
0066     if(ich<0 || ich>= 4*nGL1Pboards) return -1;
0067     else return sgl1p[ich/4].clock[ich%4];
0068       }
0069     case GL1P_SCALER:   
0070       {//Scaler value. The same rule about ich as above.
0071     if(ich<0 || ich>= 4*nGL1Pboards) return 0;
0072     else return sgl1p[ich/4].scaler[ich%4];
0073       }
0074     default:
0075       return 0;
0076     }
0077 }
0078 int Packet_gl1p::iValue(const int ich, const char *what)
0079 {
0080 
0081   
0082   if(!sgl1p) demangle();
0083   if(!sgl1p){
0084     COUT<<"Failed to fill data structure"<<std::endl;
0085     return 0;
0086   }
0087 
0088   if (!strcmp(what,"HEADER"))
0089     {
0090     if(ich<0 || ich>= nGL1Pboards) return 0;
0091     else return sgl1p[ich].header;
0092     }
0093 
0094   else if (!strcmp(what,"EVNUMBER"))
0095     {
0096     if(ich<0 || ich>= nGL1Pboards) return 0;
0097     else return sgl1p[ich].ev_number;
0098     }
0099   else if (!strcmp(what,"MODEBIT"))
0100     {
0101     if(ich<0 || ich>= nGL1Pboards) return 0;
0102     else return sgl1p[ich].modebit;
0103     }
0104   else if (!strcmp(what,"CLOCK"))
0105     {
0106     if(ich<0 || ich>= 4*nGL1Pboards) return -1;
0107     else return sgl1p[ich/4].clock[ich%4];
0108     }
0109   else if (!strcmp(what,"SCALER"))
0110     {
0111     if(ich<0 || ich>= 4*nGL1Pboards) return 0;
0112     else return sgl1p[ich/4].scaler[ich%4];
0113     }
0114   else return 0;
0115 
0116 }
0117 /** function fillIntArray fills destination[4*nGL1Pboards] if what = "CLOCK"
0118 or "SCALER", and destination[nGL1Pboards] if what = "HEADER", "EVNUMBER" or
0119 "MODEBIT" */
0120 int Packet_gl1p::fillIntArray (int destination[],    // the data go here 
0121                    const int length,      // space we have in destination
0122                    int * nw,              // words actually used
0123                    const char * what) // type of data (see above)
0124 {
0125   int i;
0126   if(!sgl1p) demangle();
0127   if(!sgl1p){
0128     COUT<<"Failed to fill data structure"<<std::endl;
0129     return -1;
0130   }
0131   if (!strcmp(what,"CLOCK"))
0132     {
0133       if(length>4*nGL1Pboards)*nw=4*nGL1Pboards;
0134       else *nw=length;
0135       for(i=0;i<*nw;i++)
0136     destination[i]=sgl1p[i/4].clock[i%4];
0137     }
0138   else if (!strcmp(what,"SCALER"))
0139     {
0140       if(length>4*nGL1Pboards)*nw=4*nGL1Pboards;
0141       else *nw=length;
0142       for(i=0;i<*nw;i++)
0143     destination[i]=sgl1p[i/4].scaler[i%4];
0144     }
0145   else if (!strcmp(what,"HEADER"))
0146     {
0147       if(length>nGL1Pboards)*nw=nGL1Pboards;
0148       else *nw=length;
0149       for(i=0;i<*nw;i++)
0150     destination[i]=sgl1p[i].header;
0151     }
0152   else if (!strcmp(what,"EVNUMBER"))
0153     {
0154       if(length>nGL1Pboards)*nw=nGL1Pboards;
0155       else *nw=length;
0156       for(i=0;i<*nw;i++)
0157     destination[i]=sgl1p[i].ev_number;
0158     }
0159   else if (!strcmp(what,"MODEBIT"))
0160     {
0161       if(length>nGL1Pboards)*nw=nGL1Pboards;
0162       else *nw=length;
0163       for(i=0;i<*nw;i++)
0164     destination[i]=sgl1p[i].modebit;
0165     }
0166   else
0167     {
0168       *nw=0;
0169     }
0170   return 0;
0171 }
0172 
0173 void Packet_gl1p::dump ( OSTREAM &os)
0174 {
0175   int i,j,l,m,dlength;
0176   dlength = getDataLength();
0177 
0178     this->identify(os); 
0179     if(!sgl1p) demangle();
0180     if(!sgl1p){
0181       os<<"Failed to fill sgl1p. Exit"<<std::endl;
0182       return;
0183     }
0184 
0185     for(m=0;m<54;m++) os<<"=";
0186     os << std::endl;
0187     os << "GL1P data packet: ";
0188     os << "Detected " << std::dec << nGL1Pboards << " GL1-1P boards in the data packet." <<std::endl;
0189     for(i=0;i<nGL1Pboards;i++){
0190       for(m=0;m<54;m++) os<<"-";
0191       COUT<<std::endl;
0192       os <<std::dec<<"GL1P["<<i<<"] header word    = 0x" << std::hex << SETW(4) << (unsigned int)sgl1p[i].header << std::endl;
0193       os <<std::dec<<"Event = " <<SETW(4)<<(unsigned int)sgl1p[i].ev_number<<std::endl;
0194       os <<"Modebit    = 0x" << std::hex << SETW(3) << sgl1p[i].modebit << std::endl;
0195       os << "Beam Cross. Counters: ";
0196       for(j=0;j<4;j++)
0197         os <<std::dec<<SETW(12)<<(unsigned int)sgl1p[i].clock[j];
0198       os<<std::endl;
0199       os << "Scalers             : ";
0200       for(j=0;j<4;j++)
0201         os<<SETW(12)<<sgl1p[i].scaler[j];
0202       os<<std::endl;
0203     }
0204     for(m=0;m<54;m++) os<<"-";
0205     // Raw data 
0206         j = 0;
0207     unsigned int* k=(unsigned int *) findPacketDataStart(packet);
0208     if (k == 0) 
0209       {
0210         os << std::endl;
0211         return;
0212       }
0213 
0214         while (1)
0215     {
0216       os << std::endl << std::dec << SETW(5) << j << " |  ";
0217       for (l=0;l<6;l++)
0218       {
0219           os << std::hex << SETW(8) << k[j++] << " ";
0220           if (j>=dlength) break;
0221       }
0222       if (j>=dlength) break;
0223     }   
0224         os << std::endl;
0225     for(m=0;m<54;m++) os<<"=";
0226         os << std::dec << std::endl;
0227 
0228 }
0229 
0230 void Packet_gl1p::demangle()
0231 {
0232   int sclk[]={1,0,3,2};
0233   /* 
0234      The job of this routine is to demangle the data into something
0235      that can be more properly addressed - the data is copied
0236      into a GL1 data structure that can be addressed properly.
0237   */
0238   int dlength = getDataLength();
0239   nGL1Pboards=dlength/6;
0240   if(sgl1p) delete sgl1p;
0241   if(nGL1Pboards<1) {
0242     sgl1p=0;
0243     return;
0244   }
0245   sgl1p=new GL1P_DATA[nGL1Pboards];
0246   if(!sgl1p){
0247     COUT<<"can't allocate memory for GL1P_DATA structure "<<std::endl;
0248     return;
0249   }
0250   unsigned int* buf = (unsigned int *) findPacketDataStart(packet);
0251   if (buf == 0) 
0252     {
0253       delete [] sgl1p;
0254       sgl1p = NULL;
0255       return;
0256     }
0257   // unpack the data into a GL1 structure:
0258 
0259 
0260   unsigned int  j, idx, idy;
0261   unsigned char bclock;
0262   int i;
0263 
0264   for(i=0;i<nGL1Pboards;i++){
0265     idx=i*6;
0266     sgl1p[i].header    = (unsigned char)((buf[idx]>>8)&0xFF);
0267     sgl1p[i].ev_number = (unsigned char)(buf[idx]&0xFF);
0268     sgl1p[i].modebit   = (unsigned short)((buf[idx]>>16)&0x1FF);
0269       /* Note the complicated way in which the 20-bit LUT inputs are packed */
0270     for(j=0;j<4;j++){
0271       idy=j*8;
0272       bclock = (unsigned char)((buf[idx+1]>>idy)&0x7F);
0273       sgl1p[i].clock[sclk[j]] =(bclock&0xF) + ((bclock>>4)&0x7)*15;
0274       sgl1p[i].scaler[j] = ((buf[idx+2+j]&0xFFFF)<<16) + ((buf[idx+2+j]>>16)&0xFFFF);
0275     }
0276 
0277   }
0278 
0279 }