]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCANeighboursFinder.cxx
changes from Matthias
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCANeighboursFinder.cxx
1 // @(#) $Id: AliHLTTPCCANeighboursFinder1.cxx 27042 2008-07-02 12:06:02Z richterm $
2 // **************************************************************************
3 // This file is property of and copyright by the ALICE HLT Project          *
4 // ALICE Experiment at CERN, All rights reserved.                           *
5 //                                                                          *
6 // Primary Authors: Sergey Gorbunov <sergey.gorbunov@kip.uni-heidelberg.de> *
7 //                  Ivan Kisel <kisel@kip.uni-heidelberg.de>                *
8 //                  for The ALICE HLT Project.                              *
9 //                                                                          *
10 // Permission to use, copy, modify and distribute this software and its     *
11 // documentation strictly for non-commercial purposes is hereby granted     *
12 // without fee, provided that the above copyright notice appears in all     *
13 // copies and that both the copyright notice and this permission notice     *
14 // appear in the supporting documentation. The authors make no claims       *
15 // about the suitability of this software for any purpose. It is            *
16 // provided "as is" without express or implied warranty.                    *
17 //                                                                          *
18 //***************************************************************************
19
20 #include "AliHLTTPCCANeighboursFinder.h"
21 #include "AliHLTTPCCAMath.h"
22 #include "AliHLTTPCCAHitArea.h"
23 #include "AliHLTTPCCATracker.h"
24
25 //#define DRAW
26
27 #ifdef DRAW
28 #include "AliHLTTPCCADisplay.h"
29 #endif
30
31 GPUd() void AliHLTTPCCANeighboursFinder::Thread
32 ( int /*nBlocks*/, int nThreads, int iBlock, int iThread, int iSync,
33   AliHLTTPCCASharedMemory &s, AliHLTTPCCATracker &tracker )
34 {
35   //* find neighbours
36
37   if ( iSync == 0 ) {
38     if ( iThread == 0 ) {
39       s.fNRows = tracker.Param().NRows();
40       s.fIRow = iBlock;
41       if ( s.fIRow < s.fNRows ) {
42         const AliHLTTPCCARow &row = tracker.Row( s.fIRow );
43         s.fNHits = row.NHits();
44
45         if ( ( s.fIRow >= 2 ) && ( s.fIRow <= s.fNRows - 3 ) ) {
46           s.fIRowUp = s.fIRow + 2;
47           s.fIRowDn = s.fIRow - 2;
48
49           // references to the rows above and below
50           const AliHLTTPCCARow &rowUp = tracker.Row( s.fIRowUp );
51           const AliHLTTPCCARow &rowDn = tracker.Row( s.fIRowDn );
52
53           // the axis perpendicular to the rows
54           const float xDn = rowDn.X();
55           const float x   = row.X();
56           const float xUp = rowUp.X();
57
58           // number of hits in rows above and below
59           s.fUpNHits = tracker.Row( s.fIRowUp ).NHits();
60           s.fDnNHits = tracker.Row( s.fIRowDn ).NHits();
61
62           // distance of the rows (absolute and relative)
63           s.fUpDx = xUp - x;
64           s.fDnDx = xDn - x;
65           s.fUpTx = xUp / x;
66           s.fDnTx = xDn / x;
67           // UpTx/DnTx is used to move the HitArea such that central events are preferred (i.e. vertices
68           // coming from y = 0, z = 0).
69
70           s.fGridUp = tracker.Row( s.fIRowUp ).Grid();
71           s.fGridDn = tracker.Row( s.fIRowDn ).Grid();
72         }
73       }
74     }
75   } else if ( iSync == 1 ) {
76     if ( s.fIRow < s.fNRows ) {
77       if ( ( s.fIRow == 0 ) || ( s.fIRow == s.fNRows - 1 ) || ( s.fIRow == 1 ) || ( s.fIRow == s.fNRows - 2 ) ) {
78         const AliHLTTPCCARow &row = tracker.Row( s.fIRow );
79         for ( int ih = iThread; ih < s.fNHits; ih += nThreads ) {
80           tracker.SetHitLinkUpData( row, ih, -1 );
81           tracker.SetHitLinkDownData( row, ih, -1 );
82         }
83       } else {
84         const AliHLTTPCCARow &rowUp = tracker.Row( s.fIRowUp );
85         const AliHLTTPCCARow &rowDn = tracker.Row( s.fIRowDn );
86
87         for ( unsigned int ih = iThread; ih < s.fGridUp.N() + s.fGridUp.Ny() + 2; ih += nThreads ) {
88           s.fGridContentUp[ih] = tracker.FirstHitInBin( rowUp, ih );
89         }
90         for ( unsigned int ih = iThread; ih < s.fGridDn.N() + s.fGridDn.Ny() + 2; ih += nThreads ) {
91           s.fGridContentDn[ih] = tracker.FirstHitInBin( rowDn, ih );
92         }
93       }
94     }
95   } else if ( iSync == 2 ) {
96     if ( ( s.fIRow <= 1 ) || ( s.fIRow >= s.fNRows - 2 ) ) return;
97
98     //const float kAreaSize = 3;
99     float chi2Cut = 3.*3.*4 * ( s.fUpDx * s.fUpDx + s.fDnDx * s.fDnDx );
100     const float kAreaSize = 3;
101     //float chi2Cut = 3.*3.*(s.fUpDx*s.fUpDx + s.fDnDx*s.fDnDx ); //SG
102     const int kMaxN = 20;
103
104     const AliHLTTPCCARow &row = tracker.Row( s.fIRow );
105     const AliHLTTPCCARow &rowUp = tracker.Row( s.fIRowUp );
106     const AliHLTTPCCARow &rowDn = tracker.Row( s.fIRowDn );
107     const float y0 = row.Grid().YMin();
108     const float z0 = row.Grid().ZMin();
109     const float stepY = row.HstepY();
110     const float stepZ = row.HstepZ();
111
112     for ( int ih = iThread; ih < s.fNHits; ih += nThreads ) {
113
114       unsigned short *neighUp = s.fB[iThread];
115       float2 *yzUp = s.fA[iThread];
116       //unsigned short neighUp[5];
117       //float2 yzUp[5];
118
119       int linkUp = -1;
120       int linkDn = -1;
121
122       if ( s.fDnNHits > 0 && s.fUpNHits > 0 ) {
123
124         int nNeighUp = 0;
125
126         // coordinates of the hit in the current row
127         const float y = y0 + tracker.HitDataY( row, ih ) * stepY;
128         const float z = z0 + tracker.HitDataZ( row, ih ) * stepZ;
129
130         AliHLTTPCCAHitArea areaDn, areaUp;
131         // TODO: for NVIDIA GPUs it should use the GridContentUp/-Dn that got copied into shared mem
132         areaUp.Init( rowUp, tracker.Data(), y*s.fUpTx, z*s.fUpTx, kAreaSize, kAreaSize );
133         areaDn.Init( rowDn, tracker.Data(), y*s.fDnTx, z*s.fDnTx, kAreaSize, kAreaSize );
134
135         do {
136           AliHLTTPCCAHit h;
137           int i = areaUp.GetNext( tracker, rowUp, tracker.Data(), &h );
138           if ( i < 0 ) break;
139           neighUp[nNeighUp] = ( unsigned short ) i;
140           yzUp[nNeighUp] = CAMath::MakeFloat2( s.fDnDx * ( h.Y() - y ), s.fDnDx * ( h.Z() - z ) );
141           if ( ++nNeighUp >= kMaxN ) break;
142         } while ( 1 );
143
144         int nNeighDn = 0;
145
146         if ( nNeighUp > 0 ) {
147
148           int bestDn = -1, bestUp = -1;
149           float bestD = 1.e10;
150
151           do {
152             AliHLTTPCCAHit h;
153             int i = areaDn.GetNext( tracker, rowDn, tracker.Data(), &h );
154             if ( i < 0 ) break;
155
156             nNeighDn++;
157             float2 yzdn = CAMath::MakeFloat2( s.fUpDx * ( h.Y() - y ), s.fUpDx * ( h.Z() - z ) );
158
159             for ( int iUp = 0; iUp < nNeighUp; iUp++ ) {
160               float2 yzup = yzUp[iUp];
161               float dy = yzdn.x - yzup.x;
162               float dz = yzdn.y - yzup.y;
163               float d = dy * dy + dz * dz;
164               if ( d < bestD ) {
165                 bestD = d;
166                 bestDn = i;
167                 bestUp = iUp;
168               }
169             }
170           } while ( 1 );
171
172           if ( bestD <= chi2Cut ) {
173             linkUp = neighUp[bestUp];
174             linkDn = bestDn;
175           }
176         }
177 #ifdef DRAW
178         std::cout << "n NeighUp = " << nNeighUp << ", n NeighDn = " << nNeighDn << std::endl;
179 #endif
180
181       }
182
183       tracker.SetHitLinkUpData( row, ih, linkUp );
184       tracker.SetHitLinkDownData( row, ih, linkDn );
185 #ifdef DRAW
186       std::cout << "Links for row " << s.fIRow << ", hit " << ih << ": " << linkUp << " " << linkDn << std::endl;
187       if ( s.fIRow == 22 && ih == 5 ) {
188         AliHLTTPCCADisplay::Instance().DrawSliceLink( s.fIRow, ih, -1, -1, 1 );
189         AliHLTTPCCADisplay::Instance().DrawSliceHit( s.fIRow, ih, kBlue, 1. );
190         AliHLTTPCCADisplay::Instance().Ask();
191       }
192 #endif
193     }
194   }
195 }
196