StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sc_reader.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <arpa/inet.h>
4 
5 #include <rtsLog.h>
6 
7 
8 #include <daqFormats.h>
9 #include <rtsSystems.h>
10 #include <rts.h>
11 
12 #include "daq_sc.h"
13 
14 int sc_reader(char *m, struct sc_t *sc, u_int driver)
15 {
16  int swapdatap = 0;
17  int swapscd = 0;
18 
19  if(m == NULL) return 0 ;
20 
21  DATAP *datap = (struct DATAP *)m ;
22  int len, off ;
23 
24 
25  // zap all first memset(&sc,0,sizeof(sc)) ;
26 
27  datap = (struct DATAP *) m ;
28  if(datap->bh.byte_order != DAQ_RAW_FORMAT_ORDER)
29  swapdatap = 1;
30 
31 
32  len = qswap32(swapdatap, datap->det[EXT_ID].len);
33  if(len == 0) return 0;
34 
35  off = qswap32(swapdatap, datap->det[EXT_ID].off);
36  if(off == 0) return 0;
37 
38  // printf("Have datapx\n");
39  DATAPX *datapx = (struct DATAPX *)(((u_int *)m)+off);
40  if(checkBank(datapx->bh.bank_type,"DATAPX") < 0) {
41  return 0;
42  }
43 
44 
45  int swapdatapx=0;
46  if(datapx->bh.byte_order != DAQ_RAW_FORMAT_ORDER)
47  swapdatapx = 1;
48 
49  len = qswap32(swapdatapx, datapx->det[SC_ID-10].len);
50  if(len == 0) return 0 ;
51  len *= 4 ;
52 
53  off = qswap32(swapdatapx, datapx->det[SC_ID-10].off);
54  if(off == 0) return 0;
55 
56  LOG(DBG,"SC raw len %d (0x%x), off %d(0x%x)",len,len,off,off,0) ;
57 
58  SCD *scd = (struct SCD *)(((u_int *)datapx)+off) ;
59  if(checkBank(scd->bh.bank_type,"SCD") < 0) { // wrong bank!
60  return 0 ;
61  }
62 
63  if(scd->bh.byte_order != DAQ_RAW_FORMAT_ORDER)
64  swapscd = 1;
65 
66 
67  int sz = qswap32(swapscd, scd->bh.length);
68  sz*=4;
69 
70  if((len != sz) ||
71  ((u_int)len > sizeof(SCD))) {
72  LOG(ERR, "SCD Sizes not consistent: datap=%d, bh=%d SCD=%d",
73  len, sz, sizeof(scd),0,0);
74 
75  return 0;
76  }
77 
78  LOG(DBG,"Need to swap SCD? %d",swapscd) ;
79 
80  // copy scd data into sc
81  sc->time = qswap32(swapscd, scd->time);
82  int mag = qswap32(swapscd, scd->mag_field);
83  memcpy(&sc->mag_field, &mag, 4);
84  for(int i=0;i<16;i++) {
85  sc->rich_scalers[i] = qswap32(swapscd, scd->rich_scalers[i]);
86  }
87 
88  sc->timelag = qswap32(swapdatap, datap->time) - sc->time;
89 
90  int alag = sc->timelag > 0 ? sc->timelag : -sc->timelag;
91 
92  if(alag > 5) sc->valid = 0;
93  else sc->valid = 1;
94 
95  return len;
96 }
Definition: daq_sc.h:6