]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCAGlobalMergerComponent.cxx
1bf13f28e8b5737598c4217894a1c5f968aa9a83
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCAGlobalMergerComponent.cxx
1 // **************************************************************************
2 // This file is property of and copyright by the ALICE HLT Project          * 
3 // ALICE Experiment at CERN, All rights reserved.                           *
4 //                                                                          *
5 // Primary Authors: Sergey Gorbunov <sergey.gorbunov@kip.uni-heidelberg.de> *
6 //                  Ivan Kisel <kisel@kip.uni-heidelberg.de>                *
7 //                  Matthias Kretz <kretz@kde.org>                          *
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 /** @file   AliHLTTPCCAGlobalMergerComponent.cxx
22     @author Matthias Kretz
23     @date
24     @brief  HLT TPC CA global merger component.
25 */
26
27 #if __GNUC__>= 3
28 using namespace std;
29 #endif
30
31 #include "AliHLTTPCCAGlobalMergerComponent.h"
32 #include "AliHLTTPCCAMergerOutput.h"
33 #include "AliHLTTPCTransform.h"
34 #include "AliHLTTPCCAMerger.h"
35 #include "AliHLTTPCVertex.h"
36 #include "AliHLTTPCVertexData.h"
37 #include "AliHLTTPCTrackSegmentData.h"
38 #include "AliHLTTPCTrack.h"
39 #include "AliHLTTPCTrackArray.h"
40 #include "AliHLTTPCTrackletDataFormat.h"
41 #include "AliHLTTPCCADef.h"
42 #include "AliHLTTPCDefinitions.h"
43 #include "AliHLTTPCCATrackConvertor.h"
44 #include "AliHLTTPCCASliceOutput.h"
45
46 #include "AliCDBEntry.h"
47 #include "AliCDBManager.h"
48 #include "TObjString.h"
49 #include "TObjArray.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 // global object for registration
61 AliHLTTPCCAGlobalMergerComponent AliHLTTPCCAGlobalMergerComponent::fgAliHLTTPCCAGlobalMergerComponent;
62
63 AliHLTTPCCAGlobalMergerComponent::AliHLTTPCCAGlobalMergerComponent()
64   : fGlobalMerger( 0 ), fSolenoidBz(5)
65 {
66   // see header file for class documentation
67 }
68
69 // Public functions to implement AliHLTComponent's interface.
70 // These functions are required for the registration process
71
72 const char *AliHLTTPCCAGlobalMergerComponent::GetComponentID()
73 {
74   // see header file for class documentation
75   return "TPCCAGlobalMerger";
76 }
77
78 void AliHLTTPCCAGlobalMergerComponent::GetInputDataTypes( AliHLTComponentDataTypeList &list )
79 {
80   // see header file for class documentation
81   list.clear();
82   list.push_back( AliHLTTPCCADefinitions::fgkTrackletsDataType );
83   //list.push_back( AliHLTTPCDefinitions::fgkTrackSegmentsDataType );
84   //list.push_back( AliHLTTPCDefinitions::fgkVertexDataType );
85 }
86
87 AliHLTComponentDataType AliHLTTPCCAGlobalMergerComponent::GetOutputDataType()
88 {
89   // see header file for class documentation
90   return AliHLTTPCDefinitions::fgkTracksDataType;
91 }
92
93 void AliHLTTPCCAGlobalMergerComponent::GetOutputDataSize( unsigned long &constBase, double &inputMultiplier )
94 {
95   // see header file for class documentation
96   // XXX TODO: Find more realistic values.
97   constBase = 0;
98   inputMultiplier = 1.0;
99 }
100
101 AliHLTComponent *AliHLTTPCCAGlobalMergerComponent::Spawn()
102 {
103   // see header file for class documentation
104   return new AliHLTTPCCAGlobalMergerComponent;
105 }
106
107 int AliHLTTPCCAGlobalMergerComponent::DoInit( int argc, const char** argv )
108 {
109   // see header file for class documentation
110   if ( fGlobalMerger ) {
111     return EINPROGRESS;
112   }
113
114   // Initialize the merger
115
116   fGlobalMerger = new AliHLTTPCCAMerger();
117
118   AliHLTTPCCAParam param;
119   
120   {
121     // get gemetry
122     Int_t iSec = 0;
123     Float_t inRmin = 83.65; 
124     Float_t outRmax = 247.7;
125     Float_t plusZmin = 0.0529937; 
126     Float_t plusZmax = 249.778; 
127     Float_t minusZmin = -249.645; 
128     Float_t minusZmax = -0.0799937; 
129     Float_t dalpha = 0.349066;
130     Float_t alpha = 0.174533 + dalpha*iSec;    
131     Bool_t zPlus = (iSec<18 );
132     Float_t zMin =  zPlus ?plusZmin :minusZmin;
133     Float_t zMax =  zPlus ?plusZmax :minusZmax;
134     Int_t nRows = AliHLTTPCTransform::GetNRows();        
135     Float_t padPitch = 0.4;
136     Float_t sigmaZ = 0.228808;    
137     Float_t *rowX = new Float_t [nRows];
138     for( Int_t irow=0; irow<nRows; irow++){
139       rowX[irow] = AliHLTTPCTransform::Row2X( irow );
140     }
141      
142     param.Initialize( iSec, nRows, rowX, alpha, dalpha,
143                       inRmin, outRmax, zMin, zMax, padPitch, sigmaZ, fSolenoidBz );    
144     delete[] rowX;
145   }
146
147
148   fGlobalMerger->SetSliceParam( param );
149
150   Int_t iResult = 0;
151
152   TString arguments=""; 
153   for (int i=0; i<argc; i++) {
154     TString argument=argv[i];
155     if (!arguments.IsNull()) arguments+=" ";
156     arguments+=argument;
157   }
158   if (!arguments.IsNull()) {
159     iResult=Configure(arguments.Data());
160   } else {
161     iResult=Reconfigure(NULL, NULL);
162   }  
163   return iResult;
164 }
165
166 int AliHLTTPCCAGlobalMergerComponent::DoDeinit()
167 {
168   // see header file for class documentation
169   delete fGlobalMerger;
170   fGlobalMerger = 0;
171
172   return 0;
173 }
174
175 int AliHLTTPCCAGlobalMergerComponent::DoEvent( const AliHLTComponentEventData &evtData,
176                                                const AliHLTComponentBlockData *blocks, AliHLTComponentTriggerData &/*trigData*/,
177     AliHLTUInt8_t *outputPtr, AliHLTUInt32_t &size, AliHLTComponentBlockDataList &outputBlocks )
178 {
179   // see header file for class documentation
180   int iResult = 0;
181   UInt_t maxBufferSize = size;
182
183   size = 0;
184
185   if ( !outputPtr ) {
186       return -ENOSPC;
187   }
188   if ( !IsDataEvent() ) {
189     return 0;
190   }
191
192   fGlobalMerger->Clear();
193
194   const AliHLTComponentBlockData *const blocksEnd = blocks + evtData.fBlockCnt;
195   for ( const AliHLTComponentBlockData *block = blocks; block < blocksEnd; ++block ) {
196     if ( block->fDataType != AliHLTTPCCADefinitions::fgkTrackletsDataType ) {
197       continue;
198     }
199
200     int slice = AliHLTTPCDefinitions::GetMinSliceNr( *block );
201     if ( slice < 0 || slice >= AliHLTTPCTransform::GetNSlice() ) {
202       HLTError( "invalid slice number %d extracted from specification 0x%08lx,  skipping block of type %s",
203           slice, block->fSpecification, DataType2Text(block->fDataType).c_str() );
204       // just remember the error, if there are other valid blocks ignore the error, return code otherwise
205       iResult = -EBADF;
206       continue;
207     }
208
209     if ( slice != AliHLTTPCDefinitions::GetMaxSliceNr( *block ) ) {
210       // the code was not written for/ never used with multiple slices in one data block/ specification
211       HLTWarning("specification 0x%08lx indicates multiple slices in data block %s: never used before, please audit the code",
212           block->fSpecification, DataType2Text(block->fDataType).c_str());
213     }
214     AliHLTTPCCASliceOutput *sliceOut =  reinterpret_cast<AliHLTTPCCASliceOutput *>( block->fPtr );
215     sliceOut->SetPointers();
216     fGlobalMerger->SetSliceData( slice, sliceOut );
217   }
218   fGlobalMerger->Reconstruct();
219
220   const AliHLTTPCCAMergerOutput *mergerOutput = fGlobalMerger->Output();
221
222
223   // Fill output tracks
224
225   UInt_t mySize = 0;
226
227   {
228     // check if there was enough space in the output buffer
229
230     Int_t nTracks = mergerOutput->NTracks();
231
232     AliHLTTPCTrackArray array( nTracks );
233
234     Int_t nClusters=0;
235     for( Int_t itr=0; itr<nTracks; itr++){
236  
237       // convert AliHLTTPCCAMergedTrack to AliHLTTPCTrack
238
239       const AliHLTTPCCAMergedTrack &track = mergerOutput->Track( itr );
240       AliHLTTPCTrack out;
241
242       // first convert to AliExternalTrackParam ( Kappa to Pt )
243
244       AliExternalTrackParam tp, tpEnd;
245       AliHLTTPCCATrackConvertor::GetExtParam( track.InnerParam(), tp, 0 );      
246       AliHLTTPCCATrackConvertor::GetExtParam( track.OuterParam(), tpEnd, 0 );      
247
248       // set parameters, with rotation to global coordinates
249       
250       out.SetCharge((Int_t ) tp.GetSign());
251       out.SetPt( TMath::Abs(tp.GetSignedPt()) );
252       out.SetPsi( fmod( TMath::ASin( tp.GetSnp() ) + track.InnerAlpha() ,2*TMath::Pi() ) );
253       out.SetTgl( tp.GetTgl() );
254       {
255         Float_t sinA = TMath::Sin( track.InnerAlpha() );
256         Float_t cosA = TMath::Cos( track.InnerAlpha() );
257         
258         out.SetFirstPoint( tp.GetX()*cosA - tp.GetY()*sinA,
259                            tp.GetX()*sinA + tp.GetY()*cosA, 
260                            tp.GetZ() );
261       }
262       
263       {
264         Float_t sinA = TMath::Sin( track.OuterAlpha() );
265         Float_t cosA = TMath::Cos( track.OuterAlpha() );
266         
267         out.SetLastPoint( tpEnd.GetX()*cosA - tpEnd.GetY()*sinA,
268                           tpEnd.GetX()*sinA + tpEnd.GetY()*cosA, 
269                           tpEnd.GetZ() );
270       }
271
272       // set parameter errors w/o rotation, as it is done in AliHLTTPCTrackArray
273
274       out.SetY0err(tp.GetSigmaY2());
275       out.SetZ0err(tp.GetSigmaZ2());
276       Float_t h = -out.GetPt()*out.GetPt();
277       out.SetPterr( h*h*tp.GetSigma1Pt2() );
278       h = 1./TMath::Sqrt(1-out.GetSnp()*out.GetSnp());
279       out.SetPsierr(h*h*tp.GetSigmaSnp2());
280       out.SetTglerr(tp.GetSigmaTgl2());  
281  
282       // set cluster ID's
283
284       UInt_t hitID[1000];
285       for( Int_t i=0; i<track.NClusters(); i++ ) hitID[i] = mergerOutput->ClusterIDsrc(track.FirstClusterRef()+i);
286
287       out.SetNHits( track.NClusters() );
288       out.SetHits( track.NClusters(), hitID );
289       
290       out.SetSector(-1);
291       out.CalculateHelix();
292       if( !out.CheckConsistency() )  *(array.NextTrack()) = out;      
293       nClusters+=track.NClusters();
294     }
295
296     
297     if ( sizeof(AliHLTTPCTrackletData) + nTracks*sizeof(AliHLTTPCTrackSegmentData) + nClusters*sizeof(UInt_t)
298          > maxBufferSize ){
299       iResult = -ENOSPC;
300     } else {
301       AliHLTTPCTrackletData *outPtr = (AliHLTTPCTrackletData*)(outputPtr);
302       UInt_t nOutTracks = 0;
303       mySize = array.WriteTracks( nOutTracks, outPtr->fTracklets );
304       mySize += sizeof(AliHLTTPCTrackletData);
305       outPtr->fTrackletCnt = nOutTracks;
306     }
307   }
308   
309   AliHLTComponentBlockData resultData;
310   FillBlockData( resultData );
311   resultData.fOffset = 0;
312   resultData.fSize = mySize;
313   resultData.fSpecification = AliHLTTPCDefinitions::EncodeDataSpecification( 0, 35, 0, 5 );
314   outputBlocks.push_back( resultData );
315   size = resultData.fSize;
316
317   //HLTWarning("CAGlobalMerger:: output %d tracks",mergerOutput->NTracks());
318
319   return iResult;
320 }
321
322
323
324 Int_t AliHLTTPCCAGlobalMergerComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/)
325 {
326   // see header file for class documentation
327
328
329   HLTWarning("TODO: dummy Reconfigure() method" );
330   return 0;
331   /*
332
333   Int_t iResult=0;
334   const char* pathBField=kAliHLTCDBSolenoidBz;
335   
336   if (pathBField) {
337     HLTInfo("reconfigure B-Field from entry %s, chain id %s", pathBField,(chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
338     AliCDBEntry *pEntry = AliCDBManager::Instance()->Get(pathBField);//,GetRunNo());
339     if (pEntry) {
340       TObjString* pString=dynamic_cast<TObjString*>(pEntry->GetObject());
341       if (pString) {
342         HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
343         iResult=Configure(pString->GetString().Data());
344       } else {
345         HLTError("configuration object \"%s\" has wrong type, required TObjString", pathBField);
346       }
347     } else {
348       HLTError("cannot fetch object \"%s\" from CDB", pathBField);
349     }
350   }  
351   return iResult;  
352 */
353 }
354
355
356 Int_t AliHLTTPCCAGlobalMergerComponent::Configure( const char* arguments )
357 {
358   //* Set parameters
359
360   Int_t iResult=0;
361   if (!arguments) return iResult;
362   
363   TString allArgs=arguments;
364   TString argument;
365   Int_t bMissingParam=0;
366   
367   TObjArray* pTokens=allArgs.Tokenize(" ");
368
369   Int_t nArgs =  pTokens ?pTokens->GetEntries() :0;
370
371   for (int i=0; i<nArgs; i++ ){
372     argument=((TObjString*)pTokens->At(i))->GetString();
373     if (argument.IsNull()){
374     }
375     else if (argument.CompareTo("-solenoidBz")==0 ){
376       if ((bMissingParam=(++i>=pTokens->GetEntries()))) break;  
377       fSolenoidBz = ((TObjString*)pTokens->At(i))->GetString().Atof();
378       HLTInfo("Magnetic Field set to: %f", fSolenoidBz );
379     }
380     else {
381       HLTError("Unknown option %s ", argument.Data());
382       iResult=-EINVAL;
383     }
384   }
385   delete pTokens;
386
387   if (bMissingParam) {
388     HLTError("Specifier missed for %s", argument.Data());    
389     iResult=-EINVAL;
390   }
391
392   return iResult;
393 }
394