]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/ITS/tracking/AliHLTITSTrackerComponent.cxx
MC labels added for ITS tracks ?\127 ?\127?\127
[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
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   int iResult2 = 0; //ReadCDBEntry( kAliHLTCDBSolenoidBz, chainId );
253   fSolenoidBz = GetBz();
254
255   //* read the actual CDB entry if required
256
257   int iResult3 = ( cdbEntry ) ? ReadCDBEntry( cdbEntry, chainId ) : 0;
258
259   //* read extra parameters from input (if they are)
260
261   int iResult4 = 0;
262
263   if ( commandLine && commandLine[0] != '\0' ) {
264     HLTInfo( "received configuration string from HLT framework: \"%s\"", commandLine );
265     iResult4 = ReadConfigurationString( commandLine );
266   }
267
268   // Initialise the tracker here
269
270   return iResult1 ? iResult1 : ( iResult2 ? iResult2 : ( iResult3 ? iResult3 : iResult4 ) );
271 }
272
273
274
275 int AliHLTITSTrackerComponent::DoInit( int argc, const char** argv )
276 {
277   // Configure the ITS tracker component
278
279   if ( fTracker ) return -EINPROGRESS;
280
281   if(AliGeomManager::GetGeometry()==NULL){
282     AliGeomManager::LoadGeometry();
283   }
284   AliGeomManager::ApplyAlignObjsFromCDB("ITS");
285
286   TString arguments = "";
287   for ( int i = 0; i < argc; i++ ) {
288     if ( !arguments.IsNull() ) arguments += " ";
289     arguments += argv[i];
290   }
291
292   int ret = Configure( NULL, NULL, arguments.Data() );
293
294   // Check field
295   if (!TGeoGlobalMagField::Instance()) {
296     HLTError("magnetic field not initialized, please set up TGeoGlobalMagField and AliMagF");
297     return -ENODEV;
298   }
299   fSolenoidBz=GetBz();
300
301   fTracker = new AliITStrackerHLT(0);
302   fBenchmark.Reset();
303   fBenchmark.SetTimer(0,"total");
304   fBenchmark.SetTimer(1,"reco");
305   return ret;
306 }
307
308
309 int AliHLTITSTrackerComponent::DoDeinit()
310 {
311   // see header file for class documentation
312   delete fTracker;
313   fTracker = 0;
314   return 0;
315 }
316
317
318
319 int AliHLTITSTrackerComponent::Reconfigure( const char* cdbEntry, const char* chainId )
320 {
321   // Reconfigure the component from OCDB .
322
323   return Configure( cdbEntry, chainId, NULL );
324 }
325
326
327
328 int AliHLTITSTrackerComponent::DoEvent
329 (
330   const AliHLTComponentEventData& evtData,
331   const AliHLTComponentBlockData* blocks,
332   AliHLTComponentTriggerData& /*trigData*/,
333   AliHLTUInt8_t* outputPtr,
334   AliHLTUInt32_t& size,
335   vector<AliHLTComponentBlockData>& outputBlocks )
336 {
337   //* process event
338
339   AliHLTUInt32_t maxBufferSize = size;
340   size = 0; // output size
341   
342   if (!IsDataEvent()) return 0;
343
344   if ( evtData.fBlockCnt <= 0 ) {
345     HLTWarning( "no blocks in event" );
346     return 0;
347   }
348
349   fBenchmark.StartNewEvent();
350   fBenchmark.Start(0);
351
352   // Event reconstruction in ITS
353
354   int iResult=0;
355
356   int nBlocks = evtData.fBlockCnt;
357
358   
359   vector< AliExternalTrackParam > tracksTPC;
360   vector< int > tracksTPCLab;
361   vector< int > tracksTPCId;
362
363   int nClustersTotal = 0;
364
365   for (int ndx=0; ndx<nBlocks && iResult>=0; ndx++) {
366
367     const AliHLTComponentBlockData* iter = blocks+ndx;
368  
369     if ( (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSSD) ) || 
370          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSPD) ) ||
371          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSDD) ) ||
372          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITS) ) 
373          ){      
374       AliHLTITSClusterData *inPtr=reinterpret_cast<AliHLTITSClusterData*>( iter->fPtr );
375       nClustersTotal+=inPtr->fSpacePointCnt;
376     }         
377   }
378
379
380   fTracker->StartLoadClusters(nClustersTotal);
381
382   // first read MC information (if present)
383   
384   std::map<int,int> mcLabels;
385
386   for (const AliHLTComponentBlockData* pBlock=GetFirstInputBlock(kAliHLTDataTypeTrackMC|kAliHLTDataOriginTPC);
387        pBlock!=NULL; pBlock=GetNextInputBlock()) {
388     
389     fBenchmark.AddInput(pBlock->fSize);
390     
391     AliHLTTrackMCData* dataPtr = reinterpret_cast<AliHLTTrackMCData*>( pBlock->fPtr );
392     if (sizeof(AliHLTTrackMCData)+dataPtr->fCount*sizeof(AliHLTTrackMCLabel)==pBlock->fSize) {
393       for( unsigned int il=0; il<dataPtr->fCount; il++ ){
394         AliHLTTrackMCLabel &lab = dataPtr->fLabels[il];
395         mcLabels[lab.fTrackID] = lab.fMCLabel;
396       }
397     } else {
398       HLTWarning("data mismatch in block %s (0x%08x): count %d, size %d -> ignoring track MC information", 
399                  DataType2Text(pBlock->fDataType).c_str(), pBlock->fSpecification, 
400                  dataPtr->fCount, pBlock->fSize);
401     }
402   }
403   
404   
405   
406   for (int ndx=0; ndx<nBlocks && iResult>=0; ndx++) {
407
408     const AliHLTComponentBlockData* iter = blocks+ndx;
409     
410     // Read TPC tracks
411     
412     if( iter->fDataType == ( kAliHLTDataTypeTrack|kAliHLTDataOriginTPC ) ){       
413       fBenchmark.AddInput(iter->fSize);
414       AliHLTTracksData* dataPtr = ( AliHLTTracksData* ) iter->fPtr;
415       int nTracks = dataPtr->fCount;
416       AliHLTExternalTrackParam* currOutTrack = dataPtr->fTracklets;
417       for( int itr=0; itr<nTracks; itr++ ){
418         AliHLTGlobalBarrelTrack t(*currOutTrack);
419         Int_t mcLabel = -1;
420         if( mcLabels.find(currOutTrack->fTrackID)!=mcLabels.end() )
421           mcLabel = mcLabels[currOutTrack->fTrackID];
422         
423         tracksTPC.push_back( t );
424         tracksTPCLab.push_back(mcLabel);
425         tracksTPCId.push_back( currOutTrack->fTrackID );
426         unsigned int dSize = sizeof( AliHLTExternalTrackParam ) + currOutTrack->fNPoints * sizeof( unsigned int );
427         currOutTrack = ( AliHLTExternalTrackParam* )( (( Byte_t * )currOutTrack) + dSize );
428       }
429     }
430
431
432     // Read ITS clusters
433
434     if ( (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSSD) ) || 
435          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSPD) ) ||
436          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITSSDD) ) ||
437          (iter->fDataType == (kAliHLTDataTypeClusters|kAliHLTDataOriginITS) ) 
438          ){
439       
440       fBenchmark.AddInput(iter->fSize);
441
442       AliHLTITSClusterData *inPtr=reinterpret_cast<AliHLTITSClusterData*>( iter->fPtr );
443       int nClusters = inPtr->fSpacePointCnt;
444       for( int icl=0; icl<nClusters; icl++ ){
445         AliHLTITSSpacePointData &d = inPtr->fSpacePoints[icl];
446
447         Int_t lab[4] = { d.fTracks[0], d.fTracks[1], d.fTracks[2], d.fIndex };
448         Int_t info[3] = { d.fNy, d.fNz, d.fLayer };
449         Float_t hit[6] = { d.fY, d.fZ, d.fSigmaY2, d.fSigmaZ2, d.fQ, d.fSigmaYZ };
450         if( d.fLayer==4 ) hit[5] = -hit[5];
451         fTracker->LoadCluster( AliITSRecPoint( lab, hit, info ) );
452         //cout<<"SG "<<d.fLayer<<" "<<d.fTracks[0]<<endl;
453       }   
454     }
455     
456   }// end read input blocks
457   
458   // Reconstruct the event
459
460     fBenchmark.Start(1);
461     fTracker->Reconstruct( &(tracksTPC[0]), &(tracksTPCLab[0]), tracksTPC.size() );
462     fBenchmark.Stop(1);
463
464   
465   // Fill output tracks
466   int nITSUpdated = 0;
467   {
468     
469     for( int iOut=0; iOut<=1; iOut++ ){
470
471       unsigned int blockSize = 0;
472
473       AliHLTTracksData* outPtr = ( AliHLTTracksData* )( outputPtr + size );
474       AliHLTExternalTrackParam* currOutTrack = outPtr->fTracklets;
475
476       blockSize =   ( ( AliHLTUInt8_t * )currOutTrack ) -  ( ( AliHLTUInt8_t * )outPtr );
477
478       if ( size + blockSize  > maxBufferSize ) {
479         HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), tracks are not stored", maxBufferSize, size + blockSize );
480         iResult = -ENOSPC;
481         break;
482       }
483
484       outPtr->fCount = 0;
485        AliHLTITSTrack *tracks=0;
486       int nTracks = 0;
487       if( iOut==0 ){
488         tracks = fTracker->Tracks();
489         nTracks = fTracker->NTracks();
490       } else{
491         tracks = fTracker->ITSOutTracks();
492         nTracks = fTracker->NITSOutTracks();
493       }
494       
495       for ( int itr = 0; itr < nTracks; itr++ ) {
496         AliHLTITSTrack &t = tracks[itr];
497         int id =  tracksTPCId[t.TPCtrackId()];      
498         int nClusters = t.GetNumberOfClusters();
499         if( iOut==0 && nClusters>0 ) nITSUpdated++;
500         
501         unsigned int dSize = sizeof( AliHLTExternalTrackParam ) + nClusters * sizeof( unsigned int );
502         
503         if ( size + blockSize + dSize > maxBufferSize ) {
504           HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), %d tracks are not stored", maxBufferSize, size + blockSize + dSize, nTracks - itr + 1 );
505           iResult = -ENOSPC;
506           break;
507         }
508         
509         currOutTrack->fAlpha = t.GetAlpha();
510         currOutTrack->fX = t.GetX();
511         currOutTrack->fY = t.GetY();
512         currOutTrack->fZ = t.GetZ();            
513         currOutTrack->fLastX = 0;
514         currOutTrack->fLastY = 0;
515         currOutTrack->fLastZ = 0;      
516         currOutTrack->fq1Pt = t.GetSigned1Pt();
517         currOutTrack->fSinPsi = t.GetSnp();
518         currOutTrack->fTgl = t.GetTgl();
519         for( int i=0; i<15; i++ ) currOutTrack->fC[i] = t.GetCovariance()[i];
520         currOutTrack->fTrackID = id;
521         currOutTrack->fFlags = 0;
522         currOutTrack->fNPoints = nClusters;    
523         for ( int i = 0; i < nClusters; i++ ) currOutTrack->fPointIDs[i] = t.GetClusterIndex( i );
524         currOutTrack = ( AliHLTExternalTrackParam* )( (( Byte_t * )currOutTrack) + dSize );
525         blockSize += dSize;
526         outPtr->fCount++;
527       }
528   
529
530       AliHLTComponentBlockData resultData;
531       FillBlockData( resultData );
532       resultData.fOffset = size;
533       resultData.fSize = blockSize;
534       if( iOut==0 ){
535         resultData.fDataType = kAliHLTDataTypeTrack|kAliHLTDataOriginITS;
536       } else {
537         resultData.fDataType = kAliHLTDataTypeTrack|kAliHLTDataOriginITSOut;
538       }
539       fBenchmark.AddOutput(resultData.fSize);
540       outputBlocks.push_back( resultData );
541       size += resultData.fSize;       
542     }
543   }  
544
545   {// fill MC labels
546
547     unsigned int blockSize = 0;
548       
549     AliHLTTrackMCData* outPtr = ( AliHLTTrackMCData* )( outputPtr +size );
550     AliHLTTrackMCLabel* currOutLabel = outPtr->fLabels;
551     
552     blockSize =   ( ( AliHLTUInt8_t * )currOutLabel ) -  ( ( AliHLTUInt8_t * )outPtr );
553     
554     outPtr->fCount = 0;
555     
556     AliHLTITSTrack *tracks= fTracker->Tracks();
557     int nTracks = fTracker->NTracks();
558     
559     for ( int itr = 0; itr < nTracks; itr++ ) {
560       AliHLTITSTrack &t = tracks[itr];
561       //cout<<"SG out:"<<tracksTPCId[t.TPCtrackId()]<<" "<<t.GetLabel()<<endl;
562       if( t.GetLabel()<0 ) continue;
563       int id =  tracksTPCId[t.TPCtrackId()];
564       
565       if ( blockSize + sizeof(AliHLTTrackMCLabel) > maxBufferSize ) {
566         HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), %d mc labels are not stored", maxBufferSize, blockSize, nTracks - itr + 1 );
567         iResult = -ENOSPC;
568         break;
569       }
570       currOutLabel->fTrackID = id;
571       currOutLabel->fMCLabel = t.GetLabel();
572       blockSize += sizeof(AliHLTTrackMCLabel);
573       currOutLabel++;
574       outPtr->fCount++;
575     }        
576     
577     AliHLTComponentBlockData resultData;
578     FillBlockData( resultData );
579     resultData.fOffset = size;
580     resultData.fSize = blockSize;
581     resultData.fDataType = kAliHLTDataTypeTrackMC|kAliHLTDataOriginITS;
582     outputBlocks.push_back( resultData );
583     size+= resultData.fSize;
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 }