7 #include <rtsSystems.h>
11 #include <SFS/sfs_index.h>
12 #include <DAQ_READER/daqReader.h>
13 #include <DAQ_READER/daq_dta.h>
19 const char *daq_sst::help_string =
"SST\n\
20 raw returns raw data\n" ;
26 daq_det_factory::det_factories[SST_ID] = this ;
40 name = rts2name(rts_id) ;
44 if(caller) caller->insert(
this, rts_id) ;
50 LOG(DBG,
"%s: constructor: caller %p",name,rts_caller) ;
56 LOG(DBG,
"%s: DEstructor",name) ;
67 daq_dta *daq_sst::get(
const char *bank,
int sec,
int rdo,
int pad,
void *p1,
void *p2)
71 if(present == 0)
return 0 ;
74 if(strcasecmp(bank,
"raw")==0) {
75 return handle_raw(sec,rdo) ;
77 else if(strcasecmp(bank,
"adc")==0) {
78 return handle_adc(sec,rdo) ;
80 else if(strcasecmp(bank,
"pedrms")==0) {
81 return handle_ped(sec) ;
85 LOG(ERR,
"%s: unknown bank type \"%s\"",name,bank) ;
97 int daq_sst::raw_to_adc_utility(
int s,
int r,
char *rdobuff,
int words,
daq_sst_ped_t *peds,
int mode)
99 u_int *dta = (u_int *)rdobuff ;
102 u_int *d32_end = dta + words ;
103 u_int *d32_start = dta ;
109 int e = events[r-1] ;
112 if(d32_end[-1] != 0xBBBBBBBB) {
113 LOG(ERR,
"S%d-%d: %u: last word is 0x%08X, expect 0xBBBBBBBB -- data corrupt,skipping!",s,r,e,d32_end[-1]) ;
124 if(d == 0xDDDDDDDD) {
132 LOG(ERR,
"S%d-%d: %u: can't find 0xDDDDDDDD -- data corrupt, skipping!",s,r,e) ;
141 if(d != 0xDDDDDDDD) {
142 u_int *d_here = d32 - 1 ;
143 LOG(ERR,
"S%d-%d: %u: fiber %d: can't find 0xDDDDDDDD at offset %d [0x%08X] -- data corrupt, skipping!",s,r,e,fib,
144 d_here-d32_start,*d_here) ;
147 for(
int i=0;i<5;i++) {
148 LOG(ERR,
" %d: 0x%08X",d_here-d32_start,*d_here) ;
154 u_int fiber_id = d32[0] ;
160 int words = fiber_id & 0x000FFFF0 ;
163 LOG(NOTE,
"S%d-%d: fiber %d: ID 0x%08X, words %d",
164 s,r,fib,fiber_id,words) ;
166 int known_data_format = 1 ;
168 if(fiber_id & 0xFFF00000) {
172 LOG(NOTE,
"S%d-%d: %u: fiber %d: empty [0x%08X]",s,r,e,fib,fiber_id) ;
175 LOG(NOTE,
"S%d-%d: %u: fiber %d: overflow [0x%08X]",s,r,e,fib,fiber_id) ;
178 LOG(WARN,
"S%d-%d: %u: fiber %d: odd fiber_id [0x%08X]",s,r,e,fib,fiber_id) ;
179 known_data_format = 0 ;
184 int pipe_mode = fiber_id & 0xF ;
190 known_data_format = 2 ;
193 known_data_format = 2 ;
198 LOG(ERR,
"S%d-%d: %u: fiber %d: unknown pipe mode 0x%X (0x%08X)",s,r,e,fib,pipe_mode,fiber_id) ;
224 for(
int i=0;i<12;i++) {
225 LOG(NOTE,
" %d: 0x%08X",i,d32[i]) ;
232 if(!known_data_format) {
233 LOG(WARN,
"Unknown data format") ;
241 if((mode==2) && (known_data_format != 1)) {
242 LOG(ERR,
"S%d-%d: %u: fiber %d: ZS data 0x%08X in pedestal runs is not allowed",s,r,e,fib,fiber_id) ;
274 switch(known_data_format) {
277 for(
int i=0;i<words;i++) {
280 int strip = (d & 0xFFC000)>>14 ;
283 sst->adc = d & 0x3FF ;
284 sst->hybrid = (d & 0x3C00)>>10 ;
288 LOG(ERR,
"S%d-%d: fiber %d, bad strip %d",s,r,fib,strip) ;
295 if(no_error) fiber_events[r-1][fib]++ ;
303 for(
int i=0;i<words;i++) {
309 LOG(ERR,
"S%d-%d: fiber %d, bad strip %d",s,r,fib,strip) ;
317 sst->hybrid = hybrid ;
321 adc_prime = (aadc+SST_PED_ADC_OFFSET)%1024 ;
322 if(adc_prime >= (1024-SST_OUTLIERBAND)) ;
323 else if(adc_prime <= (SST_OUTLIERBAND)) ;
325 if(peds->cou[fib][hybrid][strip] < 0xFFF0) {
326 peds->ped[fib][hybrid][strip] += adc_prime ;
327 peds->rms[fib][hybrid][strip] += adc_prime * adc_prime ;
328 peds->cou[fib][hybrid][strip]++ ;
344 LOG(ERR,
"S%d-%d: fiber %d, bad strip %d",s,r,fib,strip) ;
348 aadc = (d & 0xFFC00) >> 10 ;
352 sst->hybrid = hybrid ;
356 adc_prime = (aadc+SST_PED_ADC_OFFSET)%1024 ;
357 if(adc_prime >= (1024-SST_OUTLIERBAND)) ;
358 else if(adc_prime <= (SST_OUTLIERBAND)) ;
360 if(peds->cou[fib][hybrid][strip] < 0xFFF0) {
361 peds->ped[fib][hybrid][strip] += adc_prime ;
362 peds->rms[fib][hybrid][strip] += adc_prime * adc_prime ;
363 peds->cou[fib][hybrid][strip]++ ;
383 LOG(ERR,
"S%d-%d: fiber %d, bad strip %d",s,r,fib,strip) ;
387 aadc = (d & 0x3FF00000) >> 20 ;
391 sst->hybrid = hybrid ;
395 adc_prime = (aadc+SST_PED_ADC_OFFSET)%1024 ;
397 if(adc_prime >= (1024-SST_OUTLIERBAND)) ;
398 else if(adc_prime <= (SST_OUTLIERBAND)) ;
400 if(peds->cou[fib][hybrid][strip] < 0xFFF0) {
401 peds->ped[fib][hybrid][strip] += adc_prime ;
402 peds->rms[fib][hybrid][strip] += adc_prime * adc_prime ;
403 peds->cou[fib][hybrid][strip]++ ;
418 if(words==(SST_STRIP_COU*SST_HYBRID_COU)/3) fiber_events[r-1][fib]++ ;
424 if((mode==1) && (sst-sst_start)) {
425 LOG(NOTE,
"Got %d structs, requested %d",sst-sst_start,3*words) ;
426 adc->finalize(sst-sst_start,s,r,fib) ;
429 adc_count += sst-sst_start ;
432 LOG(NOTE,
"RDO %d, fiber %d: words %d",r,fib,words) ;
440 if(*d32 != 0xCCCCCCCC) {
441 LOG(ERR,
"S%d-%d: %u: can't find 0xCCCCCCCC at offset %d [0x%08X] -- data corrupt, skipping!",s,r,e,d32-d32_start,*d32) ;
443 while(d32 < d32_end) {
444 LOG(ERR,
" %d: 0x%08X",d32-d32_start,*d32) ;
451 LOG(NOTE,
"%d %d -- returing %d",s,r,adc_count) ;
461 daq_dta *daq_sst::handle_adc(
int sec,
int rdo,
char *rdobuff,
int words)
463 int r_start, r_stop ;
464 int s_start, s_stop ;
473 s_start = s_stop = sec ;
481 r_start = r_stop = rdo ;
485 adc->create(128,
"sst_adc",rts_id,DAQ_DTA_STRUCT(
daq_sst_data_t)) ;
487 LOG(NOTE,
"handle_adc: %d %d, %d %d",s_start,s_stop,r_start,r_stop) ;
489 for(
int s=s_start;s<=s_stop;s++) {
491 for(
int r=r_start;r<=r_stop;r++) {
495 daq_dta *raw_d = handle_raw(s,r) ;
496 if(raw_d == 0) continue ;
497 if(raw_d->iterate() == 0)
continue ;
499 dta = (u_int *) raw_d->Void ;
500 words = raw_d->ncontent/4 ;
504 get_l2((
char *)dta,words,trg,r) ;
508 dta = (u_int *) rdobuff ;
514 raw_to_adc_utility(s,r,(
char *)dta,words,0,1) ;
528 daq_dta *daq_sst::handle_raw(
int sec,
int rdo)
531 int r_start, r_stop ;
532 int s_start, s_stop ;
544 LOG(DBG,
"%s: present %d",name,present) ;
554 s_start = s_stop = sec ;
562 r_start = r_stop = rdo ;
566 raw->create(8*1024,
"sst_raw",rts_id,DAQ_DTA_STRUCT(
char)) ;
569 for(
int s=s_start;s<=s_stop;s++) {
571 for(
int r=r_start;r<=r_stop;r++) {
572 sprintf(str,
"%s/sec%02d/rb%02d/raw",sfs_name, s, r) ;
573 full_name = caller->get_sfs_name(str) ;
575 if(!full_name) continue ;
576 bytes = caller->sfs->fileSize(full_name) ;
579 st = (
char *) raw->request(bytes) ;
581 int ret = caller->sfs->read(str, st, bytes) ;
583 LOG(ERR,
"ret is %d") ;
587 raw->finalize(bytes,s,r,0) ; ;
600 int daq_sst::get_l2(
char *buff,
int words,
struct daq_trg_word *trg,
int rdo)
604 u_int *d32 = (u_int *)buff ;
606 int last_ix = words - 1 ;
607 int token, daq_cmd, trg_cmd ;
616 if(d32[0] != 0xAAAAAAAA) err |= 0x10000 ;
617 if(d32[last_ix] != 0xBBBBBBBB) err |= 0x1 ;
619 token_word = d32[1] ;
623 for(
int i=0;i<12;i++) {
624 if(d32[i] == 0xDDDDDDDD) {
632 token_word = d32[0] ;
638 token_word = d32[2] ;
642 if(token_word == 0x22200000) ;
654 if(token_word == 0x22200000) {
655 LOG(WARN,
"RDO %d: trigger-only event...",rdo) ;
662 if(token_word & 0xFFF00000) {
666 token = token_word & 0xFFF ;
667 daq_cmd = (token_word & 0xF000) >> 12 ;
668 trg_cmd = (token_word & 0xF0000) >> 16 ;
678 if(trg_cmd != 4) err |= 4 ;
680 trg[t_cou].t = token ;
681 trg[t_cou].daq = daq_cmd ;
682 trg[t_cou].trg = trg_cmd ;
683 trg[t_cou].rhic = d32[7] ;
684 trg[t_cou].rhic_delta = 0 ;
689 int last_p = last_ix - 1 ;
697 for(
int i=last_p;i>=0;i--) {
698 if(d32[i] == 0xCCCCCCCC) {
705 for(
int i=first_trg;i<=last_p;i++) {
706 trg[t_cou].t = d32[i] & 0xFFF ;
707 trg[t_cou].daq = (d32[i] & 0xF000) >> 12 ;
708 trg[t_cou].trg = (d32[i] & 0xF0000) >> 16 ;
709 trg[t_cou].rhic = trg[0].rhic + 1 ;
710 trg[t_cou].rhic_delta = 0 ;
712 switch(trg[t_cou].trg) {
733 LOG(ERR,
"RDO %d: error 0x%X, t_cou %d",rdo,err,t_cou) ;
735 for(
int i=0;i<16;i++) {
736 LOG(ERR,
" RDO %d: %2d/%2d: 0x%08X",rdo,i,words,d32[i]) ;
739 int s = last_ix - 10 ;
742 for(
int i=s;i<=last_ix;i++) {
743 LOG(ERR,
" RDO %d: %2d/%2d: 0x%08X",rdo,i,words,d32[i]) ;
748 if(t_cou) t_cou = 1 ;
750 else if(err & 0xFFFF0000) {
752 LOG(WARN,
"RDO %d: error 0x%X, t_cou %d",rdo,err,t_cou) ;
754 for(
int i=0;i<16;i++) {
755 LOG(WARN,
" RDO %d: %2d/%2d: 0x%08X",rdo,i,words,d32[i]) ;
758 int s = last_ix - 10 ;
761 for(
int i=s;i<=last_ix;i++) {
762 LOG(WARN,
" RDO %d: %2d/%2d: 0x%08X",rdo,i,words,d32[i]) ;
772 daq_dta *daq_sst::handle_ped(
int sec)
782 int s_start, s_stop ;
785 LOG(NOTE,
"handle_ped(%d)",sec) ;
792 s_start = s_stop = sec ;
797 for(sec=s_start;sec<=s_stop;sec++) {
799 sprintf(str,
"%s/sec%02d/pedrms",sfs_name, sec) ;
801 LOG(NOTE,
"Trying %s",str) ;
803 full_name = caller->get_sfs_name(str) ;
806 LOG(NOTE,
"full_name %s",full_name) ;
809 if(!full_name) continue ;
810 bytes = caller->sfs->fileSize(full_name) ;
812 LOG(NOTE,
"bytes %d",bytes) ;
814 d = (u_short *) malloc(bytes) ;
817 int ret = caller->sfs->read(str, (
char *)d, bytes) ;
819 LOG(ERR,
"ret is %d") ;
823 LOG(ERR,
"Bad pedestal version") ;
827 LOG(ERR,
"Bad pedestal version") ;
833 int strip_cou = d[5] ;
842 for(
int r=0;r<rdo_cou;r++) {
845 for(
int f=0;f<fib_cou;f++) {
849 for(
int h=0;h<hy_cou;h++) {
850 for(
int s=0;s<strip_cou;s++) {
851 short ped = (short)*d++ ;
852 short rms = (short)*d++ ;
854 f_ped->ped[h][s] = ped ;
855 f_ped->rms[h][s] = rms ;
860 ped->finalize(1,sec,rdo1,f) ;