3 /**************************************************************************
4 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
6 * Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
7 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
8 * for The ALICE Off-line Project. *
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 **************************************************************************/
19 /** @file AliHLTComponent.cxx
20 @author Matthias Richter, Timm Steinbeck
22 @brief Base class implementation for HLT components. */
28 #include "AliHLTStdIncludes.h"
29 #include "AliHLTComponent.h"
30 #include "AliHLTComponentHandler.h"
31 #include "AliHLTMessage.h"
33 #include "TObjArray.h"
36 /** ROOT macro for the implementation of ROOT specific class methods */
37 ClassImp(AliHLTComponent)
39 AliHLTComponent::AliHLTComponent()
47 fCurrentInputBlock(-1),
48 fSearchDataType(kAliHLTVoidDataType),
53 fOutputBufferFilled(0),
56 // see header file for class documentation
58 // refer to README to build package
60 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
61 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
62 if (fgpComponentHandler)
63 fgpComponentHandler->ScheduleRegister(this);
64 SetLocalLoggingLevel(kHLTLogDefault);
67 AliHLTComponent::AliHLTComponent(const AliHLTComponent&)
76 fCurrentInputBlock(-1),
77 fSearchDataType(kAliHLTVoidDataType),
82 fOutputBufferFilled(0),
85 // see header file for class documentation
86 HLTFatal("copy constructor untested");
89 AliHLTComponent& AliHLTComponent::operator=(const AliHLTComponent&)
91 // see header file for class documentation
92 HLTFatal("assignment operator untested");
96 AliHLTComponent::~AliHLTComponent()
98 // see header file for function documentation
99 CleanupInputObjects();
102 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
104 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
106 // see header file for function documentation
108 if (fgpComponentHandler==NULL || bOverwrite!=0)
109 fgpComponentHandler=pCH;
115 int AliHLTComponent::UnsetGlobalComponentHandler()
117 // see header file for function documentation
118 return SetGlobalComponentHandler(NULL,1);
121 int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
123 // see header file for function documentation
126 memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
127 fEnvironment.fParam=environParam;
129 const char** pArguments=NULL;
134 pArguments=new const char*[argc];
136 for (int i=0; i<argc && iResult>=0; i++) {
138 if (argument.IsNull()) continue;
141 if (argument.CompareTo("benchmark")==0) {
144 } else if (argument.CompareTo("loglevel")==0) {
145 if ((bMissingParam=(++i>=argc))) break;
146 TString parameter(argv[i]);
147 parameter.Remove(TString::kLeading, ' '); // remove all blanks
148 if (parameter.BeginsWith("0x") &&
149 parameter.Replace(0,2,"",0).IsHex()) {
150 AliHLTComponentLogSeverity loglevel=kHLTLogNone;
151 sscanf(parameter.Data(),"%x", &loglevel);
152 SetLocalLoggingLevel(loglevel);
154 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
158 pArguments[iNofChildArgs++]=argv[i];
166 HLTError("missing parameter for argument %s", argument.Data());
170 iResult=DoInit(iNofChildArgs, pArguments);
172 if (iResult>=0) fEventCount=0;
173 if (pArguments) delete [] pArguments;
177 int AliHLTComponent::Deinit()
179 // see header file for function documentation
185 int AliHLTComponent::DoInit( int argc, const char** argv )
187 // see header file for function documentation
188 if (argc==0 && argv==NULL) {
189 // this is currently just to get rid of the warning "unused parameter"
194 int AliHLTComponent::DoDeinit()
196 // see header file for function documentation
200 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
202 // see header file for function documentation
203 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
204 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
205 strcat( output, ":" );
206 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
209 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type )
211 // see header file for function documentation
214 if (type==kAliHLTVoidDataType) {
217 // some gymnastics in order to avoid a '0' which is part of either or both
218 // ID and origin terminating the whole string. Unfortunately, string doesn't
219 // stop appending at the '0' if the number of elements to append was
220 // explicitely specified
222 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
223 out.append(tmp.c_str());
226 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
227 out.append(tmp.c_str());
233 void* AliHLTComponent::AllocMemory( unsigned long size )
235 // see header file for function documentation
236 if (fEnvironment.fAllocMemoryFunc)
237 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
238 HLTFatal("no memory allocation handler registered");
242 int AliHLTComponent::MakeOutputDataBlockList( const vector<AliHLTComponentBlockData>& blocks, AliHLTUInt32_t* blockCount,
243 AliHLTComponentBlockData** outputBlocks )
245 // see header file for function documentation
246 if ( blockCount==NULL || outputBlocks==NULL )
248 AliHLTUInt32_t count = blocks.size();
252 *outputBlocks = NULL;
255 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
256 if ( !*outputBlocks )
258 for ( unsigned long i = 0; i < count; i++ ) {
259 (*outputBlocks)[i] = blocks[i];
260 if (blocks[i].fDataType==kAliHLTAnyDataType) {
261 (*outputBlocks)[i].fDataType=GetOutputDataType();
262 /* data type was set to the output data type by the PubSub AliRoot
263 Wrapper component, if data type of the block was ********:****.
264 Now handled by the component base class in order to have same
265 behavior when running embedded in AliRoot
266 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
267 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
276 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
278 // see header file for function documentation
279 if (fEnvironment.fGetEventDoneDataFunc)
280 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
284 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
286 // see header file for function documentation
289 vector<AliHLTComponentDataType> ctlist;
290 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(ctlist);
291 vector<AliHLTComponentDataType>::iterator type=ctlist.begin();
292 //AliHLTComponentDataType ouptdt=GetOutputDataType();
293 //PrintDataTypeContent(ouptdt, "publisher \'%s\'");
294 while (type!=ctlist.end() && iResult==0) {
295 //PrintDataTypeContent((*type), "consumer \'%s\'");
296 if ((*type)==GetOutputDataType() ||
297 (*type)==kAliHLTAnyDataType) {
298 if (tgtList) tgtList->push_back(*type);
300 // this loop has to be changed in case of multiple output types
311 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
313 const char* fmt="publisher \'%s\'";
314 if (format) fmt=format;
315 HLTMessage(fmt, (DataType2Text(dt)).c_str());
316 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
331 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData ) const
333 // see header file for function documentation
334 blockData.fStructSize = sizeof(blockData);
335 FillShmData( blockData.fShmKey );
336 blockData.fOffset = ~(AliHLTUInt32_t)0;
337 blockData.fPtr = NULL;
339 FillDataType( blockData.fDataType );
340 blockData.fSpecification = kAliHLTVoidDataSpec;
343 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData ) const
345 // see header file for function documentation
346 shmData.fStructSize = sizeof(shmData);
347 shmData.fShmType = gkAliHLTComponentInvalidShmType;
348 shmData.fShmID = gkAliHLTComponentInvalidShmID;
351 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType ) const
353 // see header file for function documentation
354 dataType=kAliHLTAnyDataType;
357 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
359 // see header file for function documentation
360 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
361 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
364 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
366 // see header file for function documentation
367 tgtdt.fStructSize = sizeof(AliHLTComponentDataType);
368 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
369 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
371 if ((int)strlen(id)>kAliHLTComponentDataTypefIDsize) {
372 HLTWarning("data type id %s is too long, truncated to %d", id, kAliHLTComponentDataTypefIDsize);
374 strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
376 if ((int)strlen(origin)>kAliHLTComponentDataTypefOriginSize) {
377 HLTWarning("data type origin %s is too long, truncated to %d", origin, kAliHLTComponentDataTypefOriginSize);
379 strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
382 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
384 // see header file for function documentation
385 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
386 evtData.fStructSize=sizeof(AliHLTComponentEventData);
389 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
391 // see header file for function documentation
393 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
394 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
395 if (dt.fID[i]!=0) msg+=dt.fID[i];
399 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
400 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
404 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
407 int AliHLTComponent::GetEventCount() const
409 // see header file for function documentation
413 int AliHLTComponent::IncrementEventCounter()
415 // see header file for function documentation
416 if (fEventCount>=0) fEventCount++;
420 int AliHLTComponent::GetNumberOfInputBlocks()
422 // see header file for function documentation
423 if (fpInputBlocks!=NULL) {
424 return fCurrentEventData.fBlockCnt;
429 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
430 const char* classname,
433 // see header file for function documentation
435 if (classname) fClassName=classname;
436 else fClassName.clear();
437 int idx=FindInputBlock(fSearchDataType, 0);
438 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
441 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
442 fCurrentInputBlock=idx;
449 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
450 const char* dtOrigin,
451 const char* classname,
454 // see header file for function documentation
455 AliHLTComponentDataType dt;
456 SetDataType(dt, dtID, dtOrigin);
457 return GetFirstInputObject(dt, classname, bForce);
460 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
462 // see header file for function documentation
463 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
464 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
467 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
468 fCurrentInputBlock=idx;
474 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx)
476 // see header file for function documentation
478 if (fpInputBlocks!=NULL) {
479 int idx=startIdx<0?0:startIdx;
480 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
481 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
489 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
491 // see header file for function documentation
493 if (fpInputBlocks!=NULL) {
494 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
495 if (fpInputBlocks[idx].fPtr) {
496 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
497 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
498 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
499 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
500 pObj=msg.ReadObject(msg.GetClass());
501 if (pObj && msg.GetClass()) {
502 HLTDebug("object %p type %s created", pObj, msg.GetClass()->GetName());
506 // } else if (bForce!=0) {
507 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
510 HLTFatal("block descriptor empty");
513 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
516 HLTError("no input blocks available");
522 TObject* AliHLTComponent::GetInputObject(int idx, const char* classname, int bForce)
524 // see header file for function documentation
525 if (fpInputObjects==NULL) {
526 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
529 if (fpInputObjects) {
530 pObj=fpInputObjects->At(idx);
532 pObj=CreateInputObject(idx, bForce);
534 fpInputObjects->AddAt(pObj, idx);
538 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
543 int AliHLTComponent::CleanupInputObjects()
545 if (!fpInputObjects) return 0;
546 TObjArray* array=fpInputObjects;
548 for (int i=0; i<array->GetEntries(); i++) {
549 TObject* pObj=array->At(i);
550 if (pObj) delete pObj;
555 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
557 // see header file for function documentation
558 AliHLTComponentDataType dt=kAliHLTVoidDataType;
559 int idx=fCurrentInputBlock;
561 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
563 HLTError("unknown object %p", pObject);
567 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
568 dt=fpInputBlocks[idx].fDataType;
570 HLTFatal("severe internal error, index out of range");
576 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
578 // see header file for function documentation
579 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
580 int idx=fCurrentInputBlock;
582 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
584 HLTError("unknown object %p", pObject);
588 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
589 iSpec=fpInputBlocks[idx].fSpecification;
591 HLTFatal("severe internal error, index out of range");
597 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
599 // see header file for function documentation
602 int idx=FindInputBlock(fSearchDataType, 0);
603 const AliHLTComponentBlockData* pBlock=NULL;
605 // check for fpInputBlocks pointer done in FindInputBlock
606 pBlock=&fpInputBlocks[idx];
611 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
612 const char* dtOrigin)
614 // see header file for function documentation
615 AliHLTComponentDataType dt;
616 SetDataType(dt, dtID, dtOrigin);
617 return GetFirstInputBlock(dt);
620 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
622 // see header file for function documentation
623 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
624 const AliHLTComponentBlockData* pBlock=NULL;
626 // check for fpInputBlocks pointer done in FindInputBlock
627 pBlock=&fpInputBlocks[idx];
632 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock)
634 // see header file for function documentation
636 if (fpInputBlocks!=NULL) {
638 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
639 iResult=(int)(pBlock-fpInputBlocks);
648 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
650 // see header file for function documentation
651 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
652 int idx=fCurrentInputBlock;
654 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
656 HLTError("unknown Block %p", pBlock);
660 // check for fpInputBlocks pointer done in FindInputBlock
661 iSpec=fpInputBlocks[idx].fSpecification;
666 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
668 // see header file for function documentation
671 AliHLTMessage msg(kMESS_OBJECT);
672 msg.WriteObject(pObject);
673 Int_t iMsgLength=msg.Length();
675 msg.SetLength(); // sets the length to the first (reserved) word
676 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec);
678 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
681 HLTError("object serialization failed for object %p", pObject);
690 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
692 // see header file for function documentation
693 AliHLTComponentDataType dt;
694 SetDataType(dt, dtID, dtOrigin);
695 return PushBack(pObject, dt, spec);
698 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
700 // see header file for function documentation
701 return InsertOutputBlock(pBuffer, iSize, dt, spec);
704 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
706 // see header file for function documentation
707 AliHLTComponentDataType dt;
708 SetDataType(dt, dtID, dtOrigin);
709 return PushBack(pBuffer, iSize, dt, spec);
712 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
714 // see header file for function documentation
717 if (fpOutputBuffer && iSize<=(fOutputBufferSize-fOutputBufferFilled)) {
718 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
719 AliHLTComponentBlockData bd;
721 bd.fOffset = fOutputBufferFilled;
725 bd.fSpecification = spec;
726 if (pBuffer!=NULL && pBuffer!=pTgt) {
727 memcpy(pTgt, pBuffer, iSize);
728 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
729 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iSize, pBuffer, pTgt, firstWord);
731 fOutputBufferFilled+=bd.fSize;
732 fOutputBlocks.push_back( bd );
733 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iSize, DataType2Text(dt).c_str(), spec);
735 if (fpOutputBuffer) {
736 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iSize);
738 HLTError("output buffer not available");
748 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
750 if (!pObject) return -EINVAL;
751 AliHLTMessage msg(kMESS_OBJECT);
752 msg.WriteObject(pObject);
756 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
758 // see header file for function documentation
760 //#warning function not yet implemented
761 HLTWarning("function not yet implemented");
765 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
766 const AliHLTComponentBlockData* blocks,
767 AliHLTComponentTriggerData& trigData,
768 AliHLTUInt8_t* outputPtr,
769 AliHLTUInt32_t& size,
770 AliHLTUInt32_t& outputBlockCnt,
771 AliHLTComponentBlockData*& outputBlocks,
772 AliHLTComponentEventDoneData*& edd )
774 // see header file for function documentation
776 fCurrentEvent=evtData.fEventID;
777 fCurrentEventData=evtData;
778 fpInputBlocks=blocks;
779 fCurrentInputBlock=-1;
780 fSearchDataType=kAliHLTAnyDataType;
781 fpOutputBuffer=outputPtr;
782 fOutputBufferSize=size;
783 fOutputBufferFilled=0;
784 fOutputBlocks.clear();
786 vector<AliHLTComponentBlockData> blockData;
787 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
789 if (fOutputBlocks.size()>0) {
790 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
791 if (blockData.size()>0) {
792 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
795 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
796 size=fOutputBufferFilled;
799 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
802 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
809 CleanupInputObjects();
810 IncrementEventCounter();