StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AliHLTTPCCASliceDataVector.h
1 /**************************************************************************
2  * This file is property of and copyright by the ALICE HLT Project *
3  * All rights reserved. *
4  * *
5  * Primary Authors: *
6  * Copyright 2009 Matthias Kretz <kretz@kde.org> *
7  * *
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  **************************************************************************/
16 
17 #ifndef SLICEDATAVECTOR_H
18 #define SLICEDATAVECTOR_H
19 
20 #include "AliHLTTPCCARow.h"
21 #include "AliHLTTPCCAMath.h"
22 #include "AliHLTTPCCAParam.h"
23 #include "AliHLTTPCCADef.h"
24 #include "AliHLTTPCCAClusterData.h"
25 #include <cstdio>
26 
27 #include "debug.h"
28 
30 class AliHLTTPCCAHit;
31 class AliHLTTPCCAParam;
32 
33 // index types. For efficient loads and stores we want only neighbours, so the index is a scalar
34 // giving the index of the first item. How many items are actually returned is determined by the
35 // size of the vector that is returned (e.g. short_v::Size or float_v::Size).
36 typedef short short_i;
37 typedef unsigned int uint_i;
38 
46 {
47  public:
48  AliHLTTPCCASliceData() : fRows(), fNumberOfHits(0), fMemorySize( 0 ), fMemory( 0 ), fParam( 0 ) {}
49  ~AliHLTTPCCASliceData() { if (fMemory) delete[] fMemory; }
50 
51  void InitializeRows( const AliHLTTPCCAParam &parameters );
52 
58 
62  void Clear();
63 
67  int NumberOfHits() const { return fNumberOfHits; }
68 
74  short HitLinkUpDataS ( const AliHLTTPCCARow &row, short hitIndex ) const;
75  short HitLinkDownDataS( const AliHLTTPCCARow &row, short hitIndex ) const;
76  const short *HitLinkUpData ( const AliHLTTPCCARow &row ) const;
77  const short *HitLinkDownData( const AliHLTTPCCARow &row ) const;
78  short_v HitLinkUpData ( const AliHLTTPCCARow &row, const short_i &hitIndex ) const;
79  short_v HitLinkDownData( const AliHLTTPCCARow &row, const short_i &hitIndex ) const;
80  short_v HitLinkUpData ( const AliHLTTPCCARow &row, const ushort_v &hitIndexes ) const;
81  short_v HitLinkDownData( const AliHLTTPCCARow &row, const ushort_v &hitIndexes ) const;
82  void SetHitLinkUpData ( const AliHLTTPCCARow &row, const short_i &hitIndex, const short_v &value );
83  void SetHitLinkDownData( const AliHLTTPCCARow &row, const short_i &hitIndex, const short_v &value );
84  void SetHitLinkUpData ( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_v &value, const short_m &mask );
85  void SetHitLinkDownData( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_v &value, const short_m &mask );
86  void SetHitLinkUpData ( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_v &value );
87  void SetHitLinkDownData( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_v &value);
88 
92  void ClearLinks();
93 
97  float HitDataXS( const AliHLTTPCCARow &row, short hitIndex ) const;
98  float HitDataYS( const AliHLTTPCCARow &row, short hitIndex ) const;
99  float HitDataZS( const AliHLTTPCCARow &row, short hitIndex ) const;
100  const float *HitDataY( const AliHLTTPCCARow &row ) const;
101  const float *HitDataZ( const AliHLTTPCCARow &row ) const;
102  const short *HitDataIsUsed( const AliHLTTPCCARow &row ) const;
103  sfloat_v HitDataY( const AliHLTTPCCARow &row, const uint_i &hitIndex ) const;
104  sfloat_v HitDataZ( const AliHLTTPCCARow &row, const uint_i &hitIndex ) const;
105  sfloat_v HitDataY( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const sfloat_m &mask ) const;
106  sfloat_v HitDataZ( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const sfloat_m &mask ) const;
107  //short_v HitDataIsUsed( const AliHLTTPCCARow &row, const uint_i &hitIndex ) const;
108  void SetHitAsUsed( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_m &mask );
109  void SetHitAsUsedInTrackFit( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_m &mask );
110  void SetHitAsUsedInTrackExtend( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_m &mask );
111 
118  ushort_v FirstHitInBin( const AliHLTTPCCARow &row, ushort_v binIndexes ) const;
119  const unsigned short *FirstHitInBin( const AliHLTTPCCARow &row ) const;
120  unsigned short FirstHitInBin( const AliHLTTPCCARow &row, unsigned short binIndex ) const;
121 
130  static ushort_v CalculateHitWeight( ushort_v numberOfHits, ushort_v unique );
131 
132  ushort_m TakeOwnHits( const AliHLTTPCCARow &row, const ushort_v &hitIndex, const ushort_m &mask,
133  const ushort_v &weights ) const;
134 
138  void MaximizeHitWeight( const AliHLTTPCCARow &row, const ushort_v &hitIndex,
139  const ushort_v &weight );
140 
144  ushort_v HitWeight( const AliHLTTPCCARow &row, const ushort_v &hitIndex, const ushort_m &mask ) const;
145 
149  void ClearHitWeights();
150 
154  int ClusterDataIndex( const AliHLTTPCCARow &row, int hitIndex ) const;
155 
159  const AliHLTTPCCARow &Row( int rowIndex ) const;
160 
161  // for debugging:
162  void StoreToFile( FILE * ) const;
163  void RestoreFromFile( FILE * );
164 
165  const float *RowX() const { return fParam->RowX(); }
166  float RowX( int i ) const { return fParam->RowX( i ); }
167 
168  private:
169  enum {
170  VectorSizeFactor = ushort_v::Size / float_v::Size
171  };
172 
173  void createGrid( AliHLTTPCCARow *row, const AliHLTTPCCAClusterData &data, const int clusterDataOffset );
174 
175  AliHLTFixedArray<AliHLTTPCCARow, AliHLTArraySize<AliHLTTPCCAParameters::MaxNumberOfRows8+1>, AliHLTFullyCacheLineAligned> fRows; // The row objects needed for most accessor functions
176 
177  int fNumberOfHits; // the number of hits in this slice
178  int fMemorySize; // size of the allocated memory in bytes
179  char *fMemory; // pointer to the allocated memory where all the following arrays reside in
180  const AliHLTTPCCAParam *fParam; // pointer to the Param object for gathering X coordinates of rows
181 };
182 
183 inline short AliHLTTPCCASliceData::HitLinkUpDataS ( const AliHLTTPCCARow &row, short hitIndex ) const
184 {
185  return row.fLinkUpData[hitIndex];
186 }
187 
188 inline short AliHLTTPCCASliceData::HitLinkDownDataS( const AliHLTTPCCARow &row, short hitIndex ) const
189 {
190  return row.fLinkDownData[hitIndex];
191 }
192 
193 inline const short *AliHLTTPCCASliceData::HitLinkUpData ( const AliHLTTPCCARow &row ) const
194 {
195  return row.fLinkUpData;
196 }
197 
198 inline const short *AliHLTTPCCASliceData::HitLinkDownData( const AliHLTTPCCARow &row ) const
199 {
200  return row.fLinkDownData;
201 }
202 
203 inline short_v AliHLTTPCCASliceData::HitLinkUpData ( const AliHLTTPCCARow &row, const short_i &hitIndex ) const
204 {
205  //Matthias 01.24.13 assert( hitIndex * sizeof( short_v::EntryType ) % VectorAlignment == 0 );
206  return short_v( &row.fLinkUpData[hitIndex] );
207 }
208 
209 inline short_v AliHLTTPCCASliceData::HitLinkDownData( const AliHLTTPCCARow &row, const short_i &hitIndex ) const
210 {
211  //Matthias 01.24.13 assert( hitIndex * sizeof( short_v::EntryType ) % VectorAlignment == 0 );
212  return short_v( &row.fLinkDownData[hitIndex] );
213 }
214 
215 inline short_v AliHLTTPCCASliceData::HitLinkUpData ( const AliHLTTPCCARow &row, const ushort_v &hitIndexes ) const
216 {
217  return short_v( row.fLinkUpData, hitIndexes );
218 }
219 
220 inline short_v AliHLTTPCCASliceData::HitLinkDownData( const AliHLTTPCCARow &row, const ushort_v &hitIndexes ) const
221 {
222  // short_v tmp; // IKu debug
223 // tmp.gather( row.fLinkDownData, hitIndexes );
224 // return tmp;
225 // short *array = row.fLinkDownData; // IKu debug
226 // for (int i = 0; i < short_v::Size; i++){
227 // std::cout << array[hitIndexes[i]] << " ";
228 // }
229 // std::cout << std::endl;
230  return short_v( row.fLinkDownData, hitIndexes );
231 }
232 
233 inline void AliHLTTPCCASliceData::SetHitLinkUpData ( const AliHLTTPCCARow &row, const short_i &hitIndex, const short_v &value )
234 {
235  //Matthias 01.24.13 assert( hitIndex * sizeof( short_v::EntryType ) % VectorAlignment == 0 );
236  value.store( &row.fLinkUpData[hitIndex] );
237 }
238 
239 inline void AliHLTTPCCASliceData::SetHitLinkDownData( const AliHLTTPCCARow &row, const short_i &hitIndex, const short_v &value )
240 {
241  //Matthias 01.24.13 assert( hitIndex * sizeof( short_v::EntryType ) % VectorAlignment == 0 );
242  value.store( &row.fLinkDownData[hitIndex] );
243 }
244 
245 inline void AliHLTTPCCASliceData::SetHitLinkUpData ( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_v &value, const short_m &mask )
246 {
247 // value.scatter( row.fLinkUpData, hitIndexes, mask );
248  short *array = row.fLinkUpData;
249  foreach_bit(int i, mask){
250  array[hitIndexes[i]] = value[i];
251  }
252 }
253 
254 inline void AliHLTTPCCASliceData::SetHitLinkDownData( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_v &value, const short_m &mask )
255 {
256  // value.scatter( row.fLinkDownData, hitIndexes, mask );
257  short *array = row.fLinkDownData;
258  foreach_bit(int i, mask){
259  array[hitIndexes[i]] = value[i];
260  }
261 }
262 
263 inline void AliHLTTPCCASliceData::SetHitLinkUpData ( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_v &value)
264 {
265  // value.scatter( row.fLinkUpData, hitIndexes);
266  short *array = row.fLinkUpData;
267  for (int i = 0; i < short_v::Size; i++){
268  array[hitIndexes[i]] = value[i];
269  }
270 
271 }
272 
273 inline void AliHLTTPCCASliceData::SetHitLinkDownData( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_v &value)
274 {
275 // value.scatter( row.fLinkDownData, hitIndexes);
276  short *array = row.fLinkDownData;
277  for (int i = 0; i < short_v::Size; i++){
278  array[hitIndexes[i]] = value[i];
279  }
280 }
281 
282 inline float AliHLTTPCCASliceData::HitDataXS( const AliHLTTPCCARow &row, short hitIndex ) const
283 {// TODO read from hits-file
284  hitIndex++;
285  int i = 0;
286  for( ; i < fRows.Size(); i++ ) {
287  if (fRows[i].fHitDataZ == row.fHitDataZ) break;
288  }
289  return RowX(i);
290 }
291 
292 inline float AliHLTTPCCASliceData::HitDataYS( const AliHLTTPCCARow &row, short hitIndex ) const
293 {
294  return row.fHitDataY[hitIndex];
295 }
296 
297 inline float AliHLTTPCCASliceData::HitDataZS( const AliHLTTPCCARow &row, short hitIndex ) const
298 {
299  return row.fHitDataZ[hitIndex];
300 }
301 
302 inline const float *AliHLTTPCCASliceData::HitDataY( const AliHLTTPCCARow &row ) const
303 {
304  return row.fHitDataY;
305 }
306 
307 inline const float *AliHLTTPCCASliceData::HitDataZ( const AliHLTTPCCARow &row ) const
308 {
309  return row.fHitDataZ;
310 }
311 
312 inline const short *AliHLTTPCCASliceData::HitDataIsUsed( const AliHLTTPCCARow &row ) const
313 {
314  return row.fHitDataIsUsed;
315 }
316 
317 inline void AliHLTTPCCASliceData::SetHitAsUsed( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_m &mask )
318 {
319  //short_v(Vc::One).scatter( row.fHitDataIsUsed, static_cast<ushort_v>(hitIndexes), mask); // TODO why don't work w\o cast????
320  short *array = row.fHitDataIsUsed; // TODO don't work with scatter!
321  foreach_bit(int i, mask){
322  array[hitIndexes[i]] = 1;
323  }
324 }
325 
326 inline void AliHLTTPCCASliceData::SetHitAsUsedInTrackFit( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_m &mask )
327 {
328  // short_v(2).scatter( row.fHitDataIsUsed, static_cast<ushort_v>(hitIndexes), mask);
329  short *array = row.fHitDataIsUsed;
330  foreach_bit(int i, mask){
331  array[hitIndexes[i]] = 2;
332  }
333 }
334 
335 inline void AliHLTTPCCASliceData::SetHitAsUsedInTrackExtend( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const short_m &mask )
336 {
337  // short_v(3).scatter( row.fHitDataIsUsed, static_cast<ushort_v>(hitIndexes), mask);
338  short *array = row.fHitDataIsUsed;
339  foreach_bit(int i, mask){
340  array[hitIndexes[i]] = 3;
341  }
342 }
343 
344 inline sfloat_v AliHLTTPCCASliceData::HitDataY( const AliHLTTPCCARow &row, const uint_i &hitIndex ) const
345 {
346  //Matthias 01.24.13 assert( hitIndex * sizeof( sfloat_v::EntryType ) % VectorAlignment == 0 );
347  return sfloat_v( &row.fHitDataY[hitIndex] );
348 }
349 
350 inline sfloat_v AliHLTTPCCASliceData::HitDataZ( const AliHLTTPCCARow &row, const uint_i &hitIndex ) const
351 {
352  //Matthias 01.24.13 assert( hitIndex * sizeof( sfloat_v::EntryType ) % VectorAlignment == 0 );
353  return sfloat_v( &row.fHitDataZ[hitIndex] );
354 }
355 
356 inline sfloat_v AliHLTTPCCASliceData::HitDataY( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const sfloat_m &mask ) const
357 {
358  return sfloat_v( row.fHitDataY, hitIndexes, mask );
359 }
360 
361 inline sfloat_v AliHLTTPCCASliceData::HitDataZ( const AliHLTTPCCARow &row, const ushort_v &hitIndexes, const sfloat_m &mask ) const
362 {
363  return sfloat_v( row.fHitDataZ, hitIndexes, mask );
364 }
365 
366 // inline short_v AliHLTTPCCASliceData::HitDataIsUsed( const AliHLTTPCCARow &row, const uint_i &hitIndex ) const
367 // { // don't really need this
368 // return short_v( &row.fHitDataIsUsed[hitIndex] );
369 // }
370 
371 inline ushort_v AliHLTTPCCASliceData::FirstHitInBin( const AliHLTTPCCARow &row, ushort_v binIndexes ) const
372 {
373  const ushort_v tmp( row.fFirstHitInBin, binIndexes );
374  assert( tmp <= row.NHits() );
375  return tmp;
376 }
377 
378 inline const unsigned short *AliHLTTPCCASliceData::FirstHitInBin( const AliHLTTPCCARow &row ) const
379 {
380  return row.fFirstHitInBin;
381 }
382 
383 inline unsigned short AliHLTTPCCASliceData::FirstHitInBin( const AliHLTTPCCARow &row, unsigned short binIndex ) const
384 {
385  return row.fFirstHitInBin[binIndex];
386 }
387 
388 inline int AliHLTTPCCASliceData::ClusterDataIndex( const AliHLTTPCCARow &row, int hitIndex ) const
389 {
390  return row.fClusterDataIndex[hitIndex];
391 }
392 
393 inline const AliHLTTPCCARow &AliHLTTPCCASliceData::Row( int rowIndex ) const
394 {
395  return fRows[rowIndex];
396 }
397 
398 inline ushort_v AliHLTTPCCASliceData::CalculateHitWeight( ushort_v numberOfHits, ushort_v unique )
399 {
400 // return ( numberOfHits << 8 ) | ( unique & 0xff ); // CHECKME we don't need this unique?
401  return ( numberOfHits << 8 ) | ( unique & 0 );
402 }
403 
404 inline ushort_m AliHLTTPCCASliceData::TakeOwnHits( const AliHLTTPCCARow &row,
405  const ushort_v &hitIndex, const ushort_m &mask, const ushort_v &weights ) const
406 {
407  const ushort_v storedWeights( row.fHitWeights, hitIndex, mask );
408  const ushort_m own = storedWeights == weights && mask;
409  const ushort_v takenMarker = std::numeric_limits<ushort_v>::max();
410  takenMarker.scatter( row.fHitWeights, hitIndex, own );
411  return own;
412 }
413 
415  const ushort_v &hitIndex, const ushort_v &weight )
416 {
417  const short_m mask = validHitIndexes( hitIndex );
418  // XXX critical section if the TrackletConstructor gets multi-threaded
419  const ushort_v oldWeight( row.fHitWeights, hitIndex, mask );
420  debugF() << "scatter HitWeigths " << weight << " to " << hitIndex << ( weight > oldWeight && mask ) << " old: " << oldWeight << std::endl;
421  weight.scatter( row.fHitWeights, hitIndex, weight > oldWeight && mask );
422 }
423 
424 inline ushort_v AliHLTTPCCASliceData::HitWeight( const AliHLTTPCCARow &row, const ushort_v &hitIndex, const ushort_m &mask ) const
425 {
426  return ushort_v( row.fHitWeights, hitIndex, mask );
427 }
428 
429 
430 // calculates an approximation for 1/sqrt(x)
431 // Google for 0x5f3759df :)
432 static inline float fastInvSqrt( float _x )
433 {
434  union { float f; int i; } x = { _x };
435  const float xhalf = 0.5f * x.f;
436  x.i = 0x5f3759df - ( x.i >> 1 );
437  x.f = x.f * ( 1.5f - xhalf * x.f * x.f );
438  return x.f;
439 }
440 
441 inline void AliHLTTPCCASliceData::createGrid( AliHLTTPCCARow *row, const AliHLTTPCCAClusterData &data, const int clusterDataOffset )
442 {
443  const float minCellSize = AliHLTTPCCAParameters::MinCellSize;
444  if ( row->NHits() <= 0 ) { // no hits or invalid data
445  // grid coordinates don't matter, since there are no hits
446  row->fGrid.CreateEmpty();
447  return;
448  } else if ( row->NHits() == 1 ) {
449  const float y = data.Y( clusterDataOffset );
450  const float z = data.Z( clusterDataOffset );
451  row->fGrid.Create1( y, z, minCellSize, minCellSize );
452  return;
453  }
454 
455  const float norm = fastInvSqrt( AliHLTTPCCAParameters::GridCreationCoeff*row->fNHits ); // TODO parameter
456 // const float norm = 1./sqrt( 5*row->fNHits );
457 
458  float yMin = 1.e3f;
459  float yMax = -1.e3f;
460  float zMin = 1.e3f;
461  float zMax = -1.e3f;
462  float sy, sz;
463  if ( float_v::Size > 1 ) {
464  float_v min = yMin;
465  float_v max = yMax;
466  for ( int i = clusterDataOffset; i < clusterDataOffset + row->fNHits; ++i ) {
467  float tmp[4] = { data.Y( i ), data.Z( i ), 0.f, 0.f };
468  float_v r;
469  r.load( tmp, Vc::Unaligned);
470 // const float_v r = float_v::loadUnaligned( tmp );
471  //std::cout << r << std::endl;
472  min = CAMath::Min( min, r );
473  max = CAMath::Max( max, r );
474  }
475  // slide boaders apart a little
476 // min -= 1.;
477 // max += 1.;
478 
479  yMin = min[0];
480  zMin = min[1];
481  yMax = max[0];
482  zMax = max[1];
483 
484  const float_v s = CAMath::Max( ( max - min ) * norm, float_v( minCellSize ) );
485  sy = s[0];
486  sz = s[1];
487  } else {
488  for ( int i = clusterDataOffset; i < clusterDataOffset + row->fNHits; ++i ) {
489  const float y = data.Y( i );
490  const float z = data.Z( i );
491  if ( yMax < y ) yMax = y;
492  if ( yMin > y ) yMin = y;
493  if ( zMax < z ) zMax = z;
494  if ( zMin > z ) zMin = z;
495  }
496  sy = CAMath::Max( ( yMax - yMin ) * norm, minCellSize );
497  sz = CAMath::Max( ( zMax - zMin ) * norm, minCellSize );
498  }
499  // slide boaders a little apart
500 // yMin -= sy/2;
501 // yMax += sy/2;
502 // zMin -= sz/2;
503 // zMax += sz/2;
504 // sy = ( yMax - yMin ) * norm;
505 // sz = ( zMax - zMin ) * norm;
506 
507  row->fGrid.Create( yMin, yMax, zMin, zMax, sy, sz );
508 }
509 
510 
512 
513 #endif // SLICEDATAVECTOR_H
float Y(int index) const
ushort_v FirstHitInBin(const AliHLTTPCCARow &row, ushort_v binIndexes) const
short HitLinkUpDataS(const AliHLTTPCCARow &row, short hitIndex) const
void InitFromClusterData(const AliHLTTPCCAClusterData &data)
ushort_v HitWeight(const AliHLTTPCCARow &row, const ushort_v &hitIndex, const ushort_m &mask) const
const AliHLTTPCCARow & Row(int rowIndex) const
float Z(int index) const
float HitDataXS(const AliHLTTPCCARow &row, short hitIndex) const
int ClusterDataIndex(const AliHLTTPCCARow &row, int hitIndex) const
int Size() const
Definition: AliHLTArray.h:381
static ushort_v CalculateHitWeight(ushort_v numberOfHits, ushort_v unique)
void MaximizeHitWeight(const AliHLTTPCCARow &row, const ushort_v &hitIndex, const ushort_v &weight)