2 // **************************************************************************
3 // This file is property of and copyright by the ALICE HLT Project *
4 // ALICE Experiment at CERN, All rights reserved. *
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. *
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. *
18 //***************************************************************************
20 #include "AliTPCtrackerCA.h"
23 #include "Riostream.h"
24 //#include "AliCluster.h"
25 #include "AliTPCClustersRow.h"
26 #include "AliTPCParam.h"
27 #include "AliTPCClusterParam.h"
30 #include "AliRunLoader.h"
33 #include "AliHLTTPCCAGBTracker.h"
34 #include "AliHLTTPCCAGBHit.h"
35 #include "AliHLTTPCCAGBTrack.h"
36 #include "AliHLTTPCCAPerformance.h"
37 #include "AliHLTTPCCAParam.h"
38 #include "AliHLTTPCCATrackConvertor.h"
39 #include "AliHLTTPCCATracker.h"
42 #include "AliTPCLoader.h"
44 #include "AliTPCclusterMI.h"
45 #include "AliTPCTransform.h"
46 #include "AliTPCcalibDB.h"
47 #include "AliTPCtrack.h"
48 #include "AliTPCseed.h"
49 #include "AliESDtrack.h"
50 #include "AliESDEvent.h"
51 #include "AliTrackReference.h"
52 #include "TStopwatch.h"
53 #include "AliTPCReconstructor.h"
55 //#include <fstream.h>
57 ClassImp( AliTPCtrackerCA )
59 AliTPCtrackerCA::AliTPCtrackerCA()
60 : AliTracker(), fkParam( 0 ), fClusters( 0 ), fNClusters( 0 ), fHLTTracker( 0 ), fDoHLTPerformance( 0 ), fDoHLTPerformanceClusters( 0 ), fStatNEvents( 0 )
62 //* default constructor
65 AliTPCtrackerCA::AliTPCtrackerCA( const AliTPCtrackerCA & ):
66 AliTracker(), fkParam( 0 ), fClusters( 0 ), fNClusters( 0 ), fHLTTracker( 0 ), fDoHLTPerformance( 0 ), fDoHLTPerformanceClusters( 0 ), fStatNEvents( 0 )
71 const AliTPCtrackerCA & AliTPCtrackerCA::operator=( const AliTPCtrackerCA& ) const
78 AliTPCtrackerCA::~AliTPCtrackerCA()
81 if ( fClusters ) delete[] fClusters;
82 if ( fHLTTracker ) delete fHLTTracker;
85 //#include "AliHLTTPCCADisplay.h"
87 AliTPCtrackerCA::AliTPCtrackerCA( const AliTPCParam *par ):
88 AliTracker(), fkParam( par ), fClusters( 0 ), fNClusters( 0 ), fHLTTracker( 0 ), fDoHLTPerformance( 0 ), fDoHLTPerformanceClusters( 0 ), fStatNEvents( 0 )
92 fDoHLTPerformance = 0;
93 fDoHLTPerformanceClusters = 0;
95 fHLTTracker = new AliHLTTPCCAGBTracker;
96 fHLTTracker->SetNSlices( fkParam->GetNSector() / 2 );
98 if ( fDoHLTPerformance ) {
99 AliHLTTPCCAPerformance::Instance().SetTracker( fHLTTracker );
102 for ( int iSlice = 0; iSlice < fHLTTracker->NSlices(); iSlice++ ) {
104 const double kCLight = 0.000299792458;
106 float bz = AliTracker::GetBz() * kCLight;
108 float inRmin = fkParam->GetInnerRadiusLow();
109 //float inRmax = fkParam->GetInnerRadiusUp();
110 //float outRmin = fkParam->GetOuterRadiusLow();
111 float outRmax = fkParam->GetOuterRadiusUp();
112 float plusZmin = 0.0529937;
113 float plusZmax = 249.778;
114 float minusZmin = -249.645;
115 float minusZmax = -0.0799937;
116 float dalpha = 0.349066;
117 float alpha = 0.174533 + dalpha * iSlice;
119 bool zPlus = ( iSlice < 18 );
120 float zMin = zPlus ? plusZmin : minusZmin;
121 float zMax = zPlus ? plusZmax : minusZmax;
122 //TPCZmin = -249.645, ZMax = 249.778
123 //float rMin = inRmin;
124 //float rMax = outRmax;
126 float padPitch = 0.4;
127 float sigmaZ = 0.228808;
129 int nRows = fkParam->GetNRowLow() + fkParam->GetNRowUp();
131 for ( int irow = 0; irow < fkParam->GetNRowLow(); irow++ ) {
132 rowX[irow] = fkParam->GetPadRowRadiiLow( irow );
134 for ( int irow = 0; irow < fkParam->GetNRowUp(); irow++ ) {
135 rowX[fkParam->GetNRowLow()+irow] = fkParam->GetPadRowRadiiUp( irow );
137 AliHLTTPCCAParam param;
138 param.Initialize( iSlice, nRows, rowX, alpha, dalpha,
139 inRmin, outRmax, zMin, zMax, padPitch, sigmaZ, bz );
140 param.SetHitPickUpFactor( 1. );
141 param.SetMaxTrackMatchDRow( 5 );
142 param.SetTrackConnectionFactor( 3.5 );
144 AliTPCClusterParam * clparam = AliTPCcalibDB::Instance()->GetClusterParam();
145 for ( int iRow = 0; iRow < nRows; iRow++ ) {
146 int type = ( iRow < 63 ) ? 0 : ( ( iRow > 126 ) ? 1 : 2 );
147 for ( int iyz = 0; iyz < 2; iyz++ ) {
148 for ( int k = 0; k < 7; k++ ) {
149 //std::cout<<param.fParamS0Par[iyz][type][k]<<" "<<clparam->fParamS0Par[iyz][type][k] - param.fParamS0Par[iyz][type][k]<<std::endl;
150 param.SetParamS0Par( iyz, type, k, clparam->fParamS0Par[iyz][type][k] );
154 fHLTTracker->Slices()[iSlice].Initialize( param );
160 int AliTPCtrackerCA::LoadClusters ( TTree * fromTree )
162 // load clusters to the local arrays
164 if ( fClusters ) delete[] fClusters;
166 fHLTTracker->StartEvent();
167 if ( fDoHLTPerformance ) AliHLTTPCCAPerformance::Instance().StartEvent();
169 if ( !fkParam ) return 1;
172 while ( fDoHLTPerformance ) {
173 if ( !gAlice ) break;
174 AliRunLoader *rl = AliRunLoader::Instance();//gAlice->GetRunLoader();
176 rl->LoadKinematics();
177 AliStack *stack = rl->Stack();
180 AliHLTTPCCAPerformance::Instance().SetNMCTracks( stack->GetNtrack() );
182 for ( int itr = 0; itr < stack->GetNtrack(); itr++ ) {
183 TParticle *part = stack->Particle( itr );
184 AliHLTTPCCAPerformance::Instance().ReadMCTrack( itr, part );
187 { // check for MC tracks at the TPC entrance
190 isTPC = new bool [stack->GetNtrack()];
191 for ( int i = 0; i < stack->GetNtrack(); i++ ) isTPC[i] = 0;
193 TTree *mcTree = rl->TreeTR();
194 if ( !mcTree ) break;
195 TBranch *branch = mcTree->GetBranch( "TrackReferences" );
196 if ( !branch ) break;
197 TClonesArray tpcdummy( "AliTrackReference", 1000 ), *tpcRefs = &tpcdummy;
198 branch->SetAddress( &tpcRefs );
199 int nr = ( int )mcTree->GetEntries();
200 for ( int r = 0; r < nr; r++ ) {
201 mcTree->GetEvent( r );
202 int nref = tpcRefs->GetEntriesFast();
203 if ( !nref ) continue;
204 AliTrackReference *tpcRef = 0x0;
205 for ( int iref = 0; iref < nref; ++iref ) {
206 tpcRef = ( AliTrackReference* )tpcRefs->UncheckedAt( iref );
207 if ( tpcRef->DetectorId() == AliTrackReference::kTPC ) break;
210 if ( !tpcRef ) continue;
212 if ( isTPC[tpcRef->Label()] ) continue;
214 AliHLTTPCCAPerformance::Instance().ReadMCTPCTrack( tpcRef->Label(),
215 tpcRef->X(), tpcRef->Y(), tpcRef->Z(),
216 tpcRef->Px(), tpcRef->Py(), tpcRef->Pz() );
217 isTPC[tpcRef->Label()] = 1;
220 if ( isTPC ) delete[] isTPC;
223 while ( fDoHLTPerformanceClusters ) {
224 AliTPCLoader *tpcl = ( AliTPCLoader* ) rl->GetDetectorLoader( "TPC" );
226 if ( tpcl->TreeH() == 0x0 ) {
227 if ( tpcl->LoadHits() ) break;
229 if ( tpcl->TreeH() == 0x0 ) break;
231 AliTPC *tpc = ( AliTPC* ) gAlice->GetDetector( "TPC" );
232 int nEnt = ( int )tpcl->TreeH()->GetEntries();
234 for ( int iEnt = 0; iEnt < nEnt; iEnt++ ) {
236 tpcl->TreeH()->GetEvent( iEnt );
237 AliTPChit *phit = ( AliTPChit* )tpc->FirstHit( -1 );
238 for ( ; phit; phit = ( AliTPChit* )tpc->NextHit() ) nPoints++;
240 AliHLTTPCCAPerformance::Instance().SetNMCPoints( nPoints );
242 for ( int iEnt = 0; iEnt < nEnt; iEnt++ ) {
244 tpcl->TreeH()->GetEvent( iEnt );
245 AliTPChit *phit = ( AliTPChit* )tpc->FirstHit( -1 );
246 for ( ; phit; phit = ( AliTPChit* )tpc->NextHit() ) {
247 AliHLTTPCCAPerformance::Instance().ReadMCPoint( phit->GetTrack(), phit->X(), phit->Y(), phit->Z(), phit->Time(), phit->fSector % 36 );
255 TBranch * br = fromTree->GetBranch( "Segment" );
258 AliTPCClustersRow *clrow = new AliTPCClustersRow;
259 clrow->SetClass( "AliTPCclusterMI" );
260 clrow->SetArray( 0 );
261 clrow->GetArray()->ExpandCreateFast( 10000 );
263 br->SetAddress( &clrow );
266 int nEnt = int( fromTree->GetEntries() );
269 for ( int i = 0; i < nEnt; i++ ) {
272 fkParam->AdjustSectorRow( clrow->GetID(), sec, row );
273 fNClusters += clrow->GetArray()->GetEntriesFast();
276 fClusters = new AliTPCclusterMI [fNClusters];
277 fHLTTracker->SetNHits( fNClusters );
278 if ( fDoHLTPerformance ) AliHLTTPCCAPerformance::Instance().SetNHits( fNClusters );
280 for ( int i = 0; i < nEnt; i++ ) {
283 fkParam->AdjustSectorRow( clrow->GetID(), sec, row );
284 int nClu = clrow->GetArray()->GetEntriesFast();
285 float x = fkParam->GetPadRowRadii( sec, row );
286 for ( int icl = 0; icl < nClu; icl++ ) {
290 AliTPCclusterMI* cluster = ( AliTPCclusterMI* )( clrow->GetArray()->At( icl ) );
291 if ( !cluster ) continue;
292 lab0 = cluster->GetLabel( 0 );
293 lab1 = cluster->GetLabel( 1 );
294 lab2 = cluster->GetLabel( 2 );
296 AliTPCTransform *transform = AliTPCcalibDB::Instance()->GetTransform() ;
298 AliFatal( "Tranformations not in calibDB" );
300 double xx[3] = {cluster->GetRow(), cluster->GetPad(), cluster->GetTimeBin()};
301 int id[1] = {cluster->GetDetector()};
302 transform->Transform( xx, id, 0, 1 );
303 //if (!AliTPCReconstructor::GetRecoParam()->GetBYMirror()){
304 //if (cluster->GetDetector()%36>17){
309 cluster->SetX( xx[0] );
310 cluster->SetY( xx[1] );
311 cluster->SetZ( xx[2] );
313 TGeoHMatrix *mat = fkParam->GetClusterMatrix( cluster->GetDetector() );
314 double pos[3] = {cluster->GetX(), cluster->GetY(), cluster->GetZ()};
315 double posC[3] = {cluster->GetX(), cluster->GetY(), cluster->GetZ()};
316 if ( mat ) mat->LocalToMaster( pos, posC );
318 // chack Loading of Geo matrices from GeoManager - TEMPORARY FIX
320 cluster->SetX( posC[0] );
321 cluster->SetY( posC[1] );
322 cluster->SetZ( posC[2] );
325 float y = cluster->GetY();
326 float z = cluster->GetZ();
330 row = row + fkParam->GetNRowLow();
334 fClusters[index] = *cluster;
335 fHLTTracker->ReadHit( x, y, z,
336 TMath::Sqrt( cluster->GetSigmaY2() ), TMath::Sqrt( cluster->GetSigmaZ2() ),
337 cluster->GetMax(), index, sec, row );
338 if ( fDoHLTPerformance ) AliHLTTPCCAPerformance::Instance().ReadHitLabel( index, lab0, lab1, lab2 );
345 AliCluster * AliTPCtrackerCA::GetCluster( int index ) const
347 return &( fClusters[index] );
350 int AliTPCtrackerCA::Clusters2Tracks( AliESDEvent *event )
353 //cout<<"Start of AliTPCtrackerCA"<<endl;
356 fHLTTracker->FindTracks();
357 //cout<<"Do performance.."<<endl;
358 if ( fDoHLTPerformance ) AliHLTTPCCAPerformance::Instance().Performance();
360 if ( 0 ) {// Write Event
361 if ( fStatNEvents == 0 ) {
363 geo.open( "CAEvents/settings.dat", ios::out );
364 if ( geo.is_open() ) {
365 fHLTTracker->WriteSettings( geo );
372 sprintf( name, "CAEvents/%i.event.dat", fStatNEvents );
373 hits.open( name, ios::out );
374 if ( hits.is_open() ) {
375 fHLTTracker->WriteEvent( hits );
377 sprintf( name, "CAEvents/%i.tracks.dat", fStatNEvents );
378 tracks.open( name, ios::out );
379 fHLTTracker->WriteTracks( tracks );
382 if ( fDoHLTPerformance ) {
383 fstream mcevent, mcpoints;
385 sprintf( mcname, "CAEvents/%i.mcevent.dat", fStatNEvents );
386 mcevent.open( mcname, ios::out );
387 if ( mcevent.is_open() ) {
388 AliHLTTPCCAPerformance::Instance().WriteMCEvent( mcevent );
390 if ( 1 && fDoHLTPerformanceClusters ) {
391 sprintf( mcname, "CAEvents/%i.mcpoints.dat", fStatNEvents );
392 mcpoints.open( mcname, ios::out );
393 if ( mcpoints.is_open() ) {
394 AliHLTTPCCAPerformance::Instance().WriteMCPoints( mcpoints );
405 for ( int itr = 0; itr < fHLTTracker->NTracks(); itr++ ) {
408 AliHLTTPCCAGBTrack &tCA = fHLTTracker->Tracks()[itr];
409 AliHLTTPCCATrackParam par = tCA.Param();
410 AliHLTTPCCATrackConvertor::GetExtParam( par, tTPC, tCA.Alpha() );
411 tTPC.SetMass( 0.13957 );
412 tTPC.SetdEdx( tCA.DeDx() );
413 if ( TMath::Abs( tTPC.GetSigned1Pt() ) > 1. / 0.02 ) continue;
414 int nhits = tCA.NHits();
417 firstHit = nhits - 160;
421 tTPC.SetNumberOfClusters( nhits );
423 float alpha = tCA.Alpha();
424 AliHLTTPCCATrackParam t0 = par;
425 for ( int ih = 0; ih < nhits; ih++ ) {
426 int index = fHLTTracker->TrackHits()[tCA.FirstHitRef()+firstHit+ih];
427 const AliHLTTPCCAGBHit &h = fHLTTracker->Hits()[index];
428 int extIndex = h.ID();
429 tTPC.SetClusterIndex( ih, extIndex );
431 AliTPCclusterMI *c = &( fClusters[extIndex] );
432 tTPC.SetClusterPointer( h.IRow(), c );
433 AliTPCTrackerPoint &point = *( tTPC.GetTrackPoint( h.IRow() ) );
435 int iSlice = h.ISlice();
436 AliHLTTPCCATracker &slice = fHLTTracker->Slices()[iSlice];
437 if ( slice.Param().Alpha() != alpha ) {
438 if ( ! t0.Rotate( slice.Param().Alpha() - alpha, .999 ) ) continue;
439 alpha = slice.Param().Alpha();
441 float x = slice.Row( h.IRow() ).X();
442 if ( !t0.TransportToX( x, fHLTTracker->Slices()[0].Param().GetBz( t0 ), .999 ) ) continue;
444 slice.GetErrors2( h.IRow(), t0, sy2, sz2 );
445 point.SetSigmaY( c->GetSigmaY2() / sy2 );
446 point.SetSigmaZ( c->GetSigmaZ2() / sz2 );
447 point.SetAngleY( TMath::Abs( t0.GetSinPhi() / t0.GetCosPhi() ) );
448 point.SetAngleZ( TMath::Abs( t0.GetDzDs() ) );
451 tTPC.CookdEdx( 0.02, 0.6 );
453 CookLabel( &tTPC, 0.1 );
455 if ( 1 ) { // correction like in off-line --- Adding systematic error
457 const double *param = AliTPCReconstructor::GetRecoParam()->GetSystematicError();
459 for ( int i = 0; i < 15; i++ ) covar[i] = 0;
460 covar[0] = param[0] * param[0];
461 covar[2] = param[1] * param[1];
462 covar[5] = param[2] * param[2];
463 covar[9] = param[3] * param[3];
464 double facC = AliTracker::GetBz() * kB2C;
465 covar[14] = param[4] * param[4] * facC * facC;
466 tTPC.AddCovariance( covar );
470 tESD.UpdateTrackParams( &( tTPC ), AliESDtrack::kTPCin );
471 //tESD.SetStatus( AliESDtrack::kTPCrefit );
472 //tESD.SetTPCPoints(tTPC.GetPoints());
473 int ndedx = tTPC.GetNCDEDX( 0 );
474 float sdedx = tTPC.GetSDEDX( 0 );
475 float dedx = tTPC.GetdEdx();
476 tESD.SetTPCsignal( dedx, sdedx, ndedx );
479 event->AddTrack( &tESD );
483 static double time = 0, time1 = 0;
484 static int ncalls = 0;
485 time += timer.CpuTime();
486 time1 += timer.RealTime();
488 //cout<<"\n\nCA tracker speed: cpu = "<<time/ncalls*1.e3<<" [ms/ev], real = "<<time1/ncalls*1.e3<<" [ms/ev], n calls = "<<ncalls<<endl<<endl;
490 //cout<<"End of AliTPCtrackerCA"<<endl;
495 int AliTPCtrackerCA::RefitInward ( AliESDEvent *event )
497 //* forward propagation of ESD tracks
499 //float bz = fHLTTracker->Slices()[0].Param().Bz();
500 float xTPC = fkParam->GetInnerRadiusLow();
501 float dAlpha = fkParam->GetInnerAngle() / 180.*TMath::Pi();
502 float yMax = xTPC * TMath::Tan( dAlpha / 2. );
504 int nentr = event->GetNumberOfTracks();
506 for ( int itr = 0; itr < nentr; itr++ ) {
507 AliESDtrack *esd = event->GetTrack( itr );
508 ULong_t status = esd->GetStatus();
509 if ( !( status&AliESDtrack::kTPCin ) ) continue;
510 AliHLTTPCCATrackParam t0;
511 AliHLTTPCCATrackConvertor::SetExtParam( t0, *esd );
512 AliHLTTPCCATrackParam t = t0;
513 float alpha = esd->GetAlpha();
516 int nHits = esd->GetTPCclusters( hits );
518 // convert clluster indices to AliHLTTPCCAGBHit indices
520 for ( int i = 0; i < nHits; i++ ) hits[i] = fHLTTracker->Ext2IntHitID( hits[i] );
522 bool ok = fHLTTracker->FitTrack( t, t0, alpha, hits, nHits, 0 );
523 if ( ok && nHits > 15 ) {
524 if ( t.TransportToXWithMaterial( xTPC, fHLTTracker->Slices()[0].Param().GetBz( t ) ) ) {
525 if ( t.GetY() > yMax ) {
526 if ( t.Rotate( dAlpha ) ) {
528 t.TransportToXWithMaterial( xTPC, fHLTTracker->Slices()[0].Param().GetBz( t ) );
530 } else if ( t.GetY() < -yMax ) {
531 if ( t.Rotate( -dAlpha ) ) {
533 t.TransportToXWithMaterial( xTPC, fHLTTracker->Slices()[0].Param().GetBz( t ) );
538 AliTPCtrack tt( *esd );
539 if ( AliHLTTPCCATrackConvertor::GetExtParam( t, tt, alpha ) ) {
540 if ( t.X() > 50 ) esd->UpdateTrackParams( &tt, AliESDtrack::kTPCrefit );
547 int AliTPCtrackerCA::PropagateBack( AliESDEvent *event )
550 //* backward propagation of ESD tracks
552 //float bz = fHLTTracker->Slices()[0].Param().Bz();
553 int nentr = event->GetNumberOfTracks();
555 for ( int itr = 0; itr < nentr; itr++ ) {
557 AliESDtrack *esd = event->GetTrack( itr );
558 ULong_t status = esd->GetStatus();
559 if ( !( status&AliESDtrack::kTPCin ) ) continue;
561 AliHLTTPCCATrackParam t0;
562 AliHLTTPCCATrackConvertor::SetExtParam( t0, *esd );
563 AliHLTTPCCATrackParam t = t0;
564 float alpha = esd->GetAlpha();
567 int nHits = esd->GetTPCclusters( hits );
569 // convert clluster indices to AliHLTTPCCAGBHit indices
571 for ( int i = 0; i < nHits; i++ ) hits[i] = fHLTTracker->Ext2IntHitID( hits[i] );
573 bool ok = fHLTTracker->FitTrack( t, t0, alpha, hits, nHits, 1 );
574 if ( ok && nHits > 15 ) {
575 AliTPCtrack tt( *esd );
576 if ( AliHLTTPCCATrackConvertor::GetExtParam( t, tt, alpha ) ) {
577 if ( t.X() > 50 ) esd->UpdateTrackParams( &tt, AliESDtrack::kTPCout );