]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCDigitReaderPacked.cxx
robustness
[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 #if defined(HAVE_ALIRAWDATA) && defined(HAVE_ALITPCRAWSTREAM_H)
45 AliHLTTPCDigitReaderPacked::AliHLTTPCDigitReaderPacked()
46   :
47   //#if ENABLE_PAD_SORTING
48   fCurrentRow(0),
49   fCurrentPad(0),
50   fCurrentBin(-1),
51   fNRows(0),
52   fRowOffset(0),
53   fNMaxRows(0),
54   fNMaxPads(0),
55   fNTimeBins(0),
56   fData(NULL),
57   //#endif // ENABLE_PAD_SORTING  
58   fRawMemoryReader(NULL),
59   fTPCRawStream(NULL),
60   fOldRCUFormat(kFALSE),
61   fUnsorted(kFALSE)
62 {
63   fRawMemoryReader = new AliRawReaderMemory;
64   
65   fTPCRawStream = new AliTPCRawStream( fRawMemoryReader );
66
67   //#if ENABLE_PAD_SORTING
68   // get max number of rows
69   for (Int_t ii=0; ii < 6; ii++)
70       if (AliHLTTPCTransform::GetNRows(ii) > fNMaxRows) 
71           fNMaxRows = AliHLTTPCTransform::GetNRows(ii);
72
73   // get max number of pads
74   for (Int_t ii=0; ii < AliHLTTPCTransform::GetNRows();ii++ )
75       if (AliHLTTPCTransform::GetNPads(ii) > fNMaxPads) 
76           fNMaxPads = AliHLTTPCTransform::GetNPads(ii);
77
78   // get max number of bins
79   fNTimeBins = AliHLTTPCTransform::GetNTimeBins();
80
81   HLTDebug("Array Borders  ||| MAXPAD=%d ||| MAXROW=%d ||| MAXBIN=%d ||| MAXMUL=%d", 
82            fNMaxPads, fNMaxRows, fNTimeBins, fNTimeBins*fNMaxRows*fNMaxPads);
83
84   // init Data array
85   fData = new Int_t[ fNMaxRows*fNMaxPads*fNTimeBins ];
86   //#endif // ENABLE_PAD_SORTING
87 }
88
89 AliHLTTPCDigitReaderPacked::~AliHLTTPCDigitReaderPacked(){
90   if ( fRawMemoryReader )
91     delete fRawMemoryReader;
92   fRawMemoryReader = NULL;
93   if ( fTPCRawStream )
94       delete fTPCRawStream;
95   fTPCRawStream = NULL;
96   //#if ENABLE_PAD_SORTING 
97   if ( fData )
98       delete [] fData;
99   fData = NULL;
100   //#endif // ENABLE_PAD_SORTING
101 }
102
103 Int_t AliHLTTPCDigitReaderPacked::InitBlock(void* ptr,unsigned long size, Int_t patch, Int_t slice){
104
105   fRawMemoryReader->SetMemory( reinterpret_cast<UChar_t*>( ptr ), size );
106
107   //get DDL ID in order to tell the memory reader which slice/patch to use
108   Int_t DDLid= 0;
109   if (patch < 2)
110     DDLid = 768 + 2*slice + patch;
111   else 
112     DDLid = 840 + 4*slice + patch-2;
113
114   fRawMemoryReader->SetEquipmentID(DDLid);
115   //fRawMemoryReader->SetEquipmentID(1);
116   if(fOldRCUFormat)
117     fTPCRawStream->SetOldRCUFormat(kTRUE);
118
119   if(!fUnsorted){
120   //#if ENABLE_PAD_SORTING
121
122   fCurrentRow = 0;
123   fCurrentPad = 0;
124   fCurrentBin = -1;
125
126   Int_t firstrow=AliHLTTPCTransform::GetFirstRow(patch);
127   Int_t lastrow=AliHLTTPCTransform::GetLastRow(patch);
128   fNRows = lastrow - firstrow + 1;
129
130   Int_t offset=0;
131   if (patch > 1) offset =  AliHLTTPCTransform::GetFirstRow( 2 );
132
133   fRowOffset = firstrow - offset;
134   firstrow -= offset;
135   lastrow  -= offset;
136
137   // Init array with -1
138   memset( fData, 0xFF, sizeof(Int_t)*(fNMaxRows*fNMaxPads*fNTimeBins) );
139
140   // read data and fill in array
141   while( fTPCRawStream->Next()){
142
143       Int_t row = fTPCRawStream->GetRow();
144       Int_t pad = fTPCRawStream->GetPad();
145       Int_t bin = fTPCRawStream->GetTime();
146
147       if ( row < firstrow || row > lastrow || pad > AliHLTTPCTransform::GetNPads(row + offset) || bin > fNTimeBins){
148         HLTFatal("Index out of Range Probably wrong patch! %d - %d", slice, patch);
149         if ( row < firstrow || row > lastrow ) 
150           HLTFatal("Row out of Range %d < %d < %d",firstrow, row, lastrow);
151         if ( pad > AliHLTTPCTransform::GetNPads(row + offset) ) 
152           HLTFatal("Pad out of Range %d < %d < %d",pad, AliHLTTPCTransform::GetNPads(row + offset));
153         if ( bin > fNTimeBins )
154           HLTFatal("Bin out of Range %d < %d < %d",bin, fNTimeBins);
155       }
156       else {  
157           if ((row-fRowOffset)*fNMaxPads*fNTimeBins+ pad*fNTimeBins + bin >=  fNMaxRows*fNMaxPads*fNTimeBins ) {
158               HLTFatal("Index out of array range PAD=%d ||| ROW=%d ||| BIN=%d ||| OFFSET=%d ||| ROWOFFSET=%d", pad, row, bin, offset, fRowOffset);
159               continue;
160           }
161           else {
162               fData[ (row-fRowOffset)*fNMaxPads*fNTimeBins+ pad*fNTimeBins + bin ] = fTPCRawStream->GetSignal() ;
163           }
164       }
165   }
166   //#endif // ENABLE_PAD_SORTING
167   }
168   return 0;
169 }
170
171 Bool_t AliHLTTPCDigitReaderPacked::NextSignal(){
172   Bool_t readvalue = kTRUE;
173
174   if(!fUnsorted){//added for test
175     //#if ENABLE_PAD_SORTING
176     while (1) {
177       fCurrentBin++;
178       if (fCurrentBin >= fNTimeBins){
179           fCurrentBin = 0;
180           fCurrentPad++;
181      
182           if (fCurrentPad >=fNMaxPads){
183               fCurrentPad = 0;
184               fCurrentRow++;
185               
186               if (fCurrentRow >= fNMaxRows){
187                   readvalue = kFALSE;
188                   break;
189               }
190           }
191       }
192
193       if (fCurrentRow*fNMaxPads*fNTimeBins+ fCurrentPad*fNTimeBins + fCurrentBin >=  fNMaxRows*fNMaxPads*fNTimeBins ) {
194           HLTFatal("Overflow: row=%d pad=%d bin=%d", fCurrentRow, fCurrentPad, fCurrentBin);
195           readvalue = kFALSE;
196           break;
197       }
198
199       if (fData[ fCurrentRow*fNMaxPads*fNTimeBins + fCurrentPad*fNTimeBins + fCurrentBin  ] != -1) break;
200     }
201   }// added for test
202   else{//added for test
203     //#else // !ENABLE_PAD_SORTING
204     readvalue = fTPCRawStream->Next();
205   }//added for test
206   //#endif // ENABLE_PAD_SORTING
207
208   return readvalue;
209 }
210
211 Int_t AliHLTTPCDigitReaderPacked::GetRow(){
212   /*#if ENABLE_PAD_SORTING
213   return (fCurrentRow + fRowOffset);
214 #else // !ENABLE_PAD_SORTING
215   return (Int_t) fTPCRawStream->GetRow();
216 #endif // ENABLE_PAD_SORTING
217   */
218   if(!fUnsorted){
219   return (fCurrentRow + fRowOffset);
220   }
221   else{
222   return (Int_t) fTPCRawStream->GetRow();
223   }
224 }
225
226 int AliHLTTPCDigitReaderPacked::GetPad(){
227   /*#if ENABLE_PAD_SORTING
228     return fCurrentPad;
229     #else // !ENABLE_PAD_SORTING
230     return fTPCRawStream->GetPad();
231     #endif // ENABLE_PAD_SORTING
232   */
233   if(!fUnsorted){
234     return fCurrentPad;
235   }
236   else{
237     return fTPCRawStream->GetPad();
238   }
239 }
240
241 Int_t AliHLTTPCDigitReaderPacked::GetSignal(){ 
242   /*
243     #if ENABLE_PAD_SORTING
244     return fData[ fCurrentRow*fNMaxPads*fNTimeBins+ fCurrentPad*fNTimeBins + fCurrentBin ];
245     #else // !ENABLE_PAD_SORTING
246     return fTPCRawStream->GetSignal();
247     #endif // ENABLE_PAD_SORTING
248   */
249   if(!fUnsorted){
250     return fData[ fCurrentRow*fNMaxPads*fNTimeBins+ fCurrentPad*fNTimeBins + fCurrentBin ];
251   }
252   else{
253     return fTPCRawStream->GetSignal();
254   }
255 }
256
257 Int_t AliHLTTPCDigitReaderPacked::GetTime(){
258   /*
259     #if ENABLE_PAD_SORTING
260     return fCurrentBin;
261     #else // !ENABLE_PAD_SORTING
262     return fTPCRawStream->GetTime();
263     #endif // ENABLE_PAD_SORTING
264   */
265   if(!fUnsorted){
266     return fCurrentBin;
267   }
268   else{
269     return fTPCRawStream->GetTime();
270   }
271 }
272 #endif //defined(HAVE_ALIRAWDATA) && defined(HAVE_ALITPCRAWSTREAM_H)