]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCHWClusterDecoderComponent.cxx
149583e6a9f9a3f70be25efdc1dc79747ae64116
[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 = 0;
155   //!! iResult = ConfigureFromCDBTObjString(fgkOCDBEntry);
156   if ( iResult>=0 ) iResult = InitClusterMerger();  
157   return iResult;
158 }
159
160 int AliHLTTPCHWClusterDecoderComponent::ScanConfigurationArgument(int argc, const char** argv){
161
162   // see header file for class documentation
163
164   if (argc<=0) return 0;
165   int i=0;
166   TString argument=argv[i];
167
168   if (argument.CompareTo("-do-merge")==0){
169     fDoMerge = 1;
170     return 1;
171   }
172
173   if (argument.CompareTo("-do-not-merge")==0){
174     fDoMerge = 0;
175     return 1;
176   }
177
178   // unknown argument
179   return -EINVAL;
180 }
181
182 int AliHLTTPCHWClusterDecoderComponent::InitClusterMerger()
183 {
184   //
185   // init merger
186   //
187   int iResult = 0;
188   if ( fDoMerge && !fpClusterMerger) {
189     fpClusterMerger = new AliHLTTPCHWClusterMergerV1;
190     if( !fpClusterMerger ) iResult=-ENOMEM;
191     else iResult = fpClusterMerger->Init();
192     if( iResult<0 ){
193       HLTError("Can not initialise cluster merger");
194       delete fpClusterMerger;
195       fpClusterMerger = 0;
196     }
197   }
198   return iResult;
199 }
200
201 void AliHLTTPCHWClusterDecoderComponent::GetOCDBObjectDescription( TMap* const targetMap)
202 {
203   // Get a list of OCDB object description needed for the particular component
204   if (!targetMap) return;
205   
206   // OCDB entries for component arguments
207
208   //!! targetMap->Add(new TObjString(fgkOCDBEntry), new TObjString("component argument for HW cluster decoder"));  
209 }
210
211
212 int AliHLTTPCHWClusterDecoderComponent::DoEvent(const AliHLTComponentEventData& evtData, 
213                                                   const AliHLTComponentBlockData* blocks, 
214                                                   AliHLTComponentTriggerData& /*trigData*/, AliHLTUInt8_t* outputPtr, 
215                                                   AliHLTUInt32_t& size, 
216                                                   vector<AliHLTComponentBlockData>& outputBlocks ){
217   // see header file for class documentation
218  
219   UInt_t maxOutSize = size;
220   size = 0;
221   int iResult = 0;
222   if(!IsDataEvent()) return 0;
223
224   if (!fpDecoder) return -ENODEV;
225
226   fBenchmark.StartNewEvent();
227   fBenchmark.Start(0);
228   
229   AliHLTUInt8_t* origOutputPtr = outputPtr;
230   UInt_t origOutputBlocksSize = outputBlocks.size();
231
232   for( unsigned long ndx=0; ndx<evtData.fBlockCnt; ndx++ ){
233      
234     const AliHLTComponentBlockData *iter   = blocks+ndx;
235     
236     fBenchmark.AddInput(iter->fSize);
237     
238     HLTDebug("Event 0x%08LX (%Lu) received datatype: %s - required datatype: %s",
239              evtData.fEventID, evtData.fEventID, 
240              DataType2Text( iter->fDataType).c_str(), 
241              DataType2Text(AliHLTTPCDefinitions::fgkHWClustersDataType).c_str());                       
242  
243     if(iter->fDataType == (AliHLTTPCDefinitions::fgkAliHLTDataTypeClusterMCInfo | kAliHLTDataOriginTPC) ){
244       // simply forward MC labels
245       
246       if( size+iter->fSize > maxOutSize ){
247         HLTWarning( "Output buffer (%db) is too small, required %db", maxOutSize, size+iter->fSize);
248         iResult  = -ENOSPC;
249         break;
250       }
251
252       memcpy( outputPtr, iter->fPtr, iter->fSize );
253       
254       AliHLTComponentBlockData bd;
255       FillBlockData( bd );
256       bd.fOffset = size;
257       bd.fSize = iter->fSize;      
258       bd.fSpecification = iter->fSpecification;     
259       bd.fDataType = iter->fDataType;
260       outputBlocks.push_back( bd );           
261       size   += bd.fSize;
262       outputPtr += bd.fSize;
263       fBenchmark.AddOutput(bd.fSize);    
264       continue;
265     }
266
267     if( iter->fDataType == (AliHLTTPCDefinitions::fgkHWClustersDataType | kAliHLTDataOriginTPC) ){
268         
269       UInt_t minSlice     = AliHLTTPCDefinitions::GetMinSliceNr(*iter); 
270       UInt_t minPartition = AliHLTTPCDefinitions::GetMinPatchNr(*iter);
271       
272       fBenchmark.SetName(Form("HWClusterTransform slice %d patch %d",minSlice,minPartition));
273       HLTDebug("minSlice: %d, minPartition: %d", minSlice, minPartition);     
274       
275       long maxRawClusters = ((long)maxOutSize-size-sizeof(AliHLTTPCRawClusterData))/sizeof(AliHLTTPCRawCluster);
276        
277       if( maxRawClusters<=0 ) {
278         HLTWarning("No more space to add raw clusters, exiting!");
279         iResult  = -ENOSPC;
280         continue;
281       }       
282
283       // copy raw cluster data from input
284          
285       AliHLTTPCRawClusterData* outputRaw= (AliHLTTPCRawClusterData*)(outputPtr);
286        
287       outputRaw->fVersion = 0;
288       outputRaw->fCount = 0;
289       
290       AliHLTUInt32_t *buffer = (AliHLTUInt32_t*)iter->fPtr;  
291      
292       // skip the first 8 32-bit CDH words
293       buffer += 8;
294       UInt_t bufferSize32 = ((Int_t)iter->fSize - sizeof(AliRawDataHeader) )/sizeof(AliHLTUInt32_t);
295       
296       if (fpDecoder->Init(reinterpret_cast<AliHLTUInt8_t*>(buffer), bufferSize32*sizeof(AliHLTUInt32_t))>=0 && fpDecoder->CheckVersion()>=0) {
297         
298         for (AliHLTTPCHWCFData::iterator cl=fpDecoder->begin(); cl!=fpDecoder->end(); ++cl) {
299              
300           if(outputRaw->fCount>=maxRawClusters){
301             HLTWarning("No more space to add clusters, exiting!");
302             iResult  = -ENOSPC;
303             break;
304           }
305           AliHLTTPCRawCluster &c = outputRaw->fClusters[outputRaw->fCount];       
306           c.SetPadRow(cl.GetPadRow());
307           c.SetCharge(cl.GetCharge());
308           c.SetPad(cl.GetPad());  
309           c.SetTime(cl.GetTime());
310           c.SetSigmaY2(cl.GetSigmaY2());
311           c.SetSigmaZ2(cl.GetSigmaZ2());
312           c.SetQMax(cl.GetQMax());        
313           outputRaw->fCount++;
314         }       
315         
316         // fill into HLT output data
317         AliHLTComponentBlockData bdRawClusters;
318         FillBlockData( bdRawClusters );
319         bdRawClusters.fOffset = size;   
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     fpClusterMerger->SetDataPointer(origOutputPtr);
336     for( UInt_t i=origOutputBlocksSize; i<outputBlocks.size(); i++){
337       fpClusterMerger->SetDataBlock(&(outputBlocks[i]));
338     }
339     int nMerged = fpClusterMerger->Merge();
340     fpClusterMerger->Clear();
341     HLTInfo("Merged %d clusters",nMerged);   
342   }
343  
344   fBenchmark.Stop(0);
345   HLTInfo(fBenchmark.GetStatistics());
346   
347   return iResult;
348 } // end DoEvent()
349
350
351
352 void AliHLTTPCHWClusterDecoderComponent::PrintDebug(AliHLTUInt32_t *buffer, Int_t size){
353 // see header file for class documentation 
354
355   HLTInfo("The size is: %d", size);
356   for(Int_t n32bit=0; n32bit<size; n32bit++){
357     
358     AliHLTUInt8_t *wordPtr = reinterpret_cast<AliHLTUInt8_t*>(&buffer[n32bit]);
359     //    cout << "word ptr initialized"<<endl;
360     for(Int_t w=3;w>=0;w--){
361       //     cout <<"accessing word"<<endl;
362       AliHLTUInt8_t word = wordPtr[w];
363       //     cout<< "word was accessed"<<endl; 
364       for(int n=7; n>=0; n--){
365         //print the byte values
366         if((((word>>n)<<7)&0x80) != 0){
367           printf("1");
368         }
369         else{
370           printf("0");
371         }
372       }
373       printf("  ");
374     }
375     printf("\n");
376   }
377 } // end of PrintDebug
378