StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_fcs.cxx
1 #include <assert.h>
2 #include <sys/types.h>
3 #include <errno.h>
4 #include <math.h>
5 #include <time.h>
6 
7 #include <rtsLog.h>
8 #include <rtsSystems.h>
9 #include <daqFormats.h>
10 
11 #include <SFS/sfs_index.h>
12 
13 #include <DAQ_READER/daqReader.h>
14 #include <DAQ_READER/daq_dta.h>
15 
16 
17 #include "daq_fcs.h"
18 #include "fcs_data_c.h"
19 
20 const char *daq_fcs::help_string = "\
21 \n\
22 FCS 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 \n\
26 \n\
27 " ;
28 
30 {
31 public:
33  LOG(DBG,"%s",__PRETTY_FUNCTION__) ;
34  daq_det_factory::det_factories[FCS_ID] = this ;
35  }
36 
37  daq_det *create() {
38  LOG(DBG,"%s",__PRETTY_FUNCTION__) ;
39  return new daq_fcs ;
40  }
41 } ;
42 
43 static daq_det_fcs_factory fcs_factory ;
44 
45 
46 
47 daq_fcs::daq_fcs(daqReader *rts_caller)
48 {
49  LOG(DBG,"%s",__PRETTY_FUNCTION__) ;
50 
51  rts_id = FCS_ID ;
52  name = rts2name(rts_id) ;
53  sfs_name = "fcs" ;
54  caller = rts_caller ;
55 
56  if(caller) caller->insert(this, rts_id) ;
57 
58  raw = new daq_dta ;
59  adc = new daq_dta ;
60  zs = new daq_dta ;
61  ped = new daq_dta ;
62 
63  zs->meta = (void *)&fcs_meta ;
64 
65  LOG(DBG,"%s: constructor: caller %p",name,rts_caller) ;
66  return ;
67 }
68 
69 daq_fcs::~daq_fcs()
70 {
71  LOG(DBG,"%s: DEstructor",name) ;
72 
73  delete raw ;
74  delete adc ;
75  delete zs ;
76  delete ped ;
77 
78  return ;
79 }
80 
81 
82 
83 daq_dta *daq_fcs::get(const char *bank, int sec, int raw, int pad, void *p1, void *p2)
84 {
85  Make() ;
86 
87  if(present==0) return 0 ;
88 
89 
90  zs->meta = 0 ;
91 
92  LOG(DBG,"%s: looking for bank %s",name,bank) ;
93 
94  if(strcmp(bank,"*")==0) bank = "raw" ;
95 
96 
97 
98  if(strcasecmp(bank,"raw")==0) {
99  if((present & DET_PRESENT_SFS)==0) return 0 ; // no DDL
100  return handle_raw() ; // actually sec, rdo; r1 is the number of bytes
101  }
102  else if(strcasecmp(bank,"adc")==0) {
103  if((present & DET_PRESENT_SFS)==0) return 0 ; // no DDL
104  return handle_adc() ; // actually sec, rdo; r1 is the number of bytes
105  }
106  else if(strcasecmp(bank,"zs")==0) {
107  if((present & DET_PRESENT_SFS)==0) return 0 ; // no DDL
108  return handle_zs() ; // actually sec, rdo; r1 is the number of bytes
109  }
110  else if(strcasecmp(bank,"ped")==0) {
111  if((present & DET_PRESENT_SFS)==0) return 0 ; // no DDL
112  return handle_ped() ; // actually sec, rdo; r1 is the number of bytes
113  }
114  else {
115  LOG(ERR,"%s: unknown bank type \"%s\"",name,bank) ;
116  }
117 
118  return 0 ;
119 }
120 
121 
122 daq_dta *daq_fcs::handle_ped()
123 {
124  char str[128] ;
125  int min_rdo = 1 ;
126  int max_rdo = 8 ;
127  char *full_name ;
128  int got_any = 0 ;
129 
130  // bring in the bacon from the SFS file....
131  assert(caller) ;
132 
133  ped->create(1024,"fcs_ped",rts_id,DAQ_DTA_STRUCT(u_char)) ;
134 
135 
136  for(int s=1;s<=12;s++) {
137  for(int r=min_rdo;r<=max_rdo;r++) {
138  sprintf(str,"%s/sec%02d/rdo%d/ped",sfs_name,s,r) ;
139  full_name = caller->get_sfs_name(str) ;
140 
141 
142  if(full_name==0) continue ;
143 
144  int size = caller->sfs->fileSize(full_name) ; // this is bytes
145 
146  LOG(DBG,"S%d:%d: Got size %d",1,r,size) ;
147  if(size <= 0) {
148  LOG(NOTE,"%s: %s: not found in this event",name,str) ;
149  return 0 ;
150  }
151 
152  got_any = 1 ;
153 
154  char *st = (char *) ped->request(size) ;
155 
156  caller->sfs->read(full_name, st, size) ;
157 
158  LOG(DBG,"sfs read succeeded") ;
159 
160  ped->finalize(size,s,r,0) ;
161 
162  full_name = 0 ;
163  }
164  }
165 
166  ped->rewind() ;
167 
168  if(got_any) {
169  return ped ;
170  }
171  else return 0 ;
172 
173 }
174 
175 daq_dta *daq_fcs::handle_raw()
176 {
177  char str[128] ;
178  int min_rdo = 1 ;
179  int max_rdo = 8 ;
180  char *full_name ;
181  int got_any = 0 ;
182 
183  // bring in the bacon from the SFS file....
184  assert(caller) ;
185 
186 
187  sprintf(str,"%s/sec01/rb01/raw",sfs_name) ;
188  full_name = caller->get_sfs_name(str) ;
189 
190  LOG(DBG,"%s: trying sfs on \"%s\"",name,str) ;
191  if(full_name) { // FY17 version
192  min_rdo = 1 ;
193  max_rdo = 1 ;
194  version = 2017 ;
195  }
196 
197  raw->create(1024,"fcs_raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
198 
199 
200  for(int s=1;s<=12;s++) {
201  for(int r=min_rdo;r<=max_rdo;r++) {
202  if(full_name==0) {
203  sprintf(str,"%s/sec%02d/rdo%d/raw",sfs_name,s,r) ;
204  full_name = caller->get_sfs_name(str) ;
205 
206  LOG(NOTE,"str %s, full_name %s",str,full_name) ;
207  if(full_name) version = 2018 ;
208  }
209 
210 
211  if(full_name==0) continue ;
212 
213  int size = caller->sfs->fileSize(full_name) ; // this is bytes
214 
215  LOG(DBG,"S%d:%d: Got size %d",1,r,size) ;
216  if(size <= 0) {
217  LOG(NOTE,"%s: %s: not found in this event",name,str) ;
218  return 0 ;
219  }
220 
221  got_any = 1 ;
222 
223  char *st = (char *) raw->request(size) ;
224 
225  caller->sfs->read(full_name, st, size) ;
226 
227  LOG(DBG,"sfs read succeeded") ;
228 
229  raw->finalize(size,s,r,0) ;
230 
231  full_name = 0 ;
232  }
233  }
234 
235  raw->rewind() ;
236 
237  if(got_any) {
238  return raw ;
239  }
240  else return 0 ;
241 
242 }
243 
244 daq_dta *daq_fcs::handle_zs()
245 {
246  char str[128] ;
247  u_int min_rdo = 1 ;
248  u_int max_rdo = 8 ;
249  char *full_name ;
250  int got_any = 0 ;
251 // int bytes = 0 ;
252 // char *st ;
253 
254  // bring in the bacon from the SFS file....
255  assert(caller) ;
256 
257  memset(&fcs_meta,0,sizeof(fcs_meta)) ;
258  fcs_meta.version = 0x00000001 ;
259  zs->meta = (void *)&fcs_meta ;
260 
261  // first check the global zs (new in May 2019)
262 
263  zs->create(8*1024,"fcs_zs",rts_id,DAQ_DTA_STRUCT(daq_adc_tb)) ;
264 
265  for(int s=1;s<=12;s++) {
266 
267  sprintf(str,"%s/sec%02d/zs",sfs_name,s) ;
268 
269  full_name = caller->get_sfs_name(str) ;
270 
271 
272 
273  if(full_name) {
274  char *st, *m_st ;
275  u_short *zs_start ;
276 
277  int sec, rdo ;
278  int bytes ;
279 
280 // zs->create(8*1024,"fcs_zs",rts_id,DAQ_DTA_STRUCT(daq_adc_tb)) ;
281 
282  bytes = caller->sfs->fileSize(full_name) ; // this is bytes
283 
284  m_st = st = (char *)malloc(bytes) ;
285  caller->sfs->read(full_name,st,bytes) ;
286 
287  u_int *zs_int = (u_int *)st ;
288 
289  int bytes_data = zs_int[0] & 0x0FFFFFFF ;
290 
291  LOG(NOTE,"zs first 0x%X, bytes data %d",zs_int[0],bytes_data) ;
292 
293  zs_start = (u_short *)st ;
294  for(int j=0;j<16;j++) LOG(DBG,"%d = 0x%04X",j,zs_start[j]) ;
295 
296 
297 
298  zs_int++ ;
299 
300  bytes_data -= 4 ;
301  st += 4 ;
302 
303  while(bytes_data) {
304 
305 
306  zs_start = (u_short *)st ;
307  u_short *zs_dta = zs_start ;
308  u_int *zs_int = (u_int *)st ;
309 
310  int zs_tkn = (zs_int[0]>>16)&0xFFF ;
311  LOG(NOTE,"... board_id 0x%08X, zs_tkn %d, shorts %d, bytes_data %d",zs_int[0],zs_tkn,zs_int[1],bytes_data) ;
312 
313 
314 
315 
316 // if(zs_int[0] != r) {
317 // LOG(ERR,"Expect %d, read %d",r,zs_int[0]) ;
318 // free(st) ;
319 // continue ;
320 // }
321 
322 
323  //sec is the full board_id
324  //rdo is just the dep board
325  if(zs_int[0] & 0xF0000000) {
326  sec = zs_int[0] & 0xFFFF ;
327  rdo = zs_int[0] & 0x1F ;
328 
329  fcs_meta.sector1 = ((sec>>11)&0x1F)+1 ;
330  fcs_meta.rdo1 = ((sec>>8)&0x7)+1 ;
331  }
332  else {
333  sec = 0 ;
334  rdo = 0 ;
335  }
336 
337  //bytes_data -= 2*4 ;
338  //st += 2*4 ;
339 
340  if(zs_int[1] == 0) { // number of shorts
341  LOG(WARN,"0 shorts??") ;
342  continue ;
343  }
344 
345  st += zs_int[1]*2 ;
346  bytes_data -= zs_int[1]*2 ;
347 
348  LOG(DBG,"S%d:%d - bytes_data %d",(sec>>11)+1,((sec>>8)&0x7)+1,bytes_data) ;
349 
350 // LOG(TERR,"... 0x%X : 0x%X %d",zs_int[0],sec,rdo) ;
351 
352  u_short *zs_end = zs_dta + zs_int[1] ;
353 
354  zs_dta += 2*2 ; // to skip the 2 ints
355 
356  int ch_cou = 0 ;
357 
358  while(zs_dta < zs_end) {
359  int ch = *zs_dta++ ;
360  int seq_cou = *zs_dta++ ;
361  int a_cou = 0 ;
362 
363  LOG(DBG,"Ch %d(%d), seq %d: %d",ch,ch_cou,seq_cou,zs_end-zs_dta) ;
364  ch_cou++ ;
365 
366  if(seq_cou==0) continue ;
367 
368  got_any = 1 ;
369 
370  daq_adc_tb *a_t = (daq_adc_tb *) zs->request(8*1024) ;
371 
372  for(int i=0;i<seq_cou;i++) {
373  int t_start = *zs_dta++ ;
374  int t_cou = *zs_dta++ ;
375  int t_end = t_start + t_cou ;
376 
377  LOG(DBG,"..... t_start %d, t_cou %d",t_start,t_cou) ;
378 
379  for(int t=t_start;t<t_end;t++) {
380  u_short d = *zs_dta++ ;
381 
382  a_t[a_cou].adc = d ;
383  a_t[a_cou].tb = t ;
384  a_cou++ ;
385  }
386 
387  }
388 
389  zs->finalize(a_cou,sec,rdo,ch) ;
390  }
391 
392  //st += zs_int[1]*2 ;
393  //bytes_data -= zs_int[1]*2 ;
394 
395  }
396 
397  if(m_st) free(m_st) ;
398 
399  //zs->rewind() ;
400 
401  //if(got_any) {
402  // return zs ;
403  //}
404  //else return 0 ;
405 
406  }
407  }
408 
409  zs->rewind() ;
410  if(got_any) return zs ;
411  else return 0 ;
412 
413  // OLD stuff, pre FY19
414  for(u_int r=min_rdo;r<=max_rdo;r++) {
415  int sec, rdo ;
416 
417  sprintf(str,"%s/sec01/rdo%d/zs",sfs_name,r) ;
418  full_name = caller->get_sfs_name(str) ;
419 
420  LOG(DBG,"full %s, str %s",full_name,str) ;
421 
422  if(full_name==0) continue ;
423 
424  int bytes = caller->sfs->fileSize(full_name) ; // this is bytes
425 
426  LOG(DBG,"S%d:%d: Got size %d",1,r,bytes) ;
427  if(bytes <= 0) {
428  LOG(NOTE,"%s: %s: not found in this event",name,str) ;
429  continue ;
430  }
431 
432 
433 
434  char *st = (char *)malloc(bytes) ;
435 
436  caller->sfs->read(full_name, st, bytes) ;
437 
438  LOG(DBG,"sfs read succeeded") ;
439 
440  u_short *zs_start = (u_short *)st ;
441  u_short *zs_dta = zs_start ;
442  u_int *zs_int = (u_int *)st ;
443 
444  LOG(NOTE,"... board_id 0x%08X, shorts %d",zs_int[0],zs_int[1]) ;
445 
446 // if(zs_int[0] != r) {
447 // LOG(ERR,"Expect %d, read %d",r,zs_int[0]) ;
448 // free(st) ;
449 // continue ;
450 // }
451 
452 
453  //sec is the full board_id
454  //rdo is just the dep board
455  if(zs_int[0] & 0xF0000000) {
456  sec = zs_int[0] & 0xFFFF ;
457  rdo = zs_int[0] & 0x1F ;
458  }
459  else { // early 2019, I'll do it by hand
460  if(r==1) {
461  sec = ((1-1)<<11)|((r-1)<<8)|(2<<6)|(1<<5)|0 ;
462  }
463  else {
464  sec = ((1-1)<<11)|((r-1)<<8)|(0<<6)|(1<<5)|0 ;
465  }
466 
467  rdo = 0 ;
468  }
469 
470  if(zs_int[1] == 0) { // number of shorts
471  free(st) ;
472  continue ;
473  }
474 
475 
476 // LOG(TERR,"... 0x%X : 0x%X %d",zs_int[0],sec,rdo) ;
477 
478  u_short *zs_end = zs_dta + zs_int[1] ;
479 
480  zs_dta += 2*2 ;
481 
482  while(zs_dta < zs_end) {
483  int ch = *zs_dta++ ;
484  int seq_cou = *zs_dta++ ;
485  int a_cou = 0 ;
486 
487  LOG(DBG,"Ch %d, seq %d: %d",ch,seq_cou,zs_end-zs_dta) ;
488 
489  if(seq_cou==0) continue ;
490 
491  got_any = 1 ;
492 
493  daq_adc_tb *a_t = (daq_adc_tb *) zs->request(8*1024) ;
494 
495  for(int i=0;i<seq_cou;i++) {
496  int t_start = *zs_dta++ ;
497  int t_cou = *zs_dta++ ;
498  int t_end = t_start + t_cou ;
499 
500  LOG(DBG,"..... t_start %d, t_cou %d",t_start,t_cou) ;
501 
502  for(int t=t_start;t<t_end;t++) {
503  u_short d = *zs_dta++ ;
504 
505  a_t[a_cou].adc = d ;
506  a_t[a_cou].tb = t ;
507  a_cou++ ;
508  }
509 
510  }
511 
512  zs->finalize(a_cou,sec,rdo,ch) ;
513  }
514 
515  free(st) ;
516  }
517 
518  zs->rewind() ;
519 
520  if(got_any) {
521  return zs ;
522  }
523  else return 0 ;
524 
525 }
526 
527 daq_dta *daq_fcs::handle_adc()
528 {
529  daq_dta *dta ;
530 
531  // bring in the bacon from the SFS file....
532  assert(caller) ;
533 
534  dta = handle_raw() ;
535 
536  if(dta==0) return 0 ;
537 
538  adc->create(1000,"adc",rts_id,DAQ_DTA_STRUCT(u_short)) ;
539 
540 
541  while(dta && dta->iterate()) {
542  u_short *ptr = (u_short *) dta->Void ;
543 
544  //LOG(TERR,"Hello %d",dta->ncontent) ;
545 
546  fcs_data_c fcs_c ;
547 
548 
549  fcs_c.sector = dta->sec ;
550  fcs_c.set_rdo(dta->rdo) ;
551 
552  fcs_c.start(ptr,dta->ncontent/2) ;
553 
554  //LOG(TERR,"Hello again %d %d %d = %d %d",fcs_c.sector,fcs_c.rdo, fcs_c.ch,dta->sec,dta->rdo) ;
555 
556  while(fcs_c.event()) {
557 
558  //LOG(TERR,"Request %d",fcs_c.tb_cou) ;
559 
560  u_short *at = (u_short *)adc->request(fcs_c.tb_cou) ;
561 
562  for(int i=0;i<fcs_c.tb_cou;i++) {
563  at[i] = fcs_c.adc[i] ;
564  }
565 
566  //if(fcs_c.first_rhic_strobe_tick!=0 || fcs_c.trigger_tick != 142) {
567  // LOG(WARN,"RHIC %d, Trg %d",fcs_c.first_rhic_strobe_tick,fcs_c.trigger_tick) ;
568  //}
569 
570 
571  int sec = fcs_c.hdr_board_id ; // all sorts of things
572  int rdo = fcs_c.hdr_board_id & 0x1F ; // dep id
573 
574  adc->finalize(fcs_c.tb_cou, sec, rdo, fcs_c.ch) ;
575  }
576  }
577 
578  adc->rewind() ;
579 
580  return adc ;
581 
582 }
583 
584 // knows how to get the token out of an event...
585 int daq_fcs::get_token(char *addr, int words)
586 {
587  LOG(ERR,"get_token") ;
588 
589  int cou ;
590  struct daq_trg_word trg[128] ;
591 
592  cou = get_l2(addr,words,trg,1) ;
593 
594  if(cou==0) return -1000 ; // special marker...
595  if(trg[0].t==0) return -ENOSYS ;
596 
597  return trg[0].t ;
598 }
599 
600 static inline u_int sw16(u_int d)
601 {
602  u_int tmp = d ;
603 
604  d >>= 16 ;
605 
606  d |= (tmp & 0xFFFF)<<16 ;
607 
608  return d ;
609 }
610 
611 
612 
613 // knows how to get a/the L2 command out of the event...
614 int daq_fcs::get_l2(char *addr, int words, struct daq_trg_word *trg, int rdo)
615 {
616  int t_cou = 0 ;
617  u_int *d = (u_int *)addr ;
618  u_short *d16 ;
619  u_int trg_word ;
620  u_int hdr ;
621 
622  int trg_cmd, daq_cmd ;
623  int t_hi, t_mid, t_lo ;
624 
625 // LOG(TERR,"get_l2: %p %d %p %d",addr,words,trg,rdo) ;
626  if(addr==0) return 0 ;
627 
628 // LOG(WARN,"get_l2") ;
629 // for(int i=0;i<(words+16);i++) {
630 // LOG(TERR,"... %d/%d = 0x%08X",i,words,d[i]) ;
631 // }
632 
633  d += 4 ; // skip GTP header
634 
635  d16 = (u_short *)d ;
636 
637  if(d[0] != 0xCCCC001C) {
638  LOG(ERR,"%d: comma word 0x%08X bad, words %d",rdo,d[0],words) ;
639  goto err_end ;
640  }
641 
642  hdr = sw16(d[1]) >> 16 ;
643 
644 
645 
646 // LOG(TERR,"%d: hdr 0x%X: 0x%X 0x%X 0x%X",rdo,hdr,d[3],d[4],d[5]) ;
647 
648  switch(hdr) {
649  case 0x9801 :
650  trg_word = (d16[4]<<16) | d16[3] ;
651  break ;
652  case 0x9802 :
653  case 0x9803 :
654  trg_word = (d16[5]<<16) | d16[4] ;
655  break ;
656  case 0x9800 :
657  trg_word = sw16(d[3]) ; // trigger
658  break ;
659  case 0x9810 :
660  trg_word = d[2] ;
661 // LOG(ERR,"%d: streaming trg_word 0x%08X",rdo,trg_word) ;
662  break ;
663 
664  default :
665  LOG(ERR,"%d: unexpected HDR 0x%04X",rdo,hdr) ;
666  goto err_end ;
667  }
668 
669 
670  trg_cmd = trg_word & 0xF ;
671  daq_cmd = (trg_word >> 4) & 0xF ;
672  t_hi = (trg_word >> 8) & 0xF ;
673  t_mid = (trg_word >> 12) & 0xF ;
674  t_lo = (trg_word >> 16) & 0xF ;
675 
676  t_lo |= (t_hi<<8) | (t_mid << 4) ;
677 
678 // LOG(TERR,"%d: words %d: trg_word 0x%08X: trg_cmd 0x%X, daq_cmd 0x%X, token %d",rdo,words,
679 // trg_word,trg_cmd,daq_cmd,t_lo) ;
680 
681  if(trg_cmd==0) {
682  //LOG(WARN,"trg_cmd=0 in event type 0x%04X",hdr) ;
683  // Monitoring event...
684  goto err_end ;
685  }
686  else {
687  if(t_lo==0) {
688  LOG(ERR,"%d: token-0 in event HDR 0x%04X: trg_cmd 0x%05X: trg %d, daq %d",rdo,hdr,trg_word,trg_cmd,daq_cmd) ;
689 
690  if(trg_cmd==5) { // allowed for self-triggered!
691  t_lo = 4095 ; // use this particular token!
692  }
693  else {
694  //LOG(ERR,"%d: token-0 in event HDR 0x%04X: trg_cmd 0x%05X: trg %d, daq %d",rdo,hdr,trg_word,trg_cmd,daq_cmd) ;
695 
696  u_short *d16 = (u_short *)d ;
697  for(int i=0;i<16;i++) {
698  LOG(TERR,"... %d = 0x%04X",i,d16[i]) ;
699  }
700 
701  goto err_end ;
702  }
703  }
704  else {
705  switch(trg_cmd) {
706  case 4 :
707  //case 5 :
708  case 6 : // local trigger
709  case 7 : // first event
710  case 10 : // pulser
711  case 12 : // slice
712  break ;
713  default :
714  LOG(WARN,"%d: unusual trg_cmd=0x%X in event 0x%04X: T %d, daq 0x%X",rdo,trg_cmd,hdr,t_lo,daq_cmd) ;
715  break ;
716  }
717  }
718  }
719 
720 #if 0
721  u_short *d16 = (u_short *)d ;
722  for(int i=0;i<16;i++) {
723  LOG(TERR,"... %d = 0x%04X",i,d16[i]) ;
724  }
725 #endif
726 
727 
728  trg[t_cou].t = t_lo ;
729  trg[t_cou].trg = trg_cmd ;
730  trg[t_cou].daq = daq_cmd ;
731  trg[t_cou].rhic = 0 ;
732  trg[t_cou].rhic_delta = 0 ;
733  t_cou++ ;
734 
735 
736  return t_cou ;
737 
738  err_end:;
739 
740  trg[0].t = 4096 ;
741  trg[0].trg = 0 ;
742  trg[0].daq = 0 ;
743 
744  return 1 ;
745 
746 }
747 
748 
749