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