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