]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALTriggerTRU.cxx
Changes for #93916 EMCAL commit attached patch and port to the release
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTriggerTRU.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17
18
19
20
21 Author: R. GUERNANE LPSC Grenoble CNRS/IN2P3
22 */
23
24
25 #include "AliEMCALTriggerTRU.h"
26 #include "AliEMCALTriggerPatch.h"
27 #include "AliEMCALTriggerTRUDCSConfig.h"
28 #include "AliLog.h"
29
30 #include <TClonesArray.h>
31 #include <TSystem.h>
32 #include <Riostream.h>
33
34 namespace
35 {
36         const Int_t kTimeBins       = 16; // number of sampling bins of the FastOR signal
37         const Int_t kTimeWindowSize =  4; // 
38 }
39
40 ClassImp(AliEMCALTriggerTRU)
41
42 //________________
43 AliEMCALTriggerTRU::AliEMCALTriggerTRU() : AliEMCALTriggerBoard(),
44 fDCSConfig(0x0),
45 fL0Time(0)
46 {
47         // Ctor
48         
49         for (Int_t i=0;i<96;i++) for (Int_t j=0;j<256;j++) fADC[i][j] = 0;
50 }
51
52 //________________
53 AliEMCALTriggerTRU::AliEMCALTriggerTRU(AliEMCALTriggerTRUDCSConfig* dcsConf, const TVector2& rSize, Int_t mapType) : 
54 AliEMCALTriggerBoard(rSize),
55 fDCSConfig(dcsConf),
56 fL0Time(0)
57 {
58         // Ctor
59         
60         for (Int_t i=0;i<96;i++) for (Int_t j=0;j<256;j++) fADC[i][j] = 0;
61         
62         TVector2 size;
63         
64         if (dcsConf->GetL0SEL() & 0x0001) // 4-by-4
65         {
66                 size.Set( 1. , 1. );
67                 SetSubRegionSize( size );
68                 
69                 size.Set( 2. , 2. );
70                 SetPatchSize( size );
71         }       
72         else                              // 2-by-2
73         {
74                 size.Set( 1. , 1. );
75                 SetSubRegionSize( size );
76                 
77                 size.Set( 1. , 1. );
78                 SetPatchSize( size );   
79         }       
80         
81         for (Int_t ietam=0;ietam<24;ietam++)
82         {
83                 for (Int_t iphim=0;iphim<4;iphim++)
84                 {
85                         // idx: 0..95 since iphim: 0..11 ietam: 0..23
86                         Int_t idx = ( !mapType ) ? ( 3 - iphim ) + ietam * 4 : iphim + (23 - ietam) * 4;        
87         
88                         // Build a matrix used to get TRU digit id (ADC channel) from (eta,phi)|SM
89                         fMap[ietam][iphim] = idx; // [0..11][0..3] namely [eta][phi] in SM
90                 }
91         }
92 }
93
94 //________________
95 AliEMCALTriggerTRU::~AliEMCALTriggerTRU()
96 {
97         // Dtor
98 }
99
100 //________________
101 void AliEMCALTriggerTRU::ShowFastOR(Int_t iTimeWindow, Int_t iChannel)
102 {
103         // Dump
104         
105         Int_t iChanF, iChanL;
106         
107         if (iChannel != -1) iChanF = iChanL = iChannel;
108         else
109         {
110                 iChanF =  0;
111                 iChanL = 95;
112         }
113         
114         for (Int_t i=iChanF;i<iChanL+1;i++)
115         {
116                 printf("\tChannel: %2d - ",i);
117                 for (Int_t j=0;j<60;j++) 
118                 {
119                         if (j == iTimeWindow)
120                                 printf(" | %4d",fADC[i][j]);
121                         else if (j == iTimeWindow+kTimeWindowSize-1)
122                                 printf(" %4d |",fADC[i][j]);
123                         else
124                                 printf(" %4d",fADC[i][j]);
125                 }
126                 
127                 printf("\n");
128         }
129 }
130
131 //________________
132 Int_t AliEMCALTriggerTRU::L0()
133 {
134         // L0 algo depending on TRU fw version
135         
136         const Int_t xsize    = Int_t(fRegionSize->X());
137         const Int_t ysize    = Int_t(fRegionSize->Y());
138
139         Int_t asum = 0;
140         for (Int_t j = 0; j < xsize; j++) {             
141                 for (Int_t k = 0; k < ysize; k++) {
142                         for (Int_t l = 0; l < kTimeBins; l++) {
143                                 asum += fADC[fMap[j][k]][l];
144                         }
145                 }
146         }       
147         
148         // TRU has no signal, return!
149         if (!asum) {
150                 AliDebug(999,"=== TRU has no signal ===");
151                 return 0;
152         }
153         
154         AliDebug(999,Form("=== TRU PF: %x",fDCSConfig->GetSELPF()));
155         
156         UInt_t ma = fDCSConfig->GetSELPF() & 0xffff;
157         
158 //      int nb = 0;
159 //      for (int i = 0; i < 7; i++) {
160 //              UInt_t bit = ma & (1 << i);
161 //              if (bit) nb++;
162 //      }
163         
164         int nb = ma & 0x7f;
165         
166         ma = (ma >> 8) & 0x7f;
167         
168         AliDebug(999,Form("=== TRU fw version %x ===",fDCSConfig->GetFw()));
169         
170         if (fDCSConfig->GetFw() < 0x4d) {
171                 return L0v0(nb, ma);
172         } else {
173                 return L0v1(nb, ma);
174         }
175 }
176
177 //________________
178 Int_t AliEMCALTriggerTRU::L0v0(int mask, int pattern)
179 {
180         // L0 issuing condition is: (2x2 PF) AND (4x4 > thres)
181         
182         AliDebug(999,"=== Running TRU L0 algorithm version 0 ===");
183
184         const Int_t xsize    = Int_t(fRegionSize->X());
185         const Int_t ysize    = Int_t(fRegionSize->Y());
186
187         Int_t **othr = new Int_t*[xsize];
188         Int_t **patt = new Int_t*[xsize];       
189         Int_t **buff = new Int_t*[xsize];       
190         
191         for (Int_t x = 0; x < xsize; x++) {
192                 othr[x] = new Int_t[ysize];
193                 patt[x] = new Int_t[ysize];
194                 buff[x] = new Int_t[ysize];
195         }
196         
197         for (Int_t i = 0; i < xsize; i++) {
198                 for (Int_t j = 0; j < ysize; j++) {
199                         othr[i][j] = 0; 
200                         patt[i][j] = 0;
201                         buff[i][j] = 0;
202                 }
203         }
204                         
205         // Time sliding window algorithm
206         for (int i = 0; i <= (kTimeBins - kTimeWindowSize); i++) 
207         {
208                 AliDebug(999,Form("----------- Time window: %d\n",i));
209                 
210                 if (AliDebugLevel()) ShowFastOR(i, -1);
211                 
212                 for (int j = 0; j < xsize; j++) {               
213                         for (int k = 0; k < ysize; k++) {
214                                 
215 //                              if (
216 //                                      !(j % int(fSubRegionSize->X())) 
217 //                                      && 
218 //                                      !(k % int(fSubRegionSize->Y())) 
219 //                                      && 
220 //                                      (j + int(fPatchSize->X() * fSubRegionSize->X()) <= xsize)
221 //                                      && 
222 //                                      (k + int(fPatchSize->Y() * fSubRegionSize->Y()) <= ysize)
223 //                                      ) 
224 //                              {
225 //                                      int sum = 0;
226 //                                      
227 //                                      for (int l = 0; l < int(fPatchSize->X() * fSubRegionSize->X()); l++) 
228 //                                              for (int m = 0; m < int(fPatchSize->Y() * fSubRegionSize->Y()); m++) sum += fRegion[j + l][k + m];
229 //                                      
230 //                                      if (sum > int(fDCSConfig->GetGTHRL0())) {
231 //                                              AliDebug(999,Form("----------- Patch (%2d,%2d) is over threshold\n", j, k));
232 //                                              othr[j][k] = sum;
233 //                                      }
234 //                              }
235                                 
236                                 buff[j][k] = fRegion[j][k];
237                                 
238                                 fRegion[j][k] = 0;
239                                 for (Int_t l = i; l < i + kTimeWindowSize; l++) fRegion[j][k] += fADC[fMap[j][k]][l];   
240                                 
241                                 if (fRegion[j][k] > buff[j][k]) {
242                                         patt[j][k] |= 0x1;
243                                 }
244                                 
245                                 if (patt[j][k]) AliDebug(999,Form("----------- (%2d,%2d) New: %d Old: %d patt: %x / pattern: %x / mask: %x", j, k, fRegion[j][k], buff[j][k], patt[j][k], pattern, mask));
246                         }
247                 }
248                 
249                 for (int j = 0; j <= int(fRegionSize->X() - fPatchSize->X() * fSubRegionSize->X()); j += int(fSubRegionSize->X())) {
250                         for (int k = 0; k <= int(fRegionSize->Y() - fPatchSize->Y() * fSubRegionSize->Y()); k += int(fSubRegionSize->Y())) {
251                                 
252 //                              if (!othr[j][k]) continue;
253                                 int sizeX = int(fPatchSize->X() * fSubRegionSize->X());                         
254                                 int sizeY = int(fPatchSize->Y() * fSubRegionSize->Y());
255                                 
256                                 int foundPeak = 0;
257                                 int sum       = 0;
258                                 
259                                 for (int l = 0; l < sizeX; l++) {
260                                         for (int m = 0; m < sizeY; m++) {
261                                                 sum += fRegion[j + l][k + m];
262                                                 
263                                                 if ((patt[j + l][k + m] & mask) == pattern) foundPeak++;
264                                         }
265                                 }
266                                 
267                                 if (sum > int(fDCSConfig->GetGTHRL0())) othr[j][k] = sum;
268                 
269                                 if (foundPeak && othr[j][k]) {
270                                         
271                                         new((*fPatches)[fPatches->GetEntriesFast()]) AliEMCALTriggerPatch(j, k, othr[j][k], i);
272                                         
273                                         AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)fPatches->At(fPatches->GetEntriesFast() - 1);
274                                         
275                                         if (AliDebugLevel()) p->Print("");
276                                         
277                                         const Int_t psize =  sizeX * sizeY; // Number of FastOR in the patch
278                                         
279                                         Int_t* idx = new Int_t[psize];
280                                         
281                                         for (Int_t l = 0; l < sizeX; l++) 
282                                         {
283                                                 for (Int_t m = 0; m < sizeY; m++) 
284                                                 {   
285                                                         Int_t index = l * sizeY + m;
286                                                         
287                                                         idx[index] = fMap[int(j * fSubRegionSize->X()) + l][int(k * fSubRegionSize->Y()) + m];
288                                                         
289                                                         if ((patt[j + l][k + m] & mask) == pattern) {
290 //                                                              cout << "setting peak at " << l << " " << m << endl;
291                                                                 p->SetPeak(l, m, sizeX, sizeY);
292                                                         }
293                                                         
294                                                         if (AliDebugLevel() >= 999) ShowFastOR(i, idx[index]);
295                                                 }
296                                         }
297                                         
298                                         delete [] idx;
299                                 }
300                         }
301                 }
302                 
303                 if (fPatches->GetEntriesFast() && !fL0Time) {                   
304                         // Stop the algo when at least one patch is found ( thres & max )
305                         fL0Time = i;
306
307 //                      break;
308                 }
309                 
310                 for (int j = 0; j < xsize; j++)         
311                         for (int k = 0; k < ysize; k++) patt[j][k] <<= 1;
312         }
313         
314         for (Int_t x = 0; x < xsize; x++) {
315                 delete [] othr[x];
316                 delete [] patt[x];
317                 delete [] buff[x];
318         }
319
320         delete [] othr;
321         delete [] patt;
322         delete [] buff;
323
324         return fPatches->GetEntriesFast();
325 }
326
327 //________________
328 Int_t AliEMCALTriggerTRU::L0v1(int mask, int pattern)
329 {
330         // L0 issuing condition is: (4x4 PF) AND (4x4 > thres)
331         
332         AliDebug(999,"=== Running TRU L0 algorithm version 1 ===");
333         
334         const Int_t xsize    = Int_t(fRegionSize->X());
335         const Int_t ysize    = Int_t(fRegionSize->Y());
336                 
337         Int_t **othr = new Int_t*[xsize];
338         Int_t **buff = new Int_t*[xsize];
339         Int_t **patt = new Int_t*[xsize];
340         
341         for (Int_t i = 0; i < xsize; i++) {
342                 buff[i] = new Int_t[ysize];
343                 patt[i] = new Int_t[ysize];
344                 othr[i] = new Int_t[ysize];
345         }
346         
347         for (Int_t i = 0; i < xsize; i++) for (Int_t j = 0; j < ysize; j++) {
348                 othr[i][j] = 0;
349                 patt[i][j] = 0;
350                 buff[i][j] = 0;
351         }
352         
353         // Time sliding window algorithm
354         for (Int_t i = 0; i <= (kTimeBins - kTimeWindowSize); i++) {
355                 
356                 AliDebug(999,Form("----------- Time window: %d\n",i));
357                 
358                 for (int j = 0; j < xsize; j++) {               
359                         for (int k = 0; k < ysize; k++) {
360                                 
361 //                              if (
362 //                                      !(j % int(fSubRegionSize->X())) 
363 //                                      && 
364 //                                      !(k % int(fSubRegionSize->Y())) 
365 //                                      && 
366 //                                      (j + int(fPatchSize->X() * fSubRegionSize->X()) <= xsize)
367 //                                      && 
368 //                                      (k + int(fPatchSize->Y() * fSubRegionSize->Y()) <= ysize)
369 //                                      ) 
370 //                              {
371 //                                      int sum = 0;
372 //                                      
373 //                                      for (int l = 0; l < int(fPatchSize->X() * fSubRegionSize->X()); l++) 
374 //                                              for (int m = 0; m < int(fPatchSize->Y() * fSubRegionSize->Y()); m++) sum += fRegion[j + l][k + m];
375 //                                      
376 //                                      if (sum > buff[j][k]) patt[j][k] |= 0x1;
377 //                                      
378 //                                      AliDebug(999,Form("----------- Patch (%2d,%2d) has sum %d while its whole time pattern is %x\n", j, k, sum, patt[j][k]));
379 //                                      
380 //                                      buff[j][k] = sum;
381 //                                      
382 //                                      if (sum > int(fDCSConfig->GetGTHRL0())) {
383 //                                              AliDebug(999,Form("----------- Patch (%2d,%2d) is over threshold\n", j, k));
384 //                                              othr[j][k] = sum;
385 //                                      }
386 //                              }
387                                 
388                                 fRegion[j][k] = 0;
389                                 for (Int_t l = i; l < i + kTimeWindowSize; l++) fRegion[j][k] += fADC[fMap[j][k]][l];   
390                         }
391                 }
392                 
393                 for (int j = 0; j <= int(fRegionSize->X() - fPatchSize->X() * fSubRegionSize->X()); j += int(fSubRegionSize->X())) {
394                         for (int k = 0; k <= int(fRegionSize->Y() - fPatchSize->Y() * fSubRegionSize->Y()); k += int(fSubRegionSize->Y())) {
395                                         
396                                 int sum = 0;
397                                 
398                                 for (int l = 0; l < int(fPatchSize->X() * fSubRegionSize->X()); l++) 
399                                         for (int m = 0; m < int(fPatchSize->Y() * fSubRegionSize->Y()); m++) sum += fRegion[j + l][k + m];
400                                 
401                                 if (sum > buff[j][k]) patt[j][k] |= 0x1;
402                                 
403                                 if (sum > int(fDCSConfig->GetGTHRL0())) {
404                                         AliDebug(999,Form("----------- Patch (%2d,%2d) is over threshold\n", j, k));
405                                         
406                                         othr[j][k] = sum;
407                                 }
408                                 
409                                 AliDebug(999,Form("----------- Patch (%2d,%2d) has sum %d while its whole time pattern is %x\n", j, k, sum, patt[j][k]));
410                                 
411                                 buff[j][k] = sum;
412                                 
413                                 if (othr[j][k] && (patt[j][k] & mask) == pattern) {
414                                         
415                                         new((*fPatches)[fPatches->GetEntriesFast()]) AliEMCALTriggerPatch(j, k, othr[j][k], i);
416                                         
417 //                                      AliDebug(999,Form("=== New L0 patch at (%2d,%2d) time: %2d",j, k, i));
418                                         
419                                         int sizeX = int(fPatchSize->X() * fSubRegionSize->X());                                 
420                                         int sizeY = int(fPatchSize->Y() * fSubRegionSize->Y());
421                                         
422                                         for (int xx = 0; xx < sizeX; xx++) {
423                                                 for (int yy = 0; yy < sizeY; yy++) {
424                                                         ((AliEMCALTriggerPatch*)fPatches->At(fPatches->GetEntriesFast() - 1))->SetPeak(xx, yy, sizeX, sizeY);
425                                                 }
426                                         }
427                                         
428                                         AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)fPatches->At(fPatches->GetEntriesFast() - 1);
429                                         
430                                         if (AliDebugLevel()) p->Print("");
431                                 }
432                         }
433                 }
434                 
435                 if (fPatches->GetEntriesFast() && !fL0Time) {
436                         fL0Time = i;
437                         
438 //                      break;
439                 } 
440                 
441                 for (int j = 0; j < xsize; j++)         
442                         for (int k = 0; k < ysize; k++) patt[j][k] <<= 1;
443         }
444         
445         for (Int_t x = 0; x < xsize; x++) {
446                 delete [] othr[x];
447                 delete [] patt[x];
448                 delete [] buff[x];
449         }
450         
451         delete [] othr;
452         delete [] patt;
453         delete [] buff;
454         
455         return fPatches->GetEntriesFast();
456 }
457
458 //________________
459 void AliEMCALTriggerTRU::SetADC( Int_t channel, Int_t bin, Int_t sig )
460 {
461   //Set ADC value
462   if (channel > 95 || bin > 255) {
463     AliError("TRU has 96 ADC channels and 256 bins only!");
464   }
465   else{ 
466         if (((fDCSConfig->GetMaskReg(int(channel / 16)) >> (channel % 16)) & 0x1) == 0) fADC[channel][bin] = sig;
467   }
468 }
469
470 //________________
471 void AliEMCALTriggerTRU::GetL0Region(const int time, Int_t arr[][4])
472 {
473         Int_t r0 = time - fDCSConfig->GetRLBKSTU();
474         
475         if (r0 < 0) 
476         {
477                 AliError(Form("TRU buffer not accessible! time: %d rollback: %d", time, fDCSConfig->GetRLBKSTU()));
478                 return;
479         }
480         
481         for (Int_t i = 0; i < fRegionSize->X(); i++) 
482         {
483                 for (Int_t j = 0; j < fRegionSize->Y(); j++) 
484                 {
485                         for (Int_t k = r0; k < r0 + kTimeWindowSize; k++)
486                         {
487                                 arr[i][j] += fADC[fMap[i][j]][k];
488                         }
489                 }
490         }
491 }
492
493 //________________
494 void AliEMCALTriggerTRU::SaveRegionADC(Int_t iTRU, Int_t iEvent)
495 {
496         // O for STU Hw
497         //
498         gSystem->Exec(Form("mkdir -p Event%d",iEvent));
499         
500         ofstream outfile(Form("Event%d/data_TRU%d.txt",iEvent,iTRU),ios_base::trunc);
501         
502         for (Int_t i=0;i<96;i++) 
503         {
504                 Int_t ietam = 23 - i/4;
505         
506                 Int_t iphim =  3 - i%4;
507                 
508                 outfile << fRegion[ietam][iphim] << endl;
509         }
510
511         outfile.close();
512 }
513
514 //________________
515 void AliEMCALTriggerTRU::Reset()
516 {
517         // Reset
518         
519         fPatches->Delete();
520         
521         ZeroRegion();
522         
523         for (Int_t i=0;i<96;i++) for (Int_t j=0;j<256;j++) fADC[i][j] = 0;
524 }
525