]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/TPCLib/tracking-ca/AliHLTTPCCAMerger.cxx
output of the slice tracker is compressed in order to minimize the network traffic
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCAMerger.cxx
CommitLineData
63d8b79d 1// $Id: AliHLTTPCCAMerger.cxx 30732 2009-01-22 23:02:02Z sgorbuno $
2// **************************************************************************
fbb9b71b 3// This file is property of and copyright by the ALICE HLT Project *
63d8b79d 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
7be9b0d7 20#include <stdio.h>
63d8b79d 21#include "AliHLTTPCCASliceTrack.h"
22#include "AliHLTTPCCATracker.h"
63d8b79d 23#include "AliHLTTPCCATrackParam.h"
24
25#include "AliHLTTPCCAMerger.h"
26
27#include "AliHLTTPCCAMath.h"
28#include "TStopwatch.h"
29
30#include "AliHLTTPCCATrackParam.h"
31#include "AliHLTTPCCASliceTrack.h"
32#include "AliHLTTPCCASliceOutput.h"
33#include "AliHLTTPCCAMergedTrack.h"
34#include "AliHLTTPCCAMergerOutput.h"
35#include "AliHLTTPCCADataCompressor.h"
36#include "AliHLTTPCCAParam.h"
15d2e9cf 37#include "AliHLTTPCCATrackLinearisation.h"
70e4a065 38#include "AliHLTTPCCADataCompressor.h"
e1f2d1c3 39
e1f2d1c3 40class AliHLTTPCCAMerger::AliHLTTPCCASliceTrackInfo
41{
42
fbb9b71b 43 public:
44
45 const AliHLTTPCCATrackParam &InnerParam() const { return fInnerParam; }
46 const AliHLTTPCCATrackParam &OuterParam() const { return fOuterParam; }
47 float InnerAlpha() const { return fInnerAlpha; }
48 float OuterAlpha() const { return fOuterAlpha; }
49 int NClusters() const { return fNClusters; }
50 int FirstClusterRef() const { return fFirstClusterRef; }
51 int PrevNeighbour() const { return fPrevNeighbour; }
52 int NextNeighbour() const { return fNextNeighbour; }
53 bool Used() const { return fUsed; }
54
55 void SetInnerParam( const AliHLTTPCCATrackParam &v ) { fInnerParam = v; }
56 void SetOuterParam( const AliHLTTPCCATrackParam &v ) { fOuterParam = v; }
57 void SetInnerAlpha( float v ) { fInnerAlpha = v; }
58 void SetOuterAlpha( float v ) { fOuterAlpha = v; }
59 void SetNClusters ( int v ) { fNClusters = v; }
60 void SetFirstClusterRef( int v ) { fFirstClusterRef = v; }
61 void SetPrevNeighbour( int v ) { fPrevNeighbour = v; }
62 void SetNextNeighbour( int v ) { fNextNeighbour = v; }
63 void SetUsed( bool v ) { fUsed = v; }
64
65 private:
66
67 AliHLTTPCCATrackParam fInnerParam; // inner parameters
68 AliHLTTPCCATrackParam fOuterParam; // outer parameters
69 float fInnerAlpha; // alpha angle for inner parameters
70 float fOuterAlpha; // alpha angle for outer parameters
71 int fNClusters; // N clusters
72 int fFirstClusterRef; // index of the first track cluster in the global cluster array
73 int fPrevNeighbour; // neighbour in the previous slise
74 int fNextNeighbour; // neighbour in the next slise
75 bool fUsed; // is the slice track already merged
e1f2d1c3 76
77};
78
79
80class AliHLTTPCCAMerger::AliHLTTPCCABorderTrack
81{
82
fbb9b71b 83 public:
84
85 const AliHLTTPCCATrackParam &Param() const { return fParam; }
86 int TrackID() const { return fTrackID; }
87 int NClusters() const { return fNClusters; }
88 int IRow() const { return fIRow; }
89 float X() const { return fX; }
90 bool OK() const { return fOK; }
91
92 void SetParam ( const AliHLTTPCCATrackParam &v ) { fParam = v; }
93 void SetTrackID ( int v ) { fTrackID = v; }
94 void SetNClusters ( int v ) { fNClusters = v; }
95 void SetIRow ( int v ) { fIRow = v; }
96 void SetX ( float v ) { fX = v; }
97 void SetOK ( bool v ) { fOK = v; }
98
99 private:
100
101 AliHLTTPCCATrackParam fParam; // track parameters at the border
102 int fTrackID; // track index
103 int fNClusters; // n clusters
104 int fIRow; // row number of the closest cluster
105 float fX; // X coordinate of the closest cluster
106 bool fOK; // is the track rotated and extrapolated correctly
e1f2d1c3 107
108};
109
110
111
63d8b79d 112AliHLTTPCCAMerger::AliHLTTPCCAMerger()
fbb9b71b 113 :
114 fSliceParam(),
115 fOutput( 0 ),
116 fTrackInfos( 0 ),
117 fMaxTrackInfos( 0 ),
118 fClusterInfos( 0 ),
119 fMaxClusterInfos( 0 )
63d8b79d 120{
121 //* constructor
e1f2d1c3 122 Clear();
63d8b79d 123}
124
e1f2d1c3 125/*
63d8b79d 126AliHLTTPCCAMerger::AliHLTTPCCAMerger(const AliHLTTPCCAMerger&)
fbb9b71b 127 :
63d8b79d 128 fSliceParam(),
129 fkSlices(0),
fbb9b71b 130 fOutput(0),
63d8b79d 131 fTrackInfos(0),
132 fMaxTrackInfos(0),
133 fClusterInfos(0),
134 fMaxClusterInfos(0)
135{
63d8b79d 136}
137
138const AliHLTTPCCAMerger &AliHLTTPCCAMerger::operator=(const AliHLTTPCCAMerger&) const
139{
63d8b79d 140 return *this;
141}
e1f2d1c3 142*/
63d8b79d 143
144AliHLTTPCCAMerger::~AliHLTTPCCAMerger()
145{
146 //* destructor
fbb9b71b 147 if ( fTrackInfos ) delete[] fTrackInfos;
148 if ( fClusterInfos ) delete[] fClusterInfos;
e65db743 149 if ( fOutput ) delete[] ( ( float2* )( fOutput ) );
63d8b79d 150}
151
e1f2d1c3 152void AliHLTTPCCAMerger::Clear()
153{
154 for ( int i = 0; i < fgkNSlices; ++i ) {
155 fkSlices[i] = 0;
e65db743 156 fSliceNTrackInfos[ i ] = 0;
157 fSliceTrackInfoStart[ i ] = 0;
e1f2d1c3 158 }
e65db743 159 if ( fOutput ) delete[] ( ( float2* )( fOutput ) );
160 if ( fTrackInfos ) delete[] fTrackInfos;
161 if ( fClusterInfos ) delete[] fClusterInfos;
162 fOutput = 0;
163 fTrackInfos = 0;
164 fClusterInfos = 0;
165 fMaxTrackInfos = 0;
166 fMaxClusterInfos = 0;
e1f2d1c3 167}
168
63d8b79d 169
e1f2d1c3 170void AliHLTTPCCAMerger::SetSliceData( int index, const AliHLTTPCCASliceOutput *SliceData )
171{
172 fkSlices[index] = SliceData;
173}
174
175void AliHLTTPCCAMerger::Reconstruct()
63d8b79d 176{
177 //* main merging routine
178
63d8b79d 179 UnpackSlices();
180 Merging();
181}
182
183void AliHLTTPCCAMerger::UnpackSlices()
184{
185 //* unpack the cluster information from the slice tracks and initialize track info array
186
187 // get N tracks and N clusters in event
fbb9b71b 188
189 int nTracksTotal = 0;
190 int nTrackClustersTotal = 0;
191 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
192 if ( !fkSlices[iSlice] ) continue;
193 nTracksTotal += fkSlices[iSlice]->NTracks();
194 nTrackClustersTotal += fkSlices[iSlice]->NTrackClusters();
63d8b79d 195 }
196
197 // book/clean memory if necessary
198 {
fbb9b71b 199 if ( nTracksTotal > fMaxTrackInfos || ( fMaxTrackInfos > 100 && nTracksTotal < 0.5*fMaxTrackInfos ) ) {
200 if ( fTrackInfos ) delete[] fTrackInfos;
201 fMaxTrackInfos = ( int ) ( nTracksTotal * 1.2 );
63d8b79d 202 fTrackInfos = new AliHLTTPCCASliceTrackInfo[fMaxTrackInfos];
203 }
204
fbb9b71b 205 if ( nTrackClustersTotal > fMaxClusterInfos || ( fMaxClusterInfos > 1000 && nTrackClustersTotal < 0.5*fMaxClusterInfos ) ) {
206 if ( fClusterInfos ) delete[] fClusterInfos;
207 fMaxClusterInfos = ( int ) ( nTrackClustersTotal * 1.2 );
63d8b79d 208 fClusterInfos = new AliHLTTPCCAClusterInfo [fMaxClusterInfos];
209 }
210
e65db743 211 if ( fOutput ) delete[] ( ( float2* )( fOutput ) );
fbb9b71b 212 int size = fOutput->EstimateSize( nTracksTotal, nTrackClustersTotal );
213 fOutput = ( AliHLTTPCCAMergerOutput* )( new float2[size/sizeof( float2 )+1] );
63d8b79d 214 }
215
216 // unpack track and cluster information
63d8b79d 217
fbb9b71b 218 int nTracksCurrent = 0;
219 int nClustersCurrent = 0;
220
221 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
63d8b79d 222
63d8b79d 223 fSliceTrackInfoStart[ iSlice ] = nTracksCurrent;
224 fSliceNTrackInfos[ iSlice ] = 0;
e1f2d1c3 225
fbb9b71b 226 if ( !fkSlices[iSlice] ) continue;
63d8b79d 227
fbb9b71b 228 const AliHLTTPCCASliceOutput &slice = *( fkSlices[iSlice] );
229
230 for ( int itr = 0; itr < slice.NTracks(); itr++ ) {
15d2e9cf 231
63d8b79d 232 const AliHLTTPCCASliceTrack &sTrack = slice.Track( itr );
73a33d2e 233 AliHLTTPCCATrackParam t0;
234 t0.InitParam();
235 t0.SetParam(sTrack.Param());
fbb9b71b 236 int nCluNew = 0;
237
238 for ( int iTrClu = 0; iTrClu < sTrack.NClusters(); iTrClu++ ) {
239
240 // unpack cluster information
241
242 AliHLTTPCCAClusterInfo &clu = fClusterInfos[nClustersCurrent + nCluNew];
243 int ic = sTrack.FirstClusterRef() + iTrClu;
244
245 clu.SetISlice( iSlice );
6de2bc40 246 clu.SetIRow( slice.ClusterRow( ic ) );
247 clu.SetId( slice.ClusterId( ic ) );
73a33d2e 248 clu.SetPackedAmp( 0 );
70e4a065 249 float x,y,z;
250 AliHLTTPCCADataCompressor::UnpackXYZ( clu.IRow(), slice.ClusterPackedXYZ(ic), x, y, z );
251
252 clu.SetX( x );
253 clu.SetY( y );
254 clu.SetZ( z );
fbb9b71b 255
6de2bc40 256 if ( !t0.TransportToX( clu.X(), fSliceParam.GetBz( t0 ), .999 ) ) continue;
fbb9b71b 257
258 float err2Y, err2Z;
259 fSliceParam.GetClusterErrors2( clu.IRow(), clu.Z(), t0.SinPhi(), t0.GetCosPhi(), t0.DzDs(), err2Y, err2Z );
260
261 clu.SetErr2Y( err2Y );
262 clu.SetErr2Z( err2Z );
263 nCluNew++ ;
63d8b79d 264 }
e1f2d1c3 265
fbb9b71b 266 if ( nCluNew < .8*sTrack.NClusters() ) continue;
267
268 // refit the track
269
270 int hits[1000];
271 int nHits = nCluNew;
272 for ( int i = 0; i < nHits; i++ ) hits[i] = nClustersCurrent + i;
63d8b79d 273
73a33d2e 274 AliHLTTPCCATrackParam startPoint;
275 startPoint.InitParam();
276 startPoint.SetParam(sTrack.Param());
63d8b79d 277 AliHLTTPCCATrackParam endPoint = startPoint;
fbb9b71b 278 float startAlpha = fSliceParam.Alpha( iSlice );
279 float endAlpha = startAlpha;
280
281 if ( !FitTrack( endPoint, endAlpha, startPoint, startAlpha, hits, nHits, 0 ) ) continue;
15d2e9cf 282
63d8b79d 283 startPoint = endPoint;
284 startAlpha = endAlpha;
fbb9b71b 285 if ( !FitTrack( startPoint, startAlpha, endPoint, endAlpha, hits, nHits, 1 ) ) continue;
15d2e9cf 286
fbb9b71b 287 if ( nHits < .8*sTrack.NClusters() ) continue;
63d8b79d 288
289 // store the track
fbb9b71b 290
291 AliHLTTPCCASliceTrackInfo &track = fTrackInfos[nTracksCurrent];
e1f2d1c3 292
293 track.SetInnerParam( startPoint );
294 track.SetInnerAlpha( startAlpha );
295 track.SetOuterParam( endPoint );
296 track.SetOuterAlpha( endAlpha );
297 track.SetFirstClusterRef( nClustersCurrent );
298 track.SetNClusters( nHits );
299 track.SetPrevNeighbour( -1 );
300 track.SetNextNeighbour( -1 );
301 track.SetUsed( 0 );
fbb9b71b 302
303 for ( int i = 0; i < nHits; i++ )
304 fClusterInfos[nClustersCurrent + i] = fClusterInfos[hits[i]];
63d8b79d 305 nTracksCurrent++;
306 fSliceNTrackInfos[ iSlice ]++;
fbb9b71b 307 nClustersCurrent += nHits;
63d8b79d 308 }
15d2e9cf 309 //std::cout<<"Unpack slice "<<iSlice<<": ntracks "<<slice.NTracks()<<"/"<<fSliceNTrackInfos[iSlice]<<std::endl;
63d8b79d 310 }
311}
312
313
314
fbb9b71b 315bool AliHLTTPCCAMerger::FitTrack( AliHLTTPCCATrackParam &T, float &Alpha,
4acc2401 316 AliHLTTPCCATrackParam t0, float Alpha0,
f0fb467d 317 int hits[], int &NTrackHits, bool dir, bool final,
b8139972 318 AliHLTTPCCAClusterInfo *infoArray )
63d8b79d 319{
320 // Fit the track
321
322 AliHLTTPCCATrackParam::AliHLTTPCCATrackFitParam fitPar;
323 AliHLTTPCCATrackParam t = t0;
fbb9b71b 324 AliHLTTPCCATrackLinearisation l( t0 );
325
b8139972 326 bool first = 1;
6de2bc40 327 bool doErrors = 1;
b8139972 328 if ( !infoArray ) {
6de2bc40 329 infoArray = fClusterInfos;
330 doErrors = 0;
331 }
15d2e9cf 332
15d2e9cf 333 t.CalculateFitParameters( fitPar );
63d8b79d 334
fbb9b71b 335 int hitsNew[1000];
336 int nHitsNew = 0;
337
338 for ( int ihit = 0; ihit < NTrackHits; ihit++ ) {
63d8b79d 339
fbb9b71b 340 int jhit = dir ? ( NTrackHits - 1 - ihit ) : ihit;
6de2bc40 341 AliHLTTPCCAClusterInfo &h = infoArray[hits[jhit]];
e1f2d1c3 342
fbb9b71b 343 int iSlice = h.ISlice();
63d8b79d 344
fbb9b71b 345 float sliceAlpha = fSliceParam.Alpha( iSlice );
63d8b79d 346
fbb9b71b 347 if ( CAMath::Abs( sliceAlpha - Alpha0 ) > 1.e-4 ) {
348 if ( ! t.Rotate( sliceAlpha - Alpha0, l, .999 ) ) continue;
63d8b79d 349 Alpha0 = sliceAlpha;
350 }
351
fbb9b71b 352 //float x = fSliceParam.RowX( h.IRow() );
353 float x = h.X();
354
355 if ( !t.TransportToXWithMaterial( x, l, fitPar, fSliceParam.GetBz( t ) ) ) continue;
356
357 if ( first ) {
63d8b79d 358 t.SetCov( 0, 10 );
359 t.SetCov( 1, 0 );
360 t.SetCov( 2, 10 );
361 t.SetCov( 3, 0 );
362 t.SetCov( 4, 0 );
363 t.SetCov( 5, 1 );
364 t.SetCov( 6, 0 );
365 t.SetCov( 7, 0 );
366 t.SetCov( 8, 0 );
367 t.SetCov( 9, 1 );
fbb9b71b 368 t.SetCov( 10, 0 );
369 t.SetCov( 11, 0 );
370 t.SetCov( 12, 0 );
371 t.SetCov( 13, 0 );
372 t.SetCov( 14, 10 );
63d8b79d 373 t.SetChi2( 0 );
374 t.SetNDF( -5 );
15d2e9cf 375 t.CalculateFitParameters( fitPar );
63d8b79d 376 }
fbb9b71b 377
6de2bc40 378 float err2Y = h.Err2Y();
379 float err2Z = h.Err2Z();
b8139972 380 if ( doErrors ) fSliceParam.GetClusterErrors2( h.IRow(), h.Z(), l.SinPhi(), l.CosPhi(), l.DzDs(), err2Y, err2Z );
f0fb467d 381 if( !final ){
382 err2Y*= fSliceParam.ClusterError2CorrectionY();
383 err2Z*= fSliceParam.ClusterError2CorrectionZ();
384 }
385
b8139972 386 if ( !t.Filter( h.Y(), h.Z(), err2Y, err2Z ) ) continue;
15d2e9cf 387
63d8b79d 388 first = 0;
389
390 hitsNew[nHitsNew++] = hits[jhit];
391 }
fbb9b71b 392
91794c67 393 if ( CAMath::Abs( t.QPt() ) < 1.e-4 ) t.SetQPt( 1.e-4 );
fbb9b71b 394
91794c67 395 bool ok = t.CheckNumericalQuality();
fbb9b71b 396
397 if ( CAMath::Abs( t.SinPhi() ) > .99 ) ok = 0;
398 else if ( l.CosPhi() >= 0 ) t.SetSignCosPhi( 1 );
15d2e9cf 399 else t.SetSignCosPhi( -1 );
63d8b79d 400
fbb9b71b 401 if ( ok ) {
63d8b79d 402 T = t;
403 Alpha = Alpha0;
404 NTrackHits = nHitsNew;
fbb9b71b 405 for ( int i = 0; i < NTrackHits; i++ ) {
406 hits[dir ?( NTrackHits-1-i ) :i] = hitsNew[i];
63d8b79d 407 }
408 }
409 return ok;
410}
411
412
fbb9b71b 413float AliHLTTPCCAMerger::GetChi2( float x1, float y1, float a00, float a10, float a11,
4acc2401 414 float x2, float y2, float b00, float b10, float b11 )
63d8b79d 415{
fbb9b71b 416 //* Calculate Chi2/ndf deviation
417
418 float d[2] = { x1 - x2, y1 - y2 };
63d8b79d 419
fbb9b71b 420 float mSi[3] = { a00 + b00, a10 + b10, a11 + b11 };
63d8b79d 421
fbb9b71b 422 float s = ( mSi[0] * mSi[2] - mSi[1] * mSi[1] );
63d8b79d 423
fbb9b71b 424 if ( s < 1.E-10 ) return 10000.;
63d8b79d 425
fbb9b71b 426 float mS[3] = { mSi[2], -mSi[1], mSi[0] };
63d8b79d 427
7be9b0d7 428 return AliHLTTPCCAMath::Abs( ( ( mS[0]*d[0] + mS[1]*d[1] )*d[0]
fbb9b71b 429 + ( mS[1]*d[0] + mS[2]*d[1] )*d[1] ) / s / 2 );
63d8b79d 430
431}
432
63d8b79d 433
fbb9b71b 434
435void AliHLTTPCCAMerger::MakeBorderTracks( int iSlice, int iBorder, AliHLTTPCCABorderTrack B[], int &nB )
63d8b79d 436{
437 //* prepare slice tracks for merging with next/previous/same sector
fbb9b71b 438 //* each track transported to the border line,
439 //* in some cases both inner and outer parameters of the track are transported
440
441 static int statAll = 0, statOK = 0;
442 nB = 0;
443 float dAlpha = fSliceParam.DAlpha() / 2;
444 float x0 = 0;
445
446 if ( iBorder == 0 ) { // transport to the left age of the sector and rotate horisontally
447 dAlpha = dAlpha - CAMath::Pi() / 2 ;
448 } else if ( iBorder == 1 ) { // transport to the right age of the sector and rotate horisontally
449 dAlpha = -dAlpha - CAMath::Pi() / 2 ;
450 } else if ( iBorder == 2 ) { // transport to the left age of the sector and rotate vertically
63d8b79d 451 dAlpha = dAlpha;
452 x0 = fSliceParam.RowX( 63 );
fbb9b71b 453 } else if ( iBorder == 3 ) { // transport to the right age of the sector and rotate vertically
63d8b79d 454 dAlpha = -dAlpha;
455 x0 = fSliceParam.RowX( 63 );
fbb9b71b 456 } else if ( iBorder == 4 ) { // transport to the middle of the sector, w/o rotation
63d8b79d 457 dAlpha = 0;
fbb9b71b 458 x0 = fSliceParam.RowX( 63 );
63d8b79d 459 }
460
fbb9b71b 461 for ( int itr = 0; itr < fSliceNTrackInfos[iSlice]; itr++ ) {
63d8b79d 462
e1f2d1c3 463 const AliHLTTPCCASliceTrackInfo &track = fTrackInfos[ fSliceTrackInfoStart[iSlice] + itr ];
63d8b79d 464
e1f2d1c3 465 AliHLTTPCCATrackParam t0 = track.InnerParam();
466 AliHLTTPCCATrackParam t1 = track.OuterParam();
63d8b79d 467
fbb9b71b 468 const float maxSin = CAMath::Sin( 60. / 180.*CAMath::Pi() );
469
470 bool ok0 = t0.Rotate( dAlpha, maxSin );
471 bool ok1 = t1.Rotate( dAlpha, maxSin );
63d8b79d 472
fbb9b71b 473 bool do0 = ok0;
474 bool do1 = ok1 && ( !ok0 || t1.SignCosPhi() * t0.SignCosPhi() < 0 );
475
476 if ( ok0 && !do1 && ok1 && ( t1.X() < t0.X() ) ) {
63d8b79d 477 do0 = 0;
478 do1 = 1;
479 }
480
fbb9b71b 481 if ( do0 ) {
63d8b79d 482 AliHLTTPCCABorderTrack &b = B[nB];
e1f2d1c3 483 b.SetX( t0.GetX() );
f0fb467d 484
fbb9b71b 485 if ( t0.TransportToX( x0, fSliceParam.GetBz( t0 ), maxSin ) ) {
486 b.SetOK( 1 );
487 b.SetTrackID( itr );
488 b.SetNClusters( track.NClusters() );
489 b.SetIRow( fClusterInfos[ track.FirstClusterRef() + 0 ].IRow() );
490 b.SetParam( t0 );
491 nB++;
e1f2d1c3 492 }
63d8b79d 493 }
fbb9b71b 494 if ( do1 ) {
63d8b79d 495 AliHLTTPCCABorderTrack &b = B[nB];
e1f2d1c3 496 b.SetX( t1.GetX() );
f0fb467d 497
fbb9b71b 498 if ( t1.TransportToX( x0, fSliceParam.GetBz( t1 ), maxSin ) ) {
499 b.SetOK( 1 );
500 b.SetTrackID( itr );
501 b.SetNClusters( track.NClusters() );
502 b.SetIRow( fClusterInfos[ track.FirstClusterRef() + track.NClusters()-1 ].IRow() );
503 b.SetParam( t1 );
504 nB++;
e1f2d1c3 505 }
63d8b79d 506 }
fbb9b71b 507 if ( do0 || do1 ) statOK++;
63d8b79d 508 statAll++;
509 }
510}
511
512
513
f0fb467d 514void AliHLTTPCCAMerger::MergeBorderTracks( int iSlice1, AliHLTTPCCABorderTrack B1[], int N1,
fbb9b71b 515 int iSlice2, AliHLTTPCCABorderTrack B2[], int N2
516 )
63d8b79d 517{
f0fb467d 518 //* merge two sets of tracks
63d8b79d 519
f0fb467d 520 //std::cout<<" Merge slices "<<iSlice1<<"+"<<iSlice2<<": tracks "<<N1<<"+"<<N2<<std::endl;
521
fbb9b71b 522 float factor2ys = 1.;//1.5;//SG!!!
523 float factor2zt = 1.;//1.5;//SG!!!
524 float factor2k = 2.0;//2.2;
63d8b79d 525
fbb9b71b 526 factor2k = 3.5 * 3.5 * factor2k * factor2k;
527 factor2ys = 3.5 * 3.5 * factor2ys * factor2ys;
528 factor2zt = 3.5 * 3.5 * factor2zt * factor2zt;
63d8b79d 529
fbb9b71b 530 int minNPartHits = 10;//SG!!!
531 int minNTotalHits = 20;
63d8b79d 532
fbb9b71b 533 //float maxDX = fSliceParam.RowX(40) - fSliceParam.RowX(0);
63d8b79d 534
fbb9b71b 535 for ( int i1 = 0; i1 < N1; i1++ ) {
63d8b79d 536 AliHLTTPCCABorderTrack &b1 = B1[i1];
fbb9b71b 537 if ( !b1.OK() ) continue;
538 if ( b1.NClusters() < minNPartHits ) continue;
e1f2d1c3 539 const AliHLTTPCCATrackParam &t1 = b1.Param();
fbb9b71b 540 int iBest2 = -1;
541 int lBest2 = 0;
542 int start2 = ( iSlice1 != iSlice2 ) ? 0 : i1 + 1;
543 for ( int i2 = start2; i2 < N2; i2++ ) {
63d8b79d 544 AliHLTTPCCABorderTrack &b2 = B2[i2];
fbb9b71b 545 if ( !b2.OK() ) continue;
546 if ( b2.NClusters() < minNPartHits ) continue;
547 if ( b2.NClusters() < lBest2 ) continue;
548 if ( b1.NClusters() + b2.NClusters() < minNTotalHits ) continue;
63d8b79d 549
550 //if( TMath::Abs(b1.fX - b2.fX)>maxDX ) continue;
551
e1f2d1c3 552 const AliHLTTPCCATrackParam &t2 = b2.Param();
63d8b79d 553
fbb9b71b 554 float c = t2.SignCosPhi() * t1.SignCosPhi() >= 0 ? 1 : -1;
555 float dk = t2.QPt() - c * t1.QPt();
556 float s2k = t2.Err2QPt() + t1.Err2QPt();
f0fb467d 557 //std::cout<<" check 1.. "<<dk/sqrt(factor2k)<<std::endl;
fbb9b71b 558 if ( dk*dk > factor2k*s2k ) continue;
559
560
561 float chi2ys = GetChi2( t1.Y(), c * t1.SinPhi(), t1.Cov()[0], c * t1.Cov()[3], t1.Cov()[5],
4acc2401 562 t2.Y(), t2.SinPhi(), t2.Cov()[0], t2.Cov()[3], t2.Cov()[5] );
63d8b79d 563
f0fb467d 564 //std::cout<<" check 2.. "<<sqrt(chi2ys/factor2ys)<<std::endl;
fbb9b71b 565 if ( chi2ys > factor2ys ) continue;
f0fb467d 566
63d8b79d 567
fbb9b71b 568 float chi2zt = GetChi2( t1.Z(), c * t1.DzDs(), t1.Cov()[2], c * t1.Cov()[7], t1.Cov()[9],
4acc2401 569 t2.Z(), t2.DzDs(), t2.Cov()[2], t2.Cov()[7], t2.Cov()[9] );
63d8b79d 570
f0fb467d 571 //std::cout<<" check 3.. "<<sqrt(chi2zt/factor2zt)<<std::endl;
fbb9b71b 572 if ( chi2zt > factor2zt ) continue;
63d8b79d 573
e1f2d1c3 574 lBest2 = b2.NClusters();
575 iBest2 = b2.TrackID();
63d8b79d 576 }
fbb9b71b 577
578 if ( iBest2 < 0 ) continue;
579
f0fb467d 580 //std::cout<<"Neighbour found for "<<i1<<": "<<iBest2<<std::endl;
fbb9b71b 581 AliHLTTPCCASliceTrackInfo &newTrack1 = fTrackInfos[fSliceTrackInfoStart[iSlice1] + b1.TrackID() ];
582 AliHLTTPCCASliceTrackInfo &newTrack2 = fTrackInfos[fSliceTrackInfoStart[iSlice2] + iBest2 ];
583
584 int old1 = newTrack2.PrevNeighbour();
585
586 if ( old1 >= 0 ) {
587 AliHLTTPCCASliceTrackInfo &oldTrack1 = fTrackInfos[fSliceTrackInfoStart[iSlice1] + old1];
588 if ( oldTrack1.NClusters() < newTrack1.NClusters() ) {
589 newTrack2.SetPrevNeighbour( -1 );
590 oldTrack1.SetNextNeighbour( -1 );
63d8b79d 591 } else continue;
592 }
fbb9b71b 593 int old2 = newTrack1.NextNeighbour();
594 if ( old2 >= 0 ) {
595 AliHLTTPCCASliceTrackInfo &oldTrack2 = fTrackInfos[fSliceTrackInfoStart[iSlice2] + old2];
596 if ( oldTrack2.NClusters() < newTrack2.NClusters() ) {
597 oldTrack2.SetPrevNeighbour( -1 );
63d8b79d 598 } else continue;
599 }
fbb9b71b 600 newTrack1.SetNextNeighbour( iBest2 );
601 newTrack2.SetPrevNeighbour( b1.TrackID() );
f0fb467d 602 //std::cout<<"Neighbourhood is set"<<std::endl;
fbb9b71b 603 }
604
63d8b79d 605}
606
607
608void AliHLTTPCCAMerger::Merging()
609{
610 //* track merging between slices
611
e1f2d1c3 612 fOutput->SetNTracks( 0 );
613 fOutput->SetNTrackClusters( 0 );
614 fOutput->SetPointers();
615
63d8b79d 616
617 // for each slice set number of the next neighbouring slice
63d8b79d 618
fbb9b71b 619 int nextSlice[100], prevSlice[100];
620
621 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
63d8b79d 622 nextSlice[iSlice] = iSlice + 1;
623 prevSlice[iSlice] = iSlice - 1;
624 }
fbb9b71b 625 int mid = fgkNSlices / 2 - 1 ;
626 int last = fgkNSlices - 1 ;
627 if ( mid < 0 ) mid = 0; // to avoid compiler warning
628 if ( last < 0 ) last = 0; //
63d8b79d 629 nextSlice[ mid ] = 0;
630 prevSlice[ 0 ] = mid;
fbb9b71b 631 nextSlice[ last ] = fgkNSlices / 2;
63d8b79d 632 prevSlice[ fgkNSlices/2 ] = last;
fbb9b71b 633
634 int maxNSliceTracks = 0;
635 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
636 if ( maxNSliceTracks < fSliceNTrackInfos[iSlice] ) maxNSliceTracks = fSliceNTrackInfos[iSlice];
63d8b79d 637 }
fbb9b71b 638
639 if ( 1 ) {// merging track segments withing one slice
640
fdb75fd8 641 AliHLTResizableArray<AliHLTTPCCABorderTrack> bord( maxNSliceTracks*2 );
fbb9b71b 642
63d8b79d 643 AliHLTTPCCASliceTrackInfo *tmpT = new AliHLTTPCCASliceTrackInfo[maxNSliceTracks];
644 AliHLTTPCCAClusterInfo *tmpH = new AliHLTTPCCAClusterInfo[fMaxClusterInfos];
645
fbb9b71b 646 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
647
648 int nBord = 0;
4acc2401 649 MakeBorderTracks( iSlice, 4, bord.Data(), nBord );
f0fb467d 650 MergeBorderTracks( iSlice, bord.Data(), nBord, iSlice, bord.Data(), nBord );
fbb9b71b 651
652 int nTr = 0, nH = 0;
653 int sliceFirstClusterRef = 0;
654 for ( int itr = 0; itr < fSliceNTrackInfos[iSlice]; itr++ ) {
655 AliHLTTPCCASliceTrackInfo &track = fTrackInfos[ fSliceTrackInfoStart[iSlice] + itr];
656 if ( itr == 0 ) sliceFirstClusterRef = track.FirstClusterRef();
657 track.SetPrevNeighbour( -1 );
658 if ( track.NextNeighbour() == -2 ) {
659 track.SetNextNeighbour( -1 );
660 continue;
661 }
662 AliHLTTPCCASliceTrackInfo &trackNew = tmpT[nTr];
663 trackNew = track;
664 trackNew.SetFirstClusterRef( sliceFirstClusterRef + nH );
665
666 for ( int ih = 0; ih < track.NClusters(); ih++ ) tmpH[nH+ih] = fClusterInfos[track.FirstClusterRef()+ih];
667 nTr++;
668 nH += track.NClusters();
669
670 int jtr = track.NextNeighbour();
671
672 if ( jtr < 0 ) continue;
673 AliHLTTPCCASliceTrackInfo &neighTrack = fTrackInfos[ fSliceTrackInfoStart[iSlice] + jtr];
674
675 track.SetNextNeighbour( -1 );
676 neighTrack.SetNextNeighbour( -2 );
677
678 for ( int ih = 0; ih < neighTrack.NClusters(); ih++ )
679 tmpH[nH+ih] = fClusterInfos[neighTrack.FirstClusterRef()+ih];
680
681 trackNew.SetNClusters( trackNew.NClusters() + neighTrack.NClusters() );
682 trackNew.SetNextNeighbour( -1 );
683 nH += neighTrack.NClusters();
684 if ( neighTrack.InnerParam().X() < track.InnerParam().X() ) trackNew.SetInnerParam( neighTrack.InnerParam() );
685 if ( neighTrack.OuterParam().X() > track.OuterParam().X() ) trackNew.SetOuterParam( neighTrack.OuterParam() );
63d8b79d 686 }
fbb9b71b 687
63d8b79d 688 fSliceNTrackInfos[iSlice] = nTr;
fbb9b71b 689 for ( int itr = 0; itr < nTr; itr++ ) fTrackInfos[ fSliceTrackInfoStart[iSlice] + itr] = tmpT[itr];
690 for ( int ih = 0; ih < nH; ih++ ) fClusterInfos[sliceFirstClusterRef + ih] = tmpH[ih];
63d8b79d 691
692 }
693 delete[] tmpT;
694 delete[] tmpH;
695 }
696
697
fbb9b71b 698 //* merging tracks between slices
699
63d8b79d 700
63d8b79d 701 // arrays for the rotated track parameters
702
fbb9b71b 703 AliHLTTPCCABorderTrack
fdb75fd8 704 *bCurr0 = new AliHLTTPCCABorderTrack[maxNSliceTracks*2],
705 *bNext0 = new AliHLTTPCCABorderTrack[maxNSliceTracks*2],
706 *bCurr = new AliHLTTPCCABorderTrack[maxNSliceTracks*2],
707 *bNext = new AliHLTTPCCABorderTrack[maxNSliceTracks*2];
fbb9b71b 708
709 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
63d8b79d 710
fbb9b71b 711 int jSlice = nextSlice[iSlice];
712
713 int nCurr0 = 0, nNext0 = 0;
714 int nCurr = 0, nNext = 0;
63d8b79d 715
716 MakeBorderTracks( iSlice, 0, bCurr, nCurr );
717 MakeBorderTracks( jSlice, 1, bNext, nNext );
718 MakeBorderTracks( iSlice, 2, bCurr0, nCurr0 );
719 MakeBorderTracks( jSlice, 3, bNext0, nNext0 );
fbb9b71b 720
f0fb467d 721 MergeBorderTracks( iSlice, bCurr0, nCurr0, jSlice, bNext0, nNext0 );
722 MergeBorderTracks( iSlice, bCurr, nCurr, jSlice, bNext, nNext );
63d8b79d 723 }
724
fbb9b71b 725 if ( bCurr0 ) delete[] bCurr0;
726 if ( bNext0 ) delete[] bNext0;
727 if ( bCurr ) delete[] bCurr;
728 if ( bNext ) delete[] bNext;
63d8b79d 729
730
fbb9b71b 731 //static int nRejected = 0;
732
733 int nOutTracks = 0;
734 int nOutTrackClusters = 0;
63d8b79d 735
736 AliHLTTPCCAMergedTrack *outTracks = new AliHLTTPCCAMergedTrack[fMaxTrackInfos];
6de2bc40 737 unsigned int *outClusterId = new unsigned int [fMaxClusterInfos];
63d8b79d 738 UChar_t *outClusterPackedAmp = new UChar_t [fMaxClusterInfos];
63d8b79d 739
fbb9b71b 740 for ( int iSlice = 0; iSlice < fgkNSlices; iSlice++ ) {
741
742 for ( int itr = 0; itr < fSliceNTrackInfos[iSlice]; itr++ ) {
63d8b79d 743
fbb9b71b 744 AliHLTTPCCASliceTrackInfo &track = fTrackInfos[fSliceTrackInfoStart[iSlice] + itr];
63d8b79d 745
fbb9b71b 746 if ( track.Used() ) continue;
747 if ( track.PrevNeighbour() >= 0 ) continue;
f0fb467d 748 //std::cout<<"Merged track candidate, nhits "<<track.NClusters()<<std::endl;
e1f2d1c3 749 AliHLTTPCCATrackParam startPoint = track.InnerParam(), endPoint = track.OuterParam();
fbb9b71b 750 float startAlpha = track.InnerAlpha(), endAlpha = track.OuterAlpha();
63d8b79d 751
fbb9b71b 752 int hits[2000];
753 int firstHit = 1000;
754 int nHits = 0;
755 int jSlice = iSlice;
756 int jtr = itr;
63d8b79d 757
758 {
fbb9b71b 759 track.SetUsed( 1 );
760 for ( int jhit = 0; jhit < track.NClusters(); jhit++ ) {
761 int id = track.FirstClusterRef() + jhit;
762 hits[firstHit+jhit] = id;
763 }
764 nHits = track.NClusters();
765 jtr = track.NextNeighbour();
766 jSlice = nextSlice[iSlice];
63d8b79d 767 }
768
fbb9b71b 769 while ( jtr >= 0 ) {
770 AliHLTTPCCASliceTrackInfo &segment = fTrackInfos[fSliceTrackInfoStart[jSlice] + jtr];
771 if ( segment.Used() ) break;
772 segment.SetUsed( 1 );
773 bool dir = 0;
774 int startHit = firstHit + nHits;
775 float d00 = startPoint.GetDistXZ2( segment.InnerParam() );
776 float d01 = startPoint.GetDistXZ2( segment.OuterParam() );
777 float d10 = endPoint.GetDistXZ2( segment.InnerParam() );
778 float d11 = endPoint.GetDistXZ2( segment.OuterParam() );
779 if ( d00 <= d01 && d00 <= d10 && d00 <= d11 ) {
780 startPoint = segment.OuterParam();
781 startAlpha = segment.OuterAlpha();
782 dir = 1;
783 firstHit -= segment.NClusters();
784 startHit = firstHit;
785 } else if ( d01 <= d10 && d01 <= d11 ) {
786 startPoint = segment.InnerParam();
787 startAlpha = segment.InnerAlpha();
788 dir = 0;
789 firstHit -= segment.NClusters();
790 startHit = firstHit;
791 } else if ( d10 <= d11 ) {
792 endPoint = segment.OuterParam();
793 endAlpha = segment.OuterAlpha();
794 dir = 0;
795 } else {
796 endPoint = segment.InnerParam();
797 endAlpha = segment.InnerAlpha();
798 dir = 1;
799 }
800
801 for ( int jhit = 0; jhit < segment.NClusters(); jhit++ ) {
802 int id = segment.FirstClusterRef() + jhit;
803 hits[startHit+( dir ?( segment.NClusters()-1-jhit ) :jhit )] = id;
804 }
805 nHits += segment.NClusters();
806 jtr = segment.NextNeighbour();
807 jSlice = nextSlice[jSlice];
63d8b79d 808 }
809
fbb9b71b 810 if ( endPoint.X() < startPoint.X() ) { // swap
811 for ( int i = 0; i < nHits; i++ ) hits[i] = hits[firstHit+nHits-1-i];
812 firstHit = 0;
63d8b79d 813 }
63d8b79d 814
fbb9b71b 815 if ( nHits < 30 ) continue; //SG!!!
816
817 // refit
63d8b79d 818
819 // need best t0!!!SG
820
821 endPoint = startPoint;
fbb9b71b 822
f0fb467d 823 if ( !FitTrack( endPoint, endAlpha, startPoint, startAlpha, hits + firstHit, nHits, 0,1 ) ) continue;
824 if ( !FitTrack( startPoint, startAlpha, endPoint, endAlpha, hits + firstHit, nHits, 1,1 ) ) continue;
fbb9b71b 825 if ( nHits < 30 ) continue; //SG!!!
63d8b79d 826
f0fb467d 827 AliHLTTPCCATrackParam p = startPoint;
5dc6c74b 828
829 if(0){
fbb9b71b 830 double xTPC = 83.65; //SG!!!
040db2fa 831 double dAlpha = 0.349066;
832 double ymax = 2.* xTPC * CAMath::Tan( dAlpha / 2. );
833
834 double dRot = 0;
835 if ( p.TransportToXWithMaterial( xTPC, fSliceParam.GetBz( p ) ) ) {
fbb9b71b 836 double y = p.GetY();
040db2fa 837 if ( y > ymax ) {
838 if ( p.Rotate( dAlpha ) ){
839 dRot = dAlpha;
840 p.TransportToXWithMaterial( xTPC, fSliceParam.GetBz( p ) );
841 }
842 } else if( y< -ymax ){
843 if ( p.Rotate( -dAlpha ) ){
844 dRot = -dAlpha;
845 p.TransportToXWithMaterial( xTPC, fSliceParam.GetBz( p ) );
846 }
847 }
fbb9b71b 848 }
040db2fa 849
850 if ( -ymax <= p.GetY() && p.GetY() <= ymax && p.CheckNumericalQuality() ){
851 startPoint = p;
852 startAlpha+=dRot;
853 }
63d8b79d 854 }
fbb9b71b 855
f0fb467d 856 if ( !startPoint.CheckNumericalQuality() ) continue;
63d8b79d 857
858 AliHLTTPCCAMergedTrack &mergedTrack = outTracks[nOutTracks];
fbb9b71b 859 mergedTrack.SetNClusters( nHits );
860 mergedTrack.SetFirstClusterRef( nOutTrackClusters );
861 mergedTrack.SetInnerParam( startPoint );
862 mergedTrack.SetInnerAlpha( startAlpha );
863 mergedTrack.SetOuterParam( endPoint );
864 mergedTrack.SetOuterAlpha( endAlpha );
865
866 for ( int i = 0; i < nHits; i++ ) {
867 AliHLTTPCCAClusterInfo &clu = fClusterInfos[hits[firstHit+i]];
6de2bc40 868 outClusterId[nOutTrackClusters+i] = clu.Id();
fbb9b71b 869 outClusterPackedAmp[nOutTrackClusters+i] = clu.PackedAmp();
63d8b79d 870 }
fbb9b71b 871
63d8b79d 872 nOutTracks++;
fbb9b71b 873 nOutTrackClusters += nHits;
63d8b79d 874 }
875 }
876
877 fOutput->SetNTracks( nOutTracks );
b22af1bf 878 #ifdef HLTCA_STANDALONE
7be9b0d7 879 printf("Tracks Output: %d\n", nOutTracks);
b22af1bf 880 #endif
63d8b79d 881 fOutput->SetNTrackClusters( nOutTrackClusters );
882 fOutput->SetPointers();
883
fbb9b71b 884 for ( int itr = 0; itr < nOutTracks; itr++ ) fOutput->SetTrack( itr, outTracks[itr] );
63d8b79d 885
fbb9b71b 886 for ( int ic = 0; ic < nOutTrackClusters; ic++ ) {
6de2bc40 887 fOutput->SetClusterId( ic, outClusterId[ic] );
63d8b79d 888 fOutput->SetClusterPackedAmp( ic, outClusterPackedAmp[ic] );
889 }
fbb9b71b 890
63d8b79d 891 delete[] outTracks;
6de2bc40 892 delete[] outClusterId;
63d8b79d 893 delete[] outClusterPackedAmp;
894}