]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCHWClusterDecoderComponent.cxx
- HWClusterDecoder component converts HW clusters to AliHLTTPCRawCluster format and...
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCHWClusterDecoderComponent.cxx
1 // $Id: AliHLTTPCHWClusterDecoderComponent.cxx 59611 2012-11-15 16:23:28Z sgorbuno $
2
3 //***************************************************************************
4 //* This file is property of and copyright by the ALICE HLT Project         *
5 //* ALICE Experiment at CERN, All rights reserved.                          *
6 //*                                                                         *
7 //* Primary Authors: Sergey Gorbunov <sergey.gorbunov@fias.uni-frankfurt.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 /** @file   AliHLTTPCHWClusterDecoderComponent.cxx
21     @author Sergey Gorbunov
22     @date   
23     @brief 
24 */
25
26 #include "AliHLTTPCHWClusterDecoderComponent.h"
27 #include "AliHLTTPCHWClusterMergerV1.h"
28 #include "AliHLTTPCTransform.h"
29 #include "AliHLTTPCDefinitions.h"
30 #include "AliHLTTPCSpacePointData.h"
31 #include "AliHLTTPCClusterDataFormat.h"
32 #include "AliRawDataHeader.h"
33 #include "AliHLTTPCRawCluster.h"
34 #include "AliHLTTPCHWCFEmulator.h"
35 #include "AliHLTTPCHWCFData.h"
36 #include "AliHLTErrorGuard.h"
37
38 #include "AliCDBManager.h"
39 #include "AliCDBEntry.h"
40
41 #include "TMath.h"
42 #include "TObjString.h" 
43 #include <cstdlib>
44 #include <cerrno>
45 #include <sys/time.h>
46
47 using namespace std;
48
49 ClassImp(AliHLTTPCHWClusterDecoderComponent) //ROOT macro for the implementation of ROOT specific class methods
50
51 const char* AliHLTTPCHWClusterDecoderComponent::fgkOCDBEntry="HLT/ConfigTPC/TPCHWClusterDecoder";
52
53
54 AliHLTTPCHWClusterDecoderComponent::AliHLTTPCHWClusterDecoderComponent()
55 :
56 fpDecoder(NULL),
57 fpClusterMerger(NULL),
58 fDoMerge(1),
59 fBenchmark("HWClusterDecoder")
60 {
61   // see header file for class documentation
62   // or
63   // refer to README to build package
64   // or
65   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt  
66
67   fBenchmark.Reset();
68   fBenchmark.SetTimer(0,"total");
69 }
70
71 AliHLTTPCHWClusterDecoderComponent::~AliHLTTPCHWClusterDecoderComponent()
72
73   // destructor
74   delete fpDecoder;
75   delete fpClusterMerger;
76 }
77
78 const char* AliHLTTPCHWClusterDecoderComponent::GetComponentID() 
79
80   // see header file for class documentation
81   return "TPCHWClusterDecoder";
82 }
83
84
85
86 void AliHLTTPCHWClusterDecoderComponent::GetInputDataTypes( vector<AliHLTComponentDataType>& list) { 
87   // see header file for class documentation
88
89   list.clear(); 
90   list.push_back( AliHLTTPCDefinitions::fgkHWClustersDataType | kAliHLTDataOriginTPC  );
91   list.push_back( AliHLTTPCDefinitions::fgkAliHLTDataTypeClusterMCInfo | kAliHLTDataOriginTPC );
92 }
93
94 AliHLTComponentDataType AliHLTTPCHWClusterDecoderComponent::GetOutputDataType() { 
95   // see header file for class documentation
96
97   return kAliHLTMultipleDataType;
98 }
99
100 int AliHLTTPCHWClusterDecoderComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& tgtList) { 
101   // see header file for class documentation
102
103   tgtList.clear();
104   tgtList.push_back( AliHLTTPCDefinitions::fgkRawClustersDataType  | kAliHLTDataOriginTPC );
105   tgtList.push_back( AliHLTTPCDefinitions::fgkAliHLTDataTypeClusterMCInfo | kAliHLTDataOriginTPC );
106   return tgtList.size();
107 }
108
109 void AliHLTTPCHWClusterDecoderComponent::GetOutputDataSize( unsigned long& constBase, double& inputMultiplier ) { 
110   // see header file for class documentation
111   constBase = 0;
112   inputMultiplier = 2.0;
113 }
114
115 AliHLTComponent* AliHLTTPCHWClusterDecoderComponent::Spawn() { 
116   // see header file for class documentation
117
118   return new AliHLTTPCHWClusterDecoderComponent();
119 }
120         
121 int AliHLTTPCHWClusterDecoderComponent::DoInit( int argc, const char** argv ) 
122
123   // see header file for class documentation
124   
125   int iResult=0;
126
127   fpDecoder=new AliHLTTPCHWCFData;
128   if (!fpDecoder) iResult=-ENOMEM;
129   
130   fDoMerge = 1;
131
132   if (iResult>=0) iResult = ConfigureFromCDBTObjString(fgkOCDBEntry);
133
134   if (iResult>=0 && argc>0) iResult = ConfigureFromArgumentString(argc, argv);
135
136   if ( iResult>=0 ) iResult = InitClusterMerger();  
137   
138   return iResult;
139 } // end DoInit()
140
141 int AliHLTTPCHWClusterDecoderComponent::DoDeinit() { 
142   // see header file for class documentation   
143   if (!fpDecoder) delete fpDecoder;
144   fpDecoder=NULL;
145   delete fpClusterMerger;
146   fpClusterMerger = NULL;
147   return 0;
148 }
149
150 int AliHLTTPCHWClusterDecoderComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/) { 
151   // see header file for class documentation
152
153   fDoMerge = 1;
154   int iResult = ConfigureFromCDBTObjString(fgkOCDBEntry);
155   if ( iResult>=0 ) iResult = InitClusterMerger();  
156   return iResult;
157 }
158
159 int AliHLTTPCHWClusterDecoderComponent::ScanConfigurationArgument(int argc, const char** argv){
160
161   // see header file for class documentation
162
163   if (argc<=0) return 0;
164   int i=0;
165   TString argument=argv[i];
166
167   if (argument.CompareTo("-do-merge")==0){
168     fDoMerge = 1;
169     return 1;
170   }
171
172   if (argument.CompareTo("-do-not-merge")==0){
173     fDoMerge = 0;
174     return 1;
175   }
176
177   // unknown argument
178   return -EINVAL;
179 }
180
181 int AliHLTTPCHWClusterDecoderComponent::InitClusterMerger()
182 {
183   //
184   // init merger
185   //
186   int iResult = 0;
187   if ( fDoMerge && !fpClusterMerger) {
188     fpClusterMerger = new AliHLTTPCHWClusterMergerV1;
189     if( !fpClusterMerger ) iResult=-ENOMEM;
190     else iResult = fpClusterMerger->Init();
191     if( iResult<0 ){
192       HLTError("Can not initialise cluster merger");
193       delete fpClusterMerger;
194       fpClusterMerger = 0;
195     }
196   }
197   return iResult;
198 }
199
200 void AliHLTTPCHWClusterDecoderComponent::GetOCDBObjectDescription( TMap* const targetMap)
201 {
202   // Get a list of OCDB object description needed for the particular component
203   if (!targetMap) return;
204   
205   // OCDB entries for component arguments
206
207   targetMap->Add(new TObjString(fgkOCDBEntry), new TObjString("component argument for HW cluster decoder"));  
208 }
209
210
211 int AliHLTTPCHWClusterDecoderComponent::DoEvent(const AliHLTComponentEventData& evtData, 
212                                                   const AliHLTComponentBlockData* blocks, 
213                                                   AliHLTComponentTriggerData& /*trigData*/, AliHLTUInt8_t* outputPtr, 
214                                                   AliHLTUInt32_t& size, 
215                                                   vector<AliHLTComponentBlockData>& outputBlocks ){
216   // see header file for class documentation
217  
218   UInt_t maxOutSize = size;
219   size = 0;
220   int iResult = 0;
221   if(!IsDataEvent()) return 0;
222
223   if (!fpDecoder) return -ENODEV;
224
225   fBenchmark.StartNewEvent();
226   fBenchmark.Start(0);
227   
228   UInt_t outputBlocksSize = outputBlocks.size();
229
230   for( unsigned long ndx=0; ndx<evtData.fBlockCnt; ndx++ ){
231      
232     const AliHLTComponentBlockData *iter   = blocks+ndx;
233     
234     fBenchmark.AddInput(iter->fSize);
235     
236     HLTDebug("Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
237              evtData.fEventID, evtData.fEventID, 
238              DataType2Text( iter->fDataType).c_str(), 
239              DataType2Text(AliHLTTPCDefinitions::fgkHWClustersDataType).c_str());                       
240  
241     if(iter->fDataType == (AliHLTTPCDefinitions::fgkAliHLTDataTypeClusterMCInfo | kAliHLTDataOriginTPC) ){
242       // simply forward MC labels
243       
244       if( size+iter->fSize > maxOutSize ){
245         HLTWarning( "Output buffer (%db) is too small, required %db", maxOutSize, size+iter->fSize);
246         iResult  = -ENOSPC;
247         break;
248       }
249
250       memcpy( outputPtr, iter->fPtr, iter->fSize );
251       
252       AliHLTComponentBlockData bd;
253       FillBlockData( bd );
254       bd.fOffset = size;
255       bd.fSize = iter->fSize;
256       bd.fPtr = outputPtr;
257       bd.fSpecification = iter->fSpecification;     
258       bd.fDataType = iter->fDataType;
259       outputBlocks.push_back( bd );           
260       size   += bd.fSize;
261       outputPtr += bd.fSize;
262       fBenchmark.AddOutput(bd.fSize);    
263       continue;
264     }
265
266     if( iter->fDataType == (AliHLTTPCDefinitions::fgkHWClustersDataType | kAliHLTDataOriginTPC) ){
267         
268       UInt_t minSlice     = AliHLTTPCDefinitions::GetMinSliceNr(*iter); 
269       UInt_t minPartition = AliHLTTPCDefinitions::GetMinPatchNr(*iter);
270       
271       fBenchmark.SetName(Form("HWClusterTransform slice %d patch %d",minSlice,minPartition));
272       HLTDebug("minSlice: %d, minPartition: %d", minSlice, minPartition);     
273       
274       long maxRawClusters = ((long)maxOutSize-size-sizeof(AliHLTTPCRawClusterData))/sizeof(AliHLTTPCRawCluster);
275        
276       if( maxRawClusters<=0 ) {
277         HLTWarning("No more space to add raw clusters, exiting!");
278         iResult  = -ENOSPC;
279         continue;
280       }       
281
282       // copy raw cluster data from input
283          
284       AliHLTTPCRawClusterData* outputRaw= (AliHLTTPCRawClusterData*)(outputPtr);
285        
286       outputRaw->fVersion = 0;
287       outputRaw->fCount = 0;
288       
289       AliHLTUInt32_t *buffer = (AliHLTUInt32_t*)iter->fPtr;  
290      
291       // skip the first 8 32-bit CDH words
292       buffer += 8;
293       UInt_t bufferSize32 = ((Int_t)iter->fSize - sizeof(AliRawDataHeader) )/sizeof(AliHLTUInt32_t);
294       
295       if (fpDecoder->Init(reinterpret_cast<AliHLTUInt8_t*>(buffer), bufferSize32*sizeof(AliHLTUInt32_t))>=0 && fpDecoder->CheckVersion()>=0) {
296         
297         for (AliHLTTPCHWCFData::iterator cl=fpDecoder->begin(); cl!=fpDecoder->end(); ++cl) {
298              
299           if(outputRaw->fCount>=maxRawClusters){
300             HLTWarning("No more space to add clusters, exiting!");
301             iResult  = -ENOSPC;
302             break;
303           }
304           AliHLTTPCRawCluster &c = outputRaw->fClusters[outputRaw->fCount];       
305           c.SetPadRow(cl.GetPadRow());
306           c.SetCharge(cl.GetCharge());
307           c.SetPad(cl.GetPad());  
308           c.SetTime(cl.GetTime());
309           c.SetSigmaY2(cl.GetSigmaY2());
310           c.SetSigmaZ2(cl.GetSigmaZ2());
311           c.SetQMax(cl.GetQMax());        
312           outputRaw->fCount++;
313         }       
314         
315         // fill into HLT output data
316         AliHLTComponentBlockData bdRawClusters;
317         FillBlockData( bdRawClusters );
318         bdRawClusters.fOffset = size;
319         bdRawClusters.fPtr = outputPtr;
320         bdRawClusters.fSize = sizeof(AliHLTTPCRawClusterData)+outputRaw->fCount*sizeof(AliHLTTPCRawCluster);
321         bdRawClusters.fSpecification = iter->fSpecification;
322         bdRawClusters.fDataType = AliHLTTPCDefinitions::fgkRawClustersDataType | kAliHLTDataOriginTPC;
323         outputBlocks.push_back( bdRawClusters );
324         fBenchmark.AddOutput(bdRawClusters.fSize);
325         size   += bdRawClusters.fSize;
326         outputPtr += bdRawClusters.fSize;
327       }    
328       continue; // HW clusters data block
329     }
330
331   } // end of loop over data blocks  
332
333   if( fDoMerge && fpClusterMerger ){
334     fpClusterMerger->Clear();
335     for( UInt_t i=outputBlocksSize; i<outputBlocks.size(); i++){
336       fpClusterMerger->SetDataBlock(&(outputBlocks[i]));
337     }
338     int nMerged = fpClusterMerger->Merge();
339     fpClusterMerger->Clear();
340     HLTInfo("Merged %d clusters",nMerged);   
341   }
342
343   fBenchmark.Stop(0);
344   HLTInfo(fBenchmark.GetStatistics());
345   
346   return iResult;
347 } // end DoEvent()
348
349
350
351 void AliHLTTPCHWClusterDecoderComponent::PrintDebug(AliHLTUInt32_t *buffer, Int_t size){
352 // see header file for class documentation 
353
354   HLTInfo("The size is: %d", size);
355   for(Int_t n32bit=0; n32bit<size; n32bit++){
356     
357     AliHLTUInt8_t *wordPtr = reinterpret_cast<AliHLTUInt8_t*>(&buffer[n32bit]);
358     //    cout << "word ptr initialized"<<endl;
359     for(Int_t w=3;w>=0;w--){
360       //     cout <<"accessing word"<<endl;
361       AliHLTUInt8_t word = wordPtr[w];
362       //     cout<< "word was accessed"<<endl; 
363       for(int n=7; n>=0; n--){
364         //print the byte values
365         if((((word>>n)<<7)&0x80) != 0){
366           printf("1");
367         }
368         else{
369           printf("0");
370         }
371       }
372       printf("  ");
373     }
374     printf("\n");
375   }
376 } // end of PrintDebug
377