StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_btow.cxx
1 #include <sys/types.h>
2 #include <string.h>
3 #include <assert.h>
4 
5 #include <rtsLog.h>
6 #include <rtsSystems.h>
7 
8 
9 #include <SFS/sfs_index.h>
10 #include <DAQ_READER/daqReader.h>
11 #include <DAQ_READER/daq_dta.h>
12 
13 
14 
15 #include <DAQ_EMC/daq_emc.h> // need this for the old style stuff...
16 
17 #include "daq_btow.h"
18 
19 
20 // maps received from Gerard on Mar 17,2006
21 const int btow_crate_map[] = {
22  0x12, 0x11, 0x10, 0x1e,
23  0x1d, 0x1c, 0x1b, 0x1a,
24  0x19, 0x18, 0x17, 0x16,
25  0x15, 0x14, 0x13, 0x01,
26  0x0f, 0x0e, 0x0d, 0x0c,
27  0x0b, 0x0a, 0x09, 0x08,
28  0x07, 0x06, 0x05, 0x04,
29  0x03, 0x02
30 } ;
31 
33 {
34 public:
36  daq_det_factory::det_factories[BTOW_ID] = this ;
37  }
38 
39  daq_det *create() {
40  return new daq_btow ;
41  }
42 } ;
43 
44 static daq_det_btow_factory btow_factory ;
45 
46 
47 const char *daq_btow::help_string = "BTOW tst\n" ;
48 
49 
50 
51 daq_btow::daq_btow(daqReader *rts_caller)
52 {
53 
54  LOG(DBG,"BTOW: rts_id %d, name %s",rts_id,name) ;
55 
56  rts_id = BTOW_ID ;
57  name = rts2name(rts_id) ;
58  sfs_name = "btow" ;
59  caller = rts_caller ;
60  if(caller) caller->insert(this, rts_id) ;
61 
62  raw = new daq_dta ;
63  adc = new daq_dta ;
64 
65  LOG(DBG,"%s: constructor: caller %p",name,caller) ;
66 }
67 
68 daq_btow::~daq_btow()
69 {
70  LOG(DBG,"%s: destructor",name) ;
71 
72  delete adc ;
73  delete raw ;
74 
75  return ;
76 }
77 
78 /* figure out the presence... */
79 int daq_btow::Make()
80 {
81  int dummy ;
82 
83  present = 0 ; // assume not...
84 
85 
86  assert(caller) ;
87 
88  evt_num++ ;
89 
90  LOG(DBG,"%s: Make()",name) ;
91 
92  if(presence()) { // in SFS
93  present |= DET_PRESENT_SFS;
94  LOG(NOTE,"%s: %d: has SFS(%s)",name,evt_num,sfs_name) ;
95  }
96  else if(legacyDetp(rts_id, caller->mem)) { // directly in DATAP
97  present |= DET_PRESENT_DATAP ;
98  LOG(NOTE,"%s: %d: has DATAP",name,evt_num) ;
99  }
100  else if(getEmcTrgData(caller->mem,1,&dummy)) { // perhaps in the old TRG bank (FY08); BTOW has index 1!
101  present |= DET_PRESENT_TRG ;
102  LOG(NOTE,"%s: %d: has DATAP within Trigger",name,present) ;
103  }
104  else {
105  LOG(DBG,"%s: not present",name) ;
106  }
107 
108 
109 
110  return present ;
111 
112 }
113 
114 daq_dta *daq_btow::get(const char *bank, int c1, int c2, int c3, void *p1, void *p2)
115 {
116  Make() ;
117  if(!present) return 0 ;
118 
119 
120  if(strcasecmp(bank,"raw") == 0) {
121  return handle_raw() ;
122  }
123  else if(strcasecmp(bank,"adc") == 0) {
124  return handle_adc() ;
125  }
126  else {
127  LOG(ERR,"%s: unknown bank \"%s\"",name,bank) ;
128  return 0 ;
129  }
130 
131 }
132 
133 
134 daq_dta *daq_btow::handle_raw()
135 {
136  char *from, *st ;
137  int bytes ;
138  char str[256] ;
139  const char *full_name = "?" ; // just a dummy...
140 
141  from = 0 ;
142  full_name = "?" ;
143 
144  assert(caller) ;
145 
146 
147  if(present & DET_PRESENT_DATAP) { // datap...
148  char *mem = (char *) legacyDetp(rts_id, caller->mem) ;
149  from = emc_single_reader(mem, &bytes, rts_id) ;
150  if(from == 0) return 0 ;
151 
152  }
153  else if(present & DET_PRESENT_TRG) {
154  from = getEmcTrgData(caller->mem,1,&bytes) ;
155  if(from == 0) return 0 ;
156 
157  }
158  else {
159  sprintf(str,"%s/sec%02d/rb%02d/raw",sfs_name,1,1) ;
160  full_name = caller->get_sfs_name(str) ;
161  if(!full_name) return 0 ;
162 
163  bytes = caller->sfs->fileSize(full_name) ;
164 
165  }
166 
167  raw->create(bytes,"btow_raw",rts_id,DAQ_DTA_STRUCT(char)) ;
168  st = (char *) raw->request(bytes) ;
169 
170  if(present & DET_PRESENT_SFS) { // from SFS...
171  int ret = caller->sfs->read(full_name, st, bytes) ;
172  if(ret != bytes) {
173  LOG(ERR,"ret is %d") ;
174  }
175  }
176  else {
177  assert(from) ;
178  memcpy(st, from, bytes) ;
179  }
180 
181  raw->finalize(bytes,1,1,0) ;
182  raw->rewind() ;
183 
184  LOG(DBG,"Returning raw bank...") ;
185  return raw ;
186 }
187 
188 
189 daq_dta *daq_btow::handle_adc()
190 {
191  u_short *raw_dta ;
192 
193  LOG(DBG,"Entering adc") ;
194 
195  daq_dta *dd = handle_raw() ;
196 
197  LOG(DBG,"raw bank %p",dd) ;
198 
199  if(dd && dd->iterate()) {
200  raw_dta = (u_short *) dd->Byte ;
201  }
202  else {
203  return 0 ;
204  }
205 
206 
207  LOG(DBG,"Got raw bank, on to adc...") ;
208 
209  adc->create(1,"adc", rts_id, DAQ_DTA_STRUCT(btow_t)) ;
210 
211  btow_t *btow_p = (btow_t *) adc->request(1) ; // need 1 struct...
212 
213 
214  // unpack
215 
216  // This is really a bad hack where DDL DAQ and DDL Trigger moved the
217  // whole event by 4 bytes to stay compatible with the VME BTOW Receiver
218 
219  u_short *data = (u_short *)((char *)raw_dta + 4 + 128) ; // 4 byte dummy, 128 byte header
220 
221 
222 
223 #if 0
224  u_short *ppp = (u_short *) raw_dta ;
225  for(int i=0;i<10;i++) {
226  LOG(TERR,"%d: 0x%04X",i,ppp[i]) ;
227  }
228 #endif
229 
230  for(int j=0;j<BTOW_PRESIZE;j++) {
231  for(int i=0;i<BTOW_MAXFEE;i++) {
232  btow_p->preamble[i][j] = l2h16(*data++) ;
233  }
234  }
235 
236  for(int j=0;j<BTOW_DATSIZE;j++) {
237  for(int i=0;i<BTOW_MAXFEE;i++) {
238  btow_p->adc[i][j] = l2h16(*data++) ;
239  }
240  }
241 
242 
243  adc->finalize(1,1,1,0) ;
244  adc->rewind() ;
245 
246  return adc ;
247 }
248 
249 
250 int daq_btow::get_l2(char *addr, int words, struct daq_trg_word *trg, int rdo)
251 {
252  const int BTOW_DDL_BYTES = 9972 ;
253  int buff_bytes = words * 4 ;
254  int rdo1 = rdo ;
255  int tcou = 0 ;
256 
257  u_short *us = (u_short *)addr ;
258 
259  u_short t_hi = l2h16(us[2]) ;
260  u_short t_lo = l2h16(us[3]) ;
261 
262  int err = 0 ;
263 
264  if(buff_bytes != BTOW_DDL_BYTES) {
265  err |= 1 ;
266  LOG(ERR,"Received %d bytes, expect %d!?",buff_bytes,BTOW_DDL_BYTES) ;
267  }
268 
269  if((t_lo & 0xFF00) || (t_hi & 0xFFF0)) { //error
270  err |= 1 ;
271  LOG(ERR,"Corrupt token: t_hi 0x%04X, t_lo 0x%04X",t_hi,t_lo) ;
272 
273  // sanitize
274  t_lo &= 0xFF ;
275  t_hi &= 0xF ;
276 
277  }
278 
279 
280  if(us[0] != 4) { // was fixed only in FY13...
281  //LOG(WARN,"trg cmd not 4 == 0x%04X",us[0]) ;
282  us[0] = 4 ;
283  }
284 
285  // L0 part
286  trg[tcou].t = t_hi*256 + t_lo ;
287  trg[tcou].daq = us[1] ;
288  trg[tcou].trg = us[0] ;
289  trg[tcou].rhic = l2h16(us[4]) ;
290  tcou++ ;
291 
292 
293 
294  if(trg[0].t == 0) {
295  err |= 1 ;
296  LOG(ERR,"token 0!") ;
297  }
298 
299  if(err) {
300  LOG(WARN,"RDO %d: 0x%04X 0x%04X 0x%04X 0x%04X 0x%04X",rdo1, us[0],us[1],us[2],us[3],us[4]) ;
301 
302  }
303 
304  if(err & 1) { // critical
305  return -1 ;
306  }
307 
308  return tcou ;
309 
310 
311 
312 }
313 
Definition: daq_btow.h:9