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);
66 AliHLTComponent::AliHLTComponent(const AliHLTComponent&)
75 fCurrentInputBlock(-1),
76 fSearchDataType(kAliHLTVoidDataType),
81 fOutputBufferFilled(0),
84 // see header file for class documentation
85 HLTFatal("copy constructor untested");
88 AliHLTComponent& AliHLTComponent::operator=(const AliHLTComponent&)
90 // see header file for class documentation
91 HLTFatal("assignment operator untested");
95 AliHLTComponent::~AliHLTComponent()
97 // see header file for function documentation
100 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
102 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
104 // see header file for function documentation
106 if (fgpComponentHandler==NULL || bOverwrite!=0)
107 fgpComponentHandler=pCH;
113 int AliHLTComponent::UnsetGlobalComponentHandler()
115 // see header file for function documentation
116 return SetGlobalComponentHandler(NULL,1);
119 int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
121 // see header file for function documentation
124 memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
125 fEnvironment.fParam=environParam;
127 iResult=DoInit(argc, argv);
128 if (iResult>=0) fEventCount=0;
132 int AliHLTComponent::Deinit()
134 // see header file for function documentation
140 int AliHLTComponent::DoInit( int argc, const char** argv )
142 // see header file for function documentation
143 if (argc==0 && argv==NULL) {
144 // this is currently just to get rid of the warning "unused parameter"
149 int AliHLTComponent::DoDeinit()
151 // see header file for function documentation
155 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
157 // see header file for function documentation
158 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
159 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
160 strcat( output, ":" );
161 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
164 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type )
166 // see header file for function documentation
169 if (type==kAliHLTVoidDataType) {
172 // some gymnastics in order to avoid a '0' which is part of either or both
173 // ID and origin terminating the whole string. Unfortunately, string doesn't
174 // stop appending at the '0' if the number of elements to append was
175 // explicitely specified
177 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
178 out.append(tmp.c_str());
181 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
182 out.append(tmp.c_str());
188 void* AliHLTComponent::AllocMemory( unsigned long size )
190 // see header file for function documentation
191 if (fEnvironment.fAllocMemoryFunc)
192 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
193 HLTFatal("no memory allocation handler registered");
197 int AliHLTComponent::MakeOutputDataBlockList( const vector<AliHLTComponentBlockData>& blocks, AliHLTUInt32_t* blockCount,
198 AliHLTComponentBlockData** outputBlocks )
200 // see header file for function documentation
201 if ( blockCount==NULL || outputBlocks==NULL )
203 AliHLTUInt32_t count = blocks.size();
207 *outputBlocks = NULL;
210 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
211 if ( !*outputBlocks )
213 for ( unsigned long i = 0; i < count; i++ ) {
214 (*outputBlocks)[i] = blocks[i];
215 if (blocks[i].fDataType==kAliHLTAnyDataType) {
216 (*outputBlocks)[i].fDataType=GetOutputDataType();
217 /* data type was set to the output data type by the PubSub AliRoot
218 Wrapper component, if data type of the block was ********:****.
219 Now handled by the component base class in order to have same
220 behavior when running embedded in AliRoot
221 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
222 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
231 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
233 // see header file for function documentation
234 if (fEnvironment.fGetEventDoneDataFunc)
235 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
239 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
241 // see header file for function documentation
244 vector<AliHLTComponentDataType> ctlist;
245 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(ctlist);
246 vector<AliHLTComponentDataType>::iterator type=ctlist.begin();
247 //AliHLTComponentDataType ouptdt=GetOutputDataType();
248 //PrintDataTypeContent(ouptdt, "publisher \'%s\'");
249 while (type!=ctlist.end() && iResult==0) {
250 //PrintDataTypeContent((*type), "consumer \'%s\'");
251 if ((*type)==GetOutputDataType() ||
252 (*type)==kAliHLTAnyDataType) {
253 if (tgtList) tgtList->push_back(*type);
255 // this loop has to be changed in case of multiple output types
266 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
268 const char* fmt="publisher \'%s\'";
269 if (format) fmt=format;
270 HLTMessage(fmt, (DataType2Text(dt)).c_str());
271 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
286 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData ) const
288 // see header file for function documentation
289 blockData.fStructSize = sizeof(blockData);
290 FillShmData( blockData.fShmKey );
291 blockData.fOffset = ~(AliHLTUInt32_t)0;
292 blockData.fPtr = NULL;
294 FillDataType( blockData.fDataType );
295 blockData.fSpecification = kAliHLTVoidDataSpec;
298 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData ) const
300 // see header file for function documentation
301 shmData.fStructSize = sizeof(shmData);
302 shmData.fShmType = gkAliHLTComponentInvalidShmType;
303 shmData.fShmID = gkAliHLTComponentInvalidShmID;
306 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType ) const
308 // see header file for function documentation
309 dataType=kAliHLTAnyDataType;
312 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
314 // see header file for function documentation
315 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
316 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
319 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
321 // see header file for function documentation
322 tgtdt.fStructSize = sizeof(AliHLTComponentDataType);
323 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
324 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
326 if ((int)strlen(id)>kAliHLTComponentDataTypefIDsize) {
327 HLTWarning("data type id %s is too long, truncated to %d", id, kAliHLTComponentDataTypefIDsize);
329 strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
331 if ((int)strlen(origin)>kAliHLTComponentDataTypefOriginSize) {
332 HLTWarning("data type origin %s is too long, truncated to %d", origin, kAliHLTComponentDataTypefOriginSize);
334 strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
337 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
339 // see header file for function documentation
340 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
341 evtData.fStructSize=sizeof(AliHLTComponentEventData);
344 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
346 // see header file for function documentation
348 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
349 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
350 if (dt.fID[i]!=0) msg+=dt.fID[i];
354 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
355 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
359 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
362 int AliHLTComponent::GetEventCount() const
364 // see header file for function documentation
368 int AliHLTComponent::IncrementEventCounter()
370 // see header file for function documentation
371 if (fEventCount>=0) fEventCount++;
375 int AliHLTComponent::GetNumberOfInputBlocks()
377 // see header file for function documentation
378 if (fpInputBlocks!=NULL) {
379 return fCurrentEventData.fBlockCnt;
384 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
385 const char* classname,
388 // see header file for function documentation
390 if (classname) fClassName=classname;
391 else fClassName.clear();
392 int idx=FindInputBlock(fSearchDataType, 0);
393 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
396 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
397 fCurrentInputBlock=idx;
404 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
405 const char* dtOrigin,
406 const char* classname,
409 // see header file for function documentation
410 AliHLTComponentDataType dt;
411 SetDataType(dt, dtID, dtOrigin);
412 return GetFirstInputObject(dt, classname, bForce);
415 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
417 // see header file for function documentation
418 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
419 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
422 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
423 fCurrentInputBlock=idx;
429 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx)
431 // see header file for function documentation
433 if (fpInputBlocks!=NULL) {
434 int idx=startIdx<0?0:startIdx;
435 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
436 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
444 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
446 // see header file for function documentation
448 if (fpInputBlocks!=NULL) {
449 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
450 if (fpInputBlocks[idx].fPtr) {
451 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
452 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
453 //HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
454 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
455 pObj=msg.ReadObject(msg.GetClass());
456 if (pObj && msg.GetClass()) {
457 //HLTDebug("object %p type %s created", pObj, msg.GetClass()->GetName());
461 // } else if (bForce!=0) {
462 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
465 HLTFatal("block descriptor empty");
468 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
471 HLTError("no input blocks available");
477 TObject* AliHLTComponent::GetInputObject(int idx, const char* classname, int bForce)
479 // see header file for function documentation
480 if (fpInputObjects==NULL) {
481 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
484 if (fpInputObjects) {
485 pObj=fpInputObjects->At(idx);
487 pObj=CreateInputObject(idx, bForce);
489 fpInputObjects->AddAt(pObj, idx);
493 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
498 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
500 // see header file for function documentation
501 AliHLTComponentDataType dt=kAliHLTVoidDataType;
502 int idx=fCurrentInputBlock;
504 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
506 HLTError("unknown object %p", pObject);
510 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
511 dt=fpInputBlocks[idx].fDataType;
513 HLTFatal("severe internal error, index out of range");
519 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
521 // see header file for function documentation
522 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
523 int idx=fCurrentInputBlock;
525 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
527 HLTError("unknown object %p", pObject);
531 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
532 iSpec=fpInputBlocks[idx].fSpecification;
534 HLTFatal("severe internal error, index out of range");
540 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
542 // see header file for function documentation
545 int idx=FindInputBlock(fSearchDataType, 0);
546 const AliHLTComponentBlockData* pBlock=NULL;
548 // check for fpInputBlocks pointer done in FindInputBlock
549 pBlock=&fpInputBlocks[idx];
554 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
555 const char* dtOrigin)
557 // see header file for function documentation
558 AliHLTComponentDataType dt;
559 SetDataType(dt, dtID, dtOrigin);
560 return GetFirstInputBlock(dt);
563 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
565 // see header file for function documentation
566 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
567 const AliHLTComponentBlockData* pBlock=NULL;
569 // check for fpInputBlocks pointer done in FindInputBlock
570 pBlock=&fpInputBlocks[idx];
575 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock)
577 // see header file for function documentation
579 if (fpInputBlocks!=NULL) {
581 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
582 iResult=(int)(pBlock-fpInputBlocks);
591 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
593 // see header file for function documentation
594 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
595 int idx=fCurrentInputBlock;
597 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
599 HLTError("unknown Block %p", pBlock);
603 // check for fpInputBlocks pointer done in FindInputBlock
604 iSpec=fpInputBlocks[idx].fSpecification;
609 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
611 // see header file for function documentation
614 AliHLTMessage msg(kMESS_OBJECT);
615 msg.WriteObject(pObject);
616 Int_t iMsgLength=msg.Length();
618 msg.SetLength(); // sets the length to the first (reserved) word
619 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec);
621 //HLTDebug("object %s (%p) inserted to output", pObject->ClassName(), pObject);
624 HLTError("object serialization failed for object %p", pObject);
633 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
635 // see header file for function documentation
636 AliHLTComponentDataType dt;
637 SetDataType(dt, dtID, dtOrigin);
638 return PushBack(pObject, dt, spec);
641 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
643 // see header file for function documentation
644 return InsertOutputBlock(pBuffer, iSize, dt, spec);
647 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
649 // see header file for function documentation
650 AliHLTComponentDataType dt;
651 SetDataType(dt, dtID, dtOrigin);
652 return PushBack(pBuffer, iSize, dt, spec);
655 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
657 // see header file for function documentation
660 if (fpOutputBuffer && (fOutputBufferSize-fOutputBufferFilled)) {
661 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
662 AliHLTComponentBlockData bd;
664 bd.fOffset = fOutputBufferFilled;
668 bd.fSpecification = spec;
669 if (pBuffer!=NULL && pBuffer!=pTgt) {
670 memcpy(pTgt, pBuffer, iSize);
671 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
672 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iSize, pBuffer, pTgt, firstWord);
674 fOutputBufferFilled+=bd.fSize;
675 fOutputBlocks.push_back( bd );
676 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iSize, DataType2Text(dt).c_str(), spec);
678 if (fpOutputBuffer) {
679 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iSize);
681 HLTError("output buffer not available");
691 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
693 // see header file for function documentation
695 //#warning function not yet implemented
696 HLTWarning("function not yet implemented");
700 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
701 const AliHLTComponentBlockData* blocks,
702 AliHLTComponentTriggerData& trigData,
703 AliHLTUInt8_t* outputPtr,
704 AliHLTUInt32_t& size,
705 AliHLTUInt32_t& outputBlockCnt,
706 AliHLTComponentBlockData*& outputBlocks,
707 AliHLTComponentEventDoneData*& edd )
709 // see header file for function documentation
711 fCurrentEvent=evtData.fEventID;
712 fCurrentEventData=evtData;
713 fpInputBlocks=blocks;
714 fCurrentInputBlock=-1;
715 fSearchDataType=kAliHLTAnyDataType;
716 fpOutputBuffer=outputPtr;
717 fOutputBufferSize=size;
718 fOutputBufferFilled=0;
719 fOutputBlocks.clear();
721 vector<AliHLTComponentBlockData> blockData;
722 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
724 if (fOutputBlocks.size()>0) {
725 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
726 if (blockData.size()>0) {
727 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
730 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
731 size=fOutputBufferFilled;
734 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
737 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
744 IncrementEventCounter();