9 #include <TPC/rowlen.h>
10 #include <DAQ_TPX/tpxCore.h>
11 #include <DAQ_TPX/tpxFCF_flags.h>
12 #include <DAQ_TPX/tpxGain.h>
14 #include "tpxFCF_2D.h"
16 #include <DAQ_READER/daq_dta_structs.h>
22 #define PROFILER(x) (x)
24 #define likely(x) __builtin_expect((x),1)
25 #define unlikely(x) __builtin_expect((x),0)
28 #define DTA(i,j) (dta + (dt_2)*(i) + (j))
29 #define DTA_T(i,j) (dta_t + (dt_2)*(i) + (j))
30 #define DTA_S(i,j) (dta_s + (dt_2)*(i) + (j))
31 #define DTA_ID(i,j) (dta_id + (dt_2)*(i) + (j))
33 static inline u_int get10(u_int *l, u_int p)
41 ret = (l[-1] & 0xFFC00) >> 10 ;
44 ret = (l[-1] & 0x3FF) ;
47 ret = (l[0] & 0xFFC00) >> 10 ;
75 if((lo & 0xCFF00000) || (hi & 0xCFF00000)) {
81 if((hi & 0xFFFC0) != 0xAAA80) {
86 if((lo & 0x0F000) != 0x0A000) {
92 a->id = (lo & 0xFF0) >> 4 ;
98 tpx_from_altro(fcf->rdo-1,a->id,a->ch,rrow,ppad) ;
104 wc = ((hi&0x3F)<<4) | ((lo&0xF0000)>>16) ;
107 if((wc > 529) || (wc < 0)) {
115 if(fcf->is_pad_valid(rrow,ppad)==0)
return h ;
120 while(l10 % 4) l10++ ;
138 *fcf->data_raw_p++ = rrow ;
139 *fcf->data_raw_p++ = ppad ;
141 short *remember_data_raw_p = fcf->data_raw_p ;
146 int tb_cou = get10(h,p10++) ;
147 int tb_last = get10(h,p10++) ;
151 if((tb_cou > wc) || (tb_cou <= 0) || (tb_cou > 512)) {
156 if((tb_last >= 512) || (tb_last < 0) || (tb_last >= tb_prev)) {
162 if((a->count >= 512)) {
167 tb_prev = tb_last - tb_cou ;
169 *fcf->data_raw_p++ = tb_cou ;
170 *fcf->data_raw_p++ = -1 ;
171 *fcf->data_raw_p++ = tb_last ;
174 for(;tb_last>tb_prev;tb_last--) {
176 u_short adc = get10(h,p10++) ;
177 *fcf->data_raw_p++ = adc ;
187 fcf->p_row_pad[rrow][ppad] = remember_data_raw_p ;
188 *fcf->data_raw_p++ = 0 ;
189 *fcf->data_raw_p = 0 ;
200 int tpxFCF_2D::do_print(
int row)
202 printf(
"++++ Doing row %2d\n",row) ;
204 for(
int p=1;p<=tpx_rowlen[row];p++) {
205 short *b1 = p_row_pad[row][p] ;
208 printf(
"row %d, pad %d -- empty\n",row,p) ;
215 short tb_cou = *b1++ ;
218 printf(
"row %d, pad %d -- done\n",row,p) ;
226 short tb_hi = *b1++ ;
228 printf(
"row %d, pad %d, tb_cou %d, ix %d, tb_hi %d\n",row,p,tb_cou,ix,tb_hi) ;
230 for(
int i=0;i<tb_cou;i++) {
231 printf(
" adc %4d\n",*b1++) ;
235 printf(
"Row %2d, pad %3d: seq_cou %d\n",row,p,seq_cou) ;
243 int tpxFCF_2D::stage_2d(u_int *buff,
int max_bytes)
245 u_int *locbuff = buff ;
246 u_int *startbuff = buff ;
249 int gprof0 = PROFILER(0) ;
251 for(row=1;row<=45;row++) {
257 if(gain_storage[sector-1][row] ==0) continue ;
262 int gprof1 = PROFILER(gprof0) ;
267 } late_merge[MAX_LATE_MERGE] ;
269 int late_merge_cou = 0 ;
271 u_int *row_cache = locbuff++ ;
272 u_int *clust_count_cache = locbuff++ ;
273 u_int *outbuff = locbuff ;
277 short b1_cou, t1_lo, t1_hi ;
278 short b2_cou, t2_lo, t2_hi ;
283 printf(
"Doing row %2d\n",row) ;
285 for(
int p=1;p<=tpx_rowlen[row];p++) {
286 short *b1 = p_row_pad[row][p] ;
289 printf(
"row %d, pad %d -- empty\n",row,p) ;
296 short tb_cou = *b1++ ;
300 printf(
"row %d, pad %d -- done\n",row,p) ;
310 short tb_hi = *b1++ ;
312 printf(
"row %d, pad %d, tb_cou %d, ix %d, tb_hi %d\n",row,p,tb_cou,ix,tb_hi) ;
314 for(
int i=0;i<tb_cou;i++) {
315 printf(
" adc %4d\n",*b1++) ;
322 printf(
"Row %2d, pad %3d: seq_cou %d\n",row,p,seq_cou) ;
327 for(
int p=1;p<tpx_rowlen[row];p++) {
328 short *b1 = p_row_pad[row][p] ;
336 tpx_to_altro(row,p,rr,aa,cc) ;
338 LOG(ERR,
"Huh? rdo %d: rp %d:%d",rdo,row,p) ;
344 if(get_static(row,p)->f & FCF_KILLED_PAD) {
345 LOG(ERR,
"Killed pad in data: sec %d, rp %d:%d; count %d",sector,row,p,*b1) ;
356 if(b1_cou == 0) break ; ;
360 t1_lo = t1_hi - b1_cou + 1 ;
368 short *b2 = p_row_pad[row][p+1] ;
371 goto sweep_unmerged ;
376 tpx_to_altro(row,p+1,rr,aa,cc) ;
379 goto sweep_unmerged ;
383 if(get_static(row,p+1)->f & FCF_KILLED_PAD) {
384 LOG(ERR,
"Killed pad in data: sec %d, rp %d:%d; count %d",sector,row,p+1,*b2) ;
385 goto sweep_unmerged ;
392 if(b2_cou == 0) break ;
396 t2_lo = t2_hi - b2_cou + 1 ;
407 if(t1_lo > t2_hi) merge = 0 ;
408 else if(t2_lo > t1_hi) merge = 0 ;
412 printf(
"MMMM: %d: [%d,%d]%d vs. %d [%d,%d]%d %s\n",
414 p+1,t2_lo,t2_hi,*ix2,
415 merge?
"-- merged":
"") ;
425 if(late_merge_cou >= MAX_LATE_MERGE) {
426 LOG(WARN,
"Too many late merges %d/%d: rp %d:%d",late_merge_cou,MAX_LATE_MERGE,row,p) ;
430 printf(
"***** WARN Too many late merges\n") ;
434 late_merge[late_merge_cou].lo = *ix1 ;
435 late_merge[late_merge_cou].hi = *ix2 ;
440 printf(
" late merge pad %d: %d %d\n",p,*ix1,*ix2) ;
456 blobs[blob_ix].cou = 0 ;
461 printf(
" merge: %d: %d %d\n",p,*ix1,*ix2) ;
472 blobs[blob_ix].cou = 0 ;
476 printf(
" was unmerged: %d: %d\n",p,*ix1) ;
487 int p = tpx_rowlen[row] ;
488 short *b1 = p_row_pad[row][p] ;
495 tpx_to_altro(row,p,rr,aa,cc) ;
510 if(b1_cou == 0) break ; ;
516 blobs[blob_ix].cou = 0 ;
520 printf(
" last pad: %d: %d\n",p,*ix1) ;
536 int gprof2 = PROFILER(gprof1) ;
540 #define MAX_CHAIN_COU 32
541 #define MAX_CHAIN_IX 32
543 short ix[MAX_CHAIN_IX] ;
546 } chain[MAX_CHAIN_COU] ;
550 for(
int i=0;i<late_merge_cou;i++) {
551 int lo = late_merge[i].lo ;
552 int hi = late_merge[i].hi ;
557 for(
int j=0;j<chain_cou;j++) {
559 for(
int k=0;k<chain[j].cou;k++) {
560 if(chain[j].ix[k] == lo) got_lo = j + 1 ;
561 if(chain[j].ix[k] == hi) got_hi = j + 1;
564 if(got_hi || got_lo) break ;
567 if(got_hi && got_lo) continue ;
570 chain[got_hi-1].ix[chain[got_hi-1].cou] = lo ;
571 chain[got_hi-1].cou++ ;
573 if(chain[got_hi-1].cou >= MAX_CHAIN_IX) {
575 LOG(WARN,
"Too many hi") ;
581 chain[got_lo-1].ix[chain[got_lo-1].cou] = hi ;
582 chain[got_lo-1].cou++ ;
584 if(chain[got_lo-1].cou >= MAX_CHAIN_IX) {
586 LOG(WARN,
"Too many lo") ;
593 if(chain_cou >= MAX_CHAIN_COU) {
594 LOG(WARN,
"Too many chains") ;
598 chain[chain_cou].ix[0] = lo ;
599 chain[chain_cou].ix[1] = hi ;
600 chain[chain_cou].cou = 2 ;
610 for(
int i=0;i<chain_cou;i++) {
612 for(
int j=0;j<chain[i].cou;j++) {
613 if(chain[i].ix[j] < min) {
614 min = chain[i].ix[j];
616 chain[i].leader = min ;
621 for(
int i=0;i<chain_cou;i++) {
622 printf(
"chain %d: ",i) ;
623 for(
int j=0;j<chain[i].cou;j++) {
624 printf(
"%2d ",chain[i].ix[j]) ;
630 if(blob_ix >= MAX_BLOB_COUNT) {
631 LOG(WARN,
"Too many blobs %d/%d",blob_ix,MAX_BLOB_COUNT) ;
633 blob_ix = MAX_BLOB_COUNT - 1 ;
637 int gprof3 = PROFILER(gprof2) ;
639 for(
int pad=1;pad<=tpx_rowlen[row];pad++) {
640 register short *b = p_row_pad[row][pad] ;
647 tpx_to_altro(row,pad,rr,aa,cc) ;
654 short flags = get_static(row,pad)->f & 0x0FFF ;
655 flags &= ~FCF_ONEPAD ;
656 if(rdo==0) flags &= ~FCF_BROKEN_EDGE ;
673 for(
int l=0;l<chain_cou;l++) {
674 for(
int k=0;k<chain[l].cou;k++) {
675 if(ix == chain[l].ix[k]) {
678 printf(
" pad %3d, tb_hi %3d: chain merge of %d into %d\n",pad,bs[2],ix,chain[l].leader) ;
680 ix = chain[l].leader ;
689 if((ix < 0) || (ix > blob_ix)){
690 LOG(WARN,
"Too many ix: r:p %d:%d: ix %d/%d",
691 row,pad,ix,blob_ix) ;
693 printf(
"***** WARN Too many ix: r:p %d:%d: ix %d/%d\n",
694 row,pad,ix,blob_ix) ;
702 blob_t *bl = blobs + ix ;
704 short seq_cou = bl->cou ;
706 if(seq_cou >= MAX_SEQ_PER_BLOB) {
707 LOG(WARN,
"Too many seqs: r:p %d:%d: ix %d: seq_cou %d/%d",row,pad,ix,seq_cou,MAX_SEQ_PER_BLOB) ;
710 printf(
"***** WARN Too many seqs: r:p %d:%d: ix %d: %d/%d\n",
711 row,pad,ix,seq_cou,MAX_SEQ_PER_BLOB) ;
727 blob_seq_t *seq = bl->seq + seq_cou ;
741 int gprof4 = PROFILER(gprof3) ;
745 for(
int ix=0;ix<blob_ix;ix++) {
747 printf(
"===> BLOB %2d: row %d, %d pads, flags 0x%X\n",ix,row,blobs[ix].cou,blobs[ix].flags) ;
750 if(blobs[ix].cou == 0) continue ;
752 short flags = blobs[ix].flags ;
759 int prof1 = PROFILER(0) ;
762 for(
int j=0;j<blobs[ix].cou;j++) {
763 short *s = blobs[ix].seq[j].s ;
764 short pad = blobs[ix].seq[j].pad ;
766 if(pad < p1) p1 = pad ;
767 if(pad > p2) p2 = pad ;
769 short tb_cou = *s++ ;
773 int tb_hi = (int) *s++ ;
775 int tb_lo = tb_hi - tb_cou + 1 ;
779 if(tb_hi>=500 || (tb_lo > tb_hi)) {
780 LOG(ERR,
"tb_cou %d, tb_hi %d, tb_lo %d",tb_cou,tb_hi,tb_lo) ;
783 if(tb_hi > t2) t2 = tb_hi ;
784 if(tb_lo < t1) t1 = tb_lo ;
790 short dp = p2 - p1 + 1 ;
791 short dt = t2 - t1 + 1 ;
792 short dt_2 = dt + 2 ;
796 if((t1>500)||(t2>500)||(dp<=0)||(dt<=0)) {
797 LOG(ERR,
"What dp %d, dt %d: %d:%d p, %d:%d t???",dp,dt,p1,p2,t1,t2) ;
801 if((dt <= 1) && do_cuts && !(flags & FCF_BROKEN_EDGE)) continue ;
805 if((t1>15) && do_cuts && !(flags & FCF_BROKEN_EDGE)) continue ;
807 flags |= FCF_ONEPAD ;
811 int prof2 = PROFILER(prof1) ;
817 u_int tot_size = (dp+2)*dt_2*
sizeof(
short)*2 ;
818 if(tot_size >
sizeof(dta)) {
819 LOG(WARN,
"Cluster too big: sec %d:%d, row %d, dp %d, dt %d: %d/%d-- skipping",sector,rdo,row,dp,dt,tot_size,
sizeof(dta)) ;
824 memset(dta, 0, (dp+2)*dt_2*
sizeof(
short)*2) ;
826 dta_s = dta + (dp+2)*dt_2 ;
829 memset(dta_t,0,(dp+2)*dt_2*
sizeof(
short)) ;
830 memset(dta_id,0,(dp+2)*dt_2*
sizeof(
short)) ;
833 int prof3 = PROFILER(prof2) ;
837 for(
int j=0;j<blobs[ix].cou;j++) {
838 short *s = blobs[ix].seq[j].s ;
839 short p0 = blobs[ix].seq[j].pad - p1 + 1;
842 short tb_cou = *s++ ;
846 int tb_hi = (int) *s++ ;
848 int tb_lo = tb_hi - tb_cou + 1 ;
851 int tb = tb_hi - t1 + 1 ;
853 short *udd = DTA(p0,0) ;
857 u_short *tdd = DTA_T(p0,0) ;
859 for(;tb_hi>=tb_lo;tb_hi--) {
862 u_short track_id = *s++ ;
864 *(tdd + tb) = track_id ;
875 int prof4 = PROFILER(prof3) ;
879 for(
int i=1;i<=(dp);i++) {
880 for(
int j=1;j<=(dt);j++) {
887 for(
int ii=-1;ii<=1;ii++) {
888 register short *udd = DTA(i+ii,iy) ;
890 for(
int jj=0;jj<3;jj++) {
895 register short *udd = DTA(i,iy) ;
897 for(
int jj=0;jj<3;jj++) {
907 int prof5 = PROFILER(prof4) ;
916 for(
int i=1;i<=(dp);i++) {
917 for(
int j=1;j<=(dt);j++) {
922 adc_peak = *DTA(i,j) ;
925 if(adc_peak) pix_cou++ ;
928 if(unlikely(adc_peak < 1)) continue ;
932 if(unlikely(adc < 1)) continue ;
936 for(
int ii=-1;ii<=1;ii++) {
937 short *dd = DTA_S(i+ii,iy) ;
939 for(
int jj=0;jj<3;jj++) {
940 short s_adc = *dd++ ;
942 if(likely(adc < s_adc))
goto skip_calc ;
943 if(unlikely(s_adc < 0))
goto skip_calc ;
949 peaks[peaks_cou].i = i ;
950 peaks[peaks_cou].j = j ;
951 peaks[peaks_cou].aux_flags = adc_peak ;
954 if(unlikely(peaks_cou >= MAX_PEAKS_PER_BLOB))
goto peaks_done ;
965 int prof6 = PROFILER(prof5) ;
972 if((peaks_cou==0)||(peaks_cou>=MAX_PEAKS_PER_BLOB)) {
977 printf(
"Warning: peaks_cou %d vs %d\n",peaks_cou,MAX_PEAKS_PER_BLOB) ;
991 for(
int i=1;i<=(dp);i++) {
992 for(
int j=1;j<=(dt);j++) {
998 if(adc > max_adc) max_adc = adc ;
1004 for(
int j=1;j<=(dt);j++) {
1006 for(
int i=1;i<=(dp);i++) {
1015 printf(
"FFF %d %d %d %d %d %d %d\n",event,sector,row,p1,ccc,t1+j-1,sum) ;
1024 printf(
"+++++ sec %2d, row %2d: p [%d,%d], t [%d,%d]; peaks %d\n",sector,row,p1,p2,t1,t2,peaks_cou) ;
1026 for(
int i=0;i<peaks_cou;i++) {
1027 printf(
"[%d,%d] ",peaks[i].i,peaks[i].j) ;
1032 for(
int i=0;i<=(dp+1);i++) {
1033 printf(
"p%03d ",p1+i-1) ;
1037 for(
int j=(dt+1);j>=0;j--) {
1038 printf(
"t%03d ",t1+j-1) ;
1040 for(
int i=0;i<=(dp+1);i++) {
1041 printf(
"%4d ",*DTA(i,j)) ;
1047 printf(
"##### p [%d,%d], t [%d,%d]; peaks %d\n",p1,p2,t1,t2,peaks_cou) ;
1050 for(
int i=0;i<=(dp+1);i++) {
1051 printf(
"p%03d ",p1+i-1) ;
1055 for(
int j=(dt+1);j>=0;j--) {
1056 printf(
"t%03d ",t1+j-1) ;
1058 for(
int i=0;i<=(dp+1);i++) {
1059 printf(
"%4d ",*DTA_S(i,j)) ;
1080 blob_c.flags = flags ;
1081 blob_c.dt_2 = dt_2 ;
1087 peaks[0].flags = flags ;
1089 static u_int peak_counter ;
1091 if(peaks_cou <= 1 ) {
1093 double f_charge = 0.0 ;
1094 double f_t_ave = 0.0 ;
1095 double f_p_ave = 0.0 ;
1101 for(
int i=1;i<=(dp);i++) {
1109 short *adc_p = DTA(i,1) ;
1111 for(
int j=1;j<=(dt);j++) {
1114 register int adc = *adc_p++ ;
1118 #ifdef DO_SIMULATION
1119 if(adc) *DTA_ID(i,j) = cluster_id ;
1129 printf(
"pad:tb %d:%d, i:j %d:%d = %d\n",pad,tb,i,j,adc) ;
1133 i_t_ave += (tb++) * adc ;
1137 if(i_charge == 0) continue ;
1140 int pad = p1 + i - 1 ;
1142 double gain = get_static(row,pad)->g ;
1143 double t0 = get_static(row,pad)->t0 ;
1147 LOG(ERR,
"BAD Gain 0: sector %d, rdo %d: rp %d %d, flags 0x%X",sector,rdo,row,pad,flags) ;
1151 double corr_charge = i_charge * gain ;
1153 f_charge += corr_charge ;
1154 f_t_ave += i_t_ave * gain + t0 * corr_charge ;
1155 f_p_ave += pad * corr_charge ;
1160 peaks[0].f_t_ave = f_t_ave ;
1161 peaks[0].f_p_ave = f_p_ave ;
1162 peaks[0].f_charge = f_charge ;
1163 peaks[0].pix_cou = pix_cou ;
1169 #ifdef DO_SIMULATION
1170 peaks[0].cluster_id = cluster_id++ ;
1171 do_track_id(peaks_cou) ;
1173 outbuff += do_dump(0,outbuff) ;
1175 prof7 = PROFILER(prof6) ;
1180 do_peaks(peaks_cou) ;
1181 #ifdef DO_SIMULATION
1182 do_track_id(peaks_cou) ;
1185 for(
int p=0;p<peaks_cou;p++) {
1186 outbuff += do_dump(p,outbuff) ;
1189 prof7 = PROFILER(prof6) ;
1201 cl_cou = (outbuff - locbuff)/3 ;
1204 cl_cou = (outbuff - locbuff)/2 ;
1213 *row_cache = (FCF_2D_V_FY13 << 16) | row ;
1214 *clust_count_cache = cl_cou ;
1226 if(do_debug) do_print(row) ;
1232 return (u_int *)locbuff - (u_int *)startbuff ;
1246 if(is_pad_valid(row,pad)==0) {
1251 if(a->count == 0)
return 0 ;
1255 int shorts_so_far = data_raw_p - data_raw ;
1256 if(shorts_so_far >((data_raw_shorts*9)/10)) {
1257 LOG(ERR,
"Too much data %d/%d",shorts_so_far,data_raw_shorts) ;
1264 short *start = data_raw_p ;
1266 *data_raw_p++ = row ;
1267 *data_raw_p++ = pad ;
1269 p_row_pad[row][pad] = data_raw_p ;
1272 short *tb_cou_p = data_raw_p ;
1275 for(
int i=0;i<a->count;i++) {
1279 #ifdef DO_SIMULATION
1282 if(sim_adc && modes) track_id = sim_adc[i].track_id ;
1283 else track_id = 0xFFFF ;
1287 LOG(ERR,
"%d: %d %d",i,tb,aa) ;
1290 if(tb != (tb_last-1)) {
1292 *tb_cou_p = tb_cou ;
1294 tb_cou_p = data_raw_p++ ;
1295 *data_raw_p++ = -1 ;
1296 *data_raw_p++ = tb ;
1300 *data_raw_p++ = aa ;
1301 #ifdef DO_SIMULATION
1302 *data_raw_p++ = track_id ;
1308 *data_raw_p++ = aa ;
1309 #ifdef DO_SIMULATION
1310 *data_raw_p++ = track_id ;
1319 *tb_cou_p = tb_cou ;
1325 int s_cou = data_raw_p - start ;
1332 tpxFCF_2D::tpxFCF_2D()
1335 LOG(WARN,
"TPX_ONLINE undefined -- running in Offline mode.") ;
1337 #ifdef DO_SIMULATION
1338 LOG(WARN,
"DO_SIMULATION defined.") ;
1341 LOG(WARN,
"CHECK_SANITY defined.") ;
1345 data_raw_shorts = 2*MAX_PADS_PER_RDO*512 ;
1346 data_raw = (
short *) valloc(2*data_raw_shorts) ;
1349 int tpxFCF_2D::do_dump(
int ix, u_int *obuff)
1355 double f_p_ave, f_t_ave ;
1358 short flags = peaks[ix].flags ;
1359 short p1 = peaks[ix].p1 ;
1360 short p2 = peaks[ix].p2 ;
1361 short t1 = peaks[ix].t1 ;
1362 short t2 = peaks[ix].t2 ;
1364 if(peaks[ix].f_charge) {
1366 f_p_ave = peaks[ix].f_p_ave / peaks[ix].f_charge ;
1367 f_t_ave = peaks[ix].f_t_ave / peaks[ix].f_charge ;
1372 LOG(WARN,
"no charge: ix %d: %d-%d, %d-%d: fla 0x%X: ij %d-%d, pixs %d, aux 0x%X",
1376 peaks[ix].i,peaks[ix].j,
1378 peaks[ix].aux_flags) ;
1382 u_int time_c = (u_int)(f_t_ave * 64.0 + 0.5) ;
1383 u_int pad_c = (u_int)(f_p_ave * 64.0 + 0.5) ;
1384 u_int cha = (u_int) (peaks[ix].f_charge + 0.5) ;
1387 if(flags & FCF_BROKEN_EDGE)
goto keep ;
1388 if(do_cuts == 0)
goto keep ;
1391 if(flags & (FCF_DEAD_EDGE | FCF_ROW_EDGE))
return 0 ;
1394 if((t2-t1)<=2)
return 0 ;
1395 if(cha < 10.0)
return 0 ;
1402 if((f_t_ave > 26.0) && (f_t_ave < 415.0)) ;
1405 else printf(
"PROMPT %d %d %d %f %f %d\n",event,sector,row,f_p_ave,f_t_ave,cha) ;
1412 u_int tmp_p = pad_c / 64 ;
1424 tmp_fl = (p1 << 8) | (p2 << 11) ;
1426 tmp_p = time_c / 64 ;
1432 if(t1 > 15) t1 = 15 ;
1433 if(t2 > 15) t2 = 15 ;
1438 tmp_fl |= (t2 << 4) | t1 ;
1440 if(flags & FCF_ONEPAD) time_c |= 0x8000 ;
1442 if(flags & FCF_MERGED) pad_c |= 0x8000 ;
1443 if(flags & FCF_DEAD_EDGE) pad_c |= 0x4000 ;
1445 if(flags & FCF_ROW_EDGE) tmp_fl |= 0x8000 ;
1446 if(flags & FCF_BROKEN_EDGE) tmp_fl |= 0x4000 ;
1448 if(cha > 0x7FFF) cha = 0x8000 | (cha/1024) ;
1450 *obuff++ = (time_c << 16) | pad_c ;
1451 *obuff++ = (cha << 16) | tmp_fl ;
1455 #ifdef DO_SIMULATION
1457 int quality = peaks[ix].quality ;
1458 int track_id = peaks[ix].track_id ;
1460 *obuff++ = (quality << 16) | track_id ;
1473 int tpxFCF_2D::do_peaks(
int peaks_cou)
1476 short p1 = blob_c.p1 ;
1477 short p2 = blob_c.p2 ;
1478 short t1 = blob_c.t1 ;
1479 short t2 = blob_c.t2 ;
1486 if((p1>p2)||(t1>t2)||(p1<1)||(t1<0)) {
1487 LOG(ERR,
"What??? %d %d %d %d",p1,p2,t1,t2) ;
1493 dt_2 = blob_c.dt_2 ;
1496 if(peaks_cou==0) peaks_cou = 1 ;
1497 if(peaks_cou > 1) flags = FCF_MERGED ;
1501 for(
int p=0;p<peaks_cou;p++) {
1502 peaks[p].f_charge = 0.0 ;
1503 peaks[p].f_t_ave = 0.0 ;
1504 peaks[p].f_p_ave = 0.0 ;
1505 peaks[p].pix_cou = 0 ;
1506 peaks[p].t1 = 10000 ;
1507 peaks[p].p1 = 10000 ;
1510 peaks[p].flags = flags ;
1511 #ifdef DO_SIMULATION
1512 peaks[p].cluster_id++ ;
1516 for(
int i=1;i<=(dp);i++) {
1517 int pad = p1 + i - 1 ;
1519 double gain = get_static(row,pad)->g ;
1520 double t0 = get_static(row,pad)->t0 ;
1522 short flags = get_static(row,pad)->f & 0xFFF ;
1523 if(rdo==0) flags &= ~FCF_BROKEN_EDGE ;
1527 LOG(ERR,
"What: lo gain %f %f, flags 0x%X: sec %d: rp %d:%d; i %d, dp %d",gain,t0,flags,sector,row,pad,i,dp) ;
1531 for(
int j=1;j<=(dt);j++) {
1533 short adc = *DTA(i,j) ;
1535 if(adc==0) continue ;
1537 int min_dist = 10000000 ;
1541 for(
int p=0;p<peaks_cou;p++) {
1543 int dx = i - peaks[p].i ;
1544 int dy = j - peaks[p].j ;
1546 int dist = dx*dx + dy*dy ;
1548 if(dist < min_dist) {
1556 LOG(ERR,
"What: %d: rp %d:%d",min_p,row,pad) ;
1560 #ifdef DO_SIMULATION
1561 *DTA_ID(i,j) = peaks[min_p].cluster_id ;
1563 double corr_charge = adc * gain ;
1565 peaks[min_p].f_charge += corr_charge ;
1566 peaks[min_p].f_t_ave += corr_charge*(t0+(double)(j+t1-1)) ;
1567 peaks[min_p].f_p_ave += corr_charge*(pad) ;
1568 peaks[min_p].pix_cou++ ;
1571 peaks[min_p].flags |= flags ;
1573 if(i < peaks[min_p].p1) peaks[min_p].p1 = i ;
1574 if(i > peaks[min_p].p2) peaks[min_p].p2 = i ;
1575 if(j < peaks[min_p].t1) peaks[min_p].t1 = j ;
1576 if(j > peaks[min_p].t2) peaks[min_p].t2 = j ;
1581 for(
int p=0;p<peaks_cou;p++) {
1582 peaks[p].t1 += t1 - 1 ;
1583 peaks[p].p1 += p1 - 1 ;
1584 peaks[p].t2 += t1 - 1 ;
1585 peaks[p].p2 += p1 - 1 ;
1587 if((peaks[p].p2 - peaks[p].p1)==0) peaks[p].flags |= FCF_ONEPAD ;
1588 else peaks[p].flags &= ~FCF_ONEPAD ;
1595 void tpxFCF_2D::do_track_id(
int peaks_cou)
1602 dt_2 = blob_c.dt_2 ;
1605 for(
int c=0;c<peaks_cou;c++) {
1607 const int MAX_TRACK_IDS = 10 ;
1612 } t_id[MAX_TRACK_IDS] ;
1614 memset(t_id,0,
sizeof(t_id)) ;
1617 u_short track_id = 123;
1618 u_int blob_adc = 0 ;
1620 for(
int i=1;i<=dp;i++) {
1621 for(
int j=1;j<=dt;j++) {
1622 if(*DTA_ID(i,j) == peaks[c].cluster_id) {
1623 track_id = *DTA_T(i,j) ;
1629 blob_adc += *DTA(i,j) ;
1635 for(
int t=0;t<MAX_TRACK_IDS;t++) {
1636 if(t_id[t].track_id == track_id) {
1641 else if(t_id[t].cou == 0) {
1646 if(found) continue ;
1647 if(t_use < 0) continue ;
1650 t_id[t_use].track_id = track_id ;
1658 for(
int t=0;t<MAX_TRACK_IDS;t++) {
1659 if(t_id[t].cou >= max_cou) {
1660 max_cou = t_id[t].cou ;
1661 track_id = t_id[t].track_id ;
1670 for(
int i=1;i<=dp;i++) {
1671 for(
int j=1;j<=dt;j++) {
1672 if(*DTA_ID(i,j) == peaks[c].cluster_id) {
1673 if(*DTA_T(i,j) == track_id) {
1674 track_adc += *DTA(i,j) ;
1681 peaks[c].track_id = track_id ;
1684 peaks[c].quality = (int)((
double)track_adc/(double)blob_adc * 100.0 + 0.5) ;
1686 else peaks[c].quality = 0 ;
1689 if(peaks[c].quality == 0) {
1690 LOG(ERR,
"What??? %d %d %d",max_cou,track_adc,blob_adc) ;