]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCDigitReaderPacked.cxx
enhanced interface of DigitReaders (channel locking, generic sorting)
[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 {
62   fRawMemoryReader = new AliRawReaderMemory;
63   
64   fTPCRawStream = new AliTPCRawStream( fRawMemoryReader );
65
66 #if ENABLE_PAD_SORTING
67   // get max number of rows
68   for (Int_t ii=0; ii < 6; ii++)
69       if (AliHLTTPCTransform::GetNRows(ii) > fNMaxRows) 
70           fNMaxRows = AliHLTTPCTransform::GetNRows(ii);
71
72   // get max number of pads
73   for (Int_t ii=0; ii < AliHLTTPCTransform::GetNRows();ii++ )
74       if (AliHLTTPCTransform::GetNPads(ii) > fNMaxPads) 
75           fNMaxPads = AliHLTTPCTransform::GetNPads(ii);
76
77   // get max number of bins
78   fNTimeBins = AliHLTTPCTransform::GetNTimeBins();
79
80   HLTDebug("Array Borders  ||| MAXPAD=%d ||| MAXROW=%d ||| MAXBIN=%d ||| MAXMUL=%d", 
81            fNMaxPads, fNMaxRows, fNTimeBins, fNTimeBins*fNMaxRows*fNMaxPads);
82
83   // init Data array
84   fData = new Int_t[ fNMaxRows*fNMaxPads*fNTimeBins ];
85 #endif // ENABLE_PAD_SORTING
86 }
87
88 AliHLTTPCDigitReaderPacked::~AliHLTTPCDigitReaderPacked(){
89   if ( fRawMemoryReader )
90     delete fRawMemoryReader;
91   fRawMemoryReader = NULL;
92   if ( fTPCRawStream )
93       delete fTPCRawStream;
94   fTPCRawStream = NULL;
95 #if ENABLE_PAD_SORTING 
96   if ( fData )
97       delete [] fData;
98   fData = NULL;
99 #endif // ENABLE_PAD_SORTING
100 }
101
102 Int_t AliHLTTPCDigitReaderPacked::InitBlock(void* ptr,unsigned long size, Int_t patch, Int_t slice){
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   if(fOldRCUFormat)
116     fTPCRawStream->SetOldRCUFormat(kTRUE);
117
118 #if ENABLE_PAD_SORTING
119
120   fCurrentRow = 0;
121   fCurrentPad = 0;
122   fCurrentBin = -1;
123
124   Int_t firstrow=AliHLTTPCTransform::GetFirstRow(patch);
125   Int_t lastrow=AliHLTTPCTransform::GetLastRow(patch);
126   fNRows = lastrow - firstrow + 1;
127
128   Int_t offset=0;
129   if (patch > 1) offset =  AliHLTTPCTransform::GetFirstRow( 2 );
130
131   fRowOffset = firstrow - offset;
132   firstrow -= offset;
133   lastrow  -= offset;
134
135   // Init array with -1
136   memset( fData, 0xFF, sizeof(Int_t)*(fNMaxRows*fNMaxPads*fNTimeBins) );
137
138   // read data and fill in array
139   while( fTPCRawStream->Next()){
140
141       Int_t row = fTPCRawStream->GetRow();
142       Int_t pad = fTPCRawStream->GetPad();
143       Int_t bin = fTPCRawStream->GetTime();
144
145       if ( row < firstrow || row > lastrow || pad > AliHLTTPCTransform::GetNPads(row + offset) || bin > fNTimeBins){
146         HLTFatal("Index out of Range Probably wrong patch! %d - %d", slice, patch);
147         if ( row < firstrow || row > lastrow ) 
148           HLTFatal("Row out of Range %d < %d < %d",firstrow, row, lastrow);
149         if ( pad > AliHLTTPCTransform::GetNPads(row + offset) ) 
150           HLTFatal("Pad out of Range %d < %d < %d",pad, AliHLTTPCTransform::GetNPads(row + offset));
151         if ( bin > fNTimeBins )
152           HLTFatal("Bin out of Range %d < %d < %d",bin, fNTimeBins);
153       }
154       else {  
155           if ((row-fRowOffset)*fNMaxPads*fNTimeBins+ pad*fNTimeBins + bin >=  fNMaxRows*fNMaxPads*fNTimeBins ) {
156               HLTFatal("Index out of array range PAD=%d ||| ROW=%d ||| BIN=%d ||| OFFSET=%d ||| ROWOFFSET=%d", pad, row, bin, offset, fRowOffset);
157               continue;
158           }
159           else {
160               fData[ (row-fRowOffset)*fNMaxPads*fNTimeBins+ pad*fNTimeBins + bin ] = fTPCRawStream->GetSignal() ;
161           }
162       }
163   }
164 #endif // ENABLE_PAD_SORTING
165
166   return 0;
167 }
168
169 Bool_t AliHLTTPCDigitReaderPacked::NextSignal(){
170   Bool_t readvalue = kTRUE;
171
172 #if ENABLE_PAD_SORTING
173   while (1) {
174       fCurrentBin++;
175       if (fCurrentBin >= fNTimeBins){
176           fCurrentBin = 0;
177           fCurrentPad++;
178      
179           if (fCurrentPad >=fNMaxPads){
180               fCurrentPad = 0;
181               fCurrentRow++;
182               
183               if (fCurrentRow >= fNMaxRows){
184                   readvalue = kFALSE;
185                   break;
186               }
187           }
188       }
189
190       if (fCurrentRow*fNMaxPads*fNTimeBins+ fCurrentPad*fNTimeBins + fCurrentBin >=  fNMaxRows*fNMaxPads*fNTimeBins ) {
191           HLTFatal("Overflow: row=%d pad=%d bin=%d", fCurrentRow, fCurrentPad, fCurrentBin);
192           readvalue = kFALSE;
193           break;
194       }
195
196       if (fData[ fCurrentRow*fNMaxPads*fNTimeBins + fCurrentPad*fNTimeBins + fCurrentBin  ] != -1) break;
197   }
198 #else // !ENABLE_PAD_SORTING
199   readvalue = fTPCRawStream->Next();
200 #endif // ENABLE_PAD_SORTING
201
202   return readvalue;
203 }
204
205 Int_t AliHLTTPCDigitReaderPacked::GetRow(){
206 #if ENABLE_PAD_SORTING
207   return (fCurrentRow + fRowOffset);
208 #else // !ENABLE_PAD_SORTING
209   return (Int_t) fTPCRawStream->GetRow();
210 #endif // ENABLE_PAD_SORTING
211 }
212
213 int AliHLTTPCDigitReaderPacked::GetPad(){
214 #if ENABLE_PAD_SORTING
215   return fCurrentPad;
216 #else // !ENABLE_PAD_SORTING
217   return fTPCRawStream->GetPad();
218 #endif // ENABLE_PAD_SORTING
219 }
220
221 Int_t AliHLTTPCDigitReaderPacked::GetSignal(){ 
222 #if ENABLE_PAD_SORTING
223   return fData[ fCurrentRow*fNMaxPads*fNTimeBins+ fCurrentPad*fNTimeBins + fCurrentBin ];
224 #else // !ENABLE_PAD_SORTING
225   return fTPCRawStream->GetSignal();
226 #endif // ENABLE_PAD_SORTING
227 }
228
229 Int_t AliHLTTPCDigitReaderPacked::GetTime(){
230 #if ENABLE_PAD_SORTING
231   return fCurrentBin;
232 #else // !ENABLE_PAD_SORTING
233   return fTPCRawStream->GetTime();
234 #endif // ENABLE_PAD_SORTING
235 }
236 #endif //defined(HAVE_ALIRAWDATA) && defined(HAVE_ALITPCRAWSTREAM_H)