StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
scReader.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 #include "evpSupport.h"
12 #include "scReader.h"
13 #define SWAP32(bk,x) ((bk->bh.byte_order==0x4030201)?(bk->x):swap32(bk->x))
14 
15 using namespace OLDEVP;
16 namespace OLDEVP {
17 struct sc_t sc;
18 };
19 
20 // reads the Triggers event descriptor from DATAP directly...
21 int OLDEVP::scReader(char *m)
22 {
23  int len ;
24  u_int off ;
25  struct SCD *scd ;
26  struct DATAP *datap ;
27  struct DATAPX *datapx ;
28 
29 
30  if(m == NULL) return EVP_DATA_ERR ;
31 
32  datap = (struct DATAP *)m ;
33 
34 
35  len = SWAP32(datap,det[EXT_ID].len) ;
36  if(len == 0) return EVP_NO_DET;
37  assert(len>0&&len<99999);
38 
39  off = SWAP32(datap,det[EXT_ID].off);
40  if(off == 0) return EVP_NO_DET;
41 
42  datapx = (struct DATAPX *)((u_int *)m+off);
43 
44  // verify bank
45  assert(checkBank(datapx->bh.bank_type,CHAR_DATAPX) == 0);
46 
47  len = SWAP32(datapx,det[SC_ID-10].len);
48  if(len == 0) return EVP_NO_DET ;
49 
50  off = SWAP32(datapx,det[SC_ID-10].off);
51  if(off == 0) return EVP_NO_DET;
52 
53  //LOG(DBG,"SC raw len %d (0x%x), off %d(0x%x)",len,len,off,off,0) ;
54 
55  scd = (struct SCD *)(((u_int *)datapx)+off) ;
56  if(checkBank((char *)scd,"SCD") < 0) assert(0);
57 
58  if(scd->bh.token == 0) {
59  //LOG(DBG,"Token 0 - skipping...",0,0,0,0,0) ;
60  return EVP_NO_DET ;
61  }
62  int sz = SWAP32(scd,bh.length);
63 
64  if((len != sz) ||
65  ((u_int)len > sizeof(SCD))) {
66  //LOG(ERR, "SCD Sizes not consistent: datap=%d, bh=%d SCD=%d",
67  // len, sz, sizeof(scd),0,0);
68 
69  return EVP_DATA_ERR;
70  }
71 
72 
73  // copy scd data into sc
74  sc.time = SWAP32(scd,time);
75  int mag = SWAP32(scd,mag_field);
76  sc.mag_field= *((float*)&mag);
77  for(int i=0;i<16;i++) {
78  sc.rich_scalers[i] = SWAP32(scd,rich_scalers[i]);
79  }
80 
81  sc.timelag = SWAP32(datap,time) - sc.time;
82 
83  int alag = sc.timelag > 0 ? sc.timelag : -sc.timelag;
84 
85  if(alag > 5) sc.valid = 0;
86  else sc.valid = 1;
87 
88  return len;
89 }
90