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