New assembly (I12B) in SPD to avoid probles related to volumes originally declared...
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponent.cxx
CommitLineData
f23a6e1a 1// $Id$
2
3/**************************************************************************
9be2600f 4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
f23a6e1a 6 * *
9be2600f 7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
9 * for The ALICE HLT Project. *
f23a6e1a 10 * *
11 * Permission to use, copy, modify and distribute this software and its *
12 * documentation strictly for non-commercial purposes is hereby granted *
13 * without fee, provided that the above copyright notice appears in all *
14 * copies and that both the copyright notice and this permission notice *
15 * appear in the supporting documentation. The authors make no claims *
16 * about the suitability of this software for any purpose. It is *
17 * provided "as is" without express or implied warranty. *
18 **************************************************************************/
19
bfccbf68 20/** @file AliHLTComponent.cxx
21 @author Matthias Richter, Timm Steinbeck
22 @date
23 @brief Base class implementation for HLT components. */
f23a6e1a 24
0c0c9d99 25#if __GNUC__>= 3
f23a6e1a 26using namespace std;
27#endif
28
66043029 29//#include "AliHLTStdIncludes.h"
f23a6e1a 30#include "AliHLTComponent.h"
31#include "AliHLTComponentHandler.h"
a655eae3 32#include "AliHLTMessage.h"
70ed7d01 33#include "TString.h"
a655eae3 34#include "TObjArray.h"
35#include "TClass.h"
90ebac25 36#include "TStopwatch.h"
f23a6e1a 37
b22e91eb 38/** ROOT macro for the implementation of ROOT specific class methods */
90ebac25 39ClassImp(AliHLTComponent);
40
41/** stopwatch macro using the stopwatch guard */
42#define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
43//#define ALIHLTCOMPONENT_STOPWATCH(type)
44
45/** stopwatch macro for operations of the base class */
46#define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
47/** stopwatch macro for operations of the detector algorithm (DA) */
48#define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
f23a6e1a 49
f23a6e1a 50AliHLTComponent::AliHLTComponent()
85869391 51 :
53feaef5 52 fEnvironment(),
3cde846d 53 fCurrentEvent(0),
a655eae3 54 fEventCount(-1),
55 fFailedEvents(0),
56 fCurrentEventData(),
57 fpInputBlocks(NULL),
58 fCurrentInputBlock(-1),
59 fSearchDataType(kAliHLTVoidDataType),
60 fClassName(),
61 fpInputObjects(NULL),
62 fpOutputBuffer(NULL),
63 fOutputBufferSize(0),
64 fOutputBufferFilled(0),
90ebac25 65 fOutputBlocks(),
66 fpStopwatches(new TObjArray(kSWTypeCount))
70ed7d01 67{
68 // see header file for class documentation
69 // or
70 // refer to README to build package
71 // or
72 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
f23a6e1a 73 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
70ed7d01 74 if (fgpComponentHandler)
75 fgpComponentHandler->ScheduleRegister(this);
7c781d33 76 //SetLocalLoggingLevel(kHLTLogDefault);
70ed7d01 77}
78
79AliHLTComponent::AliHLTComponent(const AliHLTComponent&)
80 :
9253e11b 81 AliHLTLogging(),
70ed7d01 82 fEnvironment(),
83 fCurrentEvent(0),
a655eae3 84 fEventCount(-1),
85 fFailedEvents(0),
86 fCurrentEventData(),
87 fpInputBlocks(NULL),
88 fCurrentInputBlock(-1),
89 fSearchDataType(kAliHLTVoidDataType),
90 fClassName(),
91 fpInputObjects(NULL),
92 fpOutputBuffer(NULL),
93 fOutputBufferSize(0),
94 fOutputBufferFilled(0),
90ebac25 95 fOutputBlocks(),
96 fpStopwatches(NULL)
70ed7d01 97{
98 // see header file for class documentation
99 HLTFatal("copy constructor untested");
100}
101
102AliHLTComponent& AliHLTComponent::operator=(const AliHLTComponent&)
103{
104 // see header file for class documentation
105 HLTFatal("assignment operator untested");
106 return *this;
f23a6e1a 107}
108
109AliHLTComponent::~AliHLTComponent()
110{
70ed7d01 111 // see header file for function documentation
8451168b 112 CleanupInputObjects();
4498d7d1 113 if (fpStopwatches!=NULL) delete fpStopwatches;
114 fpStopwatches=NULL;
f23a6e1a 115}
116
70ed7d01 117AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
b22e91eb 118
85869391 119int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
120{
70ed7d01 121 // see header file for function documentation
85869391 122 int iResult=0;
70ed7d01 123 if (fgpComponentHandler==NULL || bOverwrite!=0)
124 fgpComponentHandler=pCH;
85869391 125 else
126 iResult=-EPERM;
127 return iResult;
128}
129
70ed7d01 130int AliHLTComponent::UnsetGlobalComponentHandler()
131{
132 // see header file for function documentation
85869391 133 return SetGlobalComponentHandler(NULL,1);
134}
135
70ed7d01 136int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
f23a6e1a 137{
70ed7d01 138 // see header file for function documentation
f23a6e1a 139 int iResult=0;
140 if (environ) {
141 memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
70ed7d01 142 fEnvironment.fParam=environParam;
f23a6e1a 143 }
8451168b 144 const char** pArguments=NULL;
145 int iNofChildArgs=0;
146 TString argument="";
147 int bMissingParam=0;
148 if (argc>0) {
149 pArguments=new const char*[argc];
150 if (pArguments) {
151 for (int i=0; i<argc && iResult>=0; i++) {
152 argument=argv[i];
153 if (argument.IsNull()) continue;
154
155 // benchmark
156 if (argument.CompareTo("benchmark")==0) {
157
158 // loglevel
159 } else if (argument.CompareTo("loglevel")==0) {
160 if ((bMissingParam=(++i>=argc))) break;
161 TString parameter(argv[i]);
162 parameter.Remove(TString::kLeading, ' '); // remove all blanks
163 if (parameter.BeginsWith("0x") &&
164 parameter.Replace(0,2,"",0).IsHex()) {
165 AliHLTComponentLogSeverity loglevel=kHLTLogNone;
7a5ccd96 166 sscanf(parameter.Data(),"%x", (unsigned int*)&loglevel);
8451168b 167 SetLocalLoggingLevel(loglevel);
168 } else {
169 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
170 iResult=-EINVAL;
171 }
172 } else {
173 pArguments[iNofChildArgs++]=argv[i];
174 }
175 }
176 } else {
177 iResult=-ENOMEM;
178 }
179 }
180 if (bMissingParam) {
181 HLTError("missing parameter for argument %s", argument.Data());
182 iResult=-EINVAL;
183 }
184 if (iResult>=0) {
185 iResult=DoInit(iNofChildArgs, pArguments);
186 }
3cde846d 187 if (iResult>=0) fEventCount=0;
8451168b 188 if (pArguments) delete [] pArguments;
f23a6e1a 189 return iResult;
190}
191
192int AliHLTComponent::Deinit()
193{
70ed7d01 194 // see header file for function documentation
f23a6e1a 195 int iResult=0;
196 iResult=DoDeinit();
197 return iResult;
198}
fa2e9b7c 199
53feaef5 200int AliHLTComponent::DoInit( int argc, const char** argv )
201{
70ed7d01 202 // see header file for function documentation
53feaef5 203 if (argc==0 && argv==NULL) {
204 // this is currently just to get rid of the warning "unused parameter"
205 }
66043029 206 fEventCount=0;
53feaef5 207 return 0;
208}
209
210int AliHLTComponent::DoDeinit()
211{
70ed7d01 212 // see header file for function documentation
66043029 213 fEventCount=0;
53feaef5 214 return 0;
215}
216
70ed7d01 217void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
218{
219 // see header file for function documentation
9ce4bf4a 220 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
221 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
222 strcat( output, ":" );
223 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
fa2e9b7c 224}
225
9ce4bf4a 226string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type )
227{
70ed7d01 228 // see header file for function documentation
9ce4bf4a 229 string out("");
3cde846d 230
9ce4bf4a 231 if (type==kAliHLTVoidDataType) {
232 out="VOID:VOID";
233 } else {
3cde846d 234 // some gymnastics in order to avoid a '0' which is part of either or both
235 // ID and origin terminating the whole string. Unfortunately, string doesn't
236 // stop appending at the '0' if the number of elements to append was
237 // explicitely specified
238 string tmp("");
239 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
240 out.append(tmp.c_str());
9ce4bf4a 241 out.append(":");
3cde846d 242 tmp="";
243 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
244 out.append(tmp.c_str());
9ce4bf4a 245 }
246 return out;
247}
248
249
70ed7d01 250void* AliHLTComponent::AllocMemory( unsigned long size )
251{
252 // see header file for function documentation
85869391 253 if (fEnvironment.fAllocMemoryFunc)
254 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
9ce4bf4a 255 HLTFatal("no memory allocation handler registered");
85869391 256 return NULL;
257}
258
8ede8717 259int AliHLTComponent::MakeOutputDataBlockList( const vector<AliHLTComponentBlockData>& blocks, AliHLTUInt32_t* blockCount,
70ed7d01 260 AliHLTComponentBlockData** outputBlocks )
261{
262 // see header file for function documentation
9ce4bf4a 263 if ( blockCount==NULL || outputBlocks==NULL )
2d7ff710 264 return -EFAULT;
fa2e9b7c 265 AliHLTUInt32_t count = blocks.size();
266 if ( !count )
267 {
268 *blockCount = 0;
269 *outputBlocks = NULL;
270 return 0;
271 }
8ede8717 272 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
fa2e9b7c 273 if ( !*outputBlocks )
2d7ff710 274 return -ENOMEM;
ca8524df 275 for ( unsigned long i = 0; i < count; i++ ) {
fa2e9b7c 276 (*outputBlocks)[i] = blocks[i];
ca8524df 277 if (blocks[i].fDataType==kAliHLTAnyDataType) {
5f5b708b 278 (*outputBlocks)[i].fDataType=GetOutputDataType();
279 /* data type was set to the output data type by the PubSub AliRoot
280 Wrapper component, if data type of the block was ********:****.
281 Now handled by the component base class in order to have same
282 behavior when running embedded in AliRoot
ca8524df 283 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
284 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
5f5b708b 285 */
ca8524df 286 }
287 }
fa2e9b7c 288 *blockCount = count;
289 return 0;
290
291}
0c0c9d99 292
70ed7d01 293int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
294{
295 // see header file for function documentation
85869391 296 if (fEnvironment.fGetEventDoneDataFunc)
297 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
298 return -ENOSYS;
299}
300
8ede8717 301int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
0c0c9d99 302{
70ed7d01 303 // see header file for function documentation
0c0c9d99 304 int iResult=0;
305 if (pConsumer) {
8ede8717 306 vector<AliHLTComponentDataType> ctlist;
0c0c9d99 307 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(ctlist);
8ede8717 308 vector<AliHLTComponentDataType>::iterator type=ctlist.begin();
5f5b708b 309 //AliHLTComponentDataType ouptdt=GetOutputDataType();
310 //PrintDataTypeContent(ouptdt, "publisher \'%s\'");
0c0c9d99 311 while (type!=ctlist.end() && iResult==0) {
5f5b708b 312 //PrintDataTypeContent((*type), "consumer \'%s\'");
9ce4bf4a 313 if ((*type)==GetOutputDataType() ||
314 (*type)==kAliHLTAnyDataType) {
0c0c9d99 315 if (tgtList) tgtList->push_back(*type);
316 iResult++;
9ce4bf4a 317 // this loop has to be changed in case of multiple output types
0c0c9d99 318 break;
319 }
320 type++;
321 }
322 } else {
323 iResult=-EINVAL;
324 }
325 return iResult;
326}
2d7ff710 327
5f5b708b 328void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
329{
66043029 330 // see header file for function documentation
5f5b708b 331 const char* fmt="publisher \'%s\'";
332 if (format) fmt=format;
333 HLTMessage(fmt, (DataType2Text(dt)).c_str());
334 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
335 dt.fID[0],
336 dt.fID[1],
337 dt.fID[2],
338 dt.fID[3],
339 dt.fID[4],
340 dt.fID[5],
341 dt.fID[6],
342 dt.fID[7],
343 dt.fOrigin[0],
344 dt.fOrigin[1],
345 dt.fOrigin[2],
346 dt.fOrigin[3]);
347}
348
70ed7d01 349void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData ) const
350{
351 // see header file for function documentation
2d7ff710 352 blockData.fStructSize = sizeof(blockData);
353 FillShmData( blockData.fShmKey );
354 blockData.fOffset = ~(AliHLTUInt32_t)0;
355 blockData.fPtr = NULL;
356 blockData.fSize = 0;
357 FillDataType( blockData.fDataType );
a655eae3 358 blockData.fSpecification = kAliHLTVoidDataSpec;
2d7ff710 359}
360
70ed7d01 361void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData ) const
362{
363 // see header file for function documentation
2d7ff710 364 shmData.fStructSize = sizeof(shmData);
365 shmData.fShmType = gkAliHLTComponentInvalidShmType;
366 shmData.fShmID = gkAliHLTComponentInvalidShmID;
367}
368
70ed7d01 369void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType ) const
370{
371 // see header file for function documentation
ca8524df 372 dataType=kAliHLTAnyDataType;
2d7ff710 373}
374
70ed7d01 375void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
376{
377 // see header file for function documentation
2d7ff710 378 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
379 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
380}
381
70ed7d01 382void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
383{
384 // see header file for function documentation
2d7ff710 385 tgtdt.fStructSize = sizeof(AliHLTComponentDataType);
386 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
387 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
388
9ce4bf4a 389 if ((int)strlen(id)>kAliHLTComponentDataTypefIDsize) {
2d7ff710 390 HLTWarning("data type id %s is too long, truncated to %d", id, kAliHLTComponentDataTypefIDsize);
391 }
392 strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
393
9ce4bf4a 394 if ((int)strlen(origin)>kAliHLTComponentDataTypefOriginSize) {
2d7ff710 395 HLTWarning("data type origin %s is too long, truncated to %d", origin, kAliHLTComponentDataTypefOriginSize);
396 }
397 strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
398}
9ce4bf4a 399
400void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
401{
70ed7d01 402 // see header file for function documentation
9ce4bf4a 403 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
404 evtData.fStructSize=sizeof(AliHLTComponentEventData);
405}
406
70ed7d01 407void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
408{
409 // see header file for function documentation
9ce4bf4a 410 TString msg;
411 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
412 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
413 if (dt.fID[i]!=0) msg+=dt.fID[i];
414 else msg+="\\0";
415 }
416 msg+="\" Origin=\"";
417 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
418 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
419 else msg+="\\0";
420 }
421 msg+="\"";
3cde846d 422 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
9ce4bf4a 423}
424
70ed7d01 425int AliHLTComponent::GetEventCount() const
3cde846d 426{
70ed7d01 427 // see header file for function documentation
3cde846d 428 return fEventCount;
429}
430
431int AliHLTComponent::IncrementEventCounter()
432{
70ed7d01 433 // see header file for function documentation
3cde846d 434 if (fEventCount>=0) fEventCount++;
435 return fEventCount;
436}
437
66043029 438int AliHLTComponent::GetNumberOfInputBlocks() const
a655eae3 439{
440 // see header file for function documentation
441 if (fpInputBlocks!=NULL) {
442 return fCurrentEventData.fBlockCnt;
443 }
444 return 0;
445}
446
447const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
448 const char* classname,
449 int bForce)
450{
451 // see header file for function documentation
90ebac25 452 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 453 fSearchDataType=dt;
454 if (classname) fClassName=classname;
455 else fClassName.clear();
1edbbe49 456 int idx=FindInputBlock(fSearchDataType, 0, 1);
8451168b 457 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
a655eae3 458 TObject* pObj=NULL;
459 if (idx>=0) {
460 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
461 fCurrentInputBlock=idx;
462 } else {
463 }
464 }
465 return pObj;
466}
467
468const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
469 const char* dtOrigin,
470 const char* classname,
471 int bForce)
472{
473 // see header file for function documentation
90ebac25 474 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 475 AliHLTComponentDataType dt;
476 SetDataType(dt, dtID, dtOrigin);
477 return GetFirstInputObject(dt, classname, bForce);
478}
479
480const TObject* AliHLTComponent::GetNextInputObject(int bForce)
481{
482 // see header file for function documentation
90ebac25 483 ALIHLTCOMPONENT_BASE_STOPWATCH();
1edbbe49 484 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
a655eae3 485 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
486 TObject* pObj=NULL;
487 if (idx>=0) {
488 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
489 fCurrentInputBlock=idx;
490 }
491 }
492 return pObj;
493}
494
1edbbe49 495int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
a655eae3 496{
497 // see header file for function documentation
498 int iResult=-ENOENT;
499 if (fpInputBlocks!=NULL) {
500 int idx=startIdx<0?0:startIdx;
4b98eadb 501 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
1edbbe49 502 if (bObject!=0) {
503 if (fpInputBlocks[idx].fPtr==NULL) continue;
504 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
505 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
506 }
a655eae3 507 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
508 iResult=idx;
509 }
510 }
511 }
512 return iResult;
513}
514
515TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
516{
517 // see header file for function documentation
518 TObject* pObj=NULL;
519 if (fpInputBlocks!=NULL) {
4b98eadb 520 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
a655eae3 521 if (fpInputBlocks[idx].fPtr) {
522 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
523 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
8451168b 524 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
a655eae3 525 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
66043029 526 TClass* objclass=msg.GetClass();
527 pObj=msg.ReadObject(objclass);
528 if (pObj && objclass) {
529 HLTDebug("object %p type %s created", pObj, objclass->GetName());
a655eae3 530 } else {
531 }
1edbbe49 532 //} else {
533 } else if (bForce!=0) {
a655eae3 534 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
535 }
536 } else {
537 HLTFatal("block descriptor empty");
538 }
539 } else {
540 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
541 }
542 } else {
543 HLTError("no input blocks available");
544 }
545
546 return pObj;
547}
548
549TObject* AliHLTComponent::GetInputObject(int idx, const char* classname, int bForce)
550{
551 // see header file for function documentation
552 if (fpInputObjects==NULL) {
553 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
554 }
555 TObject* pObj=NULL;
556 if (fpInputObjects) {
557 pObj=fpInputObjects->At(idx);
558 if (pObj==NULL) {
559 pObj=CreateInputObject(idx, bForce);
560 if (pObj) {
561 fpInputObjects->AddAt(pObj, idx);
562 }
563 }
564 } else {
565 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
566 }
567 return pObj;
568}
569
8451168b 570int AliHLTComponent::CleanupInputObjects()
571{
66043029 572 // see header file for function documentation
8451168b 573 if (!fpInputObjects) return 0;
574 TObjArray* array=fpInputObjects;
575 fpInputObjects=NULL;
576 for (int i=0; i<array->GetEntries(); i++) {
577 TObject* pObj=array->At(i);
578 if (pObj) delete pObj;
579 }
580 delete array;
90ebac25 581 return 0;
8451168b 582}
583
a655eae3 584AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
585{
586 // see header file for function documentation
90ebac25 587 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 588 AliHLTComponentDataType dt=kAliHLTVoidDataType;
589 int idx=fCurrentInputBlock;
590 if (pObject) {
591 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
592 } else {
593 HLTError("unknown object %p", pObject);
594 }
595 }
596 if (idx>=0) {
4b98eadb 597 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
a655eae3 598 dt=fpInputBlocks[idx].fDataType;
599 } else {
600 HLTFatal("severe internal error, index out of range");
601 }
602 }
603 return dt;
604}
605
606AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
607{
608 // see header file for function documentation
90ebac25 609 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 610 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
611 int idx=fCurrentInputBlock;
612 if (pObject) {
613 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
614 } else {
615 HLTError("unknown object %p", pObject);
616 }
617 }
618 if (idx>=0) {
4b98eadb 619 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
a655eae3 620 iSpec=fpInputBlocks[idx].fSpecification;
621 } else {
622 HLTFatal("severe internal error, index out of range");
623 }
624 }
625 return iSpec;
626}
627
628const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
629{
630 // see header file for function documentation
90ebac25 631 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 632 fSearchDataType=dt;
633 fClassName.clear();
634 int idx=FindInputBlock(fSearchDataType, 0);
635 const AliHLTComponentBlockData* pBlock=NULL;
636 if (idx>=0) {
637 // check for fpInputBlocks pointer done in FindInputBlock
638 pBlock=&fpInputBlocks[idx];
1edbbe49 639 fCurrentInputBlock=idx;
a655eae3 640 }
641 return pBlock;
642}
643
644const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
645 const char* dtOrigin)
646{
647 // see header file for function documentation
90ebac25 648 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 649 AliHLTComponentDataType dt;
650 SetDataType(dt, dtID, dtOrigin);
651 return GetFirstInputBlock(dt);
652}
653
654const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
655{
656 // see header file for function documentation
90ebac25 657 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 658 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
659 const AliHLTComponentBlockData* pBlock=NULL;
660 if (idx>=0) {
661 // check for fpInputBlocks pointer done in FindInputBlock
662 pBlock=&fpInputBlocks[idx];
1edbbe49 663 fCurrentInputBlock=idx;
a655eae3 664 }
665 return pBlock;
666}
667
66043029 668int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
a655eae3 669{
670 // see header file for function documentation
671 int iResult=-ENOENT;
672 if (fpInputBlocks!=NULL) {
673 if (pBlock) {
674 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
132ca004 675 iResult=(int)(pBlock-fpInputBlocks);
a655eae3 676 }
677 } else {
678 iResult=-EINVAL;
679 }
680 }
681 return iResult;
682}
683
684AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
685{
686 // see header file for function documentation
90ebac25 687 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 688 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
689 int idx=fCurrentInputBlock;
690 if (pBlock) {
691 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
692 } else {
693 HLTError("unknown Block %p", pBlock);
694 }
695 }
696 if (idx>=0) {
697 // check for fpInputBlocks pointer done in FindInputBlock
698 iSpec=fpInputBlocks[idx].fSpecification;
699 }
700 return iSpec;
701}
702
703int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
704{
705 // see header file for function documentation
90ebac25 706 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 707 int iResult=0;
708 if (pObject) {
709 AliHLTMessage msg(kMESS_OBJECT);
710 msg.WriteObject(pObject);
711 Int_t iMsgLength=msg.Length();
712 if (iMsgLength>0) {
713 msg.SetLength(); // sets the length to the first (reserved) word
714 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec);
715 if (iResult>=0) {
8451168b 716 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
a655eae3 717 }
718 } else {
719 HLTError("object serialization failed for object %p", pObject);
720 iResult=-ENOMSG;
721 }
722 } else {
723 iResult=-EINVAL;
724 }
725 return iResult;
726}
727
728int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
729{
730 // see header file for function documentation
90ebac25 731 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 732 AliHLTComponentDataType dt;
733 SetDataType(dt, dtID, dtOrigin);
734 return PushBack(pObject, dt, spec);
735}
736
737int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
738{
739 // see header file for function documentation
90ebac25 740 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 741 return InsertOutputBlock(pBuffer, iSize, dt, spec);
742}
743
744int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
745{
746 // see header file for function documentation
90ebac25 747 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 748 AliHLTComponentDataType dt;
749 SetDataType(dt, dtID, dtOrigin);
750 return PushBack(pBuffer, iSize, dt, spec);
751}
752
753int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
754{
755 // see header file for function documentation
756 int iResult=0;
757 if (pBuffer) {
7a5ccd96 758 if (fpOutputBuffer && iSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
a655eae3 759 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
760 AliHLTComponentBlockData bd;
761 FillBlockData( bd );
762 bd.fOffset = fOutputBufferFilled;
763 bd.fPtr = pTgt;
764 bd.fSize = iSize;
765 bd.fDataType = dt;
766 bd.fSpecification = spec;
767 if (pBuffer!=NULL && pBuffer!=pTgt) {
768 memcpy(pTgt, pBuffer, iSize);
4b98eadb 769 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
a655eae3 770 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iSize, pBuffer, pTgt, firstWord);
771 }
772 fOutputBufferFilled+=bd.fSize;
773 fOutputBlocks.push_back( bd );
774 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iSize, DataType2Text(dt).c_str(), spec);
775 } else {
776 if (fpOutputBuffer) {
777 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iSize);
778 } else {
779 HLTError("output buffer not available");
780 }
781 iResult=-ENOSPC;
782 }
783 } else {
784 iResult=-EINVAL;
785 }
786 return iResult;
787}
788
8451168b 789int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
790{
66043029 791 // see header file for function documentation
8451168b 792 if (!pObject) return -EINVAL;
793 AliHLTMessage msg(kMESS_OBJECT);
794 msg.WriteObject(pObject);
795 return msg.Length();
796}
797
a655eae3 798int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
799{
800 // see header file for function documentation
801 int iResult=-ENOSYS;
802 //#warning function not yet implemented
803 HLTWarning("function not yet implemented");
804 return iResult;
805}
806
3cde846d 807int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
808 const AliHLTComponentBlockData* blocks,
809 AliHLTComponentTriggerData& trigData,
810 AliHLTUInt8_t* outputPtr,
811 AliHLTUInt32_t& size,
812 AliHLTUInt32_t& outputBlockCnt,
813 AliHLTComponentBlockData*& outputBlocks,
814 AliHLTComponentEventDoneData*& edd )
815{
70ed7d01 816 // see header file for function documentation
90ebac25 817 ALIHLTCOMPONENT_BASE_STOPWATCH();
3cde846d 818 int iResult=0;
819 fCurrentEvent=evtData.fEventID;
a655eae3 820 fCurrentEventData=evtData;
821 fpInputBlocks=blocks;
822 fCurrentInputBlock=-1;
823 fSearchDataType=kAliHLTAnyDataType;
824 fpOutputBuffer=outputPtr;
825 fOutputBufferSize=size;
826 fOutputBufferFilled=0;
827 fOutputBlocks.clear();
828
829 vector<AliHLTComponentBlockData> blockData;
90ebac25 830 { // dont delete, sets the scope for the stopwatch guard
831 ALIHLTCOMPONENT_DA_STOPWATCH();
832 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
833 } // end of the scope of the stopwatch guard
a655eae3 834 if (iResult>=0) {
835 if (fOutputBlocks.size()>0) {
836 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
837 if (blockData.size()>0) {
838 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
839 iResult=-EFAULT;
840 } else {
841 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
842 size=fOutputBufferFilled;
843 }
844 } else {
845 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
846 }
847 if (iResult<0) {
848 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
849 }
850 }
851 if (iResult<0) {
852 outputBlockCnt=0;
853 outputBlocks=NULL;
854 }
8451168b 855 CleanupInputObjects();
3cde846d 856 IncrementEventCounter();
857 return iResult;
858}
a655eae3 859
90ebac25 860AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
861 :
862 fpStopwatch(NULL),
863 fpPrec(NULL)
864{
865 // standard constructor (not for use)
866}
867
868AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
869 :
870 fpStopwatch(pStopwatch),
871 fpPrec(NULL)
872{
873 // constructor
874
875 // check for already existing guard
876 if (fgpCurrent) fpPrec=fgpCurrent;
877 fgpCurrent=this;
878
879 // stop the preceeding guard if it controls a different stopwatch
880 int bStart=1;
881 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
882
883 // start the stopwatch if the current guard controls a different one
884 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
885}
886
e419b223 887AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
90ebac25 888 :
889 fpStopwatch(NULL),
890 fpPrec(NULL)
891{
e419b223 892 //
893 // copy constructor not for use
894 //
895}
896
897AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
898{
899 //
900 // assignment operator not for use
901 //
902 fpStopwatch=NULL;
903 fpPrec=NULL;
904 return *this;
90ebac25 905}
906
907AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
908
909AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
910{
911 // destructor
912
913 // resume the preceeding guard if it controls a different stopwatch
914 int bStop=1;
915 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
916
917 // stop the stopwatch if the current guard controls a different one
918 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
919
920 // resume to the preceeding guard
921 fgpCurrent=fpPrec;
922}
923
924int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
925{
926 // see header file for function documentation
927 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
928 return fpStopwatch!=pSucc?1:0;
929}
930
931int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
932{
933 // see header file for function documentation
934 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
935 return fpStopwatch!=pSucc?1:0;
936}
937
938int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
939{
940 // see header file for function documentation
941 int iResult=0;
942 if (pSW!=NULL && type<kSWTypeCount) {
943 if (fpStopwatches) {
944 TObject* pObj=fpStopwatches->At((int)type);
945 if (pSW==NULL // explicit reset
946 || pObj==NULL) { // explicit set
947 fpStopwatches->AddAt(pSW, (int)type);
948 } else if (pObj!=pSW) {
949 HLTWarning("stopwatch %d already set, reset first", (int)type);
950 iResult=-EBUSY;
951 }
952 }
953 } else {
954 iResult=-EINVAL;
955 }
956 return iResult;
957}
958
959int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
960{
961 // see header file for function documentation
962 if (pStopwatches==NULL) return -EINVAL;
963
964 int iResult=0;
965 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
966 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
967 return iResult;
968}