StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
stage_2_202201.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 // Processing on the North or South DEP/IO flavoured board.
9 // Inputs are up to 32 links but I already organized them according to strawman.
10 // output is 1 link over the external OUT connector.
11 // Right now we assume we have 20 inputs from ECAL, 6 from HCAL and 4 from PRE.
12 // We also assume there is no need to know if this is North or South as
13 // the processing is exactly the same. Right??
14 
15 namespace{
16 
17  //version2 with top2 & bottom2 rows in trigger, missing far side column
18  static const int EtoHmap[15][9][2] = {
19  { { 0, 0},{ 0, 1},{ 0, 1},{ 0, 2},{ 0, 2},{ 0, 3},{ 0, 4},{ 0, 4},{ 0, 4}},
20  { { 0, 0},{ 0, 1},{ 0, 1},{ 0, 2},{ 0, 2},{ 0, 3},{ 0, 4},{ 0, 4},{ 0, 4}},
21  { { 1, 0},{ 1, 1},{ 1, 1},{ 1, 2},{ 1, 2},{ 1, 3},{ 1, 4},{ 1, 4},{ 1, 4}},
22  { { 2, 0},{ 2, 1},{ 2, 1},{ 2, 2},{ 2, 2},{ 2, 3},{ 2, 4},{ 2, 4},{ 2, 4}},
23  { { 2, 0},{ 2, 1},{ 2, 1},{ 2, 2},{ 2, 2},{ 2, 3},{ 2, 4},{ 2, 4},{ 2, 4}},
24  { { 3, 0},{ 3, 1},{ 3, 1},{ 3, 2},{ 3, 2},{ 3, 3},{ 3, 4},{ 3, 4},{ 3, 4}},
25  { { 3, 0},{ 3, 1},{ 3, 1},{ 3, 2},{ 3, 2},{ 3, 3},{ 3, 4},{ 3, 4},{ 3, 4}},
26  { { 4, 0},{ 4, 1},{ 4, 1},{ 4, 2},{ 4, 2},{ 4, 3},{ 4, 4},{ 4, 4},{ 4, 4}},
27  { { 5, 0},{ 5, 1},{ 5, 1},{ 5, 2},{ 5, 2},{ 5, 3},{ 5, 4},{ 5, 4},{ 5, 4}},
28  { { 5, 0},{ 5, 1},{ 5, 1},{ 5, 2},{ 5, 2},{ 5, 3},{ 5, 4},{ 5, 4},{ 5, 4}},
29  { { 6, 0},{ 6, 1},{ 6, 1},{ 6, 2},{ 6, 2},{ 6, 3},{ 6, 4},{ 6, 4},{ 6, 4}},
30  { { 6, 0},{ 6, 1},{ 6, 1},{ 6, 2},{ 6, 2},{ 6, 3},{ 6, 4},{ 6, 4},{ 6, 4}},
31  { { 7, 0},{ 7, 1},{ 7, 1},{ 7, 2},{ 7, 2},{ 7, 3},{ 7, 4},{ 7, 4},{ 7, 4}},
32  { { 8, 0},{ 8, 1},{ 8, 1},{ 8, 2},{ 8, 2},{ 8, 3},{ 8, 4},{ 8, 4},{ 8, 4}},
33  { { 8, 0},{ 8, 1},{ 8, 1},{ 8, 2},{ 8, 2},{ 8, 3},{ 8, 4},{ 8, 4},{ 8, 4}}
34  } ;
35 }
36 
37 void fcs_trg_base::stage_2_202201(link_t ecal[], link_t hcal[], link_t pres[], geom_t geo, link_t output[])
38 {
39  int ns=geo.ns;
40  if(fcs_trgDebug>=2) printf("Stage2v1 ns=%d\n",ns);
41  static int first=0;
42  // creating 2x2 row/column address map when called first time
43  static u_int ETbTdep[16][10]; //DEP#
44  static u_int ETbTadr[16][10]; //Input Link data address
45  static u_int HTbTdep[10][6]; //DEP#
46  static u_int HTbTadr[10][6]; //Input Link data address
47  if(first==0){
48  first=1;
49  //making map of 2x2 Ecal Sums of [4][4]
50  for(int r=0; r<16; r++){
51  printf("Ecal r=%2d : ",r);
52  for(int c=0; c<10; c++){
53  ETbTdep[r][c]= c/2 + (r/4)*5;
54  ETbTadr[r][c]= c%2 + (r%4)*2;
55  printf("%2d-%1d ",ETbTdep[r][c],ETbTadr[r][c]);
56  }
57  printf("\n");
58  }
59  //making map of 2x2 Hcal sums of [10][6]
60  for(int r=0; r<10; r++){
61  printf("HCal r=%2d : ",r);
62  for(int c=0; c<6; c++){
63  if (r==0){
64  HTbTdep[r][c]= 6;
65  HTbTadr[r][c]= c;
66  }else if(r==9){
67  HTbTdep[r][c]= 7;
68  HTbTadr[r][c]= c;
69  }else{
70  HTbTdep[r][c]= c/2 + ((r-1)/4)*3;
71  HTbTadr[r][c]= c%2 + ((r-1)%4)*2;
72  }
73  printf("%2d-%1d ",HTbTdep[r][c],HTbTadr[r][c]);
74  }
75  printf("\n");
76  }
77  }
78 
79  //compute overlapping Hcal 4x4 sum of [9][5]
80  //u_int hsum[9][5];
81  for(int r=0; r<9; r++){
82  if(fcs_trgDebug>=2) printf("H4x4 ");
83  for(int c=0; c<5; c++){
84  hsum[ns][r][c]
85  = hcal[HTbTdep[r ][c ]].d[HTbTadr[r ][c ]]
86  + hcal[HTbTdep[r ][c+1]].d[HTbTadr[r ][c+1]]
87  + hcal[HTbTdep[r+1][c ]].d[HTbTadr[r+1][c ]]
88  + hcal[HTbTdep[r+1][c+1]].d[HTbTadr[r+1][c+1]];
89  //if(hsum[r][c] > 0xff) hsum[r][c]=0xff; //Tonko says no point to saturate at 8bit here
90  if(fcs_trgDebug>=2) printf("%5d ",hsum[ns][r][c]);
91  }
92  if(fcs_trgDebug>=2) printf("\n");
93  }
94 
95  //PRES for QA
96  if(fcs_trgDebug>0){
97  for(int dep=0; dep<6; dep++) {
98  for(int j=0; j<4; j++) {
99  for(int k=0; k<8; k++){
100  phit[ns][dep][j*8+k] = (pres[dep].d[j] >> k) & 0x1;
101  }
102  }
103  }
104  }
105  if(fcs_trgDebug>=2){
106  for(int dep=0; dep<6; dep++) {
107  printf("PRES NS%1d DEP%1d : ",ns,dep);
108  for(int j=0; j<4; j++) {
109  for(int k=0; k<8; k++){
110  phit[ns][dep][j*8+k] = (pres[dep].d[j] >> k) & 0x1;
111  printf("%1d", (pres[dep].d[j]>>k)&0x1);
112  }
113  printf(" ");
114  }
115  printf("\n");
116  }
117  }
118 
119  //compute overlapping Ecal 4x4 sums of [15][9]
120  //take ratio with the closest hcal 4x4
121  //u_int esum[15][9];
122  //u_int sum[15][9];
123  //float ratio[15][9];
124  u_int EM1 =0, EM2 =0, EM3=0;
125  u_int GAM1=0, GAM2=0, GAM3=0;
126  u_int ELE1=0, ELE2=0, ELE3=0;
127  u_int HAD1=0, HAD2=0, HAD3=0;
128  u_int ETOT=0, HTOT=0;
129  for(int r=0; r<15; r++){
130  if(fcs_trgDebug>=2) printf("E4x4 ");
131  for(int c=0; c<9; c++){
132  esum[ns][r][c]
133  = ecal[ETbTdep[r ][c ]].d[ETbTadr[r ][c ]]
134  + ecal[ETbTdep[r ][c+1]].d[ETbTadr[r ][c+1]]
135  + ecal[ETbTdep[r+1][c ]].d[ETbTadr[r+1][c ]]
136  + ecal[ETbTdep[r+1][c+1]].d[ETbTadr[r+1][c+1]];
137  //if(esum[r][c] > 0xff) esum[r][c]=0xff; //Tonko says no point to saturate at 8bit here
138 
139  // locate the closest hcal
140  u_int h=hsum[ns][EtoHmap[r][c][0]][EtoHmap[r][c][1]];
141 
142  // E+H sum
143  sum[ns][r][c] = esum[ns][r][c] + h;
144 
145  //in VHDL we will do esum>hsum*threshold. Ratio is for human only
146  if(sum[ns][r][c]==0) {
147  ratio[ns][r][c]=0.0;
148  }else{
149  ratio[ns][r][c] = float(esum[ns][r][c]) / float(sum[ns][r][c]);
150  }
151 
152  //check EPD hits using the mask
153  epdcoin[ns][r][c]=0;
154  for(int dep=0; dep<6; dep++){
155  int mask;
156  if(fcs_readPresMaskFromText==0){
157  mask = fcs_ecal_epd_mask[r][c][dep]; //from include file
158  }else{
159  mask = PRES_MASK[r][c][dep]; //from static which was from text file
160  }
161  for(int j=0; j<4; j++) {
162  for(int k=0; k<8; k++){
163  if( (mask >> (j*8 + k)) & 0x1) { //if this is 0, don't even put the logic in VHDL
164  epdcoin[ns][r][c] |= (pres[dep].d[j] >> k) & 0x1;
165  }
166  }
167  }
168  }
169 
170  // integer multiplication as in VHDL!
171  // ratio thresholds are in fixed point integer where 1.0==128
172  u_int h128 = h*128 ;
173  if(h128 < esum[ns][r][c] * EM_HERATIO_THR){
174  if(sum[ns][r][c] > EMTHR1){
175  EM1 = 1;
176  if(epdcoin[ns][r][c]==0) {GAM1 = 1;}
177  else {ELE1 = 1;}
178  }
179  if(sum[ns][r][c] > EMTHR2){
180  EM2 = 1;
181  if(epdcoin[ns][r][c]==0) {GAM2 = 1;}
182  else {ELE2 = 1;}
183  }
184  if(sum[ns][r][c] > EMTHR3){
185  EM3 = 1;
186  if(epdcoin[ns][r][c]==0) {GAM3 = 1;}
187  else {ELE3 = 1;}
188  }
189  }
190  if(h128 > esum[ns][r][c] * HAD_HERATIO_THR){
191  if(sum[ns][r][c] > HADTHR1) HAD1 = 1;
192  if(sum[ns][r][c] > HADTHR2) HAD2 = 1;
193  if(sum[ns][r][c] > HADTHR3) HAD3 = 1;
194  }
195  if(fcs_trgDebug>=2) printf("%5d %1d %3.2f ",esum[ns][r][c],epdcoin[ns][r][c],ratio[ns][r][c]);
196  }
197  if(fcs_trgDebug>=2) printf("\n");
198  }
199 
200  //Ecal sub-crate sum
201  u_int esub[4];
202  esub[0] = esum[ns][ 0][0]+esum[ns][ 0][2]+esum[ns][ 0][4]+esum[ns][ 0][6]+esum[ns][ 0][8]
203  + esum[ns][ 2][0]+esum[ns][ 2][2]+esum[ns][ 2][4]+esum[ns][ 2][6]+esum[ns][ 2][8];
204  esub[1] = esum[ns][ 4][0]+esum[ns][ 4][2]+esum[ns][ 4][4]+esum[ns][ 4][6]+esum[ns][ 4][8]
205  + esum[ns][ 6][0]+esum[ns][ 6][2]+esum[ns][ 6][4]+esum[ns][ 6][6]+esum[ns][ 6][8];
206  esub[2] = esum[ns][ 8][0]+esum[ns][ 8][2]+esum[ns][ 8][4]+esum[ns][ 8][6]+esum[ns][ 8][8]
207  + esum[ns][10][0]+esum[ns][10][2]+esum[ns][10][4]+esum[ns][10][6]+esum[ns][10][8];
208  esub[3] = esum[ns][12][0]+esum[ns][12][2]+esum[ns][12][4]+esum[ns][12][6]+esum[ns][12][8]
209  + esum[ns][14][0]+esum[ns][14][2]+esum[ns][14][4]+esum[ns][14][6]+esum[ns][14][8];
210  for(int i=0; i<4; i++) if(esub[i]>0xff) esub[i]=0xff;
211 
212  //Hcal sub-crate sum
213  u_int hsub[4];
214  hsub[0] = hsum[ns][ 1][0]+hsum[ns][ 1][2]+hsum[ns][ 1][4];
215  hsub[1] = hsum[ns][ 3][0]+hsum[ns][ 3][2]+hsum[ns][ 3][4];
216  hsub[2] = hsum[ns][ 5][0]+hsum[ns][ 5][2]+hsum[ns][ 5][4];
217  hsub[3] = hsum[ns][ 7][0]+hsum[ns][ 7][2]+hsum[ns][ 7][4];
218  for(int i=0; i<4; i++) if(hsub[i]>0xff) hsub[i]=0xff;
219 
220  //Jet sum
221  //u_int jet[3];
222  u_int JP1=0,JP2=0;
223  jet[ns][0] = esub[0] + esub[1] + hsub[0] + hsub[1];
224  jet[ns][1] = esub[1] + esub[2] + hsub[1] + hsub[2];
225  jet[ns][2] = esub[2] + esub[3] + hsub[2] + hsub[3];
226  for(int i=0; i<3; i++){
227  if(jet[ns][i]>0xff) jet[ns][i]=0xff;
228  if(jet[ns][i]>JETTHR1) JP1 = 1;
229  if(jet[ns][i]>JETTHR2) JP2 = 1;
230  }
231  if(fcs_trgDebug>=2) printf("Jet = %3d %3d %3d\n",jet[ns][0],jet[ns][1],jet[ns][2]);
232 
233  //total ET
234  etot[ns] = esub[0] + esub[1] + esub[2] + esub[3];
235  htot[ns] = hsub[0] + hsub[1] + hsub[2] + hsub[3];
236  if(etot[ns]>ETOTTHR) ETOT=1;
237  if(htot[ns]>HTOTTHR) HTOT=1;
238  if(fcs_trgDebug>=2) printf("E/H Tot = %3d %3d\n",etot[ns],htot[ns]);
239 
240  //sending output bits
241  output[0].d[0] = EM1 + (EM2 <<1) + (EM3 <<2) + 0x80; // Tonko: added last bit
242  output[0].d[1] = ELE1 + (ELE2<<1) + (ELE3<<2);
243  output[0].d[2] = GAM1 + (GAM2<<1) + (GAM3<<2);
244  output[0].d[3] = HAD1 + (HAD2<<1) + (HAD3<<2);
245  output[0].d[4] = JP1 + (JP2 <<1);
246  output[0].d[5] = ETOT + (HTOT<<1);
247  output[0].d[6] = pres[0].d[1] ; // Tonko: added
248  output[0].d[7] = pres[0].d[7] ; // Tonko: added
249 
250  if(fcs_trgDebug>=1){
251  printf("FCS STG2 NS=%1d output = %02x %02x %02x %02x %02x %02x %02x %02x\n",
252  ns,
253  output[0].d[0],output[0].d[1],output[0].d[2],output[0].d[3],
254  output[0].d[4],output[0].d[5],output[0].d[6],output[0].d[7]);
255  }
256 
257  return ;
258 }
259