]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTIndexGrid.h
HLTcalo module
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTIndexGrid.h
index dddaafb104a4309443ef8936bc50016c40c100e9..0fdda2067249c66d4e19a33b5f37d3a0ae62c6de 100644 (file)
@@ -16,6 +16,8 @@
 #include <iostream>
 #include <iomanip>
 #include <memory>
+#include <cerrno>
+#include <cmath>
 
 template <typename T, typename V>
 class AliHLTIndexGrid {
@@ -51,7 +53,7 @@ class AliHLTIndexGrid {
       fCellDimension=fDimX*fDimY*fDimZ;
       fCells=new AliHLTIndexGridCell[fCellDimension];
       if (fDataDimension<0) fDataDimension=fgkDefaultDataSize;
-      fData=new AliHLTIndexGrid::ValueType[fDataDimension];
+      fData=new V[fDataDimension];
       Clear();
     }
   }
@@ -83,26 +85,42 @@ class AliHLTIndexGrid {
     if (z<0) return 0;
     return (int)(z/fStepZ);
   }
+  int Index(int xindex, int yindex, int zindex) {
+    return xindex*fDimY*fDimZ + yindex*fDimZ + zindex;
+  }
   T GetLowerBoundX(int cell) const {
-    if (fDimX==0 || fDimY==0 ||fDimZ==0) return 0.;
+    if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
     int index=cell/(fDimY*fDimZ);
     return index*fStepX;
   }
+  T GetCenterX(int cell) const {
+    if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
+    return GetLowerBoundX(cell)+fStepX/2;
+  }
   T GetLowerBoundY(int cell) const {
-    if (fDimX==0 || fDimY==0 ||fDimZ==0) return 0.;
+    if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
     int index=cell%(fDimY*fDimZ); index/=fDimZ;
     return index*fStepY;
   }
+  T GetCenterY(int cell) const {
+    if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
+    return GetLowerBoundY(cell)+fStepY/2;
+  }
   T GetLowerBoundZ(int cell) const {
-    if (fDimX==0 || fDimY==0 ||fDimZ==0) return 0.;
+    if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
     int index=cell%(fDimY*fDimZ); index%=fDimZ;
     return index*fStepZ;
   }
+  T GetCenterZ(int cell) const {
+    if (fDimX==0 || fDimY==0 ||fDimZ==0) return (T)0;
+    return GetLowerBoundZ(cell)+fStepZ/2;
+  }
   int GetCellIndex(T x, T y, T z) const {
     return GetXIndex(x)*fDimY*fDimZ + (y<0?0:GetYIndex(y))*fDimZ + (z<0?0:GetZIndex(z));
   }
