StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daq_itpc.cxx
1 #include <assert.h>
2 #include <sys/types.h>
3 #include <errno.h>
4 
5 #include <rtsLog.h>
6 #include <rtsSystems.h>
7 #include <daqFormats.h>
8 
9 #include <SFS/sfs_index.h>
10 
11 #include <DAQ_READER/daqReader.h>
12 #include <DAQ_READER/daq_dta.h>
13 
14 //#include <DAQ_TPX/tpxFCF.h>
15 
16 #include "daq_itpc.h"
17 #include "itpcCore.h"
18 #include "itpcInterpreter.h"
19 #include "itpcPed.h"
20 #include "itpcFCF.h"
21 
22 #include <DAQ_TPC23/itpc23.h>
23 
24 const char *daq_itpc::help_string = "\
25 \n\
26 ITPC Help: \n\
27 Supported Banks: \n\
28  raw returns=ptr of start of DDL data; c1=sector[1..1]; c2=rdo[1..4]; \n\
29 \n\
30 \n\
31 " ;
32 
34 {
35 public:
37  LOG(DBG,"%s",__PRETTY_FUNCTION__) ;
38  daq_det_factory::det_factories[ITPC_ID] = this ;
39  }
40 
41  daq_det *create() {
42  LOG(DBG,"%s",__PRETTY_FUNCTION__) ;
43  return new daq_itpc ;
44  }
45 } ;
46 
47 static daq_det_itpc_factory itpc_factory ;
48 
49 int daq_itpc::no_sw16 = 0 ;
50 
51 
52 daq_itpc::daq_itpc(daqReader *rts_caller)
53 {
54 // LOG(TERR,"%s",__PRETTY_FUNCTION__) ;
55 
56  rts_id = ITPC_ID ;
57  name = rts2name(rts_id) ;
58  sfs_name = "itpc" ;
59  caller = rts_caller ;
60 
61  if(caller) caller->insert(this, rts_id) ;
62 
63  ifee_fy17_raw = new daq_dta ;
64  ifee_fy17_sampa = new daq_dta ;
65 
66  raw = new daq_dta ;
67  sampa = new daq_dta ;
68  ped = new daq_dta ;
69  cld = new daq_dta ;
70 
71  adc_sim = new daq_dta;
72  cld_sim = new daq_dta ;
73  gain = new daq_dta ;
74 
75  it = new itpcInterpreter ;
76 
77  it23 = 0 ; // assume we won't use it
78  online = 0 ;
79  mode = 0 ;
80 
81 
82  memset(fcf,0,sizeof(fcf)) ;
83  fcf_det_type = 1 ; // ITPC
84  fcf_det_orient = 1 ; // normal
85 
86 
87 
88  LOG(DBG,"%s: constructor: caller %p",name,rts_caller) ;
89  return ;
90 }
91 
92 daq_itpc::~daq_itpc()
93 {
94 // LOG(TERR,"%s",__PRETTY_FUNCTION__) ;
95 
96  delete ifee_fy17_raw ;
97  delete ifee_fy17_sampa ;
98 
99  delete raw ;
100  delete sampa ;
101  delete ped ;
102  delete cld ;
103 
104  delete adc_sim ;
105  delete gain ;
106  delete cld_sim ;
107 
108  delete it ;
109 
110  for(int i=0;i<25;i++) {
111  if(fcf[i]) delete fcf[i] ;
112  }
113 
114  return ;
115 }
116 
117 void daq_itpc::setup_fcf(int det, int orient)
118 {
119  fcf_det_type = det ;
120  fcf_det_orient = orient ;
121 }
122 
123 void daq_itpc::run_stop()
124 {
125  for(int s=1;s<=24;s++) {
126  if(fcf[s]) fcf[s]->run_stop() ;
127  }
128 
129 }
130 
131 daq_dta *daq_itpc::put(const char *in_bank, int sec, int row, int pad, void *p1, void *p2)
132 {
133 
134  assert(in_bank) ;
135 
136  if(strcasecmp(in_bank,"adc_sim")==0) {
137  adc_sim->create(32*1024,(char *)"adc_sim",rts_id,DAQ_DTA_STRUCT(daq_sim_adc_tb)) ;
138  return adc_sim ;
139  }
140  else if(strcasecmp(in_bank,"gain")==0) {
141  gain->create(32*1024,(char *)"gain",rts_id,DAQ_DTA_STRUCT(daq_det_gain)) ;
142  return gain ;
143  }
144 
145  LOG(ERR,"%s: unknown bank type \"%s\"",name,in_bank) ;
146  return 0 ;
147 
148 }
149 
150 daq_dta *daq_itpc::get(const char *bank, int sec, int row, int pad, void *p1, void *p2)
151 {
152  Make() ;
153 
154  if(present==0) return 0 ;
155 
156  LOG(DBG,"%s: looking for bank %s",name,bank) ;
157 
158  if(strcmp(bank,"*")==0) bank = "raw" ;
159 
160 
161  if(strcasecmp(bank,"raw")==0) {
162  return handle_raw(sec,row) ; // actually sec, rdo; r1 is the number of bytes
163  }
164  else if(strcasecmp(bank,"adc")==0) {
165  return handle_sampa(sec,row,1) ; // actually sec, rdo; r1 is the number of bytes
166  }
167  else if(strcasecmp(bank,"sampa")==0) {
168  return handle_sampa(sec,row,0) ; // actually sec, rdo; r1 is the number of bytes
169  }
170  else if(strcasecmp(bank,"pedrms")==0) {
171  return handle_ped(sec,row) ; // actually sec, rdo; r1 is the number of bytes
172  }
173  else if(strcasecmp(bank,"cld")==0) {
174  return handle_cld(sec) ; // actually sec, rdo; r1 is the number of bytes
175  }
176  else if(strcasecmp(bank,"cld_sim")==0) {
177  return handle_cld_sim(sec) ; // actually sec, rdo; r1 is the number of bytes
178  }
179  else if(strcasecmp(bank,"adc_sim")==0) {
180  if(adc_sim->is_empty()) return 0 ;
181  adc_sim->rewind() ;
182  return adc_sim ;
183  }
184  else if(strcasecmp(bank,"gain")==0) {
185  if(gain->is_empty()) return 0 ;
186  gain->rewind() ;
187  return gain ;
188  }
189 
190  // ********************************************
191  // ************ these are FY17 banks!
192  // ********************************************
193  else if(strcasecmp(bank,"ifee_fy17_raw")==0) {
194  return handle_ifee_fy17_raw() ; // actually sec, rdo; r1 is the number of bytes
195  }
196  else if(strcasecmp(bank,"ifee_fy17_sampa")==0) {
197  return handle_ifee_fy17_sampa() ; // actually sec, rdo; r1 is the number of bytes
198  }
199  else {
200  LOG(ERR,"%s: unknown bank type \"%s\"",name,bank) ;
201  }
202 
203  return 0 ;
204 }
205 
206 daq_dta *daq_itpc::handle_cld_sim(int sec)
207 {
208  int min_sec, max_sec ;
209  u_char evt_started[25] ;
210 
211  memset(evt_started,0,sizeof(evt_started)) ;
212 
213  if(sec <= 0) {
214  min_sec = 1 ;
215  max_sec = 24 ;
216  }
217  else {
218  min_sec = sec ;
219  max_sec = sec ;
220  }
221 
222 
223  daq_dta *sim = get("adc_sim") ;
224 
225  //LOG(TERR,"%s: %d",__PRETTY_FUNCTION__,sec) ;
226 
227  while(sim && sim->iterate()) {
228  int s = sim->sec ; //shorthand
229  u_short sim_array[512] ;
230  u_short track_array[512] ;
231 
232  //LOG(TERR,"Here %d",s) ;
233 
234  if((min_sec<=s) && (s<=max_sec)) ;
235  else continue ;
236 
237 
238 
239  if(fcf[s]==0) {
240  fcf[s] = new itpc_fcf_c ; // implicit run start within constructor
241 
242  fcf[s]->my_id = s ;
243  fcf[s]->sector_id = s ;
244  fcf[s]->offline = 1 ;
245  fcf[s]->det_type = fcf_det_type ;
246  fcf[s]->y_is_timebin = fcf_det_orient ;
247 
248  fcf[s]->init(s,0) ; // in all cases
249 
250 
251  // I think I will load gains here...
252  daq_dta *g = get("gain") ;
253  if(g) fcf[s]->init(g) ;
254 
255 
256  }
257 
258  if(!evt_started[s]) {
259  fcf[s]->event_start() ;
260  evt_started[s] = 1 ;
261  }
262 
263 
264  daq_sim_adc_tb *sim_dta = (daq_sim_adc_tb *) sim->Void ;
265 
266  memset(sim_array,0,sizeof(sim_array)) ;
267  memset(track_array,0,sizeof(track_array)) ;
268 
269  for(u_int i=0;i<sim->ncontent;i++) {
270  // new in Jun 2022
271  if(sim_dta[i].adc==0) sim_dta[i].adc = 0xFFFF ; // transliterate 0's to 0xFFFF ;
272 
273  sim_array[sim_dta[i].tb] = sim_dta[i].adc ;
274  track_array[sim_dta[i].tb] = sim_dta[i].track_id ;
275 
276  }
277 
278  fcf[s]->do_ch_sim(sim->row,sim->pad,sim_array,track_array) ;
279  }
280 
281  //LOG(TERR,"After loading of data") ;
282 
283  cld_sim->create(1024,(char *)"cld_sim",rts_id,DAQ_DTA_STRUCT(daq_sim_cld_x)) ;
284 
285  char *buff = (char *) malloc(1024*1024) ;
286  for(int s=min_sec;s<=max_sec;s++) {
287  if(fcf[s]) ;
288  else {
289  //LOG(ERR,"What? No sector %d?",s) ;
290  continue ;
291  }
292 
293  int bytes = fcf[s]->do_fcf(buff,1024*1024) ; // returns words really
294  //LOG(TERR,"Sector %d: %d words",s,bytes) ;
295 
296  bytes *= 4 ; // and now it's bytes
297 
298 
299  u_int *end_buff = (u_int *)(buff + bytes) ;
300  u_int *p_buff = (u_int *)buff ;
301 
302  while(p_buff < end_buff) {
303  u_int row = *p_buff++ ;
304  u_int version = *p_buff++ ;
305  u_int int_cou = *p_buff++ ;
306 
307  int ints_per_cluster = (row>>16) ;
308  row &= 0xFFF ;
309 
310  int clusters = int_cou/ints_per_cluster ;
311 
312  //LOG(TERR,"clusters %d, sector %d, row %d",clusters,s,row) ;
313 
314  daq_sim_cld_x *dc = (daq_sim_cld_x *) cld_sim->request(clusters) ;
315 
316  for(int i=0;i<clusters;i++) {
317  fcf[s]->fcf_decode(p_buff,dc,version) ;
318 
319  p_buff += ints_per_cluster ;
320  dc++ ;
321  }
322 
323  cld_sim->finalize(clusters,s,row) ;
324  }
325  }
326 
327  cld_sim->rewind() ;
328 
329  free(buff) ;
330 
331  return cld_sim ;
332 }
333 
334 daq_dta *daq_itpc::handle_raw(int sec, int rdo)
335 {
336  int min_sec, max_sec, min_rdo, max_rdo ;
337 
338  // bring in the bacon from the SFS file....
339  assert(caller) ;
340 
341 
342  this->no_sw16 = 1 ;
343 
344  if((sec <= 0)||(sec>24)) {
345  min_sec = 1 ;
346  max_sec = 24 ;
347  }
348  else {
349  min_sec = max_sec = sec ;
350  }
351 
352  if((rdo <= 0)||(rdo>4)) {
353  min_rdo = 1 ;
354  max_rdo = 4 ;
355  }
356  else {
357  min_rdo = max_rdo = rdo ;
358  }
359 
360 
361  raw->create(16*1024,(char *)"raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
362 
363 
364  rdo_fmt = 0 ;
365 
366  for(int s=min_sec;s<=max_sec;s++) {
367  for(int r=min_rdo;r<=max_rdo;r++) {
368  char str[128] ;
369 
370 
371  sprintf(str,"%s/sec%02d/rdo%d/ifee",sfs_name,s,r) ;
372 
373  char *full_name = caller->get_sfs_name(str) ;
374 
375  LOG(DBG,"name [%s] -> full_name [%s]",str,full_name) ;
376 
377 
378  if(full_name == 0) {
379 
380  sprintf(str,"%s/sec%02d/rdo%d/ifee23",sfs_name,s,r) ;
381 
382  full_name = caller->get_sfs_name(str) ;
383 
384  LOG(DBG,"name [%s] -> full_name [%s]",str,full_name) ;
385 
386  if(full_name==0) continue ;
387 
388  rdo_fmt = 23 ;
389 
390  }
391 
392  LOG(DBG,"full name %p",full_name) ;
393  LOG(DBG,"full name %s",full_name) ;
394 
395  int size = caller->sfs->fileSize(full_name) ;
396 
397  LOG(NOTE,"full_name [%s] --> size %d",full_name,size) ;
398 
399  if(size <= 0) continue ; // this is really an error!
400 
401  char *mem = (char *) raw->request(size) ;
402 
403  if(mem==0) {
404  LOG(ERR,"ITPC: error in %d %d",s,r) ;
405  continue ;
406  }
407 
408  caller->sfs->read(full_name,mem,size) ;
409 
410  raw->finalize(size,s,r,0) ;
411 
412  }}
413 
414  raw->rewind() ;
415 
416  return raw ;
417 
418 }
419 
420 class sampa_c : public itpcData
421 {
422 public:
423  sampa_c() {;} ;
424  ~sampa_c() {;} ;
425 
426  daq_dta *dta ;
427  daq_adc_tb *at ;
428  int in_adc ;
429 
430 
431  void ch_start(int c) {
432  ch = c ;
433  tb_cou = 0 ;
434 
435  //LOG(TERR,"CH %d start",ch) ;
436  at = (daq_adc_tb *) dta->request(512) ;
437  }
438 
439  void accum(int sec0, int rdo0, int port0, int fee_id, int ch, int tb, int adc) {
440 
441  at[tb_cou].adc = adc ;
442  at[tb_cou].tb = tb ;
443  tb_cou++ ;
444  }
445 
446  void ch_done(int err) {
447 
448  if(in_adc) {
449  int row, pad ;
450  itpc_ifee_to_rowpad(fee_id, ch, row, pad) ;
451  dta->finalize(tb_cou, sector+1, row, pad) ;
452  }
453  else {
454  dta->finalize(tb_cou, sector+1, rdo*16+port, fee_id*256+ch) ;
455 
456  }
457 
458  if(err) {
459  LOG(ERR,"%d %d %d done, %d",sector+1,fee_id,ch,tb_cou) ;
460  }
461 
462  }
463 
464 } ;
465 
466 
467 daq_dta *daq_itpc::handle_sampa(int sec, int rdo, int in_adc)
468 {
469  class sampa_c sampa_c ;
470 
471  int min_sec, max_sec, min_rdo, max_rdo ;
472 
473 
474  // bring in the bacon from the SFS file....
475  assert(caller) ;
476 
477 
478 
479  if((sec <= 0)||(sec>24)) {
480  min_sec = 1 ;
481  max_sec = 24 ;
482  }
483  else {
484  min_sec = max_sec = sec ;
485  }
486 
487  if((rdo <= 0)||(rdo>4)) {
488  min_rdo = 1 ;
489  max_rdo = 4 ;
490  }
491  else {
492  min_rdo = max_rdo = rdo ;
493  }
494 
495  sampa_c.dta = sampa ;
496  sampa_c.in_adc = in_adc ;
497 
498  sampa->create(1024,(char *)"adc",rts_id,DAQ_DTA_STRUCT(daq_adc_tb)) ;
499 
500  it->ped_c = &sampa_c ;
501  it->run_start(0) ; // just in case
502  it->start_event(0) ; // I don't thihnk I need this?
503 
504 
505 
506  for(int s=min_sec;s<=max_sec;s++) {
507 
508  int it23_started = 0 ;
509 
510 
511  for(int r=min_rdo;r<=max_rdo;r++) {
512  daq_dta *rdo_dta ;
513  u_int *dta ;
514  u_int words ;
515  int ret ;
516 
517  rdo_dta = handle_raw(s,r) ;
518 
519  if(rdo_dta==0) continue ;
520 
521  ret = rdo_dta->iterate() ;
522  if(ret==0) continue ;
523 
524  dta = (u_int *)rdo_dta->Byte ;
525  words = rdo_dta->ncontent/4 ;
526 
527  if(words==0) continue ;
528 
529  if(rdo_fmt>22) {
530  if(it23==0) {
531  it23 = new itpc23 ;
532  it23->online = online ; // 0 ;
533  it23->run_type = 3 ; // NO CLUSTER FINDER PLEASE
534  it23->no_cld = 1 ;
535  it23->log_level = 0 ;
536  it23->mode = mode ;
537  }
538  it23->data_c = &sampa_c ;
539 
540  if(it23_started==0) {
541  it23->run_start() ;
542  it23->evt_start() ;
543  it23_started=1 ;
544  }
545  }
546 
547 
548 
549  // first 4 words are the GTP header so let's skip
550  //dta += 4 ;
551  //words -= 4 ;
552 
553  it->sector_id = s ;
554  it->rdo_id = r ;
555  sampa_c.rdo = r ;
556 
557  if(rdo_fmt>22) {
558  //LOG(WARN,"S%02d:%d: rdo_fmt %d -- scan will fail",s,r,rdo_fmt) ;
559  it23->set_rdo(s,r) ;
560  ret = it23->rdo_scan((char *)dta,words) ;
561 
562  if((it23->err || ret) && mode) {
563  LOG(ERR,"S%02d:%d: rdo_scan 0x%X, err 0x%X, words %d",s,r,ret,it23->err,words) ;
564  }
565  }
566  else {
567  ret = it->rdo_scan_top(dta,words) ;
568  }
569 
570  if(ret < 0) LOG(ERR,"rdo_scan S%d:%d, words %d, ret %d",s,r,words,ret) ;
571  else LOG(NOTE,"rdo_scan S%d:%d, words %d, ret %d",s,r,words,ret) ;
572 
573 
574  } // loop over rdos
575 
576  if(it23_started) {
577  it23->evt_stop();
578  it23->run_stop() ;
579  }
580 
581  } // loop over sectors
582 
583  sampa->rewind() ;
584 
585  return sampa ;
586 }
587 
588 
589 daq_dta *daq_itpc::handle_cld(int sec)
590 {
591 
592  int min_sec, max_sec ;
593  itpc_fcf_c *fcf ;
594 
595  // bring in the bacon from the SFS file....
596  assert(caller) ;
597 
598  if((sec <= 0)||(sec>24)) {
599  min_sec = 1 ;
600  max_sec = 24 ;
601  }
602  else {
603  min_sec = max_sec = sec ;
604  }
605 
606 
607  cld->create(1024,(char *)"cld",rts_id,DAQ_DTA_STRUCT(daq_cld)) ;
608 
609 
610  for(int s=min_sec;s<=max_sec;s++) {
611  char str[128] ;
612 
613 
614  sprintf(str,"%s/sec%02d/cld",sfs_name,s) ;
615 
616  char *full_name = caller->get_sfs_name(str) ;
617 
618  LOG(DBG,"name [%s] -> full_name [%s]",str,full_name) ;
619 
620  if(full_name == 0) continue ;
621 
622  int size = caller->sfs->fileSize(full_name) ;
623 
624  LOG(NOTE,"full_name [%s] --> size %d",full_name,size) ;
625 
626  if(size <= 0) continue ; // this is really an error!
627 
628  char *mem = (char *) malloc(size) ;
629 
630  if(mem==0) {
631  LOG(CRIT,"ITPC: error in %d %d",s) ;
632  break ;
633  }
634 
635  caller->sfs->read(full_name,mem,size) ;
636 
637  u_int *end_buff = (u_int *)(mem+size) ;
638  u_int *p_buff = (u_int *)mem ;
639 
640  while(p_buff < end_buff) {
641  // ints-per-cluster | row
642  // version
643  // count of clusters
644 
645  u_int row = *p_buff++ ;
646  u_int version = *p_buff++ ;
647  u_int int_cou = *p_buff++ ;
648 
649  int ints_per_cluster = (row>>16) ;
650  row &= 0xFFFF ;
651 
652  LOG(DBG,"ROW %d: cou %d[0x%X], version 0x%04X, ints_per_cluster %d",row,int_cou,int_cou,version,ints_per_cluster) ;
653 
654  int clusters = int_cou/ints_per_cluster ;
655 
656  daq_cld *dc = (daq_cld *)cld->request(clusters) ;
657 
658  for(int i=0;i<clusters;i++) {
659  fcf->fcf_decode(p_buff,dc,version) ;
660 
661  p_buff += ints_per_cluster ; // for now, but depends on version!
662  dc++ ;
663  }
664 
665  cld->finalize(clusters,s,row,0) ;
666  }
667 
668  free(mem) ;
669 
670  }
671 
672  cld->rewind() ;
673 
674  return cld ;
675 }
676 
677 
678 
679 daq_dta *daq_itpc::handle_ped(int sec, int rdo)
680 {
681 
682  return 0 ;
683 }
684 
685 
686 // FY17 bank
687 daq_dta *daq_itpc::handle_ifee_fy17_raw()
688 {
689  char str[128] ;
690 
691  // bring in the bacon from the SFS file....
692  assert(caller) ;
693 
694 
695  sprintf(str,"%s/sec01/rb01/fee_raw",sfs_name) ;
696  char *full_name = caller->get_sfs_name(str) ;
697 
698  LOG(DBG,"%s: trying sfs on \"%s\"",name,str) ;
699  if(full_name == 0) return 0 ;
700 
701  int size = caller->sfs->fileSize(full_name) ; // this is bytes
702 
703  LOG(DBG,"Got size %d",size) ;
704  if(size <= 0) {
705  LOG(DBG,"%s: %s: not found in this event",name,str) ;
706  return 0 ;
707  }
708 
709  ifee_fy17_raw->create(size,"itpc_raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
710  char *st = (char *) ifee_fy17_raw->request(size) ;
711 
712  caller->sfs->read(full_name, st, size) ;
713 
714  LOG(DBG,"sfs read succeeded") ;
715 
716  ifee_fy17_raw->finalize(size,1,1,0) ;
717 
718  ifee_fy17_raw->rewind() ;
719 
720  return ifee_fy17_raw ;
721 
722 }
723 
724 // FY17 bank
725 daq_dta *daq_itpc::handle_ifee_fy17_sampa()
726 {
727  char str[128] ;
728 
729  // bring in the bacon from the SFS file....
730  assert(caller) ;
731 
732 
733  sprintf(str,"%s/sec01/rb01/fee_raw",sfs_name) ;
734  char *full_name = caller->get_sfs_name(str) ;
735 
736  LOG(DBG,"%s: trying sfs on \"%s\"",name,str) ;
737  if(full_name == 0) return 0 ;
738 
739  int size = caller->sfs->fileSize(full_name) ; // this is bytes
740 
741  LOG(DBG,"Got size %d",size) ;
742  if(size <= 0) {
743  LOG(DBG,"%s: %s: not found in this event",name,str) ;
744  return 0 ;
745  }
746 
747  char *ptr = (char *) malloc(size) ;
748  LOG(DBG,"Malloc at %p",ptr) ;
749 
750  caller->sfs->read(full_name, ptr, size) ;
751 
752  LOG(DBG,"sfs read succeeded") ;
753 
754  ifee_fy17_sampa->create(1000,"adc",rts_id,DAQ_DTA_STRUCT(daq_adc_tb)) ;
755 
756  LOG(NOTE,"Starting fee_scan") ;
757 
758  // USES old FY17 class
759  itpc_data_c dta_c ;
760  dta_c.rdo_start(0,0,0) ;
761 
762  //raw data at "ptr"
763  while(dta_c.fee_scan((u_short *)ptr,size/2)) {
764  daq_adc_tb *at = (daq_adc_tb *) ifee_fy17_sampa->request(dta_c.tb_cou) ;
765  for(int i=0;i<dta_c.tb_cou;i++) {
766  at[i].adc = dta_c.at[i].adc ;
767  at[i].tb = dta_c.at[i].tb ;
768  }
769  ifee_fy17_sampa->finalize(dta_c.tb_cou,dta_c.sector,dta_c.fee_id,dta_c.fee_ch) ;
770  }
771 
772  dta_c.rdo_zap(dta_c.rdo_p) ;
773  free(ptr) ;
774 
775  ifee_fy17_sampa->rewind() ;
776 
777  return ifee_fy17_sampa ;
778 
779 }
780 
781 // knows how to get the token out of an event...
782 int daq_itpc::get_token(char *addr, int words)
783 {
784  LOG(ERR,"get_token") ;
785 
786  int cou ;
787  struct daq_trg_word trg[128] ;
788 
789  cou = get_l2(addr,words,trg,1) ;
790 
791  if(cou==0) return -1000 ; // special marker...
792  if(trg[0].t==0) return -ENOSYS ;
793 
794  return trg[0].t ;
795 }
796 
797 static inline u_int sw16(u_int d)
798 {
799  daq_itpc *p ;
800  if(p->no_sw16) return d ;
801 
802  d = ((d>>16)&0xFFFF)|(d<<16) ;
803 
804 /*
805  u_int tmp = d ;
806 
807  d >>= 16 ;
808 
809  d |= (tmp & 0xFFFF)<<16 ;
810 */
811 
812  return d ;
813 }
814 
815 // HACK for Dec 2019 Trigger Cable problem of TPC Sector 12
816 // 3rd and 4th bit were reversed on the TCD cable.
817 static u_int swap_s12(u_int dta)
818 {
819  u_int n_dta = (dta & 0xFFF00000) ;
820  for(int n=0;n<5;n++) {
821  int dd = (dta>>(n*4)) & 0xF ;
822 
823  int b4 = (dd & 0x8)?1:0 ;
824  int b3 = (dd & 0x4)?1:0 ;
825  int b2 = (dd & 0x2)?1:0 ;
826  int b1 = (dd & 0x1)?1:0 ;
827 
828  int sdd = (b3<<3)|(b4<<2)|(b2<<1)|(b1<<0) ;
829  //LOG(TERR," %d: 0x%X %d %d %d %d 0x%X",n,dd,b4,b3,b2,b1,sdd) ;
830 
831  n_dta |= (sdd<<(n*4)) ;
832  }
833 
834  return n_dta ;
835 
836 }
837 
838 
839 // knows how to get a/the L2 command out of the event...
840 int daq_itpc::get_l2(char *addr, int words, struct daq_trg_word *trg, int rdo)
841 {
842 #ifdef ITPC_FY17
843  int t_cou = 0 ;
844  static int token ;
845 
846  token++ ;
847  if(token>4095) token = 1 ;
848 
849  trg[t_cou].t = token ;
850  trg[t_cou].trg = 4 ;
851  trg[t_cou].daq = 2 ;
852  t_cou++ ;
853 
854  return t_cou ;
855 #else
856  u_int err = 0 ;
857  u_int trg_fired ;
858 // u_int v_fired ;
859  u_int trg_cou ;
860  int t_cou = 0 ;
861  u_int evt_status ;
862  int trl_ix ;
863  int trl_stop_ix ;
864  u_int ds ;
865  int rdo_version ;
866  char buff[128] ;
867  int buff_cou ;
868  u_int want_dump = 0 ;
869 // int sector ;
870 
871  u_int *d = (u_int *)addr + 4 ; // skip header
872  words -= 4 ;
873 
874 
875  //from Dec 2019
876 // sector = rdo>>4 ;
877  rdo &= 0xF ;
878 
879  // NOTE that since Dec 2017 the 16 bit words are swapped!!!
880  // eh, are you sure???
881 
882 
883  // since there are buggy TEF events without the start comma lets' search for it
884  for(int i=0;i<16;i++) {
885  LOG(DBG,"%d = 0x%08X",i,d[i]) ;
886  if(d[i] == 0xCCCC001C) {
887  d = d + i ;
888  words-- ;
889  break ;
890  }
891  }
892 
893  if(sw16(d[0]) != 0x001CCCCC) { // expect start-comma
894  LOG(ERR,"First word 0x%08X of %d words - bad",d[0],words) ;
895  err |= 1 ;
896  goto err_end ;
897  }
898 
899  //LOG(TERR," 0x%08X 0x%08X 0x%08X", sw16(d[1]),sw16(d[2]),sw16(d[words-1])) ;
900  ds = sw16(d[1]) ; // first payload word from RDO
901 
902  if(ds==0x980000004) { // Pre-Mar 2018 triggered event
903  rdo_version = 0 ;
904  }
905  else if((ds&0xFF00000F)==0x98000004) { // Apr+ 2018 triggered event
906  rdo_version = (ds >> 4) & 0xFF ;
907  }
908  else {
909  // Hm, not a triggered event!?
910  trg[0].t = 4096 ; // a "log" event
911  trg[0].daq = 0 ;
912  trg[0].trg = 0 ;
913 
914  LOG(WARN,"%d: not a triggered event 0x%08X",rdo,ds) ;
915 
916  return 1 ;
917  }
918 
919 
920  switch(rdo_version) {
921  case 0 :
922  if(sw16(d[2]) != 0x12340000) { // wrong version
923  LOG(ERR,"%d: wrong version 0x%X",rdo,d[2]) ;
924  err |= 2 ;
925  goto err_end ;
926  }
927 
928  trg_fired = sw16(d[3]) ;
929  break ;
930  case 1 :
931  trg_fired = sw16(d[2]) ;
932  break ;
933  default :
934  LOG(ERR,"%d: wrong version 0x%X",rdo,rdo_version) ;
935  err |= 2 ;
936  goto err_end ;
937  }
938 
939  //find trailer start-header, scanning from the end
940  if(words < 1) {
941  LOG(ERR,"%d: bad words %d",rdo,words) ;
942  err |= 0x100 ;
943  goto err_end ;
944  }
945 
946 
947  if(trg_fired==0) {
948  if(rdo_version==1) {
949  if(sw16(d[3])==0x980000FC) {
950  //LOG(TERR,"RDO_mon") ;
951  u_int *dd = d+4 ;
952  u_int *dd_end = d+words ;
953  buff_cou = 0 ;
954  while(dd < dd_end) {
955  u_int sd = sw16(*dd) ;
956  u_int dd_r = sd & 0xFFFFFF00 ;
957 
958  if(sd==0x580000FD) {
959  //LOG(TERR,"RDO_mon end") ;
960  if(buff_cou) {
961  buff[buff_cou++] = 0 ;
962  LOG(INFO,"RDO_asc(get_l2) %d: \"%s\"",rdo,buff) ;
963  buff_cou = 0 ;
964  }
965  break ;
966  }
967  else if(dd_r==0x9800F500) {
968  //LOG(TERR,"... 0x%08X",sd) ;
969 
970  int c = sd & 0xFF ;
971  if(c=='\n' || buff_cou==120) {
972  buff[buff_cou++] = 0 ;
973  LOG(INFO,"RDO_asc(get_l2) %d: \"%s\"",rdo,buff) ;
974  buff_cou = 0 ;
975  }
976  else {
977  buff[buff_cou++] = c ;
978  }
979  }
980  else LOG(TERR,"... 0x%08X",sd) ;
981  dd++ ;
982  }
983  }
984  }
985  }
986 
987  trl_ix = -1 ;
988  trl_stop_ix = -1 ;
989 
990  // hunt backwards for the signature but skup a bunch of last words which are just
991  // filler (sentinels) == 0
992  for(int i=(words-6);i>=0;i--) {
993  if(sw16(d[i]) == 0x98001000) {
994  trl_ix = i ;
995  break ;
996  }
997  if(sw16(d[i]) == 0x58001001) {
998  trl_stop_ix = i ;
999  }
1000  }
1001 
1002 
1003  if(trl_ix < 0) {
1004  LOG(ERR,"%d: no trailer found",rdo) ;
1005  err |= 0x20 ;
1006  goto err_end ;
1007  }
1008 
1009  trl_ix++ ; // skip 0x98001000
1010 
1011  if(rdo_version==0) {
1012  if(sw16(d[trl_ix++]) != 0xABCD0000) {
1013  LOG(ERR,"%d: wrong trailer word ABCD",rdo) ;
1014  err |= 0x40 ;
1015  goto err_end ;
1016  }
1017  }
1018 
1019  evt_status = sw16(d[trl_ix++]) ;
1020  trg_cou = sw16(d[trl_ix++]) & 0xFFFF ;
1021 
1022 
1023  if(evt_status) {
1024  int b_cou = 0 ;
1025  u_int fee_status = 0xABCDEF12 ;
1026 
1027  if(trl_stop_ix>0) {
1028  u_int s ;
1029 
1030  trl_stop_ix++ ;
1031 
1032  s = (sw16(d[trl_stop_ix++]))<<16 ;
1033  s |= sw16(d[trl_stop_ix++]) ;
1034 
1035 
1036  fee_status = s ;
1037  //LOG(TERR,"fee status 0x%08X",s) ;
1038  }
1039 
1040 
1041  for(int i=0;i<32;i++) {
1042  if(evt_status & (1<<i)) {
1043  if(i%2) {
1044  LOG(ERR," %d:#%d overwritten!!!",rdo,i/2+1) ;
1045  }
1046  else {
1047  //LOG(ERR," %d:#%d timeout",rdo,i/2+1) ;
1048  }
1049  b_cou++ ;
1050  }
1051  }
1052 
1053  LOG(ERR,"%d: evt_status 0x%08X:0x%08X, trg_fired 0x%08X, trg_cou %d, errs %d",rdo,evt_status,fee_status,trg_fired,trg_cou,b_cou) ;
1054 
1055  // We'll let it pass for now with just the error message
1056  //trg[0].t = -ETIMEDOUT ;
1057  //trg[0].daq = trg[0].trg = 0 ;
1058  //return 1 ;
1059  }
1060 
1061  //LOG(TERR,"trg_cou %d, fired %d",trg_cou,trg_fired) ;
1062 
1063  trg[0].reserved[0] = trg_fired ;
1064 
1065  if(trg_cou > 128) {
1066  LOG(ERR,"%d: bad trg cou 0x%08X",rdo,trg_cou) ;
1067  err |= 0x80 ;
1068  goto err_end ;
1069  }
1070  else if (trg_cou > 60) {
1071 
1072  }
1073 
1074  for(u_int i=0;i<trg_cou;i++) {
1075  if(trl_ix >= words) {
1076  LOG(WARN,"%d: %d/%d = 0x%08X",rdo,trl_ix,words,sw16(d[trl_ix])) ;
1077  trg[i+1].reserved[0] = 0 ;
1078  trl_ix++ ;
1079  }
1080  else {
1081  trg[i+1].reserved[0] = sw16(d[trl_ix++]) ;
1082  }
1083  }
1084 
1085  if(trg_cou > 60) {
1086  LOG(WARN,"%d: Lots of triggers %d",rdo,trg_cou) ;
1087 
1088  for(int i=0;i<5;i++) {
1089  u_int v = trg[i].reserved[0];
1090  u_int t ;
1091  u_int trg_cmd ;
1092  u_int daq_cmd ;
1093 
1094  t = ((v>>8)&0xF)<<8 ;
1095  t |= ((v>>12)&0xF)<<4 ;
1096  t |= ((v>>16)&0xF) ;
1097 
1098  trg_cmd = v & 0xF ;
1099  daq_cmd = (v>>4) & 0xF ;
1100 
1101 
1102  LOG(WARN," %d: %d/%d = 0x%08X = T %d, trg %d, daq %d",rdo,i,trg_cou,trg[i].reserved[0],t,trg_cmd,daq_cmd) ;
1103  }
1104 
1105  }
1106  // switch on logging...
1107 #if 0
1108  if(1) {
1109  for(int i=0;i<=trg_cou;i++) {
1110  u_int v = trg[i].reserved[0];
1111  u_int t ;
1112  u_int trg_cmd ;
1113  u_int daq_cmd ;
1114 
1115  t = ((v>>8)&0xF)<<8 ;
1116  t |= ((v>>12)&0xF)<<4 ;
1117  t |= ((v>>16)&0xF) ;
1118 
1119  trg_cmd = v & 0xF ;
1120  daq_cmd = (v>>4) & 0xF ;
1121 
1122 
1123  LOG(TERR,"%d: %d/%d = 0x%08X = T %d, trg %d, daq %d",rdo,i,trg_cou,trg[i].reserved[0],t,trg_cmd,daq_cmd) ;
1124  }
1125  }
1126 #endif
1127 
1128  if(trg_cou==0 && trg[0].reserved[0]==0) { // Monitoring Event
1129  trg[0].t = 4096 ;
1130  trg[0].daq = 0 ;
1131  trg[0].trg = 0 ;
1132 
1133  return 1 ;
1134  }
1135 
1136  // get prompt trigger/token
1137  t_cou = 0 ;
1138 // if(trg[0].reserved[0]==0) {
1139  if(!(trg[0].reserved[0] & 0x00200000)) { // didn't fire the FEE
1140  trg[t_cou].t = 4097 ;
1141  trg[t_cou].daq = 0 ;
1142  trg[t_cou].trg = 0 ;
1143  t_cou++ ;
1144  }
1145  else {
1146  u_int v = trg[0].reserved[0] ;
1147  u_int t ;
1148  u_int trg_cmd ;
1149  u_int daq_cmd ;
1150 
1151 // if(sector==12) { // HACK for bad TCD nibble in FY20
1152 // v = swap_s12(v) ;
1153 // }
1154 
1155  t = ((v>>8)&0xF)<<8 ;
1156  t |= ((v>>12)&0xF)<<4 ;
1157  t |= ((v>>16)&0xF) ;
1158 
1159  trg_cmd = v & 0xF ;
1160  daq_cmd = (v>>4) & 0xF ;
1161 
1162 
1163 #if 0
1164  LOG(TERR,"RDO %d: prompt T %d, trg %d, daq %d",rdo,t,trg_cmd,daq_cmd) ;
1165 #endif
1166 
1167 #if 0
1168  if(sector==12 && rdo==1 && trg_cmd != 4) {
1169  LOG(TERR,"Prompt: RDO %d: T %d, trg %d, daq %d",
1170  rdo,t,trg_cmd,daq_cmd) ;
1171  }
1172 #endif
1173 
1174  if(((v&0xFFF00000) != 0x04300000)&&((v&0xFFF00000)!=0x08300000)) { // 0x043 external trigger, 0x083 local trigger
1175  LOG(ERR,"%d: trigger odd: 0x%08X: %d %d %d",rdo,v,t,trg_cmd,daq_cmd) ;
1176  }
1177 
1178 
1179 
1180  if((t!=0) && (trg_cmd>=4) && (trg_cmd<=11)) {
1181 
1182 
1183  trg[t_cou].trg = trg_cmd ;
1184  trg[t_cou].daq = daq_cmd ;
1185  trg[t_cou].t = t ;
1186 
1187 
1188  //if(((v&0xFFF00000) != 0x04300000)&&((v&0xFFF00000)!=0x08300000)) { // 0x043 external trigger, 0x083 local trigger
1189  // LOG(ERR,"RDO %d: trigger odd: 0x%08X: %d %d %d",rdo,trg[0].reserved[0],trg[0].t,trg[0].trg,trg[0].daq) ;
1190  //}
1191 
1192  t_cou++ ;
1193  }
1194  else {
1195 
1196  LOG(ERR,"%d: odd prompt trigger T %d, trg 0x%X, daq 0x%X [0x%08X]",rdo,t,trg_cmd,daq_cmd,v) ;
1197 
1198  trg[t_cou].t = 4097 ;
1199  trg[t_cou].trg = 0 ;
1200  trg[t_cou].daq = 0 ;
1201 
1202  t_cou++ ;
1203  }
1204 
1205  }
1206 
1207 
1208 
1209  for(u_int i=1;i<(trg_cou+1);i++) {
1210  u_int v = trg[i].reserved[0] ;
1211  u_int t ;
1212 
1213  if(v==0) { // eh?
1214  want_dump |= 1 ;
1215  continue ; // eh?
1216  }
1217 
1218 
1219  // HACK for Sector 12
1220 // if(sector==12) v = swap_s12(v) ;
1221 
1222  t = ((v>>8)&0xF)<<8 ;
1223  t |= ((v>>12)&0xF)<<4 ;
1224  t |= ((v>>16)&0xF) ;
1225 
1226  trg[t_cou].trg = v & 0xF ;
1227  trg[t_cou].daq = (v>>4) & 0xF ;
1228  trg[t_cou].t = t ;
1229 
1230 
1231 #if 0
1232  // HACK for Sector 12
1233  if(sector==12 && rdo==1 && trg[t_cou].trg != 4) {
1234  LOG(TERR,"%d/%d: RDO %d: T %d, trg %d, daq %d",i,(trg_cou+1),
1235  rdo,t,trg[t_cou].trg,trg[t_cou].daq) ;
1236  }
1237 #endif
1238 
1239  if(t==0 || trg[t_cou].trg==0) {
1240  want_dump |= 2 ;
1241  continue ;
1242  }
1243 
1244 #if 0
1245  if(trg[t_cou].trg==4 || trg[t_cou].trg==8 || trg[t_cou].trg==10 || trg[t_cou].trg==14 || trg[t_cou].trg==15) ;
1246  else {
1247  if(trg[t_cou].trg==2 && trg[t_cou].daq==3 && trg[t_cou].t==10) ; // skip the special clear at run-start
1248  else {
1249  want_dump |= 0x10 ;
1250  }
1251  }
1252 
1253 #endif //LOG(TERR,"trg %d: 0x%08X: trg %d",i,v,v&0xF) ;
1254 
1255  if(trg[t_cou].trg>=4 && trg[t_cou].trg<13) { // FIFO trg
1256  //LOG(WARN,"RDO %d: %d/%d: 0x%08X: %d %d %d",rdo,i,(trg_cou+1),
1257  // v,
1258  // trg[t_cou].t,trg[t_cou].trg,trg[t_cou].daq) ;
1259 
1260 
1261  if((v&0xFFF00000) != 0x04300000) { // normal prompt trigger?
1262  LOG(NOTE,"RDO %d: %d/%d: 0x%08X: %d %d %d",rdo,i,(trg_cou+1),
1263  v,
1264  trg[t_cou].t,trg[t_cou].trg,trg[t_cou].daq) ;
1265  }
1266  else {
1267  //want_dump |= 4 ;
1268  continue ;
1269  }
1270  }
1271 
1272  if(trg[t_cou].trg==14) continue ; // I don't care about L1
1273  if(trg[t_cou].trg==2) continue ;
1274 
1275 
1276 // if(trg[t_cou].trg==2 && trg[t_cou].t==10 && trg[t_cou].daq==3) continue ;
1277 
1278 // if(trg[t_cou].trg<=2) {
1279 // want_dump |= 8 ;
1280 // //LOG(WARN,"%d: odd trg_cmd: T %d, trg %d, daq %d",rdo,trg[t_cou].t,trg[t_cou].trg,trg[t_cou].daq) ;
1281 // continue ;
1282 // }
1283 
1284 
1285  t_cou++ ;
1286  }
1287 
1288 #if 0
1289  if(want_dump) {
1290  for(u_int i=1;i<(trg_cou+1);i++) {
1291  u_int v = trg[i].reserved[0] ;
1292  u_int t ;
1293  int trg, daq ;
1294 
1295  t = ((v>>8)&0xF)<<8 ;
1296  t |= ((v>>12)&0xF)<<4 ;
1297  t |= ((v>>16)&0xF) ;
1298 
1299  trg = v & 0xF ;
1300  daq = (v>>4) & 0xF ;
1301 
1302  LOG(ERR,"%d: %d: %d %d %d [0x%08X;0x%X]",rdo,i,t,trg,daq,v,want_dump) ;
1303  }
1304  }
1305 #endif
1306 
1307  if(t_cou==0) { // wha?
1308  LOG(ERR,"%d: t_cou = 0?",rdo) ;
1309 
1310  trg[t_cou].t = 4097 ;
1311  trg[t_cou].daq = 0 ;
1312  trg[t_cou].trg = 0 ;
1313  t_cou++ ;
1314 
1315  }
1316 
1317  return t_cou ;
1318 
1319  err_end:;
1320 
1321  LOG(ERR,"%d: Error in get_l2 0x%X [words %d]",rdo,err,words) ;
1322 
1323  return 0 ;
1324 
1325 
1326 #endif
1327 
1328 
1329 
1330 }
1331 
Definition: itpc23.h:8
void version(std::ostream &os=std::cout)
print HepMC version
Definition: Version.h:27