3 /**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
9 * for The ALICE HLT Project. *
11 * Permission to use, copy, modify and distribute this software and its *
12 * documentation strictly for non-commercial purposes is hereby granted *
13 * without fee, provided that the above copyright notice appears in all *
14 * copies and that both the copyright notice and this permission notice *
15 * appear in the supporting documentation. The authors make no claims *
16 * about the suitability of this software for any purpose. It is *
17 * provided "as is" without express or implied warranty. *
18 **************************************************************************/
20 /** @file AliHLTComponent.cxx
21 @author Matthias Richter, Timm Steinbeck
23 @brief Base class implementation for HLT components. */
29 //#include "AliHLTStdIncludes.h"
30 #include "AliHLTComponent.h"
31 #include "AliHLTComponentHandler.h"
32 #include "AliHLTMessage.h"
34 #include "TObjArray.h"
35 #include "TObjectTable.h"
37 #include "TStopwatch.h"
38 #include "AliHLTMemoryFile.h"
40 /** ROOT macro for the implementation of ROOT specific class methods */
41 ClassImp(AliHLTComponent);
43 /** stopwatch macro using the stopwatch guard */
44 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
45 //#define ALIHLTCOMPONENT_STOPWATCH(type)
47 /** stopwatch macro for operations of the base class */
48 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
49 /** stopwatch macro for operations of the detector algorithm (DA) */
50 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
52 AliHLTComponent::AliHLTComponent()
60 fCurrentInputBlock(-1),
61 fSearchDataType(kAliHLTVoidDataType),
66 fOutputBufferFilled(0),
68 fpStopwatches(new TObjArray(kSWTypeCount)),
74 // see header file for class documentation
76 // refer to README to build package
78 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
79 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
80 if (fgpComponentHandler)
81 fgpComponentHandler->ScheduleRegister(this);
82 //SetLocalLoggingLevel(kHLTLogDefault);
85 AliHLTComponent::AliHLTComponent(const AliHLTComponent&)
94 fCurrentInputBlock(-1),
95 fSearchDataType(kAliHLTVoidDataType),
100 fOutputBufferFilled(0),
107 // see header file for class documentation
110 AliHLTComponent& AliHLTComponent::operator=(const AliHLTComponent&)
112 // see header file for class documentation
116 AliHLTComponent::~AliHLTComponent()
118 // see header file for function documentation
119 CleanupInputObjects();
120 if (fpStopwatches!=NULL) delete fpStopwatches;
122 vector<AliHLTMemoryFile*>::iterator element=fMemFiles.begin();
123 while (element!=fMemFiles.end()) {
125 if ((*element)->IsClosed()==0) {
126 HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
127 // close but do not flush as we dont know whether the buffer is still valid
128 (*element)->Close(0);
137 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
139 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
141 // see header file for function documentation
143 if (fgpComponentHandler==NULL || bOverwrite!=0)
144 fgpComponentHandler=pCH;
150 int AliHLTComponent::UnsetGlobalComponentHandler()
152 // see header file for function documentation
153 return SetGlobalComponentHandler(NULL,1);
156 int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
158 // see header file for function documentation
161 memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
162 fEnvironment.fParam=environParam;
164 const char** pArguments=NULL;
169 pArguments=new const char*[argc];
171 for (int i=0; i<argc && iResult>=0; i++) {
173 if (argument.IsNull()) continue;
176 if (argument.CompareTo("benchmark")==0) {
179 } else if (argument.CompareTo("loglevel")==0) {
180 if ((bMissingParam=(++i>=argc))) break;
181 TString parameter(argv[i]);
182 parameter.Remove(TString::kLeading, ' '); // remove all blanks
183 if (parameter.BeginsWith("0x") &&
184 parameter.Replace(0,2,"",0).IsHex()) {
185 AliHLTComponentLogSeverity loglevel=kHLTLogNone;
186 sscanf(parameter.Data(),"%x", (unsigned int*)&loglevel);
187 SetLocalLoggingLevel(loglevel);
189 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
193 pArguments[iNofChildArgs++]=argv[i];
201 HLTError("missing parameter for argument %s", argument.Data());
205 iResult=DoInit(iNofChildArgs, pArguments);
207 if (iResult>=0) fEventCount=0;
208 if (pArguments) delete [] pArguments;
212 int AliHLTComponent::Deinit()
214 // see header file for function documentation
218 HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
219 AliHLTRunDesc* pRunDesc=fpRunDesc;
226 int AliHLTComponent::DoInit( int argc, const char** argv )
228 // see header file for function documentation
229 if (argc==0 && argv==NULL) {
230 // this is currently just to get rid of the warning "unused parameter"
236 int AliHLTComponent::DoDeinit()
238 // see header file for function documentation
243 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
245 // see header file for function documentation
246 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
247 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
248 strcat( output, ":" );
249 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
252 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type )
254 // see header file for function documentation
257 if (type==kAliHLTVoidDataType) {
260 // some gymnastics in order to avoid a '0' which is part of either or both
261 // ID and origin terminating the whole string. Unfortunately, string doesn't
262 // stop appending at the '0' if the number of elements to append was
263 // explicitely specified
265 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
266 out.append(tmp.c_str());
269 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
270 out.append(tmp.c_str());
276 void* AliHLTComponent::AllocMemory( unsigned long size )
278 // see header file for function documentation
279 if (fEnvironment.fAllocMemoryFunc)
280 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
281 HLTFatal("no memory allocation handler registered");
285 int AliHLTComponent::MakeOutputDataBlockList( const vector<AliHLTComponentBlockData>& blocks, AliHLTUInt32_t* blockCount,
286 AliHLTComponentBlockData** outputBlocks )
288 // see header file for function documentation
289 if ( blockCount==NULL || outputBlocks==NULL )
291 AliHLTUInt32_t count = blocks.size();
295 *outputBlocks = NULL;
298 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
299 if ( !*outputBlocks )
301 for ( unsigned long i = 0; i < count; i++ ) {
302 (*outputBlocks)[i] = blocks[i];
303 if (blocks[i].fDataType==kAliHLTAnyDataType) {
304 (*outputBlocks)[i].fDataType=GetOutputDataType();
305 /* data type was set to the output data type by the PubSub AliRoot
306 Wrapper component, if data type of the block was ********:****.
307 Now handled by the component base class in order to have same
308 behavior when running embedded in AliRoot
309 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
310 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
319 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
321 // see header file for function documentation
322 if (fEnvironment.fGetEventDoneDataFunc)
323 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
327 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList)
329 // see header file for function documentation
332 vector<AliHLTComponentDataType> ctlist;
333 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(ctlist);
334 vector<AliHLTComponentDataType>::iterator type=ctlist.begin();
335 //AliHLTComponentDataType ouptdt=GetOutputDataType();
336 //PrintDataTypeContent(ouptdt, "publisher \'%s\'");
337 while (type!=ctlist.end() && iResult==0) {
338 //PrintDataTypeContent((*type), "consumer \'%s\'");
339 if ((*type)==GetOutputDataType() ||
340 (*type)==kAliHLTAnyDataType) {
341 if (tgtList) tgtList->push_back(*type);
343 // this loop has to be changed in case of multiple output types
354 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
356 // see header file for function documentation
357 const char* fmt="publisher \'%s\'";
358 if (format) fmt=format;
359 HLTMessage(fmt, (DataType2Text(dt)).c_str());
360 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
375 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData ) const
377 // see header file for function documentation
378 blockData.fStructSize = sizeof(blockData);
379 FillShmData( blockData.fShmKey );
380 blockData.fOffset = ~(AliHLTUInt32_t)0;
381 blockData.fPtr = NULL;
383 FillDataType( blockData.fDataType );
384 blockData.fSpecification = kAliHLTVoidDataSpec;
387 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData ) const
389 // see header file for function documentation
390 shmData.fStructSize = sizeof(shmData);
391 shmData.fShmType = gkAliHLTComponentInvalidShmType;
392 shmData.fShmID = gkAliHLTComponentInvalidShmID;
395 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType ) const
397 // see header file for function documentation
398 dataType=kAliHLTAnyDataType;
401 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
403 // see header file for function documentation
404 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
405 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
408 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
410 // see header file for function documentation
411 tgtdt.fStructSize = sizeof(AliHLTComponentDataType);
412 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
413 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
415 if ((int)strlen(id)>kAliHLTComponentDataTypefIDsize) {
416 HLTWarning("data type id %s is too long, truncated to %d", id, kAliHLTComponentDataTypefIDsize);
418 strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
420 if ((int)strlen(origin)>kAliHLTComponentDataTypefOriginSize) {
421 HLTWarning("data type origin %s is too long, truncated to %d", origin, kAliHLTComponentDataTypefOriginSize);
423 strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
426 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
428 // see header file for function documentation
429 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
430 evtData.fStructSize=sizeof(AliHLTComponentEventData);
433 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
435 // see header file for function documentation
437 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
438 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
439 if (dt.fID[i]!=0) msg+=dt.fID[i];
443 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
444 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
448 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
451 int AliHLTComponent::GetEventCount() const
453 // see header file for function documentation
457 int AliHLTComponent::IncrementEventCounter()
459 // see header file for function documentation
460 if (fEventCount>=0) fEventCount++;
464 int AliHLTComponent::GetNumberOfInputBlocks() const
466 // see header file for function documentation
467 if (fpInputBlocks!=NULL) {
468 return fCurrentEventData.fBlockCnt;
473 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
474 const char* classname,
477 // see header file for function documentation
478 ALIHLTCOMPONENT_BASE_STOPWATCH();
480 if (classname) fClassName=classname;
481 else fClassName.clear();
482 int idx=FindInputBlock(fSearchDataType, 0, 1);
485 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
486 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
487 fCurrentInputBlock=idx;
494 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
495 const char* dtOrigin,
496 const char* classname,
499 // see header file for function documentation
500 ALIHLTCOMPONENT_BASE_STOPWATCH();
501 AliHLTComponentDataType dt;
502 SetDataType(dt, dtID, dtOrigin);
503 return GetFirstInputObject(dt, classname, bForce);
506 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
508 // see header file for function documentation
509 ALIHLTCOMPONENT_BASE_STOPWATCH();
510 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
511 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
514 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
515 fCurrentInputBlock=idx;
521 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
523 // see header file for function documentation
525 if (fpInputBlocks!=NULL) {
526 int idx=startIdx<0?0:startIdx;
527 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
529 if (fpInputBlocks[idx].fPtr==NULL) continue;
530 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
531 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
533 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
541 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
543 // see header file for function documentation
545 if (fpInputBlocks!=NULL) {
546 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
547 if (fpInputBlocks[idx].fPtr) {
548 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
549 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
550 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
551 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
552 TClass* objclass=msg.GetClass();
553 pObj=msg.ReadObject(objclass);
554 if (pObj && objclass) {
555 HLTDebug("object %p type %s created", pObj, objclass->GetName());
559 } else if (bForce!=0) {
560 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
563 HLTFatal("block descriptor empty");
566 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
569 HLTError("no input blocks available");
575 TObject* AliHLTComponent::GetInputObject(int idx, const char* classname, int bForce)
577 // see header file for function documentation
578 if (fpInputObjects==NULL) {
579 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
582 if (fpInputObjects) {
583 pObj=fpInputObjects->At(idx);
585 pObj=CreateInputObject(idx, bForce);
587 fpInputObjects->AddAt(pObj, idx);
591 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
596 int AliHLTComponent::CleanupInputObjects()
598 // see header file for function documentation
599 if (!fpInputObjects) return 0;
600 TObjArray* array=fpInputObjects;
602 for (int i=0; i<array->GetEntries(); i++) {
603 TObject* pObj=array->At(i);
604 // grrr, garbage collection strikes back: When read via AliHLTMessage
605 // (CreateInputObject), and written to a TFile afterwards, the
606 // TFile::Close calls ROOOT's garbage collection. No clue why the
607 // object ended up in the key list and needs to be deleted
608 if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
614 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
616 // see header file for function documentation
617 ALIHLTCOMPONENT_BASE_STOPWATCH();
618 AliHLTComponentDataType dt=kAliHLTVoidDataType;
619 int idx=fCurrentInputBlock;
621 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
623 HLTError("unknown object %p", pObject);
627 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
628 dt=fpInputBlocks[idx].fDataType;
630 HLTFatal("severe internal error, index out of range");
636 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
638 // see header file for function documentation
639 ALIHLTCOMPONENT_BASE_STOPWATCH();
640 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
641 int idx=fCurrentInputBlock;
643 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
645 HLTError("unknown object %p", pObject);
649 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
650 iSpec=fpInputBlocks[idx].fSpecification;
652 HLTFatal("severe internal error, index out of range");
658 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
660 // see header file for function documentation
661 ALIHLTCOMPONENT_BASE_STOPWATCH();
664 int idx=FindInputBlock(fSearchDataType, 0);
665 const AliHLTComponentBlockData* pBlock=NULL;
667 // check for fpInputBlocks pointer done in FindInputBlock
668 pBlock=&fpInputBlocks[idx];
669 fCurrentInputBlock=idx;
674 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
675 const char* dtOrigin)
677 // see header file for function documentation
678 ALIHLTCOMPONENT_BASE_STOPWATCH();
679 AliHLTComponentDataType dt;
680 SetDataType(dt, dtID, dtOrigin);
681 return GetFirstInputBlock(dt);
684 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
686 // see header file for function documentation
687 ALIHLTCOMPONENT_BASE_STOPWATCH();
688 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
689 const AliHLTComponentBlockData* pBlock=NULL;
691 // check for fpInputBlocks pointer done in FindInputBlock
692 pBlock=&fpInputBlocks[idx];
693 fCurrentInputBlock=idx;
698 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
700 // see header file for function documentation
702 if (fpInputBlocks!=NULL) {
704 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
705 iResult=(int)(pBlock-fpInputBlocks);
714 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
716 // see header file for function documentation
717 ALIHLTCOMPONENT_BASE_STOPWATCH();
718 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
719 int idx=fCurrentInputBlock;
721 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
723 HLTError("unknown Block %p", pBlock);
727 // check for fpInputBlocks pointer done in FindInputBlock
728 iSpec=fpInputBlocks[idx].fSpecification;
733 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
734 void* pHeader, int headerSize)
736 // see header file for function documentation
737 ALIHLTCOMPONENT_BASE_STOPWATCH();
740 AliHLTMessage msg(kMESS_OBJECT);
741 msg.WriteObject(pObject);
742 Int_t iMsgLength=msg.Length();
744 msg.SetLength(); // sets the length to the first (reserved) word
745 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
747 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
750 HLTError("object serialization failed for object %p", pObject);
759 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
760 void* pHeader, int headerSize)
762 // see header file for function documentation
763 ALIHLTCOMPONENT_BASE_STOPWATCH();
764 AliHLTComponentDataType dt;
765 SetDataType(dt, dtID, dtOrigin);
766 return PushBack(pObject, dt, spec, pHeader, headerSize);
769 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
770 void* pHeader, int headerSize)
772 // see header file for function documentation
773 ALIHLTCOMPONENT_BASE_STOPWATCH();
774 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
777 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
778 void* pHeader, int headerSize)
780 // see header file for function documentation
781 ALIHLTCOMPONENT_BASE_STOPWATCH();
782 AliHLTComponentDataType dt;
783 SetDataType(dt, dtID, dtOrigin);
784 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
787 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
788 void* pHeader, int iHeaderSize)
790 // see header file for function documentation
792 int iBlkSize = iBufferSize + iHeaderSize;
794 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
795 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
796 AliHLTComponentBlockData bd;
798 bd.fOffset = fOutputBufferFilled;
802 bd.fSpecification = spec;
803 if (pHeader!=NULL && pHeader!=pTgt) {
804 memcpy(pTgt, pHeader, iHeaderSize);
807 pTgt += (AliHLTUInt8_t) iHeaderSize;
809 if (pBuffer!=NULL && pBuffer!=pTgt) {
810 memcpy(pTgt, pBuffer, iBufferSize);
812 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
813 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
815 fOutputBufferFilled+=bd.fSize;
816 fOutputBlocks.push_back( bd );
817 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
819 if (fpOutputBuffer) {
820 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
822 HLTError("output buffer not available");
832 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
834 // see header file for function documentation
835 if (!pObject) return -EINVAL;
836 AliHLTMessage msg(kMESS_OBJECT);
837 msg.WriteObject(pObject);
841 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
842 const char* dtOrigin,
845 // see header file for function documentation
846 ALIHLTCOMPONENT_BASE_STOPWATCH();
847 AliHLTComponentDataType dt;
848 SetDataType(dt, dtID, dtOrigin);
849 return CreateMemoryFile(capacity, dt, spec);
852 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
853 const AliHLTComponentDataType& dt,
856 // see header file for function documentation
857 ALIHLTCOMPONENT_BASE_STOPWATCH();
858 AliHLTMemoryFile* pFile=NULL;
859 if (capacity>=0 && capacity<=fOutputBufferSize-fOutputBufferFilled){
860 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
861 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
863 int nofBlocks=fOutputBlocks.size();
864 if (nofBlocks+1>fMemFiles.size()) {
865 fMemFiles.resize(nofBlocks+1, NULL);
867 if (nofBlocks<fMemFiles.size()) {
868 fMemFiles[nofBlocks]=pFile;
869 AliHLTComponentBlockData bd;
871 bd.fOffset = fOutputBufferFilled;
875 bd.fSpecification = spec;
876 fOutputBufferFilled+=bd.fSize;
877 fOutputBlocks.push_back( bd );
879 HLTError("can not allocate/grow object array");
886 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
891 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
892 const char* dtOrigin,
896 // see header file for function documentation
897 ALIHLTCOMPONENT_BASE_STOPWATCH();
898 AliHLTComponentDataType dt;
899 SetDataType(dt, dtID, dtOrigin);
900 int size=fOutputBufferSize-fOutputBufferFilled;
901 if (capacity<0 || capacity>1.0) {
902 HLTError("invalid parameter: capacity %f", capacity);
905 size=(int)(size*capacity);
906 return CreateMemoryFile(size, dt, spec);
909 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
913 // see header file for function documentation
914 ALIHLTCOMPONENT_BASE_STOPWATCH();
915 int size=fOutputBufferSize-fOutputBufferFilled;
916 if (capacity<0 || capacity>1.0) {
917 HLTError("invalid parameter: capacity %f", capacity);
920 size=(int)(size*capacity);
921 return CreateMemoryFile(size, dt, spec);
924 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
925 const char* key, int option)
928 if (pFile && pObject) {
930 iResult=pObject->Write(key, option);
934 iResult=-pFile->GetErrno();
935 if (iResult==-ENOSPC) {
936 HLTError("error writing memory file, buffer too small");
945 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
949 vector<AliHLTMemoryFile*>::iterator element=fMemFiles.begin();
951 while (element!=fMemFiles.end() && iResult>=0) {
952 if (*element && *element==pFile) {
953 iResult=pFile->Close();
955 // sync memory files and descriptors
957 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
965 HLTError("can not find memory file %p", pFile);
973 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
975 // see header file for function documentation
977 //#warning function not yet implemented
978 HLTWarning("function not yet implemented");
982 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
983 const AliHLTComponentBlockData* blocks,
984 AliHLTComponentTriggerData& trigData,
985 AliHLTUInt8_t* outputPtr,
986 AliHLTUInt32_t& size,
987 AliHLTUInt32_t& outputBlockCnt,
988 AliHLTComponentBlockData*& outputBlocks,
989 AliHLTComponentEventDoneData*& edd )
991 // see header file for function documentation
992 ALIHLTCOMPONENT_BASE_STOPWATCH();
994 fCurrentEvent=evtData.fEventID;
995 fCurrentEventData=evtData;
996 fpInputBlocks=blocks;
997 fCurrentInputBlock=-1;
998 fSearchDataType=kAliHLTAnyDataType;
999 fpOutputBuffer=outputPtr;
1000 fOutputBufferSize=size;
1001 fOutputBufferFilled=0;
1002 fOutputBlocks.clear();
1004 // find special events
1005 if (fpInputBlocks) {
1006 for (int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1007 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1009 if (fpRunDesc==NULL) {
1010 fpRunDesc=new AliHLTRunDesc;
1012 if ((iResult=CopyStruct(fpRunDesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1013 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1019 HLTWarning("already received SOR event run no %d, ignoring SOR", fpRunDesc->fRunNo);
1021 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1022 if (fpRunDesc!=NULL) {
1024 AliHLTRunDesc rundesc;
1025 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), i, "AliHLTRunDesc", "SOR"))>0) {
1026 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1027 HLTWarning("run no missmatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1029 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1032 AliHLTRunDesc* pRunDesc=fpRunDesc;
1037 HLTWarning("did not receive SOR, ignoring EOR");
1040 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1046 vector<AliHLTComponentBlockData> blockData;
1047 { // dont delete, sets the scope for the stopwatch guard
1048 ALIHLTCOMPONENT_DA_STOPWATCH();
1049 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1050 } // end of the scope of the stopwatch guard
1052 if (fOutputBlocks.size()>0) {
1053 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1055 // sync memory files and descriptors
1056 vector<AliHLTMemoryFile*>::iterator element=fMemFiles.begin();
1058 while (element!=fMemFiles.end() && iResult>=0) {
1060 if ((*element)->IsClosed()==0) {
1061 HLTWarning("memory file has not been closed, force flush");
1062 iResult=CloseMemoryFile(*element);
1069 // create the descriptor list
1070 if (blockData.size()>0) {
1071 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1074 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1075 size=fOutputBufferFilled;
1079 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1082 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1089 CleanupInputObjects();
1090 IncrementEventCounter();
1094 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1099 // standard constructor (not for use)
1102 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1104 fpStopwatch(pStopwatch),
1109 // check for already existing guard
1110 if (fgpCurrent) fpPrec=fgpCurrent;
1113 // stop the preceeding guard if it controls a different stopwatch
1115 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1117 // start the stopwatch if the current guard controls a different one
1118 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1121 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
1127 // copy constructor not for use
1131 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1134 // assignment operator not for use
1141 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1143 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1147 // resume the preceeding guard if it controls a different stopwatch
1149 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1151 // stop the stopwatch if the current guard controls a different one
1152 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1154 // resume to the preceeding guard
1158 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1160 // see header file for function documentation
1161 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1162 return fpStopwatch!=pSucc?1:0;
1165 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1167 // see header file for function documentation
1168 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1169 return fpStopwatch!=pSucc?1:0;
1172 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
1174 // see header file for function documentation
1176 if (pSW!=NULL && type<kSWTypeCount) {
1177 if (fpStopwatches) {
1178 TObject* pObj=fpStopwatches->At((int)type);
1179 if (pSW==NULL // explicit reset
1180 || pObj==NULL) { // explicit set
1181 fpStopwatches->AddAt(pSW, (int)type);
1182 } else if (pObj!=pSW) {
1183 HLTWarning("stopwatch %d already set, reset first", (int)type);
1193 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1195 // see header file for function documentation
1196 if (pStopwatches==NULL) return -EINVAL;
1199 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1200 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1204 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1206 if (fpRunDesc==NULL) return 0;
1207 return fpRunDesc->fRunNo;
1210 AliHLTUInt32_t AliHLTComponent::GetRunType() const
1212 if (fpRunDesc==NULL) return 0;
1213 return fpRunDesc->fRunType;
1216 int AliHLTComponent::CopyStruct(void* pStruct, int iStructSize, int iBlockNo,
1217 const char* structname, const char* eventname)
1220 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1221 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1222 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1223 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
1224 AliHLTUInt8_t* pSrc=((AliHLTUInt8_t*)fpInputBlocks[iBlockNo].fPtr)+fpInputBlocks[iBlockNo].fOffset;
1225 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)pSrc);
1226 if (fpInputBlocks[iBlockNo].fSize!=copy) {
1227 HLTWarning("%s event: missmatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1228 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1230 if (copy!=iStructSize) {
1231 HLTWarning("%s event: missmatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1232 if (copy>iStructSize) {
1235 memset(pTgt, 0, iStructSize);
1238 memcpy(pTgt, pSrc, copy);
1242 HLTWarning("%s event: missing data block", eventname);
1248 HLTError("invalid struct");