]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALTriggerTRU.cxx
example macros to create OADB files
[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->IsA()->GetClassVersion() >= 3) {
171                 if (fDCSConfig->GetFw() < 0x4d) {
172                         return L0v0(nb, ma);
173                 } else {
174                         return L0v1(nb, ma);
175                 }
176         } else
177                 return L0v0(nb, ma);
178 }
179
180 //________________
181 Int_t AliEMCALTriggerTRU::L0v0(int mask, int pattern)
182 {
183         // L0 issuing condition is: (2x2 PF) AND (4x4 > thres)
184         
185         AliDebug(999,"=== Running TRU L0 algorithm version 0 ===");
186
187         const Int_t xsize    = Int_t(fRegionSize->X());
188         const Int_t ysize    = Int_t(fRegionSize->Y());
189
190         Int_t **othr = new Int_t*[xsize];
191         Int_t **patt = new Int_t*[xsize];       
192         Int_t **buff = new Int_t*[xsize];       
193         
194         for (Int_t x = 0; x < xsize; x++) {
195                 othr[x] = new Int_t[ysize];
196                 patt[x] = new Int_t[ysize];
197                 buff[x] = new Int_t[ysize];
198         }
199         
200         for (Int_t i = 0; i < xsize; i++) {
201                 for (Int_t j = 0; j < ysize; j++) {
202                         othr[i][j] = 0; 
203                         patt[i][j] = 0;
204                         buff[i][j] = 0;
205                 }
206         }
207                         
208         // Time sliding window algorithm
209         for (int i = 0; i <= (kTimeBins - kTimeWindowSize); i++) 
210         {
211                 AliDebug(999,Form("----------- Time window: %d\n",i));
212                 
213                 if (AliDebugLevel()) ShowFastOR(i, -1);
214                 
215                 for (int j = 0; j < xsize; j++) {               
216                         for (int k = 0; k < ysize; k++) {
217                                 
218 //                              if (
219 //                                      !(j % int(fSubRegionSize->X())) 
220 //                                      && 
221 //                                      !(k % int(fSubRegionSize->Y())) 
222 //                                      && 
223 //                                      (j + int(fPatchSize->X() * fSubRegionSize->X()) <= xsize)
224 //                                      && 
225 //                                      (k + int(fPatchSize->Y() * fSubRegionSize->Y()) <= ysize)
226 //                                      ) 
227 //                              {
228 //                                      int sum = 0;
229 //                                      
230 //                                      for (int l = 0; l < int(fPatchSize->X() * fSubRegionSize->X()); l++) 
231 //                                              for (int m = 0; m < int(fPatchSize->Y() * fSubRegionSize->Y()); m++) sum += fRegion[j + l][k + m];
232 //                                      
233 //                                      if (sum > int(fDCSConfig->GetGTHRL0())) {
234 //                                              AliDebug(999,Form("----------- Patch (%2d,%2d) is over threshold\n", j, k));
235 //                                              othr[j][k] = sum;
236 //                                      }
237 //                              }
238                                 
239                                 buff[j][k] = fRegion[j][k];
240                                 
241                                 fRegion[j][k] = 0;
242                                 for (Int_t l = i; l < i + kTimeWindowSize; l++) fRegion[j][k] += fADC[fMap[j][k]][l];   
243                                 
244                                 if (fRegion[j][k] > buff[j][k]) {
245                                         patt[j][k] |= 0x1;
246                                 }
247                                 
248                                 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));
249                         }
250                 }
251                 
252                 for (int j = 0; j <= int(fRegionSize->X() - fPatchSize->X() * fSubRegionSize->X()); j += int(fSubRegionSize->X())) {
253                         for (int k = 0; k <= int(fRegionSize->Y() - fPatchSize->Y() * fSubRegionSize->Y()); k += int(fSubRegionSize->Y())) {
254                                 
255 //                              if (!othr[j][k]) continue;
256                                 int sizeX = int(fPatchSize->X() * fSubRegionSize->X());                         
257                                 int sizeY = int(fPatchSize->Y() * fSubRegionSize->Y());
258                                 
259                                 int foundPeak = 0;
260                                 int sum       = 0;
261                                 
262                                 for (int l = 0; l < sizeX; l++) {
263                                         for (int m = 0; m < sizeY; m++) {
264                                                 sum += fRegion[j + l][k + m];
265                                                 
266                                                 if ((patt[j + l][k + m] & mask) == pattern) foundPeak++;
267                                         }
268                                 }
269                                 
270                                 if (sum > int(fDCSConfig->GetGTHRL0())) othr[j][k] = sum;
271                 
272                                 if (foundPeak && othr[j][k]) {
273                                         
274                                         new((*fPatches)[fPatches->GetEntriesFast()]) AliEMCALTriggerPatch(j, k, othr[j][k], i);
275                                         
276                                         AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)fPatches->At(fPatches->GetEntriesFast() - 1);
277                                         
278                                         if (AliDebugLevel()) p->Print("");
279                                         
280                                         const Int_t psize =  sizeX * sizeY; // Number of FastOR in the patch
281                                         
282                                         Int_t* idx = new Int_t[psize];
283                                         
284                                         for (Int_t l = 0; l < sizeX; l++) 
285                                         {
286                                                 for (Int_t m = 0; m < sizeY; m++) 
287                                                 {   
288                                                         Int_t index = l * sizeY + m;
289                                                         
290                                                         idx[index] = fMap[int(j * fSubRegionSize->X()) + l][int(k * fSubRegionSize->Y()) + m];
291                                                         
292                                                         if ((patt[j + l][k + m] & mask) == pattern) {
293 //                                                              cout << "setting peak at " << l << " " << m << endl;
294                                                                 p->SetPeak(l, m, sizeX, sizeY);
295                                                         }
296                                                         
297                                                         if (AliDebugLevel() >= 999) ShowFastOR(i, idx[index]);
298                                                 }
299                                         }
300                                         
301                                         delete [] idx;
302                                 }
303                         }
304                 }
305                 
306                 if (fPatches->GetEntriesFast() && !fL0Time) {                   
307                         // Stop the algo when at least one patch is found ( thres & max )
308                         fL0Time = i;
309
310 //                      break;
311                 }
312                 
313                 for (int j = 0; j < xsize; j++)         
314                         for (int k = 0; k < ysize; k++) patt[j][k] <<= 1;
315         }
316         
317         for (Int_t x = 0; x < xsize; x++) {
318                 delete [] othr[x];
319                 delete [] patt[x];
320                 delete [] buff[x];
321         }
322
323         delete [] othr;
324         delete [] patt;
325         delete [] buff;
326
327         return fPatches->GetEntriesFast();
328 }
329
330 //________________
331 Int_t AliEMCALTriggerTRU::L0v1(int mask, int pattern)
332 {
333         // L0 issuing condition is: (4x4 PF) AND (4x4 > thres)
334         
335         AliDebug(999,"=== Running TRU L0 algorithm version 1 ===");
336         
337         const Int_t xsize    = Int_t(fRegionSize->X());
338         const Int_t ysize    = Int_t(fRegionSize->Y());
339                 
340         Int_t **othr = new Int_t*[xsize];
341         Int_t **buff = new Int_t*[xsize];
342         Int_t **patt = new Int_t*[xsize];
343         
344         for (Int_t i = 0; i < xsize; i++) {
345                 buff[i] = new Int_t[ysize];
346                 patt[i] = new Int_t[ysize];
347                 othr[i] = new Int_t[ysize];
348         }
349         
350         for (Int_t i = 0; i < xsize; i++) for (Int_t j = 0; j < ysize; j++) {
351                 othr[i][j] = 0;
352                 patt[i][j] = 0;
353                 buff[i][j] = 0;
354         }
355         
356         // Time sliding window algorithm
357         for (Int_t i = 0; i <= (kTimeBins - kTimeWindowSize); i++) {
358                 
359                 AliDebug(999,Form("----------- Time window: %d\n",i));
360                 
361                 for (int j = 0; j < xsize; j++) {               
362                         for (int k = 0; k < ysize; k++) {
363                                 
364 //                              if (
365 //                                      !(j % int(fSubRegionSize->X())) 
366 //                                      && 
367 //                                      !(k % int(fSubRegionSize->Y())) 
368 //                                      && 
369 //                                      (j + int(fPatchSize->X() * fSubRegionSize->X()) <= xsize)
370 //                                      && 
371 //                                      (k + int(fPatchSize->Y() * fSubRegionSize->Y()) <= ysize)
372 //                                      ) 
373 //                              {
374 //                                      int sum = 0;
375 //                                      
376 //                                      for (int l = 0; l < int(fPatchSize->X() * fSubRegionSize->X()); l++) 
377 //                                              for (int m = 0; m < int(fPatchSize->Y() * fSubRegionSize->Y()); m++) sum += fRegion[j + l][k + m];
378 //                                      
379 //                                      if (sum > buff[j][k]) patt[j][k] |= 0x1;
380 //                                      
381 //                                      AliDebug(999,Form("----------- Patch (%2d,%2d) has sum %d while its whole time pattern is %x\n", j, k, sum, patt[j][k]));
382 //                                      
383 //                                      buff[j][k] = sum;
384 //                                      
385 //                                      if (sum > int(fDCSConfig->GetGTHRL0())) {
386 //                                              AliDebug(999,Form("----------- Patch (%2d,%2d) is over threshold\n", j, k));
387 //                                              othr[j][k] = sum;
388 //                                      }
389 //                              }
390                                 
391                                 fRegion[j][k] = 0;
392                                 for (Int_t l = i; l < i + kTimeWindowSize; l++) fRegion[j][k] += fADC[fMap[j][k]][l];   
393                         }
394                 }
395                 
396                 for (int j = 0; j <= int(fRegionSize->X() - fPatchSize->X() * fSubRegionSize->X()); j += int(fSubRegionSize->X())) {
397                         for (int k = 0; k <= int(fRegionSize->Y() - fPatchSize->Y() * fSubRegionSize->Y()); k += int(fSubRegionSize->Y())) {
398                                         
399                                 int sum = 0;
400                                 
401                                 for (int l = 0; l < int(fPatchSize->X() * fSubRegionSize->X()); l++) 
402                                         for (int m = 0; m < int(fPatchSize->Y() * fSubRegionSize->Y()); m++) sum += fRegion[j + l][k + m];
403                                 
404                                 if (sum > buff[j][k]) patt[j][k] |= 0x1;
405                                 
406                                 if (sum > int(fDCSConfig->GetGTHRL0())) {
407                                         AliDebug(999,Form("----------- Patch (%2d,%2d) is over threshold\n", j, k));
408                                         
409                                         othr[j][k] = sum;
410                                 }
411                                 
412                                 AliDebug(999,Form("----------- Patch (%2d,%2d) has sum %d while its whole time pattern is %x\n", j, k, sum, patt[j][k]));
413                                 
414                                 buff[j][k] = sum;
415                                 
416                                 if (othr[j][k] && (patt[j][k] & mask) == pattern) {
417                                         
418                                         new((*fPatches)[fPatches->GetEntriesFast()]) AliEMCALTriggerPatch(j, k, othr[j][k], i);
419                                         
420 //                                      AliDebug(999,Form("=== New L0 patch at (%2d,%2d) time: %2d",j, k, i));
421                                         
422                                         int sizeX = int(fPatchSize->X() * fSubRegionSize->X());                                 
423                                         int sizeY = int(fPatchSize->Y() * fSubRegionSize->Y());
424                                         
425                                         for (int xx = 0; xx < sizeX; xx++) {
426                                                 for (int yy = 0; yy < sizeY; yy++) {
427                                                         ((AliEMCALTriggerPatch*)fPatches->At(fPatches->GetEntriesFast() - 1))->SetPeak(xx, yy, sizeX, sizeY);
428                                                 }
429                                         }
430                                         
431                                         AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)fPatches->At(fPatches->GetEntriesFast() - 1);
432                                         
433                                         if (AliDebugLevel()) p->Print("");
434                                 }
435                         }
436                 }
437                 
438                 if (fPatches->GetEntriesFast() && !fL0Time) {
439                         fL0Time = i;
440                         
441 //                      break;
442                 } 
443                 
444                 for (int j = 0; j < xsize; j++)         
445                         for (int k = 0; k < ysize; k++) patt[j][k] <<= 1;
446         }
447         
448         for (Int_t x = 0; x < xsize; x++) {
449                 delete [] othr[x];
450                 delete [] patt[x];
451                 delete [] buff[x];
452         }
453         
454         delete [] othr;
455         delete [] patt;
456         delete [] buff;
457         
458         return fPatches->GetEntriesFast();
459 }
460
461 //________________
462 void AliEMCALTriggerTRU::SetADC( Int_t channel, Int_t bin, Int_t sig )
463 {
464   //Set ADC value
465   if (channel > 95 || bin > 255) {
466     AliError("TRU has 96 ADC channels and 256 bins only!");
467   }
468   else{ 
469         if (((fDCSConfig->GetMaskReg(int(channel / 16)) >> (channel % 16)) & 0x1) == 0) fADC[channel][bin] = sig;
470   }
471 }
472
473 //________________
474 void AliEMCALTriggerTRU::GetL0Region(const int time, Int_t arr[][4])
475 {
476         Int_t r0 = time - fDCSConfig->GetRLBKSTU();
477         
478         if (r0 < 0) 
479         {
480                 AliError(Form("TRU buffer not accessible! time: %d rollback: %d", time, fDCSConfig->GetRLBKSTU()));
481                 return;
482         }
483         
484         for (Int_t i = 0; i < fRegionSize->X(); i++) 
485         {
486                 for (Int_t j = 0; j < fRegionSize->Y(); j++) 
487                 {
488                         for (Int_t k = r0; k < r0 + kTimeWindowSize; k++)
489                         {
490                                 arr[i][j] += fADC[fMap[i][j]][k];
491                         }
492                 }
493         }
494 }
495
496 //________________
497 void AliEMCALTriggerTRU::SaveRegionADC(Int_t iTRU, Int_t iEvent)
498 {
499         // O for STU Hw
500         //
501         gSystem->Exec(Form("mkdir -p Event%d",iEvent));
502         
503         ofstream outfile(Form("Event%d/data_TRU%d.txt",iEvent,iTRU),ios_base::trunc);
504         
505         for (Int_t i=0;i<96;i++) 
506         {
507                 Int_t ietam = 23 - i/4;
508         
509                 Int_t iphim =  3 - i%4;
510                 
511                 outfile << fRegion[ietam][iphim] << endl;
512         }
513
514         outfile.close();
515 }
516
517 //________________
518 void AliEMCALTriggerTRU::Reset()
519 {
520         // Reset
521         
522         fPatches->Delete();
523         
524         ZeroRegion();
525         
526         for (Int_t i=0;i<96;i++) for (Int_t j=0;j<256;j++) fADC[i][j] = 0;
527 }
528