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