6 #include <rtsSystems.h>
9 #include <SFS/sfs_index.h>
10 #include <DAQ_READER/daqReader.h>
11 #include <DAQ_READER/daq_dta.h>
16 #include <DAQ_TPC/daq_tpc.h>
17 #include <TPC/trans_table.hh>
18 #include <TPC/rowlen.h>
20 #include <DAQ1000/ddl_struct.h>
27 #include "tpxFCF_2D.h"
30 #include <DAQ_TPC23/tpx23.h>
37 daq_det_factory::det_factories[TPX_ID] = this ;
49 const char *daq_tpx::help_string =
"\
51 ****** TPX Help ******* \n\
57 raw (sector,rdo); returns (char *) of start of DDL data\n\
58 adc (sector,rdo); returns tb_adc data\n\
59 cld (sector,rdo); returns float cld from data\n\
60 cld_raw (sector,rdo); returns cld_raw CLD data\n\
63 (1<<0) calc pedestals from data when get(adc) called\n\
64 (1<<1) calc gain from data when get(adc) called\n\
67 cld_c (sector,rdo); returns float cld calculated from data\n\
68 gain (sector,rdo,0,char *fname); loads gains from database: \n\
69 fname == 0 from Offline databases\n\
70 fname != 0 from file fname\n\
71 gain_c (sector,rdo); returns calculated gains (see m_Mode) \n\
72 ped (sector,rdo); returns ped/rms from file\n\
73 ped_c (sector,rdo); returns calculated ped/rms (see m_Mode) \n\
78 void daq_tpx::help()
const
80 printf(
"%s\n%s\n",GetCVS(),help_string) ;
88 name = rts2name(rts_id) ;
94 if(caller) caller->insert(
this, rts_id) ;
99 LOG(DBG,
"legacy %p",legacy) ;
125 for(
int i=0;i<=24;i++) {
129 fcf_tmp_storage = 0 ;
131 fcf_afterburner_disable = 0 ;
132 fcf_run_compatibility = 9 ;
139 LOG(DBG,
"%s: constructor: caller %p",name, caller) ;
145 LOG(DBG,
"%s: DEstructor",name) ;
153 LOG(DBG,
"%s: DEstructor done",name) ;
157 LOG(DBG,
"%s: DEstructor done",name) ;
161 LOG(DBG,
"%s: DEstructor done",name) ;
170 LOG(DBG,
"%s: DEstructor done",name) ;
174 LOG(DBG,
"%s: DEstructor done",name) ;
178 LOG(DBG,
"%s: DEstructor done %p ",name,legacy) ;
182 LOG(DBG,
"%s: DEstructor done",name) ;
185 if(gain_algo)
delete gain_algo ;
186 if(ped_algo)
delete ped_algo ;
187 if(stat_algo)
delete stat_algo ;
189 LOG(DBG,
"%s: DEstructor done",name) ;
191 for(
int i=0;i<=24;i++) {
192 if(fcf_algo[i])
delete fcf_algo[i] ;
193 if(fcf_2d_algo[i])
delete fcf_2d_algo[i] ;
195 LOG(DBG,
"%s: DEstructor done",name) ;
196 if(fcf_tmp_storage) free(fcf_tmp_storage) ;
198 LOG(DBG,
"%s: DEstructor done",name) ;
221 LOG(NOTE,
"%s: Make(): presence %d, evt %d: m_Mode 0x%08X, event_mode 0x%08X",name,pres,evt_num,mode,event_mode) ;
224 LOG(DBG,
"%s: Make(): not found in this event...",name) ;
229 if(mode & m_Mode_DAQ_RT) {
235 char *obuff = out_buffer ;
236 int max_bytes = out_bytes ;
239 if((in_buffer == 0) || (in_bytes<=0) || (out_buffer==0) || (max_bytes==0)) {
240 LOG(ERR,
"Bad buffers -- skipping") ;
245 ret = tpx_get_start(in_buffer, in_bytes/4, &rdo, 0) ;
248 LOG(ERR,
"Horrible error -- skipping") ;
255 if(event_mode & m_Mode_DAQ_RAW) {
270 ret = Raw->Make(&rdo,obuff,max_bytes) ;
276 if(rdo.type != DDL_TYPE_DTA)
return 0 ;
282 if(event_mode & m_Mode_DAQ_GAIN) {
285 else if(event_mode & m_Mode_DAQ_PED) {
288 else if(event_mode & m_Mode_DAQ_FCF_RAW) {
289 ret = Fcf->Make(&rdo, obuff, max_bytes) ;
295 out_bytes = obuff - out_buffer ;
300 int min_sec, max_sec ;
301 int min_rdo, max_rdo ;
303 if(def_sector <= 0) {
308 min_sec = max_sec = def_sector ;
316 min_rdo = max_rdo = def_rdo ;
335 int daq_tpx::InitRun(
int run)
337 u_int mode = GetMode() ;
339 LOG(NOTE,
"%s: InitRun(%d), m_Mode 0x%08X",name, run, mode) ;
350 if(mode & m_Mode_DAQ_RT) {
351 LOG(ERR,
"Not yet ready for Online!") ;
366 LOG(DBG,
"get(gain) returns %p",g) ;
368 LOG(NOTE,
"Using externally generated gains") ;
370 while(g->iterate()) {
371 if(g->row > 45) continue ;
373 LOG(DBG,
"\tsec %d, row %d: %d",g->sec,g->row,g->ncontent) ;
374 int max_pad = tpc_rowlen[g->row] ;
375 if((
int)g->ncontent > max_pad) {
376 LOG(NOTE,
"sector %d, row %d: want %d, have %d",g->sec,g->row,g->ncontent,max_pad) ;
378 else max_pad = g->ncontent ;
380 for(
int pad=1;pad<=max_pad;pad++) {
381 LOG(DBG,
"Gains: %d %d %d %f %f",g->sec,g->row,pad,g->gain[pad].gain,g->gain[pad].t0) ;
382 gain_algo->set_gains(g->sec,g->row,pad,g->gain[pad].gain,g->gain[pad].t0) ;
387 gain_algo->from_file(
"/RTS/conf/tpx/tpx_gains.txt",0) ;
391 if(mode & m_Mode_DAQ_PED) {
400 if(mode & m_Mode_DAQ_GAIN) {
401 gain_algo->init(def_sector) ;
412 int daq_tpx::FinishRun(
int old)
414 u_int mode = GetMode() ;
416 LOG(NOTE,
"%s: Run %d finished with %d events, m_Mode 0x%08X",name,run_num,evt_num,mode) ;
418 if(mode & m_Mode_DAQ_PED) {
419 LOG(NOTE,
"Calculating peds") ;
432 if(mode & m_Mode_DAQ_GAIN) {
434 LOG(NOTE,
"Calculating gains") ;
443 for(
int i=0;i<=24;i++) {
452 for(
int i=0;i<=24;i++) {
454 delete fcf_2d_algo[i] ;
460 if(fcf_tmp_storage) {
461 free(fcf_tmp_storage) ;
462 fcf_tmp_storage = 0 ;
471 daq_dta *daq_tpx::get(
const char *in_bank,
int sec,
int row,
int pad,
void *p1,
void *p2)
482 LOG(DBG,
"%s: looking for bank %s",name,bank) ;
486 if(strcasecmp(bank,
"adc_sim")==0) {
487 if(adc_sim->is_empty())
return 0 ;
491 else if(strcasecmp(bank,
"gain")==0) {
492 if(gain->is_empty())
return 0 ;
496 else if(strcasecmp(bank,
"cld_sim")==0) {
497 return handle_cld_sim(sec,row) ;
499 else if(strcasecmp(bank,
"cld_2d_sim")==0) {
500 return handle_cld_2d_sim(sec,row) ;
505 if(!present)
return 0 ;
506 if(caller && (caller->detector_bugs & (1<<TPX_ID)))
return 0 ;
509 if(strcasecmp(bank,
"raw")==0) {
510 return handle_raw(sec,row) ;
512 else if(strcasecmp(bank,
"adc")==0) {
513 return handle_adc(sec,row) ;
515 else if(strcasecmp(bank,
"cld_raw")==0) {
516 return handle_cld_raw(sec,row) ;
518 else if(strcasecmp(bank,
"cld")==0) {
519 return handle_cld(sec,row) ;
521 else if(strcasecmp(bank,
"pedrms")==0) {
522 return handle_ped(sec) ;
524 else if(strcasecmp(bank,
"legacy")==0) {
525 return handle_legacy(sec,row) ;
527 else if(strcasecmp(bank,
"altro")==0) {
528 return handle_altro(sec,row) ;
531 LOG(ERR,
"%s: unknown bank type \"%s\"",name,bank) ;
538 daq_dta *daq_tpx::put(
const char *in_bank,
int sec,
int row,
int pad,
void *p1,
void *p2)
543 if(strcasecmp(in_bank,
"adc_sim")==0) {
549 sim_row_count = row ;
550 sim_tpx_rowlen = (u_char *)p1 ;
553 LOG(NOTE,
"adc_sim: row count %d, rowlen %p",sim_row_count,sim_tpx_rowlen) ;
555 adc_sim->create(32*1024,(
char *)
"adc_sim",rts_id,DAQ_DTA_STRUCT(
daq_sim_adc_tb)) ;
558 else if(strcasecmp(in_bank,
"gain")==0) {
559 gain->create(32*1024,(
char *)
"gain",rts_id,DAQ_DTA_STRUCT(
daq_det_gain)) ;
563 LOG(ERR,
"%s: unknown bank type \"%s\"",name,in_bank) ;
568 daq_dta *daq_tpx::handle_legacy(
int sec,
int rdo)
570 const int tpx_tpc_tb_delta = 22 ;
573 int found_something = 0 ;
580 min_s = max_s = sec ;
583 legacy->create(1,
"tpx_legacy",rts_id,DAQ_DTA_STRUCT(
tpc_t)) ;
585 for(
int s=min_s;s<=max_s;s++) {
586 struct tpc_t *tpc_p = 0 ;
591 tpc_p = (
struct tpc_t *) legacy->request(1) ;
592 memset(tpc_p,0,
sizeof(
tpc_t)) ;
599 while(dd->iterate()) {
600 int r = dd->row - 1 ;
601 int p = dd->pad - 1 ;
603 if((r<0) || (p<0)) continue ;
605 found_something = 1 ;
610 for(
int i=0;i<(
int)dd->ncontent;i++) {
611 int tpc_tb, tpc_adc ;
615 tpc_tb = i - tpx_tpc_tb_delta ;
616 if(tpc_tb <0) continue ;
618 tpc_adc = ped[i].ped ;
619 if(tpc_adc > 255) tpc_adc = 255 ;
621 int c = tpc_p->counts[r][p] ;
622 tpc_p->adc[r][p][c] = tpc_adc ;
623 tpc_p->timebin[r][p][c] = (u_char)(ped[i].rms*16.0) ;
625 (tpc_p->counts[r][p])++ ;
626 tpc_p->channels_sector++ ;
630 found_something = 1 ;
634 tpc_p->max_channels_sector = 512 * 5692 ;
635 tpc_p->max_channels_all = 512 * 5692 * 24 ;
637 legacy->finalize(1,s,0,0) ;
645 dd = handle_adc(s,-1) ;
648 tpc_p = (
struct tpc_t *) legacy->request(1) ;
649 memset(tpc_p,0,
sizeof(
tpc_t)) ;
653 while(dd->iterate()) {
654 int r = dd->row - 1 ;
655 int p = dd->pad - 1 ;
657 if((r<0) || (p<0)) continue ;
659 found_something = 1 ;
660 tpc_p->max_channels_sector = 512 * 5692 ;
664 for(u_int i=0;i<dd->ncontent;i++) {
665 int tpc_tb, tpc_adc ;
668 tpc_tb = dd->adc[i].tb - tpx_tpc_tb_delta ;
669 if(tpc_tb < 0) continue ;
672 tpc_adc = log10to8_table[dd->adc[i].adc] ;
674 if(tpc_adc == 0) continue ;
676 int c = tpc_p->counts[r][p] ;
677 tpc_p->adc[r][p][c] = tpc_adc ;
678 tpc_p->timebin[r][p][c] = tpc_tb ;
680 (tpc_p->counts[r][p])++ ;
681 tpc_p->channels_sector++ ;
690 dd = handle_cld(s,-1) ;
693 tpc_p = (
struct tpc_t *) legacy->request(1) ;
694 memset(tpc_p,0,
sizeof(
tpc_t)) ;
697 while(dd->iterate()) {
698 int r = dd->row - 1 ;
702 tpc_p->has_clusters = 1 ;
703 found_something = 1 ;
705 for(u_int i=0;i<dd->ncontent;i++) {
706 int c = tpc_p->cl_counts[r] ;
707 if(c >= TPC_READER_MAX_CLUSTERS) break ;
713 tpc_t1 = (int)dd->cld[i].t1 - tpx_tpc_tb_delta ;
714 tpc_t2 = (
int)dd->cld[i].t2 - tpx_tpc_tb_delta ;
715 tpc_t = (double)dd->cld[i].tb - tpx_tpc_tb_delta ;
718 if((tpc_t1<0) || (tpc_t2<0) || (tpc_t<0.0))
continue ;
720 tpc_p->cl[r][c].t1 = tpc_t1 ;
721 tpc_p->cl[r][c].t2 = tpc_t2 ;
722 tpc_p->cl[r][c].t = tpc_t ;
724 tpc_p->cl[r][c].p1 = dd->cld[i].p1 ;
725 tpc_p->cl[r][c].p2 = dd->cld[i].p2 ;
726 tpc_p->cl[r][c].p = dd->cld[i].pad ;
728 tpc_p->cl[r][c].charge = dd->cld[i].charge ;
729 tpc_p->cl[r][c].flags = dd->cld[i].flags ;
731 (tpc_p->cl_counts[r])++ ;
742 tpc_p->max_channels_all = 512 * 5692 * 24 ;
744 legacy->finalize(1,s,0,0) ;
751 if(found_something)
return legacy ;
756 daq_dta *daq_tpx::handle_ped(
int sec)
760 int min_sec, max_sec ;
767 else if((sec<1) || (sec>24))
return 0 ;
769 min_sec = max_sec = sec ;
779 for(
int s=min_sec;s<=max_sec;s++) {
781 for(
int rdo=0;rdo<=6;rdo++) {
784 if(rdo==0) sprintf(str,
"%s/sec%02d/pedrms",sfs_name, s) ;
785 else sprintf(str,
"%s/sec%02d/rb%02d/pedrms",sfs_name,s,rdo) ;
787 LOG(NOTE,
"%s: trying sfs on \"%s\"",name,str) ;
789 char *full_name = caller->get_sfs_name(str) ;
794 int size = caller->sfs->fileSize(full_name) ;
796 LOG(NOTE,
"%s: sector %d: ped size %d",name,s,size) ;
804 char *tmp_cache = (
char *) valloc(size) ;
808 int ret = caller->sfs->read(full_name, tmp_cache, size) ;
810 if(ret != (
int)size) {
811 LOG(ERR,
"%s: %s: read failed, expect %d, got %d [%s]",name,str,
812 size,ret,strerror(errno)) ;
826 u_short *d16 = (u_short *) tmp_cache ;
841 int cou = l2h16(*d16++) ;
843 int row = l2h16(*d16++) ;
844 int pad = row & 0xFF ;
847 LOG(DBG,
"sector %d: row %d, pad %d, cou %d",s,row,pad,cou) ;
854 for(
int i=0;i<cou;i++) {
855 u_short tmp = l2h16(*d16++) ;
857 int i_rms = (tmp & 0xFC00) >> 10 ;
858 int i_ped = tmp & 0x3FF ;
860 d[i].rms = (float)i_rms / 16.0 ;
865 ped->finalize(cou,s,row,pad) ;
874 LOG(DBG,
"Returning from ped_handler") ;
876 if(tot_bytes == 0)
return 0 ;
884 daq_dta *daq_tpx::handle_adc(
int sec,
int rdo)
888 int min_sec, max_sec ;
889 int min_rdo, max_rdo ;
897 else if((sec<1) || (sec>24))
return 0 ;
907 else if((rdo<0) || (rdo>6))
return 0 ;
909 min_rdo = max_rdo = rdo ;
915 for(
int s=min_sec;s<=max_sec;s++) {
916 for(
int r=min_rdo;r<=max_rdo;r++) {
926 adc->create(guess_bytes,(
char *)
"adc",rts_id,DAQ_DTA_STRUCT(
daq_adc_tb)) ;
929 for(
int s=min_sec;s<=max_sec;s++) {
930 for(
int r=min_rdo;r<=max_rdo;r++) {
940 LOG(NOTE,
"Calling handle_raw for %d:%d",s,r) ;
941 rdo_dta = handle_raw(s, r) ;
945 LOG(WARN,
"rdo_dta NULL?") ;
949 int ret = rdo_dta->iterate() ;
954 LOG(DBG,
"Called handle_raw for %d:%d, iterate %d, returned %d objs",s,r,ret,rdo_dta->ncontent) ;
955 if(rdo_dta->ncontent == 0) continue ;
957 rdo_ptr = (
char *)rdo_dta->Byte ;
958 rdo_words = rdo_dta->ncontent / 4 ;
961 u_int *hdr_version = (u_int *)rdo_ptr ;
963 int t23_started = 0 ;
965 if((hdr_version[0]>>24)&0xF) {
985 t23->tpx_d = &tpx_d ;
996 ret = t23->rdo_scan((
char *)rdo_ptr,rdo_words) ;
999 u_int *dd = (u_int *)rdo_ptr+rdo_words ;
1001 if(dd[-2]&0xFF000000) {
1002 LOG(NOTE,
"Token %d, bad event 0x%08X",token,dd[-2]) ;
1004 LOG(NOTE,
"FMT23: handle_adc(%d,%d), token %d, ret 0x%X, status 0x%X",s,r,token,ret,dd[-2]) ;
1008 token = tpx_get_start(rdo_ptr, rdo_words, &rdo, 0) ;
1011 LOG(ERR,
"horrible error, token is %d?",token) ;
1016 LOG(ERR,
"RDO mismatch: in data %d, expect %d",rdo.rdo,r) ;
1019 u_int *data_end = rdo.data_end ;
1021 a.rdo = rdo.rdo -1 ;
1023 a.what = TPX_ALTRO_DO_ADC ;
1028 data_end = tpx_scan_to_next(data_end, rdo.data_start, &a) ;
1030 if(a.count == 0) continue ;
1033 if((a.row>45) || (a.pad==0) || (a.pad>182)) {
1034 LOG(ERR,
"TPX: S%02d:RDO%d: row %d, pad %d",a.sector,rdo.rdo,a.row,a.pad) ;
1041 for(u_int i=0 ; i < a.count ; i++) {
1042 at[i].adc = a.adc[i] ;
1043 at[i].tb = a.tb[i] ;
1047 adc->finalize(a.count, s, a.row, a.pad) ;
1049 }
while(data_end && (data_end > rdo.data_start)) ;
1069 daq_dta *daq_tpx::handle_altro(
int sec,
int rdo)
1072 int min_sec, max_sec ;
1073 int min_rdo, max_rdo ;
1081 else if((sec<1) || (sec>24))
return 0 ;
1091 else if((rdo<0) || (rdo>6))
return 0 ;
1093 min_rdo = max_rdo = rdo ;
1099 for(
int s=min_sec;s<=max_sec;s++) {
1100 for(
int r=min_rdo;r<=max_rdo;r++) {
1108 altro->create(guess_bytes,(
char *)
"adc",rts_id,DAQ_DTA_STRUCT(
daq_adc_tb)) ;
1111 for(
int s=min_sec;s<=max_sec;s++) {
1112 for(
int r=min_rdo;r<=max_rdo;r++) {
1121 LOG(NOTE,
"Calling handle_raw for %d:%d",s,r) ;
1122 rdo_dta = handle_raw(s, r) ;
1125 int t23_started = 0 ;
1128 LOG(WARN,
"rdo_dta NULL?") ;
1132 int ret = rdo_dta->iterate() ;
1137 LOG(DBG,
"Called handle_raw for %d:%d, iterate %d, returned %d objs",s,r,ret,rdo_dta->ncontent) ;
1138 if(rdo_dta->ncontent == 0) continue ;
1140 rdo_ptr = (
char *)rdo_dta->Byte ;
1141 rdo_words = rdo_dta->ncontent / 4 ;
1145 u_int *hdr_version = (u_int *)rdo_ptr ;
1148 if((hdr_version[0]>>24)&0xF) {
1149 LOG(NOTE,
"FMT23: handle_altro(%d,%d): version 0x%X",s,r,hdr_version[0]) ;
1159 t23->log_level = 0 ;
1167 if(t23_started==0) {
1177 ret = t23->rdo_scan((
char *)rdo_ptr,rdo_words) ;
1178 token = t23->token ;
1184 token = tpx_get_start(rdo_ptr, rdo_words, &rdo, 0) ;
1187 LOG(ERR,
"horrible error, token is %d?",token) ;
1192 LOG(ERR,
"RDO mismatch: in data %d, expect %d",rdo.rdo,r) ;
1195 u_int *data_end = rdo.data_end ;
1197 a.rdo = rdo.rdo -1 ;
1199 a.what = TPX_ALTRO_DO_ADC ;
1204 data_end = tpx_scan_to_next(data_end, rdo.data_start, &a) ;
1206 if(a.count == 0) continue ;
1217 for(u_int i=0 ; i < a.count ; i++) {
1218 at[i].adc = a.adc[i] ;
1219 at[i].tb = a.tb[i] ;
1222 altro->finalize(a.count, s, a.id, a.ch) ;
1224 }
while(data_end && (data_end > rdo.data_start)) ;
1248 daq_dta *daq_tpx::handle_raw(
int sec,
int rdo)
1252 int min_sec, max_sec, min_rdo, max_rdo ;
1264 else if((sec<1) || (sec>24))
return 0 ;
1266 min_sec = max_sec = sec ;
1273 else if((rdo<1) || (rdo>6))
return 0 ;
1275 min_rdo = max_rdo = rdo ;
1285 for(
int s=min_sec;s<=max_sec;s++) {
1286 for(
int r=min_rdo;r<=max_rdo;r++) {
1290 sprintf(str,
"%s/sec%02d/rb%02d/adc",sfs_name, s, r) ;
1292 LOG(NOTE,
"%s: trying sfs on \"%s\"",name,str) ;
1294 full_name = caller->get_sfs_name(str) ;
1295 if(full_name == 0) {
1296 sprintf(str,
"%s/sec%02d/rb%02d/adc23",sfs_name, s, r) ;
1298 LOG(NOTE,
"%s: trying sfs on \"%s\"",name,str) ;
1300 full_name = caller->get_sfs_name(str) ;
1302 if(full_name==0) continue ;
1308 int size = caller->sfs->fileSize(full_name) ;
1310 LOG(NOTE,
"%s: sector %d, rdo %d : raw size %d",name,s,r,size) ;
1314 LOG(DBG,
"%s: %s: not found in this event",name,str) ;
1320 obj[o_cou].sec = s ;
1321 obj[o_cou].bytes = size ;
1327 LOG(DBG,
"%s: %s: reading in \"%s\": bytes %d",name,str,
"raw", size) ;
1332 raw->create(tot_bytes,(
char *)
"raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
1335 for(
int i=0;i<o_cou;i++) {
1337 sprintf(str,
"%s/sec%02d/rb%02d/adc",sfs_name,obj[i].sec, obj[i].
rb) ;
1338 char *full_name = caller->get_sfs_name(str) ;
1340 if(full_name == 0) {
1341 sprintf(str,
"%s/sec%02d/rb%02d/adc23",sfs_name, obj[i].sec, obj[i].
rb) ;
1343 LOG(NOTE,
"%s: trying sfs on \"%s\"",name,str) ;
1345 full_name = caller->get_sfs_name(str) ;
1347 if(full_name==0) continue ;
1353 LOG(NOTE,
"%s: request %d bytes",name,obj[i].bytes) ;
1355 char *mem = (
char *) raw->request(obj[i].bytes) ;
1357 int ret = caller->sfs->read(full_name, mem, obj[i].bytes) ;
1359 if(ret != (
int)obj[i].bytes) {
1360 LOG(ERR,
"%s: %s: read failed, expect %d, got %d [%s]",name,str,
1361 obj[i].bytes,ret,strerror(errno)) ;
1364 LOG(NOTE,
"%s: %s read %d bytes",name,str,ret) ;
1367 raw->finalize(obj[i].bytes, obj[i].sec, obj[i].
rb, 0) ;
1371 LOG(DBG,
"Returning from raw_handler") ;
1377 daq_dta *daq_tpx::handle_cld_raw(
int sec,
int rdo)
1381 int min_sec, max_sec, min_rdo, max_rdo ;
1393 else if((sec<1) || (sec>24))
return 0 ;
1395 min_sec = max_sec = sec ;
1402 else if((rdo<1) || (rdo>6))
return 0 ;
1404 min_rdo = max_rdo = rdo ;
1415 for(
int s=min_sec;s<=max_sec;s++) {
1417 sprintf(str,
"%s/sec%02d/cld23",sfs_name, s) ;
1419 LOG(NOTE,
"%s: trying sfs on \"%s\"",name,str) ;
1421 char *full_name = caller->get_sfs_name(str) ;
1422 if(full_name && min_rdo==1) {
1428 int size = caller->sfs->fileSize(full_name) ;
1430 LOG(NOTE,
"%s: CLD23: sector %d, rdo 23 : cld size %d",name,s,size) ;
1435 LOG(DBG,
"%s: %s: not found in this event",name,str) ;
1440 obj[o_cou].rb = 23 ;
1441 obj[o_cou].sec = s ;
1442 obj[o_cou].bytes = size ;
1448 LOG(DBG,
"%s: %s: reading in \"%s\": bytes %d",name,str,
"cld_raw", size) ;
1456 sprintf(str,
"%s/sec%02d/cld10",sfs_name, s) ;
1458 LOG(NOTE,
"%s: trying sfs on \"%s\"",name,str) ;
1460 full_name = caller->get_sfs_name(str) ;
1461 if(full_name && min_rdo==1) {
1468 int size = caller->sfs->fileSize(full_name) ;
1470 LOG(NOTE,
"%s: CLD23: sector %d, rdo 10 : cld size %d",name,s,size) ;
1475 LOG(DBG,
"%s: %s: not found in this event",name,str) ;
1480 obj[o_cou].rb = 10 ;
1481 obj[o_cou].sec = s ;
1482 obj[o_cou].bytes = size ;
1488 LOG(DBG,
"%s: %s: reading in \"%s\": bytes %d",name,str,
"cld_raw", size) ;
1494 sprintf(str,
"%s/sec%02d/cld11",sfs_name, s) ;
1496 LOG(NOTE,
"%s: trying sfs on \"%s\"",name,str) ;
1498 full_name = caller->get_sfs_name(str) ;
1499 if(full_name && min_rdo==2) {
1506 int size = caller->sfs->fileSize(full_name) ;
1508 LOG(NOTE,
"%s: CLD23: sector %d, rdo 11 : cld size %d",name,s,size) ;
1513 LOG(DBG,
"%s: %s: not found in this event",name,str) ;
1518 obj[o_cou].rb = 11 ;
1519 obj[o_cou].sec = s ;
1520 obj[o_cou].bytes = size ;
1526 LOG(DBG,
"%s: %s: reading in \"%s\": bytes %d",name,str,
"cld_raw", size) ;
1531 if(cld23) continue ;
1535 for(
int r=min_rdo;r<=max_rdo;r++) {
1538 sprintf(str,
"%s/sec%02d/cld%02d",sfs_name, s, r) ;
1540 LOG(NOTE,
"%s: trying sfs on \"%s\"",name,str) ;
1542 char *full_name = caller->get_sfs_name(str) ;
1543 if(full_name == 0) continue ;
1545 int size = caller->sfs->fileSize(full_name) ;
1547 LOG(NOTE,
"%s: sector %d, rdo %d : cld size %d",name,s,r,size) ;
1552 LOG(DBG,
"%s: %s: not found in this event",name,str) ;
1558 obj[o_cou].sec = s ;
1559 obj[o_cou].bytes = size ;
1565 LOG(DBG,
"%s: %s: reading in \"%s\": bytes %d",name,str,
"cld_raw", size) ;
1570 cld_raw->create(tot_bytes,(
char *)
"cld_raw",rts_id,DAQ_DTA_STRUCT(u_char)) ;
1573 for(
int i=0;i<o_cou;i++) {
1575 sprintf(str,
"%s/sec%02d/cld%02d",sfs_name,obj[i].sec, obj[i].
rb) ;
1576 char *full_name = caller->get_sfs_name(str) ;
1577 if(full_name==0) continue ;
1579 char *mem = (
char *) cld_raw->request(obj[i].bytes) ;
1581 int ret = caller->sfs->read(full_name, mem, obj[i].bytes) ;
1583 if(ret != (
int)obj[i].bytes) {
1584 LOG(ERR,
"%s: %s: read failed, expect %d, got %d [%s]",name,str,
1585 obj[i].bytes,ret,strerror(errno)) ;
1588 LOG(NOTE,
"%s: %s read %d bytes",name,str,ret) ;
1591 cld_raw->finalize(obj[i].bytes, obj[i].sec, obj[i].
rb, 0) ;
1595 LOG(DBG,
"Returning from cld_raw_handler") ;
1603 daq_dta *daq_tpx::handle_cld(
int sec,
int rdo)
1606 int min_sec, max_sec ;
1607 int min_rdo, max_rdo ;
1608 int found_broken_edges = 0 ;
1615 else if((sec<1) || (sec>24))
return 0 ;
1625 else if((rdo<0) || (rdo>6))
return 0 ;
1627 min_rdo = max_rdo = rdo ;
1633 for(
int s=min_sec;s<=max_sec;s++) {
1634 for(
int r=min_rdo;r<=max_rdo;r++) {
1640 int guess_bytes = rdos * (1152/10) * (
sizeof(
daq_store) + 10*
sizeof(
daq_cld)) ;
1643 cld->create(guess_bytes,(
char *)
"cld",rts_id,DAQ_DTA_STRUCT(
daq_cld)) ;
1646 for(
int s=min_sec;s<=max_sec;s++) {
1647 for(
int r=min_rdo;r<=max_rdo;r++) {
1651 LOG(NOTE,
"Calling handle_cld_raw for %d:%d",s,r) ;
1652 dd = handle_cld_raw(s, r) ;
1659 int ret = dd->iterate() ;
1664 LOG(NOTE,
"Called handle_cld_raw for %d:%d, iterate %d, returned %d objs",s,r,ret,dd->ncontent) ;
1666 int bytes = dd->ncontent ;
1667 if(bytes <= 0) continue ;
1669 u_int *buff = dd->Int32 ;
1671 u_int *end_buff = buff + bytes/4 ;
1672 u_int *p_buff = buff ;
1674 while(p_buff < end_buff) {
1675 u_int row = *p_buff++ ;
1676 u_int cou = *p_buff++ ;
1679 LOG(NOTE,
"CLD23 %d: S%02d:%d: row 0x%08X, cou 0x%08X",cld23,s,r,row,cou) ;
1684 LOG(WARN,
"Bad data in S%02d-%d, row %d -- count is %d -- skipping",
1695 LOG(NOTE,
"CLD23: S%02d:%d: row %d, version 0x%04X, count %d",s,r,row,version,cou) ;
1698 for(u_int i=0;i<cou;i++) {
1699 p_buff += fcf_algo[0]->fcf_decode(p_buff, dc, version) ;
1700 if((row==8) && (dc->flags & FCF_BROKEN_EDGE)) {
1701 found_broken_edges = 1 ;
1706 cld->finalize(cou,s,row,0) ;
1720 if(found_broken_edges && !fcf_afterburner_disable) {
1722 const int FCF_MAX_MERGED_COU = 256 ;
1724 daq_cld *merged_store[25][FCF_MAX_MERGED_COU] ;
1725 int merged_cou[25] ;
1727 memset(merged_cou,0,
sizeof(merged_cou)) ;
1729 while(cld->iterate()) {
1730 if(cld->row != 8) continue ;
1734 for(u_int i=0;i<cld->ncontent;i++) {
1735 if((cld->cld[i].flags & FCF_BROKEN_EDGE) && (merged_cou[s] < FCF_MAX_MERGED_COU)) {
1736 merged_store[s][merged_cou[s]] = &cld->cld[i] ;
1742 for(
int s=1;s<=24;s++) {
1743 fcf_algo[0]->afterburner(merged_cou[s],merged_store[s]) ;
1758 static int cmpr_sim_adc(
const void *first,
const void *second)
1763 if(f->tb == s->tb)
return 0 ;
1764 if(f->tb > s->tb)
return -1 ;
1773 daq_dta *daq_tpx::handle_cld_2d_sim(
int sec,
int row)
1776 int min_sec, max_sec ;
1777 int min_row, max_row ;
1785 else if((sec<1) || (sec>24))
return 0 ;
1793 max_row = sim_row_count ;
1795 else if((row<0) || (row>250))
return 0 ;
1797 min_row = max_row = row ;
1803 for(
int s=min_sec;s<=max_sec;s++) {
1804 if(fcf_2d_algo[s]) {
1805 LOG(DBG,
"start_evt(): sec %d",s) ;
1806 fcf_2d_algo[s]->start_evt_2d(s,0) ;
1809 for(
int r=min_row;r<=max_row;r++) {
1817 cld_2d_sim->create(guess_bytes,(
char *)
"cld_2d_sim",rts_id,DAQ_DTA_STRUCT(
daq_sim_cld)) ;
1821 daq_dta *sim =
get(
"adc_sim") ;
1824 LOG(ERR,
"%s: sector %d, row %d: you need to add simulated data first!",name,sec,row) ;
1830 while(sim->iterate()) {
1831 if((min_sec<=sim->sec) && (sim->sec<= max_sec) && (min_row<=sim->row) && (sim->row <= max_row)) ;
1835 if(fcf_2d_algo[sim->sec]==0) {
1837 LOG(NOTE,
"No algo assigned for sector %d -- creating one!",sim->sec) ;
1839 fcf_2d_algo[sim->sec]->set_id(sim->sec) ;
1840 fcf_2d_algo[sim->sec]->fcf_style = 2 ;
1842 fcf_2d_algo[sim->sec]->run_compatibility = fcf_run_compatibility ;
1843 fcf_2d_algo[sim->sec]->do_cuts = fcf_do_cuts ;
1845 for(
int r=1;r<=6;r++) {
1846 fcf_2d_algo[sim->sec]->config2(sim->sec,r,1,sim_row_count,sim_tpx_rowlen) ;
1852 fcf_2d_algo[sim->sec]->apply_gains2(gain_algo) ;
1856 if(fcf_tmp_storage==0) {
1857 fcf_tmp_storage = (u_int *)valloc(FCF_TMP_BYTES) ;
1860 fcf_2d_algo[sim->sec]->start_evt_2d(sim->sec,0) ;
1871 qsort(sim->sim_adc, sim->ncontent,
sizeof(sim->sim_adc[0]),cmpr_sim_adc) ;
1875 for(u_int i=0;i<sim->ncontent;i++) {
1876 a.adc[i] = sim->sim_adc[i].adc ;
1877 a.tb[i] = sim->sim_adc[i].tb ;
1883 fcf_2d_algo[sim->sec]->do_pad_2d(&a, sim->sim_adc) ;
1889 for(
int s=min_sec;s<=max_sec;s++) {
1890 if(fcf_2d_algo[s]) {
1891 LOG(NOTE,
"Trying sec %d",s) ;
1893 int words = fcf_2d_algo[s]->stage_2d(fcf_tmp_storage,FCF_TMP_BYTES) ;
1894 if(words<=0) continue ;
1896 LOG(NOTE,
"Sector %d: %d words",s,words) ;
1898 u_int *p_buff = fcf_tmp_storage ;
1899 u_int *end_buff = p_buff + words ;
1901 while(p_buff < end_buff) {
1902 u_int row = *p_buff++ ;
1903 u_int cou = *p_buff++ ;
1907 u_int version = (row >> 16) ;
1916 int skip = fcf_2d_algo[s]->fcf_decode(p_buff, cld + g_cou, version) ;
1926 cld_2d_sim->finalize(g_cou,s,row) ;
1931 LOG(NOTE,
"Sec done") ;
1933 cld_2d_sim->rewind() ;
1942 daq_dta *daq_tpx::handle_cld_sim(
int sec,
int row)
1945 int min_sec, max_sec ;
1946 int min_row, max_row ;
1954 else if((sec<1) || (sec>24))
return 0 ;
1962 max_row = sim_row_count ;
1964 else if((row<0) || (row>250))
return 0 ;
1966 min_row = max_row = row ;
1972 for(
int s=min_sec;s<=max_sec;s++) {
1974 LOG(DBG,
"start_evt(): sec %d",s) ;
1975 fcf_algo[s]->start_evt() ;
1978 for(
int r=min_row;r<=max_row;r++) {
1986 cld_sim->create(guess_bytes,(
char *)
"cld_sim",rts_id,DAQ_DTA_STRUCT(
daq_sim_cld)) ;
1990 daq_dta *sim =
get(
"adc_sim") ;
1993 LOG(ERR,
"%s: sector %d, row %d: you need to add simulated data first!",name,sec,row) ;
1999 while(sim->iterate()) {
2000 if((min_sec<=sim->sec) && (sim->sec<= max_sec) && (min_row<=sim->row) && (sim->row <= max_row)) ;
2004 if(fcf_algo[sim->sec]==0) {
2005 LOG(NOTE,
"No algo assigned for sector %d -- creating one!",sim->sec) ;
2006 fcf_algo[sim->sec] =
new tpxFCF ;
2007 fcf_algo[sim->sec]->config(0x3F,1,sim_row_count,sim_tpx_rowlen) ;
2008 fcf_algo[sim->sec]->run_compatibility = fcf_run_compatibility ;
2009 fcf_algo[sim->sec]->do_cuts = fcf_do_cuts ;
2011 fcf_algo[sim->sec]->apply_gains(sim->sec,gain_algo) ;
2013 fcf_algo[sim->sec]->start_evt() ;
2015 if(fcf_tmp_storage==0) {
2016 fcf_tmp_storage = (u_int *)valloc(FCF_TMP_BYTES) ;
2028 qsort(sim->sim_adc, sim->ncontent,
sizeof(sim->sim_adc[0]),cmpr_sim_adc) ;
2032 for(u_int i=0;i<sim->ncontent;i++) {
2033 a.adc[i] = sim->sim_adc[i].adc ;
2034 a.tb[i] = sim->sim_adc[i].tb ;
2040 fcf_algo[sim->sec]->do_pad(&a, sim->sim_adc) ;
2041 LOG(DBG,
"do_pad(): sec %d, row %d, pad %d: %d",sim->sec,a.row,a.pad,a.count) ;
2046 for(
int s=min_sec;s<=max_sec;s++) {
2049 int words = fcf_algo[s]->stage2(fcf_tmp_storage,FCF_TMP_BYTES) ;
2050 if(words<=0) continue ;
2052 LOG(DBG,
"Sector %d: %d words",s,words) ;
2053 u_int *p_buff = fcf_tmp_storage ;
2054 u_int *end_buff = p_buff + words ;
2056 while(p_buff < end_buff) {
2057 u_int row = *p_buff++ ;
2058 u_int cou = *p_buff++ ;
2062 u_int version = (row >> 16) ;
2069 int skip = fcf_algo[s]->fcf_decode(p_buff, cld + g_cou, version) ;
2078 cld_sim->finalize(g_cou,s,row) ;
2091 int daq_tpx::get_token(
char *addr,
int words)
2095 get_l2(addr, words, trgs, 1) ;
2100 LOG(ERR,
"Token 0 not allowed but I will try to use the other triggers...") ;
2112 static u_int swap_s12(u_int dta)
2114 u_int n_dta = (dta & 0xFFF00000) ;
2115 for(
int n=0;n<5;n++) {
2116 int dd = (dta>>(n*4)) & 0xF ;
2118 int b4 = (dd & 0x8)?1:0 ;
2119 int b3 = (dd & 0x4)?1:0 ;
2120 int b2 = (dd & 0x2)?1:0 ;
2121 int b1 = (dd & 0x1)?1:0 ;
2123 int sdd = (b3<<3)|(b4<<2)|(b2<<1)|(b1<<0) ;
2126 n_dta |= (sdd<<(n*4)) ;
2137 int daq_tpx::get_l2(
char *addr,
int words,
struct daq_trg_word *trgs,
int do_log)
2141 u_int collision = 0 ;
2144 int ret = tpx_get_start(addr, words, &rdo, do_log) ;
2146 LOG(ERR,
"get_l2: broken data!") ;
2150 LOG(DBG,
"rdo %d, rdo token %d, trg cou %d",rdo.rdo,rdo.token,rdo.trg_cou) ;
2153 for(u_int i=0;i<rdo.trg_cou;i++) {
2154 u_int dta = rdo.trg[i].data ;
2155 u_int marker = rdo.trg[i].csr >> 24 ;
2156 u_int rhic = rdo.trg[i].rhic_counter ;
2159 if(rdo.sector==12) {
2160 dta = swap_s12(dta) ;
2163 LOG(WARN,
"RDO %d: %d/%d: T %d, trg %d, daq %d: 0x%08X: marker 0x%X",rdo.rdo,i,rdo.trg_cou,
2177 if(rdo.rdo==1 && rdo.sector==1) {
2178 int delta = rhic - rdo.trg[0].rhic_counter ;
2179 LOG(TERR,
"RDO %d: trg %d/%d: dta 0x%08X, CSR 0x%08X, RHIC %u, delta %u",rdo.rdo,
2181 dta,rdo.trg[i].csr,rhic,
2187 if((marker==0) || (marker==0xEE)) {
2189 trgs[cou].t = dta & 0xFFF ;
2190 trgs[cou].daq = (dta >> 12) & 0xF ;
2191 trgs[cou].trg = (dta >> 16) & 0xF ;
2192 trgs[cou].rhic_delta = 0 ;
2193 trgs[cou].rhic = rhic ;
2194 trgs[cou].reserved[0] = 0xF0000000 | (0x0FFFFFFF & dta) ;
2198 switch(trgs[cou].trg) {
2207 LOG(ERR,
"RDO %d: T %d: prompt: bad trg: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2212 if(trgs[cou].t==0) {
2213 LOG(ERR,
"RDO %d: token 0 (prompt) -- ignoring: 0x%08X",rdo.rdo,dta) ;
2219 if((dta & 0x3000000) != 0x2000000) {
2220 LOG(ERR,
"RDO %d: T %d: prompt: BUSY overrun: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2226 LOG(ERR,
"RDO %d: duplicate prompt trigger",rdo.rdo) ;
2241 if(rdo.token != 4096) {
2243 LOG(NOTE,
"No triggers in event, making it %d",rdo.token) ;
2246 trgs[cou].t = rdo.token ;
2249 trgs[cou].rhic_delta = 0 ;
2250 trgs[cou].rhic = 0 ;
2251 trgs[cou].reserved[0] = 0 ;
2257 LOG(ERR,
"RDO %d: token %d? -- too many prompt contributions!",rdo.rdo,trgs[0].t) ;
2266 if(trgs[0].t == 0) {
2267 LOG(ERR,
"Token 0 in RDO %d: making it 4097",rdo.rdo) ;
2272 for(u_int i=0;i<rdo.trg_cou;i++) {
2273 u_int dta = rdo.trg[i].data ;
2274 u_int marker = rdo.trg[i].csr >> 24 ;
2275 u_int rhic = rdo.trg[i].rhic_counter ;
2277 if(rdo.sector==12) {
2278 dta = swap_s12(dta) ;
2285 trgs[cou].t = dta & 0xFFF ;
2286 trgs[cou].daq = (dta >> 12) & 0xF ;
2287 trgs[cou].trg = (dta >> 16) & 0xF ;
2288 trgs[cou].rhic = rhic ;
2289 trgs[cou].reserved[0] = 0x20000000 | (0x0FFFFFFF & dta) ;
2291 switch(trgs[cou].trg) {
2299 if((trgs[cou].t == 0x345) && (trgs[cou].daq == 2)) {
2302 if((trgs[cou].t == 1001) && (trgs[cou].daq == 3)) {
2313 if((dta & 0x03A00000) == 0x00800000) {
2318 if((dta & 0x3000000) != 0x2000000) {
2319 if(trgs[0].trg == 9) {
2320 LOG(NOTE,
"RDO %d: T %d: FIFO: BUSY overrun: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2323 LOG(ERR,
"RDO S%d:%d: T %d: FIFO: BUSY overrun: 0x%08X",rdo.sector,rdo.rdo,trgs[cou].t,dta) ;
2329 switch(trgs[cou].trg) {
2338 if(trgs[0].trg == 9) {
2339 LOG(NOTE,
"RDO %d: T %d: FIFO: bad trg_cmd: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2342 LOG(ERR,
"RDO %d: T %d: FIFO: bad trg_cmd: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2348 trgs[cou].reserved[0] = 0xA0000000 | (0x0FFFFFFF & dta) ;
2359 if(rhic >= collision) {
2360 trgs[cou].rhic_delta = rhic - collision ;
2363 trgs[cou].rhic_delta = -(collision - rhic) ;
2366 if(trgs[cou].rhic_delta == 1) {
2367 LOG(NOTE,
"RDO %d: T %d: FIFO: delta == 1: 0x%08X",rdo.rdo,trgs[cou].t,dta) ;
2368 if(trgs[0].trg == 9) {
2373 if((trgs[cou].t == 0)) {
2374 LOG(ERR,
"RDO %d: token 0 in L2 contribution 0x%08X -- skipping",rdo.rdo,dta) ;
2385 LOG(ERR,
" RDO %d: words %d",rdo.rdo,words) ;
2386 for(u_int i=0;i<rdo.trg_cou;i++) {
2387 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) ;
void version(std::ostream &os=std::cout)
print HepMC version