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 // Matthias Kretz <kretz@kde.org> *
9 // for The ALICE HLT Project. *
11 // Permission to use, copy, modify and distribute this software and its *
12 // documentation strictly for non-commercial purposes is hereby granted *
13 // without fee, provided that the above copyright notice appears in all *
14 // copies and that both the copyright notice and this permission notice *
15 // appear in the supporting documentation. The authors make no claims *
16 // about the suitability of this software for any purpose. It is *
17 // provided "as is" without express or implied warranty. *
19 //***************************************************************************
22 /// @file AliHLTTPCCAGlobalMergerComponent.cxx
23 /// @author Matthias Kretz
25 /// @brief HLT TPC CA global merger component.
30 #include "AliHLTTPCCAGlobalMergerComponent.h"
31 #include "AliHLTTPCCASliceOutput.h"
33 #include "AliHLTTPCCADef.h"
34 #include "AliHLTTPCCAMerger.h"
35 #include "AliHLTTPCCAMergerOutput.h"
36 #include "AliHLTTPCCATrackConvertor.h"
38 #include "AliHLTTPCGMMerger.h"
39 #include "AliHLTTPCGMMergedTrack.h"
41 #include "AliHLTTPCDefinitions.h"
42 #include "AliHLTTPCTransform.h"
44 #include "AliExternalTrackParam.h"
45 #include "AliCDBEntry.h"
46 #include "AliCDBManager.h"
47 #include "TObjString.h"
48 #include "TObjArray.h"
49 #include "AliHLTExternalTrackParam.h"
56 // ROOT macro for the implementation of ROOT specific class methods
57 ClassImp( AliHLTTPCCAGlobalMergerComponent )
60 AliHLTTPCCAGlobalMergerComponent::AliHLTTPCCAGlobalMergerComponent()
61 : AliHLTProcessor(), fVersion(1), fGlobalMergerVersion0( 0 ), fGlobalMerger(0), fSolenoidBz( 0 ), fClusterErrorCorrectionY(0), fClusterErrorCorrectionZ(0), fBenchmark("GlobalMerger")
63 // see header file for class documentation
66 AliHLTTPCCAGlobalMergerComponent::AliHLTTPCCAGlobalMergerComponent( const AliHLTTPCCAGlobalMergerComponent & ):AliHLTProcessor(), fVersion(1), fGlobalMergerVersion0( 0 ), fGlobalMerger(0), fSolenoidBz( 0 ), fClusterErrorCorrectionY(0), fClusterErrorCorrectionZ(0), fBenchmark("GlobalMerger")
71 AliHLTTPCCAGlobalMergerComponent &AliHLTTPCCAGlobalMergerComponent::operator=( const AliHLTTPCCAGlobalMergerComponent & )
77 // Public functions to implement AliHLTComponent's interface.
78 // These functions are required for the registration process
80 const char *AliHLTTPCCAGlobalMergerComponent::GetComponentID()
82 // see header file for class documentation
83 return "TPCCAGlobalMerger";
86 void AliHLTTPCCAGlobalMergerComponent::GetInputDataTypes( AliHLTComponentDataTypeList &list )
88 // see header file for class documentation
90 list.push_back( AliHLTTPCCADefinitions::fgkTrackletsDataType );
91 //list.push_back( AliHLTTPCDefinitions::fgkTrackSegmentsDataType );
92 //list.push_back( AliHLTTPCDefinitions::fgkVertexDataType );
95 AliHLTComponentDataType AliHLTTPCCAGlobalMergerComponent::GetOutputDataType()
97 // see header file for class documentation
98 //return AliHLTTPCDefinitions::fgkTracksDataType; // old
99 return kAliHLTDataTypeTrack|kAliHLTDataOriginTPC;
102 void AliHLTTPCCAGlobalMergerComponent::GetOutputDataSize( unsigned long &constBase, double &inputMultiplier )
104 // see header file for class documentation
105 // XXX TODO: Find more realistic values.
107 inputMultiplier = 1.0;
110 AliHLTComponent *AliHLTTPCCAGlobalMergerComponent::Spawn()
112 // see header file for class documentation
113 return new AliHLTTPCCAGlobalMergerComponent;
117 void AliHLTTPCCAGlobalMergerComponent::SetDefaultConfiguration()
119 // Set default configuration for the CA merger component
120 // Some parameters can be later overwritten from the OCDB
123 fSolenoidBz = -5.00668;
124 fClusterErrorCorrectionY = 0;
125 fClusterErrorCorrectionZ = 1.1;
127 fBenchmark.SetTimer(0,"total");
128 fBenchmark.SetTimer(1,"reco");
131 int AliHLTTPCCAGlobalMergerComponent::ReadConfigurationString( const char* arguments )
133 // Set configuration parameters for the CA merger component from the string
136 if ( !arguments ) return iResult;
138 TString allArgs = arguments;
140 int bMissingParam = 0;
142 TObjArray* pTokens = allArgs.Tokenize( " " );
144 int nArgs = pTokens ? pTokens->GetEntries() : 0;
146 for ( int i = 0; i < nArgs; i++ ) {
147 argument = ( ( TObjString* )pTokens->At( i ) )->GetString();
148 if ( argument.IsNull() ) continue;
150 if ( argument.CompareTo( "-version" ) == 0 ) {
151 if ( ( bMissingParam = ( ++i >= pTokens->GetEntries() ) ) ) break;
152 fVersion = ( ( TObjString* )pTokens->At( i ) )->GetString().Atoi();
153 HLTInfo( "Merger version set to: %d", fVersion );
157 if ( argument.CompareTo( "-solenoidBz" ) == 0 ) {
158 if ( ( bMissingParam = ( ++i >= pTokens->GetEntries() ) ) ) break;
159 HLTWarning("argument -solenoidBz is deprecated, magnetic field set up globally (%f)", GetBz());
163 if ( argument.CompareTo( "-errorCorrectionY" ) == 0 ) {
164 if ( ( bMissingParam = ( ++i >= pTokens->GetEntries() ) ) ) break;
165 fClusterErrorCorrectionY = ( ( TObjString* )pTokens->At( i ) )->GetString().Atof();
166 HLTInfo( "Cluster Y error correction factor set to: %f", fClusterErrorCorrectionY );
170 if ( argument.CompareTo( "-errorCorrectionZ" ) == 0 ) {
171 if ( ( bMissingParam = ( ++i >= pTokens->GetEntries() ) ) ) break;
172 fClusterErrorCorrectionZ = ( ( TObjString* )pTokens->At( i ) )->GetString().Atof();
173 HLTInfo( "Cluster Z error correction factor set to: %f", fClusterErrorCorrectionZ );
177 HLTError( "Unknown option \"%s\"", argument.Data() );
182 if ( bMissingParam ) {
183 HLTError( "Specifier missed for parameter \"%s\"", argument.Data() );
191 int AliHLTTPCCAGlobalMergerComponent::ReadCDBEntry( const char* cdbEntry, const char* chainId )
193 // see header file for class documentation
195 const char* defaultNotify = "";
198 cdbEntry = "HLT/ConfigTPC/TPCCAGlobalMerger";
199 defaultNotify = " (default)";
203 HLTInfo( "configure from entry \"%s\"%s, chain id %s", cdbEntry, defaultNotify, ( chainId != NULL && chainId[0] != 0 ) ? chainId : "<none>" );
204 AliCDBEntry *pEntry = AliCDBManager::Instance()->Get( cdbEntry );//,GetRunNo());
207 HLTError( "cannot fetch object \"%s\" from CDB", cdbEntry );
211 TObjString* pString = dynamic_cast<TObjString*>( pEntry->GetObject() );
214 HLTError( "configuration object \"%s\" has wrong type, required TObjString", cdbEntry );
218 HLTInfo( "received configuration object string: \"%s\"", pString->GetString().Data() );
220 return ReadConfigurationString( pString->GetString().Data() );
225 int AliHLTTPCCAGlobalMergerComponent::Configure( const char* cdbEntry, const char* chainId, const char *commandLine )
227 // Configure the component
228 // There are few levels of configuration,
229 // parameters which are set on one step can be overwritten on the next step
231 //* read hard-coded values
233 SetDefaultConfiguration();
235 //* read the default CDB entry
237 int iResult1 = ReadCDBEntry( NULL, chainId );
239 //* read magnetic field
241 fSolenoidBz = GetBz();
243 //* read the actual CDB entry if required
245 int iResult2 = ( cdbEntry ) ? ReadCDBEntry( cdbEntry, chainId ) : 0;
247 //* read extra parameters from input (if they are)
251 if ( commandLine && commandLine[0] != '\0' ) {
252 HLTInfo( "received configuration string from HLT framework: \"%s\"", commandLine );
253 iResult3 = ReadConfigurationString( commandLine );
257 // Initialize the merger
259 AliHLTTPCCAParam param;
264 float inRmin = 83.65;
265 float outRmax = 247.7;
266 float plusZmin = 0.0529937;
267 float plusZmax = 249.778;
268 //float minusZmin = -249.645;
269 //float minusZmax = -0.0799937;
270 float dalpha = 0.349066;
271 float alpha = 0.174533 + dalpha * iSec;
272 //bool zPlus = ( iSec < 18 );
273 float zMin = plusZmin; //zPlus ? plusZmin : minusZmin;
274 float zMax = plusZmax; //zPlus ? plusZmax : minusZmax;
275 int nRows = AliHLTTPCTransform::GetNRows();
276 float padPitch = 0.4;
277 float sigmaZ = 0.228808;
278 float *rowX = new float [nRows];
279 for ( int irow = 0; irow < nRows; irow++ ) {
280 rowX[irow] = AliHLTTPCTransform::Row2X( irow );
283 param.Initialize( iSec, nRows, rowX, alpha, dalpha,
284 inRmin, outRmax, zMin, zMax, padPitch, sigmaZ, fSolenoidBz );
286 if( fClusterErrorCorrectionY>1.e-4 ) param.SetClusterError2CorrectionY( fClusterErrorCorrectionY*fClusterErrorCorrectionY );
287 if( fClusterErrorCorrectionZ>1.e-4 ) param.SetClusterError2CorrectionZ( fClusterErrorCorrectionZ*fClusterErrorCorrectionZ );
294 if( fVersion==0 ) fGlobalMergerVersion0->SetSliceParam( param );
295 else fGlobalMerger->SetSliceParam( param );
297 return iResult1 ? iResult1 : ( iResult2 ? iResult2 : iResult3 );
303 int AliHLTTPCCAGlobalMergerComponent::DoInit( int argc, const char** argv )
305 // see header file for class documentation
307 if ( fGlobalMergerVersion0 || fGlobalMerger ) {
311 fGlobalMergerVersion0 = new AliHLTTPCCAMerger();
312 fGlobalMerger = new AliHLTTPCGMMerger();
314 TString arguments = "";
315 for ( int i = 0; i < argc; i++ ) {
316 if ( !arguments.IsNull() ) arguments += " ";
317 arguments += argv[i];
320 return Configure( NULL, NULL, arguments.Data() );
323 int AliHLTTPCCAGlobalMergerComponent::Reconfigure( const char* cdbEntry, const char* chainId )
325 // Reconfigure the component from OCDB
327 return Configure( cdbEntry, chainId, NULL );
332 int AliHLTTPCCAGlobalMergerComponent::DoDeinit()
334 // see header file for class documentation
335 delete fGlobalMergerVersion0;
336 fGlobalMergerVersion0 = 0;
337 delete fGlobalMerger;
343 int AliHLTTPCCAGlobalMergerComponent::DoEvent( const AliHLTComponentEventData &evtData,
344 const AliHLTComponentBlockData *blocks, AliHLTComponentTriggerData &/*trigData*/,
345 AliHLTUInt8_t *outputPtr, AliHLTUInt32_t &size, AliHLTComponentBlockDataList &outputBlocks )
347 // see header file for class documentation
349 unsigned int maxBufferSize = size;
356 if ( !IsDataEvent() ) {
359 fBenchmark.StartNewEvent();
362 if( fVersion==0 ) fGlobalMergerVersion0->Clear();
363 else fGlobalMerger->Clear();
365 const AliHLTComponentBlockData *const blocksEnd = blocks + evtData.fBlockCnt;
366 for ( const AliHLTComponentBlockData *block = blocks; block < blocksEnd; ++block ) {
367 if ( block->fDataType != AliHLTTPCCADefinitions::fgkTrackletsDataType ) {
371 fBenchmark.AddInput(block->fSize);
373 int slice = AliHLTTPCDefinitions::GetMinSliceNr( *block );
374 if ( slice < 0 || slice >= AliHLTTPCTransform::GetNSlice() ) {
375 HLTError( "invalid slice number %d extracted from specification 0x%08lx, skipping block of type %s",
376 slice, block->fSpecification, DataType2Text( block->fDataType ).c_str() );
377 // just remember the error, if there are other valid blocks ignore the error, return code otherwise
382 if ( slice != AliHLTTPCDefinitions::GetMaxSliceNr( *block ) ) {
383 // the code was not written for/ never used with multiple slices in one data block/ specification
384 HLTWarning( "specification 0x%08lx indicates multiple slices in data block %s: never used before, please audit the code",
385 block->fSpecification, DataType2Text( block->fDataType ).c_str() );
387 AliHLTTPCCASliceOutput *sliceOut = reinterpret_cast<AliHLTTPCCASliceOutput *>( block->fPtr );
388 //sliceOut->SetPointers();
389 if( fVersion==0 ) fGlobalMergerVersion0->SetSliceData( slice, sliceOut );
390 else fGlobalMerger->SetSliceData( slice, sliceOut );
392 /*char filename[256];
393 sprintf(filename, "debug%d.out", slice);
394 FILE* fp = fopen(filename, "w+b");
395 if (fp == NULL) printf("Error!!!\n");
396 fwrite(sliceOut, 1, sliceOut->EstimateSize(sliceOut->NTracks(), sliceOut->NTrackClusters()), fp);
400 if( fVersion==0 ) fGlobalMergerVersion0->Reconstruct();
401 else fGlobalMerger->Reconstruct();
408 const AliHLTTPCCAMergerOutput *mergerOutput = fGlobalMergerVersion0->Output();
410 unsigned int mySize = 0;
412 AliHLTTracksData* outPtr = ( AliHLTTracksData* )( outputPtr );
414 AliHLTExternalTrackParam* currOutTrack = outPtr->fTracklets;
416 mySize = ( ( AliHLTUInt8_t * )currOutTrack ) - ( ( AliHLTUInt8_t * )outputPtr );
420 int nTracks = mergerOutput->NTracks();
422 for ( int itr = 0; itr < nTracks; itr++ ) {
424 // convert AliHLTTPCCAMergedTrack to AliHLTTrack
426 const AliHLTTPCCAMergedTrack &track = mergerOutput->Track( itr );
428 unsigned int dSize = sizeof( AliHLTExternalTrackParam ) + track.NClusters() * sizeof( unsigned int );
430 if ( mySize + dSize > maxBufferSize ) {
431 HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), %d tracks are not stored", maxBufferSize, mySize, nTracks - itr + 1 );
436 // first convert to AliExternalTrackParam
438 AliExternalTrackParam tp, tpEnd;
439 AliHLTTPCCATrackConvertor::GetExtParam( track.InnerParam(), tp, track.InnerAlpha() );
440 AliHLTTPCCATrackConvertor::GetExtParam( track.OuterParam(), tpEnd, track.OuterAlpha() );
442 // normalize the angle to +-Pi
444 currOutTrack->fAlpha = tp.GetAlpha() - CAMath::Nint(tp.GetAlpha()/CAMath::TwoPi())*CAMath::TwoPi();
445 currOutTrack->fX = tp.GetX();
446 currOutTrack->fY = tp.GetY();
447 currOutTrack->fZ = tp.GetZ();
449 float sinA = TMath::Sin( track.OuterAlpha() - track.InnerAlpha());
450 float cosA = TMath::Cos( track.OuterAlpha() - track.InnerAlpha());
451 currOutTrack->fLastX = tpEnd.GetX()*cosA - tpEnd.GetY()*sinA;
452 currOutTrack->fLastY = tpEnd.GetX()*sinA + tpEnd.GetY()*cosA;
453 currOutTrack->fLastZ = tpEnd.GetZ();
455 currOutTrack->fq1Pt = tp.GetSigned1Pt();
456 currOutTrack->fSinPsi = tp.GetSnp();
457 currOutTrack->fTgl = tp.GetTgl();
458 for( int i=0; i<15; i++ ) currOutTrack->fC[i] = tp.GetCovariance()[i];
459 currOutTrack->fTrackID = itr;
460 currOutTrack->fFlags = 0;
461 currOutTrack->fNPoints = track.NClusters();
462 for ( int i = 0; i < track.NClusters(); i++ ) currOutTrack->fPointIDs[i] = mergerOutput->ClusterId( track.FirstClusterRef() + i );
464 currOutTrack = ( AliHLTExternalTrackParam* )( (( Byte_t * )currOutTrack) + dSize );
469 AliHLTComponentBlockData resultData;
470 FillBlockData( resultData );
471 resultData.fOffset = 0;
472 resultData.fSize = mySize;
473 resultData.fDataType = kAliHLTDataTypeTrack|kAliHLTDataOriginTPC;
474 resultData.fSpecification = AliHLTTPCDefinitions::EncodeDataSpecification( 0, 35, 0, 5 );
475 outputBlocks.push_back( resultData );
476 fBenchmark.AddOutput(resultData.fSize);
478 size = resultData.fSize;
481 HLTInfo( "CAGlobalMerger:: output %d tracks", mergerOutput->NTracks() );
482 fGlobalMergerVersion0->Clear();
484 } else { // new merger
486 unsigned int mySize = 0;
488 AliHLTTracksData* outPtr = ( AliHLTTracksData* )( outputPtr );
489 AliHLTExternalTrackParam* currOutTrack = outPtr->fTracklets;
490 mySize = ( ( AliHLTUInt8_t * )currOutTrack ) - ( ( AliHLTUInt8_t * )outputPtr );
492 int nTracks = fGlobalMerger->NOutputTracks();
494 for ( int itr = 0; itr < nTracks; itr++ ) {
496 // convert AliHLTTPCGMMergedTrack to AliHLTTrack
498 const AliHLTTPCGMMergedTrack &track = fGlobalMerger->OutputTracks()[ itr ];
499 if( !track.OK() ) continue;
500 unsigned int dSize = sizeof( AliHLTExternalTrackParam ) + track.NClusters() * sizeof( unsigned int );
502 if ( mySize + dSize > maxBufferSize ) {
503 HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), %d tracks are not stored", maxBufferSize, mySize, nTracks - itr + 1 );
508 // first convert to AliExternalTrackParam
510 AliExternalTrackParam tp;
511 track.GetParam().GetExtParam( tp, track.GetAlpha() );
513 // normalize the angle to +-Pi
515 currOutTrack->fAlpha = tp.GetAlpha() - CAMath::Nint(tp.GetAlpha()/CAMath::TwoPi())*CAMath::TwoPi();
516 currOutTrack->fX = tp.GetX();
517 currOutTrack->fY = tp.GetY();
518 currOutTrack->fZ = tp.GetZ();
519 currOutTrack->fLastX = track.LastX();
520 currOutTrack->fLastY = track.LastY();
521 currOutTrack->fLastZ = track.LastZ();
523 currOutTrack->fq1Pt = tp.GetSigned1Pt();
524 currOutTrack->fSinPsi = tp.GetSnp();
525 currOutTrack->fTgl = tp.GetTgl();
526 for( int i=0; i<15; i++ ) currOutTrack->fC[i] = tp.GetCovariance()[i];
527 currOutTrack->fTrackID = itr;
528 currOutTrack->fFlags = 0;
529 currOutTrack->fNPoints = track.NClusters();
530 for ( int i = 0; i < track.NClusters(); i++ ) currOutTrack->fPointIDs[i] = fGlobalMerger->OutputClusterIds()[track.FirstClusterRef() + i ];
532 currOutTrack = ( AliHLTExternalTrackParam* )( (( Byte_t * )currOutTrack) + dSize );
537 AliHLTComponentBlockData resultData;
538 FillBlockData( resultData );
539 resultData.fOffset = 0;
540 resultData.fSize = mySize;
541 resultData.fDataType = kAliHLTDataTypeTrack|kAliHLTDataOriginTPC;
542 resultData.fSpecification = AliHLTTPCDefinitions::EncodeDataSpecification( 0, 35, 0, 5 );
543 outputBlocks.push_back( resultData );
544 fBenchmark.AddOutput(resultData.fSize);
546 size = resultData.fSize;
549 HLTInfo( "CAGlobalMerger:: output %d tracks", fGlobalMerger->NOutputTracks() );
551 fGlobalMerger->Clear();
555 HLTInfo( fBenchmark.GetStatistics() );