]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/TPCLib/tracking-ca/AliHLTTPCCAMerger.cxx
bug fix: reconstruction crash when the output buffer size exceed
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCAMerger.cxx
index 10b381a067088ce4f140ed1005db986fb6836ad7..94c56f4e9bd6e0491d00a4df923fd5b1de7dbdc9 100644 (file)
 //                                                                          *
 //***************************************************************************
 
-
+#include <stdio.h>
 #include "AliHLTTPCCASliceTrack.h"
 #include "AliHLTTPCCATracker.h"
-#include "AliHLTTPCCAGBTrack.h"
 #include "AliHLTTPCCATrackParam.h"
 
 #include "AliHLTTPCCAMerger.h"
 #include "AliHLTTPCCATrackLinearisation.h"
 
 
-class AliHLTTPCCAMerger::AliHLTTPCCAClusterInfo
-{
-
-  public:
-
-    unsigned int  ISlice()    const { return fISlice;    }
-    unsigned int  IRow()      const { return fIRow;      }
-    unsigned int  IClu()      const { return fIClu;      }
-    UChar_t PackedAmp() const { return fPackedAmp; }
-    float X()         const { return fX;         }
-    float Y()         const { return fY;         }
-    float Z()         const { return fZ;         }
-    float Err2Y()     const { return fErr2Y;     }
-    float Err2Z()     const { return fErr2Z;     }
-
-    void SetISlice    ( unsigned int v  ) { fISlice    = v; }
-    void SetIRow      ( unsigned int v  ) { fIRow      = v; }
-    void SetIClu      ( unsigned int v  ) { fIClu      = v; }
-    void SetPackedAmp ( UChar_t v ) { fPackedAmp = v; }
-    void SetX         ( float v ) { fX         = v; }
-    void SetY         ( float v ) { fY         = v; }
-    void SetZ         ( float v ) { fZ         = v; }
-    void SetErr2Y     ( float v ) { fErr2Y     = v; }
-    void SetErr2Z     ( float v ) { fErr2Z     = v; }
-
-  private:
-
-    unsigned int fISlice;            // slice number
-    unsigned int fIRow;              // row number
-    unsigned int fIClu;              // cluster number
-    UChar_t fPackedAmp; // packed cluster amplitude
-    float fX;                // x position (slice coord.system)
-    float fY;                // y position (slice coord.system)
-    float fZ;                // z position (slice coord.system)
-    float fErr2Y;            // Squared measurement error of y position
-    float fErr2Z;            // Squared measurement error of z position
-};
-
-
 class AliHLTTPCCAMerger::AliHLTTPCCASliceTrackInfo
 {
 
@@ -260,7 +220,9 @@ void AliHLTTPCCAMerger::UnpackSlices()
     for ( int itr = 0; itr < slice.NTracks(); itr++ ) {
 
       const AliHLTTPCCASliceTrack &sTrack = slice.Track( itr );
-      AliHLTTPCCATrackParam t0 = sTrack.Param();
+      AliHLTTPCCATrackParam t0;
+         t0.InitParam();
+         t0.SetParam(sTrack.Param());
       int nCluNew = 0;
 
       for ( int iTrClu = 0; iTrClu < sTrack.NClusters(); iTrClu++ ) {
@@ -271,15 +233,15 @@ void AliHLTTPCCAMerger::UnpackSlices()
         int ic = sTrack.FirstClusterRef() + iTrClu;
 
         clu.SetISlice( iSlice );
-        clu.SetIRow( AliHLTTPCCADataCompressor::IDrc2IRow( slice.ClusterIDrc( ic ) ) );
-        clu.SetIClu( AliHLTTPCCADataCompressor::IDrc2IClu( slice.ClusterIDrc( ic ) ) );
-        clu.SetPackedAmp( slice.ClusterPackedAmp( ic ) );
+        clu.SetIRow( slice.ClusterRow( ic ) );
+        clu.SetId( slice.ClusterId( ic ) );
+        clu.SetPackedAmp( 0 );
         float2 yz = slice.ClusterUnpackedYZ( ic );
         clu.SetX( slice.ClusterUnpackedX( ic ) );
         clu.SetY( yz.x );
         clu.SetZ( yz.y );
 
-        if ( !t0.TransportToX( fSliceParam.RowX( clu.IRow() ), fSliceParam.GetBz( t0 ), .999 ) ) continue;
+        if ( !t0.TransportToX( clu.X(), fSliceParam.GetBz( t0 ), .999 ) ) continue;
 
         float err2Y, err2Z;
         fSliceParam.GetClusterErrors2( clu.IRow(), clu.Z(), t0.SinPhi(), t0.GetCosPhi(), t0.DzDs(), err2Y, err2Z );
@@ -297,7 +259,9 @@ void AliHLTTPCCAMerger::UnpackSlices()
       int nHits = nCluNew;
       for ( int i = 0; i < nHits; i++ ) hits[i] = nClustersCurrent + i;
 
-      AliHLTTPCCATrackParam startPoint = sTrack.Param();
+      AliHLTTPCCATrackParam startPoint;
+         startPoint.InitParam();
+         startPoint.SetParam(sTrack.Param());
       AliHLTTPCCATrackParam endPoint = startPoint;
       float startAlpha = fSliceParam.Alpha( iSlice );
       float endAlpha = startAlpha;
@@ -338,7 +302,8 @@ void AliHLTTPCCAMerger::UnpackSlices()
 
 bool AliHLTTPCCAMerger::FitTrack( AliHLTTPCCATrackParam &T, float &Alpha,
                                   AliHLTTPCCATrackParam t0, float Alpha0,
-                                  int hits[], int &NTrackHits, bool dir )
+                                  int hits[], int &NTrackHits, bool dir, bool final,
+                                  AliHLTTPCCAClusterInfo *infoArray )
 {
   // Fit the track
 
@@ -347,6 +312,11 @@ bool AliHLTTPCCAMerger::FitTrack( AliHLTTPCCATrackParam &T, float &Alpha,
   AliHLTTPCCATrackLinearisation l( t0 );
 
   bool first = 1;
+  bool doErrors = 1;
+  if ( !infoArray ) {
+    infoArray = fClusterInfos;
+    doErrors = 0;
+  }
 
   t.CalculateFitParameters( fitPar );
 
@@ -356,7 +326,7 @@ bool AliHLTTPCCAMerger::FitTrack( AliHLTTPCCATrackParam &T, float &Alpha,
   for ( int ihit = 0; ihit < NTrackHits; ihit++ ) {
 
     int jhit = dir ? ( NTrackHits - 1 - ihit ) : ihit;
-    AliHLTTPCCAClusterInfo &h = fClusterInfos[hits[jhit]];
+    AliHLTTPCCAClusterInfo &h = infoArray[hits[jhit]];
 
     int iSlice = h.ISlice();
 
@@ -393,24 +363,24 @@ bool AliHLTTPCCAMerger::FitTrack( AliHLTTPCCATrackParam &T, float &Alpha,
       t.CalculateFitParameters( fitPar );
     }
 
-    if ( !t.Filter( h.Y(), h.Z(), h.Err2Y(), h.Err2Z() ) ) continue;
+    float err2Y = h.Err2Y();
+    float err2Z = h.Err2Z();
+    if ( doErrors ) fSliceParam.GetClusterErrors2( h.IRow(), h.Z(), l.SinPhi(), l.CosPhi(), l.DzDs(), err2Y, err2Z );
+    if( !final ){
+      err2Y*= fSliceParam.ClusterError2CorrectionY();
+      err2Z*= fSliceParam.ClusterError2CorrectionZ();
+    }
+
+    if ( !t.Filter( h.Y(), h.Z(), err2Y, err2Z ) ) continue;
 
     first = 0;
 
     hitsNew[nHitsNew++] = hits[jhit];
   }
 
-  if ( CAMath::Abs( t.QPt() ) < 1.e-8 ) t.SetQPt( 1.e-8 );
-
-  bool ok = 1;
-
-  const float *c = t.Cov();
-  for ( int i = 0; i < 15; i++ ) ok = ok && finite( c[i] );
-  for ( int i = 0; i < 5; i++ ) ok = ok && finite( t.Par()[i] );
-  ok = ok && ( t.GetX() > 50 );
+  if ( CAMath::Abs( t.QPt() ) < 1.e-4 ) t.SetQPt( 1.e-4 );
 
-  if ( c[0] <= 0 || c[2] <= 0 || c[5] <= 0 || c[9] <= 0 || c[14] <= 0 ) ok = 0;
-  if ( c[0] > 5. || c[2] > 5. || c[5] > 2. || c[9] > 2 || c[14] > 2. ) ok = 0;
+  bool ok = t.CheckNumericalQuality();
 
   if ( CAMath::Abs( t.SinPhi() ) > .99 ) ok = 0;
   else if ( l.CosPhi() >= 0 ) t.SetSignCosPhi( 1 );
@@ -443,7 +413,7 @@ float AliHLTTPCCAMerger::GetChi2( float x1, float y1, float a00, float a10, floa
 
   float mS[3] = { mSi[2], -mSi[1], mSi[0] };
 
-  return TMath::Abs( ( ( mS[0]*d[0] + mS[1]*d[1] )*d[0]
+  return AliHLTTPCCAMath::Abs( ( ( mS[0]*d[0] + mS[1]*d[1] )*d[0]
                        + ( mS[1]*d[0] + mS[2]*d[1] )*d[1] ) / s / 2 );
 
 }
@@ -499,6 +469,7 @@ void AliHLTTPCCAMerger::MakeBorderTracks( int iSlice, int iBorder, AliHLTTPCCABo
     if ( do0 ) {
       AliHLTTPCCABorderTrack &b = B[nB];
       b.SetX( t0.GetX() );
+      
       if ( t0.TransportToX( x0, fSliceParam.GetBz( t0 ), maxSin ) ) {
         b.SetOK( 1 );
         b.SetTrackID( itr );
@@ -511,6 +482,7 @@ void AliHLTTPCCAMerger::MakeBorderTracks( int iSlice, int iBorder, AliHLTTPCCABo
     if ( do1 ) {
       AliHLTTPCCABorderTrack &b = B[nB];
       b.SetX( t1.GetX() );
+      
       if ( t1.TransportToX( x0, fSliceParam.GetBz( t1 ), maxSin ) ) {
         b.SetOK( 1 );
         b.SetTrackID( itr );
@@ -527,12 +499,14 @@ void AliHLTTPCCAMerger::MakeBorderTracks( int iSlice, int iBorder, AliHLTTPCCABo
 
 
 
-void AliHLTTPCCAMerger::SplitBorderTracks( int iSlice1, AliHLTTPCCABorderTrack B1[], int N1,
+void AliHLTTPCCAMerger::MergeBorderTracks( int iSlice1, AliHLTTPCCABorderTrack B1[], int N1,
     int iSlice2, AliHLTTPCCABorderTrack B2[], int N2
                                          )
 {
-  //* split two sets of tracks
+  //* merge two sets of tracks
 
+  //std::cout<<" Merge slices "<<iSlice1<<"+"<<iSlice2<<": tracks "<<N1<<"+"<<N2<<std::endl;
+  
   float factor2ys = 1.;//1.5;//SG!!!
   float factor2zt = 1.;//1.5;//SG!!!
   float factor2k = 2.0;//2.2;
@@ -568,18 +542,21 @@ void AliHLTTPCCAMerger::SplitBorderTracks( int iSlice1, AliHLTTPCCABorderTrack B
       float c = t2.SignCosPhi() * t1.SignCosPhi() >= 0 ? 1 : -1;
       float dk = t2.QPt() - c * t1.QPt();
       float s2k = t2.Err2QPt() + t1.Err2QPt();
-
+      //std::cout<<" check 1.. "<<dk/sqrt(factor2k)<<std::endl;
       if ( dk*dk > factor2k*s2k ) continue;
 
 
       float chi2ys = GetChi2( t1.Y(), c * t1.SinPhi(), t1.Cov()[0], c * t1.Cov()[3], t1.Cov()[5],
                               t2.Y(),  t2.SinPhi(), t2.Cov()[0],  t2.Cov()[3], t2.Cov()[5] );
 
+      //std::cout<<" check 2.. "<<sqrt(chi2ys/factor2ys)<<std::endl;
       if ( chi2ys > factor2ys ) continue;
+      
 
       float chi2zt = GetChi2( t1.Z(), c * t1.DzDs(), t1.Cov()[2], c * t1.Cov()[7], t1.Cov()[9],
                               t2.Z(),  t2.DzDs(), t2.Cov()[2],  t2.Cov()[7], t2.Cov()[9] );
 
+      //std::cout<<" check 3.. "<<sqrt(chi2zt/factor2zt)<<std::endl;
       if ( chi2zt > factor2zt ) continue;
 
       lBest2 = b2.NClusters();
@@ -588,6 +565,7 @@ void AliHLTTPCCAMerger::SplitBorderTracks( int iSlice1, AliHLTTPCCABorderTrack B
 
     if ( iBest2 < 0 ) continue;
 
+    //std::cout<<"Neighbour found for "<<i1<<": "<<iBest2<<std::endl;
     AliHLTTPCCASliceTrackInfo &newTrack1 = fTrackInfos[fSliceTrackInfoStart[iSlice1] + b1.TrackID() ];
     AliHLTTPCCASliceTrackInfo &newTrack2 = fTrackInfos[fSliceTrackInfoStart[iSlice2] + iBest2 ];
 
@@ -609,6 +587,7 @@ void AliHLTTPCCAMerger::SplitBorderTracks( int iSlice1, AliHLTTPCCABorderTrack B
     }
     newTrack1.SetNextNeighbour( iBest2 );
     newTrack2.SetPrevNeighbour( b1.TrackID() );
+    //std::cout<<"Neighbourhood is set"<<std::endl;
   }
 
 }
@@ -647,7 +626,7 @@ void AliHLTTPCCAMerger::Merging()
 
   if ( 1 ) {// merging track segments withing one slice
 
-    AliHLTResizableArray<AliHLTTPCCABorderTrack> bord( maxNSliceTracks*10 );
+    AliHLTResizableArray<AliHLTTPCCABorderTrack> bord( maxNSliceTracks*2 );
 
     AliHLTTPCCASliceTrackInfo *tmpT = new AliHLTTPCCASliceTrackInfo[maxNSliceTracks];
     AliHLTTPCCAClusterInfo *tmpH = new AliHLTTPCCAClusterInfo[fMaxClusterInfos];
@@ -656,7 +635,7 @@ void AliHLTTPCCAMerger::Merging()
 
       int nBord = 0;
       MakeBorderTracks( iSlice, 4, bord.Data(), nBord );
-      SplitBorderTracks( iSlice, bord.Data(), nBord, iSlice, bord.Data(), nBord );
+      MergeBorderTracks( iSlice, bord.Data(), nBord, iSlice, bord.Data(), nBord );
 
       int nTr = 0, nH = 0;
       int sliceFirstClusterRef = 0;
@@ -710,10 +689,10 @@ void AliHLTTPCCAMerger::Merging()
   // arrays for the rotated track parameters
 
   AliHLTTPCCABorderTrack
-  *bCurr0 = new AliHLTTPCCABorderTrack[maxNSliceTracks*10],
-  *bNext0 = new AliHLTTPCCABorderTrack[maxNSliceTracks*10],
-  *bCurr = new AliHLTTPCCABorderTrack[maxNSliceTracks*10],
-  *bNext = new AliHLTTPCCABorderTrack[maxNSliceTracks*10];
+  *bCurr0 = new AliHLTTPCCABorderTrack[maxNSliceTracks*2],
+  *bNext0 = new AliHLTTPCCABorderTrack[maxNSliceTracks*2],
+  *bCurr = new AliHLTTPCCABorderTrack[maxNSliceTracks*2],
+  *bNext = new AliHLTTPCCABorderTrack[maxNSliceTracks*2];
 
   for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
 
@@ -727,8 +706,8 @@ void AliHLTTPCCAMerger::Merging()
     MakeBorderTracks( iSlice, 2, bCurr0, nCurr0 );
     MakeBorderTracks( jSlice, 3, bNext0, nNext0 );
 
-    SplitBorderTracks( iSlice, bCurr0, nCurr0, jSlice, bNext0, nNext0 );
-    SplitBorderTracks( iSlice, bCurr, nCurr, jSlice, bNext, nNext );
+    MergeBorderTracks( iSlice, bCurr0, nCurr0, jSlice, bNext0, nNext0 );
+    MergeBorderTracks( iSlice, bCurr, nCurr, jSlice, bNext, nNext );
   }
 
   if ( bCurr0 ) delete[] bCurr0;
@@ -743,7 +722,7 @@ void AliHLTTPCCAMerger::Merging()
   int nOutTrackClusters = 0;
 
   AliHLTTPCCAMergedTrack *outTracks = new AliHLTTPCCAMergedTrack[fMaxTrackInfos];
-  unsigned int   *outClusterIDsrc = new unsigned int [fMaxClusterInfos];
+  unsigned int   *outClusterId = new unsigned int [fMaxClusterInfos];
   UChar_t  *outClusterPackedAmp = new UChar_t [fMaxClusterInfos];
 
   for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
@@ -754,7 +733,7 @@ void AliHLTTPCCAMerger::Merging()
 
       if ( track.Used() ) continue;
       if ( track.PrevNeighbour() >= 0 ) continue;
-
+      //std::cout<<"Merged track candidate, nhits "<<track.NClusters()<<std::endl;
       AliHLTTPCCATrackParam startPoint = track.InnerParam(), endPoint = track.OuterParam();
       float startAlpha = track.InnerAlpha(), endAlpha = track.OuterAlpha();
 
@@ -828,42 +807,41 @@ void AliHLTTPCCAMerger::Merging()
       // need best t0!!!SG
 
       endPoint = startPoint;
-      if ( !FitTrack( endPoint, endAlpha, startPoint, startAlpha, hits + firstHit, nHits, 0 ) ) continue;
-      if ( !FitTrack( startPoint, startAlpha, endPoint, endAlpha, hits + firstHit, nHits, 1 ) ) continue;
 
+      if ( !FitTrack( endPoint, endAlpha, startPoint, startAlpha, hits + firstHit, nHits, 0,1 ) ) continue;
+      if ( !FitTrack( startPoint, startAlpha, endPoint, endAlpha, hits + firstHit, nHits, 1,1 ) ) continue;
       if ( nHits < 30 ) continue;    //SG!!!
 
-      AliHLTTPCCATrackParam &p = startPoint;
-
-      {
+      AliHLTTPCCATrackParam p = startPoint;
+      
+      if(0){
         double xTPC = 83.65; //SG!!!
-        double dAlpha = 0.00609235;
-        AliHLTTPCCATrackParam::AliHLTTPCCATrackFitParam fitPar;
-        p.CalculateFitParameters( fitPar );
-
-        if ( p.TransportToXWithMaterial( xTPC, fitPar, fSliceParam.GetBz( p ) ) ) {
+        double dAlpha = 0.349066;
+       double ymax = 2.* xTPC * CAMath::Tan( dAlpha / 2. );
+        
+        double dRot = 0;
+        if ( p.TransportToXWithMaterial( xTPC, fSliceParam.GetBz( p ) ) ) {
           double y = p.GetY();
-          double ymax = xTPC * CAMath::Tan( dAlpha / 2. );
-          if ( y > ymax ) {
-            if ( p.Rotate( dAlpha ) ) { startAlpha += dAlpha;  p.TransportToXWithMaterial( xTPC, fitPar, fSliceParam.GetBz( p ) ); }
-          } else if ( y < -ymax ) {
-            if ( p.Rotate( -dAlpha ) ) {  startAlpha -= dAlpha; p.TransportToXWithMaterial( xTPC, fitPar, fSliceParam.GetBz( p ) );}
+          if ( y > ymax ) {         
+            if ( p.Rotate( dAlpha ) ){
+              dRot = dAlpha;
+              p.TransportToXWithMaterial( xTPC, fSliceParam.GetBz( p ) );
+            }
+          } else if( y< -ymax ){
+            if ( p.Rotate( -dAlpha ) ){
+              dRot = -dAlpha;
+              p.TransportToXWithMaterial( xTPC, fSliceParam.GetBz( p ) );
+            }
           }
         }
+        
+        if ( -ymax <= p.GetY() && p.GetY() <= ymax && p.CheckNumericalQuality() ){
+          startPoint = p;
+          startAlpha+=dRot;
+        }     
       }
 
-      {
-        bool ok = 1;
-
-        const float *c = p.Cov();
-        for ( int i = 0; i < 15; i++ ) ok = ok && finite( c[i] );
-        for ( int i = 0; i < 5; i++ ) ok = ok && finite( p.Par()[i] );
-        ok = ok && ( p.GetX() > 50 );
-
-        if ( c[0] <= 0 || c[2] <= 0 || c[5] <= 0 || c[9] <= 0 || c[14] <= 0 ) ok = 0;
-        if ( c[0] > 5. || c[2] > 5. || c[5] > 2. || c[9] > 2 || c[14] > 2 ) ok = 0;
-        if ( !ok ) continue;
-      }
+      if ( !startPoint.CheckNumericalQuality() ) continue;
 
       AliHLTTPCCAMergedTrack &mergedTrack = outTracks[nOutTracks];
       mergedTrack.SetNClusters( nHits );
@@ -875,8 +853,7 @@ void AliHLTTPCCAMerger::Merging()
 
       for ( int i = 0; i < nHits; i++ ) {
         AliHLTTPCCAClusterInfo &clu = fClusterInfos[hits[firstHit+i]];
-        outClusterIDsrc[nOutTrackClusters+i] =
-          AliHLTTPCCADataCompressor::ISliceIRowIClu2IDsrc( clu.ISlice(), clu.IRow(), clu.IClu() );
+        outClusterId[nOutTrackClusters+i] = clu.Id();
         outClusterPackedAmp[nOutTrackClusters+i] = clu.PackedAmp();
       }
 
@@ -886,17 +863,20 @@ void AliHLTTPCCAMerger::Merging()
   }
 
   fOutput->SetNTracks( nOutTracks );
+  #ifdef HLTCA_STANDALONE
+  printf("Tracks Output: %d\n", nOutTracks);
+  #endif
   fOutput->SetNTrackClusters( nOutTrackClusters );
   fOutput->SetPointers();
 
   for ( int itr = 0; itr < nOutTracks; itr++ ) fOutput->SetTrack( itr, outTracks[itr] );
 
   for ( int ic = 0; ic < nOutTrackClusters; ic++ ) {
-    fOutput->SetClusterIDsrc( ic, outClusterIDsrc[ic] );
+    fOutput->SetClusterId( ic, outClusterId[ic] );
     fOutput->SetClusterPackedAmp( ic, outClusterPackedAmp[ic] );
   }
 
   delete[] outTracks;
-  delete[] outClusterIDsrc;
+  delete[] outClusterId;
   delete[] outClusterPackedAmp;
 }