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"
41 #include "TObjArray.h"
42 #include "TObjectTable.h"
44 #include "TStopwatch.h"
45 #include "AliHLTMemoryFile.h"
48 /** ROOT macro for the implementation of ROOT specific class methods */
49 ClassImp(AliHLTComponent);
51 /** stopwatch macro using the stopwatch guard */
52 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
53 //#define ALIHLTCOMPONENT_STOPWATCH(type)
55 /** stopwatch macro for operations of the base class */
56 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
57 /** stopwatch macro for operations of the detector algorithm (DA) */
58 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
60 AliHLTComponent::AliHLTComponent()
68 fCurrentInputBlock(-1),
69 fSearchDataType(kAliHLTVoidDataType),
74 fOutputBufferFilled(0),
76 fpStopwatches(new TObjArray(kSWTypeCount)),
82 // see header file for class documentation
84 // refer to README to build package
86 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
87 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
88 if (fgpComponentHandler)
89 fgpComponentHandler->ScheduleRegister(this);
90 //SetLocalLoggingLevel(kHLTLogDefault);
93 AliHLTComponent::~AliHLTComponent()
95 // see header file for function documentation
96 CleanupInputObjects();
97 if (fpStopwatches!=NULL) delete fpStopwatches;
99 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
100 while (element!=fMemFiles.end()) {
102 if ((*element)->IsClosed()==0) {
103 HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
104 // close but do not flush as we dont know whether the buffer is still valid
105 (*element)->CloseMemoryFile(0);
114 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
116 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
118 // see header file for function documentation
120 if (fgpComponentHandler==NULL || bOverwrite!=0)
121 fgpComponentHandler=pCH;
127 int AliHLTComponent::UnsetGlobalComponentHandler()
129 // see header file for function documentation
130 return SetGlobalComponentHandler(NULL,1);
133 int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
135 // see header file for function documentation
138 memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
139 fEnvironment.fParam=environParam;
141 const char** pArguments=NULL;
146 pArguments=new const char*[argc];
148 for (int i=0; i<argc && iResult>=0; i++) {
150 if (argument.IsNull()) continue;
153 if (argument.CompareTo("benchmark")==0) {
156 } else if (argument.CompareTo("loglevel")==0) {
157 if ((bMissingParam=(++i>=argc))) break;
158 TString parameter(argv[i]);
159 parameter.Remove(TString::kLeading, ' '); // remove all blanks
160 if (parameter.BeginsWith("0x") &&
161 parameter.Replace(0,2,"",0).IsHex()) {
162 AliHLTComponentLogSeverity loglevel=kHLTLogNone;
163 sscanf(parameter.Data(),"%x", (unsigned int*)&loglevel);
164 SetLocalLoggingLevel(loglevel);
166 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
170 pArguments[iNofChildArgs++]=argv[i];
178 HLTError("missing parameter for argument %s", argument.Data());
182 iResult=DoInit(iNofChildArgs, pArguments);
184 if (iResult>=0) fEventCount=0;
185 if (pArguments) delete [] pArguments;
189 int AliHLTComponent::Deinit()
191 // see header file for function documentation
195 HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
196 AliHLTRunDesc* pRunDesc=fpRunDesc;
203 int AliHLTComponent::DoInit( int argc, const char** argv )
205 // see header file for function documentation
206 if (argc==0 && argv==NULL) {
207 // this is currently just to get rid of the warning "unused parameter"
213 int AliHLTComponent::DoDeinit()
215 // see header file for function documentation
220 int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
222 HLTLogKeyword("dummy");
226 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
228 // see header file for function documentation
229 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
230 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
231 strcat( output, ":" );
232 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
235 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
237 // see header file for function documentation
243 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
244 sprintf(tmp, "'%d", type.fOrigin[i]);
248 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
249 sprintf(tmp, "%d'", type.fID[i]);
258 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
259 unsigned char* puc=(unsigned char*)type.fOrigin;
261 sprintf(tmp, "'\\%x", type.fOrigin[i]);
263 sprintf(tmp, "'%c", type.fOrigin[i]);
267 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
268 unsigned char* puc=(unsigned char*)type.fID;
270 sprintf(tmp, "\\%x'", type.fID[i]);
272 sprintf(tmp, "%c'", type.fID[i]);
278 if (type==kAliHLTVoidDataType) {
281 // some gymnastics in order to avoid a '0' which is part of either or both
282 // ID and origin terminating the whole string. Unfortunately, string doesn't
283 // stop appending at the '0' if the number of elements to append was
284 // explicitely specified
286 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
287 out.append(tmp.c_str());
290 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
291 out.append(tmp.c_str());
297 void* AliHLTComponent::AllocMemory( unsigned long size )
299 // see header file for function documentation
300 if (fEnvironment.fAllocMemoryFunc)
301 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
302 HLTFatal("no memory allocation handler registered");
306 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
307 AliHLTComponentBlockData** outputBlocks )
309 // see header file for function documentation
310 if ( blockCount==NULL || outputBlocks==NULL )
312 AliHLTUInt32_t count = blocks.size();
316 *outputBlocks = NULL;
319 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
320 if ( !*outputBlocks )
322 for ( unsigned long i = 0; i < count; i++ ) {
323 (*outputBlocks)[i] = blocks[i];
324 if (blocks[i].fDataType==kAliHLTAnyDataType) {
325 (*outputBlocks)[i].fDataType=GetOutputDataType();
326 /* data type was set to the output data type by the PubSub AliRoot
327 Wrapper component, if data type of the block was ********:****.
328 Now handled by the component base class in order to have same
329 behavior when running embedded in AliRoot
330 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
331 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
340 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
342 // see header file for function documentation
343 if (fEnvironment.fGetEventDoneDataFunc)
344 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
348 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
350 // see header file for function documentation
353 AliHLTComponentDataTypeList itypes;
354 AliHLTComponentDataTypeList otypes;
355 otypes.push_back(GetOutputDataType());
356 if (otypes[0]==kAliHLTMultipleDataType) {
359 if ((count=GetOutputDataTypes(otypes))>0) {
360 } else if (GetComponentType()!=kSink) {
361 HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
364 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(itypes);
365 AliHLTComponentDataTypeList::iterator itype=itypes.begin();
366 while (itype!=itypes.end()) {
367 //PrintDataTypeContent((*itype), "consumer \'%s\'");
368 AliHLTComponentDataTypeList::iterator otype=otypes.begin();
369 while (otype!=otypes.end() && (*itype)!=(*otype)) otype++;
370 //if (otype!=otypes.end()) PrintDataTypeContent(*otype, "publisher \'%s\'");
371 if (otype!=otypes.end() ||
372 (*itype)==kAliHLTAnyDataType) {
373 if (tgtList) tgtList->push_back(*itype);
384 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
386 // see header file for function documentation
387 const char* fmt="publisher \'%s\'";
388 if (format) fmt=format;
389 HLTMessage(fmt, (DataType2Text(dt)).c_str());
390 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
405 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
407 // see header file for function documentation
408 blockData.fStructSize = sizeof(blockData);
409 FillShmData( blockData.fShmKey );
410 blockData.fOffset = ~(AliHLTUInt32_t)0;
411 blockData.fPtr = NULL;
413 FillDataType( blockData.fDataType );
414 blockData.fSpecification = kAliHLTVoidDataSpec;
417 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
419 // see header file for function documentation
420 shmData.fStructSize = sizeof(shmData);
421 shmData.fShmType = gkAliHLTComponentInvalidShmType;
422 shmData.fShmID = gkAliHLTComponentInvalidShmID;
425 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
427 // see header file for function documentation
428 dataType=kAliHLTAnyDataType;
431 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
433 // see header file for function documentation
434 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
435 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
438 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
440 // see header file for function documentation
441 tgtdt.fStructSize=sizeof(AliHLTComponentDataType);
443 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
444 strncpy(&tgtdt.fID[0], id, strlen(id)<(size_t)kAliHLTComponentDataTypefIDsize?strlen(id):kAliHLTComponentDataTypefIDsize);
447 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
448 strncpy(&tgtdt.fOrigin[0], origin, strlen(origin)<(size_t)kAliHLTComponentDataTypefOriginSize?strlen(origin):kAliHLTComponentDataTypefOriginSize);
452 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
454 // see header file for function documentation
455 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
456 evtData.fStructSize=sizeof(AliHLTComponentEventData);
459 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
461 // see header file for function documentation
463 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
464 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
465 if (dt.fID[i]!=0) msg+=dt.fID[i];
469 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
470 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
474 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
477 int AliHLTComponent::GetEventCount() const
479 // see header file for function documentation
483 int AliHLTComponent::IncrementEventCounter()
485 // see header file for function documentation
486 if (fEventCount>=0) fEventCount++;
490 int AliHLTComponent::GetNumberOfInputBlocks() const
492 // see header file for function documentation
493 if (fpInputBlocks!=NULL) {
494 return fCurrentEventData.fBlockCnt;
499 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
500 const char* classname,
503 // see header file for function documentation
504 ALIHLTCOMPONENT_BASE_STOPWATCH();
506 if (classname) fClassName=classname;
507 else fClassName.clear();
508 int idx=FindInputBlock(fSearchDataType, 0, 1);
511 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
512 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
513 fCurrentInputBlock=idx;
520 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
521 const char* dtOrigin,
522 const char* classname,
525 // see header file for function documentation
526 ALIHLTCOMPONENT_BASE_STOPWATCH();
527 AliHLTComponentDataType dt;
528 SetDataType(dt, dtID, dtOrigin);
529 return GetFirstInputObject(dt, classname, bForce);
532 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
534 // see header file for function documentation
535 ALIHLTCOMPONENT_BASE_STOPWATCH();
536 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
537 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
540 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
541 fCurrentInputBlock=idx;
547 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
549 // see header file for function documentation
551 if (fpInputBlocks!=NULL) {
552 int idx=startIdx<0?0:startIdx;
553 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
555 if (fpInputBlocks[idx].fPtr==NULL) continue;
556 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
557 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
559 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt ||
560 (memcmp(dt.fID, kAliHLTAnyDataTypeID, kAliHLTComponentDataTypefIDsize)==0 &&
561 memcmp(dt.fOrigin, fpInputBlocks[idx].fDataType.fOrigin, kAliHLTComponentDataTypefOriginSize)==0) ||
562 (memcmp(dt.fID, fpInputBlocks[idx].fDataType.fID, kAliHLTComponentDataTypefIDsize)==0) &&
563 memcmp(dt.fOrigin, kAliHLTDataOriginAny, kAliHLTComponentDataTypefOriginSize)==0) {
571 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
573 // see header file for function documentation
575 if (fpInputBlocks!=NULL) {
576 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
577 if (fpInputBlocks[idx].fPtr) {
578 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
579 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
580 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
581 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
582 TClass* objclass=msg.GetClass();
583 pObj=msg.ReadObject(objclass);
584 if (pObj && objclass) {
585 HLTDebug("object %p type %s created", pObj, objclass->GetName());
589 } else if (bForce!=0) {
590 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
593 HLTFatal("block descriptor empty");
596 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
599 HLTError("no input blocks available");
605 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
607 // see header file for function documentation
608 if (fpInputObjects==NULL) {
609 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
612 if (fpInputObjects) {
613 pObj=fpInputObjects->At(idx);
615 pObj=CreateInputObject(idx, bForce);
617 fpInputObjects->AddAt(pObj, idx);
621 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
626 int AliHLTComponent::CleanupInputObjects()
628 // see header file for function documentation
629 if (!fpInputObjects) return 0;
630 TObjArray* array=fpInputObjects;
632 for (int i=0; i<array->GetEntries(); i++) {
633 TObject* pObj=array->At(i);
634 // grrr, garbage collection strikes back: When read via AliHLTMessage
635 // (CreateInputObject), and written to a TFile afterwards, the
636 // TFile::Close calls ROOOT's garbage collection. No clue why the
637 // object ended up in the key list and needs to be deleted
638 if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
644 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
646 // see header file for function documentation
647 ALIHLTCOMPONENT_BASE_STOPWATCH();
648 AliHLTComponentDataType dt=kAliHLTVoidDataType;
649 int idx=fCurrentInputBlock;
651 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
653 HLTError("unknown object %p", pObject);
657 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
658 dt=fpInputBlocks[idx].fDataType;
660 HLTFatal("severe internal error, index out of range");
666 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
668 // see header file for function documentation
669 ALIHLTCOMPONENT_BASE_STOPWATCH();
670 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
671 int idx=fCurrentInputBlock;
673 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
675 HLTError("unknown object %p", pObject);
679 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
680 iSpec=fpInputBlocks[idx].fSpecification;
682 HLTFatal("severe internal error, index out of range");
688 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
690 // see header file for function documentation
691 ALIHLTCOMPONENT_BASE_STOPWATCH();
694 int idx=FindInputBlock(fSearchDataType, 0);
695 const AliHLTComponentBlockData* pBlock=NULL;
697 // check for fpInputBlocks pointer done in FindInputBlock
698 pBlock=&fpInputBlocks[idx];
699 fCurrentInputBlock=idx;
704 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
705 const char* dtOrigin)
707 // see header file for function documentation
708 ALIHLTCOMPONENT_BASE_STOPWATCH();
709 AliHLTComponentDataType dt;
710 SetDataType(dt, dtID, dtOrigin);
711 return GetFirstInputBlock(dt);
714 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index)
716 // see header file for function documentation
717 ALIHLTCOMPONENT_BASE_STOPWATCH();
718 assert( 0 <= index and index < fCurrentEventData.fBlockCnt );
719 return &fpInputBlocks[index];
722 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
724 // see header file for function documentation
725 ALIHLTCOMPONENT_BASE_STOPWATCH();
726 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
727 const AliHLTComponentBlockData* pBlock=NULL;
729 // check for fpInputBlocks pointer done in FindInputBlock
730 pBlock=&fpInputBlocks[idx];
731 fCurrentInputBlock=idx;
736 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
738 // see header file for function documentation
740 if (fpInputBlocks!=NULL) {
742 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
743 iResult=(int)(pBlock-fpInputBlocks);
752 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
754 // see header file for function documentation
755 ALIHLTCOMPONENT_BASE_STOPWATCH();
756 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
757 int idx=fCurrentInputBlock;
759 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
761 HLTError("unknown Block %p", pBlock);
765 // check for fpInputBlocks pointer done in FindInputBlock
766 iSpec=fpInputBlocks[idx].fSpecification;
771 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
772 void* pHeader, int headerSize)
774 // see header file for function documentation
775 ALIHLTCOMPONENT_BASE_STOPWATCH();
778 AliHLTMessage msg(kMESS_OBJECT);
779 msg.WriteObject(pObject);
780 Int_t iMsgLength=msg.Length();
782 msg.SetLength(); // sets the length to the first (reserved) word
783 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
785 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
788 HLTError("object serialization failed for object %p", pObject);
797 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
798 void* pHeader, int headerSize)
800 // see header file for function documentation
801 ALIHLTCOMPONENT_BASE_STOPWATCH();
802 AliHLTComponentDataType dt;
803 SetDataType(dt, dtID, dtOrigin);
804 return PushBack(pObject, dt, spec, pHeader, headerSize);
807 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
808 void* pHeader, int headerSize)
810 // see header file for function documentation
811 ALIHLTCOMPONENT_BASE_STOPWATCH();
812 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
815 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
816 void* pHeader, int headerSize)
818 // see header file for function documentation
819 ALIHLTCOMPONENT_BASE_STOPWATCH();
820 AliHLTComponentDataType dt;
821 SetDataType(dt, dtID, dtOrigin);
822 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
825 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
826 void* pHeader, int iHeaderSize)
828 // see header file for function documentation
830 int iBlkSize = iBufferSize + iHeaderSize;
832 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
833 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
834 AliHLTComponentBlockData bd;
836 bd.fOffset = fOutputBufferFilled;
839 bd.fSpecification = spec;
840 if (pHeader!=NULL && pHeader!=pTgt) {
841 memcpy(pTgt, pHeader, iHeaderSize);
844 pTgt += (AliHLTUInt8_t) iHeaderSize;
846 if (pBuffer!=NULL && pBuffer!=pTgt) {
847 memcpy(pTgt, pBuffer, iBufferSize);
849 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
850 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
852 fOutputBufferFilled+=bd.fSize;
853 fOutputBlocks.push_back( bd );
854 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
856 if (fpOutputBuffer) {
857 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
859 HLTError("output buffer not available");
869 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
871 // see header file for function documentation
872 if (!pObject) return -EINVAL;
873 AliHLTMessage msg(kMESS_OBJECT);
874 msg.WriteObject(pObject);
878 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
879 const char* dtOrigin,
882 // see header file for function documentation
883 ALIHLTCOMPONENT_BASE_STOPWATCH();
884 AliHLTComponentDataType dt;
885 SetDataType(dt, dtID, dtOrigin);
886 return CreateMemoryFile(capacity, dt, spec);
889 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
890 const AliHLTComponentDataType& dt,
893 // see header file for function documentation
894 ALIHLTCOMPONENT_BASE_STOPWATCH();
895 AliHLTMemoryFile* pFile=NULL;
896 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
897 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
898 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
900 unsigned int nofBlocks=fOutputBlocks.size();
901 if (nofBlocks+1>fMemFiles.size()) {
902 fMemFiles.resize(nofBlocks+1, NULL);
904 if (nofBlocks<fMemFiles.size()) {
905 fMemFiles[nofBlocks]=pFile;
906 AliHLTComponentBlockData bd;
908 bd.fOffset = fOutputBufferFilled;
911 bd.fSpecification = spec;
912 fOutputBufferFilled+=bd.fSize;
913 fOutputBlocks.push_back( bd );
915 HLTError("can not allocate/grow object array");
916 pFile->CloseMemoryFile(0);
922 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
927 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
928 const char* dtOrigin,
932 // see header file for function documentation
933 ALIHLTCOMPONENT_BASE_STOPWATCH();
934 AliHLTComponentDataType dt;
935 SetDataType(dt, dtID, dtOrigin);
936 int size=fOutputBufferSize-fOutputBufferFilled;
937 if (capacity<0 || capacity>1.0) {
938 HLTError("invalid parameter: capacity %f", capacity);
941 size=(int)(size*capacity);
942 return CreateMemoryFile(size, dt, spec);
945 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
949 // see header file for function documentation
950 ALIHLTCOMPONENT_BASE_STOPWATCH();
951 int size=fOutputBufferSize-fOutputBufferFilled;
952 if (capacity<0 || capacity>1.0) {
953 HLTError("invalid parameter: capacity %f", capacity);
956 size=(int)(size*capacity);
957 return CreateMemoryFile(size, dt, spec);
960 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
961 const char* key, int option)
963 // see header file for function documentation
965 if (pFile && pObject) {
967 iResult=pObject->Write(key, option);
971 iResult=-pFile->GetErrno();
972 if (iResult==-ENOSPC) {
973 HLTError("error writing memory file, buffer too small");
982 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
984 // see header file for function documentation
987 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
989 while (element!=fMemFiles.end() && iResult>=0) {
990 if (*element && *element==pFile) {
991 iResult=pFile->CloseMemoryFile();
993 // sync memory files and descriptors
995 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
1003 HLTError("can not find memory file %p", pFile);
1011 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData /*edd*/)
1013 // see header file for function documentation
1014 int iResult=-ENOSYS;
1015 //#warning function not yet implemented
1016 HLTWarning("function not yet implemented");
1020 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1021 const AliHLTComponentBlockData* blocks,
1022 AliHLTComponentTriggerData& trigData,
1023 AliHLTUInt8_t* outputPtr,
1024 AliHLTUInt32_t& size,
1025 AliHLTUInt32_t& outputBlockCnt,
1026 AliHLTComponentBlockData*& outputBlocks,
1027 AliHLTComponentEventDoneData*& edd )
1029 // see header file for function documentation
1030 ALIHLTCOMPONENT_BASE_STOPWATCH();
1032 fCurrentEvent=evtData.fEventID;
1033 fCurrentEventData=evtData;
1034 fpInputBlocks=blocks;
1035 fCurrentInputBlock=-1;
1036 fSearchDataType=kAliHLTAnyDataType;
1037 fpOutputBuffer=outputPtr;
1038 fOutputBufferSize=size;
1039 fOutputBufferFilled=0;
1040 fOutputBlocks.clear();
1042 // find special events
1043 if (fpInputBlocks) {
1044 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1045 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1047 if (fpRunDesc==NULL) {
1048 fpRunDesc=new AliHLTRunDesc;
1050 if ((iResult=CopyStruct(fpRunDesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1051 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1057 HLTWarning("already received SOR event run no %d, ignoring SOR", fpRunDesc->fRunNo);
1059 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1060 if (fpRunDesc!=NULL) {
1062 AliHLTRunDesc rundesc;
1063 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1064 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1065 HLTWarning("run no missmatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1067 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1070 AliHLTRunDesc* pRunDesc=fpRunDesc;
1075 HLTWarning("did not receive SOR, ignoring EOR");
1078 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1084 AliHLTComponentBlockDataList blockData;
1085 { // dont delete, sets the scope for the stopwatch guard
1086 ALIHLTCOMPONENT_DA_STOPWATCH();
1087 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1088 } // end of the scope of the stopwatch guard
1090 if (fOutputBlocks.size()>0) {
1091 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1093 // sync memory files and descriptors
1094 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1096 while (element!=fMemFiles.end() && iResult>=0) {
1098 if ((*element)->IsClosed()==0) {
1099 HLTWarning("memory file has not been closed, force flush");
1100 iResult=CloseMemoryFile(*element);
1107 // create the descriptor list
1108 if (blockData.size()>0) {
1109 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1112 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1113 size=fOutputBufferFilled;
1117 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1120 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1127 CleanupInputObjects();
1129 IncrementEventCounter();
1134 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1139 // standard constructor (not for use)
1142 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1144 fpStopwatch(pStopwatch),
1149 // check for already existing guard
1150 if (fgpCurrent) fpPrec=fgpCurrent;
1153 // stop the preceeding guard if it controls a different stopwatch
1155 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1157 // start the stopwatch if the current guard controls a different one
1158 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1161 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
1167 // copy constructor not for use
1171 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1174 // assignment operator not for use
1181 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1183 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1187 // resume the preceeding guard if it controls a different stopwatch
1189 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1191 // stop the stopwatch if the current guard controls a different one
1192 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1194 // resume to the preceeding guard
1198 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1200 // see header file for function documentation
1201 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1202 return fpStopwatch!=pSucc?1:0;
1205 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1207 // see header file for function documentation
1208 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1209 return fpStopwatch!=pSucc?1:0;
1212 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
1214 // see header file for function documentation
1216 if (pSW!=NULL && type<kSWTypeCount) {
1217 if (fpStopwatches) {
1218 TObject* pObj=fpStopwatches->At((int)type);
1219 if (pSW==NULL // explicit reset
1220 || pObj==NULL) { // explicit set
1221 fpStopwatches->AddAt(pSW, (int)type);
1222 } else if (pObj!=pSW) {
1223 HLTWarning("stopwatch %d already set, reset first", (int)type);
1233 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1235 // see header file for function documentation
1236 if (pStopwatches==NULL) return -EINVAL;
1239 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1240 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1244 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1246 // see header file for function documentation
1247 if (fpRunDesc==NULL) return 0;
1248 return fpRunDesc->fRunNo;
1251 AliHLTUInt32_t AliHLTComponent::GetRunType() const
1253 // see header file for function documentation
1254 if (fpRunDesc==NULL) return 0;
1255 return fpRunDesc->fRunType;
1258 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
1259 const char* structname, const char* eventname)
1261 // see header file for function documentation
1263 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1264 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1265 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1266 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
1267 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
1268 if (fpInputBlocks[iBlockNo].fSize!=copy) {
1269 HLTWarning("%s event: missmatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1270 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1272 if (copy!=iStructSize) {
1273 HLTWarning("%s event: missmatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1274 if (copy>iStructSize) {
1277 memset(pTgt, 0, iStructSize);
1280 memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
1284 HLTWarning("%s event: missing data block", eventname);
1290 HLTError("invalid struct");
1296 void AliHLTComponent::SetDDLBit(AliHLTEventDDL &list, Int_t ddlId, Bool_t state ) const
1298 // see header file for function documentation
1300 // -- Detector offset
1301 Int_t ddlIdBase = TMath::FloorNint( (Double_t) ddlId / 256.0 );
1303 // -- Word Base = 1. word of detector ( TPC has 8 words, TOF 3 )
1306 if ( ddlIdBase <= 3 )
1307 wordBase = ddlIdBase;
1308 else if ( ddlIdBase > 3 && ddlIdBase < 5 )
1309 wordBase = ddlIdBase + 7;
1311 wordBase = ddlIdBase + 9;
1313 // -- Bit index in Word
1314 Int_t bitIdx = ddlId % 32;
1317 Int_t wordIdx = wordBase;
1319 // -- if TPC (3) or TOD (5) add word idx
1320 if ( ( ddlIdBase == 3 ) || ( ddlIdBase == 5 ) ) {
1321 wordIdx += TMath::FloorNint( (Double_t) ( ddlId - ( ddlIdBase * 256 ) ) / 32.0 );
1324 // -- Set -- 'OR' word with bit mask;
1326 list.fList[wordIdx] |= ( 0x00000001 << bitIdx );
1327 // -- Unset -- 'AND' word with bit mask;
1329 list.fList[wordIdx] &= ( 0xFFFFFFFF ^ ( 0x00000001 << bitIdx ) );
1332 Int_t AliHLTComponent::GetFirstUsedDDLWord(AliHLTEventDDL &list) const
1334 // see header file for function documentation
1338 for ( Int_t wordNdx = 0 ; wordNdx < gkAliHLTDDLListSize ; wordNdx++ ) {
1340 if ( list.fList[wordNdx] != 0 && iResult == -1 ) {
1341 // check for special cases TPC and TOF
1342 if ( wordNdx > 3 && wordNdx <= 10 ) {
1346 else if ( wordNdx > 12 && wordNdx <= 14 ) {
1353 else if ( list.fList[wordNdx] != 0 && iResult >= 0 ) {
1354 HLTError( "DDLIDs for minimum of TWO detectors ( %d, %d ) set, this function works only for ONE detector.", iResult, wordNdx );