StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
tpc_reader.cxx
1 #include <string.h>
2 #include <stdio.h>
3 
4 #include <daqFormats.h>
5 #include <rtsSystems.h>
6 #include <rtsLog.h>
7 #include <rts.h>
8 
9 
10 #define ROWS 45
11 #define PADS_PER_ROW 182
12 #include <TPC/offsets.h>
13 #include <TPC/trans_table.hh>
14 
15 #include "daq_tpc.h"
16 #include <fcfClass.hh>
17 
18 static struct tpc_t *tpc_cached;
19 
20 
21 static int unpackRaw(int sec, int what, struct TPCPADK *padk, struct TPCCPPR_l *cppr, char *mem) ;
22 static int unpackCld(int sec, struct TPCMZCLD_local *mzcld) ;
23 static int dumpGainr(int sec, int rb, struct TPCGAINR *g) ;
24 
25 /*
26 static u_int max_ticks ;
27 static u_int max_clust ;
28 static u_int max_row ;
29 */
30 
31 int tpc_reader(char *m, struct tpc_t *tpc, int sector, int flags)
32 {
33  u_int rb, mz ;
34  u_int len ;
35  u_int off ;
36  u_int t ;
37  u_int first, last ;
38  u_int hsec ;
39  int ret ;
40  u_int tot_bytes ;
41 // static u_int evt ;
42 
43  int retval = 0;
44 
45  int swapdatap=0;
46  int swaptpcp=0;
47  // clear tot_bytes
48  tot_bytes = 0 ;
49 
50  struct DATAP *datap ;
51  struct TPCP *tpcp ;
52  struct TPCSECP *secp ;
53  struct TPCRBP *rbp ;
54  struct TPCMZP *mzp ;
55  struct TPCSEQD *seqd ;
56  struct TPCADCD *adcd ;
57  struct TPCADCX *adcx ;
58  struct TPCPADK *padk ;
59  struct TPCADCR_l *adcr ;
60  struct TPCCPPR_l *cppr ;
61  struct TPCPEDR *pedr ;
62  struct TPCRMSR *rmsr ;
63  struct TPCGAINR *gainr ;
64  struct TPCBADR *badr ;
65  struct TPCMZCLD_local *mzcld ;
66  struct TPCSECLP *seclp ;
67  struct TPCRBCLP *rbclp ;
68 
69 
70 // LOG(DBG,"Sizes %d %d %d %d %d",sizeof(tpc.counts),sizeof(tpc.timebin),
71 // sizeof(tpc.adc),sizeof(tpc.cl_counts),sizeof(tpc.cl)) ;
72 
73 
74  tpc_cached = tpc ;
75 
76  memset(tpc_cached->rdo_present,0,sizeof(tpc_cached->rdo_present)) ; // nix RDO presence bit...
77  // clear the data part - always
78  memset((char *)tpc_cached->counts,0,sizeof(tpc_cached->counts)) ;
79 
80 
81  // clear the cluster part - always
82  memset(tpc_cached->cl_counts,0,sizeof(tpc_cached->cl_counts)) ;
83  memset(tpc_cached->cl_p,0,sizeof(tpc_cached->cl_p)) ;
84 
85  tpc_cached->mode = 0 ;
86  tpc_cached->max_channels_sector = 512*5692 ;
87  tpc_cached->max_channels_all = tpc_cached->max_channels_sector * 24 ;
88  tpc_cached->channels_sector = 0 ;
89  tpc_cached->has_clusters = 0 ;
90 
91  if(m == NULL) return 0 ; // error
92 
93  datap = (struct DATAP *)m ;
94 
95  if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatap = 1;
96 
97  len = qswap32(swapdatap, datap->det[TPC_ID].len) ;
98  if(len==0) {
99  retval = 0 ;
100  return retval;
101  }
102 
103  len *= 4 ; // make it bytes
104 
105  off = qswap32(swapdatap, datap->det[TPC_ID].off) ;
106  if(off==0) {
107  retval = 0 ;
108  return retval;
109  }
110 
111  LOG(DBG,"TPCP len %d, off %d",len,off) ;
112 
113  tpcp = (struct TPCP *)((u_int *)m + off) ;
114  if(checkBank((char *)tpcp,"TPCP") < 0) return 0 ; // wrong bank!
115  if(tpcp->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swaptpcp = 1;
116 
117  t = qswap32(swaptpcp, tpcp->bh.token) ;
118 
119 
120 
121  LOG(DBG,"Token %d...",t,0,0,0,0) ;
122 
123  int found_something = 0 ;
124 
125  // current (i.e. 2002) setup has 2 real sectors packed in
126  // one hyper-sector
127  // if "year 1" format we have 12 RB packed in one DAQ "sector"
128  if(qswap32(swaptpcp, tpcp->bh.format_number)==1) {
129  if((sector%2)==0) {
130  first = 0 ;
131  last = 6 ;
132  hsec = sector ;
133  }
134  else {
135  first = 6 ;
136  last = 12 ;
137  hsec = sector -1 ;
138  }
139  }
140  else {
141  first = 0 ;
142  last = 6 ;
143  hsec = sector ;
144  }
145 
146 
147  LOG(DBG,"Wanted %d is in %d, first RB %d, fmt %d",sector,hsec,first,tpcp->bh.format_number,0) ;
148 
149  LOG(DBG,"Sector %d: len %d, offset %d, len+offset %d",
150  hsec,tpcp->sb[hsec].len,tpcp->sb[hsec].off,tpcp->sb[hsec].len+tpcp->sb[hsec].off,0) ;
151 
152 
153  if(tpcp->sb[hsec].len == 0) {
154  retval = 0 ; // no sector present
155  return retval;
156  }
157 
158  // TPC can't rely on the tpcp lenghts due to 2sector/crate
159  // packing
160  tot_bytes = qswap32(swaptpcp, tpcp->sb[hsec].len) * 4 ;
161 
162  // hack! we'll assume that the data is equally shared between the
163  // 2 sectors in this crate
164  tot_bytes /= 2 ;
165 
166 
167  secp = (struct TPCSECP *) ((char *)tpcp + qswap32(swaptpcp, tpcp->sb[hsec].off)*4) ;
168  if(checkBank((char *)secp,"TPCSECP") < 0) return 0 ;
169 
170 
171  if(t != b2h32(secp->bh.token)) {
172  LOG(CAUTION,"Different token %d in sector %d - should be %d",
173  b2h32(secp->bh.token),hsec,t,0,0) ;
174  return 0 ;
175  }
176 
177 
178 
179  // if after April 2002 we may have SECLP following in the reserved WORD9
180  LOG(DBG,"Sector %d: format number %d",hsec,b2h32(secp->bh.format_number),0,0,0) ;
181 
182  seclp = NULL ;
183  if(b2h32(secp->bh.format_number)==2) {
184  if(secp->bh.w9) {
185  seclp = (struct TPCSECLP *)((u_int *)secp + b2h32(secp->bh.w9)) ;
186  if(checkBank((char *)seclp,CHAR_TPCSECLP) < 0) {
187  seclp = NULL ;
188  }
189  }
190  }
191 
192  // Do the cluster data here...
193  if(seclp) { // Cluster data present!
194 
195  for(rb=first;rb<last;rb++) {
196  int rrb ; // the real, TPC RB [0..5]
197 
198  if(rb >= 6) {
199  rrb = rb - 6 ;
200  }
201  else {
202  rrb = rb ;
203  }
204 
205  if(seclp->rb[rb].len == 0) {
206  LOG(DBG,"RB %d has no CLD data (in SECLP)...",rb,0,0,0,0) ;
207  continue ;
208  }
209 
210  rbclp = (struct TPCRBCLP *) ((char *)seclp + b2h32(seclp->rb[rb].off)*4) ;
211  if(checkBank((char *)rbclp,CHAR_TPCRBCLP) < 0) {
212  break ;
213  }
214 
215  // at this point (RBCLP) data is little endian...
216 
217  if(t != l2h32(rbclp->bh.token)) {
218  LOG(CAUTION,"Different token %d in rb %d - should be %d",
219  l2h32(rbclp->bh.token),rb,t,0,0) ;
220  continue ;
221  }
222 
223  for(mz=0;mz<3;mz++) {
224  if(rbclp->mz[mz].len == 0) {
225  LOG(DBG,"MZ %d has no CLD data (in RBCLP)...",mz,0,0,0,0) ;
226  continue ;
227  }
228 
229  mzcld = (struct TPCMZCLD_local *)((char *)rbclp + l2h32(rbclp->mz[mz].off)*4) ;
230  if(checkBank((char *)mzcld,CHAR_TPCMZCLD) < 0) {
231  LOG(ERR,"Bad TPCMZCLD data bank in sector %d, RB %d, MZ %d (t %d)!",
232  sector+1,rrb+1,mz+1,t,0) ;
233 
234  break ;
235 
236  }
237 
238  if(t != l2h32(mzcld->bh.token)) {
239  LOG(CAUTION,"Different token %d in mz %d - should be %d",
240  l2h32(mzcld->bh.token),mz,t,0,0) ;
241  continue ;
242  }
243 
244  LOG(DBG,"TPCMZCLD MZ %d, token %d",mz,l2h32(mzcld->bh.token),0,0,0) ;
245 
246  ret = unpackCld(sector, mzcld) ;
247  if(ret > 0) { // at least one hit found
248  tpc_cached->has_clusters = 1 ;
249  found_something = 1 ;
250  }
251 
252  if(DAQ_RAW_FORMAT_WORD9 != l2h32(mzcld->bh.w9)) {
253  //printf("%d %d %d %d %u %d\n",evt,sector,rb,l2h32(mzcld->bh.bank_id),l2h32(mzcld->bh.w9),ret) ;
254  //LOG(DBG,"Sec %d, RB %d, MZ %d: w9 %u, hits %u",sector,rb,l2h32(mzcld->bh.bank_id),l2h32(mzcld->bh.w9),ret) ;
255  LOG(DBG,"FCF timing: sec %d, mz %d, hits %d, ms %f",sector+1,rrb*3+mz,ret,((double)l2h32(mzcld->bh.w9)*8.0)/33000.0) ;
256  }
257 
258  }
259 
260  }
261  }
262 
263 
264  // the raw data parts
265 
266  for(rb=first;rb<last;rb++) {
267  int rrb ; // the real, TPC RB [0..5]
268 
269  if(rb >= 6) {
270  rrb = rb - 6 ;
271  }
272  else {
273  rrb = rb ;
274  }
275 
276  if(secp->rb[rb].len == 0) {
277  LOG(DBG,"Sector %d: RB %d not present in raw data...",sector,rb,0,0,0) ;
278  continue ;
279  }
280 
281  rbp = (struct TPCRBP *) ((char *)secp + b2h32(secp->rb[rb].off)*4) ;
282  if(checkBank((char *)rbp,"TPCRBP") < 0) {
283  continue ;
284  }
285 
286 
287  // mark the RB/RDO as present
288  tpc_cached->rdo_present[rrb] = 1;
289 
290  // at this point (RBP) data is different endianess...
291 
292  if(t != l2h32(rbp->bh.token)) {
293  LOG(CAUTION,"Different token %d in rb %d - should be %d",
294  l2h32(rbp->bh.token),rb,t,0,0) ;
295  continue ;
296  }
297 
298  for(mz=0;mz<3;mz++) {
299  LOG(DBG,"RB %d, MZ %d: len %d, off %d",rb,mz,l2h32(rbp->mz[mz].len),l2h32(rbp->mz[mz].off)) ;
300  }
301 
302  for(mz=0;mz<3;mz++) {
303  if(rbp->mz[mz].len == 0) continue ;
304 
305 
306 
307  mzp = (struct TPCMZP *)((char *)rbp + l2h32(rbp->mz[mz].off)*4) ;
308  if(checkBank((char *)mzp,"TPCMZP") < 0) {
309  LOG(ERR,"Bad TPCMZP data bank in sector %d, RB %d, MZ %d (t %d)!",
310  sector+1,rrb+1,mz+1,t,0) ;
311 
312  continue ;
313 
314  }
315 
316  if(t != l2h32(mzp->bh.token)) {
317  LOG(CAUTION,"Different token %d in mz %d - should be %d",
318  l2h32(mzp->bh.token),mz,t,0,0) ;
319  continue ;
320  }
321 
322  LOG(DBG,"TPCMZP token %d",l2h32(mzp->bh.token),0,0,0,0) ;
323 
324  adcr = NULL ;
325  cppr = NULL ;
326  padk = NULL ;
327  rmsr = NULL ;
328  pedr = NULL ;
329  adcd = NULL ;
330  seqd = NULL ;
331 
332 
333  if(mzp->banks[TPC_MZCLD].len != 0) {
334  LOG(DBG,"MZCLD len 0x%08X, off 0x%08X",l2h32(mzp->banks[TPC_MZCLD].len),l2h32(mzp->banks[TPC_MZCLD].off),0,0,0) ;
335 
336  mzcld = (struct TPCMZCLD_local *)((char *)mzp + l2h32(mzp->banks[TPC_MZCLD].off)*4) ;
337  if(checkBank((char *)mzcld,"TPCMZCLD") < 0) ;
338 
339  // do NOTHING!
340 
341  }
342 
343  if(mzp->banks[TPC_SEQD].len != 0) { // RMS too
344  seqd = (struct TPCSEQD *)((char *)mzp + l2h32(mzp->banks[TPC_SEQD].off)*4) ;
345 
346  if(checkBank((char *)seqd,CHAR_TPCSEQD) < 0) ;
347 
348 
349  }
350 
351  if(mzp->banks[TPC_ADCX].len != 0) { // RMS too
352  adcx = (struct TPCADCX *)((char *)mzp + l2h32(mzp->banks[TPC_ADCX].off)*4) ;
353 
354  if(checkBank((char *)adcx,CHAR_TPCADCX) < 0) ;
355  }
356 
357 
358 
359  // what do we have here...
360  if((mzp->banks[TPC_ADCD].len != 0) && (tpc_cached->mode==0)) { // zero-suppressed
361  int rr, pp ;
362 
363 
364  if(mzp->banks[TPC_SEQD].len == 0) { // huh?
365  LOG(WARN,"SEQD has 0 length and ADCD doesn't???",0,0,0,0,0) ;
366  continue ;
367  }
368 
369  seqd = (struct TPCSEQD *)((char *)mzp + l2h32(mzp->banks[TPC_SEQD].off)*4) ;
370  adcd = (struct TPCADCD *)((char *)mzp + l2h32(mzp->banks[TPC_ADCD].off)*4) ;
371 
372  if(checkBank((char *)seqd,"TPCSEQD") < 0) {
373  LOG(ERR,"Bad TPCSEQD data bank in sector %d, RB %d, MZ %d!",
374  sector+1,rrb+1,mz+1,0,0) ;
375 
376  break ;
377  }
378 
379  if(checkBank((char *)adcd,"TPCADCD") < 0) return 0 ;
380 
381  int len = l2h32(seqd->bh.length) - 10 ;
382  len *= 2 ;
383 
384  int adccou = 0 ;
385  int jj ;
386 
387 
388  rr = pp = 1 ;
389 
390  for(jj=0;jj<len;jj++) {
391  int start, last, length, stop ;
392  u_short ss, f8 ;
393  int tbin ;
394 
395  ss = l2h16(seqd->seq[jj]) ;
396  f8 = ss & 0x8000 ;
397 
398 
399  if(f8) { // new pad flags
400  pp = (ss & 0x7FFF) % 256 ; // pad
401  rr = (ss & 0x7FFF) / 256 ; // row
402 
403  if(pp == 0xff) break ;
404  }
405  else {
406  last = ss & 0x0020 ;
407  length = ss & 0x1F ;
408  start = (ss & 0x7FC0) >> 6 ;
409  stop = start + length ;
410 
411  for(tbin=start;tbin<stop;tbin++) {
412  u_char val ;
413  int counter ;
414 
415  val = adcd->adc[adccou++] ;
416 
417  if((rr > 45) || (pp > 182)) {
418  LOG(CRIT, "rr = %d pp=%d",rr,pp);
419  }
420  counter = tpc_cached->counts[rr-1][pp-1] ;
421 
422  if(counter>512) {
423  LOG(CRIT, "%d %d %d counter = %d",ss,rr,pp,counter);
424  }
425 
426 
427  tpc_cached->adc[rr-1][pp-1][counter] = val ;
428  tpc_cached->timebin[rr-1][pp-1][counter] = tbin ;
429  tpc_cached->counts[rr-1][pp-1] += 1 ;
430  tpc_cached->channels_sector++ ;
431  }
432 
433  if(last) pp++ ; // increment the pad for next time
434  }
435 
436 
437  }
438 
439  found_something = 1 ;
440 
441  //printf("Found %d channels\n",tpc_cached->channels-chSave) ;
442 
443  continue ; // don;t look at more banks!
444  }
445 
446 
447  padk = NULL ;
448  if((mzp->banks[TPC_PADK].len != 0)) { // raw ...
449  padk = (struct TPCPADK *)((char *)mzp + l2h32(mzp->banks[TPC_PADK].off)*4) ;
450  if(checkBank((char *)padk,"TPCPADK") < 0) return 0 ;
451 
452  }
453 
454  cppr = NULL ;
455  if(mzp->banks[TPC_CPPR].len != 0) {
456 
457  cppr = (struct TPCCPPR_l *)((char *)mzp + l2h32(mzp->banks[TPC_CPPR].off)*4) ;
458  if(checkBank((char *)cppr,"TPCCPPR") < 0) return 0 ;
459  }
460 
461 
462 
463  if((mzp->banks[TPC_ADCR].len != 0) && (tpc_cached->mode==0)) { // raw ...
464  adcr = (struct TPCADCR_l *)((char *)mzp + l2h32(mzp->banks[TPC_ADCR].off)*4) ;
465 
466  if(checkBank((char *)adcr,"TPCADCR") < 0) return 0 ;
467 
468  if(unpackRaw(sector, 0, padk, cppr, (char *)adcr) < 0) {
469  LOG(ERR,"Problems in RAW data in sector %d, RB %d, MZ %d - skipping...",
470  sector+1,rrb+1,mz+1,0,0) ;
471  }
472 
473  found_something = 1 ;
474 
475  LOG(DBG,"TPC Raw data bank in sector %d, RB %d, MZ %d!",
476  sector+1,rrb+1,mz+1,0,0) ;
477  continue ;
478  }
479 
480  if(mzp->banks[TPC_PEDR].len != 0) { // pedestal data!
481  pedr = (struct TPCPEDR *)((char *)mzp + l2h32(mzp->banks[TPC_PEDR].off)*4) ;
482 
483  if(checkBank((char *)pedr,"TPCPEDR") < 0) return 0 ;
484 
485  found_something = 1 ;
486 
487  unpackRaw(sector, 1, padk, cppr, (char *)pedr) ;
488  tpc_cached->mode = 1 ; // pedestal data!
489 
490  }
491 
492  if(mzp->banks[TPC_RMSR].len != 0) { // RMS too
493  rmsr = (struct TPCRMSR *)((char *)mzp + l2h32(mzp->banks[TPC_RMSR].off)*4) ;
494 
495  if(checkBank((char *)rmsr,"TPCRMSR") < 0) return 0 ;
496 
497  unpackRaw(sector, 2, padk, cppr, (char *)rmsr) ;
498  tpc_cached->mode = 1 ; // pedestal data!
499  found_something = 1 ;
500  }
501 
502  if(mzp->banks[TPC_GAINR].len != 0) { // RMS too
503  gainr = (struct TPCGAINR *)((char *)mzp + l2h32(mzp->banks[TPC_GAINR].off)*4) ;
504 
505  if(checkBank((char *)gainr,CHAR_TPCGAINR) < 0) ;
506 
507  dumpGainr(sector, rrb, gainr) ;
508  found_something = 1 ;
509 
510  }
511 
512  if(mzp->banks[TPC_BADR].len != 0) { // RMS too
513  badr = (struct TPCBADR *)((char *)mzp + l2h32(mzp->banks[TPC_BADR].off)*4) ;
514 
515  if(checkBank((char *)badr,CHAR_TPCBADR) < 0) ;
516  found_something = 1 ;
517  }
518 
519 
520 
521  }
522 
523  }
524 
525 #ifdef SOME_OTHER_DAY
526  if(tpc_cached->has_clusters) {
527  int i, j ;
528  for(i=0;i<45;i++) {
529  printf("Row %2d: ",i+1) ;
530  for(j=0;j<3;j++) {
531  if(tpc_cached->cl_p[i][j]) {
532  printf(" %d[%2d] ",j,l2h32(*tpc_cached->cl_p[i][j])) ;
533  }
534  }
535  printf("\n") ;
536  }
537  }
538 #endif
539 
540 
541 
542  if(tpc_cached->mode == 1) { // hack to prevent pedestal events from having invalid data...
543  for(int r=0;r<45;r++) {
544  for(int p=0;p<182;p++) {
545  if(tpc_cached->counts[r][p] == 0) {
546  memset(tpc_cached->adc[r][p], 0, sizeof(tpc_cached->adc[r][p]));
547  memset(tpc_cached->timebin[r][p], 0, sizeof(tpc_cached->timebin[r][p]));
548  }
549  }
550  }
551  }
552 
553 // evt++ ;
554 // return tot_bytes ;
555  return found_something ;
556 }
557 
558 
559 /*
560  what == 0 ADCR
561  what == 1 PEDR
562  what == 2 RMSR
563 */
564 static int unpackRaw(int sec, int what, struct TPCPADK *padk, struct TPCCPPR_l *cppr, char *mem)
565 {
566  int i, j, t ;
567  u_char row, pad ;
568  u_short *cppseq ;
569  u_char *adcseq ;
570  u_char *adcdata ;
571  u_short *cppdata ;
572 
573  int timebins, cpps ;
574 
575  timebins = 512 ;
576  cpps = 31 ;
577 
578  adcdata = NULL ;
579  cppdata = NULL ;
580 
581 
582  if(padk == NULL) {
583  LOG(WARN,"No PADK? - skipping...",0,0,0,0,0) ;
584  return -1 ;
585  }
586  if(mem == NULL) {
587  LOG(WARN,"No DATA? - skipping...",0,0,0,0,0) ;
588  return -1 ;
589  }
590 
591 
592  switch(what) {
593  case 0 : // ADCR
594  adcdata = (u_char *) mem + sizeof(struct TPCADCR_l);
595  if(cppr == NULL) {
596  LOG(WARN,"No CPPR? - skipping...",0,0,0,0,0) ;
597  return -1 ;
598  }
599 
600  cppdata = (u_short *)((char *)cppr + sizeof(struct TPCCPPR_l)) ;
601  break ;
602  case 1 : // PEDR
603  adcdata = ((struct TPCPEDR *)mem)->ped ;
604  break ;
605  case 2 : // RMSR
606  adcdata = ((struct TPCRMSR *)mem)->rms ;
607  break ;
608  default :
609  LOG(ERR,"Unknown case variable %d",what,0,0,0,0) ;
610  return -1 ;
611  }
612 
613 
614 
615  for(i=0;i<384;i++) {
616  row = padk->rp[i].row ;
617  pad = padk->rp[i].pad ;
618 
619  if((row==0xFF) || (pad==0xFF)) continue ; // unphysical pads...
620 
621  // we'll count from 0
622  row-- ;
623  pad-- ;
624 
625  adcseq = (u_char *) adcdata + timebins*i ;
626 
627 
628  switch(what) {
629  case 0 :
630  cppseq = (u_short *)((char *) cppdata + 2*2*32*i) ;
631 
632  for(j=0;j<cpps;j++) {
633  u_short start, stop ;
634  u_char val ;
635  int counter ;
636 
637 
638 
639  //if(j==0) {
640  // start = 0 ;
641  // stop = 511 ;
642  //}
643  //else break ;
644 
645  start = l2h16(*cppseq++) ;
646  stop = l2h16(*cppseq++) ;
647 
648 
649 
650 
651  if(start & 0xFE00) break ;
652  if(start == 511) stop = 511 ;
653 
654  //LOG(DBG,"Doing row %d, pad %d, 0x%04X, 0x%04X",row,pad,start,stop,0) ;
655 
656 
657  if((stop < start) || (stop >= timebins)) {
658  LOG(WARN,"Bad data stop<start %d<%d - skipping",stop,start,0,0,0) ;
659  return -1 ;
660  }
661 
662 
663  for(t=start;t<=stop;t++) {
664 
665  val = *(adcseq + t) ;
666 
667  if(val == 0) { // skip...
668  // ASIC bug - occasional bad data in timebin 511
669  if(t!=511) {
670  LOG(WARN,"ADC==0, timebin %d, seq %d, lrow %d, row %d, pad %d",t,j,i,row+1,pad+1) ;
671  break ;
672  //return -1 ; // hardware error in the MZ!
673  }
674  }
675 
676 
677  counter = tpc_cached->counts[row][pad] ;
678  tpc_cached->adc[row][pad][counter] = val ;
679  tpc_cached->timebin[row][pad][counter] = t ;
680  tpc_cached->counts[row][pad]++ ;
681  tpc_cached->channels_sector++ ;
682 
683  //LOG(DBG,"Doing row %d, pad %d, tb %d, ADC %d",row,pad,t,val,0) ;
684  }
685 
686  }
687  break ;
688  case 1 : // PEDR is in tpc_cached->adc!
689  for(j=0;j<timebins;j++) {
690  tpc_cached->adc[row][pad][j] = *adcseq++ ;
691  tpc_cached->channels_sector++ ; // count only once!
692  tpc_cached->counts[row][pad]++; //
693  }
694  break ;
695  case 2 : // RMSR is in tpc_cached->timebin!
696  for(j=0;j<timebins;j++) {
697  tpc_cached->timebin[row][pad][j] = *adcseq++ ;
698  tpc_cached->counts[row][pad]++;
699  }
700  break ;
701  }
702 
703 
704  }
705 
706 
707  return 0 ;
708 
709 }
710 
711 
712 static int unpackCld(int sec, struct TPCMZCLD_local *mzcld)
713 {
714  u_int i, j ;
715  u_int nrows, nclust, row ;
716  u_int *rdata ;
717  u_int tot_hits ;
718 
719  tot_hits = 0 ;
720 
721  int bytes = l2h32(mzcld->bh.length)*4 ;
722  if(bytes == sizeof(struct bankHeader)) {
723  LOG(DBG,"No data in TPCMZCLD...",0,0,0,0,0) ;
724  return 0 ;
725  }
726 
727  nrows = l2h32(mzcld->rows) ;
728 
729  if(nrows == 0) {
730  LOG(NOTE,"No MZCLD rows?...",0,0,0,0,0) ;
731  return tot_hits ;
732  }
733 
734  u_int ticks = l2h32(mzcld->bh.w9) ;
735 
736 
737  rdata = (u_int *) mzcld->padrowFiller ;
738 
739  LOG(DBG,"unpackCld: 0x%08X 0x%08X %d: 0x%08X 0x%08X",mzcld,rdata,rdata-(u_int*)mzcld,*rdata,*(rdata+1),0) ;
740 
741  row = 0 ;
742 
743  for(i=0;i<nrows;i++) {
744 
745  //LOG(WARN,"Sector %d: 0x%08X 0x%08X 0x%08X 0x%08X",sec,*(rdata+0),*(rdata+1),*(rdata+2),*(rdata+3)) ;
746 
747  row = l2h32(*rdata++) ; // row starts from "1"!
748  nclust = l2h32(*rdata++) ;
749 
750 
751 
752  int instance ;
753  for(instance=0;instance<3;instance++) {
754  if(tpc_cached->cl_p[row-1][instance]) continue ;
755  tpc_cached->cl_p[row-1][instance] = rdata -2 ; // go back to the "row" pointer!
756  break ;
757  }
758 
759 
760 
761  if(instance >= 3) {
762  LOG(ERR,"Too many row %d contributions!",row) ;
763  }
764 
765  LOG(DBG,"Row %d (%d of %d), %d clusters (tot bytes %d)...",row,i,nrows,nclust,bytes) ;
766 
767 
768 
769  for(j=0;j<nclust;j++) {
770  u_short pad, tm, flags, charge, fl ;
771  u_int t1, t2, p1, p2 ;
772  u_int pt, cf ;
773 
774  pt = l2h32(*rdata++) ;
775  cf = l2h32(*rdata++) ;
776 
777 
778  tm = (pt >> 16) & 0x7FFF ;
779  pad = pt & 0x3FFF ;
780  charge = cf >> 16 ;
781  fl = cf & 0xFFFF ;
782 
783  flags = 0 ;
784  if(pt & 0x8000) flags |= FCF_DOUBLE_PAD ;
785  if(pt & 0x4000) flags |= FCF_DEAD_EDGE ;
786  if(pt & (0x8000 << 16)) flags |= FCF_ONEPAD ;
787  if(fl & 0x8000) flags |= FCF_ROW_EDGE ;
788  if(fl & 0x4000) flags |= FCF_BROKEN_EDGE ;
789 
790  t2 = (fl & 0x00F0) >> 4 ;
791  t1 = fl & 0x000F ;
792 
793  t2 = (tm >> 6) + t2 ;
794  t1 = (tm >> 6) - t1 ;
795 
796  p2 = (fl & 0x3800) >> 11 ;
797  p1 = (fl & 0x0700) >> 8 ;
798 
799  p2 = (pad >> 6) + p2 ;
800  p1 = (pad >> 6) - p1 ;
801 
802 
803  double dpad, dtimebin ;
804 
805  /* pre Apr 18, 2008 there was a 0.5 shift
806  dpad = (double)(pad)/64.0 + 0.5 ; // make pads count from 1.5
807  dtimebin = (double)(tm)/64.0 + 0.5 ; // make timebins count from 0.5
808  */
809 
810  dpad = (double)(pad)/64.0 ;
811  dtimebin = (double)(tm)/64.0 ;
812 
813  LOG(DBG," pad %d, timebin %d, charge %d, flags 0x%04X, t1 %d, t2 %d, p1 %d, p2 %d",(int)dpad,(int)dtimebin,charge,flags,t1,t2,p1,p2) ;
814 
815  u_int ix = tpc_cached->cl_counts[row-1] ;
816  if(ix < TPC_READER_MAX_CLUSTERS) {
817  tpc_cached->cl[row-1][ix].p = (float) dpad ;
818  tpc_cached->cl[row-1][ix].t = (float) dtimebin ;
819  tpc_cached->cl[row-1][ix].charge = charge ;
820  tpc_cached->cl[row-1][ix].flags = flags ;
821  tpc_cached->cl[row-1][ix].t1 = t1 ;
822  tpc_cached->cl[row-1][ix].t2 = t2 ;
823  tpc_cached->cl[row-1][ix].p1 = p1 ;
824  tpc_cached->cl[row-1][ix].p2 = p2 ;
825 
826 
827 
828 
829  tpc_cached->cl_counts[row-1]++ ;
830  tot_hits++ ;
831  }
832  else {
833  LOG(WARN,"Too many clusters (%d) in sector %d, row %d",ix,sec+1,row,0,0) ;
834  return -1 ;
835  }
836 
837  //printf("%2d %2d %7.4f %7.4f %2d %d\n",
838  // sec, row, pad, timebin, flags, charge) ;
839 
840  }
841 
842  }
843 
844 
845  LOG(DBG,"FCF: sec %d, mz %d (last row %d): %d clust in %u ticks",sec+1,l2h32(mzcld->bh.bank_id),row,tot_hits,ticks) ;
846 
847  return tot_hits ;
848 }
849 
850 static int dumpGainr(int sec, int rrb, struct TPCGAINR *g)
851 {
852  static int inited ;
853  static struct blah {
854  u_short row, pad ;
855  } seq[6][3][6*256] ;
856 
857  int i ;
858  int rmz = l2h32(g->bh.bank_id) ;
859 
860 
861 // printf("**** Sector %d: RB %d, MZ %d: events: %d, meanGain %d\n",
862 // sec,rrb,rmz, l2h32(g->events),l2h32(g->meanGain)) ;
863 
864  rmz -= 1 ; // start from 0 - God knows why anymore...
865 
866  if(!inited) {
867  u_int vram, row, pad ;
868 
869  inited = 1 ;
870 
871 
872  int rb, mz ;
873 
874  for(rb=1;rb<=6;rb++) {
875  for(mz=0;mz<3;mz++) {
876  for(i=0;i<MZ_MAX_PADS;i++) {
877  int as = i/64 ;
878 
879  if(as > 5) break ;
880 
881 
882  if(as <= 2) {
883  vram = as * 0x8000 ;
884  }
885  else {
886  vram = (as+1)*0x8000 ;
887  }
888 
889  vram += (i%64)*0x200 ;
890 
891  // and shift to get rid of 0s
892  vram >>= 9 ;
893 
894  // and tack on RB MZ
895  vram |= (rb<<12) | (mz<<9);
896 
897 
898 
899  for(row=1;row<=45;row++) {
900  for(pad=1;pad<=182;pad++) {
901  if((adc_offset[row][pad] & 0xFFFF) == (int)vram) {
902  seq[rb-1][mz][i].row = row ;
903  seq[rb-1][mz][i].pad = pad ;
904  break ;
905  }
906  }
907  }
908 
909  }
910  }
911  }
912 
913  }
914 
915  for(i=0;i<MZ_MAX_PADS;i++) {
916  if(seq[rrb][rmz][i].row == 0) continue ;
917 // printf("Sec %d, row %d, pad %d: gain %d\n",sec+1,seq[rrb][rmz][i].row,seq[rrb][rmz][i].pad,g->gain[i].rel_gain) ;
918  }
919 
920 
921  return 0 ;
922 }
Definition: rb.hh:21
Definition: daq_tpc.h:22