1 // **************************************************************************
2 // * This file is property of and copyright by the ALICE HLT Project *
3 // * All rights reserved. *
5 // * Primary Authors: *
6 // * Copyright 2009 Matthias Kretz <kretz@kde.org> *
8 // * Permission to use, copy, modify and distribute this software and its *
9 // * documentation strictly for non-commercial purposes is hereby granted *
10 // * without fee, provided that the above copyright notice appears in all *
11 // * copies and that both the copyright notice and this permission notice *
12 // * appear in the supporting documentation. The authors make no claims *
13 // * about the suitability of this software for any purpose. It is *
14 // * provided "as is" without express or implied warranty. *
15 // **************************************************************************
17 #include "AliHLTTPCCASliceData.h"
18 #include "AliHLTTPCCAClusterData.h"
19 #include "AliHLTTPCCAMath.h"
20 #include "AliHLTArray.h"
21 #include "AliHLTTPCCAHit.h"
22 #include "AliHLTTPCCAParam.h"
23 #include "MemoryAssignmentHelpers.h"
26 // calculates an approximation for 1/sqrt(x)
27 // Google for 0x5f3759df :)
28 static inline float fastInvSqrt( float _x )
30 // the function calculates fast inverse sqrt
32 union { float f; int i; } x = { _x };
33 const float xhalf = 0.5f * x.f;
34 x.i = 0x5f3759df - ( x.i >> 1 );
35 x.f = x.f * ( 1.5f - xhalf * x.f * x.f );
39 inline void AliHLTTPCCASliceData::CreateGrid( AliHLTTPCCARow *row, const AliHLTTPCCAClusterData &data )
43 if ( row->NHits() <= 0 ) { // no hits or invalid data
44 // grid coordinates don't matter, since there are no hits
45 row->fGrid.CreateEmpty();
53 for ( int i = row->fHitNumberOffset; i < row->fHitNumberOffset + row->fNHits; ++i ) {
54 const float y = data.Y( i );
55 const float z = data.Z( i );
56 if ( yMax < y ) yMax = y;
57 if ( yMin > y ) yMin = y;
58 if ( zMax < z ) zMax = z;
59 if ( zMin > z ) zMin = z;
62 const float norm = fastInvSqrt( row->fNHits );
63 row->fGrid.Create( yMin, yMax, zMin, zMax,
64 CAMath::Max( ( yMax - yMin ) * norm, 2.f ),
65 CAMath::Max( ( zMax - zMin ) * norm, 2.f ) );
68 inline void AliHLTTPCCASliceData::PackHitData( AliHLTTPCCARow *row, const AliHLTArray<AliHLTTPCCAHit> &binSortedHits )
72 static const float shortPackingConstant = 1.f / 65535.f;
73 const float y0 = row->fGrid.YMin();
74 const float z0 = row->fGrid.ZMin();
75 const float stepY = ( row->fGrid.YMax() - y0 ) * shortPackingConstant;
76 const float stepZ = ( row->fGrid.ZMax() - z0 ) * shortPackingConstant;
77 const float stepYi = 1.f / stepY;
78 const float stepZi = 1.f / stepZ;
84 row->fHstepYi = stepYi;
85 row->fHstepZi = stepZi;
87 for ( int hitIndex = 0; hitIndex < row->fNHits; ++hitIndex ) {
89 const int globalHitIndex = row->fHitNumberOffset + hitIndex;
90 const AliHLTTPCCAHit &hh = binSortedHits[globalHitIndex];
91 const float xx = ( ( hh.Y() - y0 ) * stepYi ) + .5 ;
92 const float yy = ( ( hh.Z() - z0 ) * stepZi ) + .5 ;
93 if ( xx < 0 || yy < 0 || xx >= 65536 || yy >= 65536 ) {
94 std::cout << "!!!! hit packing error!!! " << xx << " " << yy << " " << std::endl;
96 // HitData is bin sorted
97 fHitDataY[row->fHitNumberOffset + hitIndex] = xx;
98 fHitDataZ[row->fHitNumberOffset + hitIndex] = yy;
102 void AliHLTTPCCASliceData::Clear()
107 void AliHLTTPCCASliceData::InitializeRows( const AliHLTTPCCAParam &p )
109 // initialisation of rows
111 for ( int i = 0; i < p.NRows(); ++i ) {
112 fRows[i].fX = p.RowX( i );
113 fRows[i].fMaxY = CAMath::Tan( p.DAlpha() / 2. ) * fRows[i].fX;
117 void AliHLTTPCCASliceData::InitFromClusterData( const AliHLTTPCCAClusterData &data )
119 // initialisation from cluster data
121 ////////////////////////////////////
123 ////////////////////////////////////
125 fNumberOfHits = data.NumberOfClusters();
127 /* TODO Vectorization
128 for ( int rowIndex = data.FirstRow(); rowIndex <= data.LastRow(); ++rowIndex ) {
129 int NumberOfClusters( int rowIndex ) const;
131 const int memorySize = fNumberOfHits * sizeof( short_v::Type )
133 const int numberOfRows = data.LastRow() - data.FirstRow();
134 enum { kVectorAlignment = sizeof( int ) };
135 const int numberOfHitsPlusAlignment = NextMultipleOf < kVectorAlignment / sizeof( int ) > ( fNumberOfHits );
136 const int memorySize =
138 numberOfHitsPlusAlignment * 4 * sizeof( short ) +
140 NextMultipleOf<kVectorAlignment>( ( 23 * numberOfRows + 4 * fNumberOfHits + 3 ) * sizeof( int ) ) +
141 // HitWeights, ClusterDataIndex
142 numberOfHitsPlusAlignment * 2 * sizeof( int );
144 if ( fMemorySize < memorySize ) {
145 fMemorySize = memorySize;
147 fMemory = new char[fMemorySize + 4];// kVectorAlignment];
151 AssignMemory( fLinkUpData, mem, numberOfHitsPlusAlignment );
152 AssignMemory( fLinkDownData, mem, numberOfHitsPlusAlignment );
153 AssignMemory( fHitDataY, mem, numberOfHitsPlusAlignment );
154 AssignMemory( fHitDataZ, mem, numberOfHitsPlusAlignment );
155 AssignMemory( fFirstHitInBin, mem, 23 * numberOfRows + 4 * fNumberOfHits + 3 );
156 AssignMemory( fHitWeights, mem, numberOfHitsPlusAlignment );
157 AssignMemory( fClusterDataIndex, mem, numberOfHitsPlusAlignment );
159 ////////////////////////////////////
160 // 2. fill HitData and FirstHitInBin
161 ////////////////////////////////////
163 for ( int rowIndex = 0; rowIndex < data.FirstRow(); ++rowIndex ) {
164 AliHLTTPCCARow &row = fRows[rowIndex];
165 row.fGrid.CreateEmpty();
168 row.fHitNumberOffset = 0;
169 row.fFirstHitInBinOffset = 0;
178 for ( int rowIndex = data.LastRow() + 1; rowIndex < 160; ++rowIndex ) {
179 AliHLTTPCCARow &row = fRows[rowIndex];
180 row.fGrid.CreateEmpty();
183 row.fHitNumberOffset = 0;
184 row.fFirstHitInBinOffset = 0;
195 AliHLTResizableArray<AliHLTTPCCAHit> binSortedHits( fNumberOfHits );
197 int gridContentOffset = 0;
199 int binCreationMemorySize = 103 * 2 + fNumberOfHits;
200 AliHLTResizableArray<unsigned short> binCreationMemory( binCreationMemorySize );
202 for ( int rowIndex = data.FirstRow(); rowIndex <= data.LastRow(); ++rowIndex ) {
203 AliHLTTPCCARow &row = fRows[rowIndex];
204 row.fNHits = data.NumberOfClusters( rowIndex );
205 assert( row.fNHits < ( 1 << sizeof( unsigned short ) * 8 ) );
206 row.fHitNumberOffset = data.RowOffset( rowIndex );
207 row.fFirstHitInBinOffset = gridContentOffset;
209 CreateGrid( &row, data );
210 const AliHLTTPCCAGrid &grid = row.fGrid;
211 const int numberOfBins = grid.N();
213 int binCreationMemorySizeNew;
214 if ( ( binCreationMemorySizeNew = numberOfBins * 2 + 6 + row.fNHits ) > binCreationMemorySize ) {
215 binCreationMemorySize = binCreationMemorySizeNew;
216 binCreationMemory.Resize( binCreationMemorySize );
219 AliHLTArray<unsigned short> c = binCreationMemory; // number of hits in all previous bins
220 AliHLTArray<unsigned short> bins = c + ( numberOfBins + 3 ); // cache for the bin index for every hit in this row
221 AliHLTArray<unsigned short> filled = bins + row.fNHits; // counts how many hits there are per bin
223 for ( unsigned int bin = 0; bin < row.fGrid.N() + 3; ++bin ) {
224 filled[bin] = 0; // initialize filled[] to 0
227 for ( int hitIndex = 0; hitIndex < row.fNHits; ++hitIndex ) {
228 const int globalHitIndex = row.fHitNumberOffset + hitIndex;
229 const unsigned short bin = row.fGrid.GetBin( data.Y( globalHitIndex ), data.Z( globalHitIndex ) );
230 bins[hitIndex] = bin;
234 unsigned short n = 0;
235 for ( int bin = 0; bin < numberOfBins + 3; ++bin ) {
240 for ( int hitIndex = 0; hitIndex < row.fNHits; ++hitIndex ) {
241 const unsigned short bin = bins[hitIndex];
243 const unsigned short ind = c[bin] + filled[bin]; // generate an index for this hit that is >= c[bin] and < c[bin + 1]
244 const int globalBinsortedIndex = row.fHitNumberOffset + ind;
245 const int globalHitIndex = row.fHitNumberOffset + hitIndex;
247 // allows to find the global hit index / coordinates from a global bin sorted hit index
248 fClusterDataIndex[globalBinsortedIndex] = globalHitIndex;
249 binSortedHits[globalBinsortedIndex].SetY( data.Y( globalHitIndex ) );
250 binSortedHits[globalBinsortedIndex].SetZ( data.Z( globalHitIndex ) );
253 PackHitData( &row, binSortedHits );
255 for ( int i = 0; i < numberOfBins; ++i ) {
256 fFirstHitInBin[row.fFirstHitInBinOffset + i] = c[i]; // global bin-sorted hit index
258 const unsigned short a = c[numberOfBins];
259 // grid.N is <= row.fNHits
260 const int nn = numberOfBins + grid.Ny() + 3;
261 for ( int i = numberOfBins; i < nn; ++i ) {
262 assert( row.fFirstHitInBinOffset + i < 23 * numberOfRows + 4 * fNumberOfHits + 3 );
263 fFirstHitInBin[row.fFirstHitInBinOffset + i] = a;
267 gridContentOffset += nn;
271 //SG cell finder - test code
273 if ( fTmpHitInputIDs ) delete[] fTmpHitInputIDs;
274 fTmpHitInputIDs = new int [NHits];
275 const float areaY = .5;
276 const float areaZ = .5;
277 int newRowNHitsTotal = 0;
278 bool *usedHits = new bool [NHits];
279 for ( int iHit = 0; iHit < NHits; iHit++ ) usedHits[iHit] = 0;
280 for ( int iRow = 0; iRow < fParam.NRows(); iRow++ ) {
281 rowHeaders[iRow*2 ] = newRowNHitsTotal; // new first hit
282 rowHeaders[iRow*2+1] = 0; // new N hits
284 int oldRowFirstHit = RowFirstHit[iRow];
285 int oldRowLastHit = oldRowFirstHit + RowNHits[iRow];
286 for ( int iHit = oldRowFirstHit; iHit < oldRowLastHit; iHit++ ) {
287 if ( usedHits[iHit] ) continue;
296 if ( 0 ) for ( int jHit = iHit + 1; jHit < oldRowLastHit; jHit++ ) {//SG!!!
297 //if( usedHits[jHit] ) continue;
298 float dy = Y[jHit] - y0;
299 float dz = Z[jHit] - z0;
300 if ( CAMath::Abs( dy ) < areaY && CAMath::Abs( dz ) < areaZ ) {
308 int id = newRowNHitsTotal + newRowNHits;
309 hitsXYZ[id*3+0 ] = cx / nclu;
310 hitsXYZ[id*3+1 ] = cy / nclu;
311 hitsXYZ[id*3+2 ] = cz / nclu;
312 fTmpHitInputIDs[id] = iHit;
315 rowHeaders[iRow*2+1] = newRowNHits;
316 newRowNHitsTotal += newRowNHits;
318 NHitsTotal() = newRowNHitsTotal;
319 reinterpret_cast<int*>( fInputEvent )[1+fParam.NRows()*2] = newRowNHitsTotal;
325 void AliHLTTPCCASliceData::ClearHitWeights()
329 #ifdef ENABLE_VECTORIZATION
330 const int_v v0( Zero );
331 const int *const end = fHitWeights + fNumberOfHits;
332 for ( int *mem = fHitWeights; mem < end; mem += v0.Size ) {
336 for ( int i = 0; i < fNumberOfHits; ++i ) {
342 void AliHLTTPCCASliceData::ClearLinks()
346 #ifdef ENABLE_VECTORIZATION
347 const short_v v0( -1 );
348 const short *const end1 = fLinkUpData + fNumberOfHits;
349 for ( short *mem = fLinkUpData; mem < end; mem += v0.Size ) {
352 const short *const end2 = fLinkDownData + fNumberOfHits;
353 for ( short *mem = fLinkDownData; mem < end; mem += v0.Size ) {
357 for ( int i = 0; i < fNumberOfHits; ++i ) {
360 for ( int i = 0; i < fNumberOfHits; ++i ) {
361 fLinkDownData[i] = -1;