StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
pmd_reader.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <arpa/inet.h>
4 
5 #include <rtsLog.h>
6 #include <rtsSystems.h>
7 #include <daqFormats.h>
8 #include <rts.h>
9 
10 
11 #include "daq_pmd.h"
12 
13 static pmd_t *pmd_p ;
14 static int adcReader(int sec, struct PMDADCD *adcd) ;
15 static int pedReader(int sec, int type, struct PMDPEDR *pedr) ;
16 
17 
18 int pmd_reader(char *m, struct pmd_t *pmd, u_int driver)
19 {
20  struct DATAP *datap = (struct DATAP *)m ;
21  struct DATAPX *datapx ;
22  struct PMDP *pmdp = 0;
23  struct PMDSECP *secp = 0;
24  struct PMDADCD *adcd ;
25  struct PMDPEDR *pedr ;
26  struct PMDRMSR *rmsr ;
27  struct PMDTHRR *thrr ;
28 
29  int off ;
30  int len = 0 ;
31 
32  int start_sec, stop_sec ;
33  int sec, type ;
34  int ret ;
35  int swapdatap = 0;
36  int swapdatapx = 0;
37 
38  pmd_p = pmd ; // need this for support routines!
39 
40  if(pmd_p->max_channels) ; // already set...
41  else {
42  pmd_p->channels = 0 ;
43  pmd_p->max_channels = 2*PMD_CRAMS_MAX*2*PMD_CRAMS_CH_MAX ;
44  pmd_p->mode = 0 ;
45  pmd_p->status[0] = pmd_p->status[1] = 0 ;
46  }
47 
48  LOG(DBG,"PMD reader: %p: sector %d",datap,driver) ;
49 
50  if(datap == NULL) return 0 ; // no datap and no request from SFS
51 
52  if(driver != 0) {
53  secp = (struct PMDSECP *) m ; // this comes directly from SFS!
54  start_sec = stop_sec = driver - 1 ;
55  goto do_sectors ;
56  }
57  else {
58  start_sec = 0 ;
59  stop_sec = 1 ;
60  }
61 
62 
63 
64  if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatap = 1;
65 
66  off = qswap32(swapdatap, datap->det[EXT_ID].len) ;
67  if(off == 0) return 0 ; // not even a xtended det
68 
69  off = qswap32(swapdatap, datap->det[EXT_ID].off) ;
70 
71 
72  datapx = (struct DATAPX *)(m + off*4) ;
73 
74  // verify bank
75  if(checkBank(datapx->bh.bank_type, CHAR_DATAPX) < 0) {
76  return 0 ;
77  }
78 
79 
80  if(datapx->bh.byte_order != DAQ_RAW_FORMAT_ORDER) swapdatapx = 1;
81 
82  len = qswap32(swapdatapx, datapx->det[PMD_ID-10].len) * 4 ;
83 
84  if(len == 0) return 0 ;
85 
86 
87  off = qswap32(swapdatapx, datapx->det[PMD_ID-10].off)*4 ;
88 
89  pmdp = (struct PMDP *)((char *)datapx + off) ;
90 
91  // verify bank
92  if(checkBank(pmdp->bh.bank_type, CHAR_PMDP) < 0) {
93  return -1 ;
94  }
95 
96 
97 // memset(pmd_p->adc,0,sizeof(pmd_p->adc)) ;
98 
99 
100  do_sectors:;
101  for(sec=start_sec;sec<=stop_sec;sec++) { // 2 possible sectors
102 
103  if(driver == 0) {
104  LOG(DBG,"PMD %d, len %d, offset %u",sec,b2h32(pmdp->sec[sec].len),b2h32(pmdp->sec[sec].off),0,0) ;
105 
106  if(pmdp->sec[sec].len == 0) continue ;
107 
108 
109  secp = (struct PMDSECP *)((char *)pmdp + b2h32(pmdp->sec[sec].off)*4) ;
110  }
111 
112  if(checkBank((char *)secp,CHAR_PMDSECP) < 0) return -1 ;
113 
114  pmd_p->status[sec] = b2h32(secp->bh.format_number) ;
115 
116  for(type=0;type<4;type++) {
117  // hack!
118  if(secp->type[type].len == 0) continue ;
119 
120  switch(type) {
121  case PMD_ADCD_N :
122  adcd = (struct PMDADCD *)((char *)secp + b2h32(secp->type[type].off)*4) ;
123  if(checkBank(adcd->bh.bank_type, CHAR_PMDADCD) < 0) return -1 ;
124 
125  ret = adcReader(sec, adcd) ;
126  if(ret < 0) return -1 ;
127 
128  pmd_p->channels += ret ;
129 
130  break ;
131  case PMD_PEDR_N :
132  pedr = (struct PMDPEDR *)((char *)secp + b2h32(secp->type[type].off)*4) ;
133  if(checkBank(pedr->bh.bank_type, CHAR_PMDPEDR) < 0) return -1 ;
134 
135  ret = pedReader(sec, type, pedr) ;
136  if(ret < 0) return -1 ;
137 
138  pmd_p->mode = 1 ;
139 
140  pmd_p->channels += ret ;
141 
142 
143  break ;
144  case PMD_RMSR_N :
145  rmsr = (struct PMDRMSR *)((char *)secp + b2h32(secp->type[type].off)*4) ;
146  if(checkBank(rmsr->bh.bank_type, CHAR_PMDRMSR) < 0) return -1 ;
147 
148  ret = pedReader(sec, type, (struct PMDPEDR *)rmsr) ;
149  if(ret < 0) return -1 ;
150 
151  pmd_p->mode = 1 ;
152 
153 
154  break ;
155  case PMD_THRR_N :
156  thrr = (struct PMDTHRR *)((char *)secp + b2h32(secp->type[type].off)*4) ;
157  if(checkBank(thrr->bh.bank_type, CHAR_PMDTHRR) < 0) return -1 ;
158 
159  ret = pedReader(sec, type, (struct PMDPEDR *)thrr) ;
160  if(ret < 0) return -1 ;
161 
162  pmd_p->mode = 1 ;
163 
164  break ;
165 
166  }
167  }
168 
169  }
170 
171  return len ;
172 
173 }
174 
175 static int adcReader(int sec, struct PMDADCD *adcd)
176 {
177  int items = b2h32(adcd->bh.length) - sizeof(adcd->bh)/4 ;
178  u_int *datum ;
179  u_int j ;
180  u_int rb, mz ;
181  u_int ch_num ;
182  u_int ch ;
183 
184  if(items <= 0) return 0 ;
185 
186 
187  datum = adcd->data ;
188 
189  ch_num = 0 ;
190 
191  LOG(DBG,"ADC: PMD sec %d: items %d",sec,items,0,0,0) ;
192 
193  u_int *end_datum = datum + items ;
194 
195  for(end_datum=datum+items;datum<end_datum;) {
196  u_int tmp = b2h32(*datum) ;
197  datum++ ;
198 
199  // DAQ pseudo-header
200  rb = (tmp & 0xFF000000) >> 24 ;
201  mz = (tmp & 0x00FF0000) >> 16 ;
202  ch = (tmp & 0x0000FFFF) ;
203 
204 
205  //printf(" RB %d, MZ %d, ch_num %d\n",rb,mz,ch) ;
206 
207  if(rb >= PMD_CRAMS_MAX) {
208  LOG(ERR,"PMD: Bad RB number %d",rb,0,0,0,0) ;
209  return -1 ;
210  }
211 
212  if((mz != 0) && (mz != 1)) {
213  LOG(ERR,"PMD: Bad mezzanine %d in RB %d",mz,rb,0,0,0) ;
214  return -1 ;
215  }
216 
217  if(ch > PMD_CRAMS_CH_MAX) {
218  LOG(ERR,"PMD: too many channels in RB %d, MZ %d: %d",rb,mz,ch,0,0) ;
219  return -1 ;
220  }
221 
222  LOG(DBG,"PMD ADCD: sec %d, rb %d, mz %d, channels %d",sec,rb,mz,ch,0) ;
223 
224  for(j=0;j<ch;j++) {
225  u_int val ;
226 
227  val = b2h32(*datum) ;
228  datum++ ;
229 
230 
231  if(val & 0x40000000) { // zero suppressed...
232  u_int channel = (val & 0x007ff000) >> 12 ;
233  val &= 0xFFF ;
234 
235  //printf(" %4d: %4d == %4d\n",j,channel,val) ;
236 
237  if(channel >= PMD_CRAMS_CH_MAX) {
238  LOG(ERR,"PMD: channel too big %d in RB %d, MZ %d: %d",rb,mz,channel,0,0) ;
239  return -1 ;
240  }
241 
242  pmd_p->adc[sec][rb][mz][channel] = val ;
243  ch_num++ ;
244  }
245  else {
246  static const int maxCounts = 13;
247  static int Counts = 0;
248  if (Counts < maxCounts) {
249  Counts++;
250  LOG(ERR,"PMD %d: RB %d, MZ %d: illegal value in channel %d: 0x%08X",sec,rb,mz,j,val) ;
251  if (Counts == maxCounts) {LOG(ERR,"PMD stop print out after %i counts",Counts) ;}
252  }
253  }
254  }
255  }
256 
257  return ch_num ;
258 }
259 
260 static int pedReader(int sec, int type, struct PMDPEDR *pedr)
261 {
262  int items ;
263  u_short *datum ;
264  int rb, mz, ch ;
265  int ch_num ;
266  int valid ;
267 
268 
269  items = (b2h32(pedr->bh.length) - sizeof(pedr->bh)/4)*2 ;
270 
271  if(items <= 0) return 0 ;
272 
273 
274  datum = pedr->data ;
275 
276  ch_num = 0 ;
277 
278  //printf("PMD sec %d: items %d\n",sec,items) ;
279 
280  u_short *end_datum = datum + items ;
281 
282  for(end_datum=datum+items;datum<end_datum;) {
283  int channel ;
284  u_int tmp = b2h16(*datum) ;
285  datum++ ;
286 
287  rb = (tmp & 0x7F00) >> 8 ;
288  mz = (tmp & 0x00FF) ;
289 
290  valid = tmp & 0x8000 ;
291 
292 
293  ch = PMD_CRAMS_CH_MAX ;
294 
295  if(!valid) {
296  LOG(WARN,"Type %d: datum not valid at %u == 0x%04X",type,datum-pedr->data, (u_int)tmp,0,0) ;
297 
298  datum += ch ;
299  continue ;
300  }
301 
302 
303  if(rb >= PMD_CRAMS_MAX) {
304  LOG(ERR,"PMD: Bad RB number %d",rb,0,0,0,0) ;
305  return -1 ;
306  }
307 
308  if((mz != 0) && (mz != 1)) {
309  LOG(ERR,"PMD: Bad mezzanine %d in RB %d",mz,rb,0,0,0) ;
310  return -1 ;
311  }
312 
313  LOG(DBG," %d: SEC %d, RB %d, MZ %d: channel count %d...",type,sec,rb,mz,ch) ;
314 
315 
316 
317 
318  for(channel=0;channel<ch;channel++) {
319  u_int val ;
320  u_short blah ;
321 
322  blah = *datum ;
323  val = b2h16(blah) ;
324  datum++ ;
325 
326 
327  //printf(" t %d: crate %d: rb %d, mz %d, ch %4d == 0x%04X ",type,sec,rb,mz,channel,val) ;
328  //printf("%f\n",(double)val/16.0) ;
329 
330  switch(type) {
331  case PMD_PEDR_N :
332  pmd_p->ped[sec][rb][mz][channel] = val ;
333  break ;
334  case PMD_RMSR_N :
335  pmd_p->rms[sec][rb][mz][channel] = val ;
336  break ;
337  case PMD_THRR_N :
338  pmd_p->thr[sec][rb][mz][channel] = val ;
339  break ;
340  }
341  ch_num++ ;
342 
343  }
344  }
345 
346  return ch_num ;
347 }
Definition: rb.hh:21
Definition: daq_pmd.h:7