StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
stage_2_JP6_202204.cxx
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <string.h>
4 
5 #include "fcs_trg_base.h"
6 #include "fcs_ecal_epd_mask.h"
7 
8 // Modifed from stage_2_202201.cxx -- Ting
9 
10 namespace {
11  //version2 with top2 & bottom2 rows in trigger, missing far side column
12  static const int EtoHmap[15][9][2] = {
13  { { 0, 0},{ 0, 1},{ 0, 1},{ 0, 2},{ 0, 2},{ 0, 3},{ 0, 4},{ 0, 4},{ 0, 4}},
14  { { 0, 0},{ 0, 1},{ 0, 1},{ 0, 2},{ 0, 2},{ 0, 3},{ 0, 4},{ 0, 4},{ 0, 4}},
15  { { 1, 0},{ 1, 1},{ 1, 1},{ 1, 2},{ 1, 2},{ 1, 3},{ 1, 4},{ 1, 4},{ 1, 4}},
16  { { 2, 0},{ 2, 1},{ 2, 1},{ 2, 2},{ 2, 2},{ 2, 3},{ 2, 4},{ 2, 4},{ 2, 4}},
17  { { 2, 0},{ 2, 1},{ 2, 1},{ 2, 2},{ 2, 2},{ 2, 3},{ 2, 4},{ 2, 4},{ 2, 4}},
18  { { 3, 0},{ 3, 1},{ 3, 1},{ 3, 2},{ 3, 2},{ 3, 3},{ 3, 4},{ 3, 4},{ 3, 4}},
19  { { 3, 0},{ 3, 1},{ 3, 1},{ 3, 2},{ 3, 2},{ 3, 3},{ 3, 4},{ 3, 4},{ 3, 4}},
20  { { 4, 0},{ 4, 1},{ 4, 1},{ 4, 2},{ 4, 2},{ 4, 3},{ 4, 4},{ 4, 4},{ 4, 4}},
21  { { 5, 0},{ 5, 1},{ 5, 1},{ 5, 2},{ 5, 2},{ 5, 3},{ 5, 4},{ 5, 4},{ 5, 4}},
22  { { 5, 0},{ 5, 1},{ 5, 1},{ 5, 2},{ 5, 2},{ 5, 3},{ 5, 4},{ 5, 4},{ 5, 4}},
23  { { 6, 0},{ 6, 1},{ 6, 1},{ 6, 2},{ 6, 2},{ 6, 3},{ 6, 4},{ 6, 4},{ 6, 4}},
24  { { 6, 0},{ 6, 1},{ 6, 1},{ 6, 2},{ 6, 2},{ 6, 3},{ 6, 4},{ 6, 4},{ 6, 4}},
25  { { 7, 0},{ 7, 1},{ 7, 1},{ 7, 2},{ 7, 2},{ 7, 3},{ 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}}
28  } ;
29 
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}}
46  };
47 }
48 
49 void fcs_trg_base::stage_2_JP6_202204(link_t ecal[], link_t hcal[], link_t pres[], geom_t geo, link_t output[])
50 {
51  int ns=geo.ns;
52  if(fcs_trgDebug>=2) printf("Stage2v1 ns=%d\n",ns);
53  static int first=0;
54  // creating 2x2 row/column address map when called first time
55  static u_int ETbTdep[16][10]; //DEP#
56  static u_int ETbTadr[16][10]; //Input Link data address
57  static u_int HTbTdep[10][6]; //DEP#
58  static u_int HTbTadr[10][6]; //Input Link data address
59  if(first==0){
60  first=1;
61  //making map of 2x2 Ecal Sums of [4][4]
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]);
68  }
69  printf("\n");
70  }
71  //making map of 2x2 Hcal sums of [10][6]
72  for(int r=0; r<10; r++){
73  printf("HCal r=%2d : ",r);
74  for(int c=0; c<6; c++){
75  if (r==0){
76  HTbTdep[r][c]= 6;
77  HTbTadr[r][c]= c;
78  }else if(r==9){
79  HTbTdep[r][c]= 7;
80  HTbTadr[r][c]= c;
81  }else{
82  HTbTdep[r][c]= c/2 + ((r-1)/4)*3;
83  HTbTadr[r][c]= c%2 + ((r-1)%4)*2;
84  }
85  printf("%2d-%1d ",HTbTdep[r][c],HTbTadr[r][c]);
86  }
87  printf("\n");
88  }
89  }
90 
91  // Ecal 2x2 "HT" trigger
92  int ecal_ht = 0 ;
93  for(int i=0;i<DEP_ECAL_TRG_COU;i++) {// 0..19
94  for(int j=0;j<8;j++) {
95  if(s2_ch_mask[geo.ns] & (1ll<<i)) {
96  ecal[i].d[j] = 0 ;
97  continue ;
98  }
99  if(ecal[i].d[j] > EHTTHR) ecal_ht |= 1 ;
100  }
101  }
102 
103  // Hcal 2x2 "HT" trigger
104  int hcal_ht = 0 ;
105  for(int i=0;i<DEP_HCAL_TRG_COU;i++) {// 20..27
106  for(int j=0;j<8;j++) {
107  if(s2_ch_mask[geo.ns] & (1ll<<(20+i))) {
108  hcal[i].d[j] = 0 ;
109  continue ;
110  }
111  if(hcal[i].d[j] > HHTTHR) hcal_ht |= 1 ;
112  }
113  }
114 
115  // Pres OR trigger
116  int fpre_or = 0 ;
117  for(int i=0;i<DEP_PRE_TRG_COU;i++) {// 28..32
118  for(int j=0;j<8;j++) {
119  if(s2_ch_mask[geo.ns] & (1ll<<(28+i))) {
120  pres[i].d[j] = 0 ;
121  continue ;
122  }
123  if(pres[i].d[j]) fpre_or |= 1 ;
124  }
125  }
126 
127  //mapped Ecal 2x2
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]];
131  }
132  }
133  //mapped Hcal 2x2
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]];
137  }
138  }
139 
140  //compute overlapping Hcal 4x4 sum of [9][5]
141  //u_int hsum[9][5];
142  for(int r=0; r<9; r++){
143  if(fcs_trgDebug>=2) printf("H4x4 ");
144  for(int c=0; c<5; c++){
145  hsum[ns][r][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]];
150  //if(hsum[r][c] > 0xff) hsum[r][c]=0xff; //Tonko says no point to saturate at 8bit here
151  if(fcs_trgDebug>=2) printf("%5d ",hsum[ns][r][c]);
152  }
153  if(fcs_trgDebug>=2) printf("\n");
154  }
155 
156  //PRES for QA
157  if(fcs_trgDebug>0){
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;
162  }
163  }
164  }
165  }
166  if(fcs_trgDebug>=2){
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);
173  }
174  printf(" ");
175  }
176  printf("\n");
177  }
178  }
179 
180  //compute overlapping Ecal 4x4 sums of [15][9]
181  //take ratio with the closest hcal 4x4
182  //u_int esum[15][9];
183  //u_int sum[15][9];
184  //float ratio[15][9];
185  u_int EM1 =0, EM2 =0, EM3=0;
186  u_int GAM1=0, GAM2=0, GAM3=0;
187  u_int ELE1=0, ELE2=0, ELE3=0;
188  u_int HAD1=0, HAD2=0, HAD3=0;
189  u_int ETOT=0, HTOT=0;
190  for(int r=0; r<15; r++){
191  if(fcs_trgDebug>=2) printf("E4x4 ");
192  for(int c=0; c<9; c++){
193  esum[ns][r][c]
194  = ecal[ETbTdep[r ][c ]].d[ETbTadr[r ][c ]]
195  + ecal[ETbTdep[r ][c+1]].d[ETbTadr[r ][c+1]]
196  + ecal[ETbTdep[r+1][c ]].d[ETbTadr[r+1][c ]]
197  + ecal[ETbTdep[r+1][c+1]].d[ETbTadr[r+1][c+1]];
198  //if(esum[r][c] > 0xff) esum[r][c]=0xff; //Tonko says no point to saturate at 8bit here
199 
200  // locate the closest hcal
201  u_int h=hsum[ns][EtoHmap[r][c][0]][EtoHmap[r][c][1]];
202 
203  // locate the max 2x2 hcal
204  u_int hmax=0;
205  for(int iz=0; iz<4; iz++){
206  int iHCalID = EtoH3map[r][c][iz];
207  if(iHCalID < 0) continue;
208  int irow = iHCalID/5;
209  int icol = iHCalID%5;
210  if(hmax < hsum[ns][irow][icol]) hmax = hsum[ns][irow][icol];
211  }
212 
213  //if(h > 0 || hmax > 0) printf("Checking: %d, %d\n", h, hmax);
214 
215  // E+H sum
216  sum[ns][r][c] = esum[ns][r][c] + h;
217 
218  //in VHDL we will do esum>hsum*threshold. Ratio is for human only
219  if(sum[ns][r][c]==0) {
220  ratio[ns][r][c]=0.0;
221  }else{
222  ratio[ns][r][c] = float(esum[ns][r][c]) / float(sum[ns][r][c]);
223  }
224 
225  //check EPD hits using the mask
226  epdcoin[ns][r][c]=0;
227  for(int dep=0; dep<6; dep++){
228  int mask;
229  if(fcs_readPresMaskFromText==0){
230  mask = fcs_ecal_epd_mask[r][c][dep]; //from include file
231  }else{
232  mask = PRES_MASK[r][c][dep]; //from static which was from text file
233  }
234  for(int j=0; j<4; j++) {
235  for(int k=0; k<8; k++){
236  if( (mask >> (j*8 + k)) & 0x1) { //if this is 0, don't even put the logic in VHDL
237  epdcoin[ns][r][c] |= (pres[dep].d[j] >> k) & 0x1;
238  }
239  }
240  }
241  }
242 
243  // integer multiplication as in VHDL!
244  // ratio thresholds are in fixed point integer where 1.0==128
245  u_int h128 = h*128 ;
246  u_int hmax128 = hmax*128 ;
247 
248  if(hmax128 < esum[ns][r][c] * EM_HERATIO_THR){
249  if(esum[ns][r][c] > EMTHR1){
250  EM1 = 1;
251  if(epdcoin[ns][r][c]==0) {GAM1 = 1;}
252  else {ELE1 = 1;}
253  }
254  if(esum[ns][r][c] > EMTHR2){
255  EM2 = 1;
256  if(epdcoin[ns][r][c]==0) {GAM2 = 1;}
257  else {ELE2 = 1;}
258  }
259  if(esum[ns][r][c] > EMTHR3){
260  EM3 = 1;
261  if(epdcoin[ns][r][c]==0) {GAM3 = 1;}
262  else {ELE3 = 1;}
263  }
264  }
265 
266  if(h128 > esum[ns][r][c] * HAD_HERATIO_THR){
267  if(sum[ns][r][c] > HADTHR1) HAD1 = 1;
268  if(sum[ns][r][c] > HADTHR2) HAD2 = 1;
269  if(sum[ns][r][c] > HADTHR3) HAD3 = 1;
270  }
271  if(fcs_trgDebug>=2) printf("%5d %1d %3.2f ",esum[ns][r][c],epdcoin[ns][r][c],ratio[ns][r][c]);
272  }
273  if(fcs_trgDebug>=2) printf("\n");
274  }
275 
276  //Akio's 6 square JP
277  int e_col_start[2] = { 0, 4}; //these are 2x2 row/col
278  int e_col_stop[2] = { 5, 9};
279  int e_row_start[3] = { 0, 4, 8};
280  int e_row_stop[3] = { 7,11,15};
281  int h_col_start[2] = { 0, 2};
282  int h_col_stop[2] = { 3, 5};
283  int h_row_start[3] = { 0, 2, 5};
284  int h_row_stop[3] = { 4, 7, 9};
285  int JP1[6] = {0,0,0,0,0,0};
286  int JP2[6] = {0,0,0,0,0,0};
287  int j=0;
288  for(int jc=0; jc<2; jc++){
289  for(int jr=0; jr<3; jr++){
290  jet[ns][j]=0;
291  for(int c=e_col_start[jc]; c<=e_col_stop[jc]; c++){
292  for(int r=e_row_start[jr]; r<=e_row_stop[jr]; r++){
293  jet[ns][j] += ecal[ETbTdep[r][c]].d[ETbTadr[r][c]];
294  }
295  }
296  for(int c=h_col_start[jc]; c<=h_col_stop[jc]; c++){
297  for(int r=h_row_start[jr]; r<=h_row_stop[jr]; r++){
298  jet[ns][j] += hcal[HTbTdep[r][c]].d[HTbTadr[r][c]];
299  }
300  }
301  if(jet[ns][j]>0xff) jet[ns][j]=0xff;
302  if(jet[ns][j]>JETTHR1) JP1[j] = 1;
303  if(jet[ns][j]>JETTHR2) JP2[j] = 1;
304  j++;
305  }
306  }
307  if(fcs_trgDebug>=2) printf("JP6 = %3d %3d %3d %3d %3d %3d\n",jet[ns][0],jet[ns][1],jet[ns][2],jet[ns][3],jet[ns][4],jet[ns][5]);
308 
309  //Ecal sub-crate sum
310  u_int esub[4];
311  esub[0] = esum[ns][ 0][0]+esum[ns][ 0][2]+esum[ns][ 0][4]+esum[ns][ 0][6]+esum[ns][ 0][8]
312  + esum[ns][ 2][0]+esum[ns][ 2][2]+esum[ns][ 2][4]+esum[ns][ 2][6]+esum[ns][ 2][8];
313  esub[1] = esum[ns][ 4][0]+esum[ns][ 4][2]+esum[ns][ 4][4]+esum[ns][ 4][6]+esum[ns][ 4][8]
314  + esum[ns][ 6][0]+esum[ns][ 6][2]+esum[ns][ 6][4]+esum[ns][ 6][6]+esum[ns][ 6][8];
315  esub[2] = esum[ns][ 8][0]+esum[ns][ 8][2]+esum[ns][ 8][4]+esum[ns][ 8][6]+esum[ns][ 8][8]
316  + esum[ns][10][0]+esum[ns][10][2]+esum[ns][10][4]+esum[ns][10][6]+esum[ns][10][8];
317  esub[3] = esum[ns][12][0]+esum[ns][12][2]+esum[ns][12][4]+esum[ns][12][6]+esum[ns][12][8]
318  + esum[ns][14][0]+esum[ns][14][2]+esum[ns][14][4]+esum[ns][14][6]+esum[ns][14][8];
319  for(int i=0; i<4; i++) if(esub[i]>0xff) esub[i]=0xff;
320 
321  //Hcal sub-crate sum
322  u_int hsub[4];
323  hsub[0] = hsum[ns][ 1][0]+hsum[ns][ 1][2]+hsum[ns][ 1][4];
324  hsub[1] = hsum[ns][ 3][0]+hsum[ns][ 3][2]+hsum[ns][ 3][4];
325  hsub[2] = hsum[ns][ 5][0]+hsum[ns][ 5][2]+hsum[ns][ 5][4];
326  hsub[3] = hsum[ns][ 7][0]+hsum[ns][ 7][2]+hsum[ns][ 7][4];
327  for(int i=0; i<4; i++) if(hsub[i]>0xff) hsub[i]=0xff;
328 
329  //total ET
330  etot[ns] = esub[0] + esub[1] + esub[2] + esub[3];
331  htot[ns] = hsub[0] + hsub[1] + hsub[2] + hsub[3];
332  if(etot[ns]>ETOTTHR) ETOT=1;
333  if(htot[ns]>HTOTTHR) HTOT=1;
334  if(fcs_trgDebug>=2) printf("E/H Tot = %3d %3d\n",etot[ns],htot[ns]);
335 
336  //sending output bits
337  output[0].d[0] = EM1 + (EM2 <<1) + (EM3 <<2);
338  output[0].d[1] = ELE1 + (ELE2<<1) + (ELE3<<2);
339  output[0].d[2] = GAM1 + (GAM2<<1) + (GAM3<<2);
340  output[0].d[3] = HAD1 + (HAD2<<1) + (HAD3<<2);
341  output[0].d[4] = (JP1[0] << 0) + (JP1[1]<<1) + (JP1[2]<<2) + (JP1[3]<<3) + (JP1[4]<<4) + (JP1[5]<<5);
342  output[0].d[5] = ETOT + (HTOT<<1);
343  output[0].d[6] = (fpre_or<<2) | (hcal_ht<<1) | (ecal_ht); //HT
344  output[0].d[7] = 0xCD ;
345 
346  // upper 8 bits
347  output[1].d[0] = 0 ;
348  output[1].d[1] = 0 ;
349  output[1].d[2] = 0 ;
350  output[1].d[3] = 0 ;
351  output[0].d[4] = (JP2[0] << 0) + (JP2[1]<<1) + (JP2[2]<<2) + (JP2[3]<<3) + (JP2[4]<<4) + (JP2[5]<<5);
352  output[1].d[5] = 0 ;
353  output[1].d[6] = 0 ;
354  output[1].d[7] = 0xAB ;
355 
356 
357  if(fcs_trgDebug>=1){
358  printf("FCS STG2 NS=%1d output = %02x %02x %02x %02x %02x %02x %02x %02x\n",
359  ns,
360  output[0].d[0],output[0].d[1],output[0].d[2],output[0].d[3],
361  output[0].d[4],output[0].d[5],output[0].d[6],output[0].d[7]);
362  }
363 
364  return ;
365 }