]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/ITS/tracking/AliHLTITSTrackerComponent.cxx
coverity warning 10074 fixed
[u/mrichter/AliRoot.git] / HLT / ITS / tracking / AliHLTITSTrackerComponent.cxx
1 // $Id: AliHLTITSTrackerComponent.cxx 32659 2009-06-02 16:08:40Z sgorbuno $
2 // **************************************************************************
3 // This file is property of and copyright by the ALICE HLT Project          *
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
20 ///  @file   AliHLTITSTrackerComponent.cxx
21 ///  @author Sergey Gorbunov <sergey.gorbunov@kip.uni-heidelberg.de>
22 ///  @date   June 2009
23 ///  @brief  An ITS tracker processing component for the HLT
24
25
26 /////////////////////////////////////////////////////
27 //                                                 //
28 // a ITS tracker processing component for the HLT  //
29 //                                                 //
30 /////////////////////////////////////////////////////
31
32 #if __GNUC__>= 3
33 using namespace std;
34 #endif
35
36 #include "AliHLTITSTrackerComponent.h"
37 #include "AliHLTArray.h"
38 #include "AliExternalTrackParam.h"
39 #include "TStopwatch.h"
40 #include "TMath.h"
41 #include "AliCDBEntry.h"
42 #include "AliCDBManager.h"
43 #include "AliGeomManager.h"
44 #include "TObjString.h"
45 #include "TObjArray.h"
46 #include "AliITStrackerHLT.h"
47 #include "AliHLTITSSpacePointData.h"
48 #include "AliHLTITSClusterDataFormat.h"
49 #include "AliHLTDataTypes.h"
50 #include "AliHLTExternalTrackParam.h"
51 #include "AliHLTGlobalBarrelTrack.h"
52 #include "AliGeomManager.h"
53 #include "AliHLTTrackMCLabel.h"
54 #include <map>
55
56 /** ROOT macro for the implementation of ROOT specific class methods */
57 ClassImp( AliHLTITSTrackerComponent )
58 AliHLTITSTrackerComponent::AliHLTITSTrackerComponent()
59     :
60     fSolenoidBz( 0 ),
61     fBenchmark("ITSTracker"),
62     fTracker(0)
63 {
64   // see header file for class documentation
65   // or
66   // refer to README to build package
67   // or
68   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
69 }
70
71 AliHLTITSTrackerComponent::AliHLTITSTrackerComponent( const AliHLTITSTrackerComponent& )
72     :
73     AliHLTProcessor(),
74     fSolenoidBz( 0 ),
75     fBenchmark("ITSTracker"),
76     fTracker(0)
77 {
78   // see header file for class documentation
79   HLTFatal( "copy constructor untested" );
80 }
81
82 AliHLTITSTrackerComponent& AliHLTITSTrackerComponent::operator=( const AliHLTITSTrackerComponent& )
83 {
84   // see header file for class documentation
85   HLTFatal( "assignment operator untested" );
86   return *this;
87 }
88
89 AliHLTITSTrackerComponent::~AliHLTITSTrackerComponent()
90 {
91   // see header file for class documentation
92   delete fTracker;
93 }
94
95 //
96 // Public functions to implement AliHLTComponent's interface.
97 // These functions are required for the registration process
98 //
99
100 const char* AliHLTITSTrackerComponent::GetComponentID()
101 {
102   // see header file for class documentation
103   return "ITSTracker";
104 }
105
106 void AliHLTITSTrackerComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list )
107 {
108   // see header file for class documentation
109   list.clear();
110   list.push_back( kAliHLTDataTypeTrack|kAliHLTDataOriginTPC );
111   list.push_back( kAliHLTDataTypeTrackMC|kAliHLTDataOriginTPC );
112   list.push_back( kAliHLTDataTypeClusters|kAliHLTDataOriginITSSSD );
113   list.push_back( kAliHLTDataTypeClusters|kAliHLTDataOriginITSSPD );
114   list.push_back( kAliHLTDataTypeClusters|kAliHLTDataOriginITSSDD );
115   list.push_back( kAliHLTDataTypeClusters|kAliHLTDataOriginITS );
116 }
117
118 AliHLTComponentDataType AliHLTITSTrackerComponent::GetOutputDataType()
119 {
120   // see header file for class documentation  
121   return kAliHLTMultipleDataType;
122 }
123
124 int AliHLTITSTrackerComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList)
125 {
126   // see header file for class documentation  
127   tgtList.clear();
128   tgtList.push_back(kAliHLTDataTypeTrack|kAliHLTDataOriginITS);
129   tgtList.push_back(kAliHLTDataTypeTrack|kAliHLTDataOriginITSOut);
130   tgtList.push_back( kAliHLTDataTypeTrackMC|kAliHLTDataOriginITS );
131   return tgtList.size();
132 }
133
134 void AliHLTITSTrackerComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier )
135 {
136   // define guess for the output data size
137   constBase = 200;       // minimum size
138   inputMultiplier = 2.; // size relative to input
139 }
140
141 AliHLTComponent* AliHLTITSTrackerComponent::Spawn()
142 {
143   // see header file for class documentation
144   return new AliHLTITSTrackerComponent;
145 }
146
147 void AliHLTITSTrackerComponent::SetDefaultConfiguration()
148 {
149   // Set default configuration for the CA tracker component
150   // Some parameters can be later overwritten from the OCDB
151
152   fSolenoidBz = -5.00668;
153   
154 }
155
156 int AliHLTITSTrackerComponent::ReadConfigurationString(  const char* arguments )
157 {
158   // Set configuration parameters for the CA tracker component from the string
159
160   int iResult = 0;
161   if ( !arguments ) return iResult;
162
163   TString allArgs = arguments;
164   TString argument;
165   int bMissingParam = 0;
166
167   TObjArray* pTokens = allArgs.Tokenize( " " );
168
169   int nArgs =  pTokens ? pTokens->GetEntries() : 0;
170
171   for ( int i = 0; i < nArgs; i++ ) {
172     argument = ( ( TObjString* )pTokens->At( i ) )->GetString();
173     if ( argument.IsNull() ) continue;
174
175     if ( argument.CompareTo( "-solenoidBz" ) == 0 ) {
176       if ( ( bMissingParam = ( ++i >= pTokens->GetEntries() ) ) ) break;
177       HLTWarning("argument -solenoidBz is deprecated, magnetic field set up globally (%f)", GetBz());
178       continue;
179     }
180
181     //if ( argument.CompareTo( "-minNClustersOnTrack" ) == 0 ) {
182     //if ( ( bMissingParam = ( ++i >= pTokens->GetEntries() ) ) ) break;
183     //fMinNTrackClusters = ( ( TObjString* )pTokens->At( i ) )->GetString().Atoi();
184     //HLTInfo( "minNClustersOnTrack set to: %d", fMinNTrackClusters );
185     //continue;
186     //}
187
188     HLTError( "Unknown option \"%s\"", argument.Data() );
189     iResult = -EINVAL;
190   }
191   delete pTokens;
192
193   if ( bMissingParam ) {
194     HLTError( "Specifier missed for parameter \"%s\"", argument.Data() );
195     iResult = -EINVAL;
196   }
197
198   return iResult;
199 }
200
201
202 int AliHLTITSTrackerComponent::ReadCDBEntry( const char* cdbEntry, const char* chainId )
203 {
204   // see header file for class documentation
205
206   const char* defaultNotify = "";
207
208   if ( !cdbEntry ) {
209     return 0;// need to add the HLT/ConfigITS/ITSTracker directory to cdb SG!!!
210     //cdbEntry = "HLT/ConfigITS/ITSTracker";
211     //defaultNotify = " (default)";
212     //chainId = 0;
213   }
214
215   HLTInfo( "configure from entry \"%s\"%s, chain id %s", cdbEntry, defaultNotify, ( chainId != NULL && chainId[0] != 0 ) ? chainId : "<none>" );
216   AliCDBEntry *pEntry = AliCDBManager::Instance()->Get( cdbEntry );//,GetRunNo());
217
218   if ( !pEntry ) {
219     HLTError( "cannot fetch object \"%s\" from CDB", cdbEntry );
220     return -EINVAL;
221   }
222
223   TObjString* pString = dynamic_cast<TObjString*>( pEntry->GetObject() );
224
225   if ( !pString ) {
226     HLTError( "configuration object \"%s\" has wrong type, required TObjString", cdbEntry );
227     return -EINVAL;
228   }
229
230   HLTInfo( "received configuration object string: \"%s\"", pString->GetString().Data() );
231
232   return  ReadConfigurationString( pString->GetString().Data() );
233 }
234
235
236 int AliHLTITSTrackerComponent::Configure( const char* cdbEntry, const char* chainId, const char *commandLine )
237 {
238   // Configure the component
239   // There are few levels of configuration,
240   // parameters which are set on one step can be overwritten on the next step
241
242   //* read hard-coded values
243
244   SetDefaultConfiguration();
245
246   //* read the default CDB entry
247
248   int iResult1 = ReadCDBEntry( NULL, chainId );
249
250   //* read magnetic field
251
252   fSolenoidBz = GetBz();
253
254   //* read the actual CDB entry if required
255
256   int iResult2 = ( cdbEntry ) ? ReadCDBEntry( cdbEntry, chainId ) : 0;
257
258   //* read extra parameters from input (if they are)
259
260   int iResult3 = 0;
261
262   if ( commandLine && commandLine[0] != '\0' ) {
263     HLTInfo( "received configuration string from HLT framework: \"%s\"", commandLine );
264     iResult3 = ReadConfigurationString( commandLine );
265   }
266
267   // Initialise the tracker here
268
269   return iResult1 ? iResult1 : ( iResult2 ? iResult2 : iResult3 );
270 }
271
272
273
274 int AliHLTITSTrackerComponent::DoInit( int argc, const char** argv )
275 {
276   // Configure the ITS tracker component
277
278   if ( fTracker ) return -EINPROGRESS;
279
280   if(AliGeomManager::GetGeometry()==NULL){
281     AliGeomManager::LoadGeometry();
282   }
283   AliGeomManager::ApplyAlignObjsFromCDB("ITS");
284
285   TString arguments = "";
286   for ( int i = 0; i < argc; i++ ) {
287     if ( !arguments.IsNull() ) arguments += " ";
288     arguments += argv[i];
289   }
290
291   int ret = Configure( NULL, NULL, arguments.Data() );
292
293   // Check field
294   if (!TGeoGlobalMagField::Instance()) {
295     HLTError("magnetic field not initialized, please set up TGeoGlobalMagField and AliMagF");
296     return -ENODEV;
297   }
298   fSolenoidBz=GetBz();
299
300   fTracker = new AliITStrackerHLT(0);
301   fBenchmark.Reset();
302   fBenchmark.SetTimer(0,"total");
303   fBenchmark.SetTimer(1,"reco");
304   return ret;
305 }
306
307
308 int AliHLTITSTrackerComponent::DoDeinit()
309 {
310   // see header file for class documentation
311   delete fTracker;
312   fTracker = 0;
313   return 0;
314 }
315
316
317
318 int AliHLTITSTrackerComponent::Reconfigure( const char* cdbEntry, const char* chainId )
319 {
320   // Reconfigure the component from OCDB .
321
322   return Configure( cdbEntry, chainId, NULL );
323 }
324
325
326
327 int AliHLTITSTrackerComponent::DoEvent
328 (
329   const AliHLTComponentEventData& evtData,
330   const AliHLTComponentBlockData* blocks,
331   AliHLTComponentTriggerData& /*trigData*/,
332   AliHLTUInt8_t* outputPtr,
333   AliHLTUInt32_t& size,
334   vector<AliHLTComponentBlockData>& outputBlocks )
335 {
336   //* process event
337
338   AliHLTUInt32_t maxBufferSize = size;
339   size = 0; // output size
340   
341   if (!IsDataEvent()) return 0;
342
343   if ( evtData.fBlockCnt <= 0 ) {
344     HLTWarning( "no blocks in event" );
345     return 0;
346   }
347
348   fBenchmark.StartNewEvent();
349   fBenchmark.Start(0);
350
351   // Event reconstruction in ITS
352
353   int iResult=0;
354
355   int nBlocks = evtData.fBlockCnt;
356
357   
358   vector< AliExternalTrackParam > tracksTPC;
359   vector< int > tracksTPCLab;
360   vector< int > tracksTPCId;
361
362   int nClustersTotal = 0;
363
364   for (int ndx=0; ndx<nBlocks && iResult>=0; ndx++) {
365
366     const AliHLTComponentBlockData* iter = blocks+ndx;
367  
368     if ( (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSSD) ) || 
369          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSPD) ) ||
370          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSDD) ) ||
371          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITS) ) 
372          ){      
373       AliHLTITSClusterData *inPtr=reinterpret_cast<AliHLTITSClusterData*>( iter->fPtr );
374       nClustersTotal+=inPtr->fSpacePointCnt;
375     }         
376   }
377
378
379   fTracker->StartLoadClusters(nClustersTotal);
380
381   // first read MC information (if present)
382   
383   std::map<int,int> mcLabels;
384
385   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrackMC|kAliHLTDataOriginTPC);
386        pBlock!=NULL; pBlock=GetNextInputBlock()) {
387     
388     fBenchmark.AddInput(pBlock->fSize);
389     
390     AliHLTTrackMCData* dataPtr = reinterpret_cast<AliHLTTrackMCData*>( pBlock->fPtr );
391     if (sizeof(AliHLTTrackMCData)+dataPtr->fCount*sizeof(AliHLTTrackMCLabel)==pBlock->fSize) {
392       for( unsigned int il=0; il<dataPtr->fCount; il++ ){
393         AliHLTTrackMCLabel &lab = dataPtr->fLabels[il];
394         mcLabels[lab.fTrackID] = lab.fMCLabel;
395       }
396     } else {
397       HLTWarning("data mismatch in block %s (0x%08x): count %d, size %d -> ignoring track MC information", 
398                  DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, 
399                  dataPtr->fCount, pBlock->fSize);
400     }
401   }
402   
403   
404   
405   for (int ndx=0; ndx<nBlocks && iResult>=0; ndx++) {
406
407     const AliHLTComponentBlockData* iter = blocks+ndx;
408     
409     // Read TPC tracks
410     
411     if( iter->fDataType == ( kAliHLTDataTypeTrack|kAliHLTDataOriginTPC ) ){       
412       fBenchmark.AddInput(iter->fSize);
413       AliHLTTracksData* dataPtr = ( AliHLTTracksData* ) iter->fPtr;
414       int nTracks = dataPtr->fCount;
415       AliHLTExternalTrackParam* currOutTrack = dataPtr->fTracklets;
416       for( int itr=0; itr<nTracks; itr++ ){
417         AliHLTGlobalBarrelTrack t(*currOutTrack);
418         Int_t mcLabel = -1;
419         if( mcLabels.find(currOutTrack->fTrackID)!=mcLabels.end() )
420           mcLabel = mcLabels[currOutTrack->fTrackID];
421         
422         tracksTPC.push_back( t );
423         tracksTPCLab.push_back(mcLabel);
424         tracksTPCId.push_back( currOutTrack->fTrackID );
425         unsigned int dSize = sizeof( AliHLTExternalTrackParam ) + currOutTrack->fNPoints * sizeof( unsigned int );
426         currOutTrack = ( AliHLTExternalTrackParam* )( (( Byte_t * )currOutTrack) + dSize );
427       }
428     }
429
430
431     // Read ITS clusters
432
433     if ( (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSSD) ) || 
434          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSPD) ) ||
435          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSDD) ) ||
436          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITS) ) 
437          ){
438       
439       fBenchmark.AddInput(iter->fSize);
440
441       AliHLTITSClusterData *inPtr=reinterpret_cast<AliHLTITSClusterData*>( iter->fPtr );
442       int nClusters = inPtr->fSpacePointCnt;
443       for( int icl=0; icl<nClusters; icl++ ){
444         AliHLTITSSpacePointData &d = inPtr->fSpacePoints[icl];
445
446         Int_t lab[4] = { d.fTracks[0], d.fTracks[1], d.fTracks[2], d.fIndex };
447         Int_t info[3] = { d.fNy, d.fNz, d.fLayer };
448         Float_t hit[6] = { d.fY, d.fZ, d.fSigmaY2, d.fSigmaZ2, d.fQ, d.fSigmaYZ };
449         if( d.fLayer==4 ) hit[5] = -hit[5];
450         fTracker->LoadCluster( AliITSRecPoint( lab, hit, info ) );
451         //cout<<"SG "<<d.fLayer<<" "<<d.fTracks[0]<<endl;
452       }   
453     }
454     
455   }// end read input blocks
456   
457   // Reconstruct the event
458
459     fBenchmark.Start(1);
460     fTracker->Reconstruct( &(tracksTPC[0]), &(tracksTPCLab[0]), tracksTPC.size() );
461     fBenchmark.Stop(1);
462
463   
464   // Fill output tracks
465   int nITSUpdated = 0;
466   {
467     
468     for( int iOut=0; iOut<=1; iOut++ ){
469
470       unsigned int blockSize = 0;
471
472       AliHLTTracksData* outPtr = ( AliHLTTracksData* )( outputPtr + size );
473       AliHLTExternalTrackParam* currOutTrack = outPtr->fTracklets;
474
475       blockSize =   ( ( AliHLTUInt8_t * )currOutTrack ) -  ( ( AliHLTUInt8_t * )outPtr );
476
477       if ( size + blockSize  > maxBufferSize ) {
478         HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), tracks are not stored", maxBufferSize, size + blockSize );
479         iResult = -ENOSPC;
480         break;
481       }
482
483       outPtr->fCount = 0;
484        AliHLTITSTrack *tracks=0;
485       int nTracks = 0;
486       if( iOut==0 ){
487         tracks = fTracker->Tracks();
488         nTracks = fTracker->NTracks();
489       } else{
490         tracks = fTracker->ITSOutTracks();
491         nTracks = fTracker->NITSOutTracks();
492       }
493       
494       for ( int itr = 0; itr < nTracks; itr++ ) {
495         AliHLTITSTrack &t = tracks[itr];
496         int id =  tracksTPCId[t.TPCtrackId()];      
497         int nClusters = t.GetNumberOfClusters();
498         if( iOut==0 && nClusters>0 ) nITSUpdated++;
499         
500         unsigned int dSize = sizeof( AliHLTExternalTrackParam ) + nClusters * sizeof( unsigned int );
501         
502         if ( size + blockSize + dSize > maxBufferSize ) {
503           HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), %d tracks are not stored", maxBufferSize, size + blockSize + dSize, nTracks - itr + 1 );
504           iResult = -ENOSPC;
505           break;
506         }
507         
508         currOutTrack->fAlpha = t.GetAlpha();
509         currOutTrack->fX = t.GetX();
510         currOutTrack->fY = t.GetY();
511         currOutTrack->fZ = t.GetZ();            
512         currOutTrack->fLastX = 0;
513         currOutTrack->fLastY = 0;
514         currOutTrack->fLastZ = 0;      
515         currOutTrack->fq1Pt = t.GetSigned1Pt();
516         currOutTrack->fSinPsi = t.GetSnp();
517         currOutTrack->fTgl = t.GetTgl();
518         for( int i=0; i<15; i++ ) currOutTrack->fC[i] = t.GetCovariance()[i];
519         currOutTrack->fTrackID = id;
520         currOutTrack->fFlags = 0;
521         currOutTrack->fNPoints = nClusters;    
522         for ( int i = 0; i < nClusters; i++ ) currOutTrack->fPointIDs[i] = t.GetClusterIndex( i );
523         currOutTrack = ( AliHLTExternalTrackParam* )( (( Byte_t * )currOutTrack) + dSize );
524         blockSize += dSize;
525         outPtr->fCount++;
526       }
527   
528
529       AliHLTComponentBlockData resultData;
530       FillBlockData( resultData );
531       resultData.fOffset = size;
532       resultData.fSize = blockSize;
533       if( iOut==0 ){
534         resultData.fDataType = kAliHLTDataTypeTrack|kAliHLTDataOriginITS;
535       } else {
536         resultData.fDataType = kAliHLTDataTypeTrack|kAliHLTDataOriginITSOut;
537       }
538       fBenchmark.AddOutput(resultData.fSize);
539       outputBlocks.push_back( resultData );
540       size += resultData.fSize;       
541     }
542   }  
543
544   {// fill MC labels
545
546     unsigned int blockSize = 0;
547       
548     AliHLTTrackMCData* outPtr = ( AliHLTTrackMCData* )( outputPtr +size );
549     AliHLTTrackMCLabel* currOutLabel = outPtr->fLabels;
550     
551     blockSize =   ( ( AliHLTUInt8_t * )currOutLabel ) -  ( ( AliHLTUInt8_t * )outPtr );
552     
553     outPtr->fCount = 0;
554     
555     AliHLTITSTrack *tracks= fTracker->Tracks();
556     int nTracks = fTracker->NTracks();
557     
558     for ( int itr = 0; itr < nTracks; itr++ ) {
559       AliHLTITSTrack &t = tracks[itr];
560       //cout<<"SG out:"<<tracksTPCId[t.TPCtrackId()]<<" "<<t.GetLabel()<<endl;
561       if( t.GetLabel()<0 ) continue;
562       int id =  tracksTPCId[t.TPCtrackId()];
563       
564       if ( blockSize + sizeof(AliHLTTrackMCLabel) > maxBufferSize ) {
565         HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), %d mc labels are not stored", maxBufferSize, blockSize, nTracks - itr + 1 );
566         iResult = -ENOSPC;
567         break;
568       }
569       currOutLabel->fTrackID = id;
570       currOutLabel->fMCLabel = t.GetLabel();
571       blockSize += sizeof(AliHLTTrackMCLabel);
572       currOutLabel++;
573       outPtr->fCount++;
574     }        
575     if( iResult>=0 && outPtr->fCount>0 ){
576       AliHLTComponentBlockData resultData;
577       FillBlockData( resultData );
578       resultData.fOffset = size;
579       resultData.fSize = blockSize;
580       resultData.fDataType = kAliHLTDataTypeTrackMC|kAliHLTDataOriginITS;
581       outputBlocks.push_back( resultData );
582       size+= resultData.fSize;
583     }
584   }
585   
586   fBenchmark.Stop(0);
587
588   // Set log level to "Warning" for on-line system monitoring
589   HLTInfo( "ITS Tracker: output %d tracks;  input %d clusters, %d tracks",
590            nITSUpdated, nClustersTotal, tracksTPC.size() );
591
592   HLTInfo(fBenchmark.GetStatistics());
593   return iResult;
594 }