13 #include <rtsSystems.h>
17 #include <DAQ_READER/daq_dta.h>
24 #include <DAQ_TPX/daq_tpx.h>
25 #include <DAQ_TPX/tpxFCF_flags.h>
26 #include <TPC/rowlen.h>
28 #include <DAQ_ITPC/daq_itpc.h>
29 #include <DAQ_ITPC/itpcCore.h>
30 #include <DAQ_ITPC/itpcPed.h>
31 #include <DAQ_ITPC/itpcInterpreter.h>
32 #include <DAQ_ITPC/itpc_rowlen.h>
34 #include <DAQ_ITPC/itpcFCF.h>
36 #define VERSION 0x20180002
42 static double mark(
void)
44 struct timeval tmval ;
46 gettimeofday(&tmval,0) ;
48 return ((
double)tmval.tv_sec*1000000.0 + (
double)tmval.tv_usec) ;
51 static double delta(
double v)
56 itpc_fcf_c::itpc_fcf_c()
75 words_per_cluster = 2 ;
87 itpc_fcf_c::~itpc_fcf_c()
91 for(
int i=0;i<=MAX_SEC;i++) {
92 if(sec_gains[i]) free(sec_gains[i]) ;
102 free(row_pad_store) ;
108 int itpc_fcf_c::x_max(
int slice,
int y)
112 if(slice>45)
return -1 ;
113 return tpc_rowlen[slice] ;
120 else if(det_type == 1) {
122 if(slice>40)
return -1 ;
123 return itpc_rowlen[slice] ;
135 int itpc_fcf_c::x_min(
int slice,
int y)
140 if(y_is_timebin)
return 1 ;
147 int itpc_fcf_c::y_min()
152 int itpc_fcf_c::y_max()
156 if(y_is_timebin)
return 512 ;
159 if(y_is_timebin)
return 425 ;
170 int itpc_fcf_c::pad_to_x(
int pad,
int row)
174 if(y_is_timebin)
return pad ;
175 return (182/2 - tpc_rowlen[row]/2 + pad) ;
177 if(y_is_timebin)
return pad ;
178 return (120/2 - itpc_rowlen[row]/2 + pad) ;
190 itpc_fcf_c::gain_rp_t *itpc_fcf_c::sec_gains[MAX_SEC+1] ;
192 int itpc_fcf_c::get_bad(
int sec1,
int row1,
int pad1)
194 gain_rp_t (*gain_p)[MAX_PHYS_PAD+1] ;
196 if(sec_gains[sec1]==0)
return 0 ;
198 gain_p = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[sec1] ;
200 if(gain_p[row1][pad1].gain==0.0)
return 1 ;
205 float itpc_fcf_c::get_gain(
int sec1,
int row1,
int pad1)
207 if(row1==0 || pad1==0)
return 0.0 ;
209 gain_rp_t (*gain_p)[MAX_PHYS_PAD+1] ;
211 if(sec_gains[sec1]==0)
return 0.0 ;
213 gain_p = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[sec1] ;
215 return gain_p[row1][pad1].gain ;
219 float itpc_fcf_c::get_t0(
int sec1,
int row1,
int pad1)
221 if(row1==0 || pad1==0)
return 0.0 ;
223 gain_rp_t (*gain_p)[MAX_PHYS_PAD+1] ;
225 if(sec_gains[sec1]==0)
return 0.0 ;
227 gain_p = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[sec1] ;
229 return gain_p[row1][pad1].t0 ;
233 void itpc_fcf_c::set_gain(
int sec1,
int row1,
int pad1,
float gain)
237 gain_rp_t (*gain_p)[MAX_PHYS_PAD+1] ;
239 if(sec_gains[sec1]==0) return ;
241 gain_p = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[sec1] ;
243 gain_p[row1][pad1].gain = gain ;
246 u_char itpc_fcf_c::get_flags(
int sec1,
int row1,
int pad1)
248 if(row1==0 || pad1==0)
return 255 ;
250 gain_rp_t (*gain_p)[MAX_PHYS_PAD+1] ;
252 if(sec_gains[sec1]==0)
return 255 ;
254 gain_p = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[sec1] ;
256 return gain_p[row1][pad1].flags ;
260 void itpc_fcf_c::set_flags(
int sec1,
int row1,
int pad1, u_char flags)
263 gain_rp_t (*gain_p)[MAX_PHYS_PAD+1] ;
265 if(sec_gains[sec1]==0) return ;
267 gain_p = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[sec1] ;
269 gain_p[row1][pad1].flags = flags ;
272 void itpc_fcf_c::zap_fee(
int sec1,
int rdo1,
int port1)
276 int padplane_id = itpc->itpc_fee_map[sec1-1][rdo1-1][port1-1] ;
278 for(
int c=0;c<64;c++) {
282 itpc_ifee_to_rowpad(padplane_id,c,row,pad) ;
288 if(p2<x_max(row,0)) p2 = x_max(row,0) ;
290 set_gain(sec1,row,pad,0.0) ;
292 flags = get_flags(sec1,row,pad) ;
293 set_flags(sec1,row,pad,flags|3) ;
295 flags = get_flags(sec1,row,p1) ;
296 set_flags(sec1,row,p1,flags|2) ;
298 flags = get_flags(sec1,row,p2) ;
299 set_flags(sec1,row,p2,flags|1) ;
306 struct itpc_fcf_c::rp_t *itpc_fcf_c::get_row_pad(
int row,
int pad)
308 int max_pad_all = max_x + 1 ;
311 if(offline) s1_data_length = (1 + max_y) * 2 ;
313 else s1_data_length = 1 + max_y ;
315 if(row_pad_store==0) {
316 int max_row_all = max_slice + 1 ;
317 int chunks = max_row_all * max_pad_all ;
319 row_pad_store = (u_short *) valloc(chunks *s1_data_length *
sizeof(u_short)) ;
320 assert(row_pad_store) ;
321 memset(row_pad_store,0,chunks *s1_data_length *
sizeof(u_short)) ;
324 u_short *p = row_pad_store + ((row*max_pad_all) + pad)*s1_data_length ;
331 int itpc_fcf_c::init(
daq_dta *gain)
333 if(gain==0)
return -1 ;
338 while(gain->iterate()) {
340 int row = gain->row ;
342 if(sec_gains[s]==0) continue ;
343 if(row==0) continue ;
345 gain_rp_t (*sr)[MAX_PHYS_PAD+1] = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[s] ;
349 for(u_int p=0;p<gain->ncontent;p++) {
352 if(p>(u_int)x_max(row,0))
continue ;
356 sr[row][p].gain = gp[p].gain ;
357 sr[row][p].t0 = gp[p].t0 ;
360 if(gp[p].gain < 0.01) {
366 if(p2>x_max(row,0)) p2 = x_max(row,0) ;
369 sr[row][p].flags |= 3 ;
370 sr[row][p1].flags |= 2 ;
371 sr[row][p2].flags |= 2 ;
383 LOG(WARN,
"%d/%d bad channels",bad_ch,tot_ch) ;
390 int itpc_fcf_c::init(
int sec,
const char *fname)
394 gain_rp_t (*gain_p)[MAX_PHYS_PAD+1] ;
396 if(sec>MAX_SEC)
return -1 ;
403 s_min = s_max = sec ;
406 const char *s_type, *s_orient ;
451 if(y_is_timebin) s_orient=
"across-rows" ;
452 else s_orient =
"across-timebins" ;
454 LOG(INFO,
"%s: det_type %s(%s): max_x(pad) %d, max_y(timebin) %d, max_slice(row) %d",__FUNCTION__,
456 max_x, max_y, max_slice) ;
459 for(
int s=s_min;s<=s_max;s++) {
461 if(sec_gains[s]) continue ;
463 sec_gains[s] = (gain_rp_t *) malloc((MAX_PHYS_ROW+1)*(MAX_PHYS_PAD+1)*
sizeof(gain_rp_t)) ;
466 gain_p = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[s] ;
468 for(
int r=1;r<=MAX_PHYS_ROW;r++) {
470 for(
int p=1;p<=MAX_PHYS_PAD;p++) {
472 gain_p[r][p].gain = 1.0 ;
473 gain_p[r][p].t0 = 0.0 ;
477 if(p==1 || p==x_max(r,0)) gain_p[r][p].flags = 0x2 ;
478 else gain_p[r][p].flags = 0 ;
489 if(fname==0)
return 0 ;
494 FILE *f = fopen(fname,
"r") ;
496 LOG(ERR,
"%s: %s [%s]",__FUNCTION__,fname,strerror(errno)) ;
500 LOG(INFO,
"Using gain file %s",fname) ;
508 int sec,rdo,port,ch,row,pad ;
513 if(fgets(buff,
sizeof(buff),f)==0) continue ;
515 if(buff[0]==
'#') continue ;
516 if(strlen(buff)<1) continue ;
518 int ret = sscanf(buff,
"%d %d %d %d %d %d %f %f",&sec,&rdo,&port,&ch,&row,&pad,&g,&t) ;
520 if(ret != 8) continue ;
522 if(sec_gains[sec]==0) continue ;
525 zap_fee(sec,rdo,port) ;
527 LOG(TERR,
"%d %d %d kill FEE implemented",sec,rdo,port) ;
532 gain_rp_t (*sr)[MAX_PHYS_PAD+1] = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[sec] ;
534 sr[row][pad].gain = g ;
535 sr[row][pad].t0 = t ;
545 if(p2>x_max(row,0)) p2 = x_max(row,0) ;
548 sr[row][pad].flags |= 3 ;
549 sr[row][p1].flags |= 2 ;
550 sr[row][p2].flags |= 2 ;
559 if(ch_bad) LOG(WARN,
"...with %d/%d bad channels, %d bad_fees",ch_bad,ch_all,fee_bad) ;
560 else LOG(TERR,
"...with %d/%d no bad channels, %d bad_fees",ch_bad,ch_all,fee_bad) ;
571 void itpc_fcf_c::event_start()
574 f_stat.s1_found = 0 ;
577 for(
int r=0;r<=max_slice;r++) {
578 for(
int p=0;p<=max_x;p++) {
579 get_row_pad(r,p)->s1_len = 0 ;
587 int itpc_fcf_c::fcf_decode(u_int *p_buff,
daq_sim_cld_x *dc, u_int version)
592 words_used = fcf_decode(p_buff,&cld,version) ;
594 memcpy(&dc->cld,&cld,
sizeof(cld)) ;
596 p_buff += words_used ;
598 dc->track_id = *p_buff & 0xFFFF ;
599 dc->quality = (*p_buff)>>16 ;
603 dc->max_adc = *p_buff & 0xFFFF ;
604 dc->pixels = (*p_buff)>>16 ;
606 return words_used + 2 ;
610 int itpc_fcf_c::fcf_decode(u_int *p_buff,
daq_cld *dc, u_int version)
613 int p1,p2,t1,t2,cha,fla ;
617 p_tmp = *p_buff & 0xFFFF ;
620 t_tmp = *p_buff >> 16 ;
623 p = (double)(p_tmp & 0x3FFF) / 64.0 ;
624 t = (double)(t_tmp & 0x7FFF) / 64.0 ;
627 if(p_tmp & 0x8000) fla |= FCF_MERGED ;
628 if(p_tmp & 0x4000) fla |= FCF_DEAD_EDGE ;
629 if(t_tmp & 0x8000) fla |= FCF_ONEPAD ;
633 cha = *p_buff >> 16 ;
638 fla |= FCF_BIG_CHARGE ;
639 cha = (cha & 0x7FFF) * 1024 ;
645 if(cha > 0xFFFF) cha = 0xFFFF ;
649 p_tmp = *p_buff & 0xFFFF ;
651 if(p_tmp & 0x8000) fla |= FCF_ROW_EDGE ;
652 if(p_tmp & 0x4000) fla |= FCF_BROKEN_EDGE ;
655 t2 = (p_tmp >> 4) & 0xF ;
658 p1 = (p_tmp >> 8) & 0x7 ;
659 p2 = (p_tmp >> 11) & 0x7 ;
693 int itpc_fcf_c::do_ch(
int fee_id,
int fee_ch, u_int *
data,
int words)
704 int word32 = (words/3) + (words%3?1:0) ;
706 itpc_ifee_to_rowpad(fee_id, fee_ch, row, pad) ;
708 gain_rp_t (*gain_row_pad)[MAX_PHYS_PAD+1] = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[sector_id] ;
710 if(row==0)
goto err_ret ;
715 if(gain_row_pad[row][pad].flags & 1) {
722 if((word32*3)>=MAX_TB_EVER) {
723 LOG(ERR,
"%d:#%d: FEE %d:%d, words %d",rdo,port,fee_id,fee_ch,words) ;
729 for(
int i=0;i<word32;i++) {
734 LOG(ERR,
"%d:#%d: FEE %d:%d, words %d",rdo,port,fee_id,fee_ch,words) ;
739 tb_buff[t_cou++] = (d>>20) & 0x3FF ;
740 tb_buff[t_cou++] = (d>>10) & 0x3FF ;
741 tb_buff[t_cou++] = d & 0x3FF ;
747 s1_data = get_row_pad(row,pad)->s1_data ;
752 for(
int i=0;i<words;) {
753 int t_cou = tb_buff[i++] ;
754 int t_start = tb_buff[i++] ;
755 int t_stop = t_start + t_cou - 1 ;
758 if(t_start <= t_stop_last) {
759 LOG(NOTE,
"%d:#%d: FEE %d:%d, words %d: %d <= %d",rdo,port,fee_id,fee_ch,words,t_start,t_stop_last) ;
765 LOG(NOTE,
"%d:#%d: FEE %d:%d, words %d: %d > 511",rdo,port,fee_id,fee_ch,words,t_stop) ;
771 t_stop_last = t_stop ;
777 *s1_data++ = t_start ;
781 for(
int t=t_start;t<=t_stop;t++) {
789 else if((t>=26)&&(t<=31)) *s1_data = 0 ;
791 if(tb_buff[i]<=4) *s1_data = 0 ;
792 else *s1_data = tb_buff[i] ;
799 *s1_data++ = tb_buff[i++] ;
805 *s1_data++ = 0xFFFF ;
809 s_count = s1_data - get_row_pad(row,pad)->s1_data ;
810 if(s_count >= s1_data_length) {
812 LOG(ERR,
"In trouble at RP %d:%d",row,pad) ;
816 if(s_count >= (
int)f_stat.max_s1_len) {
817 f_stat.max_s1_len = s_count ;
820 f_stat.s1_found += seq_cou ;
827 get_row_pad(row,pad)->s1_len = seq_cou ;
841 int itpc_fcf_c::do_ch_sim(
int row,
int pad, u_short *tb_buff, u_short *track_id)
850 u_short *p_start = 0 ;
853 words_per_cluster = 4 ;
855 track_dta = (u_short *)malloc(MAX_BLOB_SIZE*2) ;
862 if(row==0)
goto err_ret ;
865 gain_rp_t (*gain_row_pad)[MAX_PHYS_PAD+1] = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[sector_id] ;
866 if(gain_row_pad[row][pad].flags & 1) {
872 s1_data = get_row_pad(row,pad)->s1_data ;
878 if(det_type==1 && y_is_timebin) {
882 for(
int i=0;i<512;i++) {
883 if(i>425) tb_buff[i] = 0 ;
884 else if((i>=26)&&(i<=31)) tb_buff[i]= 0 ;
886 if(tb_buff[i]<=4) tb_buff[i] = 0 ;
892 for(
int i=0;i<MAX_TB_EVER;i++) {
904 if(tb_buff[i]==0xFFFF) {
908 *s1_data++ = tb_buff[i] ;
910 *s1_data++ = track_id[i] ;
928 *s1_data++ = 0xFFFF ;
932 s_count = s1_data - get_row_pad(row,pad)->s1_data ;
933 if(s_count >= s1_data_length) {
934 LOG(ERR,
"Data too big at RP %d:%d",row,pad) ;
940 if(s_count >= (
int)f_stat.max_s1_len) {
941 f_stat.max_s1_len = s_count ;
944 f_stat.s1_found += seq_cou ;
951 get_row_pad(row,pad)->s1_len = seq_cou ;
958 int itpc_fcf_c::do_fcf(
void *v_store,
int bytes)
960 out_store = (u_int *) v_store ;
961 max_out_bytes = bytes ;
963 u_int *store_start = out_store ;
969 for(
int row=1;row<=max_slice;row++) {
973 u_int *row_store = out_store++ ;
979 do_blobs_stage1(row) ;
982 f_stat.tm[1] += tmx ;
987 do_blobs_stage2(row) ;
990 f_stat.tm[2] += tmx ;
993 found_ints = do_blobs_stage3(row) ;
996 f_stat.tm[3] += tmx ;
1001 *row_store++ = (words_per_cluster<<16)|row ;
1002 *row_store++ = version ;
1003 *row_store++ = found_ints ;
1005 out_store += found_ints ;
1008 out_store = row_store ;
1011 int ints_used = out_store - store_start ;
1013 int ints_needed = (max_out_bytes/4)-400 ;
1015 if(ints_used && (ints_used>ints_needed)) {
1016 LOG(ERR,
"not enough ints %d vs %d",out_store-store_start,max_out_bytes/4) ;
1022 if(f_stat.s1_found > f_stat.max_s1_found) {
1023 f_stat.max_s1_found = f_stat.s1_found ;
1026 return (out_store-store_start) ;
1029 void itpc_fcf_c::run_start()
1032 LOG(NOTE,
"%s: %d",__PRETTY_FUNCTION__,my_id) ;
1034 memset(&f_stat,0,
sizeof(f_stat)) ;
1036 for(
int r=0;r<=max_slice;r++) {
1037 for(
int p=0;p<=max_x;p++) {
1038 get_row_pad(r,p)->s1_len = 0 ;
1045 void itpc_fcf_c::run_stop()
1051 for(
int i=0;i<10;i++) {
1052 f_stat.tm[i] /= f_stat.evt_cou ;
1055 LOG(INFO,
"itpcFCF: %d: events %d, times %f %f %f %f",my_id,
1057 f_stat.tm[0],f_stat.tm[1],f_stat.tm[2],f_stat.tm[3]) ;
1059 LOG(INFO,
" times %f %f %f %f",
1060 f_stat.tm[4],f_stat.tm[5],f_stat.tm[6],f_stat.tm[7]) ;
1063 LOG(INFO,
" max s1_found %d, s1_len %d, blob_cou %d",f_stat.max_s1_found,f_stat.max_s1_len,f_stat.max_blob_cou) ;
1064 if(f_stat.toobigs) {
1065 LOG(WARN,
" toobigs %d",f_stat.toobigs) ;
1071 int itpc_fcf_c::do_blobs_stage1(
int row)
1074 int late_merges = 0 ;
1080 int rl = x_max(row,0) ;
1086 for(
int p=1;p<=rl;p++) {
1087 int t1_cou, t1_lo, t1_hi ;
1088 int t2_cou, t2_lo, t2_hi ;
1092 rp_t *rp1 = get_row_pad(row,p) ;
1094 if(rp1->s1_len==0) continue ;
1098 u_short *d1 = rp1->s1_data ;
1100 for(
int i=0;i<rp1->s1_len;i++) {
1103 rp_t *rp2 = get_row_pad(row,p+1) ;
1112 t1_hi = t1_lo + t1_cou - 1 ;
1121 if(p==rl)
goto sweep_unmerged ;
1125 for(
int j=0;j<rp2->s1_len;j++) {
1131 t2_hi = t2_lo + t2_cou - 1 ;
1133 if(offline) d2 += t2_cou * 2 ;
1138 if(t1_lo > t2_hi) merge = 0 ;
1139 else if(t2_lo > t1_hi) merge = 0 ;
1142 if(merge==0) continue ;
1147 if(*ix1_p != *ix2_p) {
1153 if(blob_ix[*ix1_p] < blob_ix[*ix2_p]) {
1154 blob_ix[*ix2_p] = blob_ix[*ix1_p] ;
1156 blob[blob_ix[*ix2_p]].merges++ ;
1159 blob_ix[*ix1_p] = blob_ix[*ix2_p] ;
1161 blob[blob_ix[*ix1_p]].merges++ ;
1178 blob_ix[blob_cou] = blob_cou ;
1179 blob[blob_cou].merges = 0 ;
1191 blob_ix[blob_cou] = blob_cou ;
1192 blob[blob_cou].merges = 0 ;
1201 if(blob_cou > f_stat.max_blob_cou) {
1202 f_stat.max_blob_cou = blob_cou ;
1212 int itpc_fcf_c::do_blobs_stage2(
int row)
1218 int rl = x_max(row,0) ;
1222 for(
int i=0;i<blob_cou;i++) {
1223 blob[i].seq_cou = 0 ;
1226 blob[i].p1 = 0xFFFF ;
1229 blob[i].t1 = 0xFFFF ;
1235 for(
int p=1;p<=rl;p++) {
1236 rp_t *rp = get_row_pad(row,p) ;
1243 u_short *d = rp->s1_data ;
1245 for(
int i=0;i<rp->s1_len;i++) {
1246 int t_cou, t_start, t_stop ;
1251 int b_ix = blob_ix[ix] ;
1255 LOG(WARN,
"Can't be: %d %d, RP %d:%d",b_ix,ix,row,p) ;
1259 LOG(ERR,
"Can't be: %d %d, RP %d:%d",b_ix,ix,row,p) ;
1262 blob_t *bl = &(blob[b_ix]) ;
1267 t_stop = t_start + t_cou - 1 ;
1269 if(offline) d += 2*t_cou ;
1275 if(p > bl->p2) bl->p2 = p ;
1276 if(p < bl->p1) bl->p1 = p ;
1278 if(t_stop > bl->t2) bl->t2 = t_stop ;
1279 if(t_start < bl->t1) bl->t1 = t_start ;
1288 for(
int i=0;i<blob_cou;i++) {
1289 if(blob[i].seq_cou == 0) continue ;
1291 int dp = blob[i].p2 - blob[i].p1 + 1 ;
1292 int dt = blob[i].t2 - blob[i].t1 + 1;
1295 blob[i].seq_cou = 0 ;
1296 LOG(ERR,
"%d: dp %d",rdo,dp) ;
1301 blob[i].seq_cou = 0 ;
1302 LOG(ERR,
"%d: dt %d",rdo,dt) ;
1316 blob[i].seq_cou = 0 ;
1323 if(dt<=1 || (dt<=3 && y_is_timebin)) {
1326 blob[i].seq_cou = 0 ;
1327 blob[i].p1 *= 2000 ;
1331 u_int bytes_for_blob = (dp+2)*(dt+2)*2*2 ;
1333 if(bytes_for_blob >
sizeof(smooth_dta)) {
1339 blob[i].seq_cou = 0 ;
1340 blob[i].p1 *= 3000 ;
1351 LOG(TERR,
"Blobs OK %d/%d in row %d",blob_ok,blob_cou,row) ;
1353 for(
int i=0;i<blob_cou;i++) {
1356 LOG(TERR,
"Blob %d: seq %d: flags 0x%X: pad %d:%d, tb %d:%d",i,blob[i].seq_cou,blob[i].flags,
1357 blob[i].p1,blob[i].p2,blob[i].t1,blob[i].t2) ;
1366 int itpc_fcf_c::do_blobs_stage3(
int row)
1373 u_int *obuff = (u_int *)out_store ;
1378 gain_rp_t (*gain_row_pad)[MAX_PHYS_PAD+1] = (gain_rp_t (*)[MAX_PHYS_PAD+1]) sec_gains[sector_id] ;
1381 int clusters_cou = 0 ;
1385 for(
int ix=0;ix<blob_cou;ix++) {
1386 if(blob[ix].seq_cou==0) continue ;
1394 blob[ix].tot_charge = 0 ;
1395 blob[ix].pixels = 0 ;
1398 int dt = blob[ix].t2 - blob[ix].t1 + 1 ;
1399 int dp = blob[ix].p2 - blob[ix].p1 + 1 ;
1406 printf(
"...bytes %lu vs %lu\n",dt_2*(dp+2)*
sizeof(
short)*2,
sizeof(smooth_dta)) ;
1408 memset(smooth_dta,0,dt_2*(dp+2)*
sizeof(
short)*2) ;
1411 memset(track_dta,0xFF,dt_2*(dp+2)*
sizeof(
short)*2) ;
1414 dta_s = smooth_dta + (dp+2)*dt_2 ;
1416 int p1 = blob[ix].p1 ;
1417 int p2 = blob[ix].p2 ;
1420 printf(
"BLOB START: %d X %d\n",dp,dt) ;
1423 for(
int p=p1;p<=p2;p++) {
1424 rp_t *rp = get_row_pad(row,p) ;
1426 if(rp->s1_len==0) continue ;
1428 u_short *d = rp->s1_data ;
1430 int pp = p - p1 + 1 ;
1432 short *adc_p = smooth_dta + dt_2 * pp ;
1433 u_short *track_p = track_dta + dt_2 * pp ;
1435 for(
int j=0;j<rp->s1_len;j++) {
1436 int t_cou, t_start ;
1440 int b_ix = blob_ix[ixx] ;
1448 if(offline) d += 2*t_cou ;
1454 blob_t *bl = &(blob[ix]) ;
1458 for(
int t=0;t<t_cou;t++) {
1459 u_short adc = *d++ ;
1461 u_short tb = t_start++ ;
1463 int ttb = tb - bl->t1 + 1 ;
1467 track_p[ttb] = *d++ ;
1470 bl->tot_charge += adc ;
1476 if(blob[ix].pixels == 0) {
1478 blob[ix].seq_cou = 0 ;
1482 f_stat.tm[4] += delta(tm) ;
1485 for(
int i=1;i<=dp;i++) {
1486 for(
int j=1;j<=dt;j++) {
1487 short *adc_p = smooth_dta + dt_2 * i + j ;
1489 short sum = *adc_p ;
1497 for(
int ii=-1;ii<=1;ii++) {
1498 for(
int jj=-1;jj<=1;jj++) {
1501 if(iii==1 || iii==-1) continue ;
1503 short *adc_p = smooth_dta + dt_2 * (i+ii) + (j+jj) ;
1511 adc_p = dta_s + dt_2 * i + j ;
1517 f_stat.tm[5] += delta(tm) ;
1525 for(
int i=1;i<=dp;i++) {
1526 for(
int j=1;j<=dt;j++) {
1527 short *adc_p = dta_s + dt_2 * i + j ;
1529 short adc = *adc_p - 5 ;
1532 if(adc < 1) continue ;
1534 for(
int ii=-1;ii<=1;ii++) {
1535 for(
int jj=-1;jj<=1;jj++) {
1536 if(ii==0 && jj==0) continue ;
1538 short *adc_p = dta_s + dt_2 * (i+ii) + (j+jj) ;
1539 short s_adc = *adc_p ;
1541 if(adc < s_adc)
goto skip_calc ;
1542 if(s_adc < 0)
goto skip_calc ;
1548 for(
int ii=-1;ii<=1;ii++) {
1549 for(
int jj=-1;jj<=1;jj++) {
1550 short *adc_p = dta_s + dt_2 * (i+ii) + (j+jj) ;
1561 peaks[peaks_cou].i = i ;
1562 peaks[peaks_cou].j = j ;
1563 peaks[peaks_cou].adc = adc ;
1566 if(peaks_cou >= MAX_PEAKS) {
1567 LOG(WARN,
"Too many peaks %d/%d in row %d",peaks_cou,MAX_PEAKS,row) ;
1581 f_stat.tm[6] += delta(tm) ;
1588 if(peaks_cou <= 1) {
1589 double f_charge = 0.0 ;
1590 double f_t_ave = 0.0 ;
1591 double f_p_ave = 0.0 ;
1596 u_short track_id = 0xFFFF ;
1597 u_short quality = 0 ;
1600 u_int i_charge = 0 ;
1601 u_int t_charge = 0 ;
1603 for(
int i=1;i<=dp;i++) {
1605 short *adc_p = smooth_dta + dt_2 * i + 1;
1606 u_short *track_p = track_dta + dt_2 * i + 1 ;
1609 for(
int j=1;j<=dt;j++) {
1611 int adc = *adc_p++ ;
1612 u_short t_id = *track_p++ ;
1632 for(
int i=1;i<=dp;i++) {
1634 short *adc_p = smooth_dta + dt_2 * i + 1;
1635 u_short *track_p = track_dta + dt_2 * i + 1 ;
1638 for(
int j=1;j<=dt;j++) {
1640 int adc = *adc_p++ ;
1641 u_short t_id = *track_p++ ;
1643 if(t_id == track_id) t_charge += adc ;
1649 quality = (u_short)(100.0*(
double)t_charge/(double)i_charge+0.5) ;
1656 for(
int i=1;i<=dp;i++) {
1657 int pad = p1 + i - 1 ;
1661 if(use_gain) flags |= gain_row_pad[row][pad].flags ;
1663 short *adc_p = smooth_dta + dt_2 * i + 1;
1665 u_int i_charge = 0 ;
1668 for(
int j=1;j<=dt;j++) {
1670 int adc = *adc_p++ ;
1675 i_t_ave += j * adc ;
1678 if(i_charge==0) continue ;
1683 double corr_charge = (double) i_charge * gain_row_pad[row][pad].gain ;
1685 f_charge += corr_charge ;
1686 f_t_ave += i_t_ave * gain_row_pad[row][pad].gain + gain_row_pad[row][pad].t0 * corr_charge ;
1687 f_p_ave += i * corr_charge ;
1690 f_charge += (double) i_charge ;
1691 f_p_ave += i * (double) i_charge ;
1692 f_t_ave += (double) i_t_ave ;
1700 f_t_ave /= f_charge ;
1701 f_p_ave /= f_charge ;
1704 f_t_ave += blob[ix].t1 - 1 ;
1709 u_int time_c = (u_int)(f_t_ave * 64.0 + 0.5) ;
1710 u_int pad_c = (u_int)(f_p_ave * 64.0 + 0.5) ;
1711 u_int cha = (u_int)(f_charge + 0.5) ;
1719 int p_lo = (pad_c/64) - blob[ix].p1 ;
1720 int p_hi = blob[ix].p2 - (pad_c/64) ;
1722 if(p_lo < 0) p_lo = 0 ;
1723 if(p_hi < 0) p_hi = 0 ;
1724 if(p_lo > 7) p_lo = 7 ;
1725 if(p_hi > 7) p_hi = 7 ;
1727 tmp_fl = (p_lo<<8)|(p_hi<<11) ;
1729 int t_lo = (time_c/64) - blob[ix].t1 ;
1730 int t_hi = blob[ix].t2 - (time_c/64) ;
1732 if(t_lo < 0) t_lo = 0 ;
1733 if(t_hi < 0) t_hi = 0 ;
1734 if(t_lo > 15) t_lo = 15 ;
1735 if(t_hi > 15) t_hi = 15 ;
1737 tmp_fl |= (t_hi << 4) | t_lo ;
1739 if(cha > 0x7FFF) cha = 0x8000 | (cha/1024) ;
1741 if(flags & 3) tmp_fl |= 0x8000 ;
1745 *obuff++ = (time_c << 16) | pad_c ;
1746 *obuff++ = (cha << 16) | tmp_fl ;
1748 if(words_per_cluster>2) {
1749 *obuff++ = (quality<<16)|track_id ;
1751 if(words_per_cluster>3) {
1752 *obuff++ = (pixels<<16)|adc_max ;
1767 for(
int pk=0;pk<peaks_cou;pk++) {
1768 double f_charge = 0.0 ;
1769 double f_t_ave = 0.0 ;
1770 double f_p_ave = 0.0 ;
1775 u_short track_id = 0xFFFF ;
1776 u_short quality = 0 ;
1778 ip1 = peaks[pk].i - 1 ;
1779 if(ip1 < 1) ip1 = 1 ;
1780 ip2 = peaks[pk].i + 1 ;
1781 if(ip2 > dp) ip2 = dp ;
1783 it1 = peaks[pk].j - 2 ;
1784 if(it1 < 1) it1 = 1 ;
1786 it2 = peaks[pk].j + 2 ;
1787 if(it2 > dt) it2 = dt ;
1790 printf(
"from %d:%d %d:%d\n",ip1,ip2,it1,it2) ;
1792 for(
int i=ip1;i<=ip2;i++) {
1793 int pad = p1 + i - 1 ;
1796 flags |= gain_row_pad[row][pad].flags ;
1799 short *adc_p = smooth_dta + dt_2 * i + it1 ;
1801 u_int i_charge = 0 ;
1806 for(
int j=it1;j<=it2;j++) {
1808 int adc = *adc_p++ ;
1812 printf(
"%d %d = %d\n",i,j,adc) ;
1817 if(adc>adc_max) adc_max = adc ;
1820 i_t_ave += j * adc ;
1823 if(i_charge==0) continue ;
1826 double corr_charge = (double) i_charge * gain_row_pad[row][pad].gain ;
1828 f_charge += corr_charge ;
1829 f_t_ave += (double) i_t_ave * gain_row_pad[row][pad].gain + gain_row_pad[row][pad].t0 * corr_charge ;
1830 f_p_ave += i * corr_charge ;
1833 f_charge += (double) i_charge ;
1834 f_p_ave += i * (double)i_charge ;
1835 f_t_ave += (double) i_t_ave ;
1840 if(f_charge < 0.1) continue ;
1842 f_t_ave /= f_charge ;
1843 f_p_ave /= f_charge ;
1846 f_t_ave += blob[ix].t1 - 1 ;
1851 u_int time_c = (u_int)(f_t_ave * 64.0 + 0.5) ;
1852 u_int pad_c = (u_int)(f_p_ave * 64.0 + 0.5) ;
1853 u_int cha = (u_int)(f_charge + 0.5) ;
1865 tmp_fl = (p_lo<<8)|(p_hi<<11) ;
1870 tmp_fl |= (t_hi << 4) | t_lo ;
1872 if(cha > 0x7FFF) cha = 0x8000 | (0xFFFF & (cha/1024)) ;
1876 if(flags & 3) tmp_fl |= 0x8000 ;
1881 *obuff++ = (time_c << 16) | pad_c ;
1882 *obuff++ = (cha << 16) | tmp_fl ;
1884 if(words_per_cluster > 2) {
1885 *obuff++ = (quality<<16)|track_id ;
1887 if(words_per_cluster > 3) {
1888 *obuff++ = (pixels<<16)|adc_max ;
1902 f_stat.tm[7] += delta(tm) ;
1905 printf(
"BLOB start: row %d, peaks %d: %d:%d, %d:%d\n",row,peaks_cou,blob[ix].p1,blob[ix].p2,blob[ix].t1,blob[ix].t2) ;
1907 for(
int j=1;j<=dt;j++) {
1909 printf(
"TB %3d ",blob[ix].t1+j-1) ;
1910 for(
int i=1;i<=dp;i++) {
1911 short *adc_s_p = dta_s + dt_2 * i + j ;
1913 short *adc_p = smooth_dta + dt_2 * i + j ;
1916 printf(
"%s%4d(%4d)%s ",ANSI_RED,*adc_p,*adc_s_p,ANSI_RESET) ;
1919 printf(
"%4d(%4d) ",*adc_p,*adc_s_p) ;
1930 return (obuff - out_store) ;
1933 int itpc_fcf_c::do_row_check(
int row)
1939 int rl = x_max(row,0) ;
1941 for(
int p=1;p<=rl;p++) {
1944 rp_t *rp = get_row_pad(row,p) ;
1946 if(rp->s1_len==0) continue ;
1948 u_short *d1 = rp->s1_data ;
1950 for(
int i=0;i<rp->s1_len;i++) {
1956 if(offline) d1 += 2*t1_cou ;
1959 if(*ix1_p==0 || *ix1_p==0xFFFF) {
1960 LOG(ERR,
"sequence unassigned %d:%d %d:%d = %u [%d]",p,rl,i,rp[p].s1_len,*ix1_p,t1_lo) ;
1969 for(
int i=1;i<blob_cou;i++) {
1973 for(
int i=1;i<blob_cou;i++) {
1974 blob[blob_ix[i]].cou++ ;
1977 for(
int i=1;i<blob_cou;i++) {
1978 LOG(DBG,
"blob_ix[%d] = %d",i,blob_ix[i]) ;
1982 for(
int i=1;i<blob_cou;i++) {
1984 LOG(DBG,
" Blob %d: %d",i,blob[i].cou) ;
1989 for(
int i=1;i<max_ix;i++) {
1990 if(blob[i].merges) {
1991 LOG(DBG,
"blob %d: merged %d",i,blob[i].merges) ;
1996 LOG(DBG,
"Final blob count %d/%d",max_ix,blob_cou) ;