-  int GetNumberOfSpacePoints(int index, int endIndex) const {
+  int GetNumberOfSpacePoints(int index=0, int endIndex=-1) const {
     if (!fCells) return 0;
+    if (endIndex<0) endIndex=fCellDimension;
     int count=0;
     for (int cell=index; cell<endIndex && cell<fCellDimension && count<fCount; cell++) if (fCells[cell].fCount>0) count+=fCells[cell].fCount;
     return count;
@@ -136,13 +154,14 @@ class AliHLTIndexGrid {
   void Clear(const char* /*option*/="") {
     // clear internal data
     if (fCells) memset(fCells, 0xff, fCellDimension*sizeof(AliHLTIndexGridCell));
-    if (fData) memset(fData, 0, fDataDimension*sizeof(AliHLTIndexGrid::ValueType));
+    if (fData) memset(fData, 0, fDataDimension*sizeof(V));
     fCount=0;
   }
 
   void Print(const char* /*option*/="") {
   // print info
   bool bPrintEmpty=false;
+  ios::fmtflags coutflags=cout.flags(); // backup cout status flags
   cout << "AliHLTIndexGrid: " << (fCells?fCellDimension:0) << " cells" << endl;
   cout << "   x: " << fDimX << " [0," << fMaxX << "]" << endl;
   cout << "   y: " << fDimY << " [0," << fMaxY << "]" << endl;
@@ -151,11 +170,11 @@ class AliHLTIndexGrid {
   if (fCells) {
     for (int i=0; i<fCellDimension; i++) {
       if (!bPrintEmpty && fCells[i].fCount<=0) continue;
-      cout << "     " << setfill(' ') << setw(7) << setprecision(0) << i << " (" 
-          << " " << setw(3) << GetLowerBoundX(i)
-          << " " << setw(3) << GetLowerBoundY(i)
-          << " " << setw(4) << GetLowerBoundZ(i)
-          << "): ";
+      cout << "     " << setfill(' ') << setw(7) << fixed << setprecision(0) << i << " (" 
+         << " " << setw(3) << GetLowerBoundX(i)
+         << " " << setw(3) << GetLowerBoundY(i)
+         << " " << setw(4) << GetLowerBoundZ(i)
+         << "): ";
       cout << setw(3) << fCells[i].fCount << " entries, " << setw(3) << fCells[i].fFilled << " filled";
       cout << "  start index " << setw(5) << fCells[i].fStartIndex;
       cout << endl;
@@ -165,10 +184,11 @@ class AliHLTIndexGrid {
             id!=end(); id++) {
          cout << " 0x" << hex << setw(8) << setfill('0') << id.Data();
        }
-       cout  << dec << endl;
+       cout  << endl;
       }
     }
   }
+  cout.flags(coutflags); // restore the original flags
 }
 
 
@@ -176,12 +196,12 @@ class AliHLTIndexGrid {
   public:
   iterator()
     : fData(NULL) {}
-  iterator(const ValueType* pData)
+  iterator(ValueType* pData)
     : fData(pData) {}
   iterator(const iterator& i)
     : fData(i.fData) {}
     iterator& operator=(const iterator& i)
-      { fData=i.fData; return *this;}
+      { if (this!=&i) {fData=i.fData;} return *this;}
     ~iterator() {fData=NULL;}
 
     bool operator==(const iterator& i) const  {return (fData!=NULL) && (fData==i.fData);}
@@ -196,10 +216,13 @@ class AliHLTIndexGrid {
     iterator& operator+=(int step) {fData+=step; return *this;}
 
     const ValueType& Data() const {return *fData;}
+    ValueType& Data() {return *fData;}
+
+    ValueType operator*() {return *fData;}
 
   protected:
   private:
-    const ValueType* fData; //! data
+    ValueType* fData; //! data
   };
 
   // prepare iterator and end marker
@@ -223,19 +246,25 @@ class AliHLTIndexGrid {
     if (cell<0 || !fCells || cell>=fCellDimension) return fIterator;
     // get the index of the cell
     startIndex=fCells[cell].fStartIndex;
-    if (startIndex<0 || !fData || startIndex>=fDataDimension) return fIterator;
+    if (!fData || startIndex>=fDataDimension) return fIterator;
 
     // get the range end position
     int endCell=cell+1;
     if (x<0) endCell=fCellDimension;
-    else if (y<0) endCell=GetCellIndex(x+fStepX, -1., -1.); // all entries for fixed x
-    else if (z<0) endCell=GetCellIndex(x, y+fStepY, -1.); // all entries for fixed x and y
+    else if (y<0) endCell=GetCellIndex(x+fStepX, (T)-1, (T)-1); // all entries for fixed x
+    else if (z<0) endCell=GetCellIndex(x, y+fStepY, (T)-1); // all entries for fixed x and y
     if (endCell<=cell) {
       // cell index returned is never outside the array
       // so this is a special case where we get to the bounds of the array
       endCell=fCellDimension;
     }
 
+    // find the first cell with content in the range 
+    for (; startIndex<0 && cell<endCell;) {
+      startIndex=fCells[++cell].fStartIndex;
+    }
+    if (startIndex<0) return fIterator;
+
     new (&fIterator) iterator(fData+startIndex);
     fIteratorEnd=fIterator;
     fIteratorEnd+=GetNumberOfSpacePoints(cell, endCell);
@@ -247,6 +276,26 @@ class AliHLTIndexGrid {
     return fIteratorEnd;
   }
 
+  iterator& find(ValueType v) {
+    for (iterator i=begin(); i!=end(); i++) {
+      if (i.Data()==v) {
+       fIterator=i;
+       return fIterator;
+      }
+    }
+    return end();
+  }
+
+  // find cell of entry
+  int FindCell(ValueType v) const {
+    if (!fCells) return -1;
+    for (int cell=0; cell<fCellDimension; cell++)
+      for (int count=0; count<fCells[cell].fCount; count++)
+       if (fData[fCells[cell].fStartIndex+count]==v)
+         return cell;
+    return -1;
+  }
+
   struct AliHLTIndexGridCell {
     int fCount;
     int fFilled;
@@ -276,10 +325,10 @@ class AliHLTIndexGrid {
 
     if (offset>fDataDimension) {
       // grow the data array
-      auto_ptr<AliHLTIndexGrid::ValueType> newArray(new AliHLTIndexGrid::ValueType[offset]);
+      auto_ptr<V> newArray(new V[offset]);
       if (newArray.get()) {
        memcpy(newArray.get(), fData, fDataDimension);
-       memset(newArray.get()+fDataDimension, 0, (offset-fDataDimension)*sizeof(AliHLTIndexGrid::ValueType));
+       memset(newArray.get()+fDataDimension, 0, (offset-fDataDimension)*sizeof(V));
        delete fData;
        fData=newArray.release();
        fDataDimension=offset;