7 #include <rtsSystems.h>
9 #include <SFS/sfs_index.h>
10 #include <DAQ_READER/daq_dta.h>
11 #include <DAQ_READER/daqReader.h>
13 #include "daq_pp2pp.h"
21 daq_det_factory::det_factories[PP_ID] = this ;
33 const char *daq_pp2pp::help_string =
"\
37 raw returns=ptr of start of DDL data; c1=pp[1..2]; \n\
38 adc returns=ptr of ADC data; c1=pp[1..2]; \n\
45 daq_pp2pp::daq_pp2pp(
daqReader *rts_caller)
48 name = rts2name(rts_id) ;
52 if(caller) caller->insert(
this, rts_id) ;
60 LOG(DBG,
"%s: constructor: caller %p, endianess %d",name,rts_caller,endianess) ;
66 daq_pp2pp::~daq_pp2pp()
68 LOG(DBG,
"%s: Destructor",name) ;
71 if(pedrms)
delete pedrms ;
72 if(adc_ped_sub)
delete adc_ped_sub ;
79 daq_dta *daq_pp2pp::get(
const char *bank,
int sec,
int row,
int pad,
void *p1,
void *p2)
82 if(!present)
return 0 ;
85 LOG(DBG,
"%s: looking for bank %s",name,bank) ;
87 if(strcasecmp(bank,
"raw")==0) {
88 return handle_raw(sec,row) ;
90 else if(strcasecmp(bank,
"adc")==0) {
91 return handle_adc(sec, row) ;
93 else if(strcasecmp(bank,
"adc_ped_sub")==0) {
94 return handle_adc_ped_sub(sec, row) ;
96 else if(strcasecmp(bank,
"pedrms")==0) {
97 return handle_pedrms(sec) ;
100 LOG(ERR,
"%s: unknown bank type \"%s\"",name,bank) ;
106 daq_dta *daq_pp2pp::handle_pedrms(
int sec)
113 int min_sec, max_sec ;
117 } obj[MAX_SEC*(MAX_RDO+1)] ;
124 else if((sec<0) || (sec>MAX_SEC))
return 0 ;
126 min_sec = max_sec = sec ;
135 for(
int s=min_sec;s<=max_sec;s++) {
137 sprintf(str,
"%s/sec%02d/pedrms",sfs_name, s) ;
138 full_name = caller->get_sfs_name(str) ;
139 if(!full_name) continue ;
141 LOG(DBG,
"%s: trying sfs on \"%s\"",name,str) ;
143 int size = caller->sfs->fileSize(str) ;
145 LOG(DBG,
"Got %d",size) ;
148 LOG(NOTE,
"%s: %s: not found in this event",name,str) ;
153 obj[o_cou].bytes = size ;
159 LOG(NOTE,
"%s: %s: reading in \"%s\": bytes %d",name,str,
"pedrms", size) ;
163 if(o_cou == 0)
return 0 ;
165 pedrms->create(2,
"pp2pp_rms",rts_id,DAQ_DTA_STRUCT(
pp2pp_pedrms_t)) ;
167 for(
int i=0;i<o_cou;i++) {
169 sprintf(str,
"%s/sec%02d/pedrms",sfs_name, obj[i].sec) ;
170 full_name = caller->get_sfs_name(str) ;
171 if(!full_name) continue ;
173 char *mem = (
char *)malloc(obj[i].bytes) ;
174 int ret = caller->sfs->read(full_name, mem, obj[i].bytes) ;
176 if(ret != (
int)obj[i].bytes) {
177 LOG(ERR,
"%s: %s: read failed, expect %d, got %d [%s]",name,str,
178 obj[i].bytes,ret,strerror(errno)) ;
181 LOG(NOTE,
"%s: %s read %d bytes",name,str,ret) ;
190 if(remain || (version != PP2PP_PED_VERSION)) {
191 LOG(ERR,
"PEDRMS bank corrupt (%d, %d); version %d but I known version %d",banks,remain,version,PP2PP_PED_VERSION) ;
195 for(
int j=0;j<banks;j++) {
200 pedrms->finalize(1, obj[i].sec, ped->seq_id, ped->chain_id) ;
209 if(found_some)
return pedrms ;
215 daq_dta *daq_pp2pp::handle_adc_ped_sub(
int sec,
int rdo)
217 int min_sec, max_sec ;
225 else if((sec<0) || (sec>MAX_SEC))
return 0 ;
227 min_sec = max_sec = sec ;
230 LOG(DBG,
"In handle_adc_ped_sub [%d,%d]",min_sec,max_sec) ;
232 adc_ped_sub->create(1,
"pp2pp_t",rts_id,DAQ_DTA_STRUCT(
pp2pp_t)) ;
235 for(
int i=min_sec;i<=max_sec;i++) {
239 sprintf(str,
"%s/sec%02d/rb00/adc_ped_sub",sfs_name, i) ;
240 LOG(DBG,
"Checking %s",str) ;
242 full_name = caller->get_sfs_name(str) ;
243 if(!full_name) continue ;
245 LOG(DBG,
"No bank %s",str) ;
247 int bytes = caller->sfs->fileSize(str) ;
248 if(bytes <= 0) continue ;
251 char *mem = (
char *)malloc(bytes) ;
253 int ret = caller->sfs->read(full_name, mem, bytes) ;
255 LOG(ERR,
"read error") ;
260 LOG(NOTE,
"Got %d bytes",bytes) ;
269 LOG(DBG,
"Chs %d: bytes %d",ps->ch_cou,l_bytes) ;
278 for(
int j=0;j<ps->ch_cou;j++) {
279 int ch = ps->dta[j].ch ;
280 int adc = ps->dta[j].adc ;
282 LOG(DBG,
"ix %d: ch %d, adc %d",j,ch,adc) ;
289 adc_ped_sub->finalize(1,i,pp->seq_id,pp->chain_id) ;
294 LOG(DBG,
"%d %d %d -- left %d",pp->seq_id,pp->chain_id,pp->svx_id,bytes) ;
303 LOG(DBG,
"exiting handle adc_ped_sub") ;
305 adc_ped_sub->rewind() ;
307 if(found_some)
return adc_ped_sub ;
311 daq_dta *daq_pp2pp::handle_adc(
int sec,
int rdo)
313 int min_sec, max_sec ;
321 else if((sec<0) || (sec>MAX_SEC))
return 0 ;
323 min_sec = max_sec = sec ;
326 adc->create(1,
"pp2pp_t",rts_id,DAQ_DTA_STRUCT(
pp2pp_t)) ;
328 for(
int i=min_sec;i<=max_sec;i++) {
331 LOG(DBG,
"Before sec %d",i) ;
332 sec_dta = handle_raw(i, -1) ;
333 LOG(DBG,
"After raw") ;
334 if(sec_dta == 0) continue ;
336 LOG(DBG,
"Before ite") ;
337 int ret = sec_dta->iterate() ;
338 LOG(DBG,
"After it %d",ret) ;
339 if(ret == 0) continue ;
343 LOG(DBG,
"pp2pp adc: sector %d, words %d",i,sec_dta->ncontent) ;
346 ret = decode(i,(
char *)sec_dta->Void, sec_dta->ncontent) ;
348 LOG(ERR,
"pp2pp_decode failed for sector %d",i) ;
351 LOG(DBG,
"After decode %d",ret) ;
356 if(found_some)
return adc ;
361 daq_dta *daq_pp2pp::handle_raw(
int sec,
int rdo)
368 int min_rdo, max_rdo ;
369 int min_sec, max_sec ;
374 } obj[MAX_SEC*(MAX_RDO+1)] ;
381 else if((sec<0) || (sec>MAX_SEC))
return 0 ;
383 min_sec = max_sec = sec ;
390 else if((rdo<0) || (rdo>MAX_RDO))
return 0 ;
392 min_rdo = max_rdo = rdo ;
404 for(
int s=min_sec;s<=max_sec;s++) {
405 for(
int r=min_rdo;r<=max_rdo;r++) {
407 sprintf(str,
"%s/sec%02d/rb%02d/raw",sfs_name, s, r) ;
408 full_name = caller->get_sfs_name(str) ;
409 if(!full_name) continue ;
411 LOG(DBG,
"%s: trying sfs on \"%s\"",name,str) ;
413 int size = caller->sfs->fileSize(str) ;
415 LOG(DBG,
"Got %d",size) ;
418 LOG(NOTE,
"%s: %s: not found in this event",name,str) ;
424 obj[o_cou].bytes = size ;
430 LOG(NOTE,
"%s: %s: reading in \"%s\": bytes %d",name,str,
"raw", size) ;
435 if(o_cou == 0)
return 0 ;
437 raw->create(tot_bytes,
"pp2pp_raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
439 for(
int i=0;i<o_cou;i++) {
441 sprintf(str,
"%s/sec%02d/rb%02d/raw",sfs_name, obj[i].sec, obj[i].
rb) ;
442 full_name = caller->get_sfs_name(str) ;
443 if(!full_name) continue ;
445 char *mem = (
char *)raw->request(obj[i].bytes) ;
447 int ret = caller->sfs->read(full_name, mem, obj[i].bytes) ;
449 if(ret != (
int)obj[i].bytes) {
450 LOG(ERR,
"%s: %s: read failed, expect %d, got %d [%s]",name,str,
451 obj[i].bytes,ret,strerror(errno)) ;
454 LOG(NOTE,
"%s: %s read %d bytes",name,str,ret) ;
458 raw->finalize(obj[i].bytes, obj[i].sec, obj[i].
rb, 0) ;
465 if(found_some)
return raw ;
470 int daq_pp2pp::get_token(
char *addr,
int words)
475 cou = get_l2(addr,words,trg,1) ;
477 if(cou==0)
return -1000 ;
478 if(trg[0].t==0)
return -ENOSYS ;
484 int daq_pp2pp::get_l2(
char *addr,
int words,
struct daq_trg_word *trgs,
int prompt)
486 u_int *d = (u_int *)addr ;
494 LOG(DBG,
"words %d: dta 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X",words,d[0],d[1],d[2],d[3],d[4]) ;
498 trg_cou = ntohl(d[words-1]) ;
501 LOG(ERR,
"trg_cou 0x%08X, words %d",trg_cou,words) ;
506 trg_dta = &(d[words-1-trg_cou]) ;
509 for(u_int i=0;i<trg_cou;i++) {
512 if((datum & 0xFF000000) == 0xEE000000) {
514 trgs[t_cou].t = (datum&0xF00) | ((datum & 0xF000)>>8) | ((datum & 0xF0000)>>16);
515 trgs[t_cou].daq = (datum>>4) & 0xF ;
516 trgs[t_cou].trg = datum & 0xF ;
517 trgs[t_cou].rhic = 0 ;
518 trgs[t_cou].rhic_delta = 0 ;
521 LOG(NOTE,
"T %4d (prompt): daq %d, trg %d",trgs[t_cou].t,trgs[t_cou].daq,trgs[t_cou].trg) ;
532 LOG(NOTE,
"No prompt trigger, trg_cou %d",trg_cou) ;
534 trgs[t_cou].t = 4097 ;
535 trgs[t_cou].daq = 0 ;
536 trgs[t_cou].trg = 5 ;
537 trgs[t_cou].rhic = 0 ;
538 trgs[t_cou].rhic_delta = 0 ;
541 LOG(NOTE,
"T %4d (no data): daq %d, trg %d",trgs[t_cou].t,trgs[t_cou].daq,trgs[t_cou].trg) ;
549 for(u_int i=0;i<trg_cou;i++) {
552 if((datum & 0xFF000000) != 0xEE000000) {
554 trgs[t_cou].t = (datum&0xF00) | ((datum & 0xF000)>>8) | ((datum & 0xF0000)>>16);
555 trgs[t_cou].daq = (datum>>4) & 0xF ;
556 trgs[t_cou].trg = datum & 0xF ;
557 trgs[t_cou].rhic = 1 ;
558 trgs[t_cou].rhic_delta = 1 ;
561 LOG(NOTE,
"T %4d (FIFO): daq %d, trg %d",trgs[t_cou].t,trgs[t_cou].daq,trgs[t_cou].trg) ;
566 switch(trgs[t_cou].trg) {
574 if(trgs[t_cou].t == 0) {
575 LOG(ERR,
"T %4d (FIFO): daq %d, trg %d -- token 0, skipping",trgs[t_cou].t,trgs[t_cou].daq,trgs[t_cou].trg) ;
592 int daq_pp2pp::decode(
int sec_id,
char *raw,
int bytes)
596 u_short seq[2], trg[2] ;
600 LOG(DBG,
"adc %p",adc) ;
602 int seq_id, chain_id, svx_id ;
604 int words = bytes/4 ;
607 d32 = (u_int *) raw ;
608 d16 = (u_short *) raw ;
610 for(
int i=0;i<bytes/4;i++) {
611 LOG(DBG,
"pp2pp data: %2d: 0x%08X",i,b2h32(d32[i])) ;
616 int trg_cou = b2h32(d32[words-1]) ;
619 int trg_ix = words - 1 - trg_cou ;
621 LOG(DBG,
"words %d, trg_cou %d",words,trg_cou) ;
622 for(
int i=0;i<trg_cou;i++) {
623 LOG(NOTE,
"pp2pp: trg %d/%d: 0x%08X",i+1,trg_cou,b2h32(d32[trg_ix+i])) ;
627 int w16 = 2 * (words - trg_cou - 1) ;
629 LOG(DBG,
"16bit words left: %d",w16) ;
632 int next_good_ix = 0 ;
634 int bunch_xing = -1 ;
635 u_int trigger = 0xFFFFFFFF ;
640 while(cur_ix < w16) {
647 seq[0] = b2h16(d16[cur_ix]) ;
649 seq[1] = b2h16(d16[cur_ix]) ;
653 trg[0] = b2h16(d16[cur_ix]) ;
655 trg[1] = b2h16(d16[cur_ix]) ;
658 seq_id = (seq[0] >> 8) >> 2 ;
659 chain_id = (seq[0] >> 8) & 3 ;
662 if(trigger == 0xFFFFFFFF) {
663 trigger = (trg[1] << 16) | trg[0] ;
666 u_int tmp = (trg[1] << 16) | trg[0] ;
670 LOG(ERR,
"pp2pp: seq %d:%d: expect trigger 0x%08X, read 0x%08X",seq_id,chain_id,trigger,tmp) ;
674 LOG(DBG,
"pp2pp: seq %d:%d: expect trigger 0x%08X, read 0x%08X",seq_id,chain_id,trigger,tmp) ;
679 bunch_xing = seq[0] & 0x7F ;
680 not_sparse = (seq[0] & 0x80)?1:0 ;
683 int tmp = seq[0] & 0x7F ;
685 if(tmp != bunch_xing) {
687 LOG(ERR,
"pp2pp: seq %d:%d: expect xing 0x%02X, read 0x%02X",seq_id,chain_id,bunch_xing,tmp) ;
691 LOG(DBG,
"pp2pp: seq %d:%d: expect xing 0x%02X, read 0x%02X",seq_id,chain_id,bunch_xing,tmp) ;
695 tmp = (seq[0] & 0x80)?1:0 ;
697 if(tmp != not_sparse) {
699 LOG(ERR,
"pp2pp: seq %d:%d: expect not_sparse 0x%02X, read 0x%02X",seq_id,chain_id,not_sparse,tmp) ;
703 LOG(DBG,
"pp2pp: seq %d:%d: expect not_sparse 0x%02X, read 0x%02X",seq_id,chain_id,not_sparse,tmp) ;
715 int fifo_w16 = (seq[1] >>8) | ((seq[1] & 0xF)<<8) ;
717 LOG(DBG,
"seq 0x%04X, 0x%04X; trg 0x%04X, 0x%04X; len %d",seq[0],seq[1],trg[0],trg[1],fifo_w16) ;
718 LOG(NOTE,
"pp2pp: seq_id %d:%d; words %d",seq_id,chain_id,fifo_w16) ;
723 d8 = (u_char *) &(d16[cur_ix]) ;
725 next_good_ix = cur_ix + fifo_w16 ;
730 for(i=0;i<fifo_w16;i++) {
736 LOG(DBG,
"Word %d/%d: ch %d, c_adc %d",i,fifo_w16,ch,c_adc) ;
741 LOG(ERR,
"Bad channel in seq %d:%d: %d %d",seq_id,chain_id,ch,c_adc) ;
745 LOG(NOTE,
"SVX break: seq %d:%d: SVX 0x%02X",seq_id, chain_id,ch) ;
748 LOG(DBG,
"requested %d",requested) ;
751 LOG(DBG,
"finalize %d %d %d",sec_id,d->seq_id,d->chain_id) ;
752 adc->finalize(1, sec_id, d->seq_id, d->chain_id) ;
758 LOG(DBG,
"svx_id %d",svx_id) ;
760 LOG(DBG,
"Calling adc request %p",adc) ;
762 d = (
struct pp2pp_t *) adc->request(1) ;
764 LOG(DBG,
"d is %p",d) ;
769 d->chain_id = chain_id ;
771 d->bunch_xing = bunch_xing ;
772 d->not_sparse = not_sparse ;
776 memset(d->adc,0,
sizeof(d->adc)) ;
777 memset(d->trace,0,
sizeof(d->trace)) ;
781 LOG(ERR,
"Bad data -- SVX ID was not found in sequencer %d, chain %d (0x%X 0x%X)",seq_id, chain_id, ch,c_adc) ;
786 LOG(DBG,
"datum %d/%d: %3d = 0x%02X",i,fifo_w16,ch,c_adc) ;
789 LOG(WARN,
"Seq %d, chain %c, SVX %d: duplicate channel %d: ADC now %d, was %d!",
790 seq_id,chain_id+
'A',svx_id,
791 ch,c_adc,d->adc[ch]) ;
795 else d->trace[ch] = 1 ;
812 LOG(DBG,
"Padding %d/%d: %3d = 0x%02X",i,fifo_w16,ch,c_adc) ;
818 adc->finalize(1, sec_id, d->seq_id, d->chain_id) ;
822 cur_ix = next_good_ix ;
void version(std::ostream &os=std::cout)
print HepMC version