]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCAGlobalMergerComponent.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCAGlobalMergerComponent.cxx
1 // $Id$
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 //                  Matthias Kretz <kretz@kde.org>                          *
9 //                  for The ALICE HLT Project.                              *
10 //                                                                          *
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.                    *
18 //                                                                          *
19 //***************************************************************************
20
21
22 /// @file   AliHLTTPCCAGlobalMergerComponent.cxx
23 /// @author Matthias Kretz
24 /// @date
25 /// @brief  HLT TPC CA global merger component.
26 ///
27
28 using namespace std;
29
30 #include "AliHLTTPCCAGlobalMergerComponent.h"
31 #include "AliHLTTPCCASliceOutput.h"
32
33 #include "AliHLTTPCCADef.h"
34 #include "AliHLTTPCCAMerger.h"
35 #include "AliHLTTPCCAMergerOutput.h"
36 #include "AliHLTTPCCATrackConvertor.h"
37
38 #include "AliHLTTPCGMMerger.h"
39 #include "AliHLTTPCGMMergedTrack.h"
40
41 #include "AliHLTTPCDefinitions.h"
42 #include "AliHLTTPCTransform.h"
43
44 #include "AliExternalTrackParam.h"
45 #include "AliCDBEntry.h"
46 #include "AliCDBManager.h"
47 #include "TObjString.h"
48 #include "TObjArray.h"
49 #include "AliHLTExternalTrackParam.h"
50
51 #include <climits>
52 #include <cstdlib>
53 #include <cerrno>
54
55
56 // ROOT macro for the implementation of ROOT specific class methods
57 ClassImp( AliHLTTPCCAGlobalMergerComponent )
58
59
60 AliHLTTPCCAGlobalMergerComponent::AliHLTTPCCAGlobalMergerComponent()
61 : AliHLTProcessor(), fVersion(1), fGlobalMergerVersion0( 0 ), fGlobalMerger(0), fSolenoidBz( 0 ), fClusterErrorCorrectionY(0), fClusterErrorCorrectionZ(0), fBenchmark("GlobalMerger")
62 {
63   // see header file for class documentation
64 }
65
66 AliHLTTPCCAGlobalMergerComponent::AliHLTTPCCAGlobalMergerComponent( const AliHLTTPCCAGlobalMergerComponent & ):AliHLTProcessor(), fVersion(1), fGlobalMergerVersion0( 0 ), fGlobalMerger(0), fSolenoidBz( 0 ), fClusterErrorCorrectionY(0), fClusterErrorCorrectionZ(0), fBenchmark("GlobalMerger")
67 {
68 // dummy
69 }
70
71 AliHLTTPCCAGlobalMergerComponent &AliHLTTPCCAGlobalMergerComponent::operator=( const AliHLTTPCCAGlobalMergerComponent & )
72 {
73   // dummy
74   return *this;
75 }
76
77 // Public functions to implement AliHLTComponent's interface.
78 // These functions are required for the registration process
79
80 const char *AliHLTTPCCAGlobalMergerComponent::GetComponentID()
81 {
82   // see header file for class documentation
83   return "TPCCAGlobalMerger";
84 }
85
86 void AliHLTTPCCAGlobalMergerComponent::GetInputDataTypes( AliHLTComponentDataTypeList &list )
87 {
88   // see header file for class documentation
89   list.clear();
90   list.push_back( AliHLTTPCCADefinitions::fgkTrackletsDataType );
91   //list.push_back( AliHLTTPCDefinitions::fgkTrackSegmentsDataType );
92   //list.push_back( AliHLTTPCDefinitions::fgkVertexDataType );
93 }
94
95 AliHLTComponentDataType AliHLTTPCCAGlobalMergerComponent::GetOutputDataType()
96 {
97   // see header file for class documentation
98   //return AliHLTTPCDefinitions::fgkTracksDataType; // old
99   return kAliHLTDataTypeTrack|kAliHLTDataOriginTPC;
100 }
101
102 void AliHLTTPCCAGlobalMergerComponent::GetOutputDataSize( unsigned long &constBase, double &inputMultiplier )
103 {
104   // see header file for class documentation
105   // XXX TODO: Find more realistic values.
106   constBase = 0;
107   inputMultiplier = 1.0;
108 }
109
110 AliHLTComponent *AliHLTTPCCAGlobalMergerComponent::Spawn()
111 {
112   // see header file for class documentation
113   return new AliHLTTPCCAGlobalMergerComponent;
114 }
115
116
117 void AliHLTTPCCAGlobalMergerComponent::SetDefaultConfiguration()
118 {
119   // Set default configuration for the CA merger component
120   // Some parameters can be later overwritten from the OCDB
121
122   fVersion = 1;
123   fSolenoidBz = -5.00668;
124   fClusterErrorCorrectionY = 0;
125   fClusterErrorCorrectionZ = 1.1;
126   fBenchmark.Reset();
127   fBenchmark.SetTimer(0,"total");
128   fBenchmark.SetTimer(1,"reco");    
129 }
130
131 int AliHLTTPCCAGlobalMergerComponent::ReadConfigurationString(  const char* arguments )
132 {
133   // Set configuration parameters for the CA merger component from the string
134
135   int iResult = 0;
136   if ( !arguments ) return iResult;
137
138   TString allArgs = arguments;
139   TString argument;
140   int bMissingParam = 0;
141
142   TObjArray* pTokens = allArgs.Tokenize( " " );
143
144   int nArgs =  pTokens ? pTokens->GetEntries() : 0;
145
146   for ( int i = 0; i < nArgs; i++ ) {
147     argument = ( ( TObjString* )pTokens->At( i ) )->GetString();
148     if ( argument.IsNull() ) continue;
149
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 );
154       continue;
155     }
156
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());
160       continue;
161     }
162
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 );
167       continue;
168     }
169
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 );
174       continue;
175     }
176
177     HLTError( "Unknown option \"%s\"", argument.Data() );
178     iResult = -EINVAL;
179   }
180   delete pTokens;
181
182   if ( bMissingParam ) {
183     HLTError( "Specifier missed for parameter \"%s\"", argument.Data() );
184     iResult = -EINVAL;
185   }
186
187   return iResult;
188 }
189
190
191 int AliHLTTPCCAGlobalMergerComponent::ReadCDBEntry( const char* cdbEntry, const char* chainId )
192 {
193   // see header file for class documentation
194
195   const char* defaultNotify = "";
196
197   if ( !cdbEntry ) {
198     cdbEntry = "HLT/ConfigTPC/TPCCAGlobalMerger";
199     defaultNotify = " (default)";
200     chainId = 0;
201   }
202
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());
205
206   if ( !pEntry ) {
207     HLTError( "cannot fetch object \"%s\" from CDB", cdbEntry );
208     return -EINVAL;
209   }
210
211   TObjString* pString = dynamic_cast<TObjString*>( pEntry->GetObject() );
212
213   if ( !pString ) {
214     HLTError( "configuration object \"%s\" has wrong type, required TObjString", cdbEntry );
215     return -EINVAL;
216   }
217
218   HLTInfo( "received configuration object string: \"%s\"", pString->GetString().Data() );
219
220   return  ReadConfigurationString( pString->GetString().Data() );
221 }
222
223
224
225 int AliHLTTPCCAGlobalMergerComponent::Configure( const char* cdbEntry, const char* chainId, const char *commandLine )
226 {
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
230
231   //* read hard-coded values
232
233   SetDefaultConfiguration();
234
235   //* read the default CDB entry
236
237   int iResult1 = ReadCDBEntry( NULL, chainId );
238
239   //* read magnetic field
240
241   fSolenoidBz = GetBz();
242
243   //* read the actual CDB entry if required
244
245   int iResult2 = ( cdbEntry ) ? ReadCDBEntry( cdbEntry, chainId ) : 0;
246
247   //* read extra parameters from input (if they are)
248
249   int iResult3 = 0;
250
251   if ( commandLine && commandLine[0] != '\0' ) {
252     HLTInfo( "received configuration string from HLT framework: \"%s\"", commandLine );
253     iResult3 = ReadConfigurationString( commandLine );
254   }
255
256
257   // Initialize the merger
258
259   AliHLTTPCCAParam param;
260
261   {
262     // get gemetry
263     int iSec = 0;
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 );
281     }
282
283     param.Initialize( iSec, nRows, rowX, alpha, dalpha,
284                       inRmin, outRmax, zMin, zMax, padPitch, sigmaZ, fSolenoidBz );
285
286     if( fClusterErrorCorrectionY>1.e-4 ) param.SetClusterError2CorrectionY( fClusterErrorCorrectionY*fClusterErrorCorrectionY );
287     if( fClusterErrorCorrectionZ>1.e-4 ) param.SetClusterError2CorrectionZ( fClusterErrorCorrectionZ*fClusterErrorCorrectionZ );
288     param.Update();
289
290     delete[] rowX;
291   }
292
293
294   if( fVersion==0 ) fGlobalMergerVersion0->SetSliceParam( param );
295   else fGlobalMerger->SetSliceParam( param );
296
297   return iResult1 ? iResult1 : ( iResult2 ? iResult2 : iResult3 );
298 }
299
300
301
302
303 int AliHLTTPCCAGlobalMergerComponent::DoInit( int argc, const char** argv )
304 {
305   // see header file for class documentation
306
307   if ( fGlobalMergerVersion0 || fGlobalMerger ) {
308     return EINPROGRESS;
309   }
310
311   fGlobalMergerVersion0 = new AliHLTTPCCAMerger();
312   fGlobalMerger         = new AliHLTTPCGMMerger();
313
314   TString arguments = "";
315   for ( int i = 0; i < argc; i++ ) {
316     if ( !arguments.IsNull() ) arguments += " ";
317     arguments += argv[i];
318   }
319
320   return Configure( NULL, NULL, arguments.Data()  );
321 }
322
323 int AliHLTTPCCAGlobalMergerComponent::Reconfigure( const char* cdbEntry, const char* chainId )
324 {
325   // Reconfigure the component from OCDB
326
327   return Configure( cdbEntry, chainId, NULL );
328 }
329
330
331
332 int AliHLTTPCCAGlobalMergerComponent::DoDeinit()
333 {
334   // see header file for class documentation
335   delete fGlobalMergerVersion0;
336   fGlobalMergerVersion0 = 0;
337   delete fGlobalMerger;
338   fGlobalMerger = 0;
339
340   return 0;
341 }
342
343 int AliHLTTPCCAGlobalMergerComponent::DoEvent( const AliHLTComponentEventData &evtData,
344     const AliHLTComponentBlockData *blocks, AliHLTComponentTriggerData &/*trigData*/,
345     AliHLTUInt8_t *outputPtr, AliHLTUInt32_t &size, AliHLTComponentBlockDataList &outputBlocks )
346 {
347   // see header file for class documentation
348   int iResult = 0;
349   unsigned int maxBufferSize = size;
350
351   size = 0;
352
353   if ( !outputPtr ) {
354     return -ENOSPC;
355   }
356   if ( !IsDataEvent() ) {
357     return 0;
358   }
359   fBenchmark.StartNewEvent();
360   fBenchmark.Start(0);
361
362   if( fVersion==0 )  fGlobalMergerVersion0->Clear();
363   else fGlobalMerger->Clear();
364
365   const AliHLTComponentBlockData *const blocksEnd = blocks + evtData.fBlockCnt;
366   for ( const AliHLTComponentBlockData *block = blocks; block < blocksEnd; ++block ) {
367     if ( block->fDataType != AliHLTTPCCADefinitions::fgkTrackletsDataType ) {
368       continue;
369     }
370
371     fBenchmark.AddInput(block->fSize);
372
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
378       iResult = -EBADF;
379       continue;
380     }
381
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() );
386     }
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 );
391
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);
397         fclose(fp);*/
398   }
399   fBenchmark.Start(1);
400   if( fVersion==0 ) fGlobalMergerVersion0->Reconstruct();
401   else fGlobalMerger->Reconstruct();
402   fBenchmark.Stop(1);
403
404   // Fill output 
405
406   if( fVersion==0 ){
407
408     const AliHLTTPCCAMergerOutput *mergerOutput = fGlobalMergerVersion0->Output();
409
410     unsigned int mySize = 0;
411     {
412       AliHLTTracksData* outPtr = ( AliHLTTracksData* )( outputPtr );
413       
414       AliHLTExternalTrackParam* currOutTrack = outPtr->fTracklets;
415       
416       mySize =   ( ( AliHLTUInt8_t * )currOutTrack ) -  ( ( AliHLTUInt8_t * )outputPtr );
417       
418       outPtr->fCount = 0;
419       
420       int nTracks = mergerOutput->NTracks();
421       
422       for ( int itr = 0; itr < nTracks; itr++ ) {
423         
424         // convert AliHLTTPCCAMergedTrack to AliHLTTrack
425         
426         const AliHLTTPCCAMergedTrack &track = mergerOutput->Track( itr );
427         
428         unsigned int dSize = sizeof( AliHLTExternalTrackParam ) + track.NClusters() * sizeof( unsigned int );
429         
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 );
432           iResult = -ENOSPC;
433           break;
434         }
435         
436         // first convert to AliExternalTrackParam
437         
438         AliExternalTrackParam tp, tpEnd;
439         AliHLTTPCCATrackConvertor::GetExtParam( track.InnerParam(), tp,  track.InnerAlpha() );
440         AliHLTTPCCATrackConvertor::GetExtParam( track.OuterParam(), tpEnd, track.OuterAlpha() );
441         
442         // normalize the angle to +-Pi
443         
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();      
448         {
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();
454         }
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 );
463         
464         currOutTrack = ( AliHLTExternalTrackParam* )( (( Byte_t * )currOutTrack) + dSize );
465         mySize += dSize;
466         outPtr->fCount++;
467       }    
468
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);
477       
478       size = resultData.fSize;
479     }
480     
481     HLTInfo( "CAGlobalMerger:: output %d tracks", mergerOutput->NTracks() );
482     fGlobalMergerVersion0->Clear();
483
484   } else { // new merger 
485
486     unsigned int mySize = 0;
487     {
488       AliHLTTracksData* outPtr = ( AliHLTTracksData* )( outputPtr );
489       AliHLTExternalTrackParam* currOutTrack = outPtr->fTracklets;
490       mySize =   ( ( AliHLTUInt8_t * )currOutTrack ) -  ( ( AliHLTUInt8_t * )outputPtr );
491       outPtr->fCount = 0;   
492       int nTracks = fGlobalMerger->NOutputTracks();
493
494       for ( int itr = 0; itr < nTracks; itr++ ) {
495
496         // convert AliHLTTPCGMMergedTrack to AliHLTTrack
497         
498         const AliHLTTPCGMMergedTrack &track = fGlobalMerger->OutputTracks()[ itr ];
499         if( !track.OK() ) continue;
500         unsigned int dSize = sizeof( AliHLTExternalTrackParam ) + track.NClusters() * sizeof( unsigned int );
501         
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 );
504           iResult = -ENOSPC;
505           break;
506         }
507
508         // first convert to AliExternalTrackParam
509
510         AliExternalTrackParam tp;
511         track.GetParam().GetExtParam( tp,  track.GetAlpha() );
512       
513         // normalize the angle to +-Pi
514               
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();
522       
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 ];
531         
532         currOutTrack = ( AliHLTExternalTrackParam* )( (( Byte_t * )currOutTrack) + dSize );
533         mySize += dSize;
534         outPtr->fCount++;
535       }
536   
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);
545       
546       size = resultData.fSize;
547     }
548
549     HLTInfo( "CAGlobalMerger:: output %d tracks", fGlobalMerger->NOutputTracks() );
550
551     fGlobalMerger->Clear();
552   }
553
554   fBenchmark.Stop(0);
555   HLTInfo( fBenchmark.GetStatistics() );
556   return iResult;
557 }
558