SensorThickness was defined twice. Set inner chip thickness to 250mum to bypass bug...
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTriggerTRU.cxx
CommitLineData
916f1e76 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
21Author: R. GUERNANE LPSC Grenoble CNRS/IN2P3
22*/
23
24
25#include "AliEMCALTriggerTRU.h"
26#include "AliEMCALTriggerPatch.h"
fff39dd1 27#include "AliEMCALTriggerTRUDCSConfig.h"
916f1e76 28#include "AliLog.h"
29
916f1e76 30#include <TClonesArray.h>
31#include <TSystem.h>
32#include <Riostream.h>
c4227ef7 33#include <TFile.h>
34#include <TROOT.h>
63c22917 35
916f1e76 36namespace
37{
fff39dd1 38 const Int_t kTimeBins = 16; // number of sampling bins of the FastOR signal
916f1e76 39 const Int_t kTimeWindowSize = 4; //
916f1e76 40}
41
0267cfa6 42using std::ofstream;
43using std::endl;
44using std::ios_base;
916f1e76 45ClassImp(AliEMCALTriggerTRU)
46
47//________________
de39a0ff 48AliEMCALTriggerTRU::AliEMCALTriggerTRU() : AliEMCALTriggerBoard(),
804b828a 49fDCSConfig(0x0),
50fL0Time(0)
916f1e76 51{
79b05051 52 // Ctor
53
916f1e76 54 for (Int_t i=0;i<96;i++) for (Int_t j=0;j<256;j++) fADC[i][j] = 0;
55}
56
57//________________
fff39dd1 58AliEMCALTriggerTRU::AliEMCALTriggerTRU(AliEMCALTriggerTRUDCSConfig* dcsConf, const TVector2& rSize, Int_t mapType) :
de39a0ff 59AliEMCALTriggerBoard(rSize),
804b828a 60fDCSConfig(dcsConf),
61fL0Time(0)
916f1e76 62{
79b05051 63 // Ctor
64
916f1e76 65 for (Int_t i=0;i<96;i++) for (Int_t j=0;j<256;j++) fADC[i][j] = 0;
63c22917 66
916f1e76 67 TVector2 size;
68
de39a0ff 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 }
916f1e76 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//________________
100AliEMCALTriggerTRU::~AliEMCALTriggerTRU()
101{
79b05051 102 // Dtor
916f1e76 103}
104
105//________________
916f1e76 106void AliEMCALTriggerTRU::ShowFastOR(Int_t iTimeWindow, Int_t iChannel)
107{
79b05051 108 // Dump
109
916f1e76 110 Int_t iChanF, iChanL;
111
112 if (iChannel != -1) iChanF = iChanL = iChannel;
113 else
114 {
115 iChanF = 0;
63c22917 116 iChanL = 95;
916f1e76 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//________________
fff39dd1 137Int_t AliEMCALTriggerTRU::L0()
916f1e76 138{
63c22917 139 // L0 algo depending on TRU fw version
140
8476b1c8 141 const Int_t xsize = Int_t(fRegionSize->X());
8b07b672 142 const Int_t ysize = Int_t(fRegionSize->Y());
8b07b672 143
63c22917 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 }
f4e2cc1e 150 }
63c22917 151 }
152
153 // TRU has no signal, return!
154 if (!asum) {
155 AliDebug(999,"=== TRU has no signal ===");
156 return 0;
f4e2cc1e 157 }
916f1e76 158
63c22917 159 AliDebug(999,Form("=== TRU PF: %x",fDCSConfig->GetSELPF()));
c4227ef7 160
63c22917 161 UInt_t ma = fDCSConfig->GetSELPF() & 0xffff;
162
c4227ef7 163 // Set default peak finder if null
164 if (!ma) ma = 0x1e1f;
165
63c22917 166 int nb = ma & 0x7f;
167
168 ma = (ma >> 8) & 0x7f;
169
170 AliDebug(999,Form("=== TRU fw version %x ===",fDCSConfig->GetFw()));
c4227ef7 171
172 if (fDCSConfig->GetFw() < 0x4d) {
173 return L0v0(nb, ma);
174 } else {
175 return L0v1(nb, ma);
176 }
63c22917 177}
178
179//________________
180Int_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
fff39dd1 207 // Time sliding window algorithm
63c22917 208 for (int i = 0; i <= (kTimeBins - kTimeWindowSize); i++)
916f1e76 209 {
de39a0ff 210 AliDebug(999,Form("----------- Time window: %d\n",i));
fff39dd1 211
63c22917 212 if (AliDebugLevel()) ShowFastOR(i, -1);
213
214 for (int j = 0; j < xsize; j++) {
215 for (int k = 0; k < ysize; k++) {
916f1e76 216
63c22917 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// }
916f1e76 237
63c22917 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;
fff39dd1 245 }
916f1e76 246
63c22917 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));
916f1e76 248 }
249 }
250
63c22917 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())) {
fff39dd1 253
63c22917 254// if (!othr[j][k]) continue;
255 int sizeX = int(fPatchSize->X() * fSubRegionSize->X());
256 int sizeY = int(fPatchSize->Y() * fSubRegionSize->Y());
fff39dd1 257
63c22917 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;
fff39dd1 270
63c22917 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
c4227ef7 291 if ((patt[j + l][k + m] & mask) == (pattern & mask)) {
63c22917 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;
916f1e76 301 }
916f1e76 302 }
303 }
63c22917 304
305 if (fPatches->GetEntriesFast() && !fL0Time) {
306 // Stop the algo when at least one patch is found ( thres & max )
307 fL0Time = i;
fff39dd1 308
63c22917 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 }
4bd37bd4 321
63c22917 322 delete [] othr;
323 delete [] patt;
324 delete [] buff;
4bd37bd4 325
63c22917 326 return fPatches->GetEntriesFast();
327}
328
329//________________
330Int_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());
916f1e76 338
63c22917 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++) {
916f1e76 357
63c22917 358 AliDebug(999,Form("----------- Time window: %d\n",i));
916f1e76 359
63c22917 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 }
916f1e76 394
63c22917 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())) {
916f1e76 397
63c22917 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));
916f1e76 407
63c22917 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
c4227ef7 415 if (othr[j][k] && ((patt[j][k] & mask) == (pattern & mask))) {
63c22917 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 }
de39a0ff 428 }
fff39dd1 429
63c22917 430 AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)fPatches->At(fPatches->GetEntriesFast() - 1);
431
432 if (AliDebugLevel()) p->Print("");
916f1e76 433 }
434 }
916f1e76 435 }
fff39dd1 436
63c22917 437 if (fPatches->GetEntriesFast() && !fL0Time) {
804b828a 438 fL0Time = i;
439
63c22917 440// break;
441 }
442
443 for (int j = 0; j < xsize; j++)
444 for (int k = 0; k < ysize; k++) patt[j][k] <<= 1;
916f1e76 445 }
446
63c22917 447 for (Int_t x = 0; x < xsize; x++) {
448 delete [] othr[x];
449 delete [] patt[x];
450 delete [] buff[x];
f4e2cc1e 451 }
63c22917 452
453 delete [] othr;
454 delete [] patt;
455 delete [] buff;
8b07b672 456
916f1e76 457 return fPatches->GetEntriesFast();
458}
459
916f1e76 460//________________
461void AliEMCALTriggerTRU::SetADC( Int_t channel, Int_t bin, Int_t sig )
462{
a51e676d 463 //Set ADC value
464 if (channel > 95 || bin > 255) {
465 AliError("TRU has 96 ADC channels and 256 bins only!");
466 }
467 else{
63c22917 468 if (((fDCSConfig->GetMaskReg(int(channel / 16)) >> (channel % 16)) & 0x1) == 0) fADC[channel][bin] = sig;
a51e676d 469 }
916f1e76 470}
471
472//________________
804b828a 473void AliEMCALTriggerTRU::GetL0Region(const int time, Int_t arr[][4])
474{
63c22917 475 Int_t r0 = time - fDCSConfig->GetRLBKSTU();
804b828a 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//________________
916f1e76 496void 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
916f1e76 516//________________
517void AliEMCALTriggerTRU::Reset()
518{
79b05051 519 // Reset
520
916f1e76 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