3 /**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
9 * for The ALICE HLT Project. *
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 **************************************************************************/
20 /** @file AliHLTComponent.cxx
21 @author Matthias Richter, Timm Steinbeck
23 @brief Base class implementation for HLT components. */
29 //#include "AliHLTStdIncludes.h"
30 #include "AliHLTComponent.h"
31 #include "AliHLTComponentHandler.h"
32 #include "AliHLTMessage.h"
34 #include "TObjArray.h"
35 #include "TObjectTable.h"
37 #include "TStopwatch.h"
38 #include "AliHLTMemoryFile.h"
40 /** ROOT macro for the implementation of ROOT specific class methods */
41 ClassImp(AliHLTComponent);
43 /** stopwatch macro using the stopwatch guard */
44 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
45 //#define ALIHLTCOMPONENT_STOPWATCH(type)
47 /** stopwatch macro for operations of the base class */
48 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
49 /** stopwatch macro for operations of the detector algorithm (DA) */
50 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
52 AliHLTComponent::AliHLTComponent()
60 fCurrentInputBlock(-1),
61 fSearchDataType(kAliHLTVoidDataType),
66 fOutputBufferFilled(0),
68 fpStopwatches(new TObjArray(kSWTypeCount)),
74 // see header file for class documentation
76 // refer to README to build package
78 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
79 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
80 if (fgpComponentHandler)
81 fgpComponentHandler->ScheduleRegister(this);
82 //SetLocalLoggingLevel(kHLTLogDefault);
85 AliHLTComponent::AliHLTComponent(const AliHLTComponent&)
94 fCurrentInputBlock(-1),
95 fSearchDataType(kAliHLTVoidDataType),
100 fOutputBufferFilled(0),
107 // see header file for class documentation
110 AliHLTComponent& AliHLTComponent::operator=(const AliHLTComponent&)
112 // see header file for class documentation
116 AliHLTComponent::~AliHLTComponent()
118 // see header file for function documentation
119 CleanupInputObjects();
120 if (fpStopwatches!=NULL) delete fpStopwatches;
122 vector<AliHLTMemoryFile*>::iterator element=fMemFiles.begin();
123 while (element!=fMemFiles.end()) {
125 if ((*element)->IsClosed()==0) {
126 HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
127 // close but do not flush as we dont know whether the buffer is still valid
128 (*element)->Close(0);
137 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
139 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
141 // see header file for function documentation
143 if (fgpComponentHandler==NULL || bOverwrite!=0)
144 fgpComponentHandler=pCH;
150 int AliHLTComponent::UnsetGlobalComponentHandler()
152 // see header file for function documentation
153 return SetGlobalComponentHandler(NULL,1);
156 int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
158 // see header file for function documentation
161 memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
162 fEnvironment.fParam=environParam;
164 const char** pArguments=NULL;
169 pArguments=new const char*[argc];
171 for (int i=0; i<argc && iResult>=0; i++) {
173 if (argument.IsNull()) continue;
176 if (argument.CompareTo("benchmark")==0) {
179 } else if (argument.CompareTo("loglevel")==0) {
180 if ((bMissingParam=(++i>=argc))) break;
181 TString parameter(argv[i]);
182 parameter.Remove(TString::kLeading, ' '); // remove all blanks
183 if (parameter.BeginsWith("0x") &&
184 parameter.Replace(0,2,"",0).IsHex()) {
185 AliHLTComponentLogSeverity loglevel=kHLTLogNone;
186 sscanf(parameter.Data(),"%x", (unsigned int*)&loglevel);
187 SetLocalLoggingLevel(loglevel);
189 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
193 pArguments[iNofChildArgs++]=argv[i];
201 HLTError("missing parameter for argument %s", argument.Data());
205 iResult=DoInit(iNofChildArgs, pArguments);
207 if (iResult>=0) fEventCount=0;
208 if (pArguments) delete [] pArguments;
212 int AliHLTComponent::Deinit()
214 // see header file for function documentation
218 HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
219 AliHLTRunDesc* pRunDesc=fpRunDesc;
226 int AliHLTComponent::DoInit( int argc, const char** argv )
228 // see header file for function documentation
229 if (argc==0 && argv==NULL) {
230 // this is currently just to get rid of the warning "unused parameter"
236 int AliHLTComponent::DoDeinit()
238 // see header file for function documentation
243 int AliHLTComponent::GetOutputDataTypes(vector<AliHLTComponentDataType>& /*tgtList*/)
248 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
250 // see header file for function documentation
251 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
252 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
253 strcat( output, ":" );
254 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
257 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type )
259 // see header file for function documentation
262 if (type==kAliHLTVoidDataType) {
265 // some gymnastics in order to avoid a '0' which is part of either or both
266 // ID and origin terminating the whole string. Unfortunately, string doesn't
267 // stop appending at the '0' if the number of elements to append was
268 // explicitely specified
270 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
271 out.append(tmp.c_str());
274 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
275 out.append(tmp.c_str());
281 void* AliHLTComponent::AllocMemory( unsigned long size )
283 // see header file for function documentation
284 if (fEnvironment.fAllocMemoryFunc)
285 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
286 HLTFatal("no memory allocation handler registered");
290 int AliHLTComponent::MakeOutputDataBlockList( const vector<AliHLTComponentBlockData>& blocks, AliHLTUInt32_t* blockCount,
291 AliHLTComponentBlockData** outputBlocks )
293 // see header file for function documentation
294 if ( blockCount==NULL || outputBlocks==NULL )
296 AliHLTUInt32_t count = blocks.size();
300 *outputBlocks = NULL;
303 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
304 if ( !*outputBlocks )
306 for ( unsigned long i = 0; i < count; i++ ) {
307 (*outputBlocks)[i] = blocks[i];
308 if (blocks[i].fDataType==kAliHLTAnyDataType) {
309 (*outputBlocks)[i].fDataType=GetOutputDataType();
310 /* data type was set to the output data type by the PubSub AliRoot
311 Wrapper component, if data type of the block was ********:****.
312 Now handled by the component base class in order to have same
313 behavior when running embedded in AliRoot
314 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
315 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
324 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
326 // see header file for function documentation
327 if (fEnvironment.fGetEventDoneDataFunc)
328 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
332 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
334 // see header file for function documentation
337 vector<AliHLTComponentDataType> ctlist;
338 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(ctlist);
339 vector<AliHLTComponentDataType>::iterator type=ctlist.begin();
340 //AliHLTComponentDataType ouptdt=GetOutputDataType();
341 //PrintDataTypeContent(ouptdt, "publisher \'%s\'");
342 while (type!=ctlist.end() && iResult==0) {
343 //PrintDataTypeContent((*type), "consumer \'%s\'");
344 if ((*type)==GetOutputDataType() ||
345 (*type)==kAliHLTAnyDataType) {
346 if (tgtList) tgtList->push_back(*type);
348 // this loop has to be changed in case of multiple output types
359 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
361 // see header file for function documentation
362 const char* fmt="publisher \'%s\'";
363 if (format) fmt=format;
364 HLTMessage(fmt, (DataType2Text(dt)).c_str());
365 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
380 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData ) const
382 // see header file for function documentation
383 blockData.fStructSize = sizeof(blockData);
384 FillShmData( blockData.fShmKey );
385 blockData.fOffset = ~(AliHLTUInt32_t)0;
386 blockData.fPtr = NULL;
388 FillDataType( blockData.fDataType );
389 blockData.fSpecification = kAliHLTVoidDataSpec;
392 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData ) const
394 // see header file for function documentation
395 shmData.fStructSize = sizeof(shmData);
396 shmData.fShmType = gkAliHLTComponentInvalidShmType;
397 shmData.fShmID = gkAliHLTComponentInvalidShmID;
400 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType ) const
402 // see header file for function documentation
403 dataType=kAliHLTAnyDataType;
406 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
408 // see header file for function documentation
409 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
410 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
413 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
415 // see header file for function documentation
416 tgtdt.fStructSize = sizeof(AliHLTComponentDataType);
417 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
418 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
420 if ((int)strlen(id)>kAliHLTComponentDataTypefIDsize) {
421 HLTWarning("data type id %s is too long, truncated to %d", id, kAliHLTComponentDataTypefIDsize);
423 strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
425 if ((int)strlen(origin)>kAliHLTComponentDataTypefOriginSize) {
426 HLTWarning("data type origin %s is too long, truncated to %d", origin, kAliHLTComponentDataTypefOriginSize);
428 strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
431 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
433 // see header file for function documentation
434 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
435 evtData.fStructSize=sizeof(AliHLTComponentEventData);
438 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
440 // see header file for function documentation
442 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
443 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
444 if (dt.fID[i]!=0) msg+=dt.fID[i];
448 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
449 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
453 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
456 int AliHLTComponent::GetEventCount() const
458 // see header file for function documentation
462 int AliHLTComponent::IncrementEventCounter()
464 // see header file for function documentation
465 if (fEventCount>=0) fEventCount++;
469 int AliHLTComponent::GetNumberOfInputBlocks() const
471 // see header file for function documentation
472 if (fpInputBlocks!=NULL) {
473 return fCurrentEventData.fBlockCnt;
478 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
479 const char* classname,
482 // see header file for function documentation
483 ALIHLTCOMPONENT_BASE_STOPWATCH();
485 if (classname) fClassName=classname;
486 else fClassName.clear();
487 int idx=FindInputBlock(fSearchDataType, 0, 1);
490 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
491 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
492 fCurrentInputBlock=idx;
499 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
500 const char* dtOrigin,
501 const char* classname,
504 // see header file for function documentation
505 ALIHLTCOMPONENT_BASE_STOPWATCH();
506 AliHLTComponentDataType dt;
507 SetDataType(dt, dtID, dtOrigin);
508 return GetFirstInputObject(dt, classname, bForce);
511 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
513 // see header file for function documentation
514 ALIHLTCOMPONENT_BASE_STOPWATCH();
515 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
516 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
519 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
520 fCurrentInputBlock=idx;
526 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
528 // see header file for function documentation
530 if (fpInputBlocks!=NULL) {
531 int idx=startIdx<0?0:startIdx;
532 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
534 if (fpInputBlocks[idx].fPtr==NULL) continue;
535 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
536 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
538 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
546 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
548 // see header file for function documentation
550 if (fpInputBlocks!=NULL) {
551 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
552 if (fpInputBlocks[idx].fPtr) {
553 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
554 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
555 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
556 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
557 TClass* objclass=msg.GetClass();
558 pObj=msg.ReadObject(objclass);
559 if (pObj && objclass) {
560 HLTDebug("object %p type %s created", pObj, objclass->GetName());
564 } else if (bForce!=0) {
565 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
568 HLTFatal("block descriptor empty");
571 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
574 HLTError("no input blocks available");
580 TObject* AliHLTComponent::GetInputObject(int idx, const char* classname, int bForce)
582 // see header file for function documentation
583 if (fpInputObjects==NULL) {
584 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
587 if (fpInputObjects) {
588 pObj=fpInputObjects->At(idx);
590 pObj=CreateInputObject(idx, bForce);
592 fpInputObjects->AddAt(pObj, idx);
596 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
601 int AliHLTComponent::CleanupInputObjects()
603 // see header file for function documentation
604 if (!fpInputObjects) return 0;
605 TObjArray* array=fpInputObjects;
607 for (int i=0; i<array->GetEntries(); i++) {
608 TObject* pObj=array->At(i);
609 // grrr, garbage collection strikes back: When read via AliHLTMessage
610 // (CreateInputObject), and written to a TFile afterwards, the
611 // TFile::Close calls ROOOT's garbage collection. No clue why the
612 // object ended up in the key list and needs to be deleted
613 if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
619 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
621 // see header file for function documentation
622 ALIHLTCOMPONENT_BASE_STOPWATCH();
623 AliHLTComponentDataType dt=kAliHLTVoidDataType;
624 int idx=fCurrentInputBlock;
626 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
628 HLTError("unknown object %p", pObject);
632 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
633 dt=fpInputBlocks[idx].fDataType;
635 HLTFatal("severe internal error, index out of range");
641 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
643 // see header file for function documentation
644 ALIHLTCOMPONENT_BASE_STOPWATCH();
645 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
646 int idx=fCurrentInputBlock;
648 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
650 HLTError("unknown object %p", pObject);
654 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
655 iSpec=fpInputBlocks[idx].fSpecification;
657 HLTFatal("severe internal error, index out of range");
663 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
665 // see header file for function documentation
666 ALIHLTCOMPONENT_BASE_STOPWATCH();
669 int idx=FindInputBlock(fSearchDataType, 0);
670 const AliHLTComponentBlockData* pBlock=NULL;
672 // check for fpInputBlocks pointer done in FindInputBlock
673 pBlock=&fpInputBlocks[idx];
674 fCurrentInputBlock=idx;
679 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
680 const char* dtOrigin)
682 // see header file for function documentation
683 ALIHLTCOMPONENT_BASE_STOPWATCH();
684 AliHLTComponentDataType dt;
685 SetDataType(dt, dtID, dtOrigin);
686 return GetFirstInputBlock(dt);
689 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
691 // see header file for function documentation
692 ALIHLTCOMPONENT_BASE_STOPWATCH();
693 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
694 const AliHLTComponentBlockData* pBlock=NULL;
696 // check for fpInputBlocks pointer done in FindInputBlock
697 pBlock=&fpInputBlocks[idx];
698 fCurrentInputBlock=idx;
703 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
705 // see header file for function documentation
707 if (fpInputBlocks!=NULL) {
709 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
710 iResult=(int)(pBlock-fpInputBlocks);
719 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
721 // see header file for function documentation
722 ALIHLTCOMPONENT_BASE_STOPWATCH();
723 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
724 int idx=fCurrentInputBlock;
726 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
728 HLTError("unknown Block %p", pBlock);
732 // check for fpInputBlocks pointer done in FindInputBlock
733 iSpec=fpInputBlocks[idx].fSpecification;
738 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
739 void* pHeader, int headerSize)
741 // see header file for function documentation
742 ALIHLTCOMPONENT_BASE_STOPWATCH();
745 AliHLTMessage msg(kMESS_OBJECT);
746 msg.WriteObject(pObject);
747 Int_t iMsgLength=msg.Length();
749 msg.SetLength(); // sets the length to the first (reserved) word
750 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
752 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
755 HLTError("object serialization failed for object %p", pObject);
764 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
765 void* pHeader, int headerSize)
767 // see header file for function documentation
768 ALIHLTCOMPONENT_BASE_STOPWATCH();
769 AliHLTComponentDataType dt;
770 SetDataType(dt, dtID, dtOrigin);
771 return PushBack(pObject, dt, spec, pHeader, headerSize);
774 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
775 void* pHeader, int headerSize)
777 // see header file for function documentation
778 ALIHLTCOMPONENT_BASE_STOPWATCH();
779 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
782 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
783 void* pHeader, int headerSize)
785 // see header file for function documentation
786 ALIHLTCOMPONENT_BASE_STOPWATCH();
787 AliHLTComponentDataType dt;
788 SetDataType(dt, dtID, dtOrigin);
789 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
792 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
793 void* pHeader, int iHeaderSize)
795 // see header file for function documentation
797 int iBlkSize = iBufferSize + iHeaderSize;
799 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
800 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
801 AliHLTComponentBlockData bd;
803 bd.fOffset = fOutputBufferFilled;
807 bd.fSpecification = spec;
808 if (pHeader!=NULL && pHeader!=pTgt) {
809 memcpy(pTgt, pHeader, iHeaderSize);
812 pTgt += (AliHLTUInt8_t) iHeaderSize;
814 if (pBuffer!=NULL && pBuffer!=pTgt) {
815 memcpy(pTgt, pBuffer, iBufferSize);
817 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
818 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
820 fOutputBufferFilled+=bd.fSize;
821 fOutputBlocks.push_back( bd );
822 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
824 if (fpOutputBuffer) {
825 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
827 HLTError("output buffer not available");
837 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
839 // see header file for function documentation
840 if (!pObject) return -EINVAL;
841 AliHLTMessage msg(kMESS_OBJECT);
842 msg.WriteObject(pObject);
846 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
847 const char* dtOrigin,
850 // see header file for function documentation
851 ALIHLTCOMPONENT_BASE_STOPWATCH();
852 AliHLTComponentDataType dt;
853 SetDataType(dt, dtID, dtOrigin);
854 return CreateMemoryFile(capacity, dt, spec);
857 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
858 const AliHLTComponentDataType& dt,
861 // see header file for function documentation
862 ALIHLTCOMPONENT_BASE_STOPWATCH();
863 AliHLTMemoryFile* pFile=NULL;
864 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
865 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
866 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
868 unsigned int nofBlocks=fOutputBlocks.size();
869 if (nofBlocks+1>fMemFiles.size()) {
870 fMemFiles.resize(nofBlocks+1, NULL);
872 if (nofBlocks<fMemFiles.size()) {
873 fMemFiles[nofBlocks]=pFile;
874 AliHLTComponentBlockData bd;
876 bd.fOffset = fOutputBufferFilled;
880 bd.fSpecification = spec;
881 fOutputBufferFilled+=bd.fSize;
882 fOutputBlocks.push_back( bd );
884 HLTError("can not allocate/grow object array");
891 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
896 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
897 const char* dtOrigin,
901 // see header file for function documentation
902 ALIHLTCOMPONENT_BASE_STOPWATCH();
903 AliHLTComponentDataType dt;
904 SetDataType(dt, dtID, dtOrigin);
905 int size=fOutputBufferSize-fOutputBufferFilled;
906 if (capacity<0 || capacity>1.0) {
907 HLTError("invalid parameter: capacity %f", capacity);
910 size=(int)(size*capacity);
911 return CreateMemoryFile(size, dt, spec);
914 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
918 // see header file for function documentation
919 ALIHLTCOMPONENT_BASE_STOPWATCH();
920 int size=fOutputBufferSize-fOutputBufferFilled;
921 if (capacity<0 || capacity>1.0) {
922 HLTError("invalid parameter: capacity %f", capacity);
925 size=(int)(size*capacity);
926 return CreateMemoryFile(size, dt, spec);
929 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
930 const char* key, int option)
933 if (pFile && pObject) {
935 iResult=pObject->Write(key, option);
939 iResult=-pFile->GetErrno();
940 if (iResult==-ENOSPC) {
941 HLTError("error writing memory file, buffer too small");
950 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
954 vector<AliHLTMemoryFile*>::iterator element=fMemFiles.begin();
956 while (element!=fMemFiles.end() && iResult>=0) {
957 if (*element && *element==pFile) {
958 iResult=pFile->Close();
960 // sync memory files and descriptors
962 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
970 HLTError("can not find memory file %p", pFile);
978 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
980 // see header file for function documentation
982 //#warning function not yet implemented
983 HLTWarning("function not yet implemented");
987 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
988 const AliHLTComponentBlockData* blocks,
989 AliHLTComponentTriggerData& trigData,
990 AliHLTUInt8_t* outputPtr,
991 AliHLTUInt32_t& size,
992 AliHLTUInt32_t& outputBlockCnt,
993 AliHLTComponentBlockData*& outputBlocks,
994 AliHLTComponentEventDoneData*& edd )
996 // see header file for function documentation
997 ALIHLTCOMPONENT_BASE_STOPWATCH();
999 fCurrentEvent=evtData.fEventID;
1000 fCurrentEventData=evtData;
1001 fpInputBlocks=blocks;
1002 fCurrentInputBlock=-1;
1003 fSearchDataType=kAliHLTAnyDataType;
1004 fpOutputBuffer=outputPtr;
1005 fOutputBufferSize=size;
1006 fOutputBufferFilled=0;
1007 fOutputBlocks.clear();
1009 // find special events
1010 if (fpInputBlocks) {
1011 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1012 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1014 if (fpRunDesc==NULL) {
1015 fpRunDesc=new AliHLTRunDesc;
1017 if ((iResult=CopyStruct(fpRunDesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1018 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1024 HLTWarning("already received SOR event run no %d, ignoring SOR", fpRunDesc->fRunNo);
1026 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1027 if (fpRunDesc!=NULL) {
1029 AliHLTRunDesc rundesc;
1030 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1031 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1032 HLTWarning("run no missmatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1034 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1037 AliHLTRunDesc* pRunDesc=fpRunDesc;
1042 HLTWarning("did not receive SOR, ignoring EOR");
1045 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1051 vector<AliHLTComponentBlockData> blockData;
1052 { // dont delete, sets the scope for the stopwatch guard
1053 ALIHLTCOMPONENT_DA_STOPWATCH();
1054 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1055 } // end of the scope of the stopwatch guard
1057 if (fOutputBlocks.size()>0) {
1058 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1060 // sync memory files and descriptors
1061 vector<AliHLTMemoryFile*>::iterator element=fMemFiles.begin();
1063 while (element!=fMemFiles.end() && iResult>=0) {
1065 if ((*element)->IsClosed()==0) {
1066 HLTWarning("memory file has not been closed, force flush");
1067 iResult=CloseMemoryFile(*element);
1074 // create the descriptor list
1075 if (blockData.size()>0) {
1076 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1079 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1080 size=fOutputBufferFilled;
1084 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1087 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1094 CleanupInputObjects();
1095 IncrementEventCounter();
1099 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1104 // standard constructor (not for use)
1107 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1109 fpStopwatch(pStopwatch),
1114 // check for already existing guard
1115 if (fgpCurrent) fpPrec=fgpCurrent;
1118 // stop the preceeding guard if it controls a different stopwatch
1120 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1122 // start the stopwatch if the current guard controls a different one
1123 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1126 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
1132 // copy constructor not for use
1136 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1139 // assignment operator not for use
1146 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1148 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1152 // resume the preceeding guard if it controls a different stopwatch
1154 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1156 // stop the stopwatch if the current guard controls a different one
1157 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1159 // resume to the preceeding guard
1163 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1165 // see header file for function documentation
1166 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1167 return fpStopwatch!=pSucc?1:0;
1170 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1172 // see header file for function documentation
1173 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1174 return fpStopwatch!=pSucc?1:0;
1177 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
1179 // see header file for function documentation
1181 if (pSW!=NULL && type<kSWTypeCount) {
1182 if (fpStopwatches) {
1183 TObject* pObj=fpStopwatches->At((int)type);
1184 if (pSW==NULL // explicit reset
1185 || pObj==NULL) { // explicit set
1186 fpStopwatches->AddAt(pSW, (int)type);
1187 } else if (pObj!=pSW) {
1188 HLTWarning("stopwatch %d already set, reset first", (int)type);
1198 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1200 // see header file for function documentation
1201 if (pStopwatches==NULL) return -EINVAL;
1204 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1205 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1209 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1211 if (fpRunDesc==NULL) return 0;
1212 return fpRunDesc->fRunNo;
1215 AliHLTUInt32_t AliHLTComponent::GetRunType() const
1217 if (fpRunDesc==NULL) return 0;
1218 return fpRunDesc->fRunType;
1221 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
1222 const char* structname, const char* eventname)
1225 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1226 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1227 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1228 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
1229 AliHLTUInt8_t* pSrc=((AliHLTUInt8_t*)fpInputBlocks[iBlockNo].fPtr)+fpInputBlocks[iBlockNo].fOffset;
1230 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)pSrc);
1231 if (fpInputBlocks[iBlockNo].fSize!=copy) {
1232 HLTWarning("%s event: missmatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1233 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1235 if (copy!=iStructSize) {
1236 HLTWarning("%s event: missmatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1237 if (copy>iStructSize) {
1240 memset(pTgt, 0, iStructSize);
1243 memcpy(pTgt, pSrc, copy);
1247 HLTWarning("%s event: missing data block", eventname);
1253 HLTError("invalid struct");