]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCTrackMCMarkerComponent.cxx
coverity warnings 15388 10083 10082 fixed
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCTrackMCMarkerComponent.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 //                  for The ALICE HLT Project.                              *
7 //                                                                          *
8 // Permission to use, copy, modify and distribute this software and its     *
9 // documentation strictly for non-commercial purposes is hereby granted     *
10 // without fee, provided that the above copyright notice appears in all     *
11 // copies and that both the copyright notice and this permission notice     *
12 // appear in the supporting documentation. The authors make no claims       *
13 // about the suitability of this software for any purpose. It is            *
14 // provided "as is" without express or implied warranty.                    *
15 //                                                                          *
16 //***************************************************************************
17
18
19 #if __GNUC__>= 3
20 using namespace std;
21 #endif
22
23 #include "AliHLTTPCTrackMCMarkerComponent.h"
24 #include "AliHLTTPCTransform.h"
25 #include "AliHLTTPCTrackSegmentData.h"
26 #include "AliHLTTPCTrack.h"
27 #include "AliHLTTPCTrackArray.h"
28 #include "AliHLTTPCTrackletDataFormat.h"
29 #include "AliHLTTPCCADef.h"
30 #include "AliHLTTPCDefinitions.h"
31 #include "AliHLTTPCSpacePointData.h"
32
33 #include "AliCDBEntry.h"
34 #include "AliCDBManager.h"
35 #include "TObjString.h"
36 #include "TObjArray.h"
37 #include "AliHLTExternalTrackParam.h"
38 #include "AliHLTTrackMCLabel.h"
39 #include "AliHLTTPCClusterFinder.h"
40 #include <climits>
41 #include <cstdlib>
42 #include <cerrno>
43
44
45 // ROOT macro for the implementation of ROOT specific class methods
46 ClassImp( AliHLTTPCTrackMCMarkerComponent )
47
48
49 AliHLTTPCTrackMCMarkerComponent::AliHLTTPCTrackMCMarkerComponent()
50 {
51   // see header file for class documentation
52 }
53
54 // Public functions to implement AliHLTComponent's interface.
55 // These functions are required for the registration process
56
57 const char *AliHLTTPCTrackMCMarkerComponent::GetComponentID()
58 {
59   // see header file for class documentation
60   return "TPCTrackMCMarker";
61 }
62
63 void AliHLTTPCTrackMCMarkerComponent::GetInputDataTypes( AliHLTComponentDataTypeList &list )
64 {
65   // see header file for class documentation
66   list.clear();
67   list.push_back( kAliHLTDataTypeTrack|kAliHLTDataOriginTPC );
68   list.push_back(AliHLTTPCDefinitions::fgkAliHLTDataTypeClusterMCInfo);
69 }
70
71 AliHLTComponentDataType AliHLTTPCTrackMCMarkerComponent::GetOutputDataType()
72 {
73   // see header file for class documentation
74   return kAliHLTDataTypeTrackMC|kAliHLTDataOriginTPC;
75 }
76
77 void AliHLTTPCTrackMCMarkerComponent::GetOutputDataSize( unsigned long &constBase, double &inputMultiplier )
78 {
79   // see header file for class documentation
80   constBase = 0;
81   inputMultiplier = 1.0;
82 }
83
84 AliHLTComponent *AliHLTTPCTrackMCMarkerComponent::Spawn()
85 {
86   // see header file for class documentation
87   return new AliHLTTPCTrackMCMarkerComponent;
88 }
89
90
91
92
93 void AliHLTTPCTrackMCMarkerComponent::SetDefaultConfiguration()
94 {
95   // Set default configuration for the CA merger component
96   // Some parameters can be later overwritten from the OCDB
97 }
98
99 int AliHLTTPCTrackMCMarkerComponent::ReadConfigurationString(  const char* arguments )
100 {
101   // Set configuration parameters for the CA merger component from the string
102
103   int iResult = 0;
104   if ( !arguments ) return iResult;
105
106   TString allArgs = arguments;
107   TString argument;
108   int bMissingParam = 0;
109
110   TObjArray* pTokens = allArgs.Tokenize( " " );
111   
112   int nArgs =  pTokens ? pTokens->GetEntries() : 0;
113   
114   for ( int i = 0; i < nArgs; i++ ) {
115     argument = ( ( TObjString* )pTokens->At( i ) )->GetString();
116     if ( argument.IsNull() ) continue;
117
118     HLTError( "Unknown option \"%s\"", argument.Data() );
119     iResult = -EINVAL;
120   }
121   delete pTokens;
122
123   if ( bMissingParam ) {
124     HLTError( "Specifier missed for parameter \"%s\"", argument.Data() );
125     iResult = -EINVAL;
126   }
127
128   return iResult;
129 }
130
131
132 int AliHLTTPCTrackMCMarkerComponent::ReadCDBEntry( const char* /*cdbEntry*/, const char* /*chainId*/ )
133 {
134   // see header file for class documentation
135
136   // no settings for a moment, no CDB path, threfore return
137
138   return 0;
139 /*
140   const char* defaultNotify = "";
141
142   if ( !cdbEntry ) {
143     cdbEntry = "HLT/ConfigTPC/TPCTrackMCMarker";
144     defaultNotify = " (default)";
145     chainId = 0;
146   }
147
148   HLTInfo( "configure from entry \"%s\"%s, chain id %s", cdbEntry, defaultNotify, ( chainId != NULL && chainId[0] != 0 ) ? chainId : "<none>" );
149   AliCDBEntry *pEntry = AliCDBManager::Instance()->Get( cdbEntry );//,GetRunNo());
150
151   if ( !pEntry ) {
152     HLTError( "cannot fetch object \"%s\" from CDB", cdbEntry );
153     return -EINVAL;
154   }
155
156   TObjString* pString = dynamic_cast<TObjString*>( pEntry->GetObject() );
157
158   if ( !pString ) {
159     HLTError( "configuration object \"%s\" has wrong type, required TObjString", cdbEntry );
160     return -EINVAL;
161   }
162
163   HLTInfo( "received configuration object string: \"%s\"", pString->GetString().Data() );
164
165   return  ReadConfigurationString( pString->GetString().Data() );
166 */
167 }
168
169
170
171 int AliHLTTPCTrackMCMarkerComponent::Configure( const char* cdbEntry, const char* chainId, const char *commandLine )
172 {
173   // Configure the component
174   // There are few levels of configuration,
175   // parameters which are set on one step can be overwritten on the next step
176
177   //* read hard-coded values
178
179   SetDefaultConfiguration();
180
181   //* read the default CDB entry
182
183   int iResult1 = ReadCDBEntry( NULL, chainId );
184
185   //* read the actual CDB entry if required
186
187   int iResult2 = ( cdbEntry ) ? ReadCDBEntry( cdbEntry, chainId ) : 0;
188
189   //* read extra parameters from input (if they are)
190
191   int iResult3 = 0;
192
193   if ( commandLine && commandLine[0] != '\0' ) {
194     HLTInfo( "received configuration string from HLT framework: \"%s\"", commandLine );
195     iResult3 = ReadConfigurationString( commandLine );
196   }
197
198   return iResult1 ? iResult1 : ( iResult2 ? iResult2 : iResult3 );
199 }
200
201
202
203
204 int AliHLTTPCTrackMCMarkerComponent::DoInit( int argc, const char** argv )
205 {
206   // see header file for class documentation
207
208   TString arguments = "";
209   for ( int i = 0; i < argc; i++ ) {
210     if ( !arguments.IsNull() ) arguments += " ";
211     arguments += argv[i];
212   }
213
214   return Configure( NULL, NULL, arguments.Data()  );
215 }
216
217
218 int AliHLTTPCTrackMCMarkerComponent::Reconfigure( const char* cdbEntry, const char* chainId )
219 {
220   // Reconfigure the component from OCDB
221
222   return Configure( cdbEntry, chainId, NULL );
223 }
224
225
226
227 int AliHLTTPCTrackMCMarkerComponent::DoDeinit()
228 {
229   // see header file for class documentation
230
231   return 0;
232 }
233
234 Int_t AliHLTTPCTrackMCMarkerComponent::GetTrackMCLabel( unsigned int *hits, int nHits )
235 {
236   // get MC label for the track  
237
238   Int_t mcLabel = -1;
239             
240   std::vector<int> labels;
241
242   for( Int_t ih=0; ih<nHits; ih++){
243     UInt_t id = hits[ih];
244     int iSlice = AliHLTTPCSpacePointData::GetSlice(id);
245     int iPatch = AliHLTTPCSpacePointData::GetPatch(id);
246     int iCluster = AliHLTTPCSpacePointData::GetNumber(id);
247     if( iSlice<0 || iSlice>36 || iPatch<0 || iPatch>5 ){
248       HLTError("Corrupted TPC cluster Id: slice %d, patch %d, cluster %d",
249                iSlice, iPatch,iCluster );
250       continue;
251     }
252     AliHLTTPCClusterFinder::ClusterMCInfo *patchLabels = fClusterLabels[iSlice*6 + iPatch];
253     if( !patchLabels ) continue;
254     if( iCluster >= fNClusterLabels[iSlice*6 + iPatch] ){
255       HLTError("TPC slice %d, patch %d: ClusterID==%d >= N MC labels==%d ",
256                iSlice, iPatch,iCluster, fNClusterLabels[iSlice*6 + iPatch] );
257       continue;
258     }
259     AliHLTTPCClusterFinder::ClusterMCInfo &lab = patchLabels[iCluster];     
260     if ( lab.fClusterID[0].fMCID >= 0 ) labels.push_back( lab.fClusterID[0].fMCID );
261     if ( lab.fClusterID[1].fMCID >= 0 ) labels.push_back( lab.fClusterID[1].fMCID );
262     if ( lab.fClusterID[2].fMCID >= 0 ) labels.push_back( lab.fClusterID[2].fMCID );
263   }
264           
265   std::sort( labels.begin(), labels.end() );
266           
267   labels.push_back( -1 ); // put -1 to the end
268           
269   int labelMax = -1, labelCur = -1, nLabelsMax = 0, nLabelsCurr = 0;
270   for ( unsigned int iLab = 0; iLab < labels.size(); iLab++ ) {
271     if ( labels[iLab] != labelCur ) {         
272       if ( labelCur >= 0 && nLabelsMax< nLabelsCurr ) {
273         nLabelsMax = nLabelsCurr;
274         labelMax = labelCur;
275       }
276       labelCur = labels[iLab];
277       nLabelsCurr = 0;
278     }
279     nLabelsCurr++;
280   }
281   
282   if( labelMax>=0 && nLabelsMax < 0.9 * nHits ) labelMax = -labelMax;
283   
284   mcLabel = labelMax;
285
286   return mcLabel;
287 }
288
289
290 int AliHLTTPCTrackMCMarkerComponent::DoEvent( const AliHLTComponentEventData &evtData,
291     const AliHLTComponentBlockData *blocks, AliHLTComponentTriggerData &/*trigData*/,
292     AliHLTUInt8_t *outputPtr, AliHLTUInt32_t &size, AliHLTComponentBlockDataList &outputBlocks )
293 {
294   // see header file for class documentation
295
296   int iResult = 0;
297   unsigned int maxBufferSize = size;
298
299   size = 0;
300
301   if ( !outputPtr ) {
302     return -ENOSPC;
303   }
304   if ( !IsDataEvent() ) {
305     return 0;
306   }
307
308   for( int i=0; i<36*6; i++ ){
309     fClusterLabels[i] = 0;
310     fNClusterLabels[i] = 0;
311   }
312
313   int nBlocks = (int)evtData.fBlockCnt;
314
315   int nInputMCLabels = 0;
316   int nInputTracks = 0;
317
318   // first read all the MC information
319   for (int ndx=0; ndx<nBlocks && iResult>=0; ndx++) {
320     const AliHLTComponentBlockData* iter = blocks+ndx;
321     if(iter->fDataType == AliHLTTPCDefinitions::fgkAliHLTDataTypeClusterMCInfo ) {
322       Int_t slice=AliHLTTPCDefinitions::GetMinSliceNr(iter->fSpecification);
323       Int_t patch=AliHLTTPCDefinitions::GetMinPatchNr(iter->fSpecification);
324       fClusterLabels[ slice*6 + patch] = (AliHLTTPCClusterFinder::ClusterMCInfo *)iter->fPtr;
325       fNClusterLabels[ slice*6 + patch] = iter->fSize/sizeof(AliHLTTPCClusterFinder::ClusterMCInfo);
326       nInputMCLabels+=fNClusterLabels[ slice*6 + patch];
327     }
328   }
329       
330   // read tracks and write output
331
332   
333   unsigned int mySize = 0;
334   
335   AliHLTTrackMCData* outPtr = ( AliHLTTrackMCData* )( outputPtr );
336   AliHLTTrackMCLabel* currOutLabel = outPtr->fLabels;
337
338   mySize =   ( ( AliHLTUInt8_t * )currOutLabel ) -  ( ( AliHLTUInt8_t * )outPtr );
339
340   outPtr->fCount = 0;
341
342   for (int ndx=0; ndx<nBlocks && iResult>=0; ndx++) {
343     const AliHLTComponentBlockData* iter = blocks+ndx;
344         
345     if( iter->fDataType == ( kAliHLTDataTypeTrack|kAliHLTDataOriginTPC ) ){             
346       AliHLTTracksData* dataPtr = ( AliHLTTracksData* ) iter->fPtr;
347       int nTracks = dataPtr->fCount;
348       AliHLTExternalTrackParam* currTrack = dataPtr->fTracklets;
349       for( int itr=0; itr<nTracks; itr++ ){
350         nInputTracks++;
351         Int_t mcLabel = GetTrackMCLabel( currTrack->fPointIDs, currTrack->fNPoints );  
352
353         currOutLabel->fTrackID = currTrack->fTrackID;
354         currOutLabel->fMCLabel = mcLabel;
355
356         if ( mySize + sizeof(AliHLTTrackMCLabel) > maxBufferSize ) {
357           HLTWarning( "Output buffer size exceed (buffer size %d, current size %d), %d mc labels are not stored", maxBufferSize, mySize, nTracks - itr + 1 );
358           iResult = -ENOSPC;
359           break;
360         }
361         mySize += sizeof(AliHLTTrackMCLabel);
362         currOutLabel++;
363         outPtr->fCount++;
364
365         unsigned int dSize = sizeof( AliHLTExternalTrackParam ) + currTrack->fNPoints * sizeof( unsigned int );
366         currTrack = ( AliHLTExternalTrackParam* )( (( Byte_t * )currTrack) + dSize );
367       }
368     }
369   }
370   
371
372   AliHLTComponentBlockData resultData;
373   FillBlockData( resultData );
374   resultData.fOffset = 0;
375   resultData.fSize = mySize;
376   resultData.fDataType = kAliHLTDataTypeTrackMC|kAliHLTDataOriginTPC;
377   resultData.fSpecification = AliHLTTPCDefinitions::EncodeDataSpecification( 0, 35, 0, 5 );
378   outputBlocks.push_back( resultData );
379   size = resultData.fSize;
380
381   HLTInfo( "TrackMCMarker:: input %d labels, %d tracks, output %d labels", nInputMCLabels, nInputTracks,outPtr->fCount );
382
383   return iResult;
384 }
385