10 #include <rtsSystems.h>
11 #include <DAQ_READER/daqReader.h>
12 #include <DAQ_READER/daq_dta.h>
13 #include <DAQ_READER/daq_det.h>
15 #include <DAQ_TPX/daq_tpx.h>
16 #include <DAQ_TPX/tpxFCF_flags.h>
17 #include <DAQ_TPX/tpxPed.h>
19 #include <DAQ_ITPC/daq_itpc.h>
20 #include <DAQ_ITPC/itpcFCF.h>
21 #include <DAQ_ITPC/itpcPed.h>
26 #include "tpc23_base.h"
34 short tpc23_base::bad_fee_cou[24][46] ;
35 short tpc23_base::bad_fee[24][46][36] ;
46 int tpc23_base::fcf_decode(u_int *p_buff,
daq_sim_cld_x *dc, u_int version)
52 words_used = old_fcf->fcf_decode(p_buff,&cld,version) ;
54 memcpy(&dc->cld,&cld,
sizeof(cld)) ;
56 p_buff += words_used ;
59 dc->quality = (*p_buff)>>16;
63 dc->max_adc = *p_buff & 0xFFFF ;
64 dc->pixels = (*p_buff)>>16 ;
68 dc->reserved[0] = *p_buff ;
69 dc->track_id = dc->reserved[0]&0xFFFF ;
73 return words_used + 3 ;
76 void tpc23_base::sim_evt_start(
int sec1)
81 s1_dta = (u_short *) malloc((ROW_MAX+1)*(PAD_MAX+1)*512*
sizeof(*s1)) ;
84 s1_track_id = (
int *) malloc((ROW_MAX+1)*(PAD_MAX+1)*512*
sizeof(*s1_track_id)) ;
86 if(store_track_id==0) {
87 store_track_id = (
int *) malloc((PAD_MAX+1)*512*
sizeof(*store_track_id)) ;
99 int tpc23_base::do_ch_sim(
int row,
int pad, u_short *adc,
int *track_id)
103 struct seq_t *seq = s1[row][pad].seq ;
108 s1[row][pad].ix = last_ix ;
110 u_short *dta = s1_dta + last_ix ;
111 int *
track = s1_track_id + last_ix ;
115 for(
int t=1;t<510;t++) {
116 if(adc[t-1]!=0 && adc[t]==0 && adc[t+1]!=0) {
119 else if(adc[t-1]!=0 && adc[t]==0 && adc[t+1]==0 && adc[t+2]!=0) {
130 for(
int t=0;t<512;t++) {
134 seq[s_cou].t_lo = t ;
135 seq[s_cou].dta_p = dta_p_ix ;
136 seq[s_cou].blob_id = 0 ;
146 dta[dta_p_ix] = adc[t] ;
148 track[dta_p_ix] = track_id[t] ;
155 seq[s_cou].t_hi = t-1 ;
158 LOG(ERR,
"rp %d:%d t is %d, t_start is %d",row,pad,t,t_start) ;
167 LOG(WARN,
"too many sequences %d: sec %d, row %d, pad %d, tb %d",s_cou,sector1,row,pad,t-1) ;
175 for(
int t=0;t<511;t++) {
176 LOG(TERR,
" tb %3d %d",t,adc[t]) ;
182 seq[s_cou].t_hi = 511 ;
187 LOG(ERR,
"still too many sequences %d: sec %d, row %d, pad %d",s_cou,sector1,row,pad) ;
192 sequence_cou += s_cou ;
194 seq[s_cou].t_hi = -1 ;
196 last_ix += dta_p_ix ;
202 void tpc23_base::sim_do_pad(
int row,
int pad,
short *adc,
int *track_id)
206 struct seq_t *seq = s1[row][pad].seq ;
211 s1[row][pad].ix = last_ix ;
213 u_short *dta = s1_dta + last_ix ;
214 int *track = s1_track_id + last_ix ;
216 for(
int t=0;t<512;t++) {
220 seq[s_cou].t_lo = t ;
221 seq[s_cou].dta_p = dta_p_ix ;
222 seq[s_cou].blob_id = 0 ;
226 dta[dta_p_ix] = adc[t] ;
227 track[dta_p_ix] = track_id[t] ;
233 seq[s_cou].t_hi = t-1 ;
236 LOG(ERR,
"t is %d, t_start is %d",t,t_start) ;
244 LOG(WARN,
"too many sequences %d: sec %d, row %d, pad %d, tb %d",s_cou,sector1,row,pad,t-1) ;
253 seq[s_cou].t_hi = 511 ;
258 LOG(ERR,
"still too many sequences %d: sec %d, row %d, pad %d",s_cou,sector1,row,pad) ;
263 sequence_cou += s_cou ;
265 seq[s_cou].t_hi = -1 ;
267 last_ix += dta_p_ix ;
273 int tpc23_base::row_stage2(
int row)
277 printf(
"ROW %2d: STAGE2: BLOBS: blob_cou %d\n",row,blob_cou-1) ; fflush(stdout) ;
279 u_int *s2_marker = s2_dta ;
284 for(
int i=1;i<blob_cou;i++) {
287 int t_len = blob[i].t2-blob[i].t1+1 ;
289 if(t_len<=1) blob[i].flags |= FCF_BROKEN_EDGE ;
292 if(t_len<=3) blob[i].flags |= FCF_BROKEN_EDGE ;
295 if(blob[i].area<=4) blob[i].flags |= FCF_BROKEN_EDGE ;
299 printf(
"blob %2d(%2d): pad %d:%d, tb %d:%d, flags 0x%X, area %d\n",i,blob_ix[i],
300 blob[i].p1,blob[i].p2,
301 blob[i].t1,blob[i].t2,
306 LOG(TERR,
"blob %2d: pad %d:%d, tb %d:%d, flags 0x%X, area %d",i,
307 blob[i].p1,blob[i].p2,
308 blob[i].t1,blob[i].t2,
316 if(blob[i].flags&FCF_BROKEN_EDGE) continue ;
321 if(log_level>=1) LOG(TERR,
"ROW %2d: STAGE2: BLOBS: blob_cou %d/%d",row,blob_good,blob_cou) ;
324 for(
int i=1;i<blob_cou;i++) {
325 if(blob[i].flags & FCF_BROKEN_EDGE) continue ;
326 if(blob[i].flags & 0x80) continue ;
331 printf(
"BLOB %d(%d)/%d: row %2d:\n",i,i,blob_cou,row) ;
332 printf(
" pad %d:%d, tb %d:%d\n",blob[i].p1,blob[i].p2,blob[i].t1,blob[i].t2) ;
335 int td = blob[i].t2 - blob[i].t1 + 1 ;
336 int pd = blob[i].p2 - blob[i].p1 + 1 ;
338 for(
int pad=blob[i].p1;pad<=blob[i].p2;pad++) {
339 struct seq_t *seq = s1[row][pad].seq ;
342 LOG(TERR,
"blob %d: pad %d, t_hi %d; ix %d",i,pad,seq->t_hi,s1[row][pad].ix) ;
345 if(seq->t_hi==-1) continue ;
347 u_short *d = s1_dta + s1[row][pad].ix ;
351 int px = pad - blob[i].p1 ;
353 memset(store[px],0,td*
sizeof(store[px][0])) ;
355 while(seq->t_hi>=0) {
359 int bid = blob_ix[seq->blob_id] ;
363 LOG(TERR,
" i %d, bid %d, blob_id %d",i,bid,seq->blob_id) ;
375 for(
int t=seq->t_lo;t<=seq->t_hi;t++) {
376 u_short dta = *(d+seq->dta_p+j) ;
378 printf(
" pad %3d, tb %3d, adc %3d %5u\n",pad,t,dta,dta) ;
380 int tx = t - blob[i].t1 ;
382 store[px][tx] = dta ;
391 int *d_track_id = s1_track_id + s1[row][pad].ix ;
394 for(
int t=seq->t_lo;t<=seq->t_hi;t++) {
397 tr = (int (*)[512]) store_track_id ;
399 int dta = *(d_track_id+seq->dta_p+j) ;
401 int tx = t - blob[i].t1 ;
419 printf(
"Here: %d %d\n",pd,td) ; fflush(stdout) ;
422 if(log_level>=2) LOG(TERR,
" pd %d, td %d",pd,td) ;
424 for(
int p=0;p<pd;p++) {
425 for(
int t=0;t<td;t++) {
439 for(
int ip=-1;ip<=1;ip++) {
440 for(
int it=-1;it<=1;it++) {
445 if(iii==1 || iii==-1) continue ;
448 if((p+ip)<0) d1 = 1 ;
449 if((p+ip)>=pd) d1 = 1 ;
450 if((t+it)<0) d1 = 1 ;
451 if((t+it)>=td) d1 = 1 ;
454 else adc = store[p+ip][t+it] ;
469 printf(
"Here 1: %d %d\n",pd,td) ; fflush(stdout) ;
475 if(log_level>=2) LOG(TERR,
" here %d %d",pd,td) ;
477 for(
int p=0;p<pd;p++) {
478 for(
int t=0;t<td;t++) {
480 int adc = smooth[p][t] - 5 ;
482 if(adc < 1) continue ;
484 for(
int ip=-1;ip<=1;ip++) {
485 if((p+ip)<0) continue ;
486 if((p+ip)>=pd) continue ;
488 for(
int it=-1;it<=1;it++) {
489 if((t+it)<0) continue ;
490 if((t+it)>=td) continue ;
492 if(ip==0 && it==0) continue ;
497 int s_adc = smooth[p+ip][t+it] ;
501 if(adc < s_adc)
goto skip_calc ;
502 if(s_adc < 0)
goto skip_calc ;
508 peaks[peaks_cou].t = t ;
509 peaks[peaks_cou].p = p ;
512 smooth[p][t] = -adc ;
524 printf(
"Here 2: %d %d\n",pd,td) ; fflush(stdout) ;
528 printf(
"PEAKS %d\n",peaks_cou) ;
531 for(
int p=0;p<pd;p++) {
532 printf(
"%3d ",blob[i].p1+p) ;
535 for(
int t=0;t<td;t++) {
536 printf(
"TB %3d: ",blob[i].t1+t) ;
537 for(
int p=0;p<pd;p++) {
539 for(
int j=0;j<peaks_cou;j++) {
540 if(peaks[j].p == p && peaks[j].t==t) pk = 1 ;
543 printf(
"%3d[%4d]%c ",store[p][t],smooth[p][t],pk?
'*':
' ') ;
551 if(peaks_cou<=1) one_peak = 1 ;
554 if(log_level>=2) LOG(TERR,
"row %d: blob %d: flags 0x%X: peaks_cou %d, one_peak %d",row,i,blob[i].flags,peaks_cou,one_peak) ;
559 printf(
"Here: %d %d: onepeak %d\n",pd,td,one_peak) ; fflush(stdout) ;
564 double f_charge = 0.0 ;
565 double f_t_ave = 0.0 ;
566 double f_p_ave = 0.0 ;
567 u_short flags = blob[i].flags ;
569 if(log_level>=2) LOG(TERR,
" flags again %d: %d %d",flags,pd,td) ;
571 if(flags)
goto done_peaks ;
573 for(
int p=0;p<pd;p++) {
574 int pad = blob[i].p1+p ;
575 double gain = rp_gain[sector1-1][row][pad].gain ;
576 double t0 = rp_gain[sector1-1][row][pad].t0 ;
580 if(log_level>=2) LOG(TERR,
" gain %d %d %d %f",sector1,row,pad,gain) ;
582 printf(
"... gain %d %d %d = %f\n",sector1,row,pad,gain) ;
585 for(
int t=0;t<td;t++) {
586 int adc = store[p][t] ;
592 if(i_charge==0) continue ;
594 double corr_charge = (double)i_charge * gain ;
596 f_charge += corr_charge ;
597 f_t_ave += i_t_ave * gain + t0 * corr_charge ;
598 f_p_ave += p * corr_charge ;
602 if(log_level>=2) LOG(TERR,
" here %f",f_charge) ;
604 if(f_charge<0.1)
goto done_peaks ;
609 tr = (int (*)[512]) store_track_id ;
615 for(
int p=0;p<pd;p++) {
616 for(
int t=0;t<td;t++) {
617 if(store[p][t] > sim_max_adc) {
618 sim_max_adc = store[p][t] ;
625 sim_track_id = tr[max_p][max_t] ;
629 for(
int p=0;p<pd;p++) {
630 for(
int t=0;t<td;t++) {
631 if(tr[p][t]==sim_track_id) {
634 if(store[p][t]) sim_all++ ;
639 float yada = (float)sim_quality/(
float)sim_all ;
640 yada = (100.0 * yada) + 0.5 ;
641 sim_quality = (int) yada ;
647 f_t_ave /= f_charge ;
648 f_p_ave /= f_charge ;
650 f_p_ave += blob[i].p1 ;
651 f_t_ave += blob[i].t1 ;
660 u_int time_c = (u_int)(f_t_ave*64.0+0.5) ;
661 u_int pad_c = (u_int)(f_p_ave*64.0+0.5) ;
662 u_int cha = (u_int)(f_charge+0.5) ;
664 if(cha > 0x7FFF) cha = 0x8000 | (cha/1024) ;
669 int p_lo = (pad_c/64) - blob[i].p1 ;
670 int p_hi = blob[i].p2 - (pad_c/64) ;
672 if(p_lo<0) p_lo = 0 ;
673 if(p_hi<0) p_hi = 0 ;
674 if(p_lo>7) p_lo = 7 ;
675 if(p_hi>7) p_hi = 7 ;
677 tmp_fl = (p_lo<<8)|(p_hi<<11) ;
679 int t_lo = (time_c/64) - blob[i].t1 ;
680 int t_hi = blob[i].t2 - (time_c/64) ;
683 if(t_lo<0) t_lo = 0 ;
684 if(t_hi<0) t_hi = 0 ;
685 if(t_lo>7) t_lo = 15 ;
686 if(t_hi>7) t_hi = 15 ;
688 tmp_fl |= (t_hi<<4)|t_lo ;
690 if(flags & FCF_MERGED) pad_c |= 0x8000 ;
691 if(flags & FCF_DEAD_EDGE) pad_c |= 0x4000 ;
693 if(flags & FCF_ONEPAD) time_c |= 0x8000 ;
695 if(flags & FCF_ROW_EDGE) tmp_fl |= 0x8000 ;
696 if(flags & FCF_BROKEN_EDGE) tmp_fl |= 0x4000 ;
699 *s2_dta++ = (time_c<<16)|pad_c ;
700 *s2_dta++ = (cha<<16)|tmp_fl ;
703 *s2_dta++ = sim_quality<<16 ;
704 *s2_dta++ = (blob[i].area<<16)|sim_max_adc ;
705 *s2_dta++ = sim_track_id ;
713 double ppp = (double)(pad_c & 0x3FFF)/64.0 ;
714 double ttt = (double)(time_c & 0x7FFF)/64.0 ;
716 printf(
"CLD3 row %2d, p_ave %f %d %d, t_ave %f %d %d, charge %d, flags 0x%02X\n",
717 row, ppp,blob[i].p1,blob[i].p2,
718 ttt,blob[i].t1,blob[i].t2,
724 double ppp = (double)(pad_c & 0x3FFF)/64.0 ;
725 double ttt = (double)(time_c & 0x7FFF)/64.0 ;
727 LOG(TERR,
"CLD3 row %2d, p_ave %f %d %d, t_ave %f %d %d, charge %d, flags 0x%02X",
728 row, ppp,blob[i].p1,blob[i].p2,
729 ttt,blob[i].t1,blob[i].t2,
736 for(
int pk=0;pk<peaks_cou;pk++) {
737 double f_charge = 0.0 ;
738 double f_t_ave = 0.0 ;
739 double f_p_ave = 0.0 ;
740 u_short flags = blob[i].flags ;
742 flags |= FCF_MERGED ;
747 ip1 = peaks[pk].p - 1 ;
749 ip2 = peaks[pk].p + 1;
750 if(ip2>=pd) ip2 = pd-1 ;
752 it1 = peaks[pk].t - 2 ;
754 it2 = peaks[pk].t + 2;
755 if(it2>=td) it2 = td-1 ;
757 for(
int p=ip1;p<=ip2;p++) {
758 int pad = blob[i].p1 + p ;
760 double gain = rp_gain[sector1-1][row][pad].gain ;
761 double t0 = rp_gain[sector1-1][row][pad].t0 ;
767 for(
int t=it1;t<=it2;t++) {
768 int adc = store[p][t] ;
774 if(i_charge==0) continue ;
776 double corr_charge = (double)i_charge * gain ;
778 f_charge += corr_charge ;
779 f_t_ave += i_t_ave * gain + t0 * corr_charge ;
780 f_p_ave += p * corr_charge ;
783 if(f_charge<0.1) continue ;
788 f_t_ave /= f_charge ;
789 f_p_ave /= f_charge ;
791 f_p_ave += blob[i].p1 ;
792 f_t_ave += blob[i].t1 ;
794 int p_lo = blob[i].p1 + peaks[pk].p-1 ;
795 int p_hi = blob[i].p1 + peaks[pk].p+1 ;
797 int t_lo = blob[i].t1 + peaks[pk].t-2 ;
798 int t_hi = blob[i].t1 + peaks[pk].t+2 ;
809 u_int time_c = (u_int)(f_t_ave*64.0+0.5) ;
810 u_int pad_c = (u_int)(f_p_ave*64.0+0.5) ;
811 u_int cha = (u_int)(f_charge+0.5) ;
813 if(cha > 0x7FFF) cha = 0x8000 | (cha/1024) ;
820 if(p_lo<0) p_lo = 0 ;
821 if(p_hi<0) p_hi = 0 ;
822 if(p_lo>7) p_lo = 7 ;
823 if(p_hi>7) p_hi = 7 ;
825 tmp_fl = (p_lo<<8)|(p_hi<<11) ;
831 if(t_lo<0) t_lo = 0 ;
832 if(t_hi<0) t_hi = 0 ;
833 if(t_lo>7) t_lo = 15 ;
834 if(t_hi>7) t_hi = 15 ;
836 tmp_fl |= (t_hi<<4)|t_lo ;
840 if(flags & FCF_MERGED) pad_c |= 0x8000 ;
841 if(flags & FCF_DEAD_EDGE) pad_c |= 0x4000 ;
843 if(flags & FCF_ONEPAD) time_c |= 0x8000 ;
845 if(flags & FCF_ROW_EDGE) tmp_fl |= 0x8000 ;
846 if(flags & FCF_BROKEN_EDGE) tmp_fl |= 0x4000 ;
849 *s2_dta++ = (time_c<<16)|pad_c ;
850 *s2_dta++ = (cha<<16)|tmp_fl ;
855 tr = (int (*)[512]) store_track_id ;
859 for(
int p=ip1;p<=ip2;p++) {
860 for(
int t=it1;t<=it2;t++) {
861 int adc = store[p][t] ;
863 if(adc > sim_max_adc) {
865 sim_track_id = tr[p][t] ;
872 for(
int p=ip1;p<=ip2;p++) {
873 for(
int t=it1;t<=it2;t++) {
874 if(tr[p][t]==sim_track_id) {
877 if(store[p][t]) sim_all++ ;
882 float yada = (float)sim_quality/(
float)sim_all ;
883 yada = (100.0 * yada) + 0.5 ;
884 sim_quality = (int) yada ;
888 *s2_dta++ = sim_quality<<16 ;
889 *s2_dta++ = (15<<16)|sim_max_adc ;
890 *s2_dta++ = sim_track_id ;
894 double ppp = (double)(pad_c & 0x3FFF)/64.0 ;
895 double ttt = (double)(time_c & 0x7FFF)/64.0 ;
899 printf(
"CLD3 row %2d, p_ave %f %d %d, t_ave %f %d %d, charge %d, flags 0x%02X\n",
905 double ppp = (double)(pad_c & 0x3FFF)/64.0 ;
906 double ttt = (double)(time_c & 0x7FFF)/64.0 ;
910 LOG(TERR,
"CLD3 row %2d, p_ave %f %d %d, t_ave %f %d %d, charge %d, flags 0x%02X",
911 row, ppp,peaks[pk].p,peaks[pk].p,
912 ttt,peaks[pk].t,peaks[pk].t,
926 printf(
"ROW %2d: done_peaks: good clusters %d\n",row,blob_good) ; fflush(stdout) ;
933 printf(
"ROW %2d: STAGE2: good clusters %d\n",row,blob_good) ;
936 return s2_dta-s2_marker ;
942 int tpc23_base::row_stage1(
int row)
949 int blob_merges = 0 ;
951 p_max = rowlen[row] ;
953 printf(
"ROW %2d: STAGE1, rowlen %2d\n",row,p_max) ;
956 for(
int pad=1;pad<p_max;pad++) {
957 struct seq_t *seq_l ;
961 seq_l = s1[row][pad].seq ;
964 printf(
" pad %d: t_hi %d\n",pad,seq_l->t_hi) ;
966 if(seq_l->t_hi==-1) continue ;
968 while(seq_l->t_hi!=-1) {
969 u_int tl_hi = seq_l->t_hi ;
970 u_int tl_lo = seq_l->t_lo ;
971 int bl = seq_l->blob_id ;
975 printf(
" left: pad %d: seq %d: t_lo %d, t_hi %d (bl %d)\n",pad,got_one,tl_lo,tl_hi,bl) ;
977 struct seq_t *seq_r = s1[row][pad+1].seq ;
979 char flags = rp_gain[sector1-1][row][pad].flags ;
980 char flags_r = rp_gain[sector1-1][row][pad+1].flags ;
984 while(seq_r->t_hi!=-1) {
985 u_int tr_hi = seq_r->t_hi ;
986 u_int tr_lo = seq_r->t_lo ;
987 int br = seq_r->blob_id ;
989 if(tr_hi>=512 || tr_lo>=512) {
992 LOG(ERR,
"S%d: tr_hi %d, tr_lo %d: row %d, pad %d",sector1,
993 tr_hi,tr_lo,row,pad+1) ;
1003 if(tl_lo > tr_hi) merge = 0 ;
1004 else if(tr_lo > tl_hi) merge = 0 ;
1007 printf(
" right: pad %d: seq %d: t_lo %d, t_hi %d\n",pad+1,got_one,tr_lo,tr_hi) ;
1014 if(bl==0 && br==0) {
1015 seq_l->blob_id = blob_cou ;
1016 seq_r->blob_id = blob_cou ;
1019 blob[blob_cou].p1 = pad ;
1020 blob[blob_cou].p2 = pad+1 ;
1021 blob[blob_cou].flags = flags ;
1022 blob[blob_cou].area = (tl_hi-tl_lo+1)+(tr_hi-tr_lo+1) ;
1024 blob_ix[blob_cou] = blob_cou ;
1026 if(tl_hi>tr_hi) blob[blob_cou].t2 = tl_hi ;
1027 else blob[blob_cou].t2 = tr_hi ;
1029 if(tl_lo<tr_lo) blob[blob_cou].t1 = tl_lo ;
1030 else blob[blob_cou].t1 = tr_lo ;
1032 printf(
" new blob %d: left %d:%d, right %d:%d \n",blob_cou,
1033 tl_lo,tl_hi,tr_lo,tr_hi) ;
1038 else if(bl==0 && br!=0) {
1041 seq_l->blob_id = br ;
1044 if(blob[br].p1>pad) {
1050 blob[br].flags |= flags ;
1051 blob[br].area += (tl_hi-tl_lo+1) ;
1054 printf(
" WARN: %d: left into right %d: pad in blob %d, pad %d\n",row,br,blob[br].p1,pad) ;
1056 printf(
" left %d:%d, right %d:%d, blob %d:%d\n",tl_lo,tl_hi,
1058 blob[br].t1,blob[br].t2) ;
1061 if(tl_hi>blob[br].t2) blob[br].t2 = tl_hi ;
1062 if(tl_lo<blob[br].t1) blob[br].t1 = tl_lo ;
1065 else if(bl!=0 && br==0) {
1067 seq_r->blob_id = bl ;
1069 blob[bl].p2 = pad+1 ;
1070 blob[bl].flags |= flags ;
1071 blob[bl].area += (tr_hi-tr_lo+1) ;
1075 printf(
" merge left %d: left %d:%d, right %d:%d \n",blob_cou,
1076 tl_lo,tl_hi,tr_lo,tr_hi) ;
1079 if(tr_hi>blob[bl].t2) blob[bl].t2 = tr_hi ;
1080 if(tr_lo<blob[bl].t1) blob[bl].t1 = tr_lo ;
1085 blob[bl].area += (tr_hi-tr_lo+1) ;
1087 printf(
" WARN: %d: already %d: p2 %d, r_pad %d\n",row,bl,blob[br].p2,pad+1) ;
1091 printf(
" WARN: pad %d: left: %d:%d, right %d:%d\n",pad,
1092 blob[bl].p1,blob[bl].p2,
1093 blob[br].p1,blob[br].p2) ;
1095 printf(
" WARN: tb %d:%d, left: %d:%d, right %d:%d\n",tr_lo,tr_hi,
1096 blob[bl].t1,blob[bl].t2,
1097 blob[br].t1,blob[br].t2) ;
1101 blob[bl].p2 = pad+1 ;
1102 blob[bl].flags |= flags ;
1105 if(tr_hi>blob[bl].t2) blob[bl].t2 = tr_hi ;
1106 if(tr_lo<blob[bl].t1) blob[bl].t1 = tr_lo ;
1113 printf(
" WARN: %d: blob left %d, blob right %d???\n",row,bl,br) ;
1114 printf(
" WARN: pad %d: left: %d:%d, right %d:%d\n",pad,
1115 blob[bl].p1,blob[bl].p2,
1116 blob[br].p1,blob[br].p2) ;
1118 printf(
" WARN: tb %d:%d, left: %d:%d, right %d:%d\n",tr_lo,tr_hi,
1119 blob[bl].t1,blob[bl].t2,
1120 blob[br].t1,blob[br].t2) ;
1132 printf(
"BLOB %d right -- killed, merged into left %d\n",br,bl) ;
1137 blob[br].flags |= 0x80 ;
1139 blob_ix[br] = blob_ix[bl] ;
1143 printf(
"BLOB %d left -- killed, merged into right %d\n",bl,br) ;
1145 blob[bl].flags |= 0x80 ;
1147 blob_ix[bl] = blob_ix[br] ;
1163 if(merge==0) continue ;
1175 printf(
"ROW %2d: STAGE1: %d blobs, blob_merges %d\n",row,blob_cou-1,blob_merges) ;
1179 for(
int i=0;i<blob_cou;i++) {
1180 int ix = blob_ix[i] ;
1182 if(ix==i) continue ;
1185 if(blob[i].p1 < blob[ix].p1) blob[ix].p1 = blob[i].p1 ;
1186 if(blob[i].p2 > blob[ix].p2) blob[ix].p2 = blob[i].p2 ;
1187 if(blob[i].t1 < blob[ix].t1) blob[ix].t1 = blob[i].t1 ;
1188 if(blob[i].t2 > blob[ix].t2) blob[ix].t2 = blob[i].t2 ;
1198 int tpc23_base::evt_stop()
1206 LOG(TERR,
"evt_stop: S%02d: had %d last_ix, %d sequences",sector1,last_ix,sequence_cou) ;
1207 LOG(TERR,
" token %d, run_type %d, rows %d:%d",token,run_type,row_min,row_max) ;
1210 if((token<=0)||(token>=4096)) {
1215 if(run_type==1 || run_type==5 || no_cld) {
1222 for(
int row=row_min;row<=row_max;row++) {
1223 u_int *row_store = s2_dta ;
1226 if((s2_dta-s2_start)>(s2_max_words-1000)) {
1227 LOG(ERR,
"T %d: row %d: lots of CLD words %d vs %d, sequences %d -- skipping the rest",token,row,
1228 s2_dta-s2_start, s2_max_words-1000,
1235 if(rts_id==ITPC_ID) {
1247 wds = row_stage2(row) ;
1250 if(log_level>=2) LOG(TERR,
"row %d: words %d",row,wds) ;
1253 int words_per_cluster ;
1255 if(online) words_per_cluster = 2 ;
1256 else words_per_cluster = 5 ;
1258 if(rts_id==ITPC_ID) {
1259 row_store[0] = (words_per_cluster<<16)|row ;
1260 row_store[1] = 0x20220508 ;
1261 row_store[2] = wds ;
1264 row_store[0] = 0x20230000 | row ;
1265 row_store[1] = wds ;
1269 if(rts_id==ITPC_ID) {
1286 words = s2_dta - s2_start ;
1287 if(log_level>=1) LOG(TERR,
"T %d: %d CLD words",token,words) ;
1293 LOG(ERR,
"WTF: T %d: s2_dta???",token) ;
1298 for(
int row=row_min;row<=row_max;row++) {
1299 int p_max = rowlen[row] ;
1300 for(
int pad=1;pad<=p_max;pad++) {
1301 s1[row][pad].seq->t_hi = -1 ; ;
1310 int tpc23_base::evt_start()
1330 int tpc23_base::run_start()
1332 LOG(NOTE,
"%d: run_start: detector %d",
id,rts_id) ;
1335 if(rts_id==ITPC_ID) {
1336 s1_bytes = (ROW_MAX*PAD_MAX)*512*2 ;
1340 s1_bytes = (ROW_MAX*PAD_MAX)*512*2 ;
1343 if(online) LOG(INFO,
"%d: allocing %d s1_bytes, s1_t %d, blobs %d",
id,s1_bytes,
sizeof(s1),
sizeof(blob)) ;
1345 s1_dta = (u_short *) malloc(s1_bytes) ;
1350 memset(&s1,0xFF,
sizeof(s1)) ;
1358 memset(&f_stat,0,
sizeof(f_stat)) ;
1366 int tpc23_base::run_stop()
1370 if(online || mode) {
1371 for(
int i=0;i<10;i++) {
1372 f_stat.tm[i] /= f_stat.evt_cou ;
1375 LOG(NOTE,
"id %d: evts %d, means %f %f %f %f %f %f",
id,f_stat.evt_cou,
1376 f_stat.tm[0],f_stat.tm[1],f_stat.tm[2],f_stat.tm[3],f_stat.tm[4],
1384 tpc23_base::tpc23_base()
1386 LOG(NOTE,
"%s",__PRETTY_FUNCTION__) ;
1408 for(
int f=0;f<SIM_FIFOS;f++) {
1409 for(
int r=0;r<6;r++) {
1410 sim_dta[f].rb[r].mem = 0 ;
1429 store_track_id = 0 ;
1434 tpc23_base::~tpc23_base()
1438 if(s1_dta) free(s1_dta) ;
1439 if(s1_track_id) free(s1_track_id) ;
1440 if(s2_start) free(s2_start) ;
1441 if(store_track_id) free(store_track_id) ;
1445 for(
int f=0;f<SIM_FIFOS;f++) {
1446 for(
int r=0;r<6;r++) {
1447 if(sim_dta[f].
rb[r].mem) {
1449 free(sim_dta[f].
rb[r].mem) ;
1458 int tpc23_base::rdo_scan(
char *mem,
int words)
1460 LOG(ERR,
"%s: can't be",__PRETTY_FUNCTION__) ;
1465 int tpc23_base::from22to23(
char *dta,
int words)
1467 LOG(ERR,
"%s: can't be",__PRETTY_FUNCTION__) ;
1477 int tpc23_base::gains_from_cache(
const char *fname)
1484 for(
int s=0;s<24;s++) {
1485 for(
int r=0;r<=ROW_MAX;r++) {
1486 for(
int p=0;p<=PAD_MAX;p++) {
1487 rp_gain[s][r][p].gain = 1.0 ;
1488 rp_gain[s][r][p].t0 = 0.0 ;
1489 rp_gain[s][r][p].flags = 0 ;
1493 for(
int s=0;s<24;s++) {
1494 for(
int r=0;r<46;r++) {
1495 bad_fee_cou[s][r] = 0 ;
1500 if(strcasecmp(fname,
"none")==0) {
1501 LOG(WARN,
"Requesting no gain correction") ;
1507 if(rts_id==ITPC_ID) fname =
"/RTS/conf/itpc/itpc_gains.txt" ;
1508 else fname =
"/RTS/conf/tpx/tpx_gains.txt" ;
1511 FILE *f = fopen(fname,
"r") ;
1514 LOG(INFO,
"gains_from_cache: opened %s",fname) ;
1518 int sec,rdo,port,ch,row,pad ;
1523 if(fgets(buff,
sizeof(buff),f)==0) continue ;
1525 if(buff[0]==
'#') continue ;
1527 if(strlen(buff)<1) continue ;
1530 if(rts_id==ITPC_ID) {
1531 ret = sscanf(buff,
"%d %d %d %d %d %d %f %f",&sec,&rdo,&port,&ch,&row,&pad,&g,&t) ;
1532 if(ret != 8) continue ;
1535 LOG(WARN,
"S%02d:%d FEE #%d -- killed",sec,rdo,port) ;
1537 bad_fee[sec-1][rdo-1][bad_fee_cou[sec-1][rdo-1]] = port ;
1538 bad_fee_cou[sec-1][rdo-1]++ ;
1544 ret = sscanf(buff,
"%d %d %d %f %f",&sec,&row,&pad,&g,&t) ;
1545 if(ret != 5) continue ;
1551 LOG(WARN,
"S%02d:%d FEE #%d -- killed",sec,row,pad) ;
1553 bad_fee[sec-1][row-1][bad_fee_cou[sec-1][row-1]] = pad ;
1554 bad_fee_cou[sec-1][row-1]++ ;
1565 if(row>row_max) continue ;
1570 int flags = rp_gain[sec-1][row][pad].flags ;
1573 printf(
"gain %d %d %d = %f %f; %d\n",sec,row,pad,g,t,flags) ;
1576 rp_gain[sec-1][row][pad].gain = g ;
1577 rp_gain[sec-1][row][pad].t0 = t ;
1584 if(p2>rowlen[row]) p2 = rowlen[row] ;
1587 if(log_level>0) printf(
"dead edge row %d, pad %d %d %d: %d %d %d\n",row,pad,p1,p2,
1588 rp_gain[sec-1][row][pad].flags,
1589 rp_gain[sec-1][row][p1].flags,
1590 rp_gain[sec-1][row][p2].flags) ;
1592 rp_gain[sec-1][row][pad].flags |= FCF_DEAD_EDGE ;
1593 rp_gain[sec-1][row][p1].flags |= FCF_DEAD_EDGE ;
1594 rp_gain[sec-1][row][p2].flags |= FCF_DEAD_EDGE ;
1604 LOG(ERR,
"gains_from_cache: %s [%s]",fname,strerror(errno)) ;
1612 for(
int s=0;s<24;s++) {
1613 for(
int r=1;r<=row_max;r++) {
1622 rp_gain[s][r][1].flags |= FCF_ROW_EDGE ;
1624 if(rts_id==TPX_ID) {
1632 rp_gain[s][r][p_max].flags |= FCF_ROW_EDGE ;
1647 tpc23_base::sim_dta_t tpc23_base::sim_dta[SIM_FIFOS] ;
1649 int tpc23_base::load_replay(
const char *fname,
int sec_soft)
1661 if(rts_id==ITPC_ID) {
1662 s_start = sec_soft ;
1666 daq_itpc =
new class daq_itpc ;
1673 s_start = sec_soft ;
1677 s_start = (sec_soft-25)*2+1 ;
1678 s_stop = s_start + 1 ;
1681 daq_tpx =
new class daq_tpx ;
1685 LOG(INFO,
"Loading replay data from %s for sector %d[%d:%d], offset %d",fname,sec_soft,s_start,s_stop,offset) ;
1693 if(rdr->get(0,EVP_TYPE_ANY)) ;
1698 for(
int s=s_start;s<=s_stop;s++) {
1699 daq_dta *dd = rdr->det(det)->get(
"raw",s) ;
1701 while(dd && dd->iterate()) {
1708 if(rts_id==ITPC_ID) {
1717 if(rdo1<5) continue ;
1719 if(s==s_start) rix = rdo1 - 5 ;
1720 else rix = rdo1 - 5 + 2 ;
1724 int bytes = dd->ncontent ;
1725 char *c_addr = (
char *) dd->Void ;
1728 LOG(TERR,
"FIFO %d, RB %d, SR %d:%d: bytes %d",fix,rix,dd->sec,dd->rdo,bytes) ;
1730 int words = from22to23(c_addr,bytes/4) ;
1731 int n_bytes = words * 4 ;
1735 sim_dta[fix].rb[rix].bytes = n_bytes ;
1736 sim_dta[fix].rb[rix].mem = (
char *) malloc(n_bytes) ;
1737 memcpy(sim_dta[fix].
rb[rix].mem,c_addr,n_bytes) ;
1747 if(fix==SIM_FIFOS) break ;
1755 LOG(INFO,
"Loaded %d FIFOs",fix) ;
1757 if(rdr)
delete rdr ;
1758 if(daq_tpx)
delete daq_tpx ;
1759 if(daq_itpc)
delete daq_itpc ;