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"
41 /** ROOT macro for the implementation of ROOT specific class methods */
42 ClassImp(AliHLTComponent);
44 /** stopwatch macro using the stopwatch guard */
45 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
46 //#define ALIHLTCOMPONENT_STOPWATCH(type)
48 /** stopwatch macro for operations of the base class */
49 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
50 /** stopwatch macro for operations of the detector algorithm (DA) */
51 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
53 AliHLTComponent::AliHLTComponent()
61 fCurrentInputBlock(-1),
62 fSearchDataType(kAliHLTVoidDataType),
67 fOutputBufferFilled(0),
69 fpStopwatches(new TObjArray(kSWTypeCount)),
75 // see header file for class documentation
77 // refer to README to build package
79 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
80 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
81 if (fgpComponentHandler)
82 fgpComponentHandler->ScheduleRegister(this);
83 //SetLocalLoggingLevel(kHLTLogDefault);
86 AliHLTComponent::~AliHLTComponent()
88 // see header file for function documentation
89 CleanupInputObjects();
90 if (fpStopwatches!=NULL) delete fpStopwatches;
92 vector<AliHLTMemoryFile*>::iterator element=fMemFiles.begin();
93 while (element!=fMemFiles.end()) {
95 if ((*element)->IsClosed()==0) {
96 HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
97 // close but do not flush as we dont know whether the buffer is still valid
107 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
109 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
111 // see header file for function documentation
113 if (fgpComponentHandler==NULL || bOverwrite!=0)
114 fgpComponentHandler=pCH;
120 int AliHLTComponent::UnsetGlobalComponentHandler()
122 // see header file for function documentation
123 return SetGlobalComponentHandler(NULL,1);
126 int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
128 // see header file for function documentation
131 memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
132 fEnvironment.fParam=environParam;
134 const char** pArguments=NULL;
139 pArguments=new const char*[argc];
141 for (int i=0; i<argc && iResult>=0; i++) {
143 if (argument.IsNull()) continue;
146 if (argument.CompareTo("benchmark")==0) {
149 } else if (argument.CompareTo("loglevel")==0) {
150 if ((bMissingParam=(++i>=argc))) break;
151 TString parameter(argv[i]);
152 parameter.Remove(TString::kLeading, ' '); // remove all blanks
153 if (parameter.BeginsWith("0x") &&
154 parameter.Replace(0,2,"",0).IsHex()) {
155 AliHLTComponentLogSeverity loglevel=kHLTLogNone;
156 sscanf(parameter.Data(),"%x", (unsigned int*)&loglevel);
157 SetLocalLoggingLevel(loglevel);
159 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
163 pArguments[iNofChildArgs++]=argv[i];
171 HLTError("missing parameter for argument %s", argument.Data());
175 iResult=DoInit(iNofChildArgs, pArguments);
177 if (iResult>=0) fEventCount=0;
178 if (pArguments) delete [] pArguments;
182 int AliHLTComponent::Deinit()
184 // see header file for function documentation
188 HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
189 AliHLTRunDesc* pRunDesc=fpRunDesc;
196 int AliHLTComponent::DoInit( int argc, const char** argv )
198 // see header file for function documentation
199 if (argc==0 && argv==NULL) {
200 // this is currently just to get rid of the warning "unused parameter"
206 int AliHLTComponent::DoDeinit()
208 // see header file for function documentation
213 int AliHLTComponent::GetOutputDataTypes(vector<AliHLTComponentDataType>& /*tgtList*/)
218 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
220 // see header file for function documentation
221 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
222 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
223 strcat( output, ":" );
224 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
227 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type )
229 // see header file for function documentation
232 if (type==kAliHLTVoidDataType) {
235 // some gymnastics in order to avoid a '0' which is part of either or both
236 // ID and origin terminating the whole string. Unfortunately, string doesn't
237 // stop appending at the '0' if the number of elements to append was
238 // explicitely specified
240 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
241 out.append(tmp.c_str());
244 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
245 out.append(tmp.c_str());
251 void* AliHLTComponent::AllocMemory( unsigned long size )
253 // see header file for function documentation
254 if (fEnvironment.fAllocMemoryFunc)
255 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
256 HLTFatal("no memory allocation handler registered");
260 int AliHLTComponent::MakeOutputDataBlockList( const vector<AliHLTComponentBlockData>& blocks, AliHLTUInt32_t* blockCount,
261 AliHLTComponentBlockData** outputBlocks )
263 // see header file for function documentation
264 if ( blockCount==NULL || outputBlocks==NULL )
266 AliHLTUInt32_t count = blocks.size();
270 *outputBlocks = NULL;
273 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
274 if ( !*outputBlocks )
276 for ( unsigned long i = 0; i < count; i++ ) {
277 (*outputBlocks)[i] = blocks[i];
278 if (blocks[i].fDataType==kAliHLTAnyDataType) {
279 (*outputBlocks)[i].fDataType=GetOutputDataType();
280 /* data type was set to the output data type by the PubSub AliRoot
281 Wrapper component, if data type of the block was ********:****.
282 Now handled by the component base class in order to have same
283 behavior when running embedded in AliRoot
284 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
285 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
294 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
296 // see header file for function documentation
297 if (fEnvironment.fGetEventDoneDataFunc)
298 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
302 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
304 // see header file for function documentation
307 vector<AliHLTComponentDataType> ctlist;
308 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(ctlist);
309 vector<AliHLTComponentDataType>::iterator type=ctlist.begin();
310 //AliHLTComponentDataType ouptdt=GetOutputDataType();
311 //PrintDataTypeContent(ouptdt, "publisher \'%s\'");
312 while (type!=ctlist.end() && iResult==0) {
313 //PrintDataTypeContent((*type), "consumer \'%s\'");
314 if ((*type)==GetOutputDataType() ||
315 (*type)==kAliHLTAnyDataType) {
316 if (tgtList) tgtList->push_back(*type);
318 // this loop has to be changed in case of multiple output types
329 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
331 // see header file for function documentation
332 const char* fmt="publisher \'%s\'";
333 if (format) fmt=format;
334 HLTMessage(fmt, (DataType2Text(dt)).c_str());
335 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
350 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData ) const
352 // see header file for function documentation
353 blockData.fStructSize = sizeof(blockData);
354 FillShmData( blockData.fShmKey );
355 blockData.fOffset = ~(AliHLTUInt32_t)0;
356 blockData.fPtr = NULL;
358 FillDataType( blockData.fDataType );
359 blockData.fSpecification = kAliHLTVoidDataSpec;
362 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData ) const
364 // see header file for function documentation
365 shmData.fStructSize = sizeof(shmData);
366 shmData.fShmType = gkAliHLTComponentInvalidShmType;
367 shmData.fShmID = gkAliHLTComponentInvalidShmID;
370 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType ) const
372 // see header file for function documentation
373 dataType=kAliHLTAnyDataType;
376 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
378 // see header file for function documentation
379 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
380 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
383 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
385 // see header file for function documentation
386 tgtdt.fStructSize = sizeof(AliHLTComponentDataType);
387 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
388 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
390 if ((int)strlen(id)>kAliHLTComponentDataTypefIDsize) {
391 HLTWarning("data type id %s is too long, truncated to %d", id, kAliHLTComponentDataTypefIDsize);
393 strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
395 if ((int)strlen(origin)>kAliHLTComponentDataTypefOriginSize) {
396 HLTWarning("data type origin %s is too long, truncated to %d", origin, kAliHLTComponentDataTypefOriginSize);
398 strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
401 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
403 // see header file for function documentation
404 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
405 evtData.fStructSize=sizeof(AliHLTComponentEventData);
408 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
410 // see header file for function documentation
412 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
413 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
414 if (dt.fID[i]!=0) msg+=dt.fID[i];
418 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
419 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
423 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
426 int AliHLTComponent::GetEventCount() const
428 // see header file for function documentation
432 int AliHLTComponent::IncrementEventCounter()
434 // see header file for function documentation
435 if (fEventCount>=0) fEventCount++;
439 int AliHLTComponent::GetNumberOfInputBlocks() const
441 // see header file for function documentation
442 if (fpInputBlocks!=NULL) {
443 return fCurrentEventData.fBlockCnt;
448 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
449 const char* classname,
452 // see header file for function documentation
453 ALIHLTCOMPONENT_BASE_STOPWATCH();
455 if (classname) fClassName=classname;
456 else fClassName.clear();
457 int idx=FindInputBlock(fSearchDataType, 0, 1);
460 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
461 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
462 fCurrentInputBlock=idx;
469 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
470 const char* dtOrigin,
471 const char* classname,
474 // see header file for function documentation
475 ALIHLTCOMPONENT_BASE_STOPWATCH();
476 AliHLTComponentDataType dt;
477 SetDataType(dt, dtID, dtOrigin);
478 return GetFirstInputObject(dt, classname, bForce);
481 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
483 // see header file for function documentation
484 ALIHLTCOMPONENT_BASE_STOPWATCH();
485 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
486 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
489 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
490 fCurrentInputBlock=idx;
496 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
498 // see header file for function documentation
500 if (fpInputBlocks!=NULL) {
501 int idx=startIdx<0?0:startIdx;
502 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
504 if (fpInputBlocks[idx].fPtr==NULL) continue;
505 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
506 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
508 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
516 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
518 // see header file for function documentation
520 if (fpInputBlocks!=NULL) {
521 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
522 if (fpInputBlocks[idx].fPtr) {
523 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
524 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
525 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
526 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
527 TClass* objclass=msg.GetClass();
528 pObj=msg.ReadObject(objclass);
529 if (pObj && objclass) {
530 HLTDebug("object %p type %s created", pObj, objclass->GetName());
534 } else if (bForce!=0) {
535 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
538 HLTFatal("block descriptor empty");
541 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
544 HLTError("no input blocks available");
550 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
552 // see header file for function documentation
553 if (fpInputObjects==NULL) {
554 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
557 if (fpInputObjects) {
558 pObj=fpInputObjects->At(idx);
560 pObj=CreateInputObject(idx, bForce);
562 fpInputObjects->AddAt(pObj, idx);
566 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
571 int AliHLTComponent::CleanupInputObjects()
573 // see header file for function documentation
574 if (!fpInputObjects) return 0;
575 TObjArray* array=fpInputObjects;
577 for (int i=0; i<array->GetEntries(); i++) {
578 TObject* pObj=array->At(i);
579 // grrr, garbage collection strikes back: When read via AliHLTMessage
580 // (CreateInputObject), and written to a TFile afterwards, the
581 // TFile::Close calls ROOOT's garbage collection. No clue why the
582 // object ended up in the key list and needs to be deleted
583 if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
589 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
591 // see header file for function documentation
592 ALIHLTCOMPONENT_BASE_STOPWATCH();
593 AliHLTComponentDataType dt=kAliHLTVoidDataType;
594 int idx=fCurrentInputBlock;
596 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
598 HLTError("unknown object %p", pObject);
602 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
603 dt=fpInputBlocks[idx].fDataType;
605 HLTFatal("severe internal error, index out of range");
611 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
613 // see header file for function documentation
614 ALIHLTCOMPONENT_BASE_STOPWATCH();
615 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
616 int idx=fCurrentInputBlock;
618 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
620 HLTError("unknown object %p", pObject);
624 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
625 iSpec=fpInputBlocks[idx].fSpecification;
627 HLTFatal("severe internal error, index out of range");
633 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
635 // see header file for function documentation
636 ALIHLTCOMPONENT_BASE_STOPWATCH();
639 int idx=FindInputBlock(fSearchDataType, 0);
640 const AliHLTComponentBlockData* pBlock=NULL;
642 // check for fpInputBlocks pointer done in FindInputBlock
643 pBlock=&fpInputBlocks[idx];
644 fCurrentInputBlock=idx;
649 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
650 const char* dtOrigin)
652 // see header file for function documentation
653 ALIHLTCOMPONENT_BASE_STOPWATCH();
654 AliHLTComponentDataType dt;
655 SetDataType(dt, dtID, dtOrigin);
656 return GetFirstInputBlock(dt);
659 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index)
661 // see header file for function documentation
662 ALIHLTCOMPONENT_BASE_STOPWATCH();
663 assert( 0 <= index and index < fCurrentEventData.fBlockCnt );
664 return &fpInputBlocks[index];
667 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
669 // see header file for function documentation
670 ALIHLTCOMPONENT_BASE_STOPWATCH();
671 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
672 const AliHLTComponentBlockData* pBlock=NULL;
674 // check for fpInputBlocks pointer done in FindInputBlock
675 pBlock=&fpInputBlocks[idx];
676 fCurrentInputBlock=idx;
681 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
683 // see header file for function documentation
685 if (fpInputBlocks!=NULL) {
687 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
688 iResult=(int)(pBlock-fpInputBlocks);
697 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
699 // see header file for function documentation
700 ALIHLTCOMPONENT_BASE_STOPWATCH();
701 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
702 int idx=fCurrentInputBlock;
704 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
706 HLTError("unknown Block %p", pBlock);
710 // check for fpInputBlocks pointer done in FindInputBlock
711 iSpec=fpInputBlocks[idx].fSpecification;
716 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
717 void* pHeader, int headerSize)
719 // see header file for function documentation
720 ALIHLTCOMPONENT_BASE_STOPWATCH();
723 AliHLTMessage msg(kMESS_OBJECT);
724 msg.WriteObject(pObject);
725 Int_t iMsgLength=msg.Length();
727 msg.SetLength(); // sets the length to the first (reserved) word
728 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
730 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
733 HLTError("object serialization failed for object %p", pObject);
742 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
743 void* pHeader, int headerSize)
745 // see header file for function documentation
746 ALIHLTCOMPONENT_BASE_STOPWATCH();
747 AliHLTComponentDataType dt;
748 SetDataType(dt, dtID, dtOrigin);
749 return PushBack(pObject, dt, spec, pHeader, headerSize);
752 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
753 void* pHeader, int headerSize)
755 // see header file for function documentation
756 ALIHLTCOMPONENT_BASE_STOPWATCH();
757 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
760 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
761 void* pHeader, int headerSize)
763 // see header file for function documentation
764 ALIHLTCOMPONENT_BASE_STOPWATCH();
765 AliHLTComponentDataType dt;
766 SetDataType(dt, dtID, dtOrigin);
767 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
770 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
771 void* pHeader, int iHeaderSize)
773 // see header file for function documentation
775 int iBlkSize = iBufferSize + iHeaderSize;
777 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
778 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
779 AliHLTComponentBlockData bd;
781 bd.fOffset = fOutputBufferFilled;
785 bd.fSpecification = spec;
786 if (pHeader!=NULL && pHeader!=pTgt) {
787 memcpy(pTgt, pHeader, iHeaderSize);
790 pTgt += (AliHLTUInt8_t) iHeaderSize;
792 if (pBuffer!=NULL && pBuffer!=pTgt) {
793 memcpy(pTgt, pBuffer, iBufferSize);
795 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
796 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
798 fOutputBufferFilled+=bd.fSize;
799 fOutputBlocks.push_back( bd );
800 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
802 if (fpOutputBuffer) {
803 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
805 HLTError("output buffer not available");
815 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
817 // see header file for function documentation
818 if (!pObject) return -EINVAL;
819 AliHLTMessage msg(kMESS_OBJECT);
820 msg.WriteObject(pObject);
824 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
825 const char* dtOrigin,
828 // see header file for function documentation
829 ALIHLTCOMPONENT_BASE_STOPWATCH();
830 AliHLTComponentDataType dt;
831 SetDataType(dt, dtID, dtOrigin);
832 return CreateMemoryFile(capacity, dt, spec);
835 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
836 const AliHLTComponentDataType& dt,
839 // see header file for function documentation
840 ALIHLTCOMPONENT_BASE_STOPWATCH();
841 AliHLTMemoryFile* pFile=NULL;
842 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
843 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
844 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
846 unsigned int nofBlocks=fOutputBlocks.size();
847 if (nofBlocks+1>fMemFiles.size()) {
848 fMemFiles.resize(nofBlocks+1, NULL);
850 if (nofBlocks<fMemFiles.size()) {
851 fMemFiles[nofBlocks]=pFile;
852 AliHLTComponentBlockData bd;
854 bd.fOffset = fOutputBufferFilled;
858 bd.fSpecification = spec;
859 fOutputBufferFilled+=bd.fSize;
860 fOutputBlocks.push_back( bd );
862 HLTError("can not allocate/grow object array");
869 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
874 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
875 const char* dtOrigin,
879 // see header file for function documentation
880 ALIHLTCOMPONENT_BASE_STOPWATCH();
881 AliHLTComponentDataType dt;
882 SetDataType(dt, dtID, dtOrigin);
883 int size=fOutputBufferSize-fOutputBufferFilled;
884 if (capacity<0 || capacity>1.0) {
885 HLTError("invalid parameter: capacity %f", capacity);
888 size=(int)(size*capacity);
889 return CreateMemoryFile(size, dt, spec);
892 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
896 // see header file for function documentation
897 ALIHLTCOMPONENT_BASE_STOPWATCH();
898 int size=fOutputBufferSize-fOutputBufferFilled;
899 if (capacity<0 || capacity>1.0) {
900 HLTError("invalid parameter: capacity %f", capacity);
903 size=(int)(size*capacity);
904 return CreateMemoryFile(size, dt, spec);
907 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
908 const char* key, int option)
911 if (pFile && pObject) {
913 iResult=pObject->Write(key, option);
917 iResult=-pFile->GetErrno();
918 if (iResult==-ENOSPC) {
919 HLTError("error writing memory file, buffer too small");
928 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
932 vector<AliHLTMemoryFile*>::iterator element=fMemFiles.begin();
934 while (element!=fMemFiles.end() && iResult>=0) {
935 if (*element && *element==pFile) {
936 iResult=pFile->Close();
938 // sync memory files and descriptors
940 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
948 HLTError("can not find memory file %p", pFile);
956 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
958 // see header file for function documentation
960 //#warning function not yet implemented
961 HLTWarning("function not yet implemented");
965 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
966 const AliHLTComponentBlockData* blocks,
967 AliHLTComponentTriggerData& trigData,
968 AliHLTUInt8_t* outputPtr,
969 AliHLTUInt32_t& size,
970 AliHLTUInt32_t& outputBlockCnt,
971 AliHLTComponentBlockData*& outputBlocks,
972 AliHLTComponentEventDoneData*& edd )
974 // see header file for function documentation
975 ALIHLTCOMPONENT_BASE_STOPWATCH();
977 fCurrentEvent=evtData.fEventID;
978 fCurrentEventData=evtData;
979 fpInputBlocks=blocks;
980 fCurrentInputBlock=-1;
981 fSearchDataType=kAliHLTAnyDataType;
982 fpOutputBuffer=outputPtr;
983 fOutputBufferSize=size;
984 fOutputBufferFilled=0;
985 fOutputBlocks.clear();
987 // find special events
989 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
990 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
992 if (fpRunDesc==NULL) {
993 fpRunDesc=new AliHLTRunDesc;
995 if ((iResult=CopyStruct(fpRunDesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
996 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1002 HLTWarning("already received SOR event run no %d, ignoring SOR", fpRunDesc->fRunNo);
1004 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1005 if (fpRunDesc!=NULL) {
1007 AliHLTRunDesc rundesc;
1008 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1009 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1010 HLTWarning("run no missmatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1012 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1015 AliHLTRunDesc* pRunDesc=fpRunDesc;
1020 HLTWarning("did not receive SOR, ignoring EOR");
1023 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1029 vector<AliHLTComponentBlockData> blockData;
1030 { // dont delete, sets the scope for the stopwatch guard
1031 ALIHLTCOMPONENT_DA_STOPWATCH();
1032 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1033 } // end of the scope of the stopwatch guard
1035 if (fOutputBlocks.size()>0) {
1036 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1038 // sync memory files and descriptors
1039 vector<AliHLTMemoryFile*>::iterator element=fMemFiles.begin();
1041 while (element!=fMemFiles.end() && iResult>=0) {
1043 if ((*element)->IsClosed()==0) {
1044 HLTWarning("memory file has not been closed, force flush");
1045 iResult=CloseMemoryFile(*element);
1052 // create the descriptor list
1053 if (blockData.size()>0) {
1054 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1057 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1058 size=fOutputBufferFilled;
1062 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1065 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1072 CleanupInputObjects();
1074 IncrementEventCounter();
1079 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1084 // standard constructor (not for use)
1087 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1089 fpStopwatch(pStopwatch),
1094 // check for already existing guard
1095 if (fgpCurrent) fpPrec=fgpCurrent;
1098 // stop the preceeding guard if it controls a different stopwatch
1100 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1102 // start the stopwatch if the current guard controls a different one
1103 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1106 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
1112 // copy constructor not for use
1116 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1119 // assignment operator not for use
1126 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1128 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1132 // resume the preceeding guard if it controls a different stopwatch
1134 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1136 // stop the stopwatch if the current guard controls a different one
1137 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1139 // resume to the preceeding guard
1143 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1145 // see header file for function documentation
1146 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1147 return fpStopwatch!=pSucc?1:0;
1150 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1152 // see header file for function documentation
1153 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1154 return fpStopwatch!=pSucc?1:0;
1157 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
1159 // see header file for function documentation
1161 if (pSW!=NULL && type<kSWTypeCount) {
1162 if (fpStopwatches) {
1163 TObject* pObj=fpStopwatches->At((int)type);
1164 if (pSW==NULL // explicit reset
1165 || pObj==NULL) { // explicit set
1166 fpStopwatches->AddAt(pSW, (int)type);
1167 } else if (pObj!=pSW) {
1168 HLTWarning("stopwatch %d already set, reset first", (int)type);
1178 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1180 // see header file for function documentation
1181 if (pStopwatches==NULL) return -EINVAL;
1184 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1185 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1189 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1191 if (fpRunDesc==NULL) return 0;
1192 return fpRunDesc->fRunNo;
1195 AliHLTUInt32_t AliHLTComponent::GetRunType() const
1197 if (fpRunDesc==NULL) return 0;
1198 return fpRunDesc->fRunType;
1201 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
1202 const char* structname, const char* eventname)
1205 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1206 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1207 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1208 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
1209 AliHLTUInt8_t* pSrc=((AliHLTUInt8_t*)fpInputBlocks[iBlockNo].fPtr)+fpInputBlocks[iBlockNo].fOffset;
1210 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)pSrc);
1211 if (fpInputBlocks[iBlockNo].fSize!=copy) {
1212 HLTWarning("%s event: missmatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1213 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1215 if (copy!=iStructSize) {
1216 HLTWarning("%s event: missmatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1217 if (copy>iStructSize) {
1220 memset(pTgt, 0, iStructSize);
1223 memcpy(pTgt, pSrc, copy);
1227 HLTWarning("%s event: missing data block", eventname);
1233 HLTError("invalid struct");