]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALTriggerSTURawStream.cxx
Changes for #93916 EMCAL commit attached patch and port to the release
[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                 default:
286                         AliError(Form("STU payload size not found! %d word32", iword));
287         }
288         
289 //      jetSize += (fFwVersion >> 16);
290         
291         ///////////
292         // START DECODING
293         //////////
294
295         for (int i = 0; i < nthres; i++) {
296                 DecodeL1JetPatchIndexes(i, word32, offset);
297                 
298                 offset += 11;
299         }
300         
301         DecodeL0GammaPatchIndexes(word32, offset);
302         
303         offset += 96;
304         
305         for (int i = 0; i < nthres; i++) {
306                 DecodeL1GammaPatchIndexes(i, word32, offset);   
307                 
308                 offset += 128;
309         }
310         
311         if (iword == kPayLoadSizeV0 || iword == kPayLoadSizeV1 || iword == kPayLoadSizeV2) {
312                 fGetRawData = 0;
313                 return kTRUE;
314         }
315         
316         fGetRawData = 1;
317         
318         DecodeTRUADC(word32, offset);
319         
320         return kTRUE;
321 }
322
323 //_____________________________________________________________________________
324 void AliEMCALTriggerSTURawStream::DecodeL0GammaPatchIndexes(UInt_t *word32, const int offset)
325 {
326         //////////////////////////////////////////////////////////
327         // index des L0                                         //
328         //////////////////////////////////////////////////////////
329         // FIXME: sounds like not valid data
330         
331         unsigned short truL0indexes[32][6];
332         
333         // extraction from stream
334         for (Int_t index=0;index<6;index++)
335         {
336                 for (Int_t tru_num=0;tru_num<16;tru_num++)
337                 {
338                         truL0indexes[2*tru_num  ][index] = ( word32[offset + index * 16 + tru_num]        & 0xFFFF);
339                         truL0indexes[2*tru_num+1][index] = ((word32[offset + index * 16 + tru_num] >> 16) & 0xFFFF);
340                 }
341         }
342         
343         for (Int_t tru_num=0;tru_num<32;tru_num++) 
344         {
345                 for (Int_t index=0;index<6;index++) 
346                 {
347                         for (Int_t bit_num=0;bit_num<12;bit_num++)
348                         {
349                                 if ((truL0indexes[tru_num][index] & (1 << bit_num)))
350                                 {
351                                         Int_t idx = 12 * index + bit_num;
352                                         
353                                         fNL0GammaPatch++;
354                                         
355                                         fL0GammaPatchIndex[fNL0GammaPatch-1] = (((idx << 5) & 0x7E0) | (tru_num & 0x1F));
356                                 }
357                         }
358                 }
359         }
360 }
361
362 //_____________________________________________________________________________
363 void AliEMCALTriggerSTURawStream::DecodeL1JetPatchIndexes(const int i, UInt_t *word32, const int offset)
364 {
365         //////////////////////////////////////////////////////////
366         // index des L1 jet                                     //
367         //////////////////////////////////////////////////////////
368         
369         int jetSize = 2 + (fFwVersion >> 16);
370         
371         for (Int_t jet_row = 0; jet_row < 12 - (jetSize - 1); jet_row++)
372         {
373                 UInt_t currentrow = word32[offset + jet_row];
374                 
375                 for (Int_t jet_col = 0; jet_col < 15; jet_col++)
376                 {
377                         if (currentrow & (1 << jet_col))
378                         {
379                                 fNL1JetPatch[i] = fNL1JetPatch[i] + 1;
380                                 
381                                 fL1JetPatchIndex[fNL1JetPatch[i] - 1][i] = ((jet_row << 8) & 0xFF00) | (jet_col & 0xFF);
382                         }
383                 }
384         }
385 }
386
387 //_____________________________________________________________________________
388 void AliEMCALTriggerSTURawStream::DecodeL1GammaPatchIndexes(const int i, UInt_t *word32, const int offset)
389 {
390         //////////////////////////////////////////////////////////
391         // index des L1 gamma                                   //
392         //////////////////////////////////////////////////////////
393         
394         unsigned short truL1indexes[32][8];
395         
396         // extraction from stream
397         for (Int_t index=0;index<8;index++)
398         {
399                 for (Int_t tru_num=0;tru_num<16;tru_num++)
400                 {
401                         truL1indexes[2*tru_num  ][index] = ( word32[offset + index * 16 + tru_num]        & 0xFFFF);
402                         truL1indexes[2*tru_num+1][index] = ((word32[offset + index * 16 + tru_num] >> 16) & 0xFFFF);
403                 }
404         }       
405         
406         // interpretation
407         int gammacolnum;
408         short indexcopy;
409         
410         for (Int_t tru_num=0;tru_num<32;tru_num++)
411         {
412                 for (Int_t index=0;index<8;index++)
413                 {
414                         for (Int_t bit_num=0; bit_num<12; bit_num++)
415                         {
416                                 if ((truL1indexes[tru_num][index] & (1<<bit_num)) != 0)
417                                 {
418                                         if (index<4) // Even
419                                         {
420                                                 gammacolnum = (2*bit_num  );
421                                                 indexcopy   = index;
422                                         }
423                                         else         // Odd
424                                         {
425                                                 gammacolnum = (2*bit_num+1);
426                                                 indexcopy   = index-4;
427                                         }                                               
428                                         
429                                         fNL1GammaPatch[i] = fNL1GammaPatch[i] + 1;
430                                         
431                                         fL1GammaPatchIndex[fNL1GammaPatch[i] - 1][i] = (((indexcopy << 10) & 0xC00) | ((gammacolnum << 5) & 0x3E0) | (tru_num & 0x1F));
432                                 }
433                         }
434                 }
435         }       
436 }
437
438 //_____________________________________________________________________________
439 void AliEMCALTriggerSTURawStream::DecodeTRUADC(UInt_t *word32, const int offset)
440 {
441         // extraction from stream
442         for (Int_t index=0;index<96;index++)
443         {
444                 for (Int_t tru_num=0;tru_num<16;tru_num++)
445                 {
446                         fADC[2*tru_num  ][index] = ( word32[offset + index * 16 + tru_num]        & 0xFFFF);
447                         fADC[2*tru_num+1][index] = ((word32[offset + index * 16 + tru_num] >> 16) & 0xFFFF);
448                 }
449         }       
450         
451         for (Int_t tru_num=16;tru_num<32;tru_num++) // A side
452         {
453                 Int_t v[96];
454                 for (Int_t index=0;index<96;index++) v[index] = fADC[tru_num][95-index];
455                 
456                 for (Int_t index=0;index<96;index++) fADC[tru_num][index] = v[index];
457         }
458 }
459
460
461 //_____________________________________________________________________________
462 Bool_t AliEMCALTriggerSTURawStream::GetL0GammaPatch(const Int_t i, Int_t& tru, Int_t& idx) const
463 {
464         // L0 gamma patches sent to STU (original access to L0 patch indexes)
465         
466         if (i > fNL0GammaPatch) return kFALSE;
467         
468         tru =  fL0GammaPatchIndex[i] & 0x1F;
469         idx = (fL0GammaPatchIndex[i] & 0x7E0) >> 5;
470         
471         return kTRUE;
472 }
473
474 //_____________________________________________________________________________
475 Bool_t AliEMCALTriggerSTURawStream::GetL1GammaPatch(const Int_t i, const Int_t j, Int_t& tru, Int_t& col, Int_t& row) const
476 {
477         // L1 gamma patch indexes
478         
479         if (j > 2 || i > fNL1GammaPatch[j]) return kFALSE;
480         
481         tru =  fL1GammaPatchIndex[i][j] & 0x1F;
482         col = (fL1GammaPatchIndex[i][j] & 0x3E0) >> 5;
483         row = (fL1GammaPatchIndex[i][j] & 0xC00) >> 10;
484         
485         return kTRUE;
486 }
487
488 //_____________________________________________________________________________
489 Bool_t AliEMCALTriggerSTURawStream::GetL1JetPatch(const Int_t i, const Int_t j, Int_t& col, Int_t& row) const
490 {
491         // L1 jet patch indexes
492         
493         if (j > 2 || i > fNL1JetPatch[j]) return kFALSE;
494         
495         col =  fL1JetPatchIndex[i][j] & 0xFF;
496         row = (fL1JetPatchIndex[i][j] & 0xFF00) >> 8;
497         
498         return kTRUE;
499 }
500
501 //_____________________________________________________________________________
502 void AliEMCALTriggerSTURawStream::GetADC(Int_t iTRU, UInt_t ADC[])
503 {
504         // Time sums
505         
506         for (Int_t i=0; i<96; i++) ADC[i] = fADC[iTRU][i];
507 }
508
509 //_____________________________________________________________________________
510 void AliEMCALTriggerSTURawStream::DumpPayLoad(const Option_t *option) const
511 {
512         // Dump STU payload
513         
514         TString op = option;
515         
516         printf("V0A:             %d\n", fV0A);
517         printf("V0C:             %d\n", fV0C);
518
519         printf("RawData:         %d\n", fGetRawData);
520         printf("RegionEnable:    %8x\n", fRegionEnable); 
521         printf("FrameReceived:   %8x\n", fFrameReceived);
522         printf("FwVersion:       %x\n", fFwVersion);
523         printf("Number of L0:    %d\n", fNL0GammaPatch);
524
525         for (int i = 0; i < 2; i++) {
526                 for (int j = 0; j < 3; j++) {
527                         printf("G[%d][%d]: %d\n", j, i, fG[j][i]);
528                         printf("J[%d][%d]: %d\n", j, i, fJ[j][i]);
529                 }
530                 
531                 printf("Gamma threshold[%d]: %d\n", i, fL1GammaThreshold[i]);
532                 printf("Jet Threshold[%d]:   %d\n", i, fL1JetThreshold[i]);
533
534                 printf("Number of L1-g[%d]: %d\n", i, fNL1GammaPatch[i]);
535                 printf("Number of L1-j[%d]: %d\n", i, fNL1JetPatch[i]);
536         }
537         
538         Int_t itru, col, row;
539         
540         if (op.Contains("L0") || op.Contains("ALL"))
541         {
542                 for (Int_t i = 0;i < fNL0GammaPatch; i++) {
543                         if (GetL0GammaPatch(i,itru,col)) 
544                                 cout << "> Found L0 gamma in TRU #" << setw(2) << itru <<  " at idx: " << setw(2) << col << endl;
545                 }
546         }
547         
548         if (op.Contains("L1") || op.Contains("ALL")) {
549                 for (int j = 0; j < 2; j++) {
550                         for (Int_t i = 0; i < fNL1GammaPatch[j]; i++) {
551                                 if (GetL1GammaPatch(i, j, itru, col, row)) 
552                                         cout << "> Found L1 gamma " << j << " in TRU #" << setw(2) << itru <<  " at: ( col: " << setw(2) << col << " , row: " << setw(2) << row << " )" << endl;
553                         }
554                         
555                         for (Int_t i = 0; i < fNL1JetPatch[j]; i++) {
556                                 if (GetL1JetPatch(i, j, col, row)) cout << "> Found L1 jet " << j << " at: ( col: " << setw(2) << col << " , row: " << setw(2) << row << " )" << endl;
557                         }
558                 }
559         }
560         
561         if ((op.Contains("ADC") || op.Contains("ALL")) && fGetRawData) {
562                 for (Int_t i = 0; i < 32; i++) {
563                         cout << "--------\n";
564                         cout << "TRU #" << setw(2) << i << ":";
565                         for (Int_t j = 0;j < 96; j++) { 
566                                 //TBits xadc(12); xadc.Set(12,&fADC[i][j]); 
567                                 if ((j % 4) == 0) cout << endl;
568                                 //cout << setw(2) << j << ": " << xadc << " ";
569                                 printf("%2d: %3x / ",j,fADC[i][j]); 
570                         }
571                         cout << "\n";
572                 }
573         }
574 }
575
576 //_____________________________________________________________________________
577 UShort_t AliEMCALTriggerSTURawStream::GetThreshold(Short_t A, Short_t B, Short_t C, UShort_t V0A, UShort_t V0C) const
578 {
579         // Get threshold 
580   ULong64_t v0sum = V0A + V0C;
581   
582   ULong64_t sqrV0 = v0sum * v0sum;                                      
583                                         
584   sqrV0 *= A;
585                                         
586   sqrV0 >>= 32;
587                                 
588   v0sum *= B;
589                                         
590   v0sum >>= 16;
591                                         
592   return (UShort_t)(sqrV0 + v0sum + C);
593 }