StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_esmd.cxx
1 #include <sys/types.h>
2 #include <errno.h>
3 #include <assert.h>
4 
5 #include <rtsLog.h>
6 #include <rtsSystems.h>
7 
8 
9 
10 #include <SFS/sfs_index.h>
11 #include <DAQ_READER/daqReader.h>
12 #include <DAQ_READER/daq_dta.h>
13 
14 
15 #include "daq_esmd.h"
16 
17 int esmd_crate_map[] = {
18  0x40, 0x41, 0x42, 0x43,
19  0x44, 0x45, 0x46, 0x47,
20  0x48, 0x49, 0x4a, 0x4b,
21  0x4c, 0x4d, 0x4e, 0x4f,
22  0x50, 0x51, 0x52, 0x53,
23  0x54, 0x55, 0x56, 0x57,
24  0x58, 0x59, 0x5a, 0x5b,
25  0x5c, 0x5d, 0x5e, 0x5f,
26  0x60, 0x61, 0x62, 0x63,
27  0x64, 0x65, 0x66, 0x67,
28  0x68, 0x69, 0x6a, 0x6b,
29  0x6c, 0x6d, 0x6e, 0x6f
30 };
31 
32 const char *daq_esmd::help_string = "ESMD\n\
33 adc returns esmd_t;\n\
34 raw returns raw data\n" ;
35 
37 {
38 public:
40  daq_det_factory::det_factories[ESMD_ID] = this ;
41  }
42 
43  daq_det *create() {
44  return new daq_esmd ;
45  }
46 } ;
47 
48 static daq_det_esmd_factory esmd_factory ;
49 
50 
51 daq_esmd::daq_esmd(daqReader *rts_caller)
52 {
53  rts_id = ESMD_ID ;
54  name = rts2name(rts_id) ;
55  sfs_name = "esmd" ;
56  caller = rts_caller ;
57 
58  if(caller) caller->insert(this, rts_id) ;
59 
60  raw = new daq_dta ;
61  adc = new daq_dta ;
62 
63  LOG(DBG,"%s: constructor: caller %p",name,rts_caller) ;
64  return ;
65 }
66 
67 daq_esmd::~daq_esmd()
68 {
69  LOG(DBG,"%s: DEstructor",name) ;
70 
71  delete raw ;
72  delete adc ;
73 
74  return ;
75 }
76 
77 
78 
79 daq_dta *daq_esmd::get(const char *bank, int sec, int row, int pad, void *p1, void *p2)
80 {
81  Make() ;
82  if(present == 0) return 0 ;
83 
84 
85  if(strcasecmp(bank,"raw")==0) {
86  return handle_raw() ;
87  }
88  else if(strcasecmp(bank,"adc")==0) {
89  return handle_adc() ;
90  }
91 
92 
93  LOG(ERR,"%s: unknown bank type \"%s\"",name,bank) ;
94  return 0 ;
95 }
96 
97 
98 
99 daq_dta *daq_esmd::handle_raw()
100 {
101  char *from , *st ;
102  int bytes ;
103 
104  assert(caller) ; // sanity...
105 
106 
107 
108  if(!present) {
109  LOG(ERR,"%s: not present?",name) ;
110  return 0 ;
111  }
112  else {
113  LOG(DBG,"%s: present %d",name,present) ;
114  }
115 
116  if(present & DET_PRESENT_DATAP) { // in datap!
117  char *mem = (char *)legacyDetp(rts_id, caller->mem) ;
118  from = emc_single_reader(mem, &bytes, rts_id) ;
119  if(from == 0) return 0 ;
120 
121 
122  raw->create(bytes,"esmd_raw",rts_id,DAQ_DTA_STRUCT(char)) ;
123  st = (char *) raw->request(bytes) ;
124 
125 
126 
127  memcpy(st, from, bytes) ;
128 
129  }
130  else { // SFS
131  char str[256] ;
132  char *full_name ;
133 
134  sprintf(str,"%s/sec%02d/rb%02d/raw",sfs_name, 1, 1) ;
135  full_name = caller->get_sfs_name(str) ;
136 
137  if(!full_name) return 0 ;
138 
139  bytes = caller->sfs->fileSize(full_name) ; // this is bytes
140 
141  raw->create(bytes,"esmd_raw",rts_id,DAQ_DTA_STRUCT(char)) ;
142  st = (char *) raw->request(bytes) ;
143 
144  int ret = caller->sfs->read(str, st, bytes) ;
145  if(ret != bytes) {
146  LOG(ERR,"ret is %d") ;
147  }
148  }
149 
150 
151  raw->finalize(bytes,1,1,0) ; // sector 1; rdo 1; pad irrelevant...
152  raw->rewind() ;
153 
154  return raw ;
155 
156 }
157 
158 
159 
160 daq_dta *daq_esmd::handle_adc()
161 {
162  u_short *raw_dta ;
163 
164  daq_dta *dd = handle_raw() ;
165 
166  LOG(DBG,"%s: got raw %p",name,dd) ;
167 
168  if(dd && dd->iterate()) {
169  raw_dta = (u_short *) dd->Byte ;
170  }
171  else {
172  return 0 ;
173  }
174 
175  adc->create(1,"esmd_adc", rts_id, DAQ_DTA_STRUCT(esmd_t)) ;
176 
177  esmd_t *esmd_p = (esmd_t *) adc->request(1) ; // 1 object
178 
179 
180 
181  u_short *data = (u_short *)((char *)raw_dta + 128) ;
182 
183  if(present & DET_PRESENT_DATAP) {
184  data += 2 ; // for old, VME based RBs we need to also skip 4 bytes of junk
185  }
186 
187  // FY04 data has only 30 instead of 48 so we need to zap all...
188  memset(esmd_p,0,sizeof(esmd_t)) ;
189 
190 
191 
192  int max_fee ;
193  if(raw->ncontent < (48*192)) { // FY04 data
194  max_fee = 30 ;
195  }
196  else {
197  max_fee = 48 ;
198  }
199 
200 
201 
202 
203  for(int j=0;j<ESMD_PRESIZE;j++) {
204  for(int i=0;i<max_fee;i++) {
205  esmd_p->preamble[i][j] = l2h16(*data++) ;
206  }
207  }
208 
209  for(int j=0;j<ESMD_DATSIZE;j++) {
210  for(int i=0;i<max_fee;i++) {
211  esmd_p->adc[i][j] = l2h16(*data++) ;
212  }
213  }
214 
215 
216 
217  adc->finalize(1,1,1,0) ;
218 
219  adc->rewind() ;
220 
221  return adc ;
222 }
223 
224 
225 int daq_esmd::get_l2(char *buff, int buff_words, struct daq_trg_word *trg, int rdo1)
226 {
227  const int ESMD_DDL_BYTES = 18948 ;
228  int buff_bytes = buff_words * 4 ;
229 
230  u_short *us = (u_short *)buff ;
231 
232  u_short t_hi = l2h16(us[2]) ;
233  u_short t_lo = l2h16(us[3]) ;
234 
235  int err = 0 ;
236 
237  if(buff_bytes != ESMD_DDL_BYTES) {
238  err |= 1 ;
239  LOG(ERR,"Received %d bytes, expect %d!?",buff_bytes,ESMD_DDL_BYTES) ;
240  }
241 
242  if((t_lo & 0xFF00) || (t_hi & 0xFFF0)) { //error
243  err |= 1 ;
244  LOG(ERR,"Corrupt token: t_hi 0x%04X, t_lo 0x%04X",t_hi,t_lo) ;
245 
246  // sanitize
247  t_lo &= 0xFF ;
248  t_hi &= 0xF ;
249 
250  }
251 
252 
253  if(us[0] != 4) { // Gerard fixed the trigger command
254  //LOG(WARN,"trg_cmd %d?",us[0]) ;
255  us[0] = 4 ;
256  }
257 
258  // L0 part
259  trg[0].t = t_hi*256 + t_lo ;
260  trg[0].daq = us[1] ;
261  trg[0].trg = us[0] ;
262  trg[0].rhic = l2h16(us[4]) ;
263 
264 
265 // if(us[0] != 0xF) {
266 // err |= 1 ;
267 // LOG(ERR,"trg cmd not 15 == 0x%04X",us[0]) ;
268 // }
269 
270  if(trg[0].t == 0) {
271  err |= 1 ;
272  LOG(ERR,"token 0!") ;
273  }
274 
275  if(err) {
276  LOG(WARN,"RDO %d error: 0x%04X 0x%04X 0x%04X 0x%04X 0x%04X",rdo1, us[0],us[1],us[2],us[3],us[4]) ;
277 
278  }
279 
280  if(err & 1) { // critical
281  return -1 ;
282  }
283 
284  return 1 ;
285 }