StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_tof.cxx
1 #include <assert.h>
2 #include <sys/types.h>
3 #include <errno.h>
4 
5 #include <rtsLog.h>
6 #include <rtsSystems.h>
7 
8 #include <SFS/sfs_index.h>
9 
10 #include <DAQ_READER/daqReader.h>
11 #include <DAQ_READER/daq_dta.h>
12 
13 #include "daq_tof.h"
14 
15 extern int tof_reader(char *mem, struct tof_t *tof, u_int driver) ;
16 
17 
18 
19 
20 const char *daq_tof::help_string = "\
21 \n\
22 TOF Help: \n\
23 Supported Banks: \n\
24  raw returns=ptr of start of DDL data; c1=sector[1..1]; c2=rdo[1..4]; \n\
25  legacy returns=ptr to struct tof_t; \n\
26 \n\
27 \n\
28 " ;
29 
31 {
32 public:
34  daq_det_factory::det_factories[TOF_ID] = this ;
35  }
36 
37  daq_det *create() {
38  return new daq_tof ;
39  }
40 } ;
41 
42 static daq_det_tof_factory tof_factory ;
43 
44 
45 
46 daq_tof::daq_tof(daqReader *rts_caller)
47 {
48  rts_id = TOF_ID ;
49  name = rts2name(rts_id) ;
50  sfs_name = "tof" ;
51  caller = rts_caller ;
52  if(caller) caller->insert(this, rts_id) ;
53 
54  raw = new daq_dta ;
55  legacy = new daq_dta ;
56 
57 
58 
59  LOG(DBG,"%s: constructor: caller %p",name,rts_caller) ;
60  return ;
61 }
62 
63 daq_tof::~daq_tof()
64 {
65  LOG(DBG,"%s: DEstructor",name) ;
66 
67  delete raw ;
68  delete legacy ;
69 
70  return ;
71 }
72 
73 
74 
75 daq_dta *daq_tof::get(const char *bank, int sec, int row, int pad, void *p1, void *p2)
76 {
77  Make() ;
78 
79  if(present==0) return 0 ;
80 
81  LOG(DBG,"%s: looking for bank %s",name,bank) ;
82 
83  if(strcmp(bank,"*")==0) bank = "legacy" ;
84 
85 
86 
87  if(strcasecmp(bank,"raw")==0) {
88  if((present & DET_PRESENT_SFS)==0) return 0 ; // no DDL
89  return handle_raw(sec,row) ; // actually sec, rdo; r1 is the number of bytes
90  }
91  else if(strcasecmp(bank,"legacy")==0) {
92 // if((present & DET_PRESENT_DATAP)==0) return 0 ; // no legacy
93  return handle_legacy() ;
94  }
95  else {
96  LOG(ERR,"%s: unknown bank type \"%s\"",name,bank) ;
97  }
98 
99  return 0 ;
100 }
101 
102 
103 daq_dta *daq_tof::handle_legacy()
104 {
105  assert(caller) ;
106 
107  legacy->create(1,"tof_t",rts_id,DAQ_DTA_STRUCT(tof_t)) ;
108 
109 
110  tof_t *tof_p = (tof_t *) legacy->request(1) ; // need ONE tof_t object
111 
112 
113  memset(tof_p->ddl_words,0,sizeof(tof_p->ddl_words)) ; // zap it!
114 
115  if(present & DET_PRESENT_DATAP) { // datap
116  tof_reader(caller->mem, tof_p, m_Debug) ;
117  }
118  else {
119  tof_p->mode = 1 ; // old...
120  tof_p->channels = 0 ; // signal that we have NO old data...
121  tof_p->max_channels = 48+48+32+12 ; // stale but compatible with old tofReadaer...
122 
123 
124  for(int r=1;r<=4;r++) {
125  daq_dta *dd = handle_raw(0,r) ;
126  if(dd && dd->iterate()) {
127  u_int *tmp = (u_int *)dd->Void ;
128 
129  u_int words = dd->ncontent/4 ; // tof wants words...
130 
131  LOG(DBG,"TOF: RDO %d: bytes %d (max %d)",r,dd->ncontent,sizeof(tof_p->ddl[0])) ;
132 
133  if(words > (sizeof(tof_p->ddl[0])/4)) {
134  LOG(ERR,"TOF: Huge event in RDO %d: %d words -- clipping to %d words",
135  r,words,sizeof(tof_p->ddl[0])/4) ;
136 
137  words = sizeof(tof_p->ddl[0])/4 ;
138  }
139 
140 
141  tof_p->ddl_words[r-1] = words ;
142 
143 
144  for(u_int i=0;i<words;i++) { //words!
145  tof_p->ddl[r-1][i] = l2h32(*tmp) ;
146  tmp++ ;
147  }
148 
149  }
150 
151  }
152 
153  }
154  legacy->finalize(1,1,0,0) ; // 1 entry; sector 1, row 0, pad 0
155  legacy->rewind() ;
156 
157  return legacy ;
158 }
159 
160 
161 daq_dta *daq_tof::handle_raw(int sec, int rdo)
162 {
163  char str[128] ;
164  int tot_bytes ;
165  int min_rdo, max_rdo ;
166  int min_sec, max_sec ;
167  struct {
168  int sec ;
169  int rb ;
170  u_int bytes ;
171  } obj[MAX_SEC*MAX_RDO] ;
172 
173  // sanity
174  sec = 1 ; // nothing else for now...
175  min_sec = max_sec = sec ;
176 
177  if(rdo==-1) {
178  min_rdo = 1 ;
179  max_rdo = MAX_RDO ;
180  }
181  else if((rdo<1) || (rdo>MAX_RDO)) return 0 ;
182  else {
183  min_rdo = max_rdo = rdo ;
184  }
185 
186  // bring in the bacon from the SFS file....
187  assert(caller) ;
188 
189  // calc total bytes
190  tot_bytes = 0 ;
191  int o_cou = 0 ;
192  for(int s=min_sec;s<=max_sec;s++) {
193  for(int r=min_rdo;r<=max_rdo;r++) {
194 
195  sprintf(str,"%s/sec%02d/rb%02d/raw",sfs_name, s, r) ;
196  char *full_name = caller->get_sfs_name(str) ;
197 
198  LOG(DBG,"%s: trying sfs on \"%s\"",name,str) ;
199  if(full_name == 0) continue ;
200 
201  int size = caller->sfs->fileSize(full_name) ; // this is bytes
202 
203  LOG(DBG,"Got %d",size) ;
204 
205  if(size <= 0) {
206  LOG(DBG,"%s: %s: not found in this event",name,str) ;
207  continue ;
208  }
209  else {
210  obj[o_cou].sec = s ;
211  obj[o_cou].rb = r ;
212  obj[o_cou].bytes = size ;
213 
214  o_cou++ ;
215 
216  tot_bytes += size ;
217  LOG(DBG,"%s: %s: reading in \"%s\": bytes %d",name,str,"raw", size) ;
218  }
219  }
220  }
221 
222  raw->create(tot_bytes,"tof_raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
223 
224  for(int i=0;i<o_cou;i++) {
225 
226  sprintf(str,"%s/sec%02d/rb%02d/raw",sfs_name, obj[i].sec, obj[i].rb) ;
227  char *full_name = caller->get_sfs_name(str) ;
228  if(full_name == 0) continue ;
229 
230  char *st = (char *) raw->request(obj[i].bytes) ;
231 
232  int ret = caller->sfs->read(full_name, st, obj[i].bytes) ;
233 
234  if(ret != (int)obj[i].bytes) {
235  LOG(ERR,"%s: %s: read failed, expect %d, got %d [%s]",name,str,
236  obj[i].bytes,ret,strerror(errno)) ;
237  }
238 
239  else {
240  LOG(NOTE,"%s: %s read %d bytes",name,str,ret) ;
241 
242  }
243 
244  raw->finalize(obj[i].bytes,obj[i].sec,obj[i].rb,0) ;
245 
246  }
247 
248  raw->rewind() ;
249  return raw ;
250 }
251 
252 
253 // knows how to get the token out of an event...
254 int daq_tof::get_token(char *addr, int words)
255 {
256  int cou ;
257  struct daq_trg_word trg[128] ;
258 
259  cou = get_l2(addr,words,trg,1) ;
260 
261  if(cou==0) return -1000 ; // special marker...
262  if(trg[0].t==0) return -ENOSYS ;
263 
264  return trg[0].t ;
265 }
266 
267 // knows how to get a/the L2 command out of the event...
268 int daq_tof::get_l2(char *addr, int words, struct daq_trg_word *trg, int rdo)
269 {
270  u_int *w ;
271  int cou = 0 ;
272  int t_cou = 0 ;
273  int in_words = words ;
274 
275  int err = 0 ;
276  int trg_start = -1 ;
277 
278  w = (u_int *)addr ;
279  words-- ; // point to last datum now...
280 
281  // this will be DBG...
282  LOG(DBG,"First words 0x%08X 0x%08X 0x%08X, last words 0x%08X 0x%08X 0x%08X [+0x%08X], %u",
283  w[0],w[1],w[2],w[words-2],w[words-1],w[words],w[words+1],words+1) ;
284 
285 
286  // prompt token is in word 0!
287  // unless it just reads 0xA0000000, in which case this is purely trigger data -- no
288  // content!
289 
290  trg[t_cou].t = w[0] & 0xFFF ;
291  trg[t_cou].daq = (w[0]>>12) & 0xF ;
292  trg[t_cou].trg = (w[0]>>16) & 0xF ;
293  trg[t_cou].rhic = 0 ;
294  trg[t_cou].rhic_delta = 0 ;
295  t_cou++ ;
296 
297 
298  LOG(NOTE,"prompt: T %4d, trg %d, daq %d [0x%08X]: words %d",trg[0].t,trg[0].trg,trg[0].daq,w[0],in_words) ;
299 
300  if(in_words < 2) { // absolute minimum
301  err |= 1 ;
302  LOG(ERR,"[%d] bad word count %d < 2",rdo,in_words);
303  }
304 
305 
306  if(w[0] == 0xA0000000) { // trigger only
307  trg[0].t = 4097; // trigger only contrib...
308  }
309  else {
310  if(in_words < 3) { // minimum if not trigger-only
311  err |= 1 ;
312  LOG(ERR,"[%d] bad word count %d <= 3",rdo,in_words);
313  }
314 
315  if(trg[0].t == 0) {
316  err |= 2 ;
317  LOG(ERR,"[%d] token 0",rdo);
318  trg[0].t = 4097 ;
319  }
320 
321  if(trg[0].trg != 4) { // we will allow only 4!
322  err |= 2 ;
323  LOG(ERR,"[%d] bad trg_cmd %d != 4",rdo,trg[0].trg);
324  }
325 
326  if(w[1] != 0xDEADFACE) {
327  err |= 1 ;
328  LOG(ERR,"[%d] bad DEADCODE 0x%08X",rdo,w[1]);
329  }
330 
331 
332  }
333 
334  if((w[0] >> 20) != 0xA00) {
335  err |= 1 ;
336  LOG(ERR,"[%d] bad first word 0x%08X doesn't start with 0xA00",rdo,w[0]);
337  trg[0].t = 4097 ;
338  }
339 
340 
341 #if 0
342  // move backwards to the start of the trigger block and just count at first
343  while(words) {
344  if((w[words] >> 28)==0xA) { // trigger stuff
345  words-- ;
346  cou++ ;
347  }
348  else { // stop when non 0xA reached...
349  break ;
350  }
351  }
352 
353  if(cou==0) {
354  err |= 1 ;
355  LOG(ERR,"[%d] No Trigger FIFO contribution??",rdo);
356  }
357 
358  words++ ; // move forward to start of trigger
359 
360  trg_start = words ; // mark the word where trigger starts
361 
362  // words now points to the first trigger of the FIFO block
363  for(int i=0;i<cou;i++) {
364  int l_err = 0 ;
365 
366  trg[t_cou].t = w[words+i] & 0xFFF ;
367  trg[t_cou].daq = (w[words+i]>>12) & 0xF ;
368  trg[t_cou].trg = (w[words+i]>>16) & 0xF ;
369  trg[t_cou].rhic = i+1 ;
370  trg[t_cou].rhic_delta = i+1 ;
371 
372  if(trg[t_cou].t == 0) l_err = 1 ; // token 0
373 
374  // we will take OUT all the non-L2 components here...
375  switch(trg[t_cou].trg) {
376  case 4 : // normal
377  continue ; // don't put it in!
378  case 13 : // abort
379  case 15 : // accept
380  break ;
381  default : // take out ALL other L0 commands!
382  l_err = 1 ;
383  break ;
384  }
385 
386  if(l_err) {
387  LOG(ERR,"[%d] bad FIFO trg (%d.): T %4d, trg %d, daq %d [0x%08X]",rdo,words+i,trg[t_cou].t,trg[t_cou].trg,trg[t_cou].daq,w[words+i]);
388  err |= 2 ;
389  continue ; // don't use it!
390  }
391 
392  t_cou++ ;
393 
394  if(t_cou >=120) {
395  err |= 2 ;
396  LOG(ERR,"[%d] Too many trigger contributions %d >= 120",rdo,t_cou) ;
397  break ;
398  }
399  }
400 
401 #endif
402 
403 #if 0
404  if((t_cou==1) && (trg[0].t > 0) && (trg[0].t<=4095)) {
405  trg[t_cou].t = trg[0].t ;
406  trg[t_cou].daq = 0 ;
407  trg[t_cou].trg = 15 ;
408  trg[t_cou].rhic = trg[0].rhic + 1 ;
409  trg[t_cou].rhic_delta = 1 ;
410  t_cou++ ;
411 
412  }
413  else {
414  LOG(WARN,"%d %d %d???",t_cou,trg[0].t,trg[0].trg) ;
415  }
416 #endif
417 
418  if(err) {
419  LOG(ERR,"[%d] Bad Event: T %4d: words %d, trg_words %d (start at %d) : 0x%08X 0x%08X 0x%08X",
420  rdo,trg[0].t,in_words,cou,trg_start,w[0],w[1],w[2]) ;
421 
422  //for(int i=0;i<cou;i++) {
423  // LOG(ERR,"[%d] trigger %d: %d/%d: 0x%08X",rdo,i,cou,w[trg_start+i]) ;
424  //}
425  }
426 
427  if(err & 1) { // critical -- blow the whole event
428  return -1 ;
429  }
430 
431  return t_cou ;
432 }
433 
Definition: daq_tof.h:7
Definition: tof.h:15
Definition: rb.hh:21