6 #include <rtsSystems.h>
7 #include <daqFormats.h>
9 #include <SFS/sfs_index.h>
11 #include <DAQ_READER/daqReader.h>
12 #include <DAQ_READER/daq_dta.h>
18 #include "itpcInterpreter.h"
22 #include <DAQ_TPC23/itpc23.h>
24 const char *daq_itpc::help_string =
"\
28 raw returns=ptr of start of DDL data; c1=sector[1..1]; c2=rdo[1..4]; \n\
37 LOG(DBG,
"%s",__PRETTY_FUNCTION__) ;
38 daq_det_factory::det_factories[ITPC_ID] = this ;
42 LOG(DBG,
"%s",__PRETTY_FUNCTION__) ;
49 int daq_itpc::no_sw16 = 0 ;
57 name = rts2name(rts_id) ;
61 if(caller) caller->insert(
this, rts_id) ;
82 memset(fcf,0,
sizeof(fcf)) ;
88 LOG(DBG,
"%s: constructor: caller %p",name,rts_caller) ;
96 delete ifee_fy17_raw ;
97 delete ifee_fy17_sampa ;
110 for(
int i=0;i<25;i++) {
111 if(fcf[i])
delete fcf[i] ;
117 void daq_itpc::setup_fcf(
int det,
int orient)
120 fcf_det_orient = orient ;
123 void daq_itpc::run_stop()
125 for(
int s=1;s<=24;s++) {
126 if(fcf[s]) fcf[s]->run_stop() ;
131 daq_dta *daq_itpc::put(
const char *in_bank,
int sec,
int row,
int pad,
void *p1,
void *p2)
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)) ;
140 else if(strcasecmp(in_bank,
"gain")==0) {
141 gain->create(32*1024,(
char *)
"gain",rts_id,DAQ_DTA_STRUCT(
daq_det_gain)) ;
145 LOG(ERR,
"%s: unknown bank type \"%s\"",name,in_bank) ;
150 daq_dta *daq_itpc::get(
const char *bank,
int sec,
int row,
int pad,
void *p1,
void *p2)
154 if(present==0)
return 0 ;
156 LOG(DBG,
"%s: looking for bank %s",name,bank) ;
158 if(strcmp(bank,
"*")==0) bank =
"raw" ;
161 if(strcasecmp(bank,
"raw")==0) {
162 return handle_raw(sec,row) ;
164 else if(strcasecmp(bank,
"adc")==0) {
165 return handle_sampa(sec,row,1) ;
167 else if(strcasecmp(bank,
"sampa")==0) {
168 return handle_sampa(sec,row,0) ;
170 else if(strcasecmp(bank,
"pedrms")==0) {
171 return handle_ped(sec,row) ;
173 else if(strcasecmp(bank,
"cld")==0) {
174 return handle_cld(sec) ;
176 else if(strcasecmp(bank,
"cld_sim")==0) {
177 return handle_cld_sim(sec) ;
179 else if(strcasecmp(bank,
"adc_sim")==0) {
180 if(adc_sim->is_empty())
return 0 ;
184 else if(strcasecmp(bank,
"gain")==0) {
185 if(gain->is_empty())
return 0 ;
193 else if(strcasecmp(bank,
"ifee_fy17_raw")==0) {
194 return handle_ifee_fy17_raw() ;
196 else if(strcasecmp(bank,
"ifee_fy17_sampa")==0) {
197 return handle_ifee_fy17_sampa() ;
200 LOG(ERR,
"%s: unknown bank type \"%s\"",name,bank) ;
206 daq_dta *daq_itpc::handle_cld_sim(
int sec)
208 int min_sec, max_sec ;
209 u_char evt_started[25] ;
211 memset(evt_started,0,
sizeof(evt_started)) ;
223 daq_dta *sim =
get(
"adc_sim") ;
227 while(sim && sim->iterate()) {
229 u_short sim_array[512] ;
230 u_short track_array[512] ;
234 if((min_sec<=s) && (s<=max_sec)) ;
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 ;
253 if(g) fcf[s]->init(g) ;
258 if(!evt_started[s]) {
259 fcf[s]->event_start() ;
266 memset(sim_array,0,
sizeof(sim_array)) ;
267 memset(track_array,0,
sizeof(track_array)) ;
269 for(u_int i=0;i<sim->ncontent;i++) {
271 if(sim_dta[i].adc==0) sim_dta[i].adc = 0xFFFF ;
273 sim_array[sim_dta[i].tb] = sim_dta[i].adc ;
274 track_array[sim_dta[i].tb] = sim_dta[i].track_id ;
278 fcf[s]->do_ch_sim(sim->row,sim->pad,sim_array,track_array) ;
283 cld_sim->create(1024,(
char *)
"cld_sim",rts_id,DAQ_DTA_STRUCT(
daq_sim_cld_x)) ;
285 char *buff = (
char *) malloc(1024*1024) ;
286 for(
int s=min_sec;s<=max_sec;s++) {
293 int bytes = fcf[s]->do_fcf(buff,1024*1024) ;
299 u_int *end_buff = (u_int *)(buff + bytes) ;
300 u_int *p_buff = (u_int *)buff ;
302 while(p_buff < end_buff) {
303 u_int row = *p_buff++ ;
305 u_int int_cou = *p_buff++ ;
307 int ints_per_cluster = (row>>16) ;
310 int clusters = int_cou/ints_per_cluster ;
316 for(
int i=0;i<clusters;i++) {
317 fcf[s]->fcf_decode(p_buff,dc,version) ;
319 p_buff += ints_per_cluster ;
323 cld_sim->finalize(clusters,s,row) ;
334 daq_dta *daq_itpc::handle_raw(
int sec,
int rdo)
336 int min_sec, max_sec, min_rdo, max_rdo ;
344 if((sec <= 0)||(sec>24)) {
349 min_sec = max_sec = sec ;
352 if((rdo <= 0)||(rdo>4)) {
357 min_rdo = max_rdo = rdo ;
361 raw->create(16*1024,(
char *)
"raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
366 for(
int s=min_sec;s<=max_sec;s++) {
367 for(
int r=min_rdo;r<=max_rdo;r++) {
371 sprintf(str,
"%s/sec%02d/rdo%d/ifee",sfs_name,s,r) ;
373 char *full_name = caller->get_sfs_name(str) ;
375 LOG(DBG,
"name [%s] -> full_name [%s]",str,full_name) ;
380 sprintf(str,
"%s/sec%02d/rdo%d/ifee23",sfs_name,s,r) ;
382 full_name = caller->get_sfs_name(str) ;
384 LOG(DBG,
"name [%s] -> full_name [%s]",str,full_name) ;
386 if(full_name==0) continue ;
392 LOG(DBG,
"full name %p",full_name) ;
393 LOG(DBG,
"full name %s",full_name) ;
395 int size = caller->sfs->fileSize(full_name) ;
397 LOG(NOTE,
"full_name [%s] --> size %d",full_name,size) ;
399 if(size <= 0) continue ;
401 char *mem = (
char *) raw->request(size) ;
404 LOG(ERR,
"ITPC: error in %d %d",s,r) ;
408 caller->sfs->read(full_name,mem,size) ;
410 raw->finalize(size,s,r,0) ;
431 void ch_start(
int c) {
439 void accum(
int sec0,
int rdo0,
int port0,
int fee_id,
int ch,
int tb,
int adc) {
441 at[tb_cou].adc = adc ;
446 void ch_done(
int err) {
450 itpc_ifee_to_rowpad(fee_id, ch, row, pad) ;
451 dta->finalize(tb_cou, sector+1, row, pad) ;
454 dta->finalize(tb_cou, sector+1, rdo*16+port, fee_id*256+ch) ;
459 LOG(ERR,
"%d %d %d done, %d",sector+1,fee_id,ch,tb_cou) ;
467 daq_dta *daq_itpc::handle_sampa(
int sec,
int rdo,
int in_adc)
471 int min_sec, max_sec, min_rdo, max_rdo ;
479 if((sec <= 0)||(sec>24)) {
484 min_sec = max_sec = sec ;
487 if((rdo <= 0)||(rdo>4)) {
492 min_rdo = max_rdo = rdo ;
498 sampa->create(1024,(
char *)
"adc",rts_id,DAQ_DTA_STRUCT(
daq_adc_tb)) ;
506 for(
int s=min_sec;s<=max_sec;s++) {
508 int it23_started = 0 ;
511 for(
int r=min_rdo;r<=max_rdo;r++) {
517 rdo_dta = handle_raw(s,r) ;
519 if(rdo_dta==0) continue ;
521 ret = rdo_dta->iterate() ;
522 if(ret==0) continue ;
524 dta = (u_int *)rdo_dta->Byte ;
525 words = rdo_dta->ncontent/4 ;
527 if(words==0) continue ;
532 it23->online = online ;
535 it23->log_level = 0 ;
540 if(it23_started==0) {
560 ret = it23->rdo_scan((
char *)dta,words) ;
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) ;
567 ret = it->rdo_scan_top(dta,words) ;
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) ;
589 daq_dta *daq_itpc::handle_cld(
int sec)
592 int min_sec, max_sec ;
598 if((sec <= 0)||(sec>24)) {
603 min_sec = max_sec = sec ;
607 cld->create(1024,(
char *)
"cld",rts_id,DAQ_DTA_STRUCT(
daq_cld)) ;
610 for(
int s=min_sec;s<=max_sec;s++) {
614 sprintf(str,
"%s/sec%02d/cld",sfs_name,s) ;
616 char *full_name = caller->get_sfs_name(str) ;
618 LOG(DBG,
"name [%s] -> full_name [%s]",str,full_name) ;
620 if(full_name == 0) continue ;
622 int size = caller->sfs->fileSize(full_name) ;
624 LOG(NOTE,
"full_name [%s] --> size %d",full_name,size) ;
626 if(size <= 0) continue ;
628 char *mem = (
char *) malloc(size) ;
631 LOG(CRIT,
"ITPC: error in %d %d",s) ;
635 caller->sfs->read(full_name,mem,size) ;
637 u_int *end_buff = (u_int *)(mem+size) ;
638 u_int *p_buff = (u_int *)mem ;
640 while(p_buff < end_buff) {
645 u_int row = *p_buff++ ;
646 u_int version = *p_buff++ ;
647 u_int int_cou = *p_buff++ ;
649 int ints_per_cluster = (row>>16) ;
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) ;
654 int clusters = int_cou/ints_per_cluster ;
658 for(
int i=0;i<clusters;i++) {
659 fcf->fcf_decode(p_buff,dc,version) ;
661 p_buff += ints_per_cluster ;
665 cld->finalize(clusters,s,row,0) ;
679 daq_dta *daq_itpc::handle_ped(
int sec,
int rdo)
687 daq_dta *daq_itpc::handle_ifee_fy17_raw()
695 sprintf(str,
"%s/sec01/rb01/fee_raw",sfs_name) ;
696 char *full_name = caller->get_sfs_name(str) ;
698 LOG(DBG,
"%s: trying sfs on \"%s\"",name,str) ;
699 if(full_name == 0)
return 0 ;
701 int size = caller->sfs->fileSize(full_name) ;
703 LOG(DBG,
"Got size %d",size) ;
705 LOG(DBG,
"%s: %s: not found in this event",name,str) ;
709 ifee_fy17_raw->create(size,
"itpc_raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
710 char *st = (
char *) ifee_fy17_raw->request(size) ;
712 caller->sfs->read(full_name, st, size) ;
714 LOG(DBG,
"sfs read succeeded") ;
716 ifee_fy17_raw->finalize(size,1,1,0) ;
718 ifee_fy17_raw->rewind() ;
720 return ifee_fy17_raw ;
725 daq_dta *daq_itpc::handle_ifee_fy17_sampa()
733 sprintf(str,
"%s/sec01/rb01/fee_raw",sfs_name) ;
734 char *full_name = caller->get_sfs_name(str) ;
736 LOG(DBG,
"%s: trying sfs on \"%s\"",name,str) ;
737 if(full_name == 0)
return 0 ;
739 int size = caller->sfs->fileSize(full_name) ;
741 LOG(DBG,
"Got size %d",size) ;
743 LOG(DBG,
"%s: %s: not found in this event",name,str) ;
747 char *ptr = (
char *) malloc(size) ;
748 LOG(DBG,
"Malloc at %p",ptr) ;
750 caller->sfs->read(full_name, ptr, size) ;
752 LOG(DBG,
"sfs read succeeded") ;
754 ifee_fy17_sampa->create(1000,
"adc",rts_id,DAQ_DTA_STRUCT(
daq_adc_tb)) ;
756 LOG(NOTE,
"Starting fee_scan") ;
760 dta_c.rdo_start(0,0,0) ;
763 while(dta_c.fee_scan((u_short *)ptr,size/2)) {
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 ;
769 ifee_fy17_sampa->finalize(dta_c.tb_cou,dta_c.sector,dta_c.fee_id,dta_c.fee_ch) ;
772 dta_c.rdo_zap(dta_c.rdo_p) ;
775 ifee_fy17_sampa->rewind() ;
777 return ifee_fy17_sampa ;
782 int daq_itpc::get_token(
char *addr,
int words)
784 LOG(ERR,
"get_token") ;
789 cou = get_l2(addr,words,trg,1) ;
791 if(cou==0)
return -1000 ;
792 if(trg[0].t==0)
return -ENOSYS ;
797 static inline u_int sw16(u_int d)
800 if(p->no_sw16)
return d ;
802 d = ((d>>16)&0xFFFF)|(d<<16) ;
817 static u_int swap_s12(u_int dta)
819 u_int n_dta = (dta & 0xFFF00000) ;
820 for(
int n=0;n<5;n++) {
821 int dd = (dta>>(n*4)) & 0xF ;
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 ;
828 int sdd = (b3<<3)|(b4<<2)|(b2<<1)|(b1<<0) ;
831 n_dta |= (sdd<<(n*4)) ;
840 int daq_itpc::get_l2(
char *addr,
int words,
struct daq_trg_word *trg,
int rdo)
847 if(token>4095) token = 1 ;
849 trg[t_cou].t = token ;
868 u_int want_dump = 0 ;
871 u_int *d = (u_int *)addr + 4 ;
884 for(
int i=0;i<16;i++) {
885 LOG(DBG,
"%d = 0x%08X",i,d[i]) ;
886 if(d[i] == 0xCCCC001C) {
893 if(sw16(d[0]) != 0x001CCCCC) {
894 LOG(ERR,
"First word 0x%08X of %d words - bad",d[0],words) ;
902 if(ds==0x980000004) {
905 else if((ds&0xFF00000F)==0x98000004) {
906 rdo_version = (ds >> 4) & 0xFF ;
914 LOG(WARN,
"%d: not a triggered event 0x%08X",rdo,ds) ;
920 switch(rdo_version) {
922 if(sw16(d[2]) != 0x12340000) {
923 LOG(ERR,
"%d: wrong version 0x%X",rdo,d[2]) ;
928 trg_fired = sw16(d[3]) ;
931 trg_fired = sw16(d[2]) ;
934 LOG(ERR,
"%d: wrong version 0x%X",rdo,rdo_version) ;
941 LOG(ERR,
"%d: bad words %d",rdo,words) ;
949 if(sw16(d[3])==0x980000FC) {
952 u_int *dd_end = d+words ;
955 u_int sd = sw16(*dd) ;
956 u_int dd_r = sd & 0xFFFFFF00 ;
961 buff[buff_cou++] = 0 ;
962 LOG(INFO,
"RDO_asc(get_l2) %d: \"%s\"",rdo,buff) ;
967 else if(dd_r==0x9800F500) {
971 if(c==
'\n' || buff_cou==120) {
972 buff[buff_cou++] = 0 ;
973 LOG(INFO,
"RDO_asc(get_l2) %d: \"%s\"",rdo,buff) ;
977 buff[buff_cou++] = c ;
980 else LOG(TERR,
"... 0x%08X",sd) ;
992 for(
int i=(words-6);i>=0;i--) {
993 if(sw16(d[i]) == 0x98001000) {
997 if(sw16(d[i]) == 0x58001001) {
1004 LOG(ERR,
"%d: no trailer found",rdo) ;
1011 if(rdo_version==0) {
1012 if(sw16(d[trl_ix++]) != 0xABCD0000) {
1013 LOG(ERR,
"%d: wrong trailer word ABCD",rdo) ;
1019 evt_status = sw16(d[trl_ix++]) ;
1020 trg_cou = sw16(d[trl_ix++]) & 0xFFFF ;
1025 u_int fee_status = 0xABCDEF12 ;
1032 s = (sw16(d[trl_stop_ix++]))<<16 ;
1033 s |= sw16(d[trl_stop_ix++]) ;
1041 for(
int i=0;i<32;i++) {
1042 if(evt_status & (1<<i)) {
1044 LOG(ERR,
" %d:#%d overwritten!!!",rdo,i/2+1) ;
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) ;
1063 trg[0].reserved[0] = trg_fired ;
1066 LOG(ERR,
"%d: bad trg cou 0x%08X",rdo,trg_cou) ;
1070 else if (trg_cou > 60) {
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 ;
1081 trg[i+1].reserved[0] = sw16(d[trl_ix++]) ;
1086 LOG(WARN,
"%d: Lots of triggers %d",rdo,trg_cou) ;
1088 for(
int i=0;i<5;i++) {
1089 u_int v = trg[i].reserved[0];
1094 t = ((v>>8)&0xF)<<8 ;
1095 t |= ((v>>12)&0xF)<<4 ;
1096 t |= ((v>>16)&0xF) ;
1099 daq_cmd = (v>>4) & 0xF ;
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) ;
1109 for(
int i=0;i<=trg_cou;i++) {
1110 u_int v = trg[i].reserved[0];
1115 t = ((v>>8)&0xF)<<8 ;
1116 t |= ((v>>12)&0xF)<<4 ;
1117 t |= ((v>>16)&0xF) ;
1120 daq_cmd = (v>>4) & 0xF ;
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) ;
1128 if(trg_cou==0 && trg[0].reserved[0]==0) {
1139 if(!(trg[0].reserved[0] & 0x00200000)) {
1140 trg[t_cou].t = 4097 ;
1141 trg[t_cou].daq = 0 ;
1142 trg[t_cou].trg = 0 ;
1146 u_int v = trg[0].reserved[0] ;
1155 t = ((v>>8)&0xF)<<8 ;
1156 t |= ((v>>12)&0xF)<<4 ;
1157 t |= ((v>>16)&0xF) ;
1160 daq_cmd = (v>>4) & 0xF ;
1164 LOG(TERR,
"RDO %d: prompt T %d, trg %d, daq %d",rdo,t,trg_cmd,daq_cmd) ;
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) ;
1174 if(((v&0xFFF00000) != 0x04300000)&&((v&0xFFF00000)!=0x08300000)) {
1175 LOG(ERR,
"%d: trigger odd: 0x%08X: %d %d %d",rdo,v,t,trg_cmd,daq_cmd) ;
1180 if((t!=0) && (trg_cmd>=4) && (trg_cmd<=11)) {
1183 trg[t_cou].trg = trg_cmd ;
1184 trg[t_cou].daq = daq_cmd ;
1196 LOG(ERR,
"%d: odd prompt trigger T %d, trg 0x%X, daq 0x%X [0x%08X]",rdo,t,trg_cmd,daq_cmd,v) ;
1198 trg[t_cou].t = 4097 ;
1199 trg[t_cou].trg = 0 ;
1200 trg[t_cou].daq = 0 ;
1209 for(u_int i=1;i<(trg_cou+1);i++) {
1210 u_int v = trg[i].reserved[0] ;
1222 t = ((v>>8)&0xF)<<8 ;
1223 t |= ((v>>12)&0xF)<<4 ;
1224 t |= ((v>>16)&0xF) ;
1226 trg[t_cou].trg = v & 0xF ;
1227 trg[t_cou].daq = (v>>4) & 0xF ;
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) ;
1239 if(t==0 || trg[t_cou].trg==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) ;
1247 if(trg[t_cou].trg==2 && trg[t_cou].daq==3 && trg[t_cou].t==10) ;
1253 #endif //LOG(TERR,"trg %d: 0x%08X: trg %d",i,v,v&0xF) ;
1255 if(trg[t_cou].trg>=4 && trg[t_cou].trg<13) {
1261 if((v&0xFFF00000) != 0x04300000) {
1262 LOG(NOTE,
"RDO %d: %d/%d: 0x%08X: %d %d %d",rdo,i,(trg_cou+1),
1264 trg[t_cou].t,trg[t_cou].trg,trg[t_cou].daq) ;
1272 if(trg[t_cou].trg==14) continue ;
1273 if(trg[t_cou].trg==2) continue ;
1290 for(u_int i=1;i<(trg_cou+1);i++) {
1291 u_int v = trg[i].reserved[0] ;
1295 t = ((v>>8)&0xF)<<8 ;
1296 t |= ((v>>12)&0xF)<<4 ;
1297 t |= ((v>>16)&0xF) ;
1300 daq = (v>>4) & 0xF ;
1302 LOG(ERR,
"%d: %d: %d %d %d [0x%08X;0x%X]",rdo,i,t,trg,daq,v,want_dump) ;
1308 LOG(ERR,
"%d: t_cou = 0?",rdo) ;
1310 trg[t_cou].t = 4097 ;
1311 trg[t_cou].daq = 0 ;
1312 trg[t_cou].trg = 0 ;
1321 LOG(ERR,
"%d: Error in get_l2 0x%X [words %d]",rdo,err,words) ;
void version(std::ostream &os=std::cout)
print HepMC version