]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCDigitReaderPacked.cxx
Updated SSD QA class (PAnos)
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCDigitReaderPacked.cxx
1 // $Id$
2
3 //*************************************************************************
4 // This file is property of and copyright by the ALICE HLT Project        * 
5 // ALICE Experiment at CERN, All rights reserved.                         *
6 //                                                                        *
7 // Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
8 //                  Timm Steinbeck <timm@kip.uni-heidelberg.de>           *
9 //                  Jochen Thaeder <thaeder@kip.uni-heidelberg.de>        *
10 //                  for The ALICE HLT Project.                            *
11 //                                                                        *
12 // Permission to use, copy, modify and distribute this software and its   *
13 // documentation strictly for non-commercial purposes is hereby granted   *
14 // without fee, provided that the above copyright notice appears in all   *
15 // copies and that both the copyright notice and this permission notice   *
16 // appear in the supporting documentation. The authors make no claims     *
17 // about the suitability of this software for any purpose. It is          *
18 // provided "as is" without express or implied warranty.                  *
19 //*************************************************************************/
20
21 /** @file   AliHLTTPCDigitReaderPacked.cxx
22     @author Timm Steinbeck, Jochen Thaeder, Matthias Richter, Kenneth Aamodt
23     @date   
24     @brief  A digit reader implementation for simulated, packed TPC 'raw' data.
25 */
26
27 #if __GNUC__>= 3
28 using namespace std;
29 #endif
30
31 #include "AliHLTTPCDigitReaderPacked.h"
32
33 #include "AliTPCRawStream.h"
34 #include "AliRawReaderMemory.h"
35 #include "AliRawDataHeader.h"
36
37 //#if ENABLE_PAD_SORTING
38 #include "AliHLTTPCTransform.h"
39 //#endif // ENABLE_PAD_SORTING
40 #include "AliHLTStdIncludes.h"
41
42 ClassImp(AliHLTTPCDigitReaderPacked)
43
44 AliHLTTPCDigitReaderPacked::AliHLTTPCDigitReaderPacked()
45   :
46   fRawMemoryReader(NULL),
47   fTPCRawStream(NULL),
48   //#if ENABLE_PAD_SORTING
49   fCurrentRow(0),
50   fCurrentPad(0),
51   fCurrentBin(-1),
52   fRowOffset(0),
53   fNRows(0),
54   fNMaxRows(0),
55   fNMaxPads(0),
56   fNTimeBins(0),
57   fData(NULL),
58   //#endif // ENABLE_PAD_SORTING  
59   fUnsorted(kFALSE),
60   fDataBunch(),
61   fNextChannelFlag(kFALSE),
62   fCurrentPatch(0)
63 {
64   fRawMemoryReader = new AliRawReaderMemory;
65   
66   fTPCRawStream = new AliTPCRawStream( fRawMemoryReader );
67
68   //#if ENABLE_PAD_SORTING
69   // get max number of rows
70   for (Int_t ii=0; ii < 6; ii++)
71       if (AliHLTTPCTransform::GetNRows(ii) > fNMaxRows) 
72           fNMaxRows = AliHLTTPCTransform::GetNRows(ii);
73
74   // get max number of pads
75   for (Int_t ii=0; ii < AliHLTTPCTransform::GetNRows();ii++ )
76       if (AliHLTTPCTransform::GetNPads(ii) > fNMaxPads) 
77           fNMaxPads = AliHLTTPCTransform::GetNPads(ii);
78
79   // get max number of bins
80   fNTimeBins = AliHLTTPCTransform::GetNTimeBins();
81
82   HLTDebug("Array Borders  ||| MAXPAD=%d ||| MAXROW=%d ||| MAXBIN=%d ||| MAXMUL=%d", 
83            fNMaxPads, fNMaxRows, fNTimeBins, fNTimeBins*fNMaxRows*fNMaxPads);
84
85   // init Data array
86   fData = new Int_t[ fNMaxRows*fNMaxPads*fNTimeBins ];
87   //#endif // ENABLE_PAD_SORTING
88 }
89
90 AliHLTTPCDigitReaderPacked::~AliHLTTPCDigitReaderPacked(){
91   if ( fRawMemoryReader )
92     delete fRawMemoryReader;
93   fRawMemoryReader = NULL;
94   if ( fTPCRawStream )
95       delete fTPCRawStream;
96   fTPCRawStream = NULL;
97   //#if ENABLE_PAD_SORTING 
98   if ( fData )
99       delete [] fData;
100   fData = NULL;
101   //#endif // ENABLE_PAD_SORTING
102 }
103
104 Int_t AliHLTTPCDigitReaderPacked::InitBlock(void* ptr,ULong_t size, Int_t patch, Int_t slice)
105 {
106
107   fRawMemoryReader->SetMemory( reinterpret_cast<UChar_t*>( ptr ), size );
108
109   fCurrentPatch=patch;
110   
111   //get DDL ID in order to tell the memory reader which slice/patch to use
112   Int_t DDLid= 0;
113   if (patch < 2)
114     DDLid = 768 + 2*slice + patch;
115   else 
116     DDLid = 840 + 4*slice + patch-2;
117
118   fRawMemoryReader->SetEquipmentID(DDLid);
119   //fRawMemoryReader->SetEquipmentID(1);
120   fRawMemoryReader->RewindEvents();
121   fRawMemoryReader->NextEvent();
122
123   if(!fUnsorted){
124   //#if ENABLE_PAD_SORTING
125
126   fCurrentRow = 0;
127   fCurrentPad = 0;
128   fCurrentBin = -1;
129
130   Int_t firstrow=AliHLTTPCTransform::GetFirstRow(patch);
131   Int_t lastrow=AliHLTTPCTransform::GetLastRow(patch);
132   fNRows = lastrow - firstrow + 1;
133
134   Int_t offset=0;
135   if (patch > 1) offset =  AliHLTTPCTransform::GetFirstRow( 2 );
136
137   fRowOffset = firstrow - offset;
138   firstrow -= offset;
139   lastrow  -= offset;
140
141   // Init array with -1
142   memset( fData, 0xFF, sizeof(Int_t)*(fNMaxRows*fNMaxPads*fNTimeBins) );
143
144   // read data and fill in array
145   while( fTPCRawStream->Next()){
146
147       Int_t row = fTPCRawStream->GetRow();
148       Int_t pad = fTPCRawStream->GetPad();
149       Int_t bin = fTPCRawStream->GetTime();
150
151       if ( row < firstrow || row > lastrow || pad > AliHLTTPCTransform::GetNPads(row + offset) || bin > fNTimeBins){
152         HLTFatal("Index out of Range Probably wrong patch! %d - %d", slice, patch);
153         if ( row < firstrow || row > lastrow ) 
154           HLTFatal("Row out of Range %d < %d < %d",firstrow, row, lastrow);
155         if ( pad > AliHLTTPCTransform::GetNPads(row + offset) ) 
156           HLTFatal("Pad out of Range %d < %d < %d",pad, AliHLTTPCTransform::GetNPads(row + offset));
157         if ( bin > fNTimeBins )
158           HLTFatal("Bin out of Range %d < %d < %d",bin, fNTimeBins);
159       }
160       else {  
161           if ((row-fRowOffset)*fNMaxPads*fNTimeBins+ pad*fNTimeBins + bin >=  fNMaxRows*fNMaxPads*fNTimeBins ) {
162               HLTFatal("Index out of array range PAD=%d ||| ROW=%d ||| BIN=%d ||| OFFSET=%d ||| ROWOFFSET=%d", pad, row, bin, offset, fRowOffset);
163               continue;
164           }
165           else {
166               fData[ (row-fRowOffset)*fNMaxPads*fNTimeBins+ pad*fNTimeBins + bin ] = fTPCRawStream->GetSignal() ;
167           }
168       }
169   }
170   //#endif // ENABLE_PAD_SORTING
171   }
172   return 0;
173 }
174
175 Bool_t AliHLTTPCDigitReaderPacked::NextSignal(){
176   Bool_t readvalue = kTRUE;
177
178   if(!fUnsorted){//added for test
179     //#if ENABLE_PAD_SORTING
180     while (1) {
181       fCurrentBin++;
182       if (fCurrentBin >= fNTimeBins){
183           fCurrentBin = 0;
184           fCurrentPad++;
185      
186           if (fCurrentPad >=fNMaxPads){
187               fCurrentPad = 0;
188               fCurrentRow++;
189               
190               if (fCurrentRow >= fNMaxRows){
191                   readvalue = kFALSE;
192                   break;
193               }
194           }
195       }
196
197       if (fCurrentRow*fNMaxPads*fNTimeBins+ fCurrentPad*fNTimeBins + fCurrentBin >=  fNMaxRows*fNMaxPads*fNTimeBins ) {
198           HLTFatal("Overflow: row=%d pad=%d bin=%d", fCurrentRow, fCurrentPad, fCurrentBin);
199           readvalue = kFALSE;
200           break;
201       }
202
203       if (fData[ fCurrentRow*fNMaxPads*fNTimeBins + fCurrentPad*fNTimeBins + fCurrentBin  ] != -1) break;
204     }
205   }// added for test
206   else{//added for test
207     //#else // !ENABLE_PAD_SORTING
208     readvalue = fTPCRawStream->Next();
209   }//added for test
210   //#endif // ENABLE_PAD_SORTING
211
212   return readvalue;
213 }
214
215 Int_t AliHLTTPCDigitReaderPacked::GetRow(){
216   /*#if ENABLE_PAD_SORTING
217   return (fCurrentRow + fRowOffset);
218 #else // !ENABLE_PAD_SORTING
219   return (Int_t) fTPCRawStream->GetRow();
220 #endif // ENABLE_PAD_SORTING
221   */
222   if(!fUnsorted){
223   return (fCurrentRow + fRowOffset);
224   }
225   else{
226     if(fCurrentPatch>1){
227       return (Int_t) fTPCRawStream->GetRow()-AliHLTTPCTransform::GetFirstRow(fCurrentPatch)+AliHLTTPCTransform::GetFirstRow(2);
228     }
229     else{
230       return (Int_t) fTPCRawStream->GetRow()-AliHLTTPCTransform::GetFirstRow(fCurrentPatch);
231     }
232   }
233 }
234
235 int AliHLTTPCDigitReaderPacked::GetPad(){
236   /*#if ENABLE_PAD_SORTING
237     return fCurrentPad;
238     #else // !ENABLE_PAD_SORTING
239     return fTPCRawStream->GetPad();
240     #endif // ENABLE_PAD_SORTING
241   */
242   if(!fUnsorted){
243     return fCurrentPad;
244   }
245   else{
246     return fTPCRawStream->GetPad();
247   }
248 }
249
250 AliHLTUInt32_t AliHLTTPCDigitReaderPacked::GetAltroBlockHWaddr() const
251 {
252   return fTPCRawStream->GetHWAddress();
253 }
254
255 Int_t AliHLTTPCDigitReaderPacked::GetSignal(){ 
256   /*
257     #if ENABLE_PAD_SORTING
258     return fData[ fCurrentRow*fNMaxPads*fNTimeBins+ fCurrentPad*fNTimeBins + fCurrentBin ];
259     #else // !ENABLE_PAD_SORTING
260     return fTPCRawStream->GetSignal();
261     #endif // ENABLE_PAD_SORTING
262   */
263   if(!fUnsorted){
264     return fData[ fCurrentRow*fNMaxPads*fNTimeBins+ fCurrentPad*fNTimeBins + fCurrentBin ];
265   }
266   else{
267     return fTPCRawStream->GetSignal();
268   }
269 }
270
271 Int_t AliHLTTPCDigitReaderPacked::GetTime(){
272   /*
273     #if ENABLE_PAD_SORTING
274     return fCurrentBin;
275     #else // !ENABLE_PAD_SORTING
276     return fTPCRawStream->GetTime();
277     #endif // ENABLE_PAD_SORTING
278   */
279   if(!fUnsorted){
280     return fCurrentBin;
281   }
282   else{
283     if((Int_t)(fTPCRawStream->GetTime()-fDataBunch.size()+1)>0 &&(Int_t)(fTPCRawStream->GetTime()-fDataBunch.size()+1)<=AliHLTTPCTransform::GetNTimeBins()){
284       return fTPCRawStream->GetTime()-fDataBunch.size()+1;
285     }
286     else{
287       HLTDebug("Timebin is out of range: %d",fTPCRawStream->GetTime()-fDataBunch.size()+1);
288       return 0;
289     }
290   }
291 }
292
293 Int_t AliHLTTPCDigitReaderPacked::GetTimeOfUnsortedSignal(){
294   return fTPCRawStream->GetTime();
295 }
296
297 bool AliHLTTPCDigitReaderPacked::NextChannel(){
298   bool iResult=false;
299   if(fNextChannelFlag==kFALSE){
300     if(!NextSignal()){//if there are no more signals
301       iResult=false;
302     }
303     else{
304       iResult=true;
305     }
306   }
307   else{
308     iResult=true;
309   }
310   return iResult;
311 }
312
313 int AliHLTTPCDigitReaderPacked::NextBunch(){
314   
315   fDataBunch.clear();
316   //adding the first signal (will always be the leftover from either NextChannel call or Previous bunch)
317   fDataBunch.push_back(GetSignal());
318
319   int iResult=1;
320   Bool_t continueLoop=kTRUE;
321   AliHLTUInt32_t prevHWAddress=GetAltroBlockHWaddr();
322   Int_t prevTime=GetTimeOfUnsortedSignal();
323   do{
324     if(NextSignal()){
325       if(GetAltroBlockHWaddr()==prevHWAddress){//check if there is a change in channel(new row and pad)
326         if(prevTime==GetTimeOfUnsortedSignal()+1){//if true means that we have consecutive signals
327           prevTime=GetTimeOfUnsortedSignal();
328           fDataBunch.push_back(GetSignal());
329         }
330         else{//end of bunch but not of channel
331           continueLoop=kFALSE;
332         }
333       }
334       else{
335         iResult=0;//end of bunch
336         continueLoop=kFALSE;
337         fNextChannelFlag=kTRUE;
338       }
339     }
340     else{
341       continueLoop=kFALSE;
342       fNextChannelFlag=kFALSE;
343       if(fDataBunch.size()>0){//we reached end of data in total, but we still have a bunch
344         iResult = 0;
345       }
346     }
347   }while(continueLoop);
348
349   return iResult;
350
351 }
352
353 int AliHLTTPCDigitReaderPacked::GetBunchSize(){
354   return fDataBunch.size();
355 }
356
357 const UInt_t* AliHLTTPCDigitReaderPacked::GetSignals()
358 {
359   // see header file for class documentation
360   return &fDataBunch[0];
361 }