StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ssd_reader.cxx
1 #include <string.h>
2 #include <arpa/inet.h>
3 
4 #include <daqFormats.h>
5 #include <rtsSystems.h>
6 #include <rtsLog.h>
7 #include <rts.h>
8 
9 
10 #include "daq_ssd.h"
11 
12 static int unpackRaw(int rb, int mz, int what, char *mem, ssd_t *ssd) ;
13 
14 
15 /* UNUSED
16 static struct ssdMap {
17  u_char start_ladder ;
18  u_char side ;
19 } ssdMap[4][2] = {
20  { {0,0}, {5,0} },
21  { {10,0}, {15,0} },
22  { {0,1}, {5,1} },
23  { {10,1}, {15,1} }
24 } ;
25 */
26 
27 int ssd_reader(char *m, struct ssd_t *ssd, u_int driver)
28 {
29  int sec ;
30  int rb, mz ;
31  int len ;
32  u_int off ;
33  static int init ;
34 
35  struct TPCP *ssdp ;
36  struct TPCSECP *secp ;
37  struct TPCRBP *rbp ;
38  struct TPCMZP *mzp ;
39  struct TPCSEQD *seqd ;
40  struct TPCADCD *adcd ;
41  struct TPCPADK *padk ;
42  struct TPCADCR_l *adcr ;
43  struct TPCCPPR_l *cppr ;
44  struct TPCPEDR *pedr ;
45  struct TPCRMSR *rmsr ;
46  struct DATAP *datap ;
47  struct DATAPX *datapx ;
48 
49 
50 
51  // clear total channels
52  ssd->channels = 0 ;
53 
54  // set the max channel num - constant
55  ssd->max_channels = 20*16*2*768 ;
56 
57 
58  if(m == NULL) return 0 ;
59 
60  datap = (struct DATAP *)m ;
61 
62  int swapdatap = 0;
63  if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatap = 1;
64 
65  len = qswap32(swapdatap, datap->det[EXT_ID].len) ;
66  if(len == 0) return 0 ; // not even a xtended det
67  off = qswap32(swapdatap, datap->det[EXT_ID].off) ;
68  if(off == 0) return 0 ; // not even a xtended det
69 
70  datapx = (struct DATAPX *)((u_int *)m + off) ;
71 
72  int swapdatapx = 0;
73  if(datapx->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatapx = 1;
74 
75  // verify bank
76  if(checkBank(datapx->bh.bank_type, CHAR_DATAPX) < 0) {
77  return 0 ;
78  }
79 
80 
81  len = qswap32(swapdatapx, datapx->det[SSD_ID-10].len) * 4 ;
82  if(len == 0) return 0 ;
83 
84 
85  off = qswap32(swapdatapx, datapx->det[SSD_ID-10].off) ;
86  if(off == 0) return 0 ;
87 
88  ssdp = (struct TPCP *)((u_int *)datapx + off) ;
89  if(checkBank((char *)ssdp,"SSDP") < 0) return 0 ; // wrong bank!
90 
91 
92  if(!init) { // this is the first call
93  // fool the system into clearing stuff
94  ssd->mode = 1 ;
95 
96  init = 1 ;
97  }
98 
99 
100  // clear the raw contrib
101  memset(ssd->raw,0,sizeof(ssd->raw)) ;
102 
103  // clear the data part
104  memset((char *)ssd->counts,0,sizeof(ssd->counts)) ;
105 
106  // clear mode before
107  ssd->mode = 0 ;
108 
109  u_int occup[4][3] ;
110  memset(occup,0,sizeof(occup)) ;
111 
112  for(sec=0;sec<1;sec++) { // just one sector! I kept the for loop for compatibility
113  int last ;
114 
115 
116  if(ssdp->sb[sec].len == 0) continue ;
117 
118  secp = (struct TPCSECP *) ((u_int *)ssdp + b2h32(ssdp->sb[sec].off)) ;
119  if(checkBank((char *)secp,"SSDSECP") < 0) return 0 ;
120 
121  last = 4 ; // 4 RBs max!
122 
123  for(rb=0;rb<last;rb++) {
124 
125 
126  if(secp->rb[rb].len == 0) continue ;
127 
128  rbp = (struct TPCRBP *) ((u_int *)secp + b2h32(secp->rb[rb].off)) ;
129  if(checkBank((char *)rbp,"SSDRBP") < 0) {
130  continue ;
131  }
132 
133  // at this point (RBP) data is different endianess...
134 
135  for(mz=0;mz<2;mz++) { // 3rd mezzanine will be always empty...
136  if(rbp->mz[mz].len == 0) continue ;
137 
138  mzp = (struct TPCMZP *)((u_int *)rbp + l2h32(rbp->mz[mz].off)) ;
139  if(checkBank((char *)mzp,"SSDMZP") < 0) {
140  continue ;
141  }
142 
143  // what do we have here...
144  if((mzp->banks[TPC_ADCD].len != 0) && (ssd->mode==0)) { // zero-suppressed
145 
146 
147  seqd = (struct TPCSEQD *)((u_int *)mzp + l2h32(mzp->banks[TPC_SEQD].off)) ;
148  adcd = (struct TPCADCD *)((u_int *)mzp + l2h32(mzp->banks[TPC_ADCD].off)) ;
149 
150  if(checkBank((char *)seqd,"SSDSEQD") < 0) continue ;
151  if(checkBank((char *)adcd,"SSDADCD") < 0) continue ;
152 
153  int len = l2h32(seqd->bh.length) - 10 ;
154  len *= 2 ;
155 
156  int adccou = 0 ;
157  int jj ;
158 
159  int rr, pp ;
160 
161  rr = pp = 1 ;
162 
163  for(jj=0;jj<len;jj++) {
164  int start, last, length, stop ;
165  u_short ss, f8 ;
166  int tbin ;
167 
168  ss = l2h16(seqd->seq[jj]) ;
169  f8 = ss & 0x8000 ;
170 
171 
172  if(f8) { // new pad flags
173  pp = (ss & 0x7FFF) % 256 ; // pad
174  rr = (ss & 0x7FFF) / 256 ; // row
175 
176  //LOG(DBG,"f8: last %d, start %d, stop %d, pp %d, rr %d",last,start,stop,pp,rr) ;
177  if(pp == 0xff) break ;
178  }
179  else {
180  last = ss & 0x0020 ;
181  length = ss & 0x1F ;
182  start = (ss & 0x7FC0) >> 6 ;
183  stop = start + length ;
184 
185  //LOG(DBG,"last %d, start %d, stop %d, pp %d, rr %d",last,start,stop,pp,rr) ;
186 
187  for(tbin=start;tbin<stop;tbin++) {
188  u_char val ;
189  int counter ;
190 
191  val = adcd->adc[adccou++] ;
192 
193 
194 
195  //printf("SSD %d:%d strip %d: adc %d\n",rr,pp,tbin,val) ;
196 
197  if(val==0) continue ;
198 
199  counter = ssd->counts[rr-1][pp-1] ;
200 
201  ssd->adc[rr-1][pp-1][counter] = val ;
202  ssd->strip[rr-1][pp-1][counter] = tbin ;
203  ssd->counts[rr-1][pp-1] += 1 ;
204  ssd->channels++ ;
205 
206  occup[rb][mz]++ ;
207  }
208 
209  if(last) pp++ ; // increment the pad for next time
210  }
211 
212  }
213 
214  LOG(NOTE,"SSD: RB %d, MZ %d: counts %d",rb,mz,occup[rb][mz]) ;
215 
216  continue ; // don;t look at more banks!
217 
218 
219  }
220 
221  padk = NULL ;
222  if(mzp->banks[TPC_PADK].len != 0) {
223  padk = (struct TPCPADK *)((u_int *)mzp + l2h32(mzp->banks[TPC_PADK].off)) ;
224 
225  if(checkBank((char *)padk,"SSDPADK") < 0) return 0 ;
226 
227  }
228 
229  cppr = NULL ;
230  if(mzp->banks[TPC_CPPR].len != 0) {
231 
232  cppr = (struct TPCCPPR_l *)((u_int *)mzp + l2h32(mzp->banks[TPC_CPPR].off)) ;
233  if(checkBank((char *)cppr,"SSDCPPR") < 0) return 0 ;
234  }
235 
236  if((mzp->banks[TPC_ADCR].len != 0) && (ssd->mode==0)) { // raw ...
237  adcr = (struct TPCADCR_l *)((u_int *)mzp + l2h32(mzp->banks[TPC_ADCR].off)) ;
238 
239  if(checkBank((char *)adcr,"SSDADCR") < 0) return 0 ;
240 
241  if(unpackRaw(rb, mz, 0, (char *)adcr, ssd) < 0) {
242  LOG(ERR,"Problems in RAW data in sector %d, RB %d, MZ %d - skipping...",
243  sec+1,rb+1,mz+1,0,0) ;
244  }
245 
246  LOG(DBG,"SSD Raw data bank in sector %d, RB %d, MZ %d!",
247  sec+1,rb+1,mz+1,0,0) ;
248  continue ;
249  }
250 
251  if(mzp->banks[TPC_PEDR].len != 0) { // pedestal data!
252  pedr = (struct TPCPEDR *)((u_int *)mzp + l2h32(mzp->banks[TPC_PEDR].off)) ;
253 
254  if(checkBank((char *)pedr,"SSDPEDR") < 0) return 0 ;
255 
256  unpackRaw(rb, mz, 1,(char *)pedr,ssd) ;
257  ssd->mode = 1 ; // pedestal data!
258 
259  }
260 
261  if(mzp->banks[TPC_RMSR].len != 0) { // RMS too
262  rmsr = (struct TPCRMSR *)((u_int *)mzp + l2h32(mzp->banks[TPC_RMSR].off)) ;
263 
264  if(checkBank((char *)rmsr,"SSDRMSR") < 0) return 0 ;
265 
266  unpackRaw(rb, mz, 2, (char *)rmsr, ssd) ;
267  ssd->mode = 1 ; // pedestal data!
268 
269  }
270 
271 
272  }
273 
274  }
275 
276  }
277 
278 
279 
280  return len ;
281 }
282 
283 
284 /*
285  what == 0 ADCR
286  what == 1 PEDR
287  what == 2 RMSR
288 */
289 
290 static int unpackRaw(int rb, int mz, int what, char *mem, ssd_t *ssd)
291 {
292 
293  u_char *adcdata ;
294  int as, ch, strip, cou ;
295  u_char *dta ;
296  int row ;
297 
298  if(mem == NULL) {
299  LOG(WARN,"No DATA? - skipping...",0,0,0,0,0) ;
300  return 0 ;
301  }
302 
303  if(mz == 2) {
304  LOG(WARN,"MZ3 should not exist in SSD's RB %d - skipping...",rb+1,0,0,0,0) ;
305  }
306 
307  adcdata = NULL ;
308 
309  switch(what) {
310  case 0 : // ADCR
311  adcdata = (u_char *) mem + sizeof(struct TPCADCR_l);
312  ssd->raw[rb][mz] = adcdata ;
313  LOG(NOTE,"SSD rb %d, mz %d: raw...",rb,mz) ;
314  break ;
315  case 1 : // PEDR
316  adcdata = ((struct TPCPEDR *)mem)->ped ;
317  ssd->raw[rb][mz] = adcdata ;
318  for(as=0;as<5;as++) {
319  row = rb*10+mz*5+as ; // calc the pseudo-row
320 
321  //printf("Doing RB %d, MZ %c: AS %c\n",rb+1,'A'+mz,'A'+as);
322 
323  for(ch=0;ch<64;ch++) {
324  dta = adcdata + (as*64*512) + (ch*512) ;
325  memcpy(ssd->adc[row][ch],dta,192) ;
326  }
327  }
328 
329  ssd->channels += 5*64*192 ;
330  return 0 ;
331  case 2 : // RMSR
332  adcdata = ((struct TPCRMSR *)mem)->rms ;
333  for(as=0;as<5;as++) {
334  row = rb*10+mz*5+as ;
335  for(ch=0;ch<64;ch++) {
336  dta = adcdata + (as*64*512) + (ch*512) ;
337  memcpy(ssd->strip[row][ch],dta,192) ;
338  }
339  }
340  return 0 ;
341 
342  }
343 
344 
345 
346  // I'll go over the first 5 ASIC blocks
347 
348  for(as=0;as<5;as++) {
349  row = rb*10+mz*5+as ; // pseudo-row
350 
351  for(ch=0;ch<64;ch++) {
352 
353  dta = adcdata + (as*64*512) + ch*512 ;
354 
355  cou = 0 ;
356  for(strip=0;strip<192;strip++) {
357 
358 
359  if(*dta) {
360  ssd->channels++ ;
361  ssd->strip[row][ch][cou] = strip ;
362  ssd->adc[row][ch][cou] = *dta ;
363  cou++ ;
364  }
365 
366  dta++ ;
367  }
368 
369  ssd->counts[row][ch] += cou ;
370  }
371 
372 
373  }
374 
375 
376  return 0 ;
377 
378 }
Definition: rb.hh:21
Definition: daq_ssd.h:21