StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AliHLTTPCCASliceDataVector.cxx
1 /*
2  Copyright (C) 2009 Matthias Kretz <kretz@kde.org>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) version 3.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 
19 */
20 
21 #include "AliHLTTPCCASliceDataVector.h"
22 #include "AliHLTTPCCAClusterData.h"
23 #include "AliHLTTPCCAMath.h"
24 #include "AliHLTArray.h"
25 #include "AliHLTTPCCAHit.h"
26 #include "AliHLTTPCCAParam.h"
27 #include "MemoryAssignmentHelpers.h"
28 #include <iostream>
29 
31 {
32  fNumberOfHits = 0;
33 }
34 
35 void AliHLTTPCCASliceData::InitializeRows( const AliHLTTPCCAParam &p )
36 {
37  fParam = &p;
38  const float tan = CAMath::Tan( p.DAlpha() / 2.f );
39  for ( int i = 0; i < p.NRows(); ++i ) {
40  //fRows[i].fX = p.RowX( i );
41  fRows[i].fMaxY = tan * p.RowX( i );
42  }
43 }
44 
46 {
48  // 1. prepare arrays
50 
51  fNumberOfHits = data.NumberOfClusters();
52 
53  int numberOfHitsWithPadding = 0;
54  for ( int row = data.FirstRow(); row <= data.LastRow(); ++row ) {
55  numberOfHitsWithPadding += NextMultipleOf<VectorAlignment>( data.NumberOfClusters( row ) );
56  }
57 
58  const int numberOfRows = data.LastRow() - data.FirstRow() + 1;
59  const int firstHitInBinSize = 23 * numberOfRows + AliHLTTPCCAParameters::GridCreationCoeff * 4 * fNumberOfHits;
60  const int memorySize =
61  // LinkData
62  2 * numberOfHitsWithPadding * sizeof( short ) +
63  // HitData
64  2 * numberOfHitsWithPadding * sizeof( StoredFloat ) +
65  // IsUsedData
66  numberOfHitsWithPadding * sizeof( short ) +
67  // FirstHitInBin
68  NextMultipleOf<VectorAlignment>( firstHitInBinSize * sizeof( short ) ) +
69  // HitWeights, ClusterDataIndex
70  2 * numberOfHitsWithPadding * sizeof( int );
71 
72  if ( fMemorySize < memorySize ) {
73  fMemorySize = memorySize;
74  if (fMemory) delete[] fMemory;
75  fMemory = new char[fMemorySize + 7 * VectorAlignment];
76  }
77 
78 
79  short *linkUpData;
80  short *linkDownData;
81  StoredFloat *hitDataY;
82  StoredFloat *hitDataZ;
83  short *hitDataIsUsed;
84  int *clusterDataIndex;
85  unsigned short *hitWeights;
86  unsigned short *firstHitInBin;
87 
88  char *mem = fMemory;
89  AssignMemoryAligned<VectorAlignment>( linkUpData, mem, numberOfHitsWithPadding );
90  AssignMemoryAligned<VectorAlignment>( linkDownData, mem, numberOfHitsWithPadding );
91  AssignMemoryAligned<VectorAlignment>( hitDataY, mem, numberOfHitsWithPadding );
92  AssignMemoryAligned<VectorAlignment>( hitDataZ, mem, numberOfHitsWithPadding );
93  AssignMemoryAligned<VectorAlignment>( hitDataIsUsed, mem, numberOfHitsWithPadding );
94  /*
95  * The size of the array is row.Grid.N + row.Grid.Ny + 3. The row.Grid.Ny + 3 is an optimization
96  * to remove the need for bounds checking. The last values are the same as the entry at [N - 1].
97  */
98  AssignMemoryAligned<VectorAlignment>( firstHitInBin, mem, firstHitInBinSize );
99  AssignMemoryAligned<VectorAlignment>( hitWeights, mem, numberOfHitsWithPadding );
100  AssignMemoryAligned<VectorAlignment>( clusterDataIndex, mem, numberOfHitsWithPadding );
101 
103  // 2. fill HitData and FirstHitInBin
105 
106  for ( int rowIndex = 0; rowIndex < data.FirstRow(); ++rowIndex ) {
107  AliHLTTPCCARow &row = fRows[rowIndex];
108  row.fGrid.CreateEmpty();
109  row.fNHits = 0;
110  row.fLinkUpData = linkUpData;
111  row.fLinkDownData = linkDownData;
112  row.fHitDataY = hitDataY;
113  row.fHitDataZ = hitDataZ;
114  row.fHitDataIsUsed = hitDataIsUsed;
115  row.fClusterDataIndex = clusterDataIndex;
116  row.fHitWeights = hitWeights;
117  row.fFirstHitInBin = firstHitInBin;
118  }
119 
120  AliHLTResizableArray<float> binSortedHitsY( fNumberOfHits );
121  AliHLTResizableArray<float> binSortedHitsZ( fNumberOfHits );
122 
123  int gridContentOffset = 0;
124 
125  int binCreationMemorySize = 103 * 2 + fNumberOfHits;
126  AliHLTResizableArray<unsigned short> binCreationMemory( binCreationMemorySize );
127 
128  int hitNumberOffset = 0;
129 
130  for ( int rowIndex = data.FirstRow(); rowIndex <= data.LastRow(); ++rowIndex ) {
131  AliHLTTPCCARow &row = fRows[rowIndex];
132  const int clusterDataOffset = data.RowOffset( rowIndex );
133 
134  assert( data.NumberOfClusters( rowIndex ) < ( 1 << ( sizeof( unsigned short ) * 8 - 1 ) ) );
135  row.fNHits = data.NumberOfClusters( rowIndex );
136 
137  row.fLinkUpData = &linkUpData[hitNumberOffset];
138  row.fLinkDownData = &linkDownData[hitNumberOffset];
139  row.fHitDataY = &hitDataY[hitNumberOffset];
140  row.fHitDataZ = &hitDataZ[hitNumberOffset];
141  row.fHitDataIsUsed = &hitDataIsUsed[hitNumberOffset];
142  row.fClusterDataIndex = &clusterDataIndex[hitNumberOffset];
143  row.fHitWeights = &hitWeights[hitNumberOffset];
144  row.fHitNumberOffset = hitNumberOffset;
145 
146  row.fFirstHitInBin = &firstHitInBin[gridContentOffset];
147 
148  createGrid( &row, data, clusterDataOffset );
149  const AliHLTTPCCAGrid &grid = row.fGrid;
150  const int numberOfBins = grid.N();
151 
152  int binCreationMemorySizeNew;
153  if ( ( binCreationMemorySizeNew = numberOfBins * 2 + 6 + row.fNHits ) > binCreationMemorySize ) {
154  binCreationMemorySize = binCreationMemorySizeNew;
155  binCreationMemory.Resize( binCreationMemorySize );
156  }
157 
158  AliHLTArray<unsigned short> c = binCreationMemory; // number of hits in all previous bins
159  AliHLTArray<unsigned short> bins = c + ( numberOfBins + 3 ); // cache for the bin index for every hit in this row
160  AliHLTArray<unsigned short> filled = bins + row.fNHits; // counts how many hits there are per bin
161 
162  for ( unsigned int bin = 0; bin < row.fGrid.N() + 3; ++bin ) {
163  filled[bin] = 0; // initialize filled[] to 0
164  }
165 
166  for ( int hitIndex = 0; hitIndex < row.fNHits; ++hitIndex ) {
167  const int globalHitIndex = clusterDataOffset + hitIndex;
168  const unsigned short bin = row.fGrid.GetBin( data.Y( globalHitIndex ), data.Z( globalHitIndex ) );
169  bins[hitIndex] = bin;
170  ++filled[bin];
171  }
172 
173  unsigned short n = 0;
174  for ( int bin = 0; bin < numberOfBins + 3; ++bin ) {
175  c[bin] = n;
176  n += filled[bin];
177  }
178 
179  for ( int hitIndex = 0; hitIndex < row.fNHits; ++hitIndex ) {
180  const unsigned short bin = bins[hitIndex];
181  assert( bin < numberOfBins + 3 );
182  --filled[bin];
183  const unsigned short ind = c[bin] + filled[bin]; // generate an index for this hit that is >= c[bin] and < c[bin + 1]
184  assert( ind < row.fNHits );
185  const int globalHitIndex = clusterDataOffset + hitIndex;
186 
187  // allows to find the global hit index / coordinates from a global bin sorted hit index
188  row.fClusterDataIndex[ind] = globalHitIndex;
189  row.fHitDataY[ind] = data.Y( globalHitIndex );
190  row.fHitDataZ[ind] = data.Z( globalHitIndex );
191  row.fHitDataIsUsed[ind] = 0;
192  }
193 
194  for ( int i = 0; i < numberOfBins; ++i ) {
195  row.fFirstHitInBin[i] = c[i]; // global bin-sorted hit index
196  }
197  const unsigned short a = c[numberOfBins];
198  // grid.N is <= row.fNHits
199  const int nn = numberOfBins + grid.Ny() + 3;
200  ASSERT( static_cast<int>( gridContentOffset ) + nn - 1 < firstHitInBinSize,
201  static_cast<int>( gridContentOffset ) << " + " << nn << " - 1 < " << firstHitInBinSize);
202  for ( int i = numberOfBins; i < nn; ++i ) {
203  row.fFirstHitInBin[i] = a;
204  }
205 
206  gridContentOffset += nn;
207  hitNumberOffset += NextMultipleOf<VectorAlignment>( row.fNHits );
208  }
209 
210  for ( int rowIndex = data.LastRow() + 1; rowIndex < AliHLTTPCCAParameters::MaxNumberOfRows8; ++rowIndex ) { // later data members of fRows[NRows()] will be used as end pointers for loops over rows
211  AliHLTTPCCARow &row = fRows[rowIndex];
212  row.fGrid.CreateEmpty();
213  row.fNHits = 0;
214  row.fLinkUpData = &linkUpData[hitNumberOffset];
215  row.fLinkDownData = &linkDownData[hitNumberOffset];
216  row.fHitDataY = &hitDataY[hitNumberOffset];
217  row.fHitDataZ = &hitDataZ[hitNumberOffset];
218  row.fHitDataIsUsed = &hitDataIsUsed[hitNumberOffset];
219  row.fClusterDataIndex = &clusterDataIndex[hitNumberOffset];
220  row.fHitWeights = &hitWeights[hitNumberOffset];
221  row.fFirstHitInBin = &firstHitInBin[gridContentOffset];
222  }
223 }
224 
226 {
227  const ushort_v v0( Vc::Zero );
228  const unsigned short *const end = fRows[fParam->NRows()].fHitWeights;
229  for ( unsigned short *mem = fRows[0].fHitWeights; mem < end; mem += v0.Size ) {
230  v0.store( mem );
231  }
232 }
233 
235 {
236  const short_v v0( -1 );
237  const short *const end1 = fRows[fParam->NRows()].fLinkUpData;
238  for ( short *mem = fRows[0].fLinkUpData; mem < end1; mem += v0.Size ) {
239  v0.store( mem );
240  }
241  const short *const end2 = fRows[fParam->NRows()].fLinkDownData;
242  for ( short *mem = fRows[0].fLinkDownData; mem < end2; mem += v0.Size ) {
243  v0.store( mem );
244  }
245 }
246 
248 // for debugging
250 
251 #include "BinaryStoreHelper.h"
252 
253 void AliHLTTPCCASliceData::StoreToFile( FILE *f ) const
254 {
255  BinaryStoreWrite( fNumberOfHits, f );
256 
257  const int size = fMemorySize;
258  assert( size >= 0 );
259  assert( size <= fMemorySize );
260  BinaryStoreWrite( size, f );
261  BinaryStoreWrite( static_cast<Byte_t>( reinterpret_cast<unsigned long>( fMemory ) & 0xff ), f );
262  BinaryStoreWrite( fMemory, size, f );
263 
264  for ( int i = 0; i < 200; ++i ) {
265  fRows[i].StoreToFile( f, fMemory );
266  }
267 }
268 
269 void AliHLTTPCCASliceData::RestoreFromFile( FILE *f )
270 {
271  BinaryStoreRead( fNumberOfHits, f );
272 
273  BinaryStoreRead( fMemorySize, f );
274  Byte_t alignment;
275  BinaryStoreRead( alignment, f );
276  fMemory = new char[fMemorySize + 256];
277  const Byte_t offset = alignment - static_cast<Byte_t>( reinterpret_cast<unsigned long>( fMemory ) & 0xff );
278  BinaryStoreRead( fMemory + offset, fMemorySize, f );
279 
280  for ( int i = 0; i < 200; ++i ) {
281  fRows[i].RestoreFromFile( f, fMemory + offset );
282  }
283 }
float Y(int index) const
int RowOffset(unsigned int rowIndex) const
void Resize(int x)
Definition: AliHLTArray.h:718
void InitFromClusterData(const AliHLTTPCCAClusterData &data)
float Z(int index) const
int Size() const
Definition: AliHLTArray.h:381