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. */
25 // see header file for class documentation
27 // refer to README to build package
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
35 //#include "AliHLTStdIncludes.h"
36 #include "AliHLTComponent.h"
37 #include "AliHLTComponentHandler.h"
38 #include "AliHLTMessage.h"
40 #include "TObjArray.h"
41 #include "TObjectTable.h"
43 #include "TStopwatch.h"
44 #include "AliHLTMemoryFile.h"
47 /** ROOT macro for the implementation of ROOT specific class methods */
48 ClassImp(AliHLTComponent);
50 /** stopwatch macro using the stopwatch guard */
51 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
52 //#define ALIHLTCOMPONENT_STOPWATCH(type)
54 /** stopwatch macro for operations of the base class */
55 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
56 /** stopwatch macro for operations of the detector algorithm (DA) */
57 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
59 AliHLTComponent::AliHLTComponent()
67 fCurrentInputBlock(-1),
68 fSearchDataType(kAliHLTVoidDataType),
73 fOutputBufferFilled(0),
75 fpStopwatches(new TObjArray(kSWTypeCount)),
81 // see header file for class documentation
83 // refer to README to build package
85 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
86 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
87 if (fgpComponentHandler)
88 fgpComponentHandler->ScheduleRegister(this);
89 //SetLocalLoggingLevel(kHLTLogDefault);
92 AliHLTComponent::~AliHLTComponent()
94 // see header file for function documentation
95 CleanupInputObjects();
96 if (fpStopwatches!=NULL) delete fpStopwatches;
98 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
99 while (element!=fMemFiles.end()) {
101 if ((*element)->IsClosed()==0) {
102 HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
103 // close but do not flush as we dont know whether the buffer is still valid
104 (*element)->CloseMemoryFile(0);
113 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
115 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
117 // see header file for function documentation
119 if (fgpComponentHandler==NULL || bOverwrite!=0)
120 fgpComponentHandler=pCH;
126 int AliHLTComponent::UnsetGlobalComponentHandler()
128 // see header file for function documentation
129 return SetGlobalComponentHandler(NULL,1);
132 int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
134 // see header file for function documentation
137 memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
138 fEnvironment.fParam=environParam;
140 const char** pArguments=NULL;
145 pArguments=new const char*[argc];
147 for (int i=0; i<argc && iResult>=0; i++) {
149 if (argument.IsNull()) continue;
152 if (argument.CompareTo("benchmark")==0) {
155 } else if (argument.CompareTo("loglevel")==0) {
156 if ((bMissingParam=(++i>=argc))) break;
157 TString parameter(argv[i]);
158 parameter.Remove(TString::kLeading, ' '); // remove all blanks
159 if (parameter.BeginsWith("0x") &&
160 parameter.Replace(0,2,"",0).IsHex()) {
161 AliHLTComponentLogSeverity loglevel=kHLTLogNone;
162 sscanf(parameter.Data(),"%x", (unsigned int*)&loglevel);
163 SetLocalLoggingLevel(loglevel);
165 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
169 pArguments[iNofChildArgs++]=argv[i];
177 HLTError("missing parameter for argument %s", argument.Data());
181 iResult=DoInit(iNofChildArgs, pArguments);
183 if (iResult>=0) fEventCount=0;
184 if (pArguments) delete [] pArguments;
188 int AliHLTComponent::Deinit()
190 // see header file for function documentation
194 HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
195 AliHLTRunDesc* pRunDesc=fpRunDesc;
202 int AliHLTComponent::DoInit( int argc, const char** argv )
204 // see header file for function documentation
205 if (argc==0 && argv==NULL) {
206 // this is currently just to get rid of the warning "unused parameter"
212 int AliHLTComponent::DoDeinit()
214 // see header file for function documentation
219 int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
221 HLTLogKeyword("dummy");
225 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
227 // see header file for function documentation
228 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
229 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
230 strcat( output, ":" );
231 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
234 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
236 // see header file for function documentation
242 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
243 sprintf(tmp, "'%d", type.fOrigin[i]);
247 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
248 sprintf(tmp, "%d'", type.fID[i]);
257 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
258 unsigned char* puc=(unsigned char*)type.fOrigin;
260 sprintf(tmp, "'\\%x", type.fOrigin[i]);
262 sprintf(tmp, "'%c", type.fOrigin[i]);
266 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
267 unsigned char* puc=(unsigned char*)type.fID;
269 sprintf(tmp, "\\%x'", type.fID[i]);
271 sprintf(tmp, "%c'", type.fID[i]);
277 if (type==kAliHLTVoidDataType) {
280 // some gymnastics in order to avoid a '0' which is part of either or both
281 // ID and origin terminating the whole string. Unfortunately, string doesn't
282 // stop appending at the '0' if the number of elements to append was
283 // explicitely specified
285 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
286 out.append(tmp.c_str());
289 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
290 out.append(tmp.c_str());
296 void* AliHLTComponent::AllocMemory( unsigned long size )
298 // see header file for function documentation
299 if (fEnvironment.fAllocMemoryFunc)
300 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
301 HLTFatal("no memory allocation handler registered");
305 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
306 AliHLTComponentBlockData** outputBlocks )
308 // see header file for function documentation
309 if ( blockCount==NULL || outputBlocks==NULL )
311 AliHLTUInt32_t count = blocks.size();
315 *outputBlocks = NULL;
318 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
319 if ( !*outputBlocks )
321 for ( unsigned long i = 0; i < count; i++ ) {
322 (*outputBlocks)[i] = blocks[i];
323 if (blocks[i].fDataType==kAliHLTAnyDataType) {
324 (*outputBlocks)[i].fDataType=GetOutputDataType();
325 /* data type was set to the output data type by the PubSub AliRoot
326 Wrapper component, if data type of the block was ********:****.
327 Now handled by the component base class in order to have same
328 behavior when running embedded in AliRoot
329 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
330 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
339 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
341 // see header file for function documentation
342 if (fEnvironment.fGetEventDoneDataFunc)
343 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
347 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
349 // see header file for function documentation
352 AliHLTComponentDataTypeList itypes;
353 AliHLTComponentDataTypeList otypes;
354 otypes.push_back(GetOutputDataType());
355 if (otypes[0]==kAliHLTMultipleDataType) {
358 if ((count=GetOutputDataTypes(otypes))>0) {
359 } else if (GetComponentType()!=kSink) {
360 HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
363 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(itypes);
364 AliHLTComponentDataTypeList::iterator itype=itypes.begin();
365 while (itype!=itypes.end()) {
366 //PrintDataTypeContent((*itype), "consumer \'%s\'");
367 AliHLTComponentDataTypeList::iterator otype=otypes.begin();
368 while (otype!=otypes.end() && (*itype)!=(*otype)) otype++;
369 //if (otype!=otypes.end()) PrintDataTypeContent(*otype, "publisher \'%s\'");
370 if (otype!=otypes.end() ||
371 (*itype)==kAliHLTAnyDataType) {
372 if (tgtList) tgtList->push_back(*itype);
383 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
385 // see header file for function documentation
386 const char* fmt="publisher \'%s\'";
387 if (format) fmt=format;
388 HLTMessage(fmt, (DataType2Text(dt)).c_str());
389 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
404 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
406 // see header file for function documentation
407 blockData.fStructSize = sizeof(blockData);
408 FillShmData( blockData.fShmKey );
409 blockData.fOffset = ~(AliHLTUInt32_t)0;
410 blockData.fPtr = NULL;
412 FillDataType( blockData.fDataType );
413 blockData.fSpecification = kAliHLTVoidDataSpec;
416 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
418 // see header file for function documentation
419 shmData.fStructSize = sizeof(shmData);
420 shmData.fShmType = gkAliHLTComponentInvalidShmType;
421 shmData.fShmID = gkAliHLTComponentInvalidShmID;
424 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
426 // see header file for function documentation
427 dataType=kAliHLTAnyDataType;
430 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
432 // see header file for function documentation
433 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
434 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
437 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
439 // see header file for function documentation
440 tgtdt=kAliHLTVoidDataType;
441 strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
442 strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
445 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
447 // see header file for function documentation
448 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
449 evtData.fStructSize=sizeof(AliHLTComponentEventData);
452 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
454 // see header file for function documentation
456 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
457 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
458 if (dt.fID[i]!=0) msg+=dt.fID[i];
462 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
463 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
467 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
470 int AliHLTComponent::GetEventCount() const
472 // see header file for function documentation
476 int AliHLTComponent::IncrementEventCounter()
478 // see header file for function documentation
479 if (fEventCount>=0) fEventCount++;
483 int AliHLTComponent::GetNumberOfInputBlocks() const
485 // see header file for function documentation
486 if (fpInputBlocks!=NULL) {
487 return fCurrentEventData.fBlockCnt;
492 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
493 const char* classname,
496 // see header file for function documentation
497 ALIHLTCOMPONENT_BASE_STOPWATCH();
499 if (classname) fClassName=classname;
500 else fClassName.clear();
501 int idx=FindInputBlock(fSearchDataType, 0, 1);
504 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
505 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
506 fCurrentInputBlock=idx;
513 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
514 const char* dtOrigin,
515 const char* classname,
518 // see header file for function documentation
519 ALIHLTCOMPONENT_BASE_STOPWATCH();
520 AliHLTComponentDataType dt;
521 SetDataType(dt, dtID, dtOrigin);
522 return GetFirstInputObject(dt, classname, bForce);
525 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
527 // see header file for function documentation
528 ALIHLTCOMPONENT_BASE_STOPWATCH();
529 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
530 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
533 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
534 fCurrentInputBlock=idx;
540 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
542 // see header file for function documentation
544 if (fpInputBlocks!=NULL) {
545 int idx=startIdx<0?0:startIdx;
546 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
548 if (fpInputBlocks[idx].fPtr==NULL) continue;
549 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
550 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
552 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
560 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
562 // see header file for function documentation
564 if (fpInputBlocks!=NULL) {
565 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
566 if (fpInputBlocks[idx].fPtr) {
567 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
568 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
569 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
570 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
571 TClass* objclass=msg.GetClass();
572 pObj=msg.ReadObject(objclass);
573 if (pObj && objclass) {
574 HLTDebug("object %p type %s created", pObj, objclass->GetName());
578 } else if (bForce!=0) {
579 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
582 HLTFatal("block descriptor empty");
585 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
588 HLTError("no input blocks available");
594 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
596 // see header file for function documentation
597 if (fpInputObjects==NULL) {
598 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
601 if (fpInputObjects) {
602 pObj=fpInputObjects->At(idx);
604 pObj=CreateInputObject(idx, bForce);
606 fpInputObjects->AddAt(pObj, idx);
610 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
615 int AliHLTComponent::CleanupInputObjects()
617 // see header file for function documentation
618 if (!fpInputObjects) return 0;
619 TObjArray* array=fpInputObjects;
621 for (int i=0; i<array->GetEntries(); i++) {
622 TObject* pObj=array->At(i);
623 // grrr, garbage collection strikes back: When read via AliHLTMessage
624 // (CreateInputObject), and written to a TFile afterwards, the
625 // TFile::Close calls ROOOT's garbage collection. No clue why the
626 // object ended up in the key list and needs to be deleted
627 if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
633 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
635 // see header file for function documentation
636 ALIHLTCOMPONENT_BASE_STOPWATCH();
637 AliHLTComponentDataType dt=kAliHLTVoidDataType;
638 int idx=fCurrentInputBlock;
640 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
642 HLTError("unknown object %p", pObject);
646 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
647 dt=fpInputBlocks[idx].fDataType;
649 HLTFatal("severe internal error, index out of range");
655 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
657 // see header file for function documentation
658 ALIHLTCOMPONENT_BASE_STOPWATCH();
659 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
660 int idx=fCurrentInputBlock;
662 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
664 HLTError("unknown object %p", pObject);
668 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
669 iSpec=fpInputBlocks[idx].fSpecification;
671 HLTFatal("severe internal error, index out of range");
677 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
679 // see header file for function documentation
680 ALIHLTCOMPONENT_BASE_STOPWATCH();
683 int idx=FindInputBlock(fSearchDataType, 0);
684 const AliHLTComponentBlockData* pBlock=NULL;
686 // check for fpInputBlocks pointer done in FindInputBlock
687 pBlock=&fpInputBlocks[idx];
688 fCurrentInputBlock=idx;
693 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
694 const char* dtOrigin)
696 // see header file for function documentation
697 ALIHLTCOMPONENT_BASE_STOPWATCH();
698 AliHLTComponentDataType dt;
699 SetDataType(dt, dtID, dtOrigin);
700 return GetFirstInputBlock(dt);
703 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index)
705 // see header file for function documentation
706 ALIHLTCOMPONENT_BASE_STOPWATCH();
707 assert( 0 <= index and index < fCurrentEventData.fBlockCnt );
708 return &fpInputBlocks[index];
711 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
713 // see header file for function documentation
714 ALIHLTCOMPONENT_BASE_STOPWATCH();
715 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
716 const AliHLTComponentBlockData* pBlock=NULL;
718 // check for fpInputBlocks pointer done in FindInputBlock
719 pBlock=&fpInputBlocks[idx];
720 fCurrentInputBlock=idx;
725 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
727 // see header file for function documentation
729 if (fpInputBlocks!=NULL) {
731 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
732 iResult=(int)(pBlock-fpInputBlocks);
741 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
743 // see header file for function documentation
744 ALIHLTCOMPONENT_BASE_STOPWATCH();
745 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
746 int idx=fCurrentInputBlock;
748 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
750 HLTError("unknown Block %p", pBlock);
754 // check for fpInputBlocks pointer done in FindInputBlock
755 iSpec=fpInputBlocks[idx].fSpecification;
760 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
761 void* pHeader, int headerSize)
763 // see header file for function documentation
764 ALIHLTCOMPONENT_BASE_STOPWATCH();
767 AliHLTMessage msg(kMESS_OBJECT);
768 msg.WriteObject(pObject);
769 Int_t iMsgLength=msg.Length();
771 msg.SetLength(); // sets the length to the first (reserved) word
772 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
774 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
777 HLTError("object serialization failed for object %p", pObject);
786 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
787 void* pHeader, int headerSize)
789 // see header file for function documentation
790 ALIHLTCOMPONENT_BASE_STOPWATCH();
791 AliHLTComponentDataType dt;
792 SetDataType(dt, dtID, dtOrigin);
793 return PushBack(pObject, dt, spec, pHeader, headerSize);
796 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
797 void* pHeader, int headerSize)
799 // see header file for function documentation
800 ALIHLTCOMPONENT_BASE_STOPWATCH();
801 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
804 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
805 void* pHeader, int headerSize)
807 // see header file for function documentation
808 ALIHLTCOMPONENT_BASE_STOPWATCH();
809 AliHLTComponentDataType dt;
810 SetDataType(dt, dtID, dtOrigin);
811 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
814 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
815 void* pHeader, int iHeaderSize)
817 // see header file for function documentation
819 int iBlkSize = iBufferSize + iHeaderSize;
821 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
822 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
823 AliHLTComponentBlockData bd;
825 bd.fOffset = fOutputBufferFilled;
828 bd.fSpecification = spec;
829 if (pHeader!=NULL && pHeader!=pTgt) {
830 memcpy(pTgt, pHeader, iHeaderSize);
833 pTgt += (AliHLTUInt8_t) iHeaderSize;
835 if (pBuffer!=NULL && pBuffer!=pTgt) {
836 memcpy(pTgt, pBuffer, iBufferSize);
838 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
839 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
841 fOutputBufferFilled+=bd.fSize;
842 fOutputBlocks.push_back( bd );
843 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
845 if (fpOutputBuffer) {
846 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
848 HLTError("output buffer not available");
858 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
860 // see header file for function documentation
861 if (!pObject) return -EINVAL;
862 AliHLTMessage msg(kMESS_OBJECT);
863 msg.WriteObject(pObject);
867 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
868 const char* dtOrigin,
871 // see header file for function documentation
872 ALIHLTCOMPONENT_BASE_STOPWATCH();
873 AliHLTComponentDataType dt;
874 SetDataType(dt, dtID, dtOrigin);
875 return CreateMemoryFile(capacity, dt, spec);
878 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
879 const AliHLTComponentDataType& dt,
882 // see header file for function documentation
883 ALIHLTCOMPONENT_BASE_STOPWATCH();
884 AliHLTMemoryFile* pFile=NULL;
885 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
886 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
887 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
889 unsigned int nofBlocks=fOutputBlocks.size();
890 if (nofBlocks+1>fMemFiles.size()) {
891 fMemFiles.resize(nofBlocks+1, NULL);
893 if (nofBlocks<fMemFiles.size()) {
894 fMemFiles[nofBlocks]=pFile;
895 AliHLTComponentBlockData bd;
897 bd.fOffset = fOutputBufferFilled;
900 bd.fSpecification = spec;
901 fOutputBufferFilled+=bd.fSize;
902 fOutputBlocks.push_back( bd );
904 HLTError("can not allocate/grow object array");
905 pFile->CloseMemoryFile(0);
911 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
916 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
917 const char* dtOrigin,
921 // see header file for function documentation
922 ALIHLTCOMPONENT_BASE_STOPWATCH();
923 AliHLTComponentDataType dt;
924 SetDataType(dt, dtID, dtOrigin);
925 int size=fOutputBufferSize-fOutputBufferFilled;
926 if (capacity<0 || capacity>1.0) {
927 HLTError("invalid parameter: capacity %f", capacity);
930 size=(int)(size*capacity);
931 return CreateMemoryFile(size, dt, spec);
934 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
938 // see header file for function documentation
939 ALIHLTCOMPONENT_BASE_STOPWATCH();
940 int size=fOutputBufferSize-fOutputBufferFilled;
941 if (capacity<0 || capacity>1.0) {
942 HLTError("invalid parameter: capacity %f", capacity);
945 size=(int)(size*capacity);
946 return CreateMemoryFile(size, dt, spec);
949 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
950 const char* key, int option)
952 // see header file for function documentation
954 if (pFile && pObject) {
956 iResult=pObject->Write(key, option);
960 iResult=-pFile->GetErrno();
961 if (iResult==-ENOSPC) {
962 HLTError("error writing memory file, buffer too small");
971 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
973 // see header file for function documentation
976 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
978 while (element!=fMemFiles.end() && iResult>=0) {
979 if (*element && *element==pFile) {
980 iResult=pFile->CloseMemoryFile();
982 // sync memory files and descriptors
984 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
992 HLTError("can not find memory file %p", pFile);
1000 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData /*edd*/)
1002 // see header file for function documentation
1003 int iResult=-ENOSYS;
1004 //#warning function not yet implemented
1005 HLTWarning("function not yet implemented");
1009 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1010 const AliHLTComponentBlockData* blocks,
1011 AliHLTComponentTriggerData& trigData,
1012 AliHLTUInt8_t* outputPtr,
1013 AliHLTUInt32_t& size,
1014 AliHLTUInt32_t& outputBlockCnt,
1015 AliHLTComponentBlockData*& outputBlocks,
1016 AliHLTComponentEventDoneData*& edd )
1018 // see header file for function documentation
1019 ALIHLTCOMPONENT_BASE_STOPWATCH();
1021 fCurrentEvent=evtData.fEventID;
1022 fCurrentEventData=evtData;
1023 fpInputBlocks=blocks;
1024 fCurrentInputBlock=-1;
1025 fSearchDataType=kAliHLTAnyDataType;
1026 fpOutputBuffer=outputPtr;
1027 fOutputBufferSize=size;
1028 fOutputBufferFilled=0;
1029 fOutputBlocks.clear();
1031 // find special events
1032 if (fpInputBlocks) {
1033 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1034 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1036 if (fpRunDesc==NULL) {
1037 fpRunDesc=new AliHLTRunDesc;
1039 if ((iResult=CopyStruct(fpRunDesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1040 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1046 HLTWarning("already received SOR event run no %d, ignoring SOR", fpRunDesc->fRunNo);
1048 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1049 if (fpRunDesc!=NULL) {
1051 AliHLTRunDesc rundesc;
1052 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1053 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1054 HLTWarning("run no missmatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1056 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1059 AliHLTRunDesc* pRunDesc=fpRunDesc;
1064 HLTWarning("did not receive SOR, ignoring EOR");
1067 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1073 AliHLTComponentBlockDataList blockData;
1074 { // dont delete, sets the scope for the stopwatch guard
1075 ALIHLTCOMPONENT_DA_STOPWATCH();
1076 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1077 } // end of the scope of the stopwatch guard
1079 if (fOutputBlocks.size()>0) {
1080 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1082 // sync memory files and descriptors
1083 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1085 while (element!=fMemFiles.end() && iResult>=0) {
1087 if ((*element)->IsClosed()==0) {
1088 HLTWarning("memory file has not been closed, force flush");
1089 iResult=CloseMemoryFile(*element);
1096 // create the descriptor list
1097 if (blockData.size()>0) {
1098 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1101 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1102 size=fOutputBufferFilled;
1106 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1109 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1116 CleanupInputObjects();
1118 IncrementEventCounter();
1123 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1128 // standard constructor (not for use)
1131 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1133 fpStopwatch(pStopwatch),
1138 // check for already existing guard
1139 if (fgpCurrent) fpPrec=fgpCurrent;
1142 // stop the preceeding guard if it controls a different stopwatch
1144 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1146 // start the stopwatch if the current guard controls a different one
1147 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1150 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
1156 // copy constructor not for use
1160 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1163 // assignment operator not for use
1170 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1172 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1176 // resume the preceeding guard if it controls a different stopwatch
1178 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1180 // stop the stopwatch if the current guard controls a different one
1181 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1183 // resume to the preceeding guard
1187 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1189 // see header file for function documentation
1190 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1191 return fpStopwatch!=pSucc?1:0;
1194 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1196 // see header file for function documentation
1197 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1198 return fpStopwatch!=pSucc?1:0;
1201 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
1203 // see header file for function documentation
1205 if (pSW!=NULL && type<kSWTypeCount) {
1206 if (fpStopwatches) {
1207 TObject* pObj=fpStopwatches->At((int)type);
1208 if (pSW==NULL // explicit reset
1209 || pObj==NULL) { // explicit set
1210 fpStopwatches->AddAt(pSW, (int)type);
1211 } else if (pObj!=pSW) {
1212 HLTWarning("stopwatch %d already set, reset first", (int)type);
1222 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1224 // see header file for function documentation
1225 if (pStopwatches==NULL) return -EINVAL;
1228 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1229 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1233 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1235 // see header file for function documentation
1236 if (fpRunDesc==NULL) return 0;
1237 return fpRunDesc->fRunNo;
1240 AliHLTUInt32_t AliHLTComponent::GetRunType() const
1242 // see header file for function documentation
1243 if (fpRunDesc==NULL) return 0;
1244 return fpRunDesc->fRunType;
1247 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
1248 const char* structname, const char* eventname)
1250 // see header file for function documentation
1252 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1253 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1254 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1255 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
1256 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
1257 if (fpInputBlocks[iBlockNo].fSize!=copy) {
1258 HLTWarning("%s event: missmatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1259 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1261 if (copy!=iStructSize) {
1262 HLTWarning("%s event: missmatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1263 if (copy>iStructSize) {
1266 memset(pTgt, 0, iStructSize);
1269 memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
1273 HLTWarning("%s event: missing data block", eventname);
1279 HLTError("invalid struct");