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