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 )
236 // see header file for function documentation
239 if (type==kAliHLTVoidDataType) {
242 // some gymnastics in order to avoid a '0' which is part of either or both
243 // ID and origin terminating the whole string. Unfortunately, string doesn't
244 // stop appending at the '0' if the number of elements to append was
245 // explicitely specified
247 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
248 out.append(tmp.c_str());
251 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
252 out.append(tmp.c_str());
258 void* AliHLTComponent::AllocMemory( unsigned long size )
260 // see header file for function documentation
261 if (fEnvironment.fAllocMemoryFunc)
262 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
263 HLTFatal("no memory allocation handler registered");
267 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
268 AliHLTComponentBlockData** outputBlocks )
270 // see header file for function documentation
271 if ( blockCount==NULL || outputBlocks==NULL )
273 AliHLTUInt32_t count = blocks.size();
277 *outputBlocks = NULL;
280 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
281 if ( !*outputBlocks )
283 for ( unsigned long i = 0; i < count; i++ ) {
284 (*outputBlocks)[i] = blocks[i];
285 if (blocks[i].fDataType==kAliHLTAnyDataType) {
286 (*outputBlocks)[i].fDataType=GetOutputDataType();
287 /* data type was set to the output data type by the PubSub AliRoot
288 Wrapper component, if data type of the block was ********:****.
289 Now handled by the component base class in order to have same
290 behavior when running embedded in AliRoot
291 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
292 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
301 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
303 // see header file for function documentation
304 if (fEnvironment.fGetEventDoneDataFunc)
305 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
309 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
311 // see header file for function documentation
314 AliHLTComponentDataTypeList ctlist;
315 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(ctlist);
316 AliHLTComponentDataTypeList::iterator type=ctlist.begin();
317 //AliHLTComponentDataType ouptdt=GetOutputDataType();
318 //PrintDataTypeContent(ouptdt, "publisher \'%s\'");
319 while (type!=ctlist.end() && iResult==0) {
320 //PrintDataTypeContent((*type), "consumer \'%s\'");
321 if ((*type)==GetOutputDataType() ||
322 (*type)==kAliHLTAnyDataType) {
323 if (tgtList) tgtList->push_back(*type);
325 // this loop has to be changed in case of multiple output types
336 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
338 // see header file for function documentation
339 const char* fmt="publisher \'%s\'";
340 if (format) fmt=format;
341 HLTMessage(fmt, (DataType2Text(dt)).c_str());
342 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
357 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData ) const
359 // see header file for function documentation
360 blockData.fStructSize = sizeof(blockData);
361 FillShmData( blockData.fShmKey );
362 blockData.fOffset = ~(AliHLTUInt32_t)0;
363 blockData.fPtr = NULL;
365 FillDataType( blockData.fDataType );
366 blockData.fSpecification = kAliHLTVoidDataSpec;
369 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData ) const
371 // see header file for function documentation
372 shmData.fStructSize = sizeof(shmData);
373 shmData.fShmType = gkAliHLTComponentInvalidShmType;
374 shmData.fShmID = gkAliHLTComponentInvalidShmID;
377 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType ) const
379 // see header file for function documentation
380 dataType=kAliHLTAnyDataType;
383 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
385 // see header file for function documentation
386 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
387 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
390 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
392 // see header file for function documentation
393 tgtdt.fStructSize = sizeof(AliHLTComponentDataType);
394 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
395 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
397 if ((int)strlen(id)>kAliHLTComponentDataTypefIDsize) {
398 HLTWarning("data type id %s is too long, truncated to %d", id, kAliHLTComponentDataTypefIDsize);
400 strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
402 if ((int)strlen(origin)>kAliHLTComponentDataTypefOriginSize) {
403 HLTWarning("data type origin %s is too long, truncated to %d", origin, kAliHLTComponentDataTypefOriginSize);
405 strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
408 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
410 // see header file for function documentation
411 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
412 evtData.fStructSize=sizeof(AliHLTComponentEventData);
415 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
417 // see header file for function documentation
419 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
420 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
421 if (dt.fID[i]!=0) msg+=dt.fID[i];
425 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
426 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
430 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
433 int AliHLTComponent::GetEventCount() const
435 // see header file for function documentation
439 int AliHLTComponent::IncrementEventCounter()
441 // see header file for function documentation
442 if (fEventCount>=0) fEventCount++;
446 int AliHLTComponent::GetNumberOfInputBlocks() const
448 // see header file for function documentation
449 if (fpInputBlocks!=NULL) {
450 return fCurrentEventData.fBlockCnt;
455 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
456 const char* classname,
459 // see header file for function documentation
460 ALIHLTCOMPONENT_BASE_STOPWATCH();
462 if (classname) fClassName=classname;
463 else fClassName.clear();
464 int idx=FindInputBlock(fSearchDataType, 0, 1);
467 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
468 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
469 fCurrentInputBlock=idx;
476 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
477 const char* dtOrigin,
478 const char* classname,
481 // see header file for function documentation
482 ALIHLTCOMPONENT_BASE_STOPWATCH();
483 AliHLTComponentDataType dt;
484 SetDataType(dt, dtID, dtOrigin);
485 return GetFirstInputObject(dt, classname, bForce);
488 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
490 // see header file for function documentation
491 ALIHLTCOMPONENT_BASE_STOPWATCH();
492 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
493 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
496 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
497 fCurrentInputBlock=idx;
503 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
505 // see header file for function documentation
507 if (fpInputBlocks!=NULL) {
508 int idx=startIdx<0?0:startIdx;
509 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
511 if (fpInputBlocks[idx].fPtr==NULL) continue;
512 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
513 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
515 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
523 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
525 // see header file for function documentation
527 if (fpInputBlocks!=NULL) {
528 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
529 if (fpInputBlocks[idx].fPtr) {
530 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
531 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
532 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
533 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
534 TClass* objclass=msg.GetClass();
535 pObj=msg.ReadObject(objclass);
536 if (pObj && objclass) {
537 HLTDebug("object %p type %s created", pObj, objclass->GetName());
541 } else if (bForce!=0) {
542 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
545 HLTFatal("block descriptor empty");
548 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
551 HLTError("no input blocks available");
557 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
559 // see header file for function documentation
560 if (fpInputObjects==NULL) {
561 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
564 if (fpInputObjects) {
565 pObj=fpInputObjects->At(idx);
567 pObj=CreateInputObject(idx, bForce);
569 fpInputObjects->AddAt(pObj, idx);
573 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
578 int AliHLTComponent::CleanupInputObjects()
580 // see header file for function documentation
581 if (!fpInputObjects) return 0;
582 TObjArray* array=fpInputObjects;
584 for (int i=0; i<array->GetEntries(); i++) {
585 TObject* pObj=array->At(i);
586 // grrr, garbage collection strikes back: When read via AliHLTMessage
587 // (CreateInputObject), and written to a TFile afterwards, the
588 // TFile::Close calls ROOOT's garbage collection. No clue why the
589 // object ended up in the key list and needs to be deleted
590 if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
596 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
598 // see header file for function documentation
599 ALIHLTCOMPONENT_BASE_STOPWATCH();
600 AliHLTComponentDataType dt=kAliHLTVoidDataType;
601 int idx=fCurrentInputBlock;
603 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
605 HLTError("unknown object %p", pObject);
609 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
610 dt=fpInputBlocks[idx].fDataType;
612 HLTFatal("severe internal error, index out of range");
618 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
620 // see header file for function documentation
621 ALIHLTCOMPONENT_BASE_STOPWATCH();
622 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
623 int idx=fCurrentInputBlock;
625 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
627 HLTError("unknown object %p", pObject);
631 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
632 iSpec=fpInputBlocks[idx].fSpecification;
634 HLTFatal("severe internal error, index out of range");
640 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
642 // see header file for function documentation
643 ALIHLTCOMPONENT_BASE_STOPWATCH();
646 int idx=FindInputBlock(fSearchDataType, 0);
647 const AliHLTComponentBlockData* pBlock=NULL;
649 // check for fpInputBlocks pointer done in FindInputBlock
650 pBlock=&fpInputBlocks[idx];
651 fCurrentInputBlock=idx;
656 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
657 const char* dtOrigin)
659 // see header file for function documentation
660 ALIHLTCOMPONENT_BASE_STOPWATCH();
661 AliHLTComponentDataType dt;
662 SetDataType(dt, dtID, dtOrigin);
663 return GetFirstInputBlock(dt);
666 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index)
668 // see header file for function documentation
669 ALIHLTCOMPONENT_BASE_STOPWATCH();
670 assert( 0 <= index and index < fCurrentEventData.fBlockCnt );
671 return &fpInputBlocks[index];
674 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
676 // see header file for function documentation
677 ALIHLTCOMPONENT_BASE_STOPWATCH();
678 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
679 const AliHLTComponentBlockData* pBlock=NULL;
681 // check for fpInputBlocks pointer done in FindInputBlock
682 pBlock=&fpInputBlocks[idx];
683 fCurrentInputBlock=idx;
688 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
690 // see header file for function documentation
692 if (fpInputBlocks!=NULL) {
694 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
695 iResult=(int)(pBlock-fpInputBlocks);
704 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
706 // see header file for function documentation
707 ALIHLTCOMPONENT_BASE_STOPWATCH();
708 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
709 int idx=fCurrentInputBlock;
711 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
713 HLTError("unknown Block %p", pBlock);
717 // check for fpInputBlocks pointer done in FindInputBlock
718 iSpec=fpInputBlocks[idx].fSpecification;
723 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
724 void* pHeader, int headerSize)
726 // see header file for function documentation
727 ALIHLTCOMPONENT_BASE_STOPWATCH();
730 AliHLTMessage msg(kMESS_OBJECT);
731 msg.WriteObject(pObject);
732 Int_t iMsgLength=msg.Length();
734 msg.SetLength(); // sets the length to the first (reserved) word
735 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
737 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
740 HLTError("object serialization failed for object %p", pObject);
749 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
750 void* pHeader, int headerSize)
752 // see header file for function documentation
753 ALIHLTCOMPONENT_BASE_STOPWATCH();
754 AliHLTComponentDataType dt;
755 SetDataType(dt, dtID, dtOrigin);
756 return PushBack(pObject, dt, spec, pHeader, headerSize);
759 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
760 void* pHeader, int headerSize)
762 // see header file for function documentation
763 ALIHLTCOMPONENT_BASE_STOPWATCH();
764 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
767 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
768 void* pHeader, int headerSize)
770 // see header file for function documentation
771 ALIHLTCOMPONENT_BASE_STOPWATCH();
772 AliHLTComponentDataType dt;
773 SetDataType(dt, dtID, dtOrigin);
774 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
777 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
778 void* pHeader, int iHeaderSize)
780 // see header file for function documentation
782 int iBlkSize = iBufferSize + iHeaderSize;
784 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
785 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
786 AliHLTComponentBlockData bd;
788 bd.fOffset = fOutputBufferFilled;
791 bd.fSpecification = spec;
792 if (pHeader!=NULL && pHeader!=pTgt) {
793 memcpy(pTgt, pHeader, iHeaderSize);
796 pTgt += (AliHLTUInt8_t) iHeaderSize;
798 if (pBuffer!=NULL && pBuffer!=pTgt) {
799 memcpy(pTgt, pBuffer, iBufferSize);
801 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
802 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
804 fOutputBufferFilled+=bd.fSize;
805 fOutputBlocks.push_back( bd );
806 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
808 if (fpOutputBuffer) {
809 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
811 HLTError("output buffer not available");
821 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
823 // see header file for function documentation
824 if (!pObject) return -EINVAL;
825 AliHLTMessage msg(kMESS_OBJECT);
826 msg.WriteObject(pObject);
830 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
831 const char* dtOrigin,
834 // see header file for function documentation
835 ALIHLTCOMPONENT_BASE_STOPWATCH();
836 AliHLTComponentDataType dt;
837 SetDataType(dt, dtID, dtOrigin);
838 return CreateMemoryFile(capacity, dt, spec);
841 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
842 const AliHLTComponentDataType& dt,
845 // see header file for function documentation
846 ALIHLTCOMPONENT_BASE_STOPWATCH();
847 AliHLTMemoryFile* pFile=NULL;
848 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
849 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
850 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
852 unsigned int nofBlocks=fOutputBlocks.size();
853 if (nofBlocks+1>fMemFiles.size()) {
854 fMemFiles.resize(nofBlocks+1, NULL);
856 if (nofBlocks<fMemFiles.size()) {
857 fMemFiles[nofBlocks]=pFile;
858 AliHLTComponentBlockData bd;
860 bd.fOffset = fOutputBufferFilled;
863 bd.fSpecification = spec;
864 fOutputBufferFilled+=bd.fSize;
865 fOutputBlocks.push_back( bd );
867 HLTError("can not allocate/grow object array");
868 pFile->CloseMemoryFile(0);
874 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
879 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
880 const char* dtOrigin,
884 // see header file for function documentation
885 ALIHLTCOMPONENT_BASE_STOPWATCH();
886 AliHLTComponentDataType dt;
887 SetDataType(dt, dtID, dtOrigin);
888 int size=fOutputBufferSize-fOutputBufferFilled;
889 if (capacity<0 || capacity>1.0) {
890 HLTError("invalid parameter: capacity %f", capacity);
893 size=(int)(size*capacity);
894 return CreateMemoryFile(size, dt, spec);
897 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
901 // see header file for function documentation
902 ALIHLTCOMPONENT_BASE_STOPWATCH();
903 int size=fOutputBufferSize-fOutputBufferFilled;
904 if (capacity<0 || capacity>1.0) {
905 HLTError("invalid parameter: capacity %f", capacity);
908 size=(int)(size*capacity);
909 return CreateMemoryFile(size, dt, spec);
912 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
913 const char* key, int option)
915 // see header file for function documentation
917 if (pFile && pObject) {
919 iResult=pObject->Write(key, option);
923 iResult=-pFile->GetErrno();
924 if (iResult==-ENOSPC) {
925 HLTError("error writing memory file, buffer too small");
934 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
936 // see header file for function documentation
939 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
941 while (element!=fMemFiles.end() && iResult>=0) {
942 if (*element && *element==pFile) {
943 iResult=pFile->CloseMemoryFile();
945 // sync memory files and descriptors
947 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
955 HLTError("can not find memory file %p", pFile);
963 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData /*edd*/)
965 // see header file for function documentation
967 //#warning function not yet implemented
968 HLTWarning("function not yet implemented");
972 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
973 const AliHLTComponentBlockData* blocks,
974 AliHLTComponentTriggerData& trigData,
975 AliHLTUInt8_t* outputPtr,
976 AliHLTUInt32_t& size,
977 AliHLTUInt32_t& outputBlockCnt,
978 AliHLTComponentBlockData*& outputBlocks,
979 AliHLTComponentEventDoneData*& edd )
981 // see header file for function documentation
982 ALIHLTCOMPONENT_BASE_STOPWATCH();
984 fCurrentEvent=evtData.fEventID;
985 fCurrentEventData=evtData;
986 fpInputBlocks=blocks;
987 fCurrentInputBlock=-1;
988 fSearchDataType=kAliHLTAnyDataType;
989 fpOutputBuffer=outputPtr;
990 fOutputBufferSize=size;
991 fOutputBufferFilled=0;
992 fOutputBlocks.clear();
994 // find special events
996 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
997 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
999 if (fpRunDesc==NULL) {
1000 fpRunDesc=new AliHLTRunDesc;
1002 if ((iResult=CopyStruct(fpRunDesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1003 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1009 HLTWarning("already received SOR event run no %d, ignoring SOR", fpRunDesc->fRunNo);
1011 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1012 if (fpRunDesc!=NULL) {
1014 AliHLTRunDesc rundesc;
1015 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1016 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1017 HLTWarning("run no missmatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1019 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1022 AliHLTRunDesc* pRunDesc=fpRunDesc;
1027 HLTWarning("did not receive SOR, ignoring EOR");
1030 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1036 AliHLTComponentBlockDataList blockData;
1037 { // dont delete, sets the scope for the stopwatch guard
1038 ALIHLTCOMPONENT_DA_STOPWATCH();
1039 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1040 } // end of the scope of the stopwatch guard
1042 if (fOutputBlocks.size()>0) {
1043 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1045 // sync memory files and descriptors
1046 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1048 while (element!=fMemFiles.end() && iResult>=0) {
1050 if ((*element)->IsClosed()==0) {
1051 HLTWarning("memory file has not been closed, force flush");
1052 iResult=CloseMemoryFile(*element);
1059 // create the descriptor list
1060 if (blockData.size()>0) {
1061 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1064 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1065 size=fOutputBufferFilled;
1069 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1072 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1079 CleanupInputObjects();
1081 IncrementEventCounter();
1086 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1091 // standard constructor (not for use)
1094 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1096 fpStopwatch(pStopwatch),
1101 // check for already existing guard
1102 if (fgpCurrent) fpPrec=fgpCurrent;
1105 // stop the preceeding guard if it controls a different stopwatch
1107 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1109 // start the stopwatch if the current guard controls a different one
1110 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1113 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
1119 // copy constructor not for use
1123 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1126 // assignment operator not for use
1133 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1135 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1139 // resume the preceeding guard if it controls a different stopwatch
1141 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1143 // stop the stopwatch if the current guard controls a different one
1144 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1146 // resume to the preceeding guard
1150 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1152 // see header file for function documentation
1153 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1154 return fpStopwatch!=pSucc?1:0;
1157 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1159 // see header file for function documentation
1160 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1161 return fpStopwatch!=pSucc?1:0;
1164 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
1166 // see header file for function documentation
1168 if (pSW!=NULL && type<kSWTypeCount) {
1169 if (fpStopwatches) {
1170 TObject* pObj=fpStopwatches->At((int)type);
1171 if (pSW==NULL // explicit reset
1172 || pObj==NULL) { // explicit set
1173 fpStopwatches->AddAt(pSW, (int)type);
1174 } else if (pObj!=pSW) {
1175 HLTWarning("stopwatch %d already set, reset first", (int)type);
1185 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1187 // see header file for function documentation
1188 if (pStopwatches==NULL) return -EINVAL;
1191 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1192 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1196 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1198 // see header file for function documentation
1199 if (fpRunDesc==NULL) return 0;
1200 return fpRunDesc->fRunNo;
1203 AliHLTUInt32_t AliHLTComponent::GetRunType() const
1205 // see header file for function documentation
1206 if (fpRunDesc==NULL) return 0;
1207 return fpRunDesc->fRunType;
1210 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
1211 const char* structname, const char* eventname)
1213 // see header file for function documentation
1215 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1216 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1217 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1218 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
1219 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
1220 if (fpInputBlocks[iBlockNo].fSize!=copy) {
1221 HLTWarning("%s event: missmatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1222 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1224 if (copy!=iStructSize) {
1225 HLTWarning("%s event: missmatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1226 if (copy>iStructSize) {
1229 memset(pTgt, 0, iStructSize);
1232 memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
1236 HLTWarning("%s event: missing data block", eventname);
1242 HLTError("invalid struct");