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 7, 4},{ 7, 4},{ 7, 4}},
26 { { 8, 0},{ 8, 1},{ 8, 1},{ 8, 2},{ 8, 2},{ 8, 3},{ 8, 4},{ 8, 4},{ 8, 4}},
27 { { 8, 0},{ 8, 1},{ 8, 1},{ 8, 2},{ 8, 2},{ 8, 3},{ 8, 4},{ 8, 4},{ 8, 4}}
30 static const int EtoH3map[15][9][4] = {
31 {{-1,-1,-1, 0},{-1,-1, 0, 1},{-1,-1, 1, 2},{-1,-1, 1, 2},{-1,-1, 2, 3},{-1,-1, 2, 3},{-1,-1, 3, 4},{-1,-1, 4,-1},{-1,-1, 4,-1}},
32 {{-1, 0,-1, 5},{ 0, 1, 5, 6},{ 1, 2, 6, 7},{ 1, 2, 6, 7},{ 2, 3, 7, 8},{ 2, 3, 7, 8},{ 3, 4, 8, 9},{ 4,-1, 9,-1},{ 4,-1, 9,-1}},
33 {{-1, 0,-1, 5},{ 0, 1, 5, 6},{ 1, 2, 6, 7},{ 1, 2, 6, 7},{ 2, 3, 7, 8},{ 2, 3, 7, 8},{ 3, 4, 8, 9},{ 4,-1, 9,-1},{ 4,-1, 9,-1}},
34 {{-1, 5,-1,10},{ 5, 6,10,11},{ 6, 7,11,12},{ 6, 7,11,12},{ 7, 8,12,13},{ 7, 8,12,13},{ 8, 9,13,14},{ 9,-1,14,-1},{ 9,-1,14,-1}},
35 {{-1,10,-1,15},{10,11,15,16},{11,12,16,17},{11,12,16,17},{12,13,17,18},{12,13,17,18},{13,14,18,19},{14,-1,19,-1},{14,-1,19,-1}},
36 {{-1,10,-1,15},{10,11,15,16},{11,12,16,17},{11,12,16,17},{12,13,17,18},{12,13,17,18},{13,14,18,19},{14,-1,19,-1},{14,-1,19,-1}},
37 {{-1,15,-1,20},{15,16,20,21},{16,17,21,22},{16,17,21,22},{17,18,22,23},{17,18,22,23},{18,19,23,24},{19,-1,24,-1},{19,-1,24,-1}},
38 {{-1,20,-1,25},{20,21,25,26},{21,22,26,27},{21,22,26,27},{22,23,27,28},{22,23,27,28},{23,24,28,29},{24,-1,29,-1},{24,-1,29,-1}},
39 {{-1,20,-1,25},{20,21,25,26},{21,22,26,27},{21,22,26,27},{22,23,27,28},{22,23,27,28},{23,24,28,29},{24,-1,29,-1},{24,-1,29,-1}},
40 {{-1,25,-1,30},{25,26,30,31},{26,27,31,32},{26,27,31,32},{27,28,32,33},{27,28,32,33},{28,29,33,34},{29,-1,34,-1},{29,-1,34,-1}},
41 {{-1,25,-1,30},{25,26,30,31},{26,27,31,32},{26,27,31,32},{27,28,32,33},{27,28,32,33},{28,29,33,34},{29,-1,34,-1},{29,-1,34,-1}},
42 {{-1,30,-1,35},{30,31,35,36},{31,32,36,37},{31,32,36,37},{32,33,37,38},{32,33,37,38},{33,34,38,39},{34,-1,39,-1},{34,-1,39,-1}},
43 {{-1,35,-1,40},{35,36,40,41},{36,37,41,42},{36,37,41,42},{37,38,42,43},{37,38,42,43},{38,39,43,44},{39,-1,44,-1},{39,-1,44,-1}},
44 {{-1,35,-1,40},{35,36,40,41},{36,37,41,42},{36,37,41,42},{37,38,42,43},{37,38,42,43},{38,39,43,44},{39,-1,44,-1},{39,-1,44,-1}},
45 {{-1,40,-1,-1},{40,41,-1,-1},{41,42,-1,-1},{41,42,-1,-1},{42,43,-1,-1},{42,43,-1,-1},{43,44,-1,-1},{44,-1,-1,-1},{44,-1,-1,-1}}
52 if(fcs_trgDebug>=2) printf(
"Stage2v1 ns=%d\n",ns);
55 static u_int ETbTdep[16][10];
56 static u_int ETbTadr[16][10];
57 static u_int HTbTdep[10][6];
58 static u_int HTbTadr[10][6];
62 for(
int r=0; r<16; r++){
63 printf(
"Ecal r=%2d : ",r);
64 for(
int c=0; c<10; c++){
65 ETbTdep[r][c]= c/2 + (r/4)*5;
66 ETbTadr[r][c]= c%2 + (r%4)*2;
67 printf(
"%2d-%1d ",ETbTdep[r][c],ETbTadr[r][c]);
72 for(
int r=0; r<10; r++){
73 printf(
"HCal r=%2d : ",r);
74 for(
int c=0; c<6; c++){
82 HTbTdep[r][c]= c/2 + ((r-1)/4)*3;
83 HTbTadr[r][c]= c%2 + ((r-1)%4)*2;
85 printf(
"%2d-%1d ",HTbTdep[r][c],HTbTadr[r][c]);
93 for(
int i=0;i<DEP_ECAL_TRG_COU;i++) {
94 for(
int j=0;j<8;j++) {
95 if(s2_ch_mask[geo.ns] & (1ll<<i)) {
99 if(ecal[i].d[j] > EHTTHR) ecal_ht |= 1 ;
105 for(
int i=0;i<DEP_HCAL_TRG_COU;i++) {
106 for(
int j=0;j<8;j++) {
107 if(s2_ch_mask[geo.ns] & (1ll<<(20+i))) {
111 if(hcal[i].d[j] > HHTTHR) hcal_ht |= 1 ;
117 for(
int i=0;i<DEP_PRE_TRG_COU;i++) {
118 for(
int j=0;j<8;j++) {
119 if(s2_ch_mask[geo.ns] & (1ll<<(28+i))) {
123 if(pres[i].d[j]) fpre_or |= 1 ;
128 for(
int r=0; r<16; r++){
129 for(
int c=0; c<10; c++){
130 e2x2[ns][r][c]=ecal[ETbTdep[r][c]].d[ETbTadr[r][c]];
134 for(
int r=0; r<10; r++){
135 for(
int c=0; c<6; c++){
136 h2x2[ns][r][c]=hcal[HTbTdep[r][c]].d[HTbTadr[r][c]];
142 for(
int r=0; r<9; r++){
143 if(fcs_trgDebug>=2) printf(
"H4x4 ");
144 for(
int c=0; c<5; c++){
146 = hcal[HTbTdep[r ][c ]].d[HTbTadr[r ][c ]]
147 + hcal[HTbTdep[r ][c+1]].d[HTbTadr[r ][c+1]]
148 + hcal[HTbTdep[r+1][c ]].d[HTbTadr[r+1][c ]]
149 + hcal[HTbTdep[r+1][c+1]].d[HTbTadr[r+1][c+1]];
151 if(fcs_trgDebug>=2) printf(
"%5d ",hsum[ns][r][c]);
153 if(fcs_trgDebug>=2) printf(
"\n");
158 for(
int dep=0; dep<6; dep++) {
159 for(
int j=0; j<4; j++) {
160 for(
int k=0; k<8; k++){
161 phit[ns][dep][j*8+k] = (pres[dep].d[j] >> k) & 0x1;
167 for(
int dep=0; dep<6; dep++) {
168 printf(
"PRES NS%1d DEP%1d : ",ns,dep);
169 for(
int j=0; j<4; j++) {
170 for(
int k=0; k<8; k++){
171 phit[ns][dep][j*8+k] = (pres[dep].d[j] >> k) & 0x1;
172 printf(
"%1d", (pres[dep].d[j]>>k)&0x1);
185 u_int EM2 =0, EM1 =0, EM0=0, EM3=0;
186 u_int ELE2=0, ELE1=0, ELE0=0;
187 u_int HAD2=0, HAD1=0, HAD0=0;
188 u_int ETOT=0, HTOT=0;
189 for(
int r=0; r<15; r++){
190 if(fcs_trgDebug>=2) printf(
"EM4x4 ");
191 for(
int c=0; c<9; c++){
193 = ecal[ETbTdep[r ][c ]].d[ETbTadr[r ][c ]]
194 + ecal[ETbTdep[r ][c+1]].d[ETbTadr[r ][c+1]]
195 + ecal[ETbTdep[r+1][c ]].d[ETbTadr[r+1][c ]]
196 + ecal[ETbTdep[r+1][c+1]].d[ETbTadr[r+1][c+1]];
200 u_int h=hsum[ns][EtoHmap[r][c][0]][EtoHmap[r][c][1]];
204 for(
int iz=0; iz<4; iz++){
205 int iHCalID = EtoH3map[r][c][iz];
206 if(iHCalID < 0)
continue;
207 int irow = iHCalID/5;
208 int icol = iHCalID%5;
209 if(hmax < hsum[ns][irow][icol]) hmax = hsum[ns][irow][icol];
215 sum[ns][r][c] = esum[ns][r][c] + h;
216 summax[ns][r][c] = esum[ns][r][c] + hmax;
219 if(sum[ns][r][c]==0) {
222 ratio[ns][r][c] = float(esum[ns][r][c]) / float(sum[ns][r][c]);
224 if(esum[ns][r][c]+hmax==0){
225 ratiomax[ns][r][c]=0.0;
227 ratiomax[ns][r][c]=float(esum[ns][r][c]) / float(summax[ns][r][c]);
232 for(
int dep=0; dep<6; dep++){
234 if(fcs_readPresMaskFromText==0){
235 mask = fcs_ecal_epd_mask[r][c][dep];
237 mask = PRES_MASK[r][c][dep];
239 for(
int j=0; j<4; j++) {
240 for(
int k=0; k<8; k++){
241 if( (mask >> (j*8 + k)) & 0x1) {
242 epdcoin[ns][r][c] |= (pres[dep].d[j] >> k) & 0x1;
251 u_int hmax128 = hmax*128 ;
253 if(hmax128 <= esum[ns][r][c] * EM_HERATIO_THR){
254 if(esum[ns][r][c] > EMTHR2) EM2 = 1;
255 if(esum[ns][r][c] > EMTHR1) EM1 = 1;
256 if(esum[ns][r][c] > EMTHR0) EM0 = 1;
257 if(esum[ns][r][c] > ELETHR2) EM3 = 1;
258 if(epdcoin[ns][r][c]==1){
259 if(esum[ns][r][c] > ELETHR2) ELE2 = 1;
260 if(esum[ns][r][c] > ELETHR1) ELE1 = 1;
261 if(esum[ns][r][c] > ELETHR0) ELE0 = 1;
265 if(h128 > esum[ns][r][c] * HAD_HERATIO_THR){
266 if(sum[ns][r][c] > HADTHR0) HAD0 = 1;
267 if(sum[ns][r][c] > HADTHR1) HAD1 = 1;
268 if(sum[ns][r][c] > HADTHR2) HAD2 = 1;
270 if(fcs_trgDebug>=2) printf(
"%5d %1d %3.2f ",esum[ns][r][c],epdcoin[ns][r][c],ratiomax[ns][r][c]);
272 if(fcs_trgDebug>=2) printf(
"\n");
275 for(
int r=0; r<15; r++){
277 for(
int c=0; c<9; c++){
278 printf(
"%5d %3.2f ",sum[ns][r][c],ratio[ns][r][c]);
285 int e_col_start[5] = { 0, 0, 0, 3, 3};
286 int e_col_stop[5] = { 3, 6, 6, 9, 9};
287 int e_row_start[5] = { 3, 0, 5, 0, 5};
288 int e_row_stop[5] = {12,10,15,10,15};
289 int h_col_start[5] = { 0, 0, 0, 2, 2};
290 int h_col_stop[5] = { 2, 3, 3, 5, 5};
291 int h_row_start[5] = { 2, 0, 3, 0, 3};
292 int h_row_stop[5] = { 7, 6, 9, 6, 9};
293 int JP2[5] = {0,0,0,0,0};
294 int JP1[5] = {0,0,0,0,0};
295 int JP0[5] = {0,0,0,0,0};
296 int JPd[5] = {0,0,0,0,0};
297 for(
int i=0; i<5; i++){
299 for(
int c=e_col_start[i]; c<=e_col_stop[i]; c++){
300 for(
int r=e_row_start[i]; r<=e_row_stop[i]; r++){
301 if((i==1 || i==2) && (c==0 || c==1) && (r>=5 && r<=10))
continue;
302 ejet += ecal[ETbTdep[r][c]].d[ETbTadr[r][c]];
305 for(
int c=h_col_start[i]; c<=h_col_stop[i]; c++){
306 for(
int r=h_row_start[i]; r<=h_row_stop[i]; r++){
307 if((i==1 || i==2) && (c==0) && (r>=3 && r<=6))
continue;
308 hjet += hcal[HTbTdep[r][c]].d[HTbTadr[r][c]];
311 jet[ns][i] = ejet + hjet;
316 if(jet[ns][i]>JPATHR2) JP2[i] = 1;
317 if(jet[ns][i]>JPATHR1) JP1[i] = 1;
318 if(jet[ns][i]>JPATHR0) JP0[i] = 1;
319 }
else if(i==1 || i==2){
320 if(jet[ns][i]>JPBCTHR2) JP2[i] = 1;
321 if(jet[ns][i]>JPBCTHR1) JP1[i] = 1;
322 if(jet[ns][i]>JPBCTHR0) JP0[i] = 1;
323 if(jet[ns][i]>JPBCTHRD) JPd[i] = 1;
325 if(jet[ns][i]>JPDETHR2) JP2[i] = 1;
326 if(jet[ns][i]>JPDETHR1) JP1[i] = 1;
327 if(jet[ns][i]>JPDETHR0) JP0[i] = 1;
328 if(jet[ns][i]>JPDETHRD) JPd[i] = 1;
331 if(fcs_trgDebug>=2) printf(
"JP5 (ns=%1d) = %3d %3d %3d %3d %3d\n",ns,jet[ns][0],jet[ns][1],jet[ns][2],jet[ns][3],jet[ns][4]);
332 if(fcs_trgDebug>=2) printf(
"JP5x(ns=%1d) = %3x %3x %3x %3x %3x\n",ns,jet[ns][0],jet[ns][1],jet[ns][2],jet[ns][3],jet[ns][4]);
336 esub[0] = esum[ns][ 0][0]+esum[ns][ 0][2]+esum[ns][ 0][4]+esum[ns][ 0][6]+esum[ns][ 0][8]
337 + esum[ns][ 2][0]+esum[ns][ 2][2]+esum[ns][ 2][4]+esum[ns][ 2][6]+esum[ns][ 2][8];
338 esub[1] = esum[ns][ 4][0]+esum[ns][ 4][2]+esum[ns][ 4][4]+esum[ns][ 4][6]+esum[ns][ 4][8]
339 + esum[ns][ 6][0]+esum[ns][ 6][2]+esum[ns][ 6][4]+esum[ns][ 6][6]+esum[ns][ 6][8];
340 esub[2] = esum[ns][ 8][0]+esum[ns][ 8][2]+esum[ns][ 8][4]+esum[ns][ 8][6]+esum[ns][ 8][8]
341 + esum[ns][10][0]+esum[ns][10][2]+esum[ns][10][4]+esum[ns][10][6]+esum[ns][10][8];
342 esub[3] = esum[ns][12][0]+esum[ns][12][2]+esum[ns][12][4]+esum[ns][12][6]+esum[ns][12][8]
343 + esum[ns][14][0]+esum[ns][14][2]+esum[ns][14][4]+esum[ns][14][6]+esum[ns][14][8];
344 for(
int i=0; i<4; i++) if(esub[i]>0xff) esub[i]=0xff;
348 hsub[0] = hsum[ns][ 1][0]+hsum[ns][ 1][2]+hsum[ns][ 1][4];
349 hsub[1] = hsum[ns][ 3][0]+hsum[ns][ 3][2]+hsum[ns][ 3][4];
350 hsub[2] = hsum[ns][ 5][0]+hsum[ns][ 5][2]+hsum[ns][ 5][4];
351 hsub[3] = hsum[ns][ 7][0]+hsum[ns][ 7][2]+hsum[ns][ 7][4];
352 for(
int i=0; i<4; i++) if(hsub[i]>0xff) hsub[i]=0xff;
355 etot[ns] = esub[0] + esub[1] + esub[2] + esub[3];
356 htot[ns] = hsub[0] + hsub[1] + hsub[2] + hsub[3];
357 if(etot[ns]>ETOTTHR) ETOT=1;
358 if(htot[ns]>HTOTTHR) HTOT=1;
359 if(fcs_trgDebug>=2) printf(
"E/H Tot = %3d %3d\n",etot[ns],htot[ns]);
362 output[0].d[0] = (EM0<<0) + (EM1<<1) + (EM2<<2) + (EM3<<3) + (ELE0<<4) + (ELE1<<5) + (ELE2<<6) + (fpre_or<<7);
363 output[1].d[0] = (HAD0<<0) + (HAD1<<1) + (HAD2<<2) + (0<<3) + (ecal_ht<<4) + (hcal_ht<<5) + (ETOT<<6) + (HTOT<<7);
364 output[0].d[1] = (JP2[0]<<0) + (JP2[1]<<1) + (JP2[2]<<2) + (JP2[3]<<3) + (JP2[4]<<4);
365 output[1].d[1] = (JP1[0]<<0) + (JP1[1]<<1) + (JP1[2]<<2) + (JP1[3]<<3) + (JP1[4]<<4);
366 output[0].d[2] = (JP0[0]<<0) + (JP0[1]<<1) + (JP0[2]<<2) + (JP0[3]<<3) + (JP0[4]<<4);
367 output[1].d[2] = (JPd[0]<<0) + (JPd[1]<<1) + (JPd[2]<<2) + (JPd[3]<<3) + (JPd[4]<<4);
379 *s2_to_dsm = (ELE0<<0) + (ELE1<<1) + (ELE2<<2) + (EM3<<3)
380 + (ecal_ht<<4) + (hcal_ht<<5) + (ETOT<<6) + (HTOT<<7);
383 printf(
"FCS STG2 NS=%1d output = %02x %02x %02x %02x %02x %02x TCU=%04x\n",
385 output[0].d[0],output[1].d[0],output[0].d[1],output[1].d[1],
386 output[0].d[2],output[1].d[2],*s2_to_dsm);
387 printf(
"emuout = %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x TCU=%04x\n",
388 output[1].d[0],output[0].d[0],
389 output[1].d[1],output[0].d[1],
390 output[1].d[2],output[0].d[2],
391 output[1].d[3],output[0].d[3],
392 output[1].d[4],output[0].d[4],
393 output[1].d[5],output[0].d[5],
394 output[1].d[6],output[0].d[6],
395 output[1].d[7],output[0].d[7],