a76d1e8f9274ca90044484587875a28a358e3a6b
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTriggerSTURawStream.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 This class provides access to STU DDL raw data.
21 Author: R. GUERNANE LPSC Grenoble CNRS/IN2P3
22 */
23
24 #include "AliEMCALTriggerSTURawStream.h"
25 #include "AliRawReader.h"
26 #include "AliLog.h"
27
28 #include "Riostream.h"
29 #include "TBits.h"
30
31 #include <cstdlib>
32
33 namespace
34 {
35         const Int_t kPayLoadSize = 944;
36 }
37
38 ClassImp(AliEMCALTriggerSTURawStream)
39
40 //_____________________________________________________________________________
41 AliEMCALTriggerSTURawStream::AliEMCALTriggerSTURawStream() : TObject(),
42 fRawReader(0x0),
43 fL1JetThreshold(0),
44 fL1GammaThreshold(0),
45 fL0GammaPatchIndex(0x0),
46 fL1GammaPatchIndex(0x0),
47 fL1JetPatchIndex(0x0),
48 fNL0GammaPatch(0),
49 fNL1JetPatch(0),
50 fNL1GammaPatch(0),
51 fGetRawData(0),
52 fL0(0)
53 {
54         //
55 }
56
57 //_____________________________________________________________________________
58 AliEMCALTriggerSTURawStream::AliEMCALTriggerSTURawStream(AliRawReader* rawReader) : TObject(),
59 fRawReader(rawReader),
60 fL1JetThreshold(0),
61 fL1GammaThreshold(0),
62 fL0GammaPatchIndex(0x0),
63 fL1GammaPatchIndex(0x0),
64 fL1JetPatchIndex(0x0),
65 fNL0GammaPatch(0),
66 fNL1JetPatch(0),
67 fNL1GammaPatch(0),  
68 fGetRawData(0),                                                                
69 fL0(0)
70 {
71         //
72         fRawReader->Reset();
73         fRawReader->Select("EMCAL",44);
74 }
75
76 //_____________________________________________________________________________
77 AliEMCALTriggerSTURawStream::~AliEMCALTriggerSTURawStream()
78 {
79         // destructor
80 }
81
82 //_____________________________________________________________________________
83 void AliEMCALTriggerSTURawStream::Reset()
84 {
85         //
86         if (fRawReader) fRawReader->Reset();
87
88         fNL0GammaPatch = 0;
89         fNL1GammaPatch = 0;
90         fNL1JetPatch   = 0;     
91         
92         delete fL0GammaPatchIndex; fL0GammaPatchIndex = 0x0;
93         delete fL1GammaPatchIndex; fL1GammaPatchIndex = 0x0;
94         delete fL1JetPatchIndex;   fL1JetPatchIndex   = 0x0;    
95 }
96
97 //_____________________________________________________________________________
98 Bool_t AliEMCALTriggerSTURawStream::ReadPayLoad()
99 {
100         // STU data decoder from Olivier Bourrion LPSC CNRS-IN2P3
101         // bourrion@lpsc.in2p3.fr
102         
103         UInt_t word32[1772]; // 32b words
104
105         Int_t iword = 0;
106         
107         fNL0GammaPatch = 0;
108         fNL1GammaPatch = 0;
109         fNL1JetPatch   = 0;
110         
111         delete fL0GammaPatchIndex; fL0GammaPatchIndex = 0x0;
112         delete fL1GammaPatchIndex; fL1GammaPatchIndex = 0x0;
113         delete fL1JetPatchIndex;   fL1JetPatchIndex   = 0x0;
114         
115         UInt_t w32;
116         while (fRawReader->ReadNextInt(w32)) word32[iword++] = w32;
117         
118         if (iword < kPayLoadSize) 
119         {
120                 AliError(Form("STU raw data size is too small: %d word32 only!", iword));
121                 return kFALSE;
122         } 
123         else if (iword > kPayLoadSize )
124         {
125                 AliLog::Message(AliLog::kInfo, "TRU raw data in the STU payload enabled","EMCAL","AliEMCALTriggerSTURawStream","ReadPayLoad()","AliEMCALTriggerSTURawStream.cxx",104);
126         }
127
128         fL0 = 0;
129         
130           fL1JetThreshold = ((word32[0]>>16) & 0xFFF);
131         fL1GammaThreshold =  (word32[0]      & 0xFFF);
132         
133         for (Int_t jet_row=0; jet_row<11; jet_row++)
134         {
135                 UInt_t currentrow = word32[1+jet_row];
136                 
137                 for (Int_t jet_col=0; jet_col<15; jet_col++)
138                 {
139                         if (currentrow & (1 << jet_col))
140                         {
141                                 fNL1JetPatch++;
142                                 fL1JetPatchIndex = (UShort_t*)realloc(fL1JetPatchIndex, fNL1JetPatch * sizeof(UShort_t));
143                                 if (fL1JetPatchIndex == NULL) {AliError("Error (re)allocating L1 jet patch memory");}
144                                 
145                                 fL1JetPatchIndex[fNL1JetPatch-1] = ((jet_row << 8) & 0xFF00) | (jet_col & 0xFF);
146                         }
147                 }
148         }
149         
150         //////////////////////////////////////////////////////////
151         // index des L0                                         //
152         //////////////////////////////////////////////////////////
153         // FIXME: still not interpreted to be done with Jiri
154         
155         unsigned short TRU_L0_indexes[32][6];
156         
157         // extraction from stream
158         for (Int_t index=0;index<6;index++)
159         {
160                 for (Int_t tru_num=0;tru_num<16;tru_num++)
161                 {
162                         TRU_L0_indexes[2*tru_num  ][index] = ( word32[12+index*16+tru_num]      & 0xFFFF);
163                         TRU_L0_indexes[2*tru_num+1][index] = ((word32[12+index*16+tru_num]>>16) & 0xFFFF);
164                 }
165         }
166
167         for (Int_t tru_num=0;tru_num<32;tru_num++) 
168         {
169                 for (Int_t index=0;index<6;index++) 
170                 {
171                         for (Int_t bit_num=0;bit_num<12;bit_num++)
172                         {
173                                 if ((TRU_L0_indexes[tru_num][index] & (1 << bit_num)))
174                                 {
175                                         fL0 = 1;
176
177                                         Int_t idx = 12 * index + bit_num;
178                                         
179                                         Int_t col = idx / 3;
180                                         Int_t row = idx % 3;
181                                         
182                                         fNL0GammaPatch++;
183                                         fL0GammaPatchIndex = (UShort_t*)realloc(fL0GammaPatchIndex, fNL0GammaPatch * sizeof(UShort_t));
184                                         
185                                         if (fL0GammaPatchIndex == NULL) {AliError("Error (re)allocating L0 gamma patch memory");}
186                                         
187                                         fL0GammaPatchIndex[fNL0GammaPatch-1] = (((row << 10) & 0xC00) | ((col << 5) & 0x3E0) | (tru_num & 0x1F));
188                                 }
189                         }
190                 }
191         }
192
193         //////////////////////////////////////////////////////////
194         // index des L1 gamma                                   //
195         //////////////////////////////////////////////////////////
196         
197         unsigned short TRU_L1_indexes[32][8];
198         
199         // extraction from stream
200         for (Int_t index=0;index<8;index++)
201         {
202                 for (Int_t tru_num=0;tru_num<16;tru_num++)
203                 {
204                         TRU_L1_indexes[2*tru_num  ][index] = ( word32[108+index*16+tru_num]      & 0xFFFF);
205                         TRU_L1_indexes[2*tru_num+1][index] = ((word32[108+index*16+tru_num]>>16) & 0xFFFF);
206                 }
207         }       
208
209         // interpretation
210         int gammacolnum;
211         short indexcopy;
212         
213         for (Int_t tru_num=0;tru_num<32;tru_num++)
214         {
215                 for (Int_t index=0;index<8;index++)
216                 {
217                         for (Int_t bit_num=0; bit_num<12; bit_num++)
218                         {
219                                 if ((TRU_L1_indexes[tru_num][index] & (1<<bit_num)) != 0)
220                                 {
221                                         if (index<4) // Even
222                                         {
223                                                 gammacolnum = (2*bit_num  );
224                                                 indexcopy   = index;
225                                         }
226                                         else         // Odd
227                                         {
228                                                 gammacolnum = (2*bit_num+1);
229                                                 indexcopy   = index-4;
230                                         }                                               
231                                         
232                                         fNL1GammaPatch++;
233                                         fL1GammaPatchIndex = (UShort_t*)realloc(fL1GammaPatchIndex, fNL1GammaPatch * sizeof(UShort_t));
234
235                                         if (fL1GammaPatchIndex == NULL) {AliError("Error (re)allocating L1 gamma patch memory");}
236                                         
237                                         fL1GammaPatchIndex[fNL1GammaPatch-1] = (((indexcopy << 10) & 0xC00) | ((gammacolnum << 5) & 0x3E0) | (tru_num & 0x1F));
238                                 }
239                         }
240                 }
241         }       
242
243         //////////////////////////////////////////////////////////
244         // raw output                                           //
245         //////////////////////////////////////////////////////////
246         
247         if ( iword <= kPayLoadSize ) 
248         {
249                 fGetRawData = 0;
250                 return kFALSE;
251         }
252         
253         fGetRawData = 1;
254         
255         // extraction from stream
256         for (Int_t index=0;index<96;index++)
257         {
258                 for (Int_t tru_num=0;tru_num<16;tru_num++)
259                 {
260                         fADC[2*tru_num  ][index] = ( word32[236+index*16+tru_num]      & 0xFFFF);
261                         fADC[2*tru_num+1][index] = ((word32[236+index*16+tru_num]>>16) & 0xFFFF);
262                 }
263         }       
264
265         for (Int_t tru_num=16;tru_num<32;tru_num++) // A side
266         {
267                 for (Int_t index=0;index<96;index++)
268                 {
269                         fADC[tru_num][index] = fADC[tru_num][95-index];
270                 }
271         }
272         
273         return kFALSE;
274 }
275
276 //_____________________________________________________________________________
277 Bool_t AliEMCALTriggerSTURawStream::GetL0GammaPatch(const Int_t i, Int_t& tru, Int_t& col, Int_t& row) const
278 {
279         //
280         if (i > fNL0GammaPatch) return kFALSE;
281         
282         tru =  fL0GammaPatchIndex[i] & 0x1F;
283         col = (fL0GammaPatchIndex[i] & 0x3E0) >> 5;
284         row = (fL0GammaPatchIndex[i] & 0xC00) >> 10;
285         
286         return kTRUE;
287 }
288
289 //_____________________________________________________________________________
290 Bool_t AliEMCALTriggerSTURawStream::GetL1GammaPatch(const Int_t i, Int_t& tru, Int_t& col, Int_t& row) const
291 {
292         //
293         if (i > fNL1GammaPatch) return kFALSE;
294         
295         tru =  fL1GammaPatchIndex[i] & 0x1F;
296         col = (fL1GammaPatchIndex[i] & 0x3E0) >> 5;
297         row = (fL1GammaPatchIndex[i] & 0xC00) >> 10;
298         
299         return kTRUE;
300 }
301
302 //_____________________________________________________________________________
303 Bool_t AliEMCALTriggerSTURawStream::GetL1JetPatch(const Int_t i, Int_t& col, Int_t& row) const
304 {
305         //
306         if (i > fNL1JetPatch) return kFALSE;
307         
308         col =  fL1JetPatchIndex[i] & 0xFF;
309         row = (fL1JetPatchIndex[i] & 0xFF00) >> 8;
310         
311         return kTRUE;
312 }
313
314 //_____________________________________________________________________________
315 void AliEMCALTriggerSTURawStream::GetADC(Int_t iTRU, UInt_t ADC[])
316 {
317         //
318         for (Int_t i=0; i<96; i++) ADC[i] = fADC[iTRU][i];
319 }
320
321 //_____________________________________________________________________________
322 void AliEMCALTriggerSTURawStream::DumpPayLoad(const Option_t *option) const
323 {
324         //
325         TString op = option;
326         
327         cout << "Jet Threshold: " << fL1JetThreshold << " Gamma threshold: " << fL1GammaThreshold << endl;
328         
329         Int_t itru, col, row;
330
331         Bool_t isOK;
332         
333         if (op.Contains("L0") || op.Contains("ALL"))
334         {
335                 for (Int_t i=0;i<fNL0GammaPatch;i++)
336                 {
337                         isOK = GetL0GammaPatch(i,itru,col,row);
338                         if (isOK) cout << "> Found L0 gamma in TRU #" << setw(2) << itru
339                                                 <<  " at: ( col: " << setw(2) << col << " , row: " << setw(2) << row << " )" << endl;
340                 }
341         }
342         
343         if (op.Contains("L1") || op.Contains("ALL"))
344         {
345                 for (Int_t i=0;i<fNL1GammaPatch;i++)
346                 {
347                         isOK = GetL1GammaPatch(i,itru,col,row);
348                         if (isOK) cout << "> Found L1 gamma in TRU #" << setw(2) << itru
349                                                 <<  " at: ( col: " << setw(2) << col << " , row: " << setw(2) << row << " )" << endl;
350                 }
351
352                 for (Int_t i=0;i<fNL1JetPatch;i++)
353                 {
354                         isOK = GetL1JetPatch(i,col,row);
355                         if (isOK) cout << "> Found L1 jet at: ( col: " << setw(2) << col << " , row: " << setw(2) << row << " )" << endl;
356                 }
357         }
358         
359         cout << "> RawData: " << fGetRawData << endl;
360         
361         if ( (op.Contains("ADC") || op.Contains("ALL")) && fGetRawData )
362         {
363                 for (Int_t i=0;i<32;i++)
364                 {
365                         cout << "--------\n";
366                         cout << "TRU #" << setw(2) << i << ":";
367                         for (Int_t j=0;j<96;j++) 
368                         { 
369                                 TBits xadc(12); xadc.Set(12,&fADC[i][j]); 
370                                 if ((j%4)==0) cout << endl;
371                                 //cout << setw(2) << j << ": " << xadc << " ";
372                                 printf("%2d: %3x / ",j,fADC[i][j]); 
373                         }
374                         cout << "\n";
375                 }
376         }
377 }