Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
packet_gl1p.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file packet_gl1p.cc
1 #include <packet_gl1p.h>
2 #include <string.h>
3 
5  : Packet_w4(data)
6 {
7  sgl1p=0;
8 }
9 
11 {
12  if(sgl1p)
13  {
14  delete [] sgl1p;
15  }
16 }
17 
18 int *Packet_gl1p::decode ( int *nwout)
19 {
20  int *p,*k;
21  int i, olength;
22  int temp[MAX_OUTLENGTH];
23 
24  int dlength = getDataLength();
25 
26  int status = decode_gl1p( temp
27  ,(int *) findPacketDataStart(packet)
28  ,dlength
29  ,MAX_OUTLENGTH, &olength);
30 
31  if (status || olength<=0 ) return NULL;
32 
33  p = new int[olength];
34  k = p;
35  for (i =0; i<olength; i++) *k++ = temp[i];
36  *nwout = olength;
37  return p;
38 }
39 int Packet_gl1p::iValue(const int ich, const int what)
40 {// int what is used hear instead of char* what to reduce data taking time.
41  if(!sgl1p) demangle();
42  if(!sgl1p){
43  COUT<<"Failed to fill data structure"<<std::endl;
44  return 0;
45  }
46  switch (what)
47  {
48  case GL1P_HEADER:
49  {
50  if(ich<0 || ich>= nGL1Pboards) return 0;
51  else return sgl1p[ich].header;
52  }
53  case GL1P_EVNUMBER:
54  {
55  if(ich<0 || ich>= nGL1Pboards) return 0;
56  else return sgl1p[ich].ev_number;
57  }
58  case GL1P_MODEBIT:
59  {
60  if(ich<0 || ich>= nGL1Pboards) return 0;
61  else return sgl1p[ich].modebit;
62  }
63  case GL1P_CLOCK:
64  {// it is supposed that the number of "clock counters" is 4*nGL1Pboards.
65  //For example, to get Clock Counter for Scaler A of GL1P board 2 call iValue(5,GL1P_CLOCK)
66  if(ich<0 || ich>= 4*nGL1Pboards) return -1;
67  else return sgl1p[ich/4].clock[ich%4];
68  }
69  case GL1P_SCALER:
70  {//Scaler value. The same rule about ich as above.
71  if(ich<0 || ich>= 4*nGL1Pboards) return 0;
72  else return sgl1p[ich/4].scaler[ich%4];
73  }
74  default:
75  return 0;
76  }
77 }
78 int Packet_gl1p::iValue(const int ich, const char *what)
79 {
80 
81 
82  if(!sgl1p) demangle();
83  if(!sgl1p){
84  COUT<<"Failed to fill data structure"<<std::endl;
85  return 0;
86  }
87 
88  if (!strcmp(what,"HEADER"))
89  {
90  if(ich<0 || ich>= nGL1Pboards) return 0;
91  else return sgl1p[ich].header;
92  }
93 
94  else if (!strcmp(what,"EVNUMBER"))
95  {
96  if(ich<0 || ich>= nGL1Pboards) return 0;
97  else return sgl1p[ich].ev_number;
98  }
99  else if (!strcmp(what,"MODEBIT"))
100  {
101  if(ich<0 || ich>= nGL1Pboards) return 0;
102  else return sgl1p[ich].modebit;
103  }
104  else if (!strcmp(what,"CLOCK"))
105  {
106  if(ich<0 || ich>= 4*nGL1Pboards) return -1;
107  else return sgl1p[ich/4].clock[ich%4];
108  }
109  else if (!strcmp(what,"SCALER"))
110  {
111  if(ich<0 || ich>= 4*nGL1Pboards) return 0;
112  else return sgl1p[ich/4].scaler[ich%4];
113  }
114  else return 0;
115 
116 }
120 int Packet_gl1p::fillIntArray (int destination[], // the data go here
121  const int length, // space we have in destination
122  int * nw, // words actually used
123  const char * what) // type of data (see above)
124 {
125  int i;
126  if(!sgl1p) demangle();
127  if(!sgl1p){
128  COUT<<"Failed to fill data structure"<<std::endl;
129  return -1;
130  }
131  if (!strcmp(what,"CLOCK"))
132  {
133  if(length>4*nGL1Pboards)*nw=4*nGL1Pboards;
134  else *nw=length;
135  for(i=0;i<*nw;i++)
136  destination[i]=sgl1p[i/4].clock[i%4];
137  }
138  else if (!strcmp(what,"SCALER"))
139  {
140  if(length>4*nGL1Pboards)*nw=4*nGL1Pboards;
141  else *nw=length;
142  for(i=0;i<*nw;i++)
143  destination[i]=sgl1p[i/4].scaler[i%4];
144  }
145  else if (!strcmp(what,"HEADER"))
146  {
147  if(length>nGL1Pboards)*nw=nGL1Pboards;
148  else *nw=length;
149  for(i=0;i<*nw;i++)
150  destination[i]=sgl1p[i].header;
151  }
152  else if (!strcmp(what,"EVNUMBER"))
153  {
154  if(length>nGL1Pboards)*nw=nGL1Pboards;
155  else *nw=length;
156  for(i=0;i<*nw;i++)
157  destination[i]=sgl1p[i].ev_number;
158  }
159  else if (!strcmp(what,"MODEBIT"))
160  {
161  if(length>nGL1Pboards)*nw=nGL1Pboards;
162  else *nw=length;
163  for(i=0;i<*nw;i++)
164  destination[i]=sgl1p[i].modebit;
165  }
166  else
167  {
168  *nw=0;
169  }
170  return 0;
171 }
172 
174 {
175  int i,j,l,m,dlength;
176  dlength = getDataLength();
177 
178  this->identify(os);
179  if(!sgl1p) demangle();
180  if(!sgl1p){
181  os<<"Failed to fill sgl1p. Exit"<<std::endl;
182  return;
183  }
184 
185  for(m=0;m<54;m++) os<<"=";
186  os << std::endl;
187  os << "GL1P data packet: ";
188  os << "Detected " << std::dec << nGL1Pboards << " GL1-1P boards in the data packet." <<std::endl;
189  for(i=0;i<nGL1Pboards;i++){
190  for(m=0;m<54;m++) os<<"-";
191  COUT<<std::endl;
192  os <<std::dec<<"GL1P["<<i<<"] header word = 0x" << std::hex << SETW(4) << (unsigned int)sgl1p[i].header << std::endl;
193  os <<std::dec<<"Event = " <<SETW(4)<<(unsigned int)sgl1p[i].ev_number<<std::endl;
194  os <<"Modebit = 0x" << std::hex << SETW(3) << sgl1p[i].modebit << std::endl;
195  os << "Beam Cross. Counters: ";
196  for(j=0;j<4;j++)
197  os <<std::dec<<SETW(12)<<(unsigned int)sgl1p[i].clock[j];
198  os<<std::endl;
199  os << "Scalers : ";
200  for(j=0;j<4;j++)
201  os<<SETW(12)<<sgl1p[i].scaler[j];
202  os<<std::endl;
203  }
204  for(m=0;m<54;m++) os<<"-";
205  // Raw data
206  j = 0;
207  unsigned int* k=(unsigned int *) findPacketDataStart(packet);
208  if (k == 0)
209  {
210  os << std::endl;
211  return;
212  }
213 
214  while (1)
215  {
216  os << std::endl << std::dec << SETW(5) << j << " | ";
217  for (l=0;l<6;l++)
218  {
219  os << std::hex << SETW(8) << k[j++] << " ";
220  if (j>=dlength) break;
221  }
222  if (j>=dlength) break;
223  }
224  os << std::endl;
225  for(m=0;m<54;m++) os<<"=";
226  os << std::dec << std::endl;
227 
228 }
229 
231 {
232  int sclk[]={1,0,3,2};
233  /*
234  The job of this routine is to demangle the data into something
235  that can be more properly addressed - the data is copied
236  into a GL1 data structure that can be addressed properly.
237  */
238  int dlength = getDataLength();
239  nGL1Pboards=dlength/6;
240  if(sgl1p) delete sgl1p;
241  if(nGL1Pboards<1) {
242  sgl1p=0;
243  return;
244  }
246  if(!sgl1p){
247  COUT<<"can't allocate memory for GL1P_DATA structure "<<std::endl;
248  return;
249  }
250  unsigned int* buf = (unsigned int *) findPacketDataStart(packet);
251  if (buf == 0)
252  {
253  delete [] sgl1p;
254  sgl1p = NULL;
255  return;
256  }
257  // unpack the data into a GL1 structure:
258 
259 
260  unsigned int j, idx, idy;
261  unsigned char bclock;
262  int i;
263 
264  for(i=0;i<nGL1Pboards;i++){
265  idx=i*6;
266  sgl1p[i].header = (unsigned char)((buf[idx]>>8)&0xFF);
267  sgl1p[i].ev_number = (unsigned char)(buf[idx]&0xFF);
268  sgl1p[i].modebit = (unsigned short)((buf[idx]>>16)&0x1FF);
269  /* Note the complicated way in which the 20-bit LUT inputs are packed */
270  for(j=0;j<4;j++){
271  idy=j*8;
272  bclock = (unsigned char)((buf[idx+1]>>idy)&0x7F);
273  sgl1p[i].clock[sclk[j]] =(bclock&0xF) + ((bclock>>4)&0x7)*15;
274  sgl1p[i].scaler[j] = ((buf[idx+2+j]&0xFFFF)<<16) + ((buf[idx+2+j]>>16)&0xFFFF);
275  }
276 
277  }
278 
279 }