]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCAInputDataCompressorComponent.cxx
bug fix: reconstruction crash when the output buffer size exceed
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCAInputDataCompressorComponent.cxx
1 // **************************************************************************
2 // This file is property of and copyright by the ALICE HLT Project          *
3 // ALICE Experiment at CERN, All rights reserved.                           *
4 //                                                                          *
5 // Primary Authors: Sergey Gorbunov <sergey.gorbunov@kip.uni-heidelberg.de> *
6 //                  Ivan Kisel <kisel@kip.uni-heidelberg.de>                *
7 //                  for The ALICE HLT Project.                              *
8 //                                                                          *
9 // Permission to use, copy, modify and distribute this software and its     *
10 // documentation strictly for non-commercial purposes is hereby granted     *
11 // without fee, provided that the above copyright notice appears in all     *
12 // copies and that both the copyright notice and this permission notice     *
13 // appear in the supporting documentation. The authors make no claims       *
14 // about the suitability of this software for any purpose. It is            *
15 // provided "as is" without express or implied warranty.                    *
16 //                                                                          *
17 //***************************************************************************
18
19
20 ///////////////////////////////////////////////////////////////////////////////
21 //                                                                           //
22 // a TPC tracker processing component for the HLT based on CA by Ivan Kisel  //
23 //                                                                           //
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #if __GNUC__>= 3
27 using namespace std;
28 #endif
29
30 #include "AliHLTTPCCAInputDataCompressorComponent.h"
31 #include "AliHLTTPCCACompressedInputData.h"
32 #include "AliHLTTPCTransform.h"
33 #include "AliHLTTPCClusterDataFormat.h"
34 #include "AliHLTTPCSpacePointData.h"
35 #include "AliHLTTPCDefinitions.h"
36 #include "AliHLTTPCCADef.h"
37 #include "TStopwatch.h"
38
39
40 const AliHLTComponentDataType AliHLTTPCCADefinitions::fgkCompressedInputDataType = AliHLTComponentDataTypeInitializer( "CAINPACK", kAliHLTDataOriginTPC );
41
42 /** ROOT macro for the implementation of ROOT specific class methods */
43 ClassImp( AliHLTTPCCAInputDataCompressorComponent )
44
45 AliHLTTPCCAInputDataCompressorComponent::AliHLTTPCCAInputDataCompressorComponent()
46   :
47   AliHLTProcessor(),
48   fTotalTime( 0 ),
49   fTotalInputSize( 0 ),
50   fTotalOutputSize( 0 ),
51   fNEvents( 0 )
52 {
53   // see header file for class documentation
54   // or
55   // refer to README to build package
56   // or
57   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
58 }
59
60 AliHLTTPCCAInputDataCompressorComponent::AliHLTTPCCAInputDataCompressorComponent( const AliHLTTPCCAInputDataCompressorComponent& )
61   :
62   AliHLTProcessor(),
63   fTotalTime( 0 ),
64   fTotalInputSize( 0 ),
65   fTotalOutputSize( 0 ),
66   fNEvents( 0 )
67 {
68   // see header file for class documentation
69   HLTFatal( "copy constructor untested" );
70 }
71
72 AliHLTTPCCAInputDataCompressorComponent& AliHLTTPCCAInputDataCompressorComponent::operator=( const AliHLTTPCCAInputDataCompressorComponent& )
73 {
74   // see header file for class documentation
75   HLTFatal( "assignment operator untested" );
76   return *this;
77 }
78
79 AliHLTTPCCAInputDataCompressorComponent::~AliHLTTPCCAInputDataCompressorComponent()
80 {
81   // see header file for class documentation  
82 }
83
84 //
85 // Public functions to implement AliHLTComponent's interface.
86 // These functions are required for the registration process
87 //
88
89 const char* AliHLTTPCCAInputDataCompressorComponent::GetComponentID()
90 {
91   // see header file for class documentation
92   return "TPCCAInputDataCompressor";
93 }
94
95 void AliHLTTPCCAInputDataCompressorComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list )
96 {
97   // see header file for class documentation
98   list.clear();
99   list.push_back( AliHLTTPCDefinitions::fgkClustersDataType );
100 }
101
102 AliHLTComponentDataType AliHLTTPCCAInputDataCompressorComponent::GetOutputDataType()
103 {
104   // see header file for class documentation
105   return AliHLTTPCCADefinitions::fgkCompressedInputDataType;
106 }
107
108 void AliHLTTPCCAInputDataCompressorComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
109 {
110   // define guess for the output data size
111   constBase = 200;       // minimum size
112   inputMultiplier = 0.25; // size relative to input
113 }
114
115 AliHLTComponent* AliHLTTPCCAInputDataCompressorComponent::Spawn()
116 {
117   // see header file for class documentation
118   return new AliHLTTPCCAInputDataCompressorComponent;
119 }
120
121
122
123
124 int AliHLTTPCCAInputDataCompressorComponent::DoInit( int /*argc*/, const char** /*argv*/ )
125 {
126   // Configure the CA tracker component
127   fTotalTime = 0;
128   fTotalInputSize = 0;
129   fTotalOutputSize = 0; 
130   fNEvents = 0;
131   return 0;
132 }
133
134
135 int AliHLTTPCCAInputDataCompressorComponent::DoDeinit()
136 {
137   // see header file for class documentation
138   return 0;
139 }
140
141
142
143 int AliHLTTPCCAInputDataCompressorComponent::Reconfigure( const char* /*cdbEntry*/, const char* /*chainId*/ )
144 {
145   // Reconfigure the component from OCDB .
146   return 0;
147 }
148
149
150
151 int AliHLTTPCCAInputDataCompressorComponent::DoEvent
152 (
153   const AliHLTComponentEventData& evtData,
154   const AliHLTComponentBlockData* blocks,
155   AliHLTComponentTriggerData& /*trigData*/,
156   AliHLTUInt8_t* outputPtr,
157   AliHLTUInt32_t& size,
158   vector<AliHLTComponentBlockData>& outputBlocks )
159 {
160   //* process event
161
162   AliHLTUInt32_t maxBufferSize = size;
163   size = 0; // output size
164
165   if ( GetFirstInputBlock( kAliHLTDataTypeSOR ) || GetFirstInputBlock( kAliHLTDataTypeEOR ) ) {
166     return 0;
167   }
168
169   TStopwatch timer;
170
171   // Preprocess the data for CA Slice Tracker
172
173   if ( evtData.fBlockCnt <= 0 ) {
174     HLTWarning( "no blocks in event" );
175     return 0;
176   }
177
178   Int_t ret = 0;
179
180   Int_t inTotalSize = 0;    
181   Int_t outTotalSize = 0;
182   Int_t minSlice = 100;
183   Int_t maxSlice = -1;
184
185   for ( unsigned long ndx = 0; ndx < evtData.fBlockCnt; ndx++ ) {
186     const AliHLTComponentBlockData* iter = blocks + ndx;
187     if ( iter->fDataType != AliHLTTPCDefinitions::fgkClustersDataType ) continue;    
188
189     if( minSlice>AliHLTTPCDefinitions::GetMinSliceNr( *iter ) ) minSlice = AliHLTTPCDefinitions::GetMinSliceNr( *iter ) ;
190     if( maxSlice<AliHLTTPCDefinitions::GetMaxSliceNr( *iter ) ) maxSlice = AliHLTTPCDefinitions::GetMaxSliceNr( *iter ) ;
191    
192    
193     inTotalSize += iter->fSize;
194
195     AliHLTTPCClusterData* inPtrSP = ( AliHLTTPCClusterData* )( iter->fPtr );    
196
197     AliHLTTPCCACompressedCluster *outCluster = (AliHLTTPCCACompressedCluster*)( outputPtr+outTotalSize );
198     AliHLTTPCCACompressedClusterRow *outRow = 0;
199
200     Int_t dSize = 0;
201     UShort_t oldId = 0;
202     for ( unsigned int i = 0; i < inPtrSP->fSpacePointCnt; i++ ){ 
203       AliHLTTPCSpacePointData *cluster = &( inPtrSP->fSpacePoints[i] );
204       UInt_t origId = cluster->fID;
205       UInt_t patch = (origId>>22)&0x7;
206       UInt_t slice = origId>>25;
207       UInt_t row = cluster->fPadRow;
208       Double_t rowX = AliHLTTPCTransform::Row2X( row );
209       row = row - AliHLTTPCTransform::GetFirstRow( patch );
210       UShort_t id = (UShort_t)( (slice<<10) +(patch<<6) + row );
211       if( i==0 || id!= oldId ){ 
212         // fill new row header  
213         outRow = (AliHLTTPCCACompressedClusterRow*) outCluster;
214         outCluster = outRow->fClusters;  
215         dSize+= ( ( AliHLTUInt8_t * )outCluster ) -  (( AliHLTUInt8_t * )outRow);
216         if ( outTotalSize + dSize > (int) maxBufferSize ) break;
217         outRow->fSlicePatchRowID = id;  
218         outRow->fNClusters = 0;
219         oldId = id;
220         //cout<<"Fill row: s "<<slice<<" p "<<patch<<" r "<<row<<" x "<<outRow->fX<<":"<<endl;
221       }
222     
223       // pack the cluster
224       {
225         // get coordinates in [um]
226         
227         Double_t x = (cluster->fX - rowX )*1.e4 + 32768.;
228         Double_t y = (cluster->fY)*1.e4 + 8388608.;
229         Double_t z = (cluster->fZ)*1.e4 + 8388608.;
230         
231         // truncate if necessary
232         if( x<0 ) x = 0; else if( x > 0x0000FFFF ) x = 0x0000FFFF;
233         if( y<0 ) y = 0; else if( y > 0x00FFFFFF ) y = 0x00FFFFFF;
234         if( z<0 ) z = 0; else if( z > 0x00FFFFFF ) z = 0x00FFFFFF;
235         
236         UInt_t ix0 =  ( (UInt_t) x )&0x000000FF;
237         UInt_t ix1 = (( (UInt_t) x )&0x0000FF00 )>>8;
238         UInt_t iy = ( (UInt_t) y )&0x00FFFFFF;
239         UInt_t iz = ( (UInt_t) z )&0x00FFFFFF;
240         
241         dSize+= sizeof( AliHLTTPCCACompressedCluster );
242         if ( outTotalSize + dSize > (int) maxBufferSize ) break;      
243         outCluster->fP0 = (ix0<<24) + iy;
244         outCluster->fP1 = (ix1<<24) + iz;      
245         outCluster++;
246         outRow->fNClusters++;
247         //cout<<"clu "<<outRow->fNClusters-1<<": "<<cluster->fX<<" "<<cluster->fY<<" "<<cluster->fZ<<" "<<cluster->fID<<endl;
248       }
249     }
250     
251     if ( outTotalSize + dSize > (int) maxBufferSize ) {
252       HLTWarning( "Output buffer size exceed (buffer size %d, current size %d)", maxBufferSize, outTotalSize+ dSize );
253       ret = -ENOSPC;
254       break;
255     }    
256     AliHLTComponentBlockData bd;
257     FillBlockData( bd );
258     bd.fOffset = outTotalSize;
259     bd.fSize = dSize;
260     bd.fSpecification = iter->fSpecification;
261     bd.fDataType = GetOutputDataType();
262     outputBlocks.push_back( bd );
263     outTotalSize+=dSize;    
264   }
265
266   size = outTotalSize;
267   
268   timer.Stop();
269   
270   fTotalTime += timer.RealTime();
271   fTotalInputSize+= inTotalSize;
272   fTotalOutputSize+= outTotalSize;
273   fNEvents++;
274
275   if( maxSlice<0 ) minSlice = -1;
276   Int_t hz = ( int ) ( fTotalTime > 1.e-10 ? fNEvents / fTotalTime : 100000 );
277   Float_t ratio = 0;
278   if( fTotalOutputSize>0 ) ratio = (Float_t ) (fTotalInputSize/fTotalOutputSize);
279
280   // Set log level to "Warning" for on-line system monitoring
281   
282   HLTInfo( "CAInputDataCompressor, slices %d-%d: speed %d Hz, ratio %f, %d events", minSlice, maxSlice, hz, ratio, fNEvents );
283
284   return ret;
285 }
286
287