]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALTriggerSTURawStream.cxx
Coverity
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTriggerSTURawStream.cxx
1
2 /**************************************************************************
3  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4  *                                                                        *
5  * Author: The ALICE Off-line Project.                                    *
6  * Contributors are mentioned in the code where appropriate.              *
7  *                                                                        *
8  * Permission to use, copy, modify and distribute this software and its   *
9  * documentation strictly for non-commercial purposes is hereby granted   *
10  * without fee, provided that the above copyright notice appears in all   *
11  * copies and that both the copyright notice and this permission notice   *
12  * appear in the supporting documentation. The authors make no claims     *
13  * about the suitability of this software for any purpose. It is          *
14  * provided "as is" without express or implied warranty.                  *
15  **************************************************************************/
16
17 /*
18
19
20
21 This class provides access to STU DDL raw data.
22 Author: R. GUERNANE LPSC Grenoble CNRS/IN2P3
23 */
24
25 #include "AliEMCALTriggerSTURawStream.h"
26 #include "AliRawReader.h"
27 #include "AliLog.h"
28
29 #include "Riostream.h"
30 #include "TBits.h"
31
32 #include <cstdlib>
33
34 namespace
35 {
36         const Int_t kPayLoadSizeV0     = 236;
37         const Int_t kPayLoadSizeV1     = 245;        
38         const Int_t kPayLoadSizeV2     = 390;        
39 }
40
41 ClassImp(AliEMCALTriggerSTURawStream)
42
43 //_____________________________________________________________________________
44 AliEMCALTriggerSTURawStream::AliEMCALTriggerSTURawStream() : TObject(),
45 fRawReader(0x0),
46 fL1JetThreshold(),
47 fL1GammaThreshold(),
48 fL0GammaPatchIndex(),
49 fL1GammaPatchIndex(),
50 fL1JetPatchIndex(),
51 fNL0GammaPatch(0),
52 fNL1JetPatch(),
53 fNL1GammaPatch(),
54 fGetRawData(0),
55 fV0A(0),
56 fV0C(0),
57 fG(),
58 fJ(),
59 fRegionEnable(0),
60 fFrameReceived(0),
61 fFwVersion(0)
62 {
63         //
64         for (int i = 0; i < 2; i++) {
65                 //
66                 fL1JetThreshold[i] = fL1GammaThreshold[i] = 0;
67                 
68                 fNL1JetPatch[i] = fNL1GammaPatch[i] = 0;
69         }
70         
71         for (int i = 0; i < 3; i++) {
72                 for (int j = 0; j < 2; j++) {
73                         //
74                         fG[i][j] = fJ[i][j] = 0;
75                 }
76         }
77         
78         for (int i = 0; i < 3100; i++) {
79                 //
80                 fL0GammaPatchIndex[i] = 0;
81                 
82                 for (int j = 0; j < 2; j++) {
83                         
84                         fL1GammaPatchIndex[i][j] = 0;
85                 }
86         }
87         
88         for (int i = 0; i < 200; i++) {
89                 for (int j = 0; j < 2; j++) {
90                         
91                         fL1JetPatchIndex[i][j] = 0;
92                 }
93         }
94 }
95
96 //_____________________________________________________________________________
97 AliEMCALTriggerSTURawStream::AliEMCALTriggerSTURawStream(AliRawReader* rawReader) : TObject(),
98 fRawReader(rawReader),
99 fL1JetThreshold(),
100 fL1GammaThreshold(),
101 fL0GammaPatchIndex(),
102 fL1GammaPatchIndex(),
103 fL1JetPatchIndex(),
104 fNL0GammaPatch(0),
105 fNL1JetPatch(),
106 fNL1GammaPatch(),
107 fGetRawData(0),
108 fV0A(0),
109 fV0C(0),
110 fG(),
111 fJ(),
112 fRegionEnable(0),
113 fFrameReceived(0),
114 fFwVersion(0)
115 {
116         //
117         fRawReader->Reset();
118         fRawReader->Select("EMCAL",44);
119         //
120         for (int i = 0; i < 2; i++) {
121                 //
122                 fL1JetThreshold[i] = fL1GammaThreshold[i] = 0;
123                 
124                 fNL1JetPatch[i] = fNL1GammaPatch[i] = 0;
125         }
126         
127         for (int i = 0; i < 3; i++) {
128                 for (int j = 0; j < 2; j++) {
129                         //
130                         fG[i][j] = fJ[i][j] = 0;
131                 }
132         }
133         
134         for (int i = 0; i < 3100; i++) {
135                 //
136                 fL0GammaPatchIndex[i] = 0;
137                 
138                 for (int j = 0; j < 2; j++) {
139                         
140                         fL1GammaPatchIndex[i][j] = 0;
141                 }
142         }
143         
144         for (int i = 0; i < 200; i++) {
145                 for (int j = 0; j < 2; j++) {
146                         
147                         fL1JetPatchIndex[i][j] = 0;
148                 }
149         }
150 }
151
152 //_____________________________________________________________________________
153 AliEMCALTriggerSTURawStream::~AliEMCALTriggerSTURawStream()
154 {
155         // destructor
156 }
157
158 //_____________________________________________________________________________
159 void AliEMCALTriggerSTURawStream::Reset()
160 {
161         // Reset
162         
163         if (fRawReader) fRawReader->Reset();
164         
165         fNL0GammaPatch = 0;
166         fNL1GammaPatch[0] = fNL1GammaPatch[1] = 0;
167         fNL1JetPatch[0] = fNL1JetPatch[1] = 0;
168 }
169
170 //_____________________________________________________________________________
171 Bool_t AliEMCALTriggerSTURawStream::ReadPayLoad()
172 {
173         // STU data decoder from Olivier Bourrion LPSC CNRS-IN2P3
174         // bourrion_at_lpsc_dot_in2p3_dot_fr
175         
176         UInt_t word32[kPayLoadSizeV2 + 1536]; // 32b words
177         for (Int_t i = 0;i < kPayLoadSizeV2 + 1536; i++) word32[i] = 0;
178         
179         Int_t iword = 0;
180         
181         fNL0GammaPatch = 0;
182         fNL1GammaPatch[0] = fNL1GammaPatch[1] = 0;
183         fNL1JetPatch[0] = fNL1JetPatch[1] = 0;
184         
185         Int_t eqId = -1, eqSize = 0;
186         
187         UInt_t w32;
188         
189         while (fRawReader->ReadNextInt(w32)) 
190         {
191                 if (!iword)
192                 {
193                         eqId   = fRawReader->GetEquipmentId();
194                         eqSize = fRawReader->GetEquipmentSize();
195                 }
196                 
197                 word32[iword++] = w32;
198         }
199  
200         if (iword != kPayLoadSizeV0 && iword != kPayLoadSizeV1 && iword != kPayLoadSizeV2 
201                 && 
202                 iword != (kPayLoadSizeV0 + 1536) && iword != (kPayLoadSizeV1 + 1536) && iword != (kPayLoadSizeV2 + 1536))
203         {
204                 AliError(Form("STU payload (eqId: %d, eqSize: %d) doesn't match expected size! %d word32",
205                                           eqId, eqSize, iword));
206                 return kFALSE;
207         }
208         
209         AliDebug(1, Form("STU (eqId: %d, eqSize: %d) payload size: %d word32",
210                                          eqId, eqSize, iword));
211         
212         int offset = 1;//, jetSize = 2;
213         
214         int nthres = 1;
215         
216         switch (iword) 
217         {
218                 case kPayLoadSizeV0:
219                 case kPayLoadSizeV0 + 1536:
220                 {
221                         fL1JetThreshold[0]   = ((word32[0]>>16) & 0xFFFF);
222                         fL1GammaThreshold[0] =  (word32[0]      & 0xFFFF);
223                         
224                         break;
225                 }
226                 case kPayLoadSizeV1:
227                 case kPayLoadSizeV1 + 1536:
228                 {
229                         fV0A = ((word32[0]>>16) & 0xFFFF);
230                         fV0C =  (word32[0]      & 0xFFFF);
231                         
232                         fG[0][0]       = word32[1];
233                         fG[1][0]       = word32[2];
234                         fG[2][0]       = word32[3];
235                         fJ[0][0]       = word32[4];
236                         fJ[1][0]       = word32[5];
237                         fJ[2][0]       = word32[6];             
238                         fRegionEnable  = word32[7];
239                         fFrameReceived = word32[8];
240                         fFwVersion     = word32[9];
241                         
242                         fL1JetThreshold[0]   = GetThreshold(fJ[0][0], fJ[1][0], fJ[2][0], fV0A, fV0C);
243                         fL1GammaThreshold[0] = GetThreshold(fG[0][0], fG[1][0], fG[2][0], fV0A, fV0C); 
244                         
245                         offset = 10;
246                         
247                         break;
248                 }
249                 case kPayLoadSizeV2:
250                 case kPayLoadSizeV2 + 1536:
251                 {
252                         fV0A = ((word32[0]>>16) & 0xFFFF);
253                         fV0C =  (word32[0]      & 0xFFFF);
254                         
255                         fG[0][0]       = word32[1];
256                         fG[1][0]       = word32[2];
257                         fG[2][0]       = word32[3];
258                         fJ[0][0]       = word32[4];
259                         fJ[1][0]       = word32[5];
260                         fJ[2][0]       = word32[6];             
261                         
262                         fG[0][1]       = word32[7];
263                         fG[1][1]       = word32[8];
264                         fG[2][1]       = word32[9];
265                         fJ[0][1]       = word32[10];
266                         fJ[1][1]       = word32[11];
267                         fJ[2][1]       = word32[12];            
268                         
269                         fRegionEnable  = word32[13];
270                         fFrameReceived = word32[14];
271                         fFwVersion     = word32[15];
272                         
273                         for (int i = 0; i < 2; i++) {
274                                 //
275                                 fL1JetThreshold[i]   = GetThreshold(fJ[0][i], fJ[1][i], fJ[2][i], fV0A, fV0C);
276                                 fL1GammaThreshold[i] = GetThreshold(fG[0][i], fG[1][i], fG[2][i], fV0A, fV0C); 
277                         }
278                         
279                         offset = 16;                    
280                         
281                         nthres = 2;
282                         
283                         break;
284                 }
285         }
286         
287 //      jetSize += (fFwVersion >> 16);
288         
289         ///////////
290         // START DECODING
291         //////////
292
293         for (int i = 0; i < nthres; i++) {
294                 DecodeL1JetPatchIndexes(i, word32, offset);
295                 
296                 offset += 11;
297         }
298         
299         DecodeL0GammaPatchIndexes(word32, offset);
300         
301         offset += 96;
302         
303         for (int i = 0; i < nthres; i++) {
304                 DecodeL1GammaPatchIndexes(i, word32, offset);   
305                 
306                 offset += 128;
307         }
308         
309         if (iword == kPayLoadSizeV0 || iword == kPayLoadSizeV1 || iword == kPayLoadSizeV2) {
310                 fGetRawData = 0;
311                 return kTRUE;
312         }
313         
314         fGetRawData = 1;
315         
316         DecodeTRUADC(word32, offset);
317         
318         return kTRUE;
319 }
320
321 //_____________________________________________________________________________
322 void AliEMCALTriggerSTURawStream::DecodeL0GammaPatchIndexes(UInt_t *word32, const int offset)
323 {
324         //////////////////////////////////////////////////////////
325         // index des L0                                         //
326         //////////////////////////////////////////////////////////
327         // FIXME: sounds like not valid data
328         
329         unsigned short truL0indexes[32][6];
330         
331         // extraction from stream
332         for (Int_t index=0;index<6;index++)
333         {
334                 for (Int_t tru_num=0;tru_num<16;tru_num++)
335                 {
336                         truL0indexes[2*tru_num  ][index] = ( word32[offset + index * 16 + tru_num]        & 0xFFFF);
337                         truL0indexes[2*tru_num+1][index] = ((word32[offset + index * 16 + tru_num] >> 16) & 0xFFFF);
338                 }
339         }
340         
341         for (Int_t tru_num=0;tru_num<32;tru_num++) 
342         {
343                 for (Int_t index=0;index<6;index++) 
344                 {
345                         for (Int_t bit_num=0;bit_num<12;bit_num++)
346                         {
347                                 if ((truL0indexes[tru_num][index] & (1 << bit_num)))
348                                 {
349                                         Int_t idx = 12 * index + bit_num;
350                                         
351                                         fNL0GammaPatch++;
352                                         
353                                         fL0GammaPatchIndex[fNL0GammaPatch-1] = (((idx << 5) & 0x7E0) | (tru_num & 0x1F));
354                                 }
355                         }
356                 }
357         }
358 }
359
360 //_____________________________________________________________________________
361 void AliEMCALTriggerSTURawStream::DecodeL1JetPatchIndexes(const int i, UInt_t *word32, const int offset)
362 {
363         //////////////////////////////////////////////////////////
364         // index des L1 jet                                     //
365         //////////////////////////////////////////////////////////
366         
367         int jetSize = 2 + (fFwVersion >> 16);
368         
369         for (Int_t jet_row = 0; jet_row < 12 - (jetSize - 1); jet_row++)
370         {
371                 UInt_t currentrow = word32[offset + jet_row];
372                 
373                 for (Int_t jet_col = 0; jet_col < 15; jet_col++)
374                 {
375                         if (currentrow & (1 << jet_col))
376                         {
377                                 fNL1JetPatch[i] = fNL1JetPatch[i] + 1;
378                                 
379                                 fL1JetPatchIndex[fNL1JetPatch[i] - 1][i] = ((jet_row << 8) & 0xFF00) | (jet_col & 0xFF);
380                         }
381                 }
382         }
383 }
384
385 //_____________________________________________________________________________
386 void AliEMCALTriggerSTURawStream::DecodeL1GammaPatchIndexes(const int i, UInt_t *word32, const int offset)
387 {
388         //////////////////////////////////////////////////////////
389         // index des L1 gamma                                   //
390         //////////////////////////////////////////////////////////
391         
392         unsigned short truL1indexes[32][8];
393         
394         // extraction from stream
395         for (Int_t index=0;index<8;index++)
396         {
397                 for (Int_t tru_num=0;tru_num<16;tru_num++)
398                 {
399                         truL1indexes[2*tru_num  ][index] = ( word32[offset + index * 16 + tru_num]        & 0xFFFF);
400                         truL1indexes[2*tru_num+1][index] = ((word32[offset + index * 16 + tru_num] >> 16) & 0xFFFF);
401                 }
402         }       
403         
404         // interpretation
405         int gammacolnum;
406         short indexcopy;
407         
408         for (Int_t tru_num=0;tru_num<32;tru_num++)
409         {
410                 for (Int_t index=0;index<8;index++)
411                 {
412                         for (Int_t bit_num=0; bit_num<12; bit_num++)
413                         {
414                                 if ((truL1indexes[tru_num][index] & (1<<bit_num)) != 0)
415                                 {
416                                         if (index<4) // Even
417                                         {
418                                                 gammacolnum = (2*bit_num  );
419                                                 indexcopy   = index;
420                                         }
421                                         else         // Odd
422                                         {
423                                                 gammacolnum = (2*bit_num+1);
424                                                 indexcopy   = index-4;
425                                         }                                               
426                                         
427                                         fNL1GammaPatch[i] = fNL1GammaPatch[i] + 1;
428                                         
429                                         fL1GammaPatchIndex[fNL1GammaPatch[i] - 1][i] = (((indexcopy << 10) & 0xC00) | ((gammacolnum << 5) & 0x3E0) | (tru_num & 0x1F));
430                                 }
431                         }
432                 }
433         }       
434 }
435
436 //_____________________________________________________________________________
437 void AliEMCALTriggerSTURawStream::DecodeTRUADC(UInt_t *word32, const int offset)
438 {
439         // extraction from stream
440         for (Int_t index=0;index<96;index++)
441         {
442                 for (Int_t tru_num=0;tru_num<16;tru_num++)
443                 {
444                         fADC[2*tru_num  ][index] = ( word32[offset + index * 16 + tru_num]        & 0xFFFF);
445                         fADC[2*tru_num+1][index] = ((word32[offset + index * 16 + tru_num] >> 16) & 0xFFFF);
446                 }
447         }       
448         
449         for (Int_t tru_num=16;tru_num<32;tru_num++) // A side
450         {
451                 Int_t v[96];
452                 for (Int_t index=0;index<96;index++) v[index] = fADC[tru_num][95-index];
453                 
454                 for (Int_t index=0;index<96;index++) fADC[tru_num][index] = v[index];
455         }
456 }
457
458
459 //_____________________________________________________________________________
460 Bool_t AliEMCALTriggerSTURawStream::GetL0GammaPatch(const Int_t i, Int_t& tru, Int_t& idx) const
461 {
462         // L0 gamma patches sent to STU (original access to L0 patch indexes)
463         
464         if (i > fNL0GammaPatch) return kFALSE;
465         
466         tru =  fL0GammaPatchIndex[i] & 0x1F;
467         idx = (fL0GammaPatchIndex[i] & 0x7E0) >> 5;
468         
469         return kTRUE;
470 }
471
472 //_____________________________________________________________________________
473 Bool_t AliEMCALTriggerSTURawStream::GetL1GammaPatch(const Int_t i, const Int_t j, Int_t& tru, Int_t& col, Int_t& row) const
474 {
475         // L1 gamma patch indexes
476         
477         if (j >= 2 || i > fNL1GammaPatch[j]) return kFALSE;
478         
479         tru =  fL1GammaPatchIndex[i][j] & 0x1F;
480         col = (fL1GammaPatchIndex[i][j] & 0x3E0) >> 5;
481         row = (fL1GammaPatchIndex[i][j] & 0xC00) >> 10;
482         
483         return kTRUE;
484 }
485
486 //_____________________________________________________________________________
487 Bool_t AliEMCALTriggerSTURawStream::GetL1JetPatch(const Int_t i, const Int_t j, Int_t& col, Int_t& row) const
488 {
489         // L1 jet patch indexes
490         
491         if (j >= 2 || i > fNL1JetPatch[j]) return kFALSE;
492         
493         col =  fL1JetPatchIndex[i][j] & 0xFF;
494         row = (fL1JetPatchIndex[i][j] & 0xFF00) >> 8;
495         
496         return kTRUE;
497 }
498
499 //_____________________________________________________________________________
500 void AliEMCALTriggerSTURawStream::GetADC(Int_t iTRU, UInt_t ADC[])
501 {
502         // Time sums
503         
504         for (Int_t i=0; i<96; i++) ADC[i] = fADC[iTRU][i];
505 }
506
507 //_____________________________________________________________________________
508 void AliEMCALTriggerSTURawStream::DumpPayLoad(const Option_t *option) const
509 {
510         // Dump STU payload
511         
512         TString op = option;
513         
514         printf("V0A:             %d\n", fV0A);
515         printf("V0C:             %d\n", fV0C);
516
517         printf("RawData:         %d\n", fGetRawData);
518         printf("RegionEnable:    %8x\n", fRegionEnable); 
519         printf("FrameReceived:   %8x\n", fFrameReceived);
520         printf("FwVersion:       %x\n", fFwVersion);
521         printf("Number of L0:    %d\n", fNL0GammaPatch);
522
523         for (int i = 0; i < 2; i++) {
524                 for (int j = 0; j < 3; j++) {
525                         printf("G[%d][%d]: %d\n", j, i, fG[j][i]);
526                         printf("J[%d][%d]: %d\n", j, i, fJ[j][i]);
527                 }
528                 
529                 printf("Gamma threshold[%d]: %d\n", i, fL1GammaThreshold[i]);
530                 printf("Jet Threshold[%d]:   %d\n", i, fL1JetThreshold[i]);
531
532                 printf("Number of L1-g[%d]: %d\n", i, fNL1GammaPatch[i]);
533                 printf("Number of L1-j[%d]: %d\n", i, fNL1JetPatch[i]);
534         }
535         
536         Int_t itru, col, row;
537         
538         if (op.Contains("L0") || op.Contains("ALL"))
539         {
540                 for (Int_t i = 0;i < fNL0GammaPatch; i++) {
541                         if (GetL0GammaPatch(i,itru,col)) 
542                                 cout << "> Found L0 gamma in TRU #" << setw(2) << itru <<  " at idx: " << setw(2) << col << endl;
543                 }
544         }
545         
546         if (op.Contains("L1") || op.Contains("ALL")) {
547                 for (int j = 0; j < 2; j++) {
548                         for (Int_t i = 0; i < fNL1GammaPatch[j]; i++) {
549                                 if (GetL1GammaPatch(i, j, itru, col, row)) 
550                                         cout << "> Found L1 gamma " << j << " in TRU #" << setw(2) << itru <<  " at: ( col: " << setw(2) << col << " , row: " << setw(2) << row << " )" << endl;
551                         }
552                         
553                         for (Int_t i = 0; i < fNL1JetPatch[j]; i++) {
554                                 if (GetL1JetPatch(i, j, col, row)) cout << "> Found L1 jet " << j << " at: ( col: " << setw(2) << col << " , row: " << setw(2) << row << " )" << endl;
555                         }
556                 }
557         }
558         
559         if ((op.Contains("ADC") || op.Contains("ALL")) && fGetRawData) {
560                 for (Int_t i = 0; i < 32; i++) {
561                         cout << "--------\n";
562                         cout << "TRU #" << setw(2) << i << ":";
563                         for (Int_t j = 0;j < 96; j++) { 
564                                 //TBits xadc(12); xadc.Set(12,&fADC[i][j]); 
565                                 if ((j % 4) == 0) cout << endl;
566                                 //cout << setw(2) << j << ": " << xadc << " ";
567                                 printf("%2d: %3x / ",j,fADC[i][j]); 
568                         }
569                         cout << "\n";
570                 }
571         }
572 }
573
574 //_____________________________________________________________________________
575 UShort_t AliEMCALTriggerSTURawStream::GetThreshold(Short_t A, Short_t B, Short_t C, UShort_t V0A, UShort_t V0C) const
576 {
577         // Get threshold 
578   ULong64_t v0sum = V0A + V0C;
579   
580   ULong64_t sqrV0 = v0sum * v0sum;                                      
581                                         
582   sqrV0 *= A;
583                                         
584   sqrV0 >>= 32;
585                                 
586   v0sum *= B;
587                                         
588   v0sum >>= 16;
589                                         
590   return (UShort_t)(sqrV0 + v0sum + C);
591 }