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&)
74 fCurrentInputBlock(-1),
75 fSearchDataType(kAliHLTVoidDataType),
80 fOutputBufferFilled(0),
83 // see header file for class documentation
84 HLTFatal("copy constructor untested");
87 AliHLTComponent& AliHLTComponent::operator=(const AliHLTComponent&)
89 // see header file for class documentation
90 HLTFatal("assignment operator untested");
94 AliHLTComponent::~AliHLTComponent()
96 // see header file for function documentation
99 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
101 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
103 // see header file for function documentation
105 if (fgpComponentHandler==NULL || bOverwrite!=0)
106 fgpComponentHandler=pCH;
112 int AliHLTComponent::UnsetGlobalComponentHandler()
114 // see header file for function documentation
115 return SetGlobalComponentHandler(NULL,1);
118 int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
120 // see header file for function documentation
123 memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
124 fEnvironment.fParam=environParam;
126 iResult=DoInit(argc, argv);
127 if (iResult>=0) fEventCount=0;
131 int AliHLTComponent::Deinit()
133 // see header file for function documentation
139 int AliHLTComponent::DoInit( int argc, const char** argv )
141 // see header file for function documentation
142 if (argc==0 && argv==NULL) {
143 // this is currently just to get rid of the warning "unused parameter"
148 int AliHLTComponent::DoDeinit()
150 // see header file for function documentation
154 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
156 // see header file for function documentation
157 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
158 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
159 strcat( output, ":" );
160 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
163 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type )
165 // see header file for function documentation
168 if (type==kAliHLTVoidDataType) {
171 // some gymnastics in order to avoid a '0' which is part of either or both
172 // ID and origin terminating the whole string. Unfortunately, string doesn't
173 // stop appending at the '0' if the number of elements to append was
174 // explicitely specified
176 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
177 out.append(tmp.c_str());
180 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
181 out.append(tmp.c_str());
187 void* AliHLTComponent::AllocMemory( unsigned long size )
189 // see header file for function documentation
190 if (fEnvironment.fAllocMemoryFunc)
191 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
192 HLTFatal("no memory allocation handler registered");
196 int AliHLTComponent::MakeOutputDataBlockList( const vector<AliHLTComponentBlockData>& blocks, AliHLTUInt32_t* blockCount,
197 AliHLTComponentBlockData** outputBlocks )
199 // see header file for function documentation
200 if ( blockCount==NULL || outputBlocks==NULL )
202 AliHLTUInt32_t count = blocks.size();
206 *outputBlocks = NULL;
209 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
210 if ( !*outputBlocks )
212 for ( unsigned long i = 0; i < count; i++ ) {
213 (*outputBlocks)[i] = blocks[i];
214 if (blocks[i].fDataType==kAliHLTAnyDataType) {
215 (*outputBlocks)[i].fDataType=GetOutputDataType();
216 /* data type was set to the output data type by the PubSub AliRoot
217 Wrapper component, if data type of the block was ********:****.
218 Now handled by the component base class in order to have same
219 behavior when running embedded in AliRoot
220 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
221 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
230 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
232 // see header file for function documentation
233 if (fEnvironment.fGetEventDoneDataFunc)
234 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
238 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
240 // see header file for function documentation
243 vector<AliHLTComponentDataType> ctlist;
244 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(ctlist);
245 vector<AliHLTComponentDataType>::iterator type=ctlist.begin();
246 //AliHLTComponentDataType ouptdt=GetOutputDataType();
247 //PrintDataTypeContent(ouptdt, "publisher \'%s\'");
248 while (type!=ctlist.end() && iResult==0) {
249 //PrintDataTypeContent((*type), "consumer \'%s\'");
250 if ((*type)==GetOutputDataType() ||
251 (*type)==kAliHLTAnyDataType) {
252 if (tgtList) tgtList->push_back(*type);
254 // this loop has to be changed in case of multiple output types
265 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
267 const char* fmt="publisher \'%s\'";
268 if (format) fmt=format;
269 HLTMessage(fmt, (DataType2Text(dt)).c_str());
270 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
285 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData ) const
287 // see header file for function documentation
288 blockData.fStructSize = sizeof(blockData);
289 FillShmData( blockData.fShmKey );
290 blockData.fOffset = ~(AliHLTUInt32_t)0;
291 blockData.fPtr = NULL;
293 FillDataType( blockData.fDataType );
294 blockData.fSpecification = kAliHLTVoidDataSpec;
297 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData ) const
299 // see header file for function documentation
300 shmData.fStructSize = sizeof(shmData);
301 shmData.fShmType = gkAliHLTComponentInvalidShmType;
302 shmData.fShmID = gkAliHLTComponentInvalidShmID;
305 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType ) const
307 // see header file for function documentation
308 dataType=kAliHLTAnyDataType;
311 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
313 // see header file for function documentation
314 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
315 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
318 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
320 // see header file for function documentation
321 tgtdt.fStructSize = sizeof(AliHLTComponentDataType);
322 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
323 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
325 if ((int)strlen(id)>kAliHLTComponentDataTypefIDsize) {
326 HLTWarning("data type id %s is too long, truncated to %d", id, kAliHLTComponentDataTypefIDsize);
328 strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
330 if ((int)strlen(origin)>kAliHLTComponentDataTypefOriginSize) {
331 HLTWarning("data type origin %s is too long, truncated to %d", origin, kAliHLTComponentDataTypefOriginSize);
333 strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
336 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
338 // see header file for function documentation
339 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
340 evtData.fStructSize=sizeof(AliHLTComponentEventData);
343 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
345 // see header file for function documentation
347 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
348 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
349 if (dt.fID[i]!=0) msg+=dt.fID[i];
353 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
354 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
358 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
361 int AliHLTComponent::GetEventCount() const
363 // see header file for function documentation
367 int AliHLTComponent::IncrementEventCounter()
369 // see header file for function documentation
370 if (fEventCount>=0) fEventCount++;
374 int AliHLTComponent::GetNumberOfInputBlocks()
376 // see header file for function documentation
377 if (fpInputBlocks!=NULL) {
378 return fCurrentEventData.fBlockCnt;
383 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
384 const char* classname,
387 // see header file for function documentation
389 if (classname) fClassName=classname;
390 else fClassName.clear();
391 int idx=FindInputBlock(fSearchDataType, 0);
392 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
395 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
396 fCurrentInputBlock=idx;
403 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
404 const char* dtOrigin,
405 const char* classname,
408 // see header file for function documentation
409 AliHLTComponentDataType dt;
410 SetDataType(dt, dtID, dtOrigin);
411 return GetFirstInputObject(dt, classname, bForce);
414 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
416 // see header file for function documentation
417 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
418 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
421 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
422 fCurrentInputBlock=idx;
428 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx)
430 // see header file for function documentation
432 if (fpInputBlocks!=NULL) {
433 int idx=startIdx<0?0:startIdx;
434 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
435 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
443 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
445 // see header file for function documentation
447 if (fpInputBlocks!=NULL) {
448 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
449 if (fpInputBlocks[idx].fPtr) {
450 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
451 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
452 //HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
453 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
454 pObj=msg.ReadObject(msg.GetClass());
455 if (pObj && msg.GetClass()) {
456 //HLTDebug("object %p type %s created", pObj, msg.GetClass()->GetName());
460 // } else if (bForce!=0) {
461 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
464 HLTFatal("block descriptor empty");
467 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
470 HLTError("no input blocks available");
476 TObject* AliHLTComponent::GetInputObject(int idx, const char* classname, int bForce)
478 // see header file for function documentation
479 if (fpInputObjects==NULL) {
480 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
483 if (fpInputObjects) {
484 pObj=fpInputObjects->At(idx);
486 pObj=CreateInputObject(idx, bForce);
488 fpInputObjects->AddAt(pObj, idx);
492 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
497 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
499 // see header file for function documentation
500 AliHLTComponentDataType dt=kAliHLTVoidDataType;
501 int idx=fCurrentInputBlock;
503 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
505 HLTError("unknown object %p", pObject);
509 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
510 dt=fpInputBlocks[idx].fDataType;
512 HLTFatal("severe internal error, index out of range");
518 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
520 // see header file for function documentation
521 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
522 int idx=fCurrentInputBlock;
524 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
526 HLTError("unknown object %p", pObject);
530 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
531 iSpec=fpInputBlocks[idx].fSpecification;
533 HLTFatal("severe internal error, index out of range");
539 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
541 // see header file for function documentation
544 int idx=FindInputBlock(fSearchDataType, 0);
545 const AliHLTComponentBlockData* pBlock=NULL;
547 // check for fpInputBlocks pointer done in FindInputBlock
548 pBlock=&fpInputBlocks[idx];
553 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
554 const char* dtOrigin)
556 // see header file for function documentation
557 AliHLTComponentDataType dt;
558 SetDataType(dt, dtID, dtOrigin);
559 return GetFirstInputBlock(dt);
562 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
564 // see header file for function documentation
565 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
566 const AliHLTComponentBlockData* pBlock=NULL;
568 // check for fpInputBlocks pointer done in FindInputBlock
569 pBlock=&fpInputBlocks[idx];
574 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock)
576 // see header file for function documentation
578 if (fpInputBlocks!=NULL) {
580 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
581 iResult=(int)(pBlock-fpInputBlocks);
590 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
592 // see header file for function documentation
593 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
594 int idx=fCurrentInputBlock;
596 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
598 HLTError("unknown Block %p", pBlock);
602 // check for fpInputBlocks pointer done in FindInputBlock
603 iSpec=fpInputBlocks[idx].fSpecification;
608 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
610 // see header file for function documentation
613 AliHLTMessage msg(kMESS_OBJECT);
614 msg.WriteObject(pObject);
615 Int_t iMsgLength=msg.Length();
617 msg.SetLength(); // sets the length to the first (reserved) word
618 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec);
620 //HLTDebug("object %s (%p) inserted to output", pObject->ClassName(), pObject);
623 HLTError("object serialization failed for object %p", pObject);
632 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
634 // see header file for function documentation
635 AliHLTComponentDataType dt;
636 SetDataType(dt, dtID, dtOrigin);
637 return PushBack(pObject, dt, spec);
640 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
642 // see header file for function documentation
643 return InsertOutputBlock(pBuffer, iSize, dt, spec);
646 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
648 // see header file for function documentation
649 AliHLTComponentDataType dt;
650 SetDataType(dt, dtID, dtOrigin);
651 return PushBack(pBuffer, iSize, dt, spec);
654 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
656 // see header file for function documentation
659 if (fpOutputBuffer && (fOutputBufferSize-fOutputBufferFilled)) {
660 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
661 AliHLTComponentBlockData bd;
663 bd.fOffset = fOutputBufferFilled;
667 bd.fSpecification = spec;
668 if (pBuffer!=NULL && pBuffer!=pTgt) {
669 memcpy(pTgt, pBuffer, iSize);
670 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
671 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iSize, pBuffer, pTgt, firstWord);
673 fOutputBufferFilled+=bd.fSize;
674 fOutputBlocks.push_back( bd );
675 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iSize, DataType2Text(dt).c_str(), spec);
677 if (fpOutputBuffer) {
678 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iSize);
680 HLTError("output buffer not available");
690 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
692 // see header file for function documentation
694 //#warning function not yet implemented
695 HLTWarning("function not yet implemented");
699 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
700 const AliHLTComponentBlockData* blocks,
701 AliHLTComponentTriggerData& trigData,
702 AliHLTUInt8_t* outputPtr,
703 AliHLTUInt32_t& size,
704 AliHLTUInt32_t& outputBlockCnt,
705 AliHLTComponentBlockData*& outputBlocks,
706 AliHLTComponentEventDoneData*& edd )
708 // see header file for function documentation
710 fCurrentEvent=evtData.fEventID;
711 fCurrentEventData=evtData;
712 fpInputBlocks=blocks;
713 fCurrentInputBlock=-1;
714 fSearchDataType=kAliHLTAnyDataType;
715 fpOutputBuffer=outputPtr;
716 fOutputBufferSize=size;
717 fOutputBufferFilled=0;
718 fOutputBlocks.clear();
720 vector<AliHLTComponentBlockData> blockData;
721 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
723 if (fOutputBlocks.size()>0) {
724 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
725 if (blockData.size()>0) {
726 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
729 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
730 size=fOutputBufferFilled;
733 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
736 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
743 IncrementEventCounter();