]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCATrackerComponent.cxx
converting to component registration by library agent - getting rid of global objects
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCATrackerComponent.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: Jochen Thaeder <thaeder@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 //                                                                           //
21 // a TPC tracker processing component for the HLT based on CA by Ivan Kisel  //
22 //                                                                           //
23 ///////////////////////////////////////////////////////////////////////////////
24
25 #if __GNUC__>= 3
26 using namespace std;
27 #endif
28
29 #include "AliHLTTPCCATrackerComponent.h"
30 #include "AliHLTTPCTransform.h"
31 #include "AliHLTTPCCATracker.h"
32 #include "AliHLTTPCCAHit.h"
33 #include "AliHLTTPCCAOutTrack.h"
34
35 #include "AliHLTTPCSpacePointData.h"
36 #include "AliHLTTPCClusterDataFormat.h"
37 #include "AliHLTTPCTransform.h"
38 #include "AliHLTTPCTrackSegmentData.h"
39 #include "AliHLTTPCTrackArray.h"
40 #include "AliHLTTPCTrackletDataFormat.h"
41 #include "AliHLTTPCDefinitions.h"
42 #include "TStopwatch.h"
43 #include "TMath.h"
44
45 /** ROOT macro for the implementation of ROOT specific class methods */
46 ClassImp(AliHLTTPCCATrackerComponent)
47
48 AliHLTTPCCATrackerComponent::AliHLTTPCCATrackerComponent()
49   :
50   fTracker(NULL),
51   fBField(0)
52 {
53   // see header file for class documentation
54   // or
55   // refer to README to build package
56   // or
57   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
58 }
59
60 AliHLTTPCCATrackerComponent::AliHLTTPCCATrackerComponent(const AliHLTTPCCATrackerComponent&)
61   :
62   AliHLTProcessor(),
63   fTracker(NULL),
64   fBField(0)
65 {
66   // see header file for class documentation
67   HLTFatal("copy constructor untested");
68 }
69
70 AliHLTTPCCATrackerComponent& AliHLTTPCCATrackerComponent::operator=(const AliHLTTPCCATrackerComponent&)
71 {
72   // see header file for class documentation
73   HLTFatal("assignment operator untested");
74   return *this;
75 }
76
77 AliHLTTPCCATrackerComponent::~AliHLTTPCCATrackerComponent()
78 {
79   // see header file for class documentation
80   delete fTracker;
81 }
82
83 //
84 // Public functions to implement AliHLTComponent's interface.
85 // These functions are required for the registration process
86 //
87
88 const char* AliHLTTPCCATrackerComponent::GetComponentID() 
89 {
90   // see header file for class documentation
91   return "TPCCATracker";
92 }
93
94 void AliHLTTPCCATrackerComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list) 
95 {
96   // see header file for class documentation
97   list.clear();
98   list.push_back( AliHLTTPCDefinitions::fgkClustersDataType );
99 }
100
101 AliHLTComponentDataType AliHLTTPCCATrackerComponent::GetOutputDataType() 
102 {
103   // see header file for class documentation
104   return AliHLTTPCDefinitions::fgkTrackSegmentsDataType;
105 }
106
107 void AliHLTTPCCATrackerComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier ) 
108 {
109   // define guess for the output data size
110   constBase = 200;       // minimum size
111   inputMultiplier = 0.5; // size relative to input 
112 }
113
114 AliHLTComponent* AliHLTTPCCATrackerComponent::Spawn() 
115 {
116   // see header file for class documentation
117   return new AliHLTTPCCATrackerComponent;
118 }
119
120 int AliHLTTPCCATrackerComponent::DoInit( int argc, const char** argv )
121 {
122   // Initialize the CA tracker component 
123   //
124   // arguments could be:
125   // bfield - the magnetic field value
126   //
127
128   if ( fTracker ) return EINPROGRESS;
129   
130   fTracker = new AliHLTTPCCATracker();
131   
132   // read command line
133
134   int i = 0;
135   char* cpErr;
136   while ( i < argc ){
137     if ( !strcmp( argv[i], "bfield" ) ){
138       if ( i+1 >= argc )
139         {
140           Logging( kHLTLogError, "HLT::TPCCATracker::DoInit", "Missing B-field", "Missing B-field specifier." );
141           return ENOTSUP;
142         }
143       fBField = strtod( argv[i+1], &cpErr );
144       if ( *cpErr )
145         {
146           Logging( kHLTLogError, "HLT::TPCCATracker::DoInit", "Missing multiplicity", "Cannot convert B-field specifier '%s'.", argv[i+1] );
147           return EINVAL;
148         }
149
150       Logging( kHLTLogDebug, "HLT::TPCCATracker::DoInit", "Reading command line",
151                "Magnetic field value is set to %f kG", fBField );
152
153       i += 2;
154       continue;
155     }
156     
157     Logging(kHLTLogError, "HLT::TPCCATracker::DoInit", "Unknown Option", "Unknown option '%s'", argv[i] );
158     return EINVAL;
159   }
160   
161   return 0;
162 }
163
164 int AliHLTTPCCATrackerComponent::DoDeinit()
165 {
166   // see header file for class documentation
167   if ( fTracker ) delete fTracker;
168   fTracker = NULL;  
169   return 0;
170 }
171
172 int AliHLTTPCCATrackerComponent::DoEvent
173
174  const AliHLTComponentEventData& evtData, 
175  const AliHLTComponentBlockData* blocks, 
176  AliHLTComponentTriggerData& /*trigData*/, 
177  AliHLTUInt8_t* outputPtr, 
178  AliHLTUInt32_t& size, 
179  vector<AliHLTComponentBlockData>& outputBlocks )
180 {
181
182   AliHLTUInt32_t MaxBufferSize = size;
183   size = 0; // output size
184
185   TStopwatch timer;
186
187   // Event reconstruction in one TPC slice with CA Tracker
188
189   Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "DoEvent", "DoEvent()" );
190   if ( evtData.fBlockCnt<=0 )
191     {
192       Logging( kHLTLogWarning, "HLT::TPCCATracker::DoEvent", "DoEvent", "no blocks in event" );
193       return 0;
194     }
195   
196   const AliHLTComponentBlockData* iter = NULL;
197   unsigned long ndx;
198   AliHLTTPCClusterData* inPtrSP; 
199  
200   // Determine the slice number 
201   
202   Int_t slice=-1;
203   {
204     std::vector<Int_t> slices;
205     std::vector<Int_t>::iterator slIter;
206     std::vector<unsigned> sliceCnts;
207     std::vector<unsigned>::iterator slCntIter;
208   
209     for ( ndx = 0; ndx < evtData.fBlockCnt; ndx++ ){
210       iter = blocks+ndx;
211       if ( iter->fDataType != AliHLTTPCDefinitions::fgkClustersDataType ) continue;
212
213       slice = AliHLTTPCDefinitions::GetMinSliceNr( *iter );
214
215       Bool_t found = 0;
216       slCntIter = sliceCnts.begin();
217       for( slIter = slices.begin(); slIter!=slices.end(); slIter++, slCntIter++ ){
218         if ( *slIter == slice ){
219           found = kTRUE;
220           break;
221         }
222       }
223       if ( !found ){
224         slices.push_back( slice );
225         sliceCnts.push_back( 1 );
226       } else *slCntIter++;     
227     }
228   
229     
230     // Determine slice number to really use.
231     if ( slices.size()>1 )
232       {
233         Logging( kHLTLogError, "HLT::TPCSliceTracker::DoEvent", "Multiple slices found in event",
234                  "Multiple slice numbers found in event 0x%08lX (%lu). Determining maximum occuring slice number...",
235                  evtData.fEventID, evtData.fEventID );
236         unsigned maxCntSlice=0;
237         slCntIter = sliceCnts.begin();
238         for( slIter = slices.begin(); slIter != slices.end(); slIter++, slCntIter++ )
239           {
240             Logging( kHLTLogError, "HLT::TPCSliceTracker::DoEvent", "Multiple slices found in event",
241                      "Slice %lu found %lu times.", *slIter, *slCntIter );
242             if ( maxCntSlice<*slCntIter )
243               {
244                 maxCntSlice = *slCntIter;
245                 slice = *slIter;
246               }
247           }
248         Logging( kHLTLogError, "HLT::TPCSliceTracker::DoEvent", "Multiple slices found in event",
249                  "Using slice %lu.", slice );
250       }
251     else if ( slices.size()>0 )
252       {
253         slice = *(slices.begin());
254       }
255   }
256   
257   if( slice<0 ){
258     Logging( kHLTLogWarning, "HLT::TPCCATracker::DoEvent", "DoEvent", "no slices found in event" );
259     return 0;
260   }
261
262
263   // Initialize the tracker
264
265   Double_t Bz = fBField;
266   
267   {
268     if( !fTracker ) fTracker = new AliHLTTPCCATracker;
269     Int_t iSec = slice;
270     Double_t inRmin = 83.65; 
271     //    Double_t inRmax = 133.3;
272     //    Double_t outRmin = 133.5; 
273     Double_t outRmax = 247.7;
274     Double_t plusZmin = 0.0529937; 
275     Double_t plusZmax = 249.778; 
276     Double_t minusZmin = -249.645; 
277     Double_t minusZmax = -0.0799937; 
278     Double_t dalpha = 0.349066;
279     Double_t alpha = 0.174533 + dalpha*iSec;
280     
281     Bool_t zPlus = (iSec<18 );
282     Double_t zMin =  zPlus ?plusZmin :minusZmin;
283     Double_t zMax =  zPlus ?plusZmax :minusZmax;
284     //TPCZmin = -249.645, ZMax = 249.778    
285     //    Double_t rMin =  inRmin;
286     //    Double_t rMax =  outRmax;
287     Int_t NRows = AliHLTTPCTransform::GetNRows();
288         
289     Double_t padPitch = 0.4;
290     Double_t sigmaZ = 0.228808;
291     
292      Double_t rowX[NRows];
293     for( Int_t irow=0; irow<NRows; irow++){
294       rowX[irow] = AliHLTTPCTransform::Row2X( irow );
295     }     
296      
297     AliHLTTPCCAParam param;
298     param.Initialize( iSec, NRows, rowX, alpha, dalpha,
299                       inRmin, outRmax, zMin, zMax, padPitch, sigmaZ, Bz );
300     param.YErrorCorrection() = 1;
301     param.ZErrorCorrection() = 2;
302
303     fTracker->Initialize( param ); 
304
305   }
306
307     
308   // min and max patch numbers and row numbers
309
310   Int_t row[2] = {0,0};
311   Int_t minPatch=INT_MAX, maxPatch = -1;
312
313   // total n Hits
314
315   Int_t nHitsTotal = 0;
316
317   // sort patches
318
319   std::vector<unsigned long> patchIndices;
320
321   for ( ndx = 0; ndx < evtData.fBlockCnt; ndx++ ){
322     iter = blocks+ndx;      
323     if( iter->fDataType != AliHLTTPCDefinitions::fgkClustersDataType ) continue;
324     if( slice!=AliHLTTPCDefinitions::GetMinSliceNr( *iter ) ) continue;
325     inPtrSP = (AliHLTTPCClusterData*)(iter->fPtr);
326     nHitsTotal+=inPtrSP->fSpacePointCnt;
327     Int_t patch = AliHLTTPCDefinitions::GetMinPatchNr( *iter );
328     if ( minPatch>patch ){
329       minPatch = patch;
330       row[0] = AliHLTTPCTransform::GetFirstRow( patch );
331     }
332     if ( maxPatch<patch ){
333       maxPatch = patch;
334       row[1] = AliHLTTPCTransform::GetLastRow( patch );
335     }
336     std::vector<unsigned long>::iterator pIter = patchIndices.begin(); 
337     while( pIter!=patchIndices.end() && AliHLTTPCDefinitions::GetMinPatchNr( blocks[*pIter] ) < patch ){
338       pIter++;
339     }
340     patchIndices.insert( pIter, ndx );
341   }
342            
343
344   // pass event to CA Tracker
345   
346   fTracker->StartEvent();
347
348   AliHLTTPCCAHit vHits[nHitsTotal]; // CA hit array
349   Double_t vHitStoreX[nHitsTotal];       // hit X coordinates
350   Int_t vHitStoreID[nHitsTotal];            // hit ID's
351   Int_t vHitRowID[nHitsTotal];            // hit ID's
352
353   Int_t nHits = 0;
354  
355   Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Reading hits",
356            "Total %d hits to read for slice %d", nHitsTotal, slice );
357
358   Int_t nClusters=0;
359
360   for( std::vector<unsigned long>::iterator pIter = patchIndices.begin(); pIter!=patchIndices.end(); pIter++ ){
361     ndx = *pIter;
362     iter = blocks+ndx;
363       
364     Int_t patch = AliHLTTPCDefinitions::GetMinPatchNr( *iter );
365     inPtrSP = (AliHLTTPCClusterData*)(iter->fPtr);
366       
367     Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Reading hits",
368              "Reading %d hits for slice %d - patch %d", inPtrSP->fSpacePointCnt, slice, patch );
369       
370     // Read patch hits, row by row
371
372     Int_t oldRow = -1;
373     Int_t nRowHits = 0;
374     Int_t firstRowHit = 0;
375     for (UInt_t i=0; i<inPtrSP->fSpacePointCnt; i++ ){  
376       AliHLTTPCSpacePointData* pSP = &(inPtrSP->fSpacePoints[i]);
377
378       if( pSP->fPadRow != oldRow ){
379         if( oldRow>=0 ) fTracker->ReadHitRow( oldRow, vHits+firstRowHit, nRowHits );
380         oldRow = pSP->fPadRow;
381         firstRowHit = nHits;
382         nRowHits = 0;
383       }
384       AliHLTTPCCAHit &h = vHits[nHits];
385       if( TMath::Abs(pSP->fX- fTracker->Rows()[pSP->fPadRow].X() )>1.e-4 ) cout<<"row "<<(Int_t)pSP->fPadRow<<" "<<fTracker->Rows()[pSP->fPadRow].X()-pSP->fX <<endl;
386
387       h.Y() = pSP->fY;
388       h.Z() = pSP->fZ;
389       h.ErrY() = TMath::Sqrt(TMath::Abs(pSP->fSigmaY2));
390       h.ErrZ() = TMath::Sqrt(TMath::Abs(pSP->fSigmaZ2));  
391       if( h.ErrY()<.1 ) h.ErrY() = .1;
392       if( h.ErrZ()<.1 ) h.ErrZ() = .1;
393       if( h.ErrY()>1. ) h.ErrY() = 1.;
394       if( h.ErrZ()>1. ) h.ErrZ() = 1.;
395       h.ID() = nHits;
396       vHitStoreX[nHits] = pSP->fX;
397       vHitStoreID[nHits] = pSP->fID;
398       vHitRowID[nHits] = pSP->fPadRow;
399       nHits++;  
400       nRowHits++;
401       nClusters++;
402     }   
403     if( oldRow>=0 ) fTracker->ReadHitRow( oldRow, vHits+firstRowHit, nRowHits );
404   }
405
406   // reconstruct the event  
407
408   TStopwatch timerReco;
409
410   fTracker->Reconstruct();
411
412   timerReco.Stop();
413
414   Int_t ret = 0;
415
416   Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Reconstruct",
417            "%d tracks found for slice %d",fTracker->NOutTracks(), slice);
418
419   // write reconstructed tracks
420
421   AliHLTTPCTrackletData* outPtr = (AliHLTTPCTrackletData*)(outputPtr);
422
423   AliHLTTPCTrackSegmentData* currOutTracklet = outPtr->fTracklets;
424
425   Int_t ntracks = fTracker->NOutTracks();
426
427   UInt_t mySize =   ((AliHLTUInt8_t *)currOutTracklet) -  ((AliHLTUInt8_t *)outputPtr);
428
429   outPtr->fTrackletCnt = 0; 
430
431   for( int itr=0; itr<ntracks; itr++ ){
432     
433     AliHLTTPCCAOutTrack &t = fTracker->OutTracks()[itr];    
434
435     //Logging( kHLTLogDebug, "HLT::TPCCATracker::DoEvent", "Wrtite output","track %d with %d hits", itr, t.NHits());
436
437     // calculate output track size
438
439     UInt_t dSize = sizeof(AliHLTTPCTrackSegmentData) + t.NHits()*sizeof(UInt_t);
440     
441     if( mySize + dSize > MaxBufferSize ){
442       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);
443       ret = -ENOSPC;
444       break;
445     }
446     
447     // convert CA track parameters to HLT Track Segment
448
449     Int_t iFirstHit = fTracker->OutTrackHits()[t.FirstHitRef()];
450     Int_t iLastHit = fTracker->OutTrackHits()[t.FirstHitRef()+t.NHits()-1];
451     
452     AliHLTTPCCAHit &firstHit = vHits[iFirstHit];
453     AliHLTTPCCAHit &lastHit = vHits[iLastHit];
454    
455     t.Param().TransportBz(Bz, vHitStoreX[iFirstHit], firstHit.Y(), firstHit.Z() );
456
457     currOutTracklet->fX = t.Param().Par()[0];
458     currOutTracklet->fY = t.Param().Par()[1];
459     currOutTracklet->fZ = t.Param().Par()[2];
460     Double_t qp = t.Param().Par()[6];
461     Double_t p = TMath::Abs(qp)>1.e-5 ?1./TMath::Abs(qp) :1.e5;
462     Double_t ex = t.Param().Par()[3];
463     Double_t ey = t.Param().Par()[4];
464     Double_t ez = t.Param().Par()[5];
465     Double_t et = TMath::Sqrt( ex*ex + ey*ey );
466     
467     currOutTracklet->fCharge = (qp>0) ?+1 :(qp<0 ?-1 :0);
468     currOutTracklet->fPt = p*et;
469
470     Double_t h3 =  TMath::Abs(ex) >1.e-5 ? p*ex/et :0;
471     Double_t h4 =  TMath::Abs(ey) >1.e-5 ? p*ey/et :0;
472     Double_t h5;
473     Double_t h6 =  - currOutTracklet->fCharge * p * currOutTracklet->fPt;
474   
475     currOutTracklet->fPterr = ( h3*h3*t.Param().Cov()[9] + h4*h4*t.Param().Cov()[14] + h6*h6*t.Param().Cov()[27] 
476                                 + 2.*(h3*h4*t.Param().Cov()[13]+h3*h6*t.Param().Cov()[24]+h4*h6*t.Param().Cov()[25] )
477                                 );    
478     currOutTracklet->fPsi = TMath::ATan2(ey, ex);
479     
480     h3 =  ex/(et*et);
481     h4 = -ey/(et*et);
482     currOutTracklet->fPsierr = h3*h3*t.Param().Cov()[9] + h4*h4*t.Param().Cov()[14] + 2.*h3*h4*t.Param().Cov()[13];
483     
484     currOutTracklet->fTgl = TMath::Abs(et)>1.e-5  ? ez/et :1.e5;
485       
486     if( TMath::Abs(et) >1.e-2 ){
487       h3 = -ez*ex/et/et;
488       h4 = -ez*ey/et/et;
489       h5 = 1.;
490       currOutTracklet->fTglerr =  ( h3*h3*t.Param().Cov()[9] + h4*h4*t.Param().Cov()[14] + h5*h5*t.Param().Cov()[20] 
491                                     + 2.*(h3*h4*t.Param().Cov()[13]+h3*h5*t.Param().Cov()[18]+h4*h5*t.Param().Cov()[19] )
492                                     )/et/et;
493     }else{
494       currOutTracklet->fTglerr =  1.e-2;
495     }
496
497     currOutTracklet->fCharge = -currOutTracklet->fCharge;
498     
499     t.Param().TransportBz(Bz, vHitStoreX[iLastHit], lastHit.Y(), lastHit.Z() );
500      
501     currOutTracklet->fLastX = t.Param().Par()[0];
502     currOutTracklet->fLastY = t.Param().Par()[1];
503     currOutTracklet->fLastZ = t.Param().Par()[2];
504
505 #ifdef INCLUDE_TPC_HOUGH
506 #ifdef ROWHOUGHPARAMS
507     currOutTracklet->fTrackID = 0;
508     currOutTracklet->fRowRange1 = vHitRowID[iFirstHit];
509     currOutTracklet->fRowRange2 = vHitRowID[iLastHit];
510     currOutTracklet->fSector = slice;
511     currOutTracklet->fPID = 211;
512 #endif
513 #endif // INCLUDE_TPC_HOUGH
514
515
516     currOutTracklet->fNPoints = t.NHits();
517
518     for( Int_t i=0; i<t.NHits(); i++ ){
519       currOutTracklet->fPointIDs[i] = vHitStoreID[fTracker->OutTrackHits()[t.FirstHitRef()+i]];
520     }
521
522     currOutTracklet = (AliHLTTPCTrackSegmentData*)( (Byte_t *)currOutTracklet + dSize );
523     mySize+=dSize;
524     outPtr->fTrackletCnt++; 
525   }
526   
527   
528   AliHLTComponentBlockData bd;
529   FillBlockData( bd );
530   bd.fOffset = 0;
531   bd.fSize = mySize;
532   bd.fSpecification = AliHLTTPCDefinitions::EncodeDataSpecification( slice, slice, minPatch, maxPatch );      
533   outputBlocks.push_back( bd );
534   
535   size = mySize;
536   
537   timerReco.Stop();
538
539   // Set log level to "Warning" for on-line system monitoring
540
541   Logging( kHLTLogWarning, "HLT::TPCCATracker::DoEvent", "Tracks",
542            "CATracker slice %d: output %d tracks;  input %d clusters, patches %d..%d, rows %d..%d; reco time %d/%d us", 
543            slice, ntracks, nClusters, minPatch, maxPatch, row[0], row[1], (Int_t) (timer.RealTime()*1.e6), (Int_t) (timerReco.RealTime()*1.e6) );
544
545   return ret;
546
547 }
548
549