6 #include <rtsSystems.h>
9 #include <SFS/sfs_index.h>
10 #include <DAQ_READER/daqReader.h>
11 #include <DAQ_READER/daq_dta.h>
20 #include <DAQ1000/ddl_struct.h>
23 #include "stgc_data_c.h"
25 #include <DAQ_TPX/tpxCore.h>
40 daq_det_factory::det_factories[STGC_ID] = this ;
52 const char *daq_stgc::help_string =
"\
54 ****** STGC Help ******* \n\
60 raw (sector,rdo); returns (char *) of start of DDL data\n\
65 void daq_stgc::help()
const
67 printf(
"%s\n%s\n",GetCVS(),help_string) ;
75 name = rts2name(rts_id) ;
79 if(caller) caller->insert(
this, rts_id) ;
88 LOG(DBG,
"%s: constructor: caller %p",name, caller) ;
101 daq_stgc::~daq_stgc()
103 LOG(DBG,
"%s: DEstructor",name) ;
112 LOG(DBG,
"%s: DEstructor done",name) ;
121 daq_dta *daq_stgc::get(
const char *in_bank,
int sec,
int row,
int pad,
void *p1,
void *p2)
134 LOG(NOTE,
"%s: looking for bank %s",name,bank) ;
136 if(!present)
return 0 ;
139 if(strcasecmp(bank,
"raw")==0) {
140 return handle_raw(sec,row) ;
142 else if(strcasecmp(bank,
"altro")==0) {
143 return handle_altro(sec,row) ;
145 else if(strcasecmp(bank,
"vmm")==0) {
146 return handle_vmm(sec) ;
148 else if(strcasecmp(bank,
"vmmraw")==0) {
149 return handle_vmmraw(sec) ;
152 LOG(ERR,
"%s: unknown bank type \"%s\"",name,bank) ;
159 daq_dta *daq_stgc::handle_altro(
int sec,
int rdo)
162 int min_sec, max_sec ;
163 int min_rdo, max_rdo ;
171 else if((sec<1) || (sec>MAX_SEC))
return 0 ;
181 else if((rdo<0) || (rdo>6))
return 0 ;
183 min_rdo = max_rdo = rdo ;
189 for(
int s=min_sec;s<=max_sec;s++) {
190 for(
int r=min_rdo;r<=max_rdo;r++) {
195 LOG(DBG,
"handle_altro: secs %d to %d, rdos %d to %d",min_sec,max_sec,min_rdo,max_rdo) ;
204 altro->create(guess_bytes,(
char *)
"adc",rts_id,DAQ_DTA_STRUCT(
daq_adc_tb)) ;
207 for(
int s=min_sec;s<=max_sec;s++) {
208 for(
int r=min_rdo;r<=max_rdo;r++) {
217 LOG(NOTE,
"Calling handle_raw for %d:%d",s,r) ;
218 rdo_dta = handle_raw(s, r) ;
222 LOG(WARN,
"rdo_dta NULL?") ;
226 int ret = rdo_dta->iterate() ;
231 LOG(DBG,
"Called handle_raw for %d:%d, iterate %d, returned %d objs",s,r,ret,rdo_dta->ncontent) ;
232 if(rdo_dta->ncontent == 0) continue ;
234 rdo_ptr = (
char *)rdo_dta->Byte ;
235 rdo_words = rdo_dta->ncontent / 4 ;
238 int token = tpx_get_start(rdo_ptr, rdo_words, &rdo, 0) ;
241 LOG(ERR,
"horrible error, token is %d?",token) ;
246 LOG(ERR,
"RDO mismatch: in data %d, expect %d",rdo.rdo,r) ;
252 u_int *data_end = rdo.data_end ;
256 a.what = TPX_ALTRO_DO_ADC ;
261 data_end = tpx_scan_to_next(data_end, rdo.data_start, &a) ;
266 if(a.count == 0) continue ;
277 for(u_int i=0 ; i < a.count ; i++) {
278 at[i].adc = a.adc[i] ;
283 altro->finalize(a.count, s, a.id, a.ch) ;
285 }
while(data_end && (data_end > rdo.data_start)) ;
302 daq_dta *daq_stgc::handle_raw(
int sec,
int rdo)
306 int min_sec, max_sec, min_rdo, max_rdo ;
318 else if((sec<1) || (sec>MAX_SEC))
return 0 ;
320 min_sec = max_sec = sec ;
327 else if((rdo<1) || (rdo>6))
return 0 ;
329 min_rdo = max_rdo = rdo ;
339 for(
int s=min_sec;s<=max_sec;s++) {
340 for(
int r=min_rdo;r<=max_rdo;r++) {
342 sprintf(str,
"%s/sec%02d/rb%02d/adc",sfs_name, s, r) ;
344 LOG(NOTE,
"%s: trying sfs on \"%s\"",name,str) ;
346 char *full_name = caller->get_sfs_name(str) ;
347 if(full_name == 0) continue ;
349 int size = caller->sfs->fileSize(full_name) ;
351 LOG(DBG,
"%s: sector %d, rdo %d : raw size %d",name,s,r,size) ;
355 LOG(DBG,
"%s: %s: not found in this event",name,str) ;
362 obj[o_cou].bytes = size ;
368 LOG(DBG,
"%s: %s: reading in \"%s\": bytes %d",name,str,
"raw", size) ;
373 raw->create(tot_bytes,(
char *)
"raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
376 for(
int i=0;i<o_cou;i++) {
378 sprintf(str,
"%s/sec%02d/rb%02d/adc",sfs_name,obj[i].sec, obj[i].
rb) ;
379 char *full_name = caller->get_sfs_name(str) ;
380 if(!full_name) continue ;
382 LOG(NOTE,
"%s: request %d bytes",name,obj[i].bytes) ;
384 char *mem = (
char *) raw->request(obj[i].bytes) ;
386 int ret = caller->sfs->read(full_name, mem, obj[i].bytes) ;
388 if(ret != (
int)obj[i].bytes) {
389 LOG(ERR,
"%s: %s: read failed, expect %d, got %d [%s]",name,str,
390 obj[i].bytes,ret,strerror(errno)) ;
393 LOG(NOTE,
"%s: %s read %d bytes",name,str,ret) ;
396 raw->finalize(obj[i].bytes, obj[i].sec, obj[i].
rb, 0) ;
400 LOG(DBG,
"Returning from raw_handler") ;
407 daq_dta *daq_stgc::handle_vmmraw(
int sec)
411 int min_sec, max_sec, min_rdo, max_rdo ;
423 else if((sec<1) || (sec>4))
return 0 ;
425 min_sec = max_sec = sec ;
438 for(
int s=min_sec;s<=max_sec;s++) {
439 for(
int r=min_rdo;r<=max_rdo;r++) {
441 sprintf(str,
"%s/sec%02d/rdo%d/vmm_raw",sfs_name, s, r) ;
443 LOG(DBG,
"%s: trying sfs on \"%s\"",name,str) ;
445 char *full_name = caller->get_sfs_name(str) ;
446 if(full_name == 0) continue ;
448 int size = caller->sfs->fileSize(full_name) ;
450 LOG(NOTE,
"%s: sector %d, rdo %d : raw size %d",name,s,r,size) ;
454 LOG(DBG,
"%s: %s: not found in this event",name,str) ;
461 obj[o_cou].bytes = size ;
467 LOG(DBG,
"%s: %s: reading in \"%s\": bytes %d",name,str,
"raw", size) ;
472 vmmraw->create(tot_bytes,(
char *)
"vmmraw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
475 for(
int i=0;i<o_cou;i++) {
477 sprintf(str,
"%s/sec%02d/rdo%d/vmm_raw",sfs_name,obj[i].sec, obj[i].
rb) ;
478 char *full_name = caller->get_sfs_name(str) ;
479 if(!full_name) continue ;
481 LOG(NOTE,
"%s: request %d bytes",name,obj[i].bytes) ;
483 char *mem = (
char *) vmmraw->request(obj[i].bytes) ;
485 int ret = caller->sfs->read(full_name, mem, obj[i].bytes) ;
487 if(ret != (
int)obj[i].bytes) {
488 LOG(ERR,
"%s: %s: read failed, expect %d, got %d [%s]",name,str,
489 obj[i].bytes,ret,strerror(errno)) ;
492 LOG(NOTE,
"%s: %s read %d bytes",name,str,ret) ;
495 vmmraw->finalize(obj[i].bytes, obj[i].sec, obj[i].
rb, 0) ;
499 LOG(DBG,
"Returning from raw_handler") ;
505 daq_dta *daq_stgc::handle_vmm(
int sec)
509 int min_sec, max_sec, min_rdo, max_rdo ;
519 LOG(DBG,
"sizeof stgc_vmm_t %d",
sizeof(
stgc_vmm_t)) ;
527 min_sec = max_sec = sec ;
534 stgc_d->xing_min = xing_min ;
535 stgc_d->xing_max = xing_max ;
537 LOG(NOTE,
"xing_min %d, xing_max %d",stgc_d->xing_min,stgc_d->xing_max) ;
543 for(
int s=min_sec;s<=max_sec;s++) {
544 for(
int r=min_rdo;r<=max_rdo;r++) {
546 sprintf(str,
"%s/sec%02d/rdo%d/vmm_raw",sfs_name, s, r) ;
548 LOG(NOTE,
"%s: trying sfs on \"%s\"",name,str) ;
550 char *full_name = caller->get_sfs_name(str) ;
551 if(full_name == 0) continue ;
553 int size = caller->sfs->fileSize(full_name) ;
555 LOG(DBG,
"%s: sector %d, rdo %d : raw size %d",name,s,r,size) ;
559 LOG(DBG,
"%s: %s: not found in this event",name,str) ;
566 obj[o_cou].bytes = size ;
578 for(
int i=0;i<o_cou;i++) {
580 int vmm_max = obj[i].bytes/8 ;
582 sprintf(str,
"%s/sec%02d/rdo%d/vmm_raw",sfs_name,obj[i].sec, obj[i].
rb) ;
583 char *full_name = caller->get_sfs_name(str) ;
584 if(!full_name) continue ;
586 LOG(NOTE,
"%s: request: %d bytes raw, vmm_hits %d",name,obj[i].bytes,vmm_max) ;
590 char *mem = (
char *)malloc(obj[i].bytes) ;
592 int ret = caller->sfs->read(full_name, mem, obj[i].bytes) ;
594 if(ret != (
int)obj[i].bytes) {
595 LOG(ERR,
"%s: %s: read failed, expect %d, got %d [%s]",name,str,
596 obj[i].bytes,ret,strerror(errno)) ;
599 LOG(NOTE,
"%s: %s read %d bytes",name,str,ret) ;
604 stgc_d->sector1 = obj[i].sec ;
605 stgc_d->rdo1 = obj[i].rb ;
606 stgc_d->start((u_short *)mem,obj[i].bytes/2) ;
607 while(stgc_d->event()) {
608 if(stgc_d->vmm.feb_vmm==0 && stgc_d->vmm.adc==0 && stgc_d->vmm.bcid==0 && stgc_d->vmm.ch==0) {
615 if(hits >= vmm_max) {
616 LOG(NOTE,
"ERROR: S%d:%d -- too many hits %d/%d",obj[i].sec,obj[i].
rb,hits,vmm_max) ;
620 vm[hits] = stgc_d->vmm ;
626 if(all_hits>=vmm_max) {
627 LOG(ERR,
"S%d:%d -- too many hits %d/%d",obj[i].sec,obj[i].
rb,all_hits,vmm_max) ;
635 for(
int j=0;j<32;j++) {
636 LOG(TERR,
"%d = 0x%04X",j,d[j]) ;
640 vmm->finalize(hits, obj[i].sec, obj[i].
rb, 0) ;
645 LOG(DBG,
"Returning from vmm_handler") ;
652 int daq_stgc::get_token(
char *addr,
int words)
656 get_l2(addr, words, trgs, 1) ;
661 LOG(ERR,
"Token 0 not allowed but I will try to use the other triggers...") ;
670 int daq_stgc::get_l2_vmm(
char *addr,
int words,
struct daq_trg_word *trgs,
int do_log)
672 int t_hi, t_mid, t_lo ;
675 u_short *d = (u_short *)addr ;
678 int shorts = words*2 ;
682 u_short type = d[8] ;
687 u_short t, t_lo, t_mid, t_hi ;
691 t_hi = (d[4]>>4)&0xF ;
692 t_mid = (d[4]>>8)&0xF ;
693 t_lo = (d[4]>>12)&0xF ;
695 t = (t_hi<<8)|(t_mid<<4)|t_lo ;
697 if((trg_cmd>=4)&&(trg_cmd<=12)&&(t!=0)) {
699 trgs[0].trg = trg_cmd ;
700 trgs[0].daq = daq_cmd ;
703 LOG(ERR,
"get_l2_vmm: shorts %d: type 0x%04X: T %d, trg %d, daq %d [0x%04X]",shorts,type,
704 trgs[0].t,trgs[0].trg,trgs[0].daq,d[4]) ;
728 int cou = shorts>32?32:shorts ;
729 for(
int i=0;i<cou;i++) {
730 LOG(TERR,
" %d/%d = 0x%04X",i,shorts,d[i]) ;
738 if(d[16] != 0x4544) {
746 trgs[0].trg = d[11]&0xF ;
747 trgs[0].daq = d[12]&0xF ;
749 t_hi = (d[12]>>4)&0xF ;
750 t_mid = (d[12]>>8)&0xF ;
751 t_lo = (d[12]>>12)&0xF ;
753 trgs[0].t = (t_hi<<8)|(t_mid<<4)|t_lo ;
765 int daq_stgc::get_l2(
char *addr,
int words,
struct daq_trg_word *trgs,
int do_log)
768 int ret = get_l2_vmm(addr, words, trgs, do_log) ;
774 u_int collision = 0 ;
777 int ret = tpx_get_start(addr, words, &rdo, do_log) ;
779 LOG(ERR,
"get_l2: broken data!") ;
783 LOG(DBG,
"rdo %d, rdo token %d, trg cou %d",rdo.rdo,rdo.token,rdo.trg_cou) ;
786 for(u_int i=0;i<rdo.trg_cou;i++) {
787 u_int dta = rdo.trg[i].data ;
788 u_int marker = rdo.trg[i].csr >> 24 ;
789 u_int rhic = rdo.trg[i].rhic_counter ;
794 if(rdo.rdo==1 && rdo.sector==1) {
795 int delta = rhic - rdo.trg[0].rhic_counter ;
796 LOG(TERR,
"RDO %d: trg %d/%d: dta 0x%08X, CSR 0x%08X, RHIC %u, delta %u",rdo.rdo,
798 dta,rdo.trg[i].csr,rhic,
804 if((marker==0) || (marker==0xEE)) {
806 trgs[cou].t = dta & 0xFFF ;
807 trgs[cou].daq = (dta >> 12) & 0xF ;
808 trgs[cou].trg = (dta >> 16) & 0xF ;
809 trgs[cou].rhic_delta = 0 ;
810 trgs[cou].rhic = rhic ;
811 trgs[cou].reserved[0] = 0xF0000000 | (0x0FFFFFFF & dta) ;
813 switch(trgs[cou].trg) {
820 LOG(ERR,
"RDO %d: T %d: prompt: bad trg: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
826 LOG(ERR,
"RDO %d: token 0 (prompt) -- ignoring: 0x%08X",rdo.rdo,dta) ;
832 if((dta & 0x3000000) != 0x2000000) {
833 LOG(ERR,
"RDO %d: T %d: prompt: BUSY overrun: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
839 LOG(ERR,
"RDO %d: duplicate prompt trigger",rdo.rdo) ;
854 if(rdo.token != 4096) {
856 LOG(NOTE,
"No triggers in event, making it %d",rdo.token) ;
859 trgs[cou].t = rdo.token ;
862 trgs[cou].rhic_delta = 0 ;
864 trgs[cou].reserved[0] = 0 ;
870 LOG(ERR,
"RDO %d: token %d? -- too many prompt contributions!",rdo.rdo,trgs[0].t) ;
880 LOG(ERR,
"Token 0 in RDO %d: making it 4097",rdo.rdo) ;
885 for(u_int i=0;i<rdo.trg_cou;i++) {
886 u_int dta = rdo.trg[i].data ;
887 u_int marker = rdo.trg[i].csr >> 24 ;
888 u_int rhic = rdo.trg[i].rhic_counter ;
893 trgs[cou].t = dta & 0xFFF ;
894 trgs[cou].daq = (dta >> 12) & 0xF ;
895 trgs[cou].trg = (dta >> 16) & 0xF ;
896 trgs[cou].rhic = rhic ;
897 trgs[cou].reserved[0] = 0x20000000 | (0x0FFFFFFF & dta) ;
899 switch(trgs[cou].trg) {
907 if((trgs[cou].t == 0x345) && (trgs[cou].daq == 2)) {
910 if((trgs[cou].t == 1001) && (trgs[cou].daq == 3)) {
921 if((dta & 0x03A00000) == 0x00800000) {
926 if((dta & 0x3000000) != 0x2000000) {
927 if(trgs[0].trg == 9) {
928 LOG(NOTE,
"RDO %d: T %d: FIFO: BUSY overrun: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
931 LOG(ERR,
"RDO %d: T %d: FIFO: BUSY overrun: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
937 switch(trgs[cou].trg) {
944 if(trgs[0].trg == 9) {
945 LOG(NOTE,
"RDO %d: T %d: FIFO: bad trg_cmd: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
948 LOG(ERR,
"RDO %d: T %d: FIFO: bad trg_cmd: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
954 trgs[cou].reserved[0] = 0xA0000000 | (0x0FFFFFFF & dta) ;
965 if(rhic >= collision) {
966 trgs[cou].rhic_delta = rhic - collision ;
969 trgs[cou].rhic_delta = -(collision - rhic) ;
972 if(trgs[cou].rhic_delta == 1) {
973 LOG(NOTE,
"RDO %d: T %d: FIFO: delta == 1: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
974 if(trgs[0].trg == 9) {
979 if((trgs[cou].t == 0)) {
980 LOG(ERR,
"RDO %d: token 0 in L2 contribution 0x%08X -- skipping",rdo.rdo,dta) ;
991 LOG(ERR,
" RDO %d: words %d",rdo.rdo,words) ;
992 for(u_int i=0;i<rdo.trg_cou;i++) {
993 LOG(ERR,
" RDO %d: T %4d: %d/%d: data 0x%08X, CSR 0x%08X, RHIC %u",rdo.rdo, rdo.token, i, rdo.trg_cou, rdo.trg[i].data, rdo.trg[i].csr, rdo.trg[i].rhic_counter) ;