StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
svt_reader.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <arpa/inet.h>
4 
5 #include <daqFormats.h>
6 #include <rtsSystems.h>
7 #include <rtsLog.h>
8 #include <rts.h>
9 
10 #include <DAQ_READER/daq_det.h>
11 
12 
13 #include <SVT/key_map.h>
14 
15 
16 
17 //#include <evpReader.hh>
18 //#include <evpSupport.h>
19 //#include <svtReader.h>
20 
21 #include "daq_svt.h"
22 
23 
24 //int DAQsvtReader(char *m);
25 static int unpackRaw(int sec, int what, struct SVTANODK *padk, struct TPCCPPR_l *cppr, char *mem, svt_t *svt) ;
26 
27 
28 
29 
30 
31 // m is DATAP
32 int svt_reader(char *m, struct svt_t *svt, u_int driver)
33 {
34  int sec, i ;
35  int rb, mz ;
36  int len ;
37  u_int off ;
38  static int first = 1 ;
39 
40  struct DATAP *datap ;
41  struct TPCP *svtp ;
42  struct TPCSECP *secp ;
43  struct TPCRBP *rbp ;
44  struct TPCMZP *mzp ;
45  struct TPCSEQD *seqd ;
46  struct TPCADCD *adcd ;
47  struct SVTANODK *padk ;
48  struct TPCADCR_l *adcr ;
49  struct TPCCPPR_l *cppr ;
50  struct TPCPEDR *pedr ;
51  struct TPCRMSR *rmsr ;
52 
53  // clear total channels
54  svt->channels = 0 ;
55 
56  // clear mode before
57  svt->mode = 0 ;
58 
59 
60  if(m==NULL) return 0 ;
61 
62  datap = (struct DATAP *) m ;
63 
64  len = ntohl(datap->det[SVT_ID].len) ;
65  if(len == 0) return 0 ;
66  len *= 4 ;
67 
68  off = ntohl(datap->det[SVT_ID].off) ;
69  if(off == 0) return 0 ;
70 
71  svtp = (struct TPCP *)((u_int *)m+off) ;
72  if(checkBank((char *)svtp,"SVTP") < 0) return -1 ; // wrong bank!
73 
74 
75 // if(first) { // this can't work anymore since the svt is allocated and not static!
76  {
77  int j, k ;
78 
79  svt->max_channels = 24*3*6*240*128 ;
80  // create the remap file which maps from [rb(1..24)][mz(1..3)][hy(1..6)] to
81  // barrel, ladder, wafer, hybrid
82  for(i=0;i<24;i++) {
83  for(j=0;j<3;j++) {
84  for(k=0;k<6;k++) {
85  svt->B[i][j][k] = (svt_pad_key[i][j][k] & 0xFF000000) >> 24 ;
86  svt->L[i][j][k] = (svt_pad_key[i][j][k] & 0x00FF0000) >> 16 ;
87  svt->W[i][j][k] = (svt_pad_key[i][j][k] & 0x00000F00) >> 8 ;
88  svt->H[i][j][k] = (svt_pad_key[i][j][k] & 0x0000F000) >> 12 ;
89  }
90  }
91  }
92 
93  first = 0 ;
94  }
95 
96 
97  // clear the data part
98  memset((char *)svt->counts,0,sizeof(svt->counts)) ;
99 
100  for(sec=0;sec<4;sec++) {
101  int last ;
102 
103  if((b2h32(svtp->bh.format_number)==1) && (sec%2)) continue ; // 2 sectors packed per offset - no even sectors
104 
105  if(svtp->sb[sec].len == 0) continue ;
106 
107  LOG(DBG,"SVT sector %d: len %d, off %d",sec+1,svtp->sb[sec].len,svtp->sb[sec].off) ;
108 
109  secp = (struct TPCSECP *) ((char *)svtp + b2h32(svtp->sb[sec].off)*4) ;
110  if(checkBank((char *)secp,"SVTSECP") < 0) return -1 ;
111 
112  // if "year 1" format we have 12 RB packed in one DAQ "sector"
113  if(b2h32(svtp->bh.format_number)==1) {
114  last = 12 ;
115  }
116  else {
117  last = 6 ;
118  }
119 
120  for(rb=0;rb<last;rb++) {
121  int rsec ; // the real, SVT sector...
122  int rrb ; // the real, SVT RB [0..23]
123 
124  if(rb >= 6) {
125  rsec = sec+1 ;
126  rrb = rsec*6 + (rb - 6) ;
127  }
128  else {
129  rsec = sec ;
130  rrb = rsec*6 + rb ;
131  }
132 
133  if(secp->rb[rb].len == 0) continue ;
134 
135  rbp = (struct TPCRBP *) ((char *)secp + b2h32(secp->rb[rb].off)*4) ;
136  if(checkBank((char *)rbp,"SVTRBP") < 0) return -1 ;
137 
138  // at this point (RBP) data is different endianess...
139 
140  for(mz=0;mz<3;mz++) {
141  if(rbp->mz[mz].len == 0) continue ;
142 
143  mzp = (struct TPCMZP *)((char *)rbp + l2h32(rbp->mz[mz].off)*4) ;
144  if(checkBank((char *)mzp,"SVTMZP") < 0) return -1 ;
145 
146 
147  // what do we have here...
148  if((mzp->banks[TPC_ADCD].len != 0) && (svt->mode==0)) { // zero-suppressed
149 
150 
151 
152 
153  seqd = (struct TPCSEQD *)((char *)mzp + l2h32(mzp->banks[TPC_SEQD].off)*4) ;
154  adcd = (struct TPCADCD *)((char *)mzp + l2h32(mzp->banks[TPC_ADCD].off)*4) ;
155 
156  //printf("(%d %d) -- adcd 0x%x, seqd 0x%x\n",rb,mz,adcd,seqd);
157 
158 
159  //printf("adcx offset = 0x%x, len=%d\n",
160  // l2h32(mzp->banks[TPC_ADCX].off)*4,
161  // l2h32(mzp->banks[TPC_ADCX].len)*4);
162 
163 
164 // TPCADCD *fb = adcd;
165  // char buff[10];
166 // memset(buff, 0, sizeof(buff));
167 // memcpy(buff,fb->bh.bank_type,8);
168 // printf("buff = %s\n",buff);
169 // printf("len=%d bank_id=%d format=%d 0x%x token=%d w9=%d\n",
170 // fb->bh.length,
171 // fb->bh.bank_id,
172 // fb->bh.format_ver,
173 // fb->bh.byte_order,
174 // fb->bh.token,
175 // fb->bh.w9);
176 
177  if(checkBank((char *)seqd,"SVTSEQD") < 0) return -1 ;
178  if(checkBank((char *)adcd,"SVTADCD") < 0) return -1 ;
179 
180  int len = l2h32(seqd->bh.length) - 10 ;
181  len *= 2 ;
182 
183  int adccou = 0 ;
184  int jj ;
185 
186 
187 
188  for(jj=0;jj<len;jj++) {
189  int start, last, length, stop ;
190  u_short ss, f8 ;
191  u_int rr, pp, hy ;
192  int tbin ;
193  int k ;
194 
195  ss = l2h16(seqd->seq[jj]) ;
196  f8 = (ss & 0x8000) ? 1 : 0 ;
197 
198 
199  if(f8) { // new pad flags
200  pp = (ss & 0x7FFF) % 256 ; // anode
201  hy = (ss & 0x7FFF) / 256 ; // hybrid
202 
203  last = 0 ;
204 
205  rr = 0 ;
206  // get the ASIC from the hybrid
207  for(k=0;k<6;k++) {
208  if((svt_pad_key[rrb][mz][k] & 0x7F) == hy) {
209  rr = k + 1 ; // make it from 1
210  break ;
211  }
212  }
213 
214 
215 
216  if(rr == 0) {
217  if(hy == 0) { // hm, seem like a normal end...
218  LOG(DBG,"Hybrid 0: 0x%04X, seq %d of %d",
219  ss,jj,len,0,0) ;
220  break ;
221  }
222  LOG(ERR,"Can't find hybrid 0x%02X in the svt_key for RB %d, MZ %d (seq %d of %d)!",
223  hy,rrb+1,mz+1,jj,len) ;
224  break ;
225  }
226 
227  LOG(DBG,"Hy %d %d %d 0x%02X %d",rrb+1,mz+1,rr,hy,pp) ;
228 
229  if(pp == 0xFF) {
230  LOG(WARN,"Anode is 0xFF!",0,0,0,0,0) ;
231  continue ;
232  }
233 
234  }
235  else {
236  last = (ss & 0x0020) ? 1 : 0 ;
237  length = ss & 0x1F ;
238  start = (ss & 0x7FC0) >> 6 ;
239  stop = start + length ;
240 
241  if(pp == 1) {
242  //LOG(DBG,"Anode 1: RB %d, MZ %d, HY %d: start %d, stop %d",
243  // rrb+1,mz+1,rr,start,stop-1) ;
244  }
245 
246  for(tbin=start;tbin<stop;tbin++) {
247  u_char val ;
248  val = adcd->adc[adccou++] ;
249 
250  int counter = svt->counts[rrb][mz][rr-1][pp-1] ;
251  svt->adc[rrb][mz][rr-1][pp-1][counter] = val ;
252  svt->timebin[rrb][mz][rr-1][pp-1][counter] = tbin ;
253  svt->counts[rrb][mz][rr-1][pp-1] += 1 ;
254  svt->channels++ ;
255  }
256  }
257 
258  if(last) {
259  pp++ ;
260  }
261  }
262 
263 
264  continue ; // don;t look at more banks!
265  }
266 
267  padk = NULL ;
268  if(mzp->banks[SVT_ANODK].len != 0) { // raw ...
269  padk = (struct SVTANODK *)((char *)mzp + l2h32(mzp->banks[SVT_ANODK].off)*4) ;
270 
271  if(checkBank((char *)padk,"SVTANODK") < 0) return -1 ;
272  }
273 
274 
275  cppr = NULL ;
276  if(mzp->banks[TPC_CPPR].len != 0) {
277 
278  cppr = (struct TPCCPPR_l *)((char *)mzp + l2h32(mzp->banks[TPC_CPPR].off)*4) ;
279  if(checkBank((char *)cppr,"SVTCPPR") < 0) return -1 ;
280  }
281 
282 
283  if((mzp->banks[TPC_ADCR].len != 0) && (svt->mode==0)) { // raw ...
284  adcr = (struct TPCADCR_l *)((char *)mzp + l2h32(mzp->banks[TPC_ADCR].off)*4) ;
285 
286  if(checkBank((char *)adcr,"SVTADCR") < 0) return -1 ;
287 
288 
289 
290  if(unpackRaw(rrb*3+mz, 0, padk, cppr, (char *)adcr, svt) < 0) {
291  LOG(ERR,"Problems in RAW data in sector %d, RB %d, MZ %d - skipping...",
292  rsec+1,rrb+1,mz+1,0,0) ;
293  }
294 
295  LOG(DBG,"SVT Raw data bank in sector %d, RB %d, MZ %d [sec %d, rb %d]!",
296  rsec+1,rrb+1,mz+1,sec,rb) ;
297  continue ;
298  }
299 
300  if(mzp->banks[TPC_PEDR].len != 0) { // pedestal data!
301  pedr = (struct TPCPEDR *)((char *)mzp + l2h32(mzp->banks[TPC_PEDR].off)*4) ;
302 
303  if(checkBank((char *)pedr,"SVTPEDR") < 0) return -1 ;
304 
305  unpackRaw(rrb*3+mz, 1, padk, cppr, (char *)pedr, svt) ;
306  svt->mode = 1 ; // pedestal data!
307 
308  }
309 
310  if(mzp->banks[TPC_RMSR].len != 0) { // RMS too
311  rmsr = (struct TPCRMSR *)((char *)mzp + l2h32(mzp->banks[TPC_RMSR].off)*4) ;
312 
313  if(checkBank((char *)rmsr,"SVTRMSR") < 0) return -1 ;
314 
315  unpackRaw(rrb*3+mz, 2, padk, cppr, (char *)rmsr, svt) ;
316  svt->mode = 1 ; // pedestal data!
317 
318  }
319 
320 
321  }
322 
323  }
324 
325  }
326 
327 
328 
329  return len ;
330 }
331 
332 
333 /*
334  what == 0 ADCR
335  what == 1 PEDR
336  what == 2 RMSR
337 */
338 static int unpackRaw(int sec, int what, struct SVTANODK *padk, struct TPCCPPR_l *cppr, char *mem, svt_t *svt)
339 {
340  int i, j, t ;
341  u_char pad ;
342  u_short *cppseq ;
343  u_char *adcseq ;
344  u_char *adcdata ;
345  u_short *cppdata ;
346  int rb, mz ;
347  static u_char svtindex[128] ;
348  int timebins, cpps ;
349 
350  timebins = 128 ;
351  cpps = 8 ;
352 
353  if(padk == NULL) {
354  LOG(WARN,"No ANODK? - skipping...",0,0,0,0,0) ;
355  return -1 ;
356  }
357  if(mem == NULL) {
358  LOG(WARN,"No DATA? - skipping...",0,0,0,0,0) ;
359  return -1 ;
360  }
361 
362  adcdata = NULL ;
363  cppdata = NULL ;
364 
365  // check SVT version - pre 2001 is excluded!
366  int ver = l2h32((((struct TPCRMSR *)mem)->bh.format_number)) ;
367 
368  if(ver != 2) {
369  LOG(WARN,"Can't work with pre-2001 data - sorry (ver %d)",ver,0,0,0,0) ;
370  return -1 ;
371  }
372 
373 
374  svt->pre = l2h32(padk->pre) ;
375  svt->post = l2h32(padk->post) ;
376  svt->pedoffset = l2h32(padk->pedOff) ;
377 
378  LOG(DBG,"SVT: pre %d, post %d, pedOff %d",svt->pre,svt->post,svt->pedoffset) ;
379 
380 // for(i=0;i<6;i++) {
381 // fprintf(stderr,"sector %d, hybrid %d : 0x%04X\n",sec,i,padk->hybrids[i]) ;
382 // }
383 
384 
385  switch(what) {
386  case 0 : // ADCR
387  adcdata = (u_char *) mem + sizeof(struct TPCADCR_l);
388 
389  if(cppr == NULL) {
390  LOG(WARN,"No CPPR? - skipping...",0,0,0,0,0) ;
391  return -1 ;
392  }
393 
394  cppdata = (u_short *)((char *)cppr + sizeof(struct TPCCPPR_l)) ;
395  break ;
396  case 1 : // PEDR
397  adcdata = ((struct TPCPEDR *)mem)->ped ;
398  break ;
399  case 2 : // RMSR
400  adcdata = ((struct TPCRMSR *)mem)->rms ;
401  break ;
402  }
403 
404  // unpack "sec" which is svtrb*3+mz
405  mz = sec % 3 ;
406  rb = sec / 3 ;
407 
408 
409  for(i=0;i<6;i++) {
410 
411  for(pad=0;pad<240;pad++) {
412 
413 
414  // arghh... one has to be careful with the SVT due to the new packing
415  u_int anode_offset = (pad % 0x40) * 128 * 4 + (pad/64) ;
416 
417  adcseq = (u_char *) adcdata + i*0x8000 + anode_offset ;
418 
419  switch(what) {
420  case 0 :
421  cppseq = (u_short *)((char *) cppdata + 2*2*8*(i*256+pad)) ;
422 
423  // let's make life easier
424  memset(svtindex,0,sizeof(svtindex)) ;
425 
426  for(j=0;j<cpps;j++) {
427  u_short start, stop ;
428  int pre, post ;
429 
430 
431  start = l2h16(*cppseq++) ;
432  stop = l2h16(*cppseq++) ;
433 
434 
435 
436  if(start & 0xFE00) break ;
437 
438  if(j==7) stop &= 0x7F ; // ASIC bug - SVT only...
439  if(start == 127) stop = 127 ; // ASIC bug
440 
441  if((stop < start) || (stop >= timebins)) {
442  LOG(WARN,"Bad data stop<start %d<%d, cpp %d - skipping",stop,start,j,0,0) ;
443  return -1 ;
444  }
445 
446  pre = (int)start - svt->pre ;
447  if(pre < 0) start = 0 ;
448  else start = pre ;
449 
450  post = stop + svt->post ;
451  if(post > 127) stop = 127 ;
452  else stop = post ;
453 
454  for(t=start;t<=stop;t++) {
455  svtindex[t] = 1 ;
456  }
457  }
458 
459  for(t=0;t<128;t++) {
460  u_char val ;
461  int counter ;
462 
463  if(svtindex[t]) { // use it
464 
465  val = *(adcseq + t*4) ; // weird SVT scheme
466 
467  if(val == 0) { // skip...
468  // don't on the last timebin - ASIC bug
469  //if(t!=127) return -1 ; // hardware error in the MZ!
470  }
471 
472  counter = svt->counts[rb][mz][i][pad] ;
473  svt->adc[rb][mz][i][pad][counter] = val ;
474  svt->timebin[rb][mz][i][pad][counter] = t ;
475  svt->counts[rb][mz][i][pad]++ ;
476 
477  svt->channels++ ;
478  }
479  }
480 
481  break ;
482  case 1 : // PEDR is in svt->adc!
483  for(j=0;j<timebins;j++) {
484  svt->adc[rb][mz][i][pad][j] = *(adcseq + j*4) ;
485  svt->channels++ ; // count only once!
486  }
487  break ;
488  case 2 : // RMSR is in svt->timebin!
489  for(j=0;j<timebins;j++) {
490  svt->timebin[rb][mz][i][pad][j] = *(adcseq + j*4) ;
491  }
492  break ;
493  }
494  }
495 
496  }
497 
498 
499  return 0 ;
500 
501 }
Definition: rb.hh:21
Definition: daq_svt.h:9