StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_etow.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 
18 #include "daq_etow.h"
19 
20 
21 const int etow_crate_map[] = {
22  0x01, 0x02, 0x03, 0x04, 0x05, 0x06
23 };
24 
25 
26 const char *daq_etow::help_string = "ETOW tst\n" ;
27 
29 {
30 public:
32  daq_det_factory::det_factories[ETOW_ID] = this ;
33  }
34 
35  daq_det *create() {
36  return new daq_etow ;
37  }
38 } ;
39 
40 static daq_det_etow_factory etow_factory ;
41 
42 
43 
44 daq_etow::daq_etow(daqReader *rts_caller)
45 {
46 
47  LOG(DBG,"ETOW: rts_id %d, name %s",rts_id,name) ;
48 
49  // dname is ignored
50  rts_id = ETOW_ID ;
51  name = rts2name(rts_id) ;
52  sfs_name = "etow" ;
53  caller = rts_caller ;
54  if(caller) caller->insert(this, rts_id) ;
55 
56  raw = new daq_dta ;
57  adc = new daq_dta ;
58 
59  LOG(DBG,"%s: constructor: caller %p",name,caller) ;
60 }
61 
62 daq_etow::~daq_etow()
63 {
64  LOG(DBG,"%s: destructor",name) ;
65 
66  delete adc ;
67  delete raw ;
68 
69  return ;
70 }
71 
72 /* figure out the presence... */
73 int daq_etow::Make()
74 {
75  int dummy ;
76 
77  present = 0 ; // assume not...
78  assert(caller) ;
79 
80  evt_num++ ;
81 
82  LOG(DBG,"%s: Make()",name) ;
83 
84  if(presence()) { // in SFS
85  present |= DET_PRESENT_SFS ;
86  }
87  else if(legacyDetp(rts_id, caller->mem)) { // directly in DATAP
88  present |= DET_PRESENT_DATAP ;
89  }
90  else if(getEmcTrgData(caller->mem,2,&dummy)) { // perhaps in the old TRG bank (FY08); ETOW has index 1!
91  present |= DET_PRESENT_TRG ;
92  }
93 
94  switch(present) {
95  case DET_PRESENT_DATAP :
96  LOG(NOTE,"%s: %d: has DATAP",name,evt_num) ;
97  break ;
98  case DET_PRESENT_SFS :
99  LOG(NOTE,"%s: %d: has SFS(%s)",name,evt_num,sfs_name) ;
100  break ;
101  case DET_PRESENT_TRG :
102  LOG(NOTE,"%s: %d: has DATAP within Trigger",name,evt_num) ;
103  break ;
104  }
105 
106  return present ;
107 
108 }
109 
110 daq_dta *daq_etow::get(const char *bank, int c1, int c2, int c3, void *p1, void *p2)
111 {
112  Make() ;
113  if(!present) return 0 ;
114 
115 
116  if(strcasecmp(bank,"raw") == 0) {
117  return handle_raw() ;
118  }
119  else if(strcasecmp(bank,"adc") == 0) {
120  return handle_adc() ;
121  }
122  else {
123  LOG(ERR,"%s: unknown bank \"%s\"",name,bank) ;
124  return 0 ;
125  }
126 
127 }
128 
129 
130 daq_dta *daq_etow::handle_raw()
131 {
132  char *from, *st ;
133  int bytes ;
134 
135  assert(caller) ;
136 
137 
138 
139  if(present & DET_PRESENT_DATAP) { // datap...
140  char *mem = (char *)legacyDetp(rts_id, caller->mem) ;
141  from = emc_single_reader(mem, &bytes, rts_id) ;
142  if(from == 0) return 0 ;
143 
144  }
145  else if(present & DET_PRESENT_TRG) {
146  from = getEmcTrgData(caller->mem,2,&bytes) ; // etow is index "0"
147  if(from == 0) return 0 ;
148  }
149  else {
150  char str[256] ;
151  char *full_name ;
152 
153  sprintf(str,"%s/sec%02d/rb%02d/raw",sfs_name,1,1) ;
154  // first iteration had this wrong name!
155  // sprintf(str,"%s/sec%d/rb%02d/raw",sfs_name,0,1) ;
156  full_name = caller->get_sfs_name(str) ;
157 
158  LOG(DBG,"%s: %s: %p",name,str,full_name) ;
159 
160  if(!full_name) return 0 ;
161 
162  bytes = caller->sfs->fileSize(full_name) ;
163 
164  LOG(DBG,"ETOW sfs bytes %d",bytes) ;
165 
166  raw->create(bytes,"raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
167 
168  st = (char *) raw->request(bytes) ;
169 
170  int ret = caller->sfs->read(str, st, bytes) ;
171  if(ret != bytes) {
172  LOG(ERR,"ret is %d") ;
173  }
174  goto done ;
175  }
176 
177  raw->create(bytes,"raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
178  st = (char *)raw->request(bytes) ;
179  memcpy(st, from, bytes) ;
180 
181  done: ;
182 
183  raw->finalize(bytes,1,1,0) ;
184  raw->rewind() ;
185 
186  LOG(DBG,"Returning raw bank...") ;
187  return raw ;
188 }
189 
190 
191 daq_dta *daq_etow::handle_adc()
192 {
193  u_short *raw_dta ;
194 
195  LOG(DBG,"Entering adc") ;
196 
197  daq_dta *dd = handle_raw() ;
198 
199  LOG(DBG,"raw bank %p",dd) ;
200 
201  if(dd && dd->iterate()) {
202  raw_dta = (u_short *) dd->Byte ;
203  }
204  else {
205  return 0 ;
206  }
207 
208 
209  LOG(DBG,"Got raw bank, on to adc...") ;
210 
211  adc->create(1,"adc", rts_id, DAQ_DTA_STRUCT(etow_t)) ;
212 
213  etow_t *etow_p = (etow_t *) adc->request(1) ; // need 1 struct...
214 
215 
216  // unpack
217  u_short *data = (u_short *)((char *)raw_dta + 4 + 128) ; // 4 byte dummy, 128 byte header
218 
219  for(int j=0;j<ETOW_PRESIZE;j++) {
220  for(int i=0;i<ETOW_MAXFEE;i++) {
221  etow_p->preamble[i][j] = l2h16(*data++) ;
222  }
223  }
224 
225  for(int j=0;j<ETOW_DATSIZE;j++) {
226  for(int i=0;i<ETOW_MAXFEE;i++) {
227  etow_p->adc[i][j] = l2h16(*data++) ;
228  }
229  }
230 
231 
232  adc->finalize(1,1,1,0) ;
233  adc->rewind() ;
234 
235  return adc ;
236 }
237 
238 int daq_etow::get_l2(char *addr, int words, struct daq_trg_word *trg, int rdo1)
239 {
240  const int ETOW_DDL_BYTES = 2100 ;
241  int buff_bytes = words * 4 ;
242 
243  u_short *us = (u_short *)addr ;
244 
245  u_short t_hi = l2h16(us[2]) ;
246  u_short t_lo = l2h16(us[3]) ;
247 
248  int err = 0 ;
249 
250  if(buff_bytes != ETOW_DDL_BYTES) {
251  err |= 1 ;
252  LOG(ERR,"Received %d bytes, expect %d!?",buff_bytes,ETOW_DDL_BYTES) ;
253  }
254 
255  if((t_lo & 0xFF00) || (t_hi & 0xFFF0)) { //error
256  err |= 1 ;
257  LOG(ERR,"Corrupt token: t_hi 0x%04X, t_lo 0x%04X",t_hi,t_lo) ;
258 
259  // sanitize
260  t_lo &= 0xFF ;
261  t_hi &= 0xF ;
262 
263  }
264 
265  if(us[0] != 4) {
266  //LOG(WARN,"trg cmd not 4 == 0x%04X",us[0]) ;
267  us[0] = 4 ;
268  }
269 
270 
271  // L0 part
272  trg[0].t = t_hi*256 + t_lo ;
273  trg[0].daq = us[1] ;
274  trg[0].trg = us[0] ;
275  trg[0].rhic = l2h16(us[4]) ;
276 
277 
278 
279  if(trg[0].t == 0) {
280  err |= 1 ;
281  LOG(ERR,"token 0!") ;
282  }
283 
284  if(err) {
285  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]) ;
286 
287  }
288 
289  if(err & 1) { // critical
290  return -1 ;
291  }
292 
293  return 1 ; // just the prompt
294 
295 
296 
297 }
298 
Definition: daq_etow.h:9