]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDchamberTimeBin.cxx
Fix Coverity
[u/mrichter/AliRoot.git] / TRD / AliTRDchamberTimeBin.cxx
index f1cbee91dc386299c6dcc14cc1ce06fa9c9ef9df..51c0eb1dbca806c7208d074eac205fc2572ca862 100644 (file)
 ///////////////////////////////////////////////////////////////////////////////
 
 #include <TObject.h>
-#include <TROOT.h>
 #include <TMath.h>
-#include <TStopwatch.h>
 #include <TTreeStream.h>
 
 #include "AliLog.h"
-
+#include "AliTRDcluster.h"
 #include "AliTRDgeometry.h"
 #include "AliTRDpadPlane.h"
 #include "AliTRDchamberTimeBin.h"
@@ -49,7 +47,7 @@ ClassImp(AliTRDchamberTimeBin)
 //_____________________________________________________________________________
 AliTRDchamberTimeBin::AliTRDchamberTimeBin(Int_t plane, Int_t stack, Int_t sector, Double_t z0, Double_t zLength)
   :TObject()
-  ,fReconstructor(0x0)
+  ,fkReconstructor(NULL)
   ,fPlane(plane)
   ,fStack(stack)
   ,fSector(sector)
@@ -73,7 +71,7 @@ AliTRDchamberTimeBin::AliTRDchamberTimeBin(Int_t plane, Int_t stack, Int_t secto
 //_____________________________________________________________________________
 AliTRDchamberTimeBin::AliTRDchamberTimeBin(const AliTRDchamberTimeBin &layer):
   TObject()
-  ,fReconstructor(layer.fReconstructor)
+  ,fkReconstructor(layer.fkReconstructor)
   ,fPlane(layer.fPlane)
   ,fStack(layer.fStack)
   ,fSector(layer.fSector)
@@ -107,10 +105,11 @@ AliTRDchamberTimeBin &AliTRDchamberTimeBin::operator=(const AliTRDchamberTimeBin
 //_____________________________________________________________________________
 void AliTRDchamberTimeBin::Clear(const Option_t *) 
 { 
-  for(Int_t it = 0; it<kMaxClustersLayer; it++){
-    if(IsOwner()) delete fClusters[it];
-    fClusters[it] = NULL;
-  } 
+  // Reset the Chamber Timebin
+  if(IsOwner())
+    for(Int_t it = 0; it<kMaxClustersLayer; it++)
+      delete fClusters[it];
+  memset(fClusters,0,kMaxClustersLayer*sizeof(fClusters[0]));
   fN = 0; 
 }
 
@@ -120,7 +119,7 @@ void AliTRDchamberTimeBin::Copy(TObject &o) const
 // Copy method. Performs a deep copy of all data from this object to object o.
   
   AliTRDchamberTimeBin &layer = (AliTRDchamberTimeBin &)o;
-  layer.fReconstructor = fReconstructor;
+  layer.fkReconstructor = fkReconstructor;
   layer.fPlane       = fPlane;
   layer.fStack       = fStack;
   layer.fSector      = fSector;
@@ -216,7 +215,7 @@ void AliTRDchamberTimeBin::Bootstrap(const AliTRDReconstructor *rec, Int_t det)
 // Reinitialize all data members from the clusters array
 // It has to be used after reading from disk
 
-  fReconstructor = rec;
+  fkReconstructor = rec;
   fPlane  = AliTRDgeometry::GetLayer(det);
   fStack  = AliTRDgeometry::GetStack(det);
   fSector = AliTRDgeometry::GetSector(det);
@@ -247,18 +246,18 @@ void AliTRDchamberTimeBin::BuildIndices(Int_t iter)
   Int_t nClStack = 0;                                  // Internal counter
   for(Int_t i = 0; i < fN; i++){
     if(fClusters[i]->IsUsed() || fClusters[i]->IsShared()){
-      fClusters[i] = 0x0;
+      fClusters[i] = NULL;
       fIndex[i] = 0xffff;
     } else nClStack++;
   }
-  if(nClStack > kMaxClustersLayer) AliWarning(Form("Number of clusters in stack %d exceed buffer size %d. Truncating.", nClStack, kMaxClustersLayer));
+  if(nClStack > kMaxClustersLayer) AliInfo(Form("Number of clusters in stack %d exceed buffer size %d. Truncating.", nClStack, kMaxClustersLayer));
     
   // Nothing in this time bin. Reset indexes 
   if(!nClStack){
     fN = 0;
-    memset(&fPositions[0], 0xff, sizeof(UChar_t) * kMaxRows);
-    memset(&fClusters[0], 0x0, sizeof(AliTRDcluster*) * kMaxClustersLayer);
-    memset(&fIndex[0], 0xffff, sizeof(UInt_t) * kMaxClustersLayer);
+    memset(&fPositions[0], 0, sizeof(UChar_t) * kMaxRows);
+    memset(&fClusters[0], 0, sizeof(AliTRDcluster*) * kMaxClustersLayer);
+    memset(&fIndex[0], 0, sizeof(UInt_t) * kMaxClustersLayer);
     return;
   }
   
@@ -268,7 +267,7 @@ void AliTRDchamberTimeBin::BuildIndices(Int_t iter)
   nClStack = 0;
   // Defining iterators
   AliTRDcluster **fcliter = &fClusters[0], **hcliter = &helpCL[0]; UInt_t *finditer = &fIndex[0], *hinditer = &helpInd[0];
-  AliTRDcluster *tmpcl = 0x0;
+  AliTRDcluster *tmpcl = NULL;
   for(Int_t i = 0; i < TMath::Min(fN, kMaxClustersLayer); i++){
     if(!(tmpcl = *(fcliter++))){
        finditer++;
@@ -276,7 +275,7 @@ void AliTRDchamberTimeBin::BuildIndices(Int_t iter)
     }
     *(hcliter++)  = tmpcl;
     *(hinditer++) = *finditer;
-    tmpcl = 0x0;
+    tmpcl = NULL;
     *(finditer++) = 0xffff;
     nClStack++;
   }
@@ -288,13 +287,17 @@ void AliTRDchamberTimeBin::BuildIndices(Int_t iter)
   // Reset Positions array
   memset(fPositions, 0, sizeof(UChar_t)*kMaxRows);
   AliTRDcluster **cliter = &helpCL[0]; // Declare iterator running over the whole array
+  const AliTRDrecoParam* const recoParam = fkReconstructor->GetRecoParam(); //the dynamic cast in GetRecoParam is slow, so caching the pointer to it
+  Int_t tb(-1);
   for(Int_t i = 0; i < fN; i++){
     // boundary check
     AliTRDcluster *cl = *(cliter++);
     UChar_t rowIndex = cl->GetPadRow();
+    if(tb<0) tb=cl->GetLocalTimeBin();
     // Insert Leaf
-    Int_t pos = FindYPosition(cl->GetY(), rowIndex, i);
-    if(pos == -1){             // zbin is empty;
+    Int_t pos = FindYPosition(cl->GetY(), rowIndex, nClStack);
+    if(pos == -2) continue;   // zbin error;
+    else if(pos == -1) {    // zbin is empty;
       Int_t upper = (rowIndex == fNRows - 1) ? nClStack : fPositions[rowIndex + 1];
       memmove(fClusters + upper + 1, fClusters + upper, (sizeof(AliTRDcluster *))*(nClStack-upper));
       memmove(fIndex + upper + 1, fIndex + upper, (sizeof(UInt_t))*(nClStack-upper));
@@ -317,14 +320,15 @@ void AliTRDchamberTimeBin::BuildIndices(Int_t iter)
     fX += cl->GetX(); 
     
     // Debug Streaming
-    if(fReconstructor->GetStreamLevel(AliTRDReconstructor::kTracker) >= 3){
-      TTreeSRedirector &cstream = *fReconstructor->GetDebugStream(AliTRDReconstructor::kTracker);
+    if(recoParam->GetStreamLevel(AliTRDrecoParam::kTracker) >= 3){
+      AliTRDcluster dcl(*cl);
+      TTreeSRedirector &cstream = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
       cstream << "BuildIndices"
       << "Plane="    << fPlane
       << "Stack="    << fStack
       << "Sector="   << fSector
       << "Iter="     << iter
-      << "C.="       << cl
+      << "C.="       << &dcl
       << "rowIndex=" << rowIndex
       << "\n";
     }
@@ -332,8 +336,17 @@ void AliTRDchamberTimeBin::BuildIndices(Int_t iter)
 
 //     AliInfo("Positions");
 //     for(int ir=0; ir<fNRows; ir++) printf("pos[%d] %d\n", ir, fPositions[ir]);
-
-  fX /= fN;
+  if(nClStack < fN){
+    AliWarning(Form("Found %d out of %d clusters outside in ChamberTimeBin[%02d_%d_%d|%2d]", fN-nClStack, fN, fSector, fStack, fPlane, tb));
+    fN =  nClStack;
+    if(!fN){ // Nothing left in this time bin. Reset indexes
+      memset(&fPositions[0], 0, sizeof(UChar_t) * kMaxRows);
+      memset(&fClusters[0], 0, sizeof(AliTRDcluster*) * kMaxClustersLayer);
+      memset(&fIndex[0], 0, sizeof(UInt_t) * kMaxClustersLayer);
+      return;
+    }
+  }
+  if(fN) fX /= fN;
 }
 
 //_____________________________________________________________________________
@@ -379,6 +392,10 @@ Int_t AliTRDchamberTimeBin::FindYPosition(Double_t y, UChar_t z, Int_t nClusters
 // Index of the nearest left cluster in the StackLayer indexing (-1 if no clusters are found)
 //
 
+  if(z>=fNRows){ // check pad row of cluster
+    AliDebug(1, Form("Row[%2d] outside range [0 %2d] in %02d_%d_%d.", z, fNRows, fSector, fStack, fPlane));
+    return -2;
+  }
   Int_t start = fPositions[z];                 // starting Position of the bin
   Int_t upper = (Int_t)((z != fNRows - 1) ? fPositions[z+1] : nClusters); // ending Position of the bin 
   Int_t end = upper - 1; // ending Position of the bin 
@@ -411,7 +428,7 @@ Int_t AliTRDchamberTimeBin::FindNearestYCluster(Double_t y, UChar_t z) const
 //
 
   Int_t position = FindYPosition(y, z, fN);
-  if(position == -1) return position; // bin empty
+  if(position == -2 || position == -1) return position; // bin empty
   // FindYPosition always returns the left Neighbor. We don't know if the left or the right Neighbor is nearest
   // to the Reference y-position, so test both
   Int_t upper = (Int_t)((z < fNRows-1) ? fPositions[z+1] : fN); // ending Position of the bin
@@ -512,7 +529,7 @@ Int_t AliTRDchamberTimeBin::SearchNearestCluster(Double_t y, Double_t z, Double_
 }
 
 //_____________________________________________________________________________
-void AliTRDchamberTimeBin::BuildCond(AliTRDcluster *cl, Double_t *cond, UChar_t Layer, Double_t theta, Double_t phi)
+void AliTRDchamberTimeBin::BuildCond(AliTRDcluster * const cl, Double_t *cond, UChar_t Layer, Double_t theta, Double_t phi)
 {
 // Helper function to calculate the area where to expect a cluster in THIS
 // layer. 
@@ -536,7 +553,7 @@ void AliTRDchamberTimeBin::BuildCond(AliTRDcluster *cl, Double_t *cond, UChar_t
 //End_Html
 //
 
-  if(!fReconstructor){
+  if(!fkReconstructor){
     AliError("Reconstructor not set.");
     return;
   }
@@ -544,18 +561,18 @@ void AliTRDchamberTimeBin::BuildCond(AliTRDcluster *cl, Double_t *cond, UChar_t
   if(Layer == 0){
     cond[0] = cl->GetY();                      // center: y-Direction
     cond[1] = cl->GetZ();                      // center: z-Direction
-    cond[2] = fReconstructor->GetRecoParam()->GetMaxPhi()   * (cl->GetX() - GetX()) + 1.0;                     // deviation: y-Direction
-    cond[3] = fReconstructor->GetRecoParam()->GetMaxTheta() * (cl->GetX() - GetX()) + 1.0;                     // deviation: z-Direction
+    cond[2] = fkReconstructor->GetRecoParam()->GetMaxPhi()   * (cl->GetX() - GetX()) + 1.0;                    // deviation: y-Direction
+    cond[3] = fkReconstructor->GetRecoParam()->GetMaxTheta() * (cl->GetX() - GetX()) + 1.0;                    // deviation: z-Direction
   } else {
     cond[0] = cl->GetY() + phi   * (GetX() - cl->GetX()); 
     cond[1] = cl->GetZ() + theta * (GetX() - cl->GetX());
-    cond[2] = fReconstructor->GetRecoParam()->GetRoad0y() + phi;
-    cond[3] = fReconstructor->GetRecoParam()->GetRoad0z();
+    cond[2] = fkReconstructor->GetRecoParam()->GetRoad0y() + phi;
+    cond[3] = fkReconstructor->GetRecoParam()->GetRoad0z();
   }
 }
 
 //_____________________________________________________________________________
-void AliTRDchamberTimeBin::GetClusters(Double_t *cond, Int_t *index, Int_t& ncl, Int_t BufferSize)
+void AliTRDchamberTimeBin::GetClusters(const Double_t * const cond, Int_t *index, Int_t& ncl, Int_t BufferSize)
 {
 // Finds all clusters situated in this layer inside a rectangle  given by the center an ranges.
 //
@@ -599,7 +616,7 @@ void AliTRDchamberTimeBin::GetClusters(Double_t *cond, Int_t *index, Int_t& ncl,
     //AliInfo(Form("z[%d] y [%d %d]", z, fPositions[z], upper));
     for(Int_t y = fPositions[z]; y < (Int_t)upper; y++){
       if(ncl == BufferSize){
-        AliWarning("Buffer size riched. Some clusters may be lost.");
+        AliDebug(1, Form("Buffer size [%d] riched. Some clusters may be lost.", BufferSize));
         return;        //Buffer filled
       }
       
@@ -630,11 +647,11 @@ AliTRDcluster *AliTRDchamberTimeBin::GetNearestCluster(Double_t *cond)
 // successfull) by the help of the method FindNearestCluster
   
   
-  Double_t maxroad  = fReconstructor->GetRecoParam()->GetRoad2y();
-  Double_t maxroadz = fReconstructor->GetRecoParam()->GetRoad2z();
+  Double_t maxroad  = fkReconstructor->GetRecoParam()->GetRoad2y();
+  Double_t maxroadz = fkReconstructor->GetRecoParam()->GetRoad2z();
   
   Int_t index = SearchNearestCluster(cond[0],cond[1],maxroad,maxroadz);
-  AliTRDcluster *returnCluster = 0x0;
+  AliTRDcluster *returnCluster = NULL;
   if(index != -1) returnCluster = (AliTRDcluster *) fClusters[index];
   return returnCluster;
 }