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