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"
38 #include "AliHLTTPCSpacePointData.h"
39 #include "AliHLTTPCClusterDataFormat.h"
40 #include "AliHLTTPCTransform.h"
41 #include "AliHLTTPCTrackSegmentData.h"
42 #include "AliHLTTPCTrackArray.h"
43 #include "AliHLTTPCTrackletDataFormat.h"
44 #include "AliHLTTPCDefinitions.h"
45 #include "AliExternalTrackParam.h"
46 #include "TStopwatch.h"
48 #include "AliCDBEntry.h"
49 #include "AliCDBManager.h"
50 #include "TObjString.h"
51 #include "TObjArray.h"
53 /** global object for registration
54 * Matthias 2009-01-13 temporarily using the global object approach again.
55 * CA cade had to be disabled because of various compilation problems, so
56 * the global object approach fits better for the moment.
58 AliHLTTPCCATrackerComponent gAliHLTTPCCATrackerComponent;
60 /** ROOT macro for the implementation of ROOT specific class methods */
61 ClassImp(AliHLTTPCCATrackerComponent)
63 AliHLTTPCCATrackerComponent::AliHLTTPCCATrackerComponent()
67 fMinNTrackClusters(0),
73 // see header file for class documentation
75 // refer to README to build package
77 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
80 AliHLTTPCCATrackerComponent::AliHLTTPCCATrackerComponent(const AliHLTTPCCATrackerComponent&)
85 fMinNTrackClusters(30),
91 // see header file for class documentation
92 HLTFatal("copy constructor untested");
95 AliHLTTPCCATrackerComponent& AliHLTTPCCATrackerComponent::operator=(const AliHLTTPCCATrackerComponent&)
97 // see header file for class documentation
98 HLTFatal("assignment operator untested");
102 AliHLTTPCCATrackerComponent::~AliHLTTPCCATrackerComponent()
104 // see header file for class documentation
109 // Public functions to implement AliHLTComponent's interface.
110 // These functions are required for the registration process
113 const char* AliHLTTPCCATrackerComponent::GetComponentID()
115 // see header file for class documentation
116 return "TPCCATracker";
119 void AliHLTTPCCATrackerComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list)
121 // see header file for class documentation
123 list.push_back( AliHLTTPCDefinitions::fgkClustersDataType );
126 AliHLTComponentDataType AliHLTTPCCATrackerComponent::GetOutputDataType()
128 // see header file for class documentation
129 return AliHLTTPCDefinitions::fgkTrackSegmentsDataType;
132 void AliHLTTPCCATrackerComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
134 // define guess for the output data size
135 constBase = 200; // minimum size
136 inputMultiplier = 0.5; // size relative to input
139 AliHLTComponent* AliHLTTPCCATrackerComponent::Spawn()
141 // see header file for class documentation
142 return new AliHLTTPCCATrackerComponent;
145 Int_t AliHLTTPCCATrackerComponent::DoInit( Int_t argc, const char** argv )
147 // Configure the CA tracker component
150 fMinNTrackClusters = 0;
156 if ( fTracker ) return EINPROGRESS;
157 fTracker = new AliHLTTPCCATracker();
159 Int_t iResult = EINVAL;
161 TString arguments="";
162 for (int i=0; i<argc; i++) {
163 TString argument=argv[i];
164 if (!arguments.IsNull()) arguments+=" ";
167 if (!arguments.IsNull()) {
168 iResult=Configure(arguments.Data());
170 iResult=Reconfigure(NULL, NULL);
176 Int_t AliHLTTPCCATrackerComponent::DoDeinit()
178 // see header file for class documentation
179 if ( fTracker ) delete fTracker;
184 Int_t AliHLTTPCCATrackerComponent::Reconfigure(const char* cdbEntry, const char* chainId)
186 // see header file for class documentation
188 Int_t iResult=EINVAL;
189 const char* path="HLT/ConfigTPC/CATrackerComponent";
190 const char* defaultNotify="";
193 defaultNotify=" (default)";
196 HLTInfo("reconfigure from entry %s%s, chain id %s", path, defaultNotify,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
197 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(path/*,GetRunNo()*/);
199 TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
201 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
202 iResult=Configure(pString->GetString().Data());
204 HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
207 HLTError("cannot fetch object \"%s\" from CDB", path);
211 const char* pathBField=kAliHLTCDBSolenoidBz;
214 HLTInfo("reconfigure B-Field from entry %s, chain id %s", path,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
215 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(pathBField/*,GetRunNo()*/);
217 TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
219 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
220 iResult=Configure(pString->GetString().Data());
222 HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
225 HLTError("cannot fetch object \"%s\" from CDB", path);
232 Bool_t AliHLTTPCCATrackerComponent::CompareClusters(AliHLTTPCSpacePointData *a, AliHLTTPCSpacePointData *b)
234 //* Comparison function for sorting clusters
235 if( a->fPadRow<b->fPadRow ) return 1;
236 if( a->fPadRow>b->fPadRow ) return 0;
237 return (a->fZ < b->fZ);
241 Int_t AliHLTTPCCATrackerComponent::Configure( const char* arguments )
245 Int_t iResult=EINVAL;
246 if (!arguments) return iResult;
248 TString allArgs=arguments;
250 Int_t bMissingParam=0;
252 TObjArray* pTokens=allArgs.Tokenize(" ");
254 Int_t nArgs = pTokens ?pTokens->GetEntries() :0;
256 for (int i=0; i<nArgs; i++ ){
257 argument=((TObjString*)pTokens->At(i))->GetString();
258 if (argument.IsNull()){
260 else if (argument.CompareTo("-solenoidBz")==0 ){
261 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
262 fSolenoidBz = ((TObjString*)pTokens->At(i))->GetString().Atof();
263 HLTInfo("Magnetic Field set to: %f", fSolenoidBz );
265 else if ( argument.CompareTo("-minNClustersOnTrack")==0 ||
266 argument.CompareTo("-minNTrackClusters")==0 ){
267 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
268 fMinNTrackClusters = ((TObjString*)pTokens->At(i))->GetString().Atoi();
269 HLTInfo("minNClustersOnTrack set to: %d", fMinNTrackClusters );
271 else if ( argument.CompareTo("-clusterZCut")==0 ){
272 if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;
273 fClusterZCut = TMath::Abs( ((TObjString*)pTokens->At(i))->GetString().Atof());
274 HLTInfo("ClusterZCut set to: %f", fClusterZCut );
277 HLTError("Unknown option %s ", argument.Data());
284 HLTError("Specifier missed for %s", argument.Data());
294 Int_t AliHLTTPCCATrackerComponent::DoEvent
296 const AliHLTComponentEventData& evtData,
297 const AliHLTComponentBlockData* blocks,
298 AliHLTComponentTriggerData& /*trigData*/,
299 AliHLTUInt8_t* outputPtr,
300 AliHLTUInt32_t& size,
301 vector<AliHLTComponentBlockData>& outputBlocks )
304 AliHLTUInt32_t maxBufferSize = size;
305 size = 0; // output size
307 if(GetFirstInputBlock( kAliHLTDataTypeSOR ) || GetFirstInputBlock( kAliHLTDataTypeEOR )){
313 // Event reconstruction in one TPC slice with CA Tracker
315 //Logging( kHLTLogWarning, "HLT::TPCCATracker::DoEvent", "DoEvent", "CA::DoEvent()" );
316 if ( evtData.fBlockCnt<=0 )
318 Logging( kHLTLogWarning, "HLT::TPCCATracker::DoEvent", "DoEvent", "no blocks in event" );
322 const AliHLTComponentBlockData* iter = NULL;
324 AliHLTTPCClusterData* inPtrSP;
326 // Determine the slice number
330 std::vector<Int_t> slices;
331 std::vector<Int_t>::iterator slIter;
332 std::vector<unsigned> sliceCnts;
333 std::vector<unsigned>::iterator slCntIter;
335 for ( ndx = 0; ndx < evtData.fBlockCnt; ndx++ ){
337 if ( iter->fDataType != AliHLTTPCDefinitions::fgkClustersDataType ) continue;
339 slice = AliHLTTPCDefinitions::GetMinSliceNr( *iter );
342 slCntIter = sliceCnts.begin();
343 for( slIter = slices.begin(); slIter!=slices.end(); slIter++, slCntIter++ ){
344 if ( *slIter == slice ){
350 slices.push_back( slice );
351 sliceCnts.push_back( 1 );
356 // Determine slice number to really use.
357 if ( slices.size()>1 )
359 Logging( kHLTLogError, "HLT::TPCSliceTracker::DoEvent", "Multiple slices found in event",
360 "Multiple slice numbers found in event 0x%08lX (%lu). Determining maximum occuring slice number...",
361 evtData.fEventID, evtData.fEventID );
362 unsigned maxCntSlice=0;
363 slCntIter = sliceCnts.begin();
364 for( slIter = slices.begin(); slIter != slices.end(); slIter++, slCntIter++ )
366 Logging( kHLTLogError, "HLT::TPCSliceTracker::DoEvent", "Multiple slices found in event",
367 "Slice %lu found %lu times.", *slIter, *slCntIter );
368 if ( maxCntSlice<*slCntIter )
370 maxCntSlice = *slCntIter;
374 Logging( kHLTLogError, "HLT::TPCSliceTracker::DoEvent", "Multiple slices found in event",
375 "Using slice %lu.", slice );
377 else if ( slices.size()>0 )
379 slice = *(slices.begin());
384 Logging( kHLTLogWarning, "HLT::TPCCATracker::DoEvent", "DoEvent", "CA:: no slices found in event" );
389 // Initialize the tracker
393 if( !fTracker ) fTracker = new AliHLTTPCCATracker;
395 Float_t inRmin = 83.65;
396 // Float_t inRmax = 133.3;
397 // Float_t outRmin = 133.5;
398 Float_t outRmax = 247.7;
399 Float_t plusZmin = 0.0529937;
400 Float_t plusZmax = 249.778;
401 Float_t minusZmin = -249.645;
402 Float_t minusZmax = -0.0799937;
403 Float_t dalpha = 0.349066;
404 Float_t alpha = 0.174533 + dalpha*iSec;
406 Bool_t zPlus = (iSec<18 );
407 Float_t zMin = zPlus ?plusZmin :minusZmin;
408 Float_t zMax = zPlus ?plusZmax :minusZmax;
409 //TPCZmin = -249.645, ZMax = 249.778
410 // Float_t rMin = inRmin;
411 // Float_t rMax = outRmax;
412 Int_t nRows = AliHLTTPCTransform::GetNRows();
414 Float_t padPitch = 0.4;
415 Float_t sigmaZ = 0.228808;
417 Float_t *rowX = new Float_t [nRows];
418 for( Int_t irow=0; irow<nRows; irow++){
419 rowX[irow] = AliHLTTPCTransform::Row2X( irow );
422 AliHLTTPCCAParam param;
423 param.Initialize( iSec, nRows, rowX, alpha, dalpha,
424 inRmin, outRmax, zMin, zMax, padPitch, sigmaZ, fSolenoidBz );
425 param.SetHitPickUpFactor( 2 );
427 fTracker->Initialize( param );
432 // min and max patch numbers and row numbers
434 Int_t row[2] = {0,0};
435 Int_t minPatch=100, maxPatch = -1;
439 Int_t nHitsTotal = 0;
443 std::vector<unsigned long> patchIndices;
445 for ( ndx = 0; ndx < evtData.fBlockCnt; ndx++ ){
447 if( iter->fDataType != AliHLTTPCDefinitions::fgkClustersDataType ) continue;
448 if( slice!=AliHLTTPCDefinitions::GetMinSliceNr( *iter ) ) continue;
449 inPtrSP = (AliHLTTPCClusterData*)(iter->fPtr);
450 nHitsTotal+=inPtrSP->fSpacePointCnt;
451 Int_t patch = AliHLTTPCDefinitions::GetMinPatchNr( *iter );
452 if ( minPatch>patch ){
454 row[0] = AliHLTTPCTransform::GetFirstRow( patch );
456 if ( maxPatch<patch ){
458 row[1] = AliHLTTPCTransform::GetLastRow( patch );
460 std::vector<unsigned long>::iterator pIter = patchIndices.begin();
461 while( pIter!=patchIndices.end() && AliHLTTPCDefinitions::GetMinPatchNr( blocks[*pIter] ) < patch ){
464 patchIndices.insert( pIter, ndx );
468 // pass event to CA Tracker
470 fTracker->StartEvent();
472 Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Reading hits",
473 "Total %d hits to read for slice %d", nHitsTotal, slice );
476 AliHLTTPCSpacePointData** vOrigClusters = new AliHLTTPCSpacePointData* [ nHitsTotal];
480 for( std::vector<unsigned long>::iterator pIter = patchIndices.begin(); pIter!=patchIndices.end(); pIter++ ){
484 Int_t patch = AliHLTTPCDefinitions::GetMinPatchNr( *iter );
485 inPtrSP = (AliHLTTPCClusterData*)(iter->fPtr);
487 Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Reading hits",
488 "Reading %d hits for slice %d - patch %d", inPtrSP->fSpacePointCnt, slice, patch );
490 for (UInt_t i=0; i<inPtrSP->fSpacePointCnt; i++ ){
491 vOrigClusters[nClusters++] = &(inPtrSP->fSpacePoints[i]);
495 // sort clusters since they are not sorted fore some reason
497 sort( vOrigClusters, vOrigClusters+nClusters, CompareClusters );
499 Float_t *vHitStoreX = new Float_t [nHitsTotal]; // hit X coordinates
500 Float_t *vHitStoreY = new Float_t [nHitsTotal]; // hit Y coordinates
501 Float_t *vHitStoreZ = new Float_t [nHitsTotal]; // hit Z coordinates
502 Int_t *vHitStoreIntID = new Int_t [nHitsTotal]; // hit ID's
503 Int_t *vHitStoreID = new Int_t [nHitsTotal]; // hit ID's
504 Int_t *vHitRowID = new Int_t [nHitsTotal]; // hit ID's
510 Int_t rowFirstHits[200];
511 for( Int_t ir=0; ir<200; ir++ ) rowNHits[ir] = 0;
513 for (Int_t i=0; i<nClusters; i++ ){
514 AliHLTTPCSpacePointData* pSP = vOrigClusters[i];
515 if( oldRow>=0 && pSP->fPadRow < oldRow )
516 HLTError("CA: clusters from row %d are readed twice",oldRow);
518 if( TMath::Abs(pSP->fZ)>fClusterZCut) continue;
520 vHitStoreX[nHits] = pSP->fX;
521 vHitStoreY[nHits] = pSP->fY;
522 vHitStoreZ[nHits] = pSP->fZ;
523 vHitStoreIntID[nHits] = nHits;
524 vHitStoreID[nHits] = pSP->fID;
525 vHitRowID[nHits] = pSP->fPadRow;
527 rowNHits[pSP->fPadRow]++;
530 Int_t firstRowHit = 0;
531 for( Int_t ir=0; ir<200; ir++ ){
532 rowFirstHits[ir] = firstRowHit;
533 firstRowHit+=rowNHits[ir];
536 fTracker->ReadEvent( rowFirstHits, rowNHits, vHitStoreY, vHitStoreZ, nHits );
539 if( vOrigClusters ) delete[] vOrigClusters;
541 // reconstruct the event
543 TStopwatch timerReco;
545 fTracker->Reconstruct();
551 Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Reconstruct",
552 "%d tracks found for slice %d",fTracker->NOutTracks(), slice);
554 // write reconstructed tracks
556 AliHLTTPCTrackletData* outPtr = (AliHLTTPCTrackletData*)(outputPtr);
558 AliHLTTPCTrackSegmentData* currOutTracklet = outPtr->fTracklets;
560 Int_t ntracks = *fTracker->NOutTracks();
562 UInt_t mySize = ((AliHLTUInt8_t *)currOutTracklet) - ((AliHLTUInt8_t *)outputPtr);
564 outPtr->fTrackletCnt = 0;
566 for( Int_t itr=0; itr<ntracks; itr++ ){
568 AliHLTTPCCAOutTrack &t = fTracker->OutTracks()[itr];
570 //Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Wrtite output","track %d with %d hits", itr, t.NHits());
572 if( t.NHits()<fMinNTrackClusters ) continue;
574 // calculate output track size
576 UInt_t dSize = sizeof(AliHLTTPCTrackSegmentData) + t.NHits()*sizeof(UInt_t);
578 if( mySize + dSize > maxBufferSize ){
579 Logging( kHLTLogWarning, "HLT::TPCCATracker::DoEvent", "Wrtite output","Output buffer size exceed (buffer size %d, current size %d), %d tracks are not stored", maxBufferSize, mySize, ntracks-itr+1);
584 // convert CA track parameters to HLT Track Segment
586 Int_t iFirstRow = 1000;
588 Int_t iFirstHit = fTracker->OutTrackHits()[t.FirstHitRef()];
589 Int_t iLastHit = iFirstHit;
590 for( Int_t ih=0; ih<t.NHits(); ih++ ){
591 Int_t hitID = fTracker->OutTrackHits()[t.FirstHitRef() + ih ];
592 Int_t iRow = vHitRowID[hitID];
593 if( iRow<iFirstRow ){ iFirstRow = iRow; iFirstHit = hitID; }
594 if( iRow>iLastRow ){ iLastRow = iRow; iLastHit = hitID; }
597 AliHLTTPCCATrackParam par = t.StartPoint();
599 par.TransportToX( vHitStoreX[iFirstHit], .99 );
601 AliExternalTrackParam tp;
602 AliHLTTPCCATrackConvertor::GetExtParam( par, tp, 0, fSolenoidBz );
604 currOutTracklet->fX = tp.GetX();
605 currOutTracklet->fY = tp.GetY();
606 currOutTracklet->fZ = tp.GetZ();
607 currOutTracklet->fCharge = (Int_t ) tp.GetSign();
608 currOutTracklet->fPt = TMath::Abs(tp.GetSignedPt());
609 Float_t snp = tp.GetSnp() ;
610 if( snp>.999 ) snp=.999;
611 if( snp<-.999 ) snp=-.999;
612 currOutTracklet->fPsi = TMath::ASin( snp );
613 currOutTracklet->fTgl = tp.GetTgl();
615 currOutTracklet->fY0err = tp.GetSigmaY2();
616 currOutTracklet->fZ0err = tp.GetSigmaZ2();
617 Float_t h = -currOutTracklet->fPt*currOutTracklet->fPt;
618 currOutTracklet->fPterr = h*h*tp.GetSigma1Pt2();
619 h = 1./TMath::Sqrt(1-snp*snp);
620 currOutTracklet->fPsierr = h*h*tp.GetSigmaSnp2();
621 currOutTracklet->fTglerr = tp.GetSigmaTgl2();
623 if( par.TransportToX( vHitStoreX[iLastHit],.99 ) ){
624 currOutTracklet->fLastX = par.GetX();
625 currOutTracklet->fLastY = par.GetY();
626 currOutTracklet->fLastZ = par.GetZ();
628 currOutTracklet->fLastX = vHitStoreX[iLastHit];
629 currOutTracklet->fLastY = vHitStoreY[iLastHit];
630 currOutTracklet->fLastZ = vHitStoreZ[iLastHit];
632 //if( currOutTracklet->fLastX<10. ) {
633 //HLTError("CA last point: hitxyz=%f,%f,%f, track=%f,%f,%f, tracklet=%f,%f,%f, nhits=%d",vHitStoreX[iLastHit],vHitStoreY[iLastHit],vHitStoreZ[iLastHit],
634 //par.GetX(), par.GetY(),par.GetZ(),currOutTracklet->fLastX,currOutTracklet->fLastY ,currOutTracklet->fLastZ, t.NHits());
636 #ifdef INCLUDE_TPC_HOUGH
637 #ifdef ROWHOUGHPARAMS
638 currOutTracklet->fTrackID = 0;
639 currOutTracklet->fRowRange1 = vHitRowID[iFirstHit];
640 currOutTracklet->fRowRange2 = vHitRowID[iLastHit];
641 currOutTracklet->fSector = slice;
642 currOutTracklet->fPID = 211;
644 #endif // INCLUDE_TPC_HOUGH
647 currOutTracklet->fNPoints = t.NHits();
649 for( Int_t i=0; i<t.NHits(); i++ ){
650 currOutTracklet->fPointIDs[i] = vHitStoreID[fTracker->OutTrackHits()[t.FirstHitRef()+i]];
653 currOutTracklet = (AliHLTTPCTrackSegmentData*)( (Byte_t *)currOutTracklet + dSize );
655 outPtr->fTrackletCnt++;
658 if( vHitStoreX ) delete[] vHitStoreX;
659 if( vHitStoreY ) delete[] vHitStoreY;
660 if( vHitStoreZ ) delete[] vHitStoreZ;
661 if( vHitStoreIntID ) delete[] vHitStoreIntID;
662 if( vHitStoreID ) delete[] vHitStoreID;
663 if( vHitRowID ) delete[] vHitRowID;
665 AliHLTComponentBlockData bd;
669 bd.fSpecification = AliHLTTPCDefinitions::EncodeDataSpecification( slice, slice, minPatch, maxPatch );
670 outputBlocks.push_back( bd );
676 fFullTime+= timer.RealTime();
677 fRecoTime+= timerReco.RealTime();
680 // Set log level to "Warning" for on-line system monitoring
681 Int_t hz = (Int_t) (fFullTime>1.e-10 ?fNEvents/fFullTime :100000);
682 Int_t hz1 = (Int_t) (fRecoTime>1.e-10 ?fNEvents/fRecoTime :100000);
683 Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Tracks",
684 "CATracker slice %d: output %d tracks; input %d clusters, patches %d..%d, rows %d..%d; reco time %d/%d Hz",
685 slice, ntracks, nClusters, minPatch, maxPatch, row[0], row[1], hz, hz1 );