]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/BASE/AliHLTSpacePointContainer.h
bugfix: initialization of data array after growing it was out of array bounds
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTSpacePointContainer.h
CommitLineData
488caaa0 1//-*- Mode: C++ -*-
2// $Id$
3#ifndef ALIHLTSPACEPOINTCONTAINER_H
4#define ALIHLTSPACEPOINTCONTAINER_H
5//* This file is property of and copyright by the ALICE HLT Project *
6//* ALICE Experiment at CERN, All rights reserved. *
7//* See cxx source for full Copyright notice *
8
9/// @file AliHLTSpacePointContainer.h
10/// @author Matthias Richter
11/// @date 2011-04-29
12/// @brief Base helper class for handling of HLT space point data blocks
13///
14
15#include <vector>
86e69fe5 16#include <cmath>
488caaa0 17#include <TObject.h>
18#include "AliHLTLogging.h"
19#include "AliHLTDataTypes.h"
20#include "AliHLTStdIncludes.h"
21
06b152d9 22class AliHLTDataDeflater;
488caaa0 23class TArrayC;
27d45f0c 24class TH1;
6fa220b4 25class TTree;
488caaa0 26
27/**
28 * @class AliHLTSpacePointContainer
29 * Base class of helper classes for space point data blocks.
30 * The class implements a couple of interface methods to be commonly used
31 * for space point data blocks.
32 *
33 * @ingroup alihlt_base
34 */
35class AliHLTSpacePointContainer : public TObject, public AliHLTLogging
36{
37 public:
38 /// standard constructor
39 AliHLTSpacePointContainer();
40 /// copy constructor
41 AliHLTSpacePointContainer(const AliHLTSpacePointContainer&);
42 /// assignment operator
43 AliHLTSpacePointContainer& operator=(const AliHLTSpacePointContainer&);
44
45 /// destructor
46 ~AliHLTSpacePointContainer();
47
86e69fe5 48 //////////////////////////////////////////////////////////////////////////
49 //
50 // helper classes for access of space points within a grid
51 //
52 struct AliHLTSpacePointCell {
53 int fCount;
54 int fFilled;
55 int fStartIndex;
56 };
57
58 class AliHLTSpacePointGrid {
59 public:
60 AliHLTSpacePointGrid(float maxX, float stepX,
61 float maxY, float stepY,
62 float maxZ, float stepZ,
63 int initialDataSize=-1);
64 virtual ~AliHLTSpacePointGrid();
65
66 // for now array of spacepoint ids
67 typedef AliHLTUInt32_t ValueType;
68
69 int GetDimensionX() const {return fDimX;}
70 int GetDimensionY() const {return fDimY;}
71 int GetDimensionZ() const {return fDimZ;}
72 int GetXIndex(float x) const {
73 if (x>fMaxX) return fDimX-1;
74 if (x<0) return 0;
75 return (int)floor(x/fStepX);
76 }
77 int GetYIndex(float y) const {
78 if (y>fMaxY) return fDimY-1;
79 if (y<0) return 0;
80 return (int)floor(y/fStepY);
81 }
82 int GetZIndex(float z) const {
83 if (z>fMaxZ) return fDimZ-1;
84 if (z<0) return 0;
85 return (int)floor(z/fStepZ);
86 }
87 float GetLowerBoundX(int cell) const {
88 if (fDimX==0 || fDimY==0 ||fDimZ==0) return 0.;
89 int index=cell/(fDimY*fDimZ);
90 return index*fStepX;
91 }
92 float GetLowerBoundY(int cell) const {
93 if (fDimX==0 || fDimY==0 ||fDimZ==0) return 0.;
94 int index=cell%(fDimY*fDimZ); index/=fDimZ;
95 return index*fStepY;
96 }
97 float GetLowerBoundZ(int cell) const {
98 if (fDimX==0 || fDimY==0 ||fDimZ==0) return 0.;
99 int index=cell%(fDimY*fDimZ); index%=fDimZ;
100 return index*fStepZ;
101 }
102 int GetCellIndex(float x, float y, float z) const {
103 return GetXIndex(x)*fDimY*fDimZ + (y<0?0:GetYIndex(y))*fDimZ + (z<0?0:GetZIndex(z));
104 }
105 int GetNumberOfSpacePoints(int index, int endIndex) const {
106 if (!fCells) return 0;
107 int count=0;
108 for (int cell=index; cell<endIndex && cell<fCellDimension && count<fCount; cell++) if (fCells[cell].fCount>0) count+=fCells[cell].fCount;
109 return count;
110 }
111
112 // increment counter of the cell where the spacepoint is
113 int CountSpacePoint(float x, float y, float z);
114
115 // add spacepoint, all spacepoints must have been counted before
116 int AddSpacePoint(ValueType t, float x, float y, float z);
117
118 void Clear(const char* option="");
119 void Print(const char* option="");
120
121 class iterator {
122 public:
123 iterator()
124 : fData(NULL) {}
125 iterator(const ValueType* pData)
126 : fData(pData) {}
127 iterator(const iterator& i)
128 : fData(i.fData) {}
129 iterator& operator=(const iterator& i)
130 { fData=i.fData; return *this;}
131 ~iterator() {fData=NULL;}
132
133 bool operator==(const iterator& i) const {return (fData!=NULL) && (fData==i.fData);}
134 bool operator!=(const iterator& i) const {return (fData!=NULL) && (fData!=i.fData);}
135 // prefix operators
136 iterator& operator++() {fData++; return *this;}
137 iterator& operator--() {fData--; return *this;}
138 // postfix operators
139 iterator operator++(int) {iterator i(*this); fData++; return i;}
140 iterator operator--(int) {iterator i(*this); fData--; return i;}
141
142 iterator& operator+=(int step) {fData+=step; return *this;}
143
144 const ValueType& Data() const {return *fData;}
145
146 protected:
147 private:
148 const ValueType* fData; //! data
149 };
150
151 // prepare iterator and end marker
152 iterator& begin(float x=-1., float y=-1., float z=-1.) {
153 fIterator.~iterator();
154 fIteratorEnd.~iterator();
155
156 int startIndex=0;
157 if (x<0) {
158 // get all data
159 if (fData) {
160 new (&fIterator) iterator(fData);
161 fIteratorEnd=fIterator;
162 fIteratorEnd+=fCount;
163 }
164 return fIterator;
165 }
166
167 // only search for the start index if specific x selected
168 int cell=GetCellIndex(x, y, z);
169 if (cell<0 || !fCells || cell>=fCellDimension) return fIterator;
170 // get the index of the cell
171 startIndex=fCells[cell].fStartIndex;
172 if (startIndex<0 || !fData || startIndex>=fDataDimension) return fIterator;
173
174 // get the range end position
175 int endCell=cell+1;
176 if (x<0) endCell=fCellDimension;
177 else if (y<0) endCell=GetCellIndex(x+fStepX, -1., -1.); // all entries for fixed x
178 else if (z<0) endCell=GetCellIndex(x, y+fStepY, -1.); // all entries for fixed x and y
179 if (endCell<=cell) {
180 // cell index returned is never outside the array
181 // so this is a special case where we get to the bounds of the array
182 endCell=fCellDimension;
183 }
184
185 new (&fIterator) iterator(fData+startIndex);
186 fIteratorEnd=fIterator;
187 fIteratorEnd+=GetNumberOfSpacePoints(cell, endCell);
188 return fIterator;
189 }
190
191 // get loop end marker
192 iterator& end() {
193 return fIteratorEnd;
194 }
195
196 protected:
197 private:
198 // standard constructor prohibited
199 AliHLTSpacePointGrid();
200 // copy constructor prohibited
201 AliHLTSpacePointGrid(const AliHLTSpacePointGrid&);
202 // assignment operator prohibited
203 AliHLTSpacePointGrid& operator=(const AliHLTSpacePointGrid&);
204
205 int IndexCells();
206
207 float fMaxX;
208 float fStepX;
209 float fMaxY;
210 float fStepY;
211 float fMaxZ;
212 float fStepZ;
213
214 int fDimX;
215 int fDimY;
216 int fDimZ;
217
218 AliHLTSpacePointCell* fCells; //! cell array
219 int fCellDimension; //! size of cell array
220 ValueType* fData; //! spacepoint data
221 int fDataDimension; //! size of spacepoint data
222 int fCount;
223
224 iterator fIterator; //! iterator
225 iterator fIteratorEnd; //! end marker iterator
226
fc7ea273 227 static const int fgkDefaultDataSize; //! the default data size
228
86e69fe5 229 ClassDef(AliHLTSpacePointGrid, 0)
230 };
231
232 //////////////////////////////////////////////////////////////////////////
233 //
234 // interface functions
235 //
236
488caaa0 237 /// add input block to the collection
238 virtual int AddInputBlock(const AliHLTComponentBlockData* pDesc)=0;
86e69fe5 239 virtual int PopulateAccessGrid(AliHLTSpacePointGrid* /*pGrid*/, AliHLTUInt32_t /*mask*/) const {return -ENOSYS;}
240 virtual const AliHLTSpacePointGrid* GetAccessGrid(AliHLTUInt32_t /*mask*/) const {return NULL;}
488caaa0 241
e1e03704 242 virtual int GetNumberOfSpacePoints() const;
243 virtual bool Check(AliHLTUInt32_t clusterID) const;
488caaa0 244 virtual int GetClusterIDs(vector<AliHLTUInt32_t>& tgt) const = 0;
54ff4c01 245 virtual const vector<AliHLTUInt32_t>* GetClusterIDs(AliHLTUInt32_t /*mask*/) {return NULL;}
488caaa0 246 virtual float GetX(AliHLTUInt32_t clusterID) const = 0;
247 virtual float GetXWidth(AliHLTUInt32_t clusterID) const = 0;
248 virtual float GetY(AliHLTUInt32_t clusterID) const = 0;
249 virtual float GetYWidth(AliHLTUInt32_t clusterID) const = 0;
250 virtual float GetZ(AliHLTUInt32_t clusterID) const = 0;
251 virtual float GetZWidth(AliHLTUInt32_t clusterID) const = 0;
252 virtual float GetCharge(AliHLTUInt32_t clusterID) const = 0;
37c3ba28 253 virtual float GetMaxSignal(AliHLTUInt32_t /*clusterID*/) const {return 0.0;}
27d45f0c 254 virtual float GetPhi(AliHLTUInt32_t /*clusterID*/) const {return 0.0;}
488caaa0 255
54ff4c01 256 /// create a collection of clusters for a space point mask
257 virtual AliHLTSpacePointContainer* SelectByMask(AliHLTUInt32_t mask, bool bAlloc=false) const;
258
488caaa0 259 /// create a collection of clusters for a specific track
260 virtual AliHLTSpacePointContainer* SelectByTrack(int trackId, bool bAlloc=false) const;
261
262 /// create a collection of clusters for a specific MC track
263 virtual AliHLTSpacePointContainer* SelectByMC(int mcId, bool bAlloc=false) const;
264
265 /// create a collection of all used clusters
266 virtual AliHLTSpacePointContainer* UsedClusters(bool bAlloc=false) const;
267
268 /// create a collection of all unused clusters
269 virtual AliHLTSpacePointContainer* UnusedClusters(bool bAlloc=false) const;
270
271 int MarkUsed(AliHLTUInt32_t clusterID) {return MarkUsed(&clusterID, sizeof(clusterID));}
272 virtual int MarkUsed(const AliHLTUInt32_t* clusterIDs, int arraySize);
273
274 int SetTrackID(int trackID, AliHLTUInt32_t clusterID) {
275 return SetTrackID(trackID, &clusterID, sizeof(clusterID));
276 }
277 virtual int SetTrackID(int trackID, const AliHLTUInt32_t* clusterIDs, int arraySize);
278
e1e03704 279 virtual int GetTrackID(AliHLTUInt32_t /*clusterID*/) const {return -1;}
280
488caaa0 281 int SetMCID(int mcID, AliHLTUInt32_t clusterID) {
282 return SetMCID(mcID, &clusterID, sizeof(clusterID));
283 }
284 virtual int SetMCID(int clusterID, const AliHLTUInt32_t* clusterIDs, int arraySize);
285
da9919a1 286 /// write blocks to HLT component output
06b152d9 287 virtual int Write(AliHLTUInt8_t* outputPtr, AliHLTUInt32_t size,
288 vector<AliHLTComponentBlockData>& outputBlocks,
289 AliHLTDataDeflater* /*pDeflater*/,
290 const char* option="") const {
291 return Write(outputPtr, size, outputBlocks, option);
292 }
293
294 /// write blocks to HLT component output: old function definition for backward compatibility
da9919a1 295 virtual int Write(AliHLTUInt8_t* /*outputPtr*/, AliHLTUInt32_t /*size*/,
296 vector<AliHLTComponentBlockData>& /*outputBlocks*/,
297 const char* /*option*/="") const {return 0;}
298
488caaa0 299 /// add input block from file to collection
300 int AddInputBlock(const char* filename, AliHLTComponentDataType dt, unsigned specification);
301
302 /// add input block from list of blank separated files to collection
303 int AddInputBlocks(const char* filenames, AliHLTComponentDataType dt);
304
3488f70b 305 /// alloc memory for a space point data block
306 AliHLTUInt8_t* Alloc(int size);
307
488caaa0 308 /// inherited from TObject: clear the object and reset pointer references
309 virtual void Clear(Option_t * /*option*/ ="");
310
311 /// inherited from TObject
312 virtual void Print(Option_t *option="") const;
313
314 virtual void Print(ostream& out, Option_t *option="") const;
315
5f195a83 316 void Draw(Option_t *option);
317
27d45f0c 318 TH1* DrawProjection(const char* plane) const {
319 vector<AliHLTUInt32_t> selection; // empty list -> no selection
320 return DrawProjection(plane, selection);
321 }
322
323 TH1* DrawProjection(const char* plane, AliHLTUInt32_t specification) const {
324 vector<AliHLTUInt32_t> selection; selection.push_back(specification);
325 return DrawProjection(plane, selection);
326 }
327
328 TH1* DrawProjection(const char* plane, const vector<AliHLTUInt32_t>& selection) const;
329
6fa220b4 330 TTree* FillTree(const char* name, const char* title="");
331
488caaa0 332 protected:
333
334 private:
335 vector<TArrayC*> fBuffers; //! buffers of loaded files
336
337 ClassDef(AliHLTSpacePointContainer, 0)
338};
339
340ostream& operator<<(ostream &out, const AliHLTSpacePointContainer& c);
341
342#endif