StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ftp_reader.cxx
1 #include <string.h>
2 #include <arpa/inet.h>
3 #include <stdio.h>
4 
5 #include <daqFormats.h>
6 #include <rtsSystems.h>
7 #include <rtsLog.h>
8 #include <rts.h>
9 
10 #include "daq_ftp.h"
11 
12 static int unpackRaw(int sec, int what, struct TPCPADK *padk, struct TPCCPPR_l *cppr, char *mem, ftp_t *ftp) ;
13 
14 int ftp_reader(char *m, struct ftp_t *ftp, u_int driver)
15 {
16  int sec ;
17  int rb, mz ;
18  int len ;
19  u_int off ;
20 
21  struct TPCP *ftpp ;
22  struct TPCSECP *secp ;
23  struct TPCRBP *rbp ;
24  struct TPCMZP *mzp ;
25  struct TPCSEQD *seqd ;
26  struct TPCADCD *adcd ;
27  struct TPCPADK *padk ;
28  struct TPCADCR_l *adcr ;
29  struct TPCCPPR_l *cppr ;
30  struct TPCPEDR *pedr ;
31  struct TPCRMSR *rmsr ;
32  struct DATAP *datap ;
33 
34 
35  // clear total channels
36  ftp->channels = 0 ;
37 
38  // clear mode before
39  ftp->mode = 0 ;
40 
41  // set the max channel num - constant
42  ftp->max_channels = 2*10*960*256 ;
43 
44  if(m == NULL) return 0 ;
45  datap = (struct DATAP *)m ;
46 
47  len = ntohl(datap->det[FTP_ID].len) ;
48  if(len == 0) return 0 ;
49  len *= 4 ; // make it bytes...
50 
51  off = ntohl(datap->det[FTP_ID].off) ;
52  if(off == 0) return 0 ;
53 
54  ftpp = (struct TPCP *)((u_int *)m + off) ;
55  if(checkBank((char *)ftpp,"FTPP") < 0) return -1 ; // wrong bank!
56 
57  // clear the data part
58  memset((char *)ftp->counts,0,sizeof(ftp->counts)) ;
59 
60 
61  for(sec=0;sec<2;sec++) {
62  int last ;
63 
64 
65  if(ftpp->sb[sec].len == 0) continue ;
66 
67  secp = (struct TPCSECP *) ((char *)ftpp + b2h32(ftpp->sb[sec].off)*4) ;
68  if(checkBank((char *)secp,"FTPSECP") < 0) return -1 ;
69 
70  last = 10 ;
71 
72  for(rb=0;rb<last;rb++) {
73 
74 
75  if(secp->rb[rb].len == 0) continue ;
76 
77  rbp = (struct TPCRBP *) ((char *)secp + b2h32(secp->rb[rb].off)*4) ;
78  if(checkBank((char *)rbp,"FTPRBP") < 0) {
79  break ;
80  return -1 ;
81  }
82 
83  // at this point (RBP) data is different endianess...
84 
85  for(mz=0;mz<3;mz++) {
86  if(rbp->mz[mz].len == 0) continue ;
87 
88  mzp = (struct TPCMZP *)((char *)rbp + l2h32(rbp->mz[mz].off)*4) ;
89  if(checkBank((char *)mzp,"FTPMZP") < 0) {
90  break ;
91  return -1 ;
92  }
93 
94  u_int mz_pix = 0 ;
95 
96  // what do we have here...
97  if((mzp->banks[TPC_ADCD].len != 0) && (ftp->mode==0)) { // zero-suppressed
98 
99 
100  seqd = (struct TPCSEQD *)((char *)mzp + l2h32(mzp->banks[TPC_SEQD].off)*4) ;
101  adcd = (struct TPCADCD *)((char *)mzp + l2h32(mzp->banks[TPC_ADCD].off)*4) ;
102 
103  if(checkBank((char *)seqd,"FTPSEQD") < 0) return -1 ;
104  if(checkBank((char *)adcd,"FTPADCD") < 0) return -1 ;
105 
106  int len = l2h32(seqd->bh.length) - 10 ;
107  len *= 2 ;
108 
109  int adccou = 0 ;
110  int jj ;
111 
112 
113 
114  for(jj=0;jj<len;jj++) {
115  int start, last, length, stop ;
116  u_short ss, f8 ;
117  static int rr, pp ;
118  int tbin ;
119  int frow, fpad ; // FTPC row, pad
120 
121  ss = l2h16(seqd->seq[jj]) ;
122  f8 = (ss & 0x8000) ? 1 : 0 ;
123 
124 
125  frow = (rr-1)/6 ;
126  fpad = ((rr-1)%6)*160 + (pp-1) ;
127 
128 
129  if(f8) { // new pad flags
130 
131  pp = (ss & 0x7FFF) % 256 ; // daqpad
132  rr = (ss & 0x7FFF) / 256 ; // daqrow
133 
134 
135 
136  last = 0 ;
137 
138  if(pp==0xFF) { // dummy
139  LOG(DBG,"Dummy row %d...",jj,0,0,0,0) ;
140  continue ;
141  }
142 
143 
144  }
145  else {
146  int counter ;
147  last = (ss & 0x0020) ? 1 : 0 ;
148  start = (ss & 0x7FC0) >> 6 ;
149 
150  length = ss & 0x1F ;
151 
152  stop = start + length ;
153 
154 
155 
156  for(tbin=start;tbin<stop;tbin++) {
157  u_char val ;
158 
159  val = adcd->adc[adccou++] ;
160  mz_pix++ ;
161  if(tbin > 255) continue ;
162 
163  //LOG(DBG,"row %d, pad %d, tbin %d, val %d",frow,fpad,tbin,val,0) ;
164  counter = ftp->counts[sec][frow][fpad] ;
165 
166  ftp->adc[sec][frow][fpad][counter] = val ;
167  ftp->timebin[sec][frow][fpad][counter] = tbin ;
168  if(ftp->counts[sec][frow][fpad] != 255) {
169  (ftp->counts[sec][frow][fpad])++ ;
170  }
171  ftp->channels++ ;
172  }
173  }
174 
175  if(last) {
176  pp++ ;
177  }
178  }
179 
180  LOG(DBG,"FTP: Setor %d, RB %d, MZ %d: ticks %u, pix %u",sec+1,rb+1,mz+1,l2h32(mzp->bh.w9),mz_pix) ;
181 
182  //printf("Found %d channels\n",ftp->channels-chSave) ;
183 
184  continue ; // don;t look at more banks!
185  }
186 
187  padk = NULL ;
188  if(mzp->banks[TPC_PADK].len != 0) {
189  padk = (struct TPCPADK *)((char *)mzp + l2h32(mzp->banks[TPC_PADK].off)*4) ;
190 
191  if(checkBank((char *)padk,"FTPPADK") < 0) return -1 ;
192 
193  }
194 
195  cppr = NULL ;
196  if(mzp->banks[TPC_CPPR].len != 0) {
197 
198  cppr = (struct TPCCPPR_l *)((char *)mzp + l2h32(mzp->banks[TPC_CPPR].off)*4) ;
199  if(checkBank((char *)cppr,"FTPCPPR") < 0) return -1 ;
200  }
201 
202  if((mzp->banks[TPC_ADCR].len != 0) && (ftp->mode==0)) { // raw ...
203  adcr = (struct TPCADCR_l *)((char *)mzp + l2h32(mzp->banks[TPC_ADCR].off)*4) ;
204 
205  if(checkBank((char *)adcr,"FTPADCR") < 0) return -1 ;
206 
207  if(unpackRaw(sec, 0, padk, cppr, (char *)adcr, ftp) < 0) {
208  LOG(ERR,"Problems in RAW data in sector %d, RB %d, MZ %d - skipping...",
209  sec+1,rb+1,mz+1,0,0) ;
210  }
211 
212  LOG(DBG,"FTP Raw data bank in sector %d, RB %d, MZ %d!",
213  sec+1,rb+1,mz+1,0,0) ;
214  continue ;
215  }
216 
217  if(mzp->banks[TPC_PEDR].len != 0) { // pedestal data!
218  pedr = (struct TPCPEDR *)((char *)mzp + l2h32(mzp->banks[TPC_PEDR].off)*4) ;
219 
220  if(checkBank((char *)pedr,"FTPPEDR") < 0) return -1 ;
221 
222  unpackRaw(sec, 1, padk, cppr, (char *)pedr, ftp) ;
223  ftp->mode = 1 ; // pedestal data!
224 
225  }
226 
227  if(mzp->banks[TPC_RMSR].len != 0) { // RMS too
228  rmsr = (struct TPCRMSR *)((char *)mzp + l2h32(mzp->banks[TPC_RMSR].off)*4) ;
229 
230  if(checkBank((char *)rmsr,"FTPRMSR") < 0) return -1 ;
231 
232  unpackRaw(sec, 2, padk, cppr, (char *)rmsr, ftp) ;
233  ftp->mode = 1 ; // pedestal data!
234 
235  }
236 
237 
238  }
239 
240  }
241 
242  }
243 
244 
245 
246  return len ;
247 }
248 
249 
250 /*
251  what == 0 ADCR
252  what == 1 PEDR
253  what == 2 RMSR
254 */
255 static int unpackRaw(int sec, int what, struct TPCPADK *padk, struct TPCCPPR_l *cppr, char *mem, ftp_t *ftp)
256 {
257  int i, j, t ;
258  u_char row, pad ;
259  u_short *cppseq ;
260  u_char *adcseq ;
261  u_char *adcdata ;
262  u_short *cppdata ;
263  int frow, fpad ;
264  int timebins, cpps ;
265 
266  timebins = 512 ;
267  cpps = 31 ;
268 
269 
270 
271  if(padk == NULL) {
272  LOG(WARN,"No PADK? - skipping...",0,0,0,0,0) ;
273  return -1 ;
274  }
275  if(mem == NULL) {
276  LOG(WARN,"No DATA? - skipping...",0,0,0,0,0) ;
277  return -1 ;
278  }
279 
280  adcdata = NULL ;
281  cppdata = NULL ;
282 
283  switch(what) {
284  case 0 : // ADCR
285  adcdata = (u_char *) mem + sizeof(struct TPCADCR_l);
286 
287  if(cppr == NULL) {
288  LOG(WARN,"No CPPR? - skipping...",0,0,0,0,0) ;
289  return -1 ;
290  }
291 
292  cppdata = (u_short *)((char *)cppr + sizeof(struct TPCCPPR_l)) ;
293  break ;
294  case 1 : // PEDR
295  adcdata = ((struct TPCPEDR *)mem)->ped ;
296  break ;
297  case 2 : // RMSR
298  adcdata = ((struct TPCRMSR *)mem)->rms ;
299  break ;
300  }
301 
302 
303  for(i=0;i<320;i++) {
304  row = padk->rp[i].row ;
305  pad = padk->rp[i].pad ;
306 
307  if((row==0xFF) || (pad==0xFF)) continue ;
308 
309 
310  // we'll count from 0
311  row-- ;
312  pad-- ;
313 
314  // get to the real FTPC row:pad
315  frow = row/6 ;
316  fpad = (row%6)*160 + pad ;
317 
318  adcseq = (u_char *) adcdata + timebins*i ;
319 
320  switch(what) {
321  case 0 :
322  cppseq = (u_short *)((char *) cppdata + 2*2*32*i) ;
323  for(j=0;j<cpps;j++) {
324  u_short start, stop ;
325  u_char val ;
326  int counter ;
327 
328 
329 
330  start = l2h16(*cppseq++) ;
331  stop = l2h16(*cppseq++) ;
332 
333 
334  if(start & 0xFE00) break ;
335  if(start >= 256) break ; // no point...
336 
337  // redundant for the FTP but anyway...
338  if(start == 511) stop = 511 ;
339 
340  if((stop < start) || (stop >= timebins)) {
341  LOG(WARN,"Bad data stop<start %d<%d - skipping",stop,start,0,0,0) ;
342  return -1 ;
343  }
344 
345  // stop here for SVT
346  if(stop >= 256) stop = 255 ;
347 
348  for(t=start;t<=stop;t++) {
349 
350  val = *(adcseq + t) ;
351 
352  if(val == 0) { // skip...
353  return -1 ; // hardware error in the MZ!
354  }
355 
356  counter = ftp->counts[sec][frow][fpad] ;
357 
358  //printf("%d %d\n",t,val) ;
359  ftp->adc[sec][frow][fpad][counter] = val ;
360  ftp->timebin[sec][frow][fpad][counter] = t ;
361  if(ftp->counts[sec][frow][fpad] != 255) {
362  ftp->counts[sec][frow][fpad]++ ;
363  }
364  ftp->channels++ ;
365  }
366 
367  }
368  break ;
369  case 1 : // PEDR is in ftp->adc!
370  for(j=0;j<256;j++) {
371  u_char val ;
372  val = *adcseq++ ;
373 
374  ftp->adc[sec][frow][fpad][j] = val ;
375  ftp->channels++ ; // count only once!
376 
377  }
378  break ;
379  case 2 : // RMSR is in ftp->timebin!
380  for(j=0;j<256;j++) {
381  u_char val ;
382  val = *adcseq++ ;
383 
384  ftp->timebin[sec][frow][fpad][j] = val ;
385  }
386  break ;
387  }
388 
389 
390  }
391 
392 
393  return 0 ;
394 
395 }
Definition: rb.hh:21
Definition: daq_ftp.h:4