forked from star-bnl/star-sw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtpxFCF.cxx
More file actions
1698 lines (1188 loc) · 35.3 KB
/
tpxFCF.cxx
File metadata and controls
1698 lines (1188 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This code is property of Brookhaven National Laboratory, USA.
Authored by Tonko Ljubicic.
No modifications are to be made without the express consent of
the author.
Version 6.00 6/18/2007 Brand new TPX version
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <rtsLog.h>
#include <I386/i386Lib.h>
#include <TPC/rowlen.h>
#include "tpxGain.h"
#include "tpxFCF.h"
#include <DAQ_READER/daq_dta_structs.h>
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
char *tpxFCF::fcf_flags(u_char flags)
{
static char c_flags[32] ;
c_flags[0] = 0 ; // empty string...
if(flags & FCF_IN_DOUBLE) strcat(c_flags,"?") ; // can't be
if(flags & FCF_DEAD_EDGE) strcat(c_flags,"D") ;
if(flags & FCF_BROKEN_EDGE) strcat(c_flags,"B") ;
if(flags & FCF_ROW_EDGE) strcat(c_flags,"E") ;
if(flags & FCF_BIG_CHARGE) strcat(c_flags,"c") ;
if(flags & FCF_DOUBLE_T) strcat(c_flags,"!") ; // can't be
if(flags & FCF_MERGED) strcat(c_flags,"m") ;
if(flags & FCF_ONEPAD) strcat(c_flags,"1") ;
if(c_flags[0] == 0) strcpy(c_flags,"0") ;
return c_flags ;
}
int tpxFCF::afterburner(int cou, daq_cld *store[])
{
int merged ;
if(likely(cou == 0)) return 0 ; // most cases like this...
//printf("Start afterburner: cou %d\n",cou) ;
merged = 0 ;
for(int i=0;i<cou;i++) {
daq_cld *l = store[i] ; // left guy
int merge_ix = -1 ;
if(l->charge == 0) continue ; // already merged
if(l->flags & FCF_BROKEN_EDGE) ;
else continue ;
int type = 0 ;
for(int j=0;j<cou;j++) {
int ok = 0 ;
if(j<=i) continue ;
daq_cld *r = store[j] ; // right guy
if(r->charge == 0) continue ; // already merged!
if(r->flags & FCF_BROKEN_EDGE) ;
else continue ;
if(r->p1==13) { // right
if(l->p2 != 12) continue ;
type = 1 ;
}
else if(r->p2==12) {
if(l->p1 != 13) continue ;
type = 2 ;
}
else if(r->p1==131) {
if(l->p2 != 130) continue ;
type = 3 ;
}
else if(r->p2==130) {
if(l->p1 != 131) continue ;
type = 4 ;
}
else { // can happen if one was deconvoluted...
continue ;
type = 5 ;
}
if((r->t1 >= l->t1) && (r->t1 <= l->t2)) ok = 1 ;
if((r->t2 >= l->t1) && (r->t2 <= l->t2)) ok = 1 ;
if(ok==0) continue ;
merge_ix = j ;
break ;
}
if(merge_ix < 0) { // no merge -- let it go...
}
else {
// merge...
merged++ ;
// merge
daq_cld *r = store[merge_ix] ;
double charge = l->charge + r->charge ;
/*
printf("can merge %d: L%d: [%d:%d] %f, f 0x%02X, R%d: [%d:%d] %f f 0x%02X\n",
type,
i,l->p1,l->p2,l->tb,l->flags,
merge_ix,r->p1,r->p2,r->tb,r->flags) ;
*/
l->tb = (l->tb * l->charge + r->tb * r->charge) / charge ;
l->pad = (l->pad * l->charge + r->pad * r->charge) / charge ;
if(r->t1 < l->t1) l->t1 = r->t1 ;
if(r->t2 > l->t2) l->t2 = r->t2 ;
if(r->p1 < l->p1) l->p1 = r->p1 ;
if(r->p2 > l->p2) l->p2 = r->p2 ;
l->flags |= r->flags ; // merge flags
l->flags &= ~FCF_ONEPAD ; // remove onepad, by dedfinition.
// bug fixed...
if(charge > (double)0x7FFF) {
int ch = (int) (charge+1) ; // roundoff
ch /= 1024 ; // do what FCF does...
l->charge = (u_short) ch * 1024 ; // do what decoder does...
l->flags |= FCF_BIG_CHARGE ;
}
else {
l->charge = (u_short) charge ;
}
l->flags &= ~FCF_BROKEN_EDGE ;
// and kill the right guy!
r->flags |= FCF_DEAD_EDGE ;
r->charge = 0 ;
}
}
// cleanup flags
for(int i=0;i<cou;i++) {
daq_cld *l = store[i] ; // left guy
// printf("AfterB: %2d: P [%d:%d], T [%d:%d], flags %d, charge %d\n",i,l->p1,l->p2,l->t1,l->t2,l->flags,l->charge) ;
if(l->flags & (FCF_ONEPAD | FCF_DEAD_EDGE | FCF_ROW_EDGE)) { // is this is still on, kill it with dead edge
l->flags |= FCF_DEAD_EDGE ;
l->charge = 0 ;
}
else {
l->flags &= ~FCF_BROKEN_EDGE ; // remove this flag since we ran the afterburner
}
}
return merged ;
}
int tpxFCF::fcf_decode(u_int *p_buff, daq_cld *dc, u_short version)
{
double p, t ;
int p1,p2,t1,t2,cha,fla ;
u_int p_tmp, t_tmp ;
// pad
p_tmp = *p_buff & 0xFFFF ;
// time
t_tmp = *p_buff >> 16 ;
if(version==FCF_V_FY08) { // had a bug in FY08...
p = (double)(p_tmp & 0x3FFF) / 32.0 ;
t = (double)(t_tmp & 0x7FFF) / 32.0 ;
}
else {
p = (double)(p_tmp & 0x3FFF) / 64.0 ;
t = (double)(t_tmp & 0x7FFF) / 64.0 ;
}
fla = 0 ;
if(p_tmp & 0x8000) fla |= FCF_MERGED ;
if(p_tmp & 0x4000) fla |= FCF_DEAD_EDGE ;
if(t_tmp & 0x8000) fla |= FCF_ONEPAD ;
p_buff++ ; // advance to next word
cha = *p_buff >> 16 ;
if(cha >= 0x8000) { // special case of very large charge...
//printf("1: Big cha: 0x%08X %d; %f %f\n",cha,cha,p,t) ;
fla |= FCF_BIG_CHARGE ;
cha = (cha & 0x7FFF) * 1024 ;
// if(cha == 0) cha = 0x8000; // exaclty, but can't be I think...
//printf("2: Big cha: 0x%08X %d\n",cha,cha) ;
// quasi fix of the very large problem...
if(cha > 0xFFFF) cha = 0xFFFF ; // because the daq_cld structure has charge as a short... damn...
}
p_tmp = *p_buff & 0xFFFF ; // reuse p_tmp
if(p_tmp & 0x8000) fla |= FCF_ROW_EDGE ;
if(p_tmp & 0x4000) fla |= FCF_BROKEN_EDGE ;
t1 = p_tmp & 0xF ;
t2 = (p_tmp >> 4) & 0xF ;
p1 = (p_tmp >> 8) & 0x7 ;
p2 = (p_tmp >> 11) & 0x7 ;
t1 = (int)t - t1 ;
t2 = (int)t + t2 ;
p1 = (int)p - p1 ;
p2 = (int)p + p2 ;
dc->t1 = t1 ;
dc->t2 = t2 ;
dc->p1 = p1 ;
dc->p2 = p2 ;
dc->charge = cha ; // this is a problem for BIG_CHARGE... it will strip the upper bits... unsolved.
dc->flags = fla ;
dc->pad = p ;
dc->tb = t ;
return 2 ; // 2 u_ints used
}
int tpxFCF::fcf_decode(u_int *p_buff, daq_sim_cld *sdc, u_short version)
{
int skip = fcf_decode(p_buff,&(sdc->cld), version) ;
sdc->track_id = 0 ;
sdc->quality = 0 ;
p_buff += skip ;
sdc->quality = (*p_buff) >> 16 ;
sdc->track_id = (*p_buff) & 0xFFFF ;
//LOG(TERR,"Q %d, T %d",sdc->quality, sdc->track_id) ;
// took one int so...
skip++ ;
return skip ;
}
tpxFCF::s_static_storage *tpxFCF::gain_storage[24][256] ;
tpxFCF::tpxFCF()
{
rbs = 0 ;
sector = 0 ;
modes = 0 ;
rdo = 0 ;
fcf_style = 0 ;
memset(working_storage,0,sizeof(working_storage)) ;
cl_marker = 0 ;
my_id = -1 ;
run_compatibility = 9 ; // run 9
do_cuts = 2 ; // 1 means always, 2 means don't cut edges (for i.e. pulser run), 0 means don't...
ch_min = 10 ;
row_count = 45 ; // default for our usual TPC
tpx_rowlen = tpc_rowlen ; // default for out usual TPC
tpx_padplane = 0 ; // original padplane
memset(row_ix,0xFF,sizeof(row_ix)) ;
storage = 0 ;
read_version = 0 ;
do_version = FCF_V_FY09 ;
return ;
}
tpxFCF::~tpxFCF()
{
if(storage) {
free(storage) ;
storage = 0 ;
}
for(int s=0;s<24;s++) {
for(int r=0;r<256;r++) {
if(working_storage[s][r]) {
free(working_storage[s][r]) ;
working_storage[s][r] = 0 ;
}
if(gain_storage[s][r]) {
free(gain_storage[s][r]) ;
gain_storage[s][r] = 0;
}
}
}
return ;
}
/*
Called once at startup to allocate the static storage
*/
void tpxFCF::config2(int sec1, int rdo1, int mode, int rows, unsigned char *rowlen)
{
int rdo0 = rdo1 - 1 ;
int sec0 = sec1 - 1 ;
//temporary
sector = sec1 ;
modes = mode ;
if((rowlen==0) || (rows<=0)) {
tpx_rowlen = tpc_rowlen ;
row_count = 45 ; // force override
tpx_padplane = 0 ; // original
}
else {
tpx_rowlen = rowlen ;
row_count = rows ;
tpx_padplane = 1 ; // new padplane
}
int tot_count = 0 ;
for(int a=0;a<256;a++) {
for(int ch=0;ch<16;ch++) {
int pad, row ;
tpx_from_altro(rdo0,a,ch,row,pad) ;
if(row > 250) continue ; // not in this RDO...
if(row == 0) continue ; // will nix row 0 as well...
int bytes = tpx_rowlen[row] * sizeof(s_static_storage) ;
if(gain_storage[sec0][row]) ;
else {
gain_storage[sec0][row] = (s_static_storage *)valloc(bytes) ;
//and clear
memset(gain_storage[sec0][row],0, bytes) ;
tot_count += tpx_rowlen[row] ;
}
s_static_storage *ss = get_static(row,pad) ;
ss->f = FCF_NEED_PAD | FCF_ONEPAD ;
ss->g = 1.0 ;
ss->t0 = 0.0 ;
}
}
LOG(TERR,"config2: S%2d, RDO %d: %d pads",sec1,rdo1,tot_count) ;
}
void tpxFCF::config(u_int mask, int mode, int rows, unsigned char *rowlen)
{
rbs = mask ;
modes = mode ;
int r ;
int row, pad ;
int a, ch ;
memset(row_ix,0,sizeof(row_ix)) ;
if((rowlen==0) || (rows<=0)) {
tpx_rowlen = tpc_rowlen ;
row_count = 45 ; // force override
tpx_padplane = 0 ; // original
}
else {
tpx_rowlen = rowlen ;
row_count = rows ;
tpx_padplane = 1 ; // new padplane
}
//LOG(WARN,"calling config: mask 0x%X, mode %d, rows %3d",mask,mode,row_count) ;
// There is some amount of acrobatics involved so
// bear with me...
if(tpx_padplane) { // some new padplane
for(row=1;row<=row_count;row++) row_ix[row] = 1 ;
}
else { // original TPC padplane
// First, we figure out which rows are needed from the RB mask
for(r=0;r<6;r++) { // hell, let's assume 32 RDOs per sector...
if(rbs & (1<<r)) {
for(a=0;a<256;a++) {
for(ch=0;ch<16;ch++) {
tpx_from_altro(r,a,ch,row,pad) ;
if(row > 250) continue ; // not in this RDO...
if(row == 0) continue ; // will nix row 0 as well...
row_ix[row] = 1 ; // mark as needed...
}
}
}
}
}
// get the count of pads needed assuming _whole_ rows!
int tot_count = 0 ;
for(row=0;row<=row_count;row++) {
if(row_ix[row]) {
tot_count += tpx_rowlen[row] ; // allocate whole rows!
}
}
LOG(NOTE,"fcfconfig: RDO mask 0x%03X: allocated %d pads (%d bytes)",rbs,tot_count,tot_count * sizeof(struct stage1)) ;
// allocate storage
if(storage) {
LOG(WARN,"Whoa! Storage already allocated!") ;
free(storage) ;
}
storage = (struct stage1 *) valloc(tot_count * sizeof(struct stage1)) ;
LOG(NOTE,"FCF for mask 0x%02X: alloced %d bytes for %d tot_count X %d; cleared gains",mask,
tot_count * sizeof(struct stage1),
tot_count,
sizeof(struct stage1)) ;
// clear storage
memset(storage,0,tot_count * sizeof(struct stage1)) ;
// re-create offsets which we use in the row+pad navigation
tot_count = 0 ; // re use...
for(row=0;row<=row_count;row++) {
if(row_ix[row] == 0) {
row_ix[row] = -1 ; // nix!
continue ;
}
row_ix[row] = tot_count ;
tot_count += tpx_rowlen[row] ;
}
// OK -- we have the storage and the navigation via get_stage1(row,pad) ready.
// Now let's get the per-pad flags ready:
if(tpx_padplane) {
for(row=1;row<=row_count;row++) {
for(pad=1;pad<=tpx_rowlen[row];pad++) {
get_stage1(row,pad)->f = FCF_NEED_PAD | FCF_ONEPAD ;
get_stage1(row,pad)->g = 1.0 ;
get_stage1(row,pad)->t0 = 0.0 ;
}
}
}
else {
// Mark pads which are there as present...
for(r=0;r<6;r++) {
if(rbs & (1<<r)) {
for(a=0;a<256;a++) {
for(ch=0;ch<16;ch++) {
tpx_from_altro(r,a,ch,row,pad) ;
if(row > 250) continue ;
if(row == 0) continue ;
get_stage1(row, pad)->f = FCF_NEED_PAD | FCF_ONEPAD ;
get_stage1(row, pad)->g = 1.0 ;
get_stage1(row, pad)->t0 = 0.0 ;
}
}
}
}
}
return ;
}
/*
This might me reapplied...
*/
void tpxFCF::apply_gains2(tpxGain *gain)
{
// gains = gain ;
if(tpx_padplane) gain = 0 ; // force it!
if(gain == 0) {
LOG(WARN,"Sector %2d, gains NULL",sector) ;
}
else {
LOG(NOTE,"Applying gains to sector %d [%p ?]",sector,gain) ;
}
// clear all flags but the existing higher ones
for(int s=0;s<24;s++) {
for(int r=0;r<256;r++) {
if(gain_storage[s][r] == 0) continue ;
sector = s+1 ;
rdo = r+1 ;
for(int row=1;row<=row_count;row++) {
for(int pad=1;pad<=tpx_rowlen[row];pad++) {
s_static_storage *ss = get_static(row,pad) ;
if(ss==0) continue ;
ss->f &= 0xFF00 ;
}
}
}
}
for(int s=0;s<24;s++) {
for(int r=0;r<256;r++) {
if(gain_storage[s][r] == 0) continue ;
sector = s+1 ;
rdo = r+1 ;
for(int row=1;row<=row_count;row++) {
for(int pad=1;pad<=tpx_rowlen[row];pad++) {
s_static_storage *ss = get_static(row,pad) ;
int kill = 0 ;
if(ss==0) continue ;
if(gain) {
ss->g = gain->get_gains(sector,row,pad)->g ;
ss->t0 = gain->get_gains(sector,row,pad)->t0 ;
}
else {
ss->g = 1.0 ;
ss->t0 = 0.0 ;
}
int fl = 0 ;
if(tpx_fy16_map==0) {
if(!(ss->f & FCF_NEED_PAD)) { // not in this RDO i.e. row 8 is split between RDO1 and RDO2
fl |= FCF_BROKEN_EDGE ;
LOG(TERR,"Broken edge in RDO %d: RP %d:%d",rdo,row,pad) ;
}
}
if(ss->g < 0.001) {
fl |= FCF_DEAD_EDGE ;
kill |= FCF_KILLED_PAD ; // this pad is really dead!!!
}
if(fl) { // apply the above flags to adjecant pads as well
if(pad>1) {
get_static(row,pad-1)->f |= fl ;
}
if(pad < tpx_rowlen[row]) {
get_static(row,pad+1)->f |= fl ;
}
}
// marks the first and last pad as "edge"
if((pad==1) || (pad==tpx_rowlen[row])) {
fl |= FCF_ROW_EDGE ;
}
ss->f |= fl | FCF_ONEPAD | kill ;
//if(row<=13) {
// LOG(WARN,"S %d:%d, RP %d:%d is 0x%X",sector,rdo,row,pad,ss->f) ;
//}
}
}
}
}
return ;
}
/*
This might me reapplied...
*/
void tpxFCF::apply_gains(int sec, tpxGain *gain)
{
sector = sec ;
gain = gain ;
int row, pad ;
if(tpx_padplane) gain = 0 ; // force it!
LOG(WARN,"apply_gains???") ;
if(gain == 0) {
LOG(WARN,"Sector %2d, gains NULL",sector) ;
}
else {
LOG(NOTE,"Applying gains to sector %d [%p ?]",sector,gain) ;
}
// clear all flags but the existing ones
for(row=1;row<=row_count;row++) {
if(row_ix[row] < 0) continue ;
for(pad=1;pad <= tpx_rowlen[row]; pad++) {
get_stage1(row, pad)->f &= 0xFF00 ; // clear, keeping upper bits intact...
}
}
// put gains & flags
for(row=1;row<=row_count;row++) {
if(row_ix[row] < 0) continue ;
for(pad=1;pad<=tpx_rowlen[row];pad++) {
stage1 *s = get_stage1(row, pad) ;
if(gain) {
s->g = gain->get_gains(sector,row,pad)->g ;
s->t0 = gain->get_gains(sector,row,pad)->t0 ;
}
else {
s->g = 1.0 ;
s->t0 = 0.0 ;
}
u_int fl = 0 ;
if(!(s->f & FCF_NEED_PAD)) { // not really here; missing in the RDO...
//LOG(WARN,"Applying broken edge to row:pad %d:%d",row,pad) ;
fl |= FCF_BROKEN_EDGE ;
}
if(s->g == 0.0) { // dead
fl |= FCF_DEAD_EDGE ;
}
// for these we must mark adjacent pads!
if(fl) {
if(pad>1) {
get_stage1(row,pad-1)->f |= fl ; // bad_edge
}
if(pad < tpx_rowlen[row]) {
get_stage1(row,pad+1)->f |= fl ; // bad_edge ;
}
}
// marks the first and last pad as "edge"
if((pad==1) || (pad==tpx_rowlen[row])) {
fl |= FCF_ROW_EDGE ;
}
s->f |= fl | FCF_ONEPAD ;
LOG(DBG,"FCF gains: row %2d, pad %3d: gain %f, flags 0x%04X",row,pad,s->g,s->f) ;
}
}
return ;
}
void tpxFCF::start_evt2(int sec1, int rdo1)
{
cl_marker = 10000 ; // used to mark unique clusters sector...
// LOG(TERR,"start_evt2: START: %d %d",sec1,rdo1) ;
sector = sec1 ;
rdo = rdo1 ;
for(int r=1;r<=row_count;r++) {
if(gain_storage[sector-1][r] == 0) continue ; //this is an optimization thing...
for(int p=1;p<=tpx_rowlen[r];p++) {
struct stage1 *w ;
w = get_working(r, p) ;
if(unlikely(w==0)) {
LOG(ERR,"[%d] S%02d:%d: no row pad %d:%d???",my_id,sector,rdo,r,p) ;
}
else {
//LOG(TERR,"[%d] S%02d:%d: got rp %d:%d",my_id,sector,rdo,r,p) ;
w->count = 0 ;
}
}
}
// LOG(TERR,"start_evt2: END: %d %d",sec1,rdo1) ;
return ;
}
void tpxFCF::start_evt()
{
cl_marker = 10000 ; // used to mark unique clusters sector...
for(int r=1;r<=row_count;r++) {
if(row_ix[r] < 0) continue ;
for(int p=1;p<=tpx_rowlen[r];p++) {
struct stage1 *o ;
o = get_stage1(r, p) ;
if(unlikely(o==0)) {
LOG(ERR,"No row pad %d:%d???",r,p) ;
}
else {
o->count = 0 ;
}
}
}
return ;
}
int tpxFCF::do_pad(tpx_altro_struct *a, daq_sim_adc_tb *sim_adc)
{
struct stage1 *s ;
if(unlikely(a->row > 250)) return 0 ;
if(unlikely(a->row == 0)) return 0 ;
if(unlikely(a->pad > tpx_rowlen[a->row])) return 0 ;
if(fcf_style) {
s = get_working(a->row,a->pad) ;
}
else {
s = get_stage1(a->row, a->pad) ;
}
if(unlikely(s==0)) {
LOG(ERR,"[%d] Whoa -- no row:pad %d:%d???",my_id,a->row,a->pad) ;
return 0 ;
}
s->count = 0 ;
if(unlikely(a->count<=1)) return 0 ; // no point....
// HACK put in on Mar 6th, 2009 to suppress those
// long strips (>=415 tb) of unknown nature (to that date).
// After run 10065096.
// Tonko.
// Actually, I will leave this cut in and lower to 400
if(unlikely(a->count >= 400)) {
//LOG(WARN,"count %d on r:p %d:%d",a->count,a->row,a->pad) ;
return 0 ;
}
u_int flags ;
u_int orig_flags ;
if(fcf_style) {
struct s_static_storage *ss ;
ss = get_static(a->row,a->pad) ;
if(unlikely(ss->g <= 0.001)) {
//LOG(WARN,"Killed %d:%d",a->row,a->pad) ;
return 0 ;
}
orig_flags = flags = ss->f & 0xFF ;
}
else {
// kill, by hand, the pad which has 0.0 gain just to avoid confusion
// when the gains i.e. in Offline are misapplied
if(unlikely(s->g <= 0.001)) {
//LOG(WARN,"Killed %d:%d",a->row,a->pad) ;
return 0 ;
}
orig_flags = flags = s->f & 0xFF ;
}
// if(a->row <= 13) {
// LOG(TERR,"Doing %d:%d %d",a->row,a->pad,a->count) ;
// }
u_int t_ave, charge ;
u_int tb_start ;
u_int last_falling, last_adc ;
u_int tb_prev ;
int new_cluster ;
struct tpxFCF_cl *cl, *cl_max ;
cl = s->cl ; // start...
cl_max = &(s->cl[FCF_MAX_CL]) ; // end of cl sentinel...
u_int max_adc = 0 ;
last_falling = last_adc = 0 ;
t_ave = charge = 0 ;
tb_prev = tb_start = a->tb[0] ;
new_cluster = 0 ;
// start the loop over raw pixels in this pad...
for(int i=0;likely( i < a->count );i++) {
u_int adc, tb ;
adc = a->adc[i] ;
tb = a->tb[i] ;
// printf("........looking at %d %d %d %d\n",a->row,a->pad,tb,adc) ;
if(unlikely( adc <= 1 )) { // possible due to ALTROs way of sticking sequences...
continue ;
}
if(unlikely( tb < (tb_prev - 1) )) { // end of previous sequence
new_cluster |= FCF_ONEPAD ;
}
if(unlikely( last_falling )) {
if(unlikely( last_falling > FCF_MIN_WIDTH )) {
if(unlikely( adc > (last_adc + FCF_ADC_NOISE) )) {
flags |= FCF_DOUBLE_T ;
new_cluster |= FCF_DOUBLE_T ;
}
}
else {
last_falling++ ;
}
}
else {
if(unlikely( (adc + FCF_ADC_NOISE) < last_adc )) {
last_falling = 1 ;
}
}
if(unlikely( new_cluster )) {
if(likely(max_adc >= FCF_ADC_MIN)) {
cl->t_ave = t_ave ;
cl->charge = charge ;
cl->t1 = tb_prev; // put previous; t1 is the lower value
cl->t2 = tb_start ;
cl->flags = flags ;
cl++ ;
// protect storage!
if(unlikely( cl >= cl_max )) goto pad_done ; // to many!
}
// prepare for new one...
if(unlikely( new_cluster & FCF_DOUBLE_T )) ;
// else flags = s->f & 0xFF ;
else flags = orig_flags & 0xFF ;
t_ave = charge = 0 ;
tb_start = tb ;
last_falling = 0 ;
max_adc = 0 ;
new_cluster = 0 ;
}
if(unlikely( adc > max_adc )) {
max_adc = adc ;
}
last_adc = adc ;
tb_prev = tb ;
t_ave += adc * tb ;
charge += adc ;
} // scan over the sequence
// finish off the rest...
if(likely(max_adc >= FCF_ADC_MIN)) {
cl->t1 = tb_prev ; // t1 is the lower value
cl->t2 = tb_start ;
cl->flags = flags ;
cl->charge = charge ;
cl->t_ave = t_ave ;
cl++ ;
}
// goto target...
pad_done: ;
s->count = cl - s->cl ; // count of 1d clusters
if(unlikely( s->count == 0 )) {
//LOG(DBG,"No 1D clusters?") ;
}
else {
if(unlikely( s->count >= FCF_MAX_CL )) {
LOG(NOTE,"Row %d:%d, %d clusters [max!]",a->row,a->pad,s->count) ;
}
else {
//LOG(DBG,"Row %d:%d, %d clusters",a->row,a->pad,s->count) ;
}
}
#ifdef FCF_DEBUG