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 //***************************************************************************
21 ///////////////////////////////////////////////////////////////////////////////
23 // a TPC tracker processing component for the HLT based on CA by Ivan Kisel //
25 ///////////////////////////////////////////////////////////////////////////////
31 #include "AliHLTTPCCATrackerComponent.h"
32 #include "AliHLTTPCTransform.h"
33 #include "AliHLTTPCCATracker.h"
34 #include "AliHLTTPCCAOutTrack.h"
35 #include "AliHLTTPCCAParam.h"
36 #include "AliHLTTPCCATrackConvertor.h"
37 #include "AliHLTArray.h"
39 #include "AliHLTTPCSpacePointData.h"
40 #include "AliHLTTPCClusterDataFormat.h"
41 #include "AliHLTTPCTransform.h"
42 #include "AliHLTTPCTrackSegmentData.h"
43 #include "AliHLTTPCTrackArray.h"
44 #include "AliHLTTPCTrackletDataFormat.h"
45 #include "AliHLTTPCDefinitions.h"
46 #include "AliExternalTrackParam.h"
47 #include "TStopwatch.h"
49 #include "AliCDBEntry.h"
50 #include "AliCDBManager.h"
51 #include "TObjString.h"
52 #include "TObjArray.h"
53 #include "AliHLTTPCCASliceOutput.h"
54 #include "AliHLTTPCCAClusterData.h"
56 const AliHLTComponentDataType AliHLTTPCCADefinitions::fgkTrackletsDataType = AliHLTComponentDataTypeInitializer( "CATRACKL", kAliHLTDataOriginTPC );
58 /** ROOT macro for the implementation of ROOT specific class methods */
59 ClassImp( AliHLTTPCCATrackerComponent )
61 /** global object for registration
62 * Matthias 2009-01-13 temporarily using the global object approach again.
63 * CA cade had to be disabled because of various compilation problems, so
64 * the global object approach fits better for the moment.
67 AliHLTTPCCATrackerComponent gAliHLTTPCCATrackerComponent;
69 AliHLTTPCCATrackerComponent::AliHLTTPCCATrackerComponent()
73 fMinNTrackClusters( 0 ),
80 // see header file for class documentation
82 // refer to README to build package
84 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
87 AliHLTTPCCATrackerComponent::AliHLTTPCCATrackerComponent( const AliHLTTPCCATrackerComponent& )
92 fMinNTrackClusters( 30 ),
99 // see header file for class documentation
100 HLTFatal( "copy constructor untested" );
103 AliHLTTPCCATrackerComponent& AliHLTTPCCATrackerComponent::operator=( const AliHLTTPCCATrackerComponent& )
105 // see header file for class documentation
106 HLTFatal( "assignment operator untested" );
110 AliHLTTPCCATrackerComponent::~AliHLTTPCCATrackerComponent()
112 // see header file for class documentation
117 // Public functions to implement AliHLTComponent's interface.
118 // These functions are required for the registration process
121 const char* AliHLTTPCCATrackerComponent::GetComponentID()
123 // see header file for class documentation
124 return "TPCCATracker";
127 void AliHLTTPCCATrackerComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list )
129 // see header file for class documentation
131 list.push_back( AliHLTTPCDefinitions::fgkClustersDataType );
134 AliHLTComponentDataType AliHLTTPCCATrackerComponent::GetOutputDataType()
136 // see header file for class documentation
137 if ( fNewOutputType ) return AliHLTTPCCADefinitions::fgkTrackletsDataType;
138 else return AliHLTTPCDefinitions::fgkTrackSegmentsDataType;
141 void AliHLTTPCCATrackerComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
143 // define guess for the output data size
144 constBase = 200; // minimum size
145 inputMultiplier = 0.5; // size relative to input
148 AliHLTComponent* AliHLTTPCCATrackerComponent::Spawn()
150 // see header file for class documentation
151 return new AliHLTTPCCATrackerComponent;
154 int AliHLTTPCCATrackerComponent::DoInit( int argc, const char** argv )
156 // Configure the CA tracker component
159 fMinNTrackClusters = 0;
166 if ( fTracker ) return EINPROGRESS;
167 fTracker = new AliHLTTPCCATracker();
171 TString arguments = "";
172 for ( int i = 0; i < argc; i++ ) {
173 TString argument = argv[i];
174 if ( !arguments.IsNull() ) arguments += " ";
175 arguments += argument;
177 if ( !arguments.IsNull() ) {
178 iResult = Configure( arguments.Data() );
180 iResult = Reconfigure( NULL, NULL );
186 int AliHLTTPCCATrackerComponent::DoDeinit()
188 // see header file for class documentation
189 if ( fTracker ) delete fTracker;
194 int AliHLTTPCCATrackerComponent::Reconfigure( const char* /*cdbEntry*/, const char* /*chainId*/ )
196 // see header file for class documentation
198 HLTWarning( "TODO: dummy Reconfigure() method" );
205 const char* path="HLT/ConfigTPC/CATrackerComponent";
206 const char* defaultNotify="";
209 defaultNotify=" (default)";
212 HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
213 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path);//,GetRunNo());
215 TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
217 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
218 iResult=Configure(pString->GetString().Data());
220 HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
223 HLTError("cannot fetch object \"%s\" from CDB", path);
227 const char* pathBField=kAliHLTCDBSolenoidBz;
230 HLTInfo("reconfigure B-Field from entry %s, chain id %s", path,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
231 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(pathBField);//,GetRunNo());
233 TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
235 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
236 iResult=Configure(pString->GetString().Data());
238 HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
241 HLTError("cannot fetch object \"%s\" from CDB", path);
249 bool AliHLTTPCCATrackerComponent::CompareClusters( AliHLTTPCSpacePointData *a, AliHLTTPCSpacePointData *b )
251 //* Comparison function for sorting clusters
252 if ( a->fPadRow < b->fPadRow ) return 1;
253 if ( a->fPadRow > b->fPadRow ) return 0;
254 return ( a->fZ < b->fZ );
258 int AliHLTTPCCATrackerComponent::Configure( const char* arguments )
263 if ( !arguments ) return iResult;
265 TString allArgs = arguments;
267 int bMissingParam = 0;
269 TObjArray* pTokens = allArgs.Tokenize( " " );
271 int nArgs = pTokens ? pTokens->GetEntries() : 0;
273 for ( int i = 0; i < nArgs; i++ ) {
274 argument = ( ( TObjString* )pTokens->At( i ) )->GetString();
275 if ( argument.IsNull() ) {
276 } else if ( argument.CompareTo( "-solenoidBz" ) == 0 ) {
277 if ( ( bMissingParam = ( ++i >= pTokens->GetEntries() ) ) ) break;
278 fSolenoidBz = ( ( TObjString* )pTokens->At( i ) )->GetString().Atof();
279 HLTInfo( "Magnetic Field set to: %f", fSolenoidBz );
280 } else if ( argument.CompareTo( "-minNClustersOnTrack" ) == 0 ||
281 argument.CompareTo( "-minNTrackClusters" ) == 0 ) {
282 if ( ( bMissingParam = ( ++i >= pTokens->GetEntries() ) ) ) break;
283 fMinNTrackClusters = ( ( TObjString* )pTokens->At( i ) )->GetString().Atoi();
284 HLTInfo( "minNClustersOnTrack set to: %d", fMinNTrackClusters );
285 } else if ( argument.CompareTo( "-clusterZCut" ) == 0 ) {
286 if ( ( bMissingParam = ( ++i >= pTokens->GetEntries() ) ) ) break;
287 fClusterZCut = TMath::Abs( ( ( TObjString* )pTokens->At( i ) )->GetString().Atof() );
288 HLTInfo( "ClusterZCut set to: %f", fClusterZCut );
289 } else if ( argument.CompareTo( "-newOutputType" ) == 0 ) {
291 HLTInfo( "NewOutputType is set" );
293 HLTError( "Unknown option %s ", argument.Data() );
299 if ( bMissingParam ) {
300 HLTError( "Specifier missed for %s", argument.Data() );
310 int AliHLTTPCCATrackerComponent::DoEvent
312 const AliHLTComponentEventData& evtData,
313 const AliHLTComponentBlockData* blocks,
314 AliHLTComponentTriggerData& /*trigData*/,
315 AliHLTUInt8_t* outputPtr,
316 AliHLTUInt32_t& size,
317 vector<AliHLTComponentBlockData>& outputBlocks )
320 AliHLTUInt32_t maxBufferSize = size;
321 size = 0; // output size
323 if ( GetFirstInputBlock( kAliHLTDataTypeSOR ) || GetFirstInputBlock( kAliHLTDataTypeEOR ) ) {
329 // Event reconstruction in one TPC slice with CA Tracker
331 //Logging( kHLTLogWarning, "HLT::TPCCATracker::DoEvent", "DoEvent", "CA::DoEvent()" );
332 if ( evtData.fBlockCnt <= 0 ) {
333 HLTWarning( "no blocks in event" );
337 const AliHLTComponentBlockData* iter = NULL;
339 AliHLTTPCClusterData* inPtrSP;
341 // Determine the slice number
345 std::vector<int> slices;
346 std::vector<int>::iterator slIter;
347 std::vector<unsigned> sliceCnts;
348 std::vector<unsigned>::iterator slCntIter;
350 for ( ndx = 0; ndx < evtData.fBlockCnt; ndx++ ) {
352 if ( iter->fDataType != AliHLTTPCDefinitions::fgkClustersDataType ) continue;
354 slice = AliHLTTPCDefinitions::GetMinSliceNr( *iter );
357 slCntIter = sliceCnts.begin();
358 for ( slIter = slices.begin(); slIter != slices.end(); slIter++, slCntIter++ ) {
359 if ( *slIter == slice ) {
365 slices.push_back( slice );
366 sliceCnts.push_back( 1 );
371 // Determine slice number to really use.
372 if ( slices.size() > 1 ) {
373 Logging( kHLTLogError, "HLT::TPCSliceTracker::DoEvent", "Multiple slices found in event",
374 "Multiple slice numbers found in event 0x%08lX (%lu). Determining maximum occuring slice number...",
375 evtData.fEventID, evtData.fEventID );
376 unsigned maxCntSlice = 0;
377 slCntIter = sliceCnts.begin();
378 for ( slIter = slices.begin(); slIter != slices.end(); slIter++, slCntIter++ ) {
379 Logging( kHLTLogError, "HLT::TPCSliceTracker::DoEvent", "Multiple slices found in event",
380 "Slice %lu found %lu times.", *slIter, *slCntIter );
381 if ( maxCntSlice < *slCntIter ) {
382 maxCntSlice = *slCntIter;
386 Logging( kHLTLogError, "HLT::TPCSliceTracker::DoEvent", "Multiple slices found in event",
387 "Using slice %lu.", slice );
388 } else if ( slices.size() > 0 ) {
389 slice = *( slices.begin() );
394 HLTWarning( "no slices found in event" );
399 // Initialize the tracker
403 if ( !fTracker ) fTracker = new AliHLTTPCCATracker;
405 float inRmin = 83.65;
406 // float inRmax = 133.3;
407 // float outRmin = 133.5;
408 float outRmax = 247.7;
409 float plusZmin = 0.0529937;
410 float plusZmax = 249.778;
411 float minusZmin = -249.645;
412 float minusZmax = -0.0799937;
413 float dalpha = 0.349066;
414 float alpha = 0.174533 + dalpha * iSec;
416 bool zPlus = ( iSec < 18 );
417 float zMin = zPlus ? plusZmin : minusZmin;
418 float zMax = zPlus ? plusZmax : minusZmax;
419 //TPCZmin = -249.645, ZMax = 249.778
420 // float rMin = inRmin;
421 // float rMax = outRmax;
422 int nRows = AliHLTTPCTransform::GetNRows();
424 float padPitch = 0.4;
425 float sigmaZ = 0.228808;
427 float *rowX = new float [nRows];
428 for ( int irow = 0; irow < nRows; irow++ ) {
429 rowX[irow] = AliHLTTPCTransform::Row2X( irow );
432 const double kCLight = 0.000299792458;
434 AliHLTTPCCAParam param;
436 param.Initialize( iSec, nRows, rowX, alpha, dalpha,
437 inRmin, outRmax, zMin, zMax, padPitch, sigmaZ, fSolenoidBz*kCLight );
438 param.SetHitPickUpFactor( 2 );
440 fTracker->Initialize( param );
445 // min and max patch numbers and row numbers
448 int minPatch = 100, maxPatch = -1;
452 int nClustersTotal = 0;
456 std::vector<unsigned long> patchIndices;
458 for ( ndx = 0; ndx < evtData.fBlockCnt; ndx++ ) {
460 if ( iter->fDataType != AliHLTTPCDefinitions::fgkClustersDataType ) continue;
461 if ( slice != AliHLTTPCDefinitions::GetMinSliceNr( *iter ) ) continue;
462 inPtrSP = ( AliHLTTPCClusterData* )( iter->fPtr );
463 nClustersTotal += inPtrSP->fSpacePointCnt;
464 int patch = AliHLTTPCDefinitions::GetMinPatchNr( *iter );
465 if ( minPatch > patch ) {
467 row[0] = AliHLTTPCTransform::GetFirstRow( patch );
469 if ( maxPatch < patch ) {
471 row[1] = AliHLTTPCTransform::GetLastRow( patch );
473 std::vector<unsigned long>::iterator pIter = patchIndices.begin();
474 while ( pIter != patchIndices.end() && AliHLTTPCDefinitions::GetMinPatchNr( blocks[*pIter] ) < patch ) {
477 patchIndices.insert( pIter, ndx );
481 // pass event to CA Tracker
484 Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Reading hits",
485 "Total %d hits to read for slice %d", nClustersTotal, slice );
488 AliHLTTPCCAClusterData clusterData;
489 clusterData.StartReading( slice, nClustersTotal );
491 for ( std::vector<unsigned long>::iterator pIter = patchIndices.begin(); pIter != patchIndices.end(); pIter++ ) {
495 int patch = AliHLTTPCDefinitions::GetMinPatchNr( *iter );
496 inPtrSP = ( AliHLTTPCClusterData* )( iter->fPtr );
498 Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Reading hits",
499 "Reading %d hits for slice %d - patch %d", inPtrSP->fSpacePointCnt, slice, patch );
501 for ( unsigned int i = 0; i < inPtrSP->fSpacePointCnt; i++ ) {
502 AliHLTTPCSpacePointData *c = &( inPtrSP->fSpacePoints[i] );
503 if ( CAMath::Abs( c->fZ ) > fClusterZCut ) continue;
504 clusterData.ReadCluster( c->fID, c->fPadRow, c->fX, c->fY, c->fZ, c->fCharge );
508 clusterData.FinishReading();
510 // reconstruct the event
512 TStopwatch timerReco;
514 fTracker->ReadEvent( &clusterData );
516 fTracker->Reconstruct();
522 Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Reconstruct",
523 "%d tracks found for slice %d", fTracker->NOutTracks(), slice );
526 // write reconstructed tracks
528 unsigned int mySize = 0;
529 int ntracks = *fTracker->NOutTracks();
532 if ( !fNewOutputType ) {
534 AliHLTTPCTrackletData* outPtr = ( AliHLTTPCTrackletData* )( outputPtr );
536 AliHLTTPCTrackSegmentData* currOutTracklet = outPtr->fTracklets;
538 mySize = ( ( AliHLTUInt8_t * )currOutTracklet ) - ( ( AliHLTUInt8_t * )outputPtr );
540 outPtr->fTrackletCnt = 0;
542 for ( int itr = 0; itr < ntracks; itr++ ) {
544 AliHLTTPCCAOutTrack &t = fTracker->OutTracks()[itr];
546 //Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Wrtite output","track %d with %d hits", itr, t.NHits());
548 if ( t.NHits() < fMinNTrackClusters ) continue;
550 // calculate output track size
552 unsigned int dSize = sizeof( AliHLTTPCTrackSegmentData ) + t.NHits() * sizeof( unsigned int );
554 if ( mySize + dSize > maxBufferSize ) {
555 HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), %d tracks are not stored", maxBufferSize, mySize, ntracks - itr + 1 );
560 // convert CA track parameters to HLT Track Segment
562 int iFirstRow = 1000;
564 int iFirstHit = fTracker->OutTrackHits()[t.FirstHitRef()];
565 int iLastHit = iFirstHit;
566 for ( int ih = 0; ih < t.NHits(); ih++ ) {
567 int hitID = fTracker->OutTrackHits()[t.FirstHitRef() + ih ];
568 int iRow = clusterData.RowNumber( hitID );
569 if ( iRow < iFirstRow ) { iFirstRow = iRow; iFirstHit = hitID; }
570 if ( iRow > iLastRow ) { iLastRow = iRow; iLastHit = hitID; }
573 AliHLTTPCCATrackParam par = t.StartPoint();
575 par.TransportToX( clusterData.X( iFirstHit ), .99 );
577 AliExternalTrackParam tp;
578 AliHLTTPCCATrackConvertor::GetExtParam( par, tp, 0 );
580 currOutTracklet->fX = tp.GetX();
581 currOutTracklet->fY = tp.GetY();
582 currOutTracklet->fZ = tp.GetZ();
583 currOutTracklet->fCharge = ( int ) tp.GetSign();
584 currOutTracklet->fPt = TMath::Abs( tp.GetSignedPt() );
585 float snp = tp.GetSnp() ;
586 if ( snp > .999 ) snp = .999;
587 if ( snp < -.999 ) snp = -.999;
588 currOutTracklet->fPsi = TMath::ASin( snp );
589 currOutTracklet->fTgl = tp.GetTgl();
591 currOutTracklet->fY0err = tp.GetSigmaY2();
592 currOutTracklet->fZ0err = tp.GetSigmaZ2();
593 float h = -currOutTracklet->fPt * currOutTracklet->fPt;
594 currOutTracklet->fPterr = h * h * tp.GetSigma1Pt2();
595 h = 1. / TMath::Sqrt( 1 - snp * snp );
596 currOutTracklet->fPsierr = h * h * tp.GetSigmaSnp2();
597 currOutTracklet->fTglerr = tp.GetSigmaTgl2();
599 if ( par.TransportToX( clusterData.X( iLastHit ), .99 ) ) {
600 currOutTracklet->fLastX = par.GetX();
601 currOutTracklet->fLastY = par.GetY();
602 currOutTracklet->fLastZ = par.GetZ();
604 currOutTracklet->fLastX = clusterData.X( iLastHit );
605 currOutTracklet->fLastY = clusterData.Y( iLastHit );
606 currOutTracklet->fLastZ = clusterData.Z( iLastHit );
608 //if( currOutTracklet->fLastX<10. ) {
609 //HLTError("CA last point: hitxyz=%f,%f,%f, track=%f,%f,%f, tracklet=%f,%f,%f, nhits=%d",clusterData.X( iLastHit ),clusterData.Y( iLastHit],clusterData.Z( iLastHit],
610 //par.GetX(), par.GetY(),par.GetZ(),currOutTracklet->fLastX,currOutTracklet->fLastY ,currOutTracklet->fLastZ, t.NHits());
612 #ifdef INCLUDE_TPC_HOUGH
613 #ifdef ROWHOUGHPARAMS
614 currOutTracklet->fTrackID = 0;
615 currOutTracklet->fRowRange1 = clusterData.RowNumber( iFirstHit );
616 currOutTracklet->fRowRange2 = clusterData.RowNumber( iLastHit );
617 currOutTracklet->fSector = slice;
618 currOutTracklet->fPID = 211;
620 #endif // INCLUDE_TPC_HOUGH
623 currOutTracklet->fNPoints = t.NHits();
625 for ( int i = 0; i < t.NHits(); i++ ) {
626 currOutTracklet->fPointIDs[i] = clusterData.Id( fTracker->OutTrackHits()[t.FirstHitRef()+i] );
629 currOutTracklet = ( AliHLTTPCTrackSegmentData* )( ( Byte_t * )currOutTracklet + dSize );
631 outPtr->fTrackletCnt++;
633 } else { // new output type
635 mySize = fTracker->Output()->EstimateSize( fTracker->Output()->NTracks(),
636 fTracker->Output()->NTrackClusters() );
637 if ( mySize <= maxBufferSize ) {
638 const AliHLTUInt8_t* outputevent = reinterpret_cast<const AliHLTUInt8_t*>( fTracker->Output() );
639 for ( unsigned int i = 0; i < mySize; i++ ) outputPtr[i] = outputevent[i];
641 HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), tracks are not stored", maxBufferSize, mySize );
648 AliHLTComponentBlockData bd;
652 bd.fSpecification = AliHLTTPCDefinitions::EncodeDataSpecification( slice, slice, minPatch, maxPatch );
653 outputBlocks.push_back( bd );
659 fFullTime += timer.RealTime();
660 fRecoTime += timerReco.RealTime();
663 // Set log level to "Warning" for on-line system monitoring
664 int hz = ( int ) ( fFullTime > 1.e-10 ? fNEvents / fFullTime : 100000 );
665 int hz1 = ( int ) ( fRecoTime > 1.e-10 ? fNEvents / fRecoTime : 100000 );
666 HLTWarning( "CATracker slice %d: output %d tracks; input %d clusters, patches %d..%d, rows %d..%d; reco time %d/%d Hz",
667 slice, ntracks, nClustersTotal, minPatch, maxPatch, row[0], row[1], hz, hz1 );