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"
35 #include "TObjArray.h"
36 #include "TObjectTable.h"
38 #include "TStopwatch.h"
39 #include "AliHLTMemoryFile.h"
40 #include "AliHLTMisc.h"
44 /** ROOT macro for the implementation of ROOT specific class methods */
45 ClassImp(AliHLTComponent);
47 /** stopwatch macro using the stopwatch guard */
48 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
49 //#define ALIHLTCOMPONENT_STOPWATCH(type)
51 /** stopwatch macro for operations of the base class */
52 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
53 /** stopwatch macro for operations of the detector algorithm (DA) */
54 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
56 AliHLTComponent::AliHLTComponent()
64 fCurrentInputBlock(-1),
65 fSearchDataType(kAliHLTVoidDataType),
70 fOutputBufferFilled(0),
72 fpStopwatches(new TObjArray(kSWTypeCount)),
76 fCDBSetRunNoFunc(false),
80 fRequireSteeringBlocks(false),
81 fEventType(gkAliEventTypeUnknown),
84 // see header file for class documentation
86 // refer to README to build package
88 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
89 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
90 if (fgpComponentHandler)
91 fgpComponentHandler->ScheduleRegister(this);
92 //SetLocalLoggingLevel(kHLTLogDefault);
95 AliHLTComponent::~AliHLTComponent()
97 // see header file for function documentation
98 if (fpBenchmark) delete fpBenchmark;
101 CleanupInputObjects();
102 if (fpStopwatches!=NULL) delete fpStopwatches;
104 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
105 while (element!=fMemFiles.end()) {
107 if ((*element)->IsClosed()==0) {
108 HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
109 // close but do not flush as we dont know whether the buffer is still valid
110 (*element)->CloseMemoryFile(0);
123 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
125 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
127 // see header file for function documentation
129 if (fgpComponentHandler==NULL || bOverwrite!=0)
130 fgpComponentHandler=pCH;
136 int AliHLTComponent::UnsetGlobalComponentHandler()
138 // see header file for function documentation
139 return SetGlobalComponentHandler(NULL,1);
142 int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environParam, int argc, const char** argv )
144 // see header file for function documentation
145 HLTLogKeyword(GetComponentID());
148 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
149 memcpy(&fEnvironment, comenv, comenv->fStructSize<sizeof(AliHLTAnalysisEnvironment)?comenv->fStructSize:sizeof(AliHLTAnalysisEnvironment));
150 fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
151 fEnvironment.fParam=environParam;
154 const char** pArguments=NULL;
159 pArguments=new const char*[argc];
161 for (int i=0; i<argc && iResult>=0; i++) {
162 if (fComponentArgs.size()>0) fComponentArgs+=" ";
163 fComponentArgs+=argv[i];
165 if (argument.IsNull()) continue;
168 if (argument.CompareTo("benchmark")==0) {
171 } else if (argument.CompareTo("loglevel")==0) {
172 if ((bMissingParam=(++i>=argc))) break;
173 TString parameter(argv[i]);
174 parameter.Remove(TString::kLeading, ' '); // remove all blanks
175 if (parameter.BeginsWith("0x") &&
176 parameter.Replace(0,2,"",0).IsHex()) {
177 unsigned int loglevel=kHLTLogNone;
178 sscanf(parameter.Data(),"%x", &loglevel);
179 SetLocalLoggingLevel((AliHLTComponentLogSeverity)loglevel);
181 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
185 pArguments[iNofChildArgs++]=argv[i];
193 HLTError("missing parameter for argument %s", argument.Data());
197 iResult=DoInit(iNofChildArgs, pArguments);
202 // find out if the component wants to get the steering events
204 AliHLTComponentDataTypeList inputDt;
205 GetInputDataTypes(inputDt);
206 for (AliHLTComponentDataTypeList::iterator dt=inputDt.begin();
207 dt!=inputDt.end() && !fRequireSteeringBlocks;
209 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeSOR);
210 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeRunType);
211 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeEOR);
212 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeDDL);
213 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeComponentStatistics);
216 if (pArguments) delete [] pArguments;
218 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
219 // benchmarking stopwatch for the component statistics
220 fpBenchmark=new TStopwatch;
222 fpBenchmark->Start();
229 int AliHLTComponent::Deinit()
231 // see header file for function documentation
232 HLTLogKeyword(GetComponentID());
236 HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
237 AliHLTRunDesc* pRunDesc=fpRunDesc;
245 int AliHLTComponent::InitCDB(const char* cdbPath, AliHLTComponentHandler* pHandler)
247 // see header file for function documentation
250 // I have to think about separating the library handling from the
251 // component handler. Requiring the component hanlder here is not
252 // the cleanest solution.
253 // We presume the library already to be loaded
255 AliHLTMiscInitCDB_t pFunc=(AliHLTMiscInitCDB_t)pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_INIT_CDB);
258 if (cdbPath && cdbPath[0]!=0) {
260 // very temporary fix, have to check for other formats
261 if (!path.BeginsWith("local://")) {
266 if ((iResult=(*pFunc)(path.Data()))>=0) {
267 if (!(fCDBSetRunNoFunc=pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_SET_CDB_RUNNO))) {
268 Message(NULL, kHLTLogWarning, "AliHLTComponent::InitCDB", "init CDB",
269 "can not find function to set CDB run no");
273 Message(NULL, kHLTLogError, "AliHLTComponent::InitCDB", "init CDB",
274 "can not find initialization function");
283 int AliHLTComponent::SetCDBRunNo(int runNo)
285 // see header file for function documentation
286 if (!fCDBSetRunNoFunc) return 0;
287 return (*((AliHLTMiscSetCDBRunNo_t)fCDBSetRunNoFunc))(runNo);
290 int AliHLTComponent::SetRunDescription(const AliHLTRunDesc* desc, const char* /*runType*/)
292 // see header file for function documentation
293 if (!desc) return -EINVAL;
294 if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
295 HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
300 fpRunDesc=new AliHLTRunDesc;
301 if (!fpRunDesc) return -ENOMEM;
302 *fpRunDesc=kAliHLTVoidRunDesc;
305 if (fpRunDesc->fRunNo!=kAliHLTVoidRunNo && fpRunDesc->fRunNo!=desc->fRunNo) {
306 HLTWarning("Run description has already been set");
309 SetCDBRunNo(fpRunDesc->fRunNo);
310 // TODO: we have to decide about the runType
314 int AliHLTComponent::SetComponentDescription(const char* desc)
316 // see header file for function documentation
320 TString descriptor=desc;
321 TObjArray* pTokens=descriptor.Tokenize(" ");
323 for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
324 TString argument=((TObjString*)pTokens->At(i++))->GetString();
325 if (!argument || argument.IsNull()) continue;
328 if (argument.BeginsWith("chainid")) {
329 argument.ReplaceAll("chainid", "");
330 if (argument.BeginsWith("=")) {
331 fChainId=argument.Replace(0,1,"");
332 fChainIdCrc=CalculateChecksum((const AliHLTUInt8_t*)fChainId.c_str(), fChainId.length());
333 HLTDebug("setting component description: chain id %s crc 0x%8x", fChainId.c_str(), fChainIdCrc);
338 HLTWarning("unknown component description %s", argument.Data());
346 int AliHLTComponent::DoInit( int /*argc*/, const char** /*argv*/)
348 // default implementation, childs can overload
349 HLTLogKeyword("dummy");
353 int AliHLTComponent::DoDeinit()
355 // default implementation, childs can overload
356 HLTLogKeyword("dummy");
360 int AliHLTComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/)
362 // default implementation, childs can overload
363 HLTLogKeyword("dummy");
367 int AliHLTComponent::ReadPreprocessorValues(const char* /*modules*/)
369 // default implementation, childs can overload
370 HLTLogKeyword("dummy");
374 int AliHLTComponent::StartOfRun()
376 // default implementation, childs can overload
377 HLTLogKeyword("dummy");
381 int AliHLTComponent::EndOfRun()
383 // default implementation, childs can overload
384 HLTLogKeyword("dummy");
389 int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
391 // default implementation, childs can overload
392 HLTLogKeyword("dummy");
396 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
398 // see header file for function documentation
399 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
400 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
401 strcat( output, ":" );
402 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
405 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
407 // see header file for function documentation
413 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
414 sprintf(tmp, "'%d", type.fOrigin[i]);
418 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
419 sprintf(tmp, "%d'", type.fID[i]);
428 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
429 unsigned char* puc=(unsigned char*)type.fOrigin;
431 sprintf(tmp, "'\\%x", type.fOrigin[i]);
433 sprintf(tmp, "'%c", type.fOrigin[i]);
437 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
438 unsigned char* puc=(unsigned char*)type.fID;
440 sprintf(tmp, "\\%x'", type.fID[i]);
442 sprintf(tmp, "%c'", type.fID[i]);
448 if (type==kAliHLTVoidDataType) {
451 // some gymnastics in order to avoid a '0' which is part of either or both
452 // ID and origin terminating the whole string. Unfortunately, string doesn't
453 // stop appending at the '0' if the number of elements to append was
454 // explicitely specified
456 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
457 out.append(tmp.c_str());
460 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
461 out.append(tmp.c_str());
467 void* AliHLTComponent::AllocMemory( unsigned long size )
469 // see header file for function documentation
470 if (fEnvironment.fAllocMemoryFunc)
471 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
472 HLTFatal("no memory allocation handler registered");
476 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
477 AliHLTComponentBlockData** outputBlocks )
479 // see header file for function documentation
480 if ( blockCount==NULL || outputBlocks==NULL )
482 AliHLTUInt32_t count = blocks.size();
486 *outputBlocks = NULL;
489 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
490 if ( !*outputBlocks )
492 for ( unsigned long i = 0; i < count; i++ ) {
493 (*outputBlocks)[i] = blocks[i];
494 if (MatchExactly(blocks[i].fDataType, kAliHLTAnyDataType)) {
495 (*outputBlocks)[i].fDataType=GetOutputDataType();
496 /* data type was set to the output data type by the PubSub AliRoot
497 Wrapper component, if data type of the block was ********:****.
498 Now handled by the component base class in order to have same
499 behavior when running embedded in AliRoot
500 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
501 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
510 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
512 // see header file for function documentation
513 if (fEnvironment.fGetEventDoneDataFunc)
514 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
518 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
520 // see header file for function documentation
523 AliHLTComponentDataTypeList itypes;
524 AliHLTComponentDataTypeList otypes;
525 otypes.push_back(GetOutputDataType());
526 if (otypes[0]==kAliHLTMultipleDataType) {
529 if ((count=GetOutputDataTypes(otypes))>0) {
530 } else if (GetComponentType()!=kSink) {
531 HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
534 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(itypes);
535 AliHLTComponentDataTypeList::iterator otype=otypes.begin();
536 for (;otype!=otypes.end();otype++) {
537 //PrintDataTypeContent((*otype), "publisher \'%s\'");
538 if ((*otype)==(kAliHLTAnyDataType|kAliHLTDataOriginPrivate)) {
539 if (tgtList) tgtList->push_back(*otype);
544 AliHLTComponentDataTypeList::iterator itype=itypes.begin();
545 for (;itype!=itypes.end() && (*itype)!=(*otype); itype++);
546 //if (itype!=itypes.end()) PrintDataTypeContent(*itype, "consumer \'%s\'");
547 if (itype!=itypes.end()) {
548 if (tgtList) tgtList->push_back(*otype);
558 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format)
560 // see header file for function documentation
561 const char* fmt="\'%s\'";
562 if (format) fmt=format;
563 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, Form(fmt, (DataType2Text(dt)).c_str()));
564 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL,
565 Form("%x %x %x %x %x %x %x %x : %x %x %x %x",
580 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
582 // see header file for function documentation
583 blockData.fStructSize = sizeof(blockData);
584 FillShmData( blockData.fShmKey );
585 blockData.fOffset = ~(AliHLTUInt32_t)0;
586 blockData.fPtr = NULL;
588 FillDataType( blockData.fDataType );
589 blockData.fSpecification = kAliHLTVoidDataSpec;
592 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
594 // see header file for function documentation
595 shmData.fStructSize = sizeof(shmData);
596 shmData.fShmType = gkAliHLTComponentInvalidShmType;
597 shmData.fShmID = gkAliHLTComponentInvalidShmID;
600 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
602 // see header file for function documentation
603 dataType=kAliHLTAnyDataType;
606 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
608 // see header file for function documentation
609 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
610 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
613 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
615 // see header file for function documentation
616 tgtdt.fStructSize=sizeof(AliHLTComponentDataType);
618 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
619 strncpy(&tgtdt.fID[0], id, strlen(id)<(size_t)kAliHLTComponentDataTypefIDsize?strlen(id):kAliHLTComponentDataTypefIDsize);
622 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
623 strncpy(&tgtdt.fOrigin[0], origin, strlen(origin)<(size_t)kAliHLTComponentDataTypefOriginSize?strlen(origin):kAliHLTComponentDataTypefOriginSize);
627 void AliHLTComponent::SetDataType(AliHLTComponentDataType& dt, AliHLTUInt64_t id, AliHLTUInt32_t origin)
629 // see header file for function documentation
630 dt.fStructSize=sizeof(AliHLTComponentDataType);
631 assert(kAliHLTComponentDataTypefIDsize==sizeof(id));
632 assert(kAliHLTComponentDataTypefOriginSize==sizeof(origin));
633 memcpy(&dt.fID, &id, kAliHLTComponentDataTypefIDsize);
634 memcpy(&dt.fOrigin, &origin, kAliHLTComponentDataTypefOriginSize);
637 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
639 // see header file for function documentation
640 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
641 evtData.fStructSize=sizeof(AliHLTComponentEventData);
642 evtData.fEventID=kAliHLTVoidEventID;
645 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
647 // see header file for function documentation
649 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
650 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
651 if (dt.fID[i]!=0) msg+=dt.fID[i];
655 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
656 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
660 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
663 int AliHLTComponent::GetEventCount() const
665 // see header file for function documentation
669 int AliHLTComponent::IncrementEventCounter()
671 // see header file for function documentation
672 if (fEventCount>=0) fEventCount++;
676 int AliHLTComponent::GetNumberOfInputBlocks() const
678 // see header file for function documentation
679 if (fpInputBlocks!=NULL) {
680 return fCurrentEventData.fBlockCnt;
685 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
686 const char* classname,
689 // see header file for function documentation
690 ALIHLTCOMPONENT_BASE_STOPWATCH();
692 if (classname) fClassName=classname;
693 else fClassName.clear();
694 int idx=FindInputBlock(fSearchDataType, 0, 1);
697 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
698 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
699 fCurrentInputBlock=idx;
706 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
707 const char* dtOrigin,
708 const char* classname,
711 // see header file for function documentation
712 ALIHLTCOMPONENT_BASE_STOPWATCH();
713 AliHLTComponentDataType dt;
714 SetDataType(dt, dtID, dtOrigin);
715 return GetFirstInputObject(dt, classname, bForce);
718 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
720 // see header file for function documentation
721 ALIHLTCOMPONENT_BASE_STOPWATCH();
722 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
723 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
726 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
727 fCurrentInputBlock=idx;
733 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
735 // see header file for function documentation
737 if (fpInputBlocks!=NULL) {
738 int idx=startIdx<0?0:startIdx;
739 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
740 if (dt!=fpInputBlocks[idx].fDataType) continue;
743 if (fpInputBlocks[idx].fPtr==NULL) continue;
744 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
745 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
753 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
755 // see header file for function documentation
757 if (fpInputBlocks!=NULL) {
758 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
759 if (fpInputBlocks[idx].fPtr) {
760 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
761 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
762 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
763 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
764 TClass* objclass=msg.GetClass();
765 pObj=msg.ReadObject(objclass);
766 if (pObj && objclass) {
767 HLTDebug("object %p type %s created", pObj, objclass->GetName());
771 } else if (bForce!=0) {
772 HLTError("size mismatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
775 HLTFatal("block descriptor empty");
778 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
781 HLTError("no input blocks available");
787 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
789 // see header file for function documentation
790 if (fpInputObjects==NULL) {
791 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
794 if (fpInputObjects) {
795 pObj=fpInputObjects->At(idx);
797 pObj=CreateInputObject(idx, bForce);
799 fpInputObjects->AddAt(pObj, idx);
803 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
808 int AliHLTComponent::CleanupInputObjects()
810 // see header file for function documentation
811 if (!fpInputObjects) return 0;
812 TObjArray* array=fpInputObjects;
814 for (int i=0; i<array->GetEntries(); i++) {
815 TObject* pObj=array->At(i);
816 // grrr, garbage collection strikes back: When read via AliHLTMessage
817 // (CreateInputObject), and written to a TFile afterwards, the
818 // TFile::Close calls ROOOT's garbage collection. No clue why the
819 // object ended up in the key list and needs to be deleted
820 if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
826 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
828 // see header file for function documentation
829 ALIHLTCOMPONENT_BASE_STOPWATCH();
830 AliHLTComponentDataType dt=kAliHLTVoidDataType;
831 int idx=fCurrentInputBlock;
833 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
835 HLTError("unknown object %p", pObject);
839 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
840 dt=fpInputBlocks[idx].fDataType;
842 HLTFatal("severe internal error, index out of range");
848 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
850 // see header file for function documentation
851 ALIHLTCOMPONENT_BASE_STOPWATCH();
852 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
853 int idx=fCurrentInputBlock;
855 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
857 HLTError("unknown object %p", pObject);
861 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
862 iSpec=fpInputBlocks[idx].fSpecification;
864 HLTFatal("severe internal error, index out of range");
870 int AliHLTComponent::Forward(const TObject* pObject)
872 // see header file for function documentation
874 int idx=fCurrentInputBlock;
876 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
878 HLTError("unknown object %p", pObject);
883 fOutputBlocks.push_back(fpInputBlocks[idx]);
888 int AliHLTComponent::Forward(const AliHLTComponentBlockData* pBlock)
890 // see header file for function documentation
892 int idx=fCurrentInputBlock;
894 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
896 HLTError("unknown Block %p", pBlock);
901 // check for fpInputBlocks pointer done in FindInputBlock
902 fOutputBlocks.push_back(fpInputBlocks[idx]);
907 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
909 // see header file for function documentation
910 ALIHLTCOMPONENT_BASE_STOPWATCH();
913 int idx=FindInputBlock(fSearchDataType, 0);
914 const AliHLTComponentBlockData* pBlock=NULL;
916 // check for fpInputBlocks pointer done in FindInputBlock
917 pBlock=&fpInputBlocks[idx];
918 fCurrentInputBlock=idx;
923 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
924 const char* dtOrigin)
926 // see header file for function documentation
927 ALIHLTCOMPONENT_BASE_STOPWATCH();
928 AliHLTComponentDataType dt;
929 SetDataType(dt, dtID, dtOrigin);
930 return GetFirstInputBlock(dt);
933 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index) const
935 // see header file for function documentation
936 ALIHLTCOMPONENT_BASE_STOPWATCH();
937 assert( 0 <= index and index < (int)fCurrentEventData.fBlockCnt );
938 return &fpInputBlocks[index];
941 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
943 // see header file for function documentation
944 ALIHLTCOMPONENT_BASE_STOPWATCH();
945 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
946 const AliHLTComponentBlockData* pBlock=NULL;
948 // check for fpInputBlocks pointer done in FindInputBlock
949 pBlock=&fpInputBlocks[idx];
950 fCurrentInputBlock=idx;
955 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
957 // see header file for function documentation
959 if (fpInputBlocks!=NULL) {
961 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
962 iResult=(int)(pBlock-fpInputBlocks);
971 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
973 // see header file for function documentation
974 ALIHLTCOMPONENT_BASE_STOPWATCH();
975 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
976 int idx=fCurrentInputBlock;
978 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
980 HLTError("unknown Block %p", pBlock);
984 // check for fpInputBlocks pointer done in FindInputBlock
985 iSpec=fpInputBlocks[idx].fSpecification;
990 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
991 void* pHeader, int headerSize)
993 // see header file for function documentation
994 ALIHLTCOMPONENT_BASE_STOPWATCH();
997 AliHLTMessage msg(kMESS_OBJECT);
998 msg.WriteObject(pObject);
999 Int_t iMsgLength=msg.Length();
1001 msg.SetLength(); // sets the length to the first (reserved) word
1002 assert(msg.Buffer()!=NULL);
1003 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
1005 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
1008 HLTError("object serialization failed for object %p", pObject);
1017 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1018 void* pHeader, int headerSize)
1020 // see header file for function documentation
1021 ALIHLTCOMPONENT_BASE_STOPWATCH();
1022 AliHLTComponentDataType dt;
1023 SetDataType(dt, dtID, dtOrigin);
1024 return PushBack(pObject, dt, spec, pHeader, headerSize);
1027 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1028 const void* pHeader, int headerSize)
1030 // see header file for function documentation
1031 ALIHLTCOMPONENT_BASE_STOPWATCH();
1032 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
1035 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1036 const void* pHeader, int headerSize)
1038 // see header file for function documentation
1039 ALIHLTCOMPONENT_BASE_STOPWATCH();
1040 AliHLTComponentDataType dt;
1041 SetDataType(dt, dtID, dtOrigin);
1042 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
1045 int AliHLTComponent::InsertOutputBlock(const void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1046 const void* pHeader, int iHeaderSize)
1048 // see header file for function documentation
1050 int iBlkSize = iBufferSize + iHeaderSize;
1052 if ((pBuffer!=NULL && iBufferSize>0) || (pHeader!=NULL && iHeaderSize>0)) {
1053 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
1054 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1056 // copy header if provided but skip if the header is the target location
1057 // in that case it has already been copied
1058 if (pHeader!=NULL && pHeader!=pTgt) {
1059 memcpy(pTgt, pHeader, iHeaderSize);
1062 pTgt += (AliHLTUInt8_t) iHeaderSize;
1064 // copy buffer if provided but skip if buffer is the target location
1065 // in that case it has already been copied
1066 if (pBuffer!=NULL && pBuffer!=pTgt) {
1067 memcpy(pTgt, pBuffer, iBufferSize);
1069 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
1070 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
1072 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
1074 if (fpOutputBuffer) {
1075 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
1077 HLTError("output buffer not available");
1083 AliHLTComponentBlockData bd;
1084 FillBlockData( bd );
1085 bd.fOffset = fOutputBufferFilled;
1086 bd.fSize = iBlkSize;
1088 bd.fSpecification = spec;
1089 fOutputBlocks.push_back( bd );
1090 fOutputBufferFilled+=bd.fSize;
1096 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
1098 // see header file for function documentation
1099 if (!pObject) return -EINVAL;
1100 AliHLTMessage msg(kMESS_OBJECT);
1101 msg.WriteObject(pObject);
1102 return msg.Length();
1105 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
1106 const char* dtOrigin,
1107 AliHLTUInt32_t spec)
1109 // see header file for function documentation
1110 ALIHLTCOMPONENT_BASE_STOPWATCH();
1111 AliHLTComponentDataType dt;
1112 SetDataType(dt, dtID, dtOrigin);
1113 return CreateMemoryFile(capacity, dt, spec);
1116 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
1117 const AliHLTComponentDataType& dt,
1118 AliHLTUInt32_t spec)
1120 // see header file for function documentation
1121 ALIHLTCOMPONENT_BASE_STOPWATCH();
1122 AliHLTMemoryFile* pFile=NULL;
1123 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
1124 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1125 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
1127 unsigned int nofBlocks=fOutputBlocks.size();
1128 if (nofBlocks+1>fMemFiles.size()) {
1129 fMemFiles.resize(nofBlocks+1, NULL);
1131 if (nofBlocks<fMemFiles.size()) {
1132 fMemFiles[nofBlocks]=pFile;
1133 AliHLTComponentBlockData bd;
1134 FillBlockData( bd );
1135 bd.fOffset = fOutputBufferFilled;
1136 bd.fSize = capacity;
1138 bd.fSpecification = spec;
1139 fOutputBufferFilled+=bd.fSize;
1140 fOutputBlocks.push_back( bd );
1142 HLTError("can not allocate/grow object array");
1143 pFile->CloseMemoryFile(0);
1149 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
1154 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
1155 const char* dtOrigin,
1156 AliHLTUInt32_t spec,
1159 // see header file for function documentation
1160 ALIHLTCOMPONENT_BASE_STOPWATCH();
1161 AliHLTComponentDataType dt;
1162 SetDataType(dt, dtID, dtOrigin);
1163 int size=fOutputBufferSize-fOutputBufferFilled;
1164 if (capacity<0 || capacity>1.0) {
1165 HLTError("invalid parameter: capacity %f", capacity);
1168 size=(int)(size*capacity);
1169 return CreateMemoryFile(size, dt, spec);
1172 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
1173 AliHLTUInt32_t spec,
1176 // see header file for function documentation
1177 ALIHLTCOMPONENT_BASE_STOPWATCH();
1178 int size=fOutputBufferSize-fOutputBufferFilled;
1179 if (capacity<0 || capacity>1.0) {
1180 HLTError("invalid parameter: capacity %f", capacity);
1183 size=(int)(size*capacity);
1184 return CreateMemoryFile(size, dt, spec);
1187 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
1188 const char* key, int option)
1190 // see header file for function documentation
1192 if (pFile && pObject) {
1194 iResult=pObject->Write(key, option);
1198 iResult=-pFile->GetErrno();
1199 if (iResult==-ENOSPC) {
1200 HLTError("error writing memory file, buffer too small");
1209 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
1211 // see header file for function documentation
1214 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1216 while (element!=fMemFiles.end() && iResult>=0) {
1217 if (*element && *element==pFile) {
1218 iResult=pFile->CloseMemoryFile();
1220 // sync memory files and descriptors
1222 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
1230 HLTError("can not find memory file %p", pFile);
1238 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData /*edd*/)
1240 // see header file for function documentation
1241 int iResult=-ENOSYS;
1242 //#warning function not yet implemented
1243 HLTWarning("function not yet implemented");
1247 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1248 const AliHLTComponentBlockData* blocks,
1249 AliHLTComponentTriggerData& trigData,
1250 AliHLTUInt8_t* outputPtr,
1251 AliHLTUInt32_t& size,
1252 AliHLTUInt32_t& outputBlockCnt,
1253 AliHLTComponentBlockData*& outputBlocks,
1254 AliHLTComponentEventDoneData*& edd )
1256 // see header file for function documentation
1257 HLTLogKeyword(GetComponentID());
1258 ALIHLTCOMPONENT_BASE_STOPWATCH();
1260 fCurrentEvent=evtData.fEventID;
1261 fCurrentEventData=evtData;
1262 fpInputBlocks=blocks;
1263 fCurrentInputBlock=-1;
1264 fSearchDataType=kAliHLTAnyDataType;
1265 fpOutputBuffer=outputPtr;
1266 fOutputBufferSize=size;
1267 fOutputBufferFilled=0;
1268 fOutputBlocks.clear();
1272 AliHLTComponentBlockDataList forwardedBlocks;
1274 // optional component statistics
1275 AliHLTComponentStatisticsList compStats;
1276 bool bAddComponentTableEntry=false;
1277 vector<AliHLTUInt32_t> parentComponentTables;
1278 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
1279 AliHLTComponentStatistics outputStat;
1280 memset(&outputStat, 0, sizeof(AliHLTComponentStatistics));
1281 outputStat.fId=fChainIdCrc;
1282 compStats.push_back(outputStat);
1284 fpBenchmark->Reset();
1285 fpBenchmark->Start();
1289 // data processing is skipped
1290 // - if there are only steering events in the block list.
1291 // For the sake of data source components data processing
1292 // is not skipped if there is no block list at all or if it
1293 // just contains the eventType block
1294 // - always skipped if the event is of type
1295 // - gkAliEventTypeConfiguration
1296 // - gkAliEventTypeReadPreprocessor
1297 const unsigned int skipModeDefault=0x1;
1298 const unsigned int skipModeForce=0x2;
1299 unsigned int bSkipDataProcessing=skipModeDefault;
1301 // find special events
1302 if (fpInputBlocks && evtData.fBlockCnt>0) {
1303 // first look for all special events and execute in the appropriate
1304 // sequence afterwords
1305 int indexComConfEvent=-1;
1306 int indexUpdtDCSEvent=-1;
1307 int indexSOREvent=-1;
1308 int indexEOREvent=-1;
1309 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1310 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1312 // the AliHLTCalibrationProcessor relies on the SOR and EOR events
1313 bSkipDataProcessing&=~skipModeDefault;
1314 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeRunType) {
1316 // handling is not clear yet
1317 if (fpInputBlocks[i].fPtr) {
1318 HLTDebug("got run type \"%s\"\n", fpInputBlocks[i].fPtr);
1320 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1322 // the calibration processor relies on the SOR and EOR events
1323 bSkipDataProcessing&=~skipModeDefault;
1324 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1326 // this event is most likely deprecated
1327 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComConf) {
1328 indexComConfEvent=i;
1329 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeUpdtDCS) {
1330 indexUpdtDCSEvent=i;
1331 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEvent) {
1332 fEventType=fpInputBlocks[i].fSpecification;
1334 // skip always in case of gkAliEventTypeConfiguration
1335 if (fpInputBlocks[i].fSpecification==gkAliEventTypeConfiguration) bSkipDataProcessing|=skipModeForce;
1337 // skip always in case of gkAliEventTypeReadPreprocessor
1338 if (fpInputBlocks[i].fSpecification==gkAliEventTypeReadPreprocessor) bSkipDataProcessing|=skipModeForce;
1340 // never skip if the event type block is the only block
1341 if (evtData.fBlockCnt==1) bSkipDataProcessing&=~skipModeDefault;
1343 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentStatistics) {
1344 if (compStats.size()>0) {
1345 AliHLTUInt8_t* pData=reinterpret_cast<AliHLTUInt8_t*>(fpInputBlocks[i].fPtr);
1346 for (AliHLTUInt32_t offset=0;
1347 offset+sizeof(AliHLTComponentStatistics)<=fpInputBlocks[i].fSize;
1348 offset+=sizeof(AliHLTComponentStatistics)) {
1349 AliHLTComponentStatistics* pStat=reinterpret_cast<AliHLTComponentStatistics*>(pData+offset);
1350 if (pStat && compStats[0].fLevel<=pStat->fLevel) {
1351 compStats[0].fLevel=pStat->fLevel+1;
1353 compStats.push_back(*pStat);
1356 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentTable) {
1357 forwardedBlocks.push_back(fpInputBlocks[i]);
1358 parentComponentTables.push_back(fpInputBlocks[i].fSpecification);
1360 // the processing function is called if there is at least one
1361 // non-steering data block. Steering blocks are not filtered out
1362 // for sake of performance
1363 bSkipDataProcessing&=~skipModeDefault;
1364 if (compStats.size()>0) {
1365 compStats[0].fInputBlockCount++;
1366 compStats[0].fTotalInputSize+=fpInputBlocks[i].fSize;
1371 if (indexSOREvent>=0) {
1373 bAddComponentTableEntry=true;
1374 if (fpRunDesc==NULL) {
1375 fpRunDesc=new AliHLTRunDesc;
1376 if (fpRunDesc) *fpRunDesc=kAliHLTVoidRunDesc;
1379 AliHLTRunDesc rundesc;
1380 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexSOREvent, "AliHLTRunDesc", "SOR"))>0) {
1381 if (fpRunDesc->fRunNo==kAliHLTVoidRunNo) {
1383 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1384 SetCDBRunNo(fpRunDesc->fRunNo);
1385 } else if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1386 HLTWarning("already set run properties run no %d, ignoring SOR with run no %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1393 if (indexEOREvent>=0) {
1394 bAddComponentTableEntry=true;
1395 if (fpRunDesc!=NULL) {
1397 AliHLTRunDesc rundesc;
1398 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexEOREvent, "AliHLTRunDesc", "SOR"))>0) {
1399 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1400 HLTWarning("run no mismatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1402 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1405 AliHLTRunDesc* pRunDesc=fpRunDesc;
1410 HLTWarning("did not receive SOR, ignoring EOR");
1413 if (indexComConfEvent>=0 || fEventType==gkAliEventTypeConfiguration) {
1415 if (indexComConfEvent>=0 && fpInputBlocks[indexComConfEvent].fPtr!=NULL && fpInputBlocks[indexComConfEvent].fSize>0) {
1416 cdbEntry.Append(reinterpret_cast<const char*>(fpInputBlocks[indexComConfEvent].fPtr), fpInputBlocks[indexComConfEvent].fSize);
1418 HLTDebug("received component configuration command: entry %s", cdbEntry.IsNull()?"none":cdbEntry.Data());
1419 int tmpResult=Reconfigure(cdbEntry[0]==0?NULL:cdbEntry.Data(), fChainId.c_str());
1421 HLTWarning("reconfiguration of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1424 if (indexUpdtDCSEvent>=0 || fEventType==gkAliEventTypeReadPreprocessor) {
1426 if (fpInputBlocks[indexUpdtDCSEvent].fPtr!=NULL && fpInputBlocks[indexUpdtDCSEvent].fSize>0) {
1427 modules.Append(reinterpret_cast<const char*>(fpInputBlocks[indexUpdtDCSEvent].fPtr), fpInputBlocks[indexUpdtDCSEvent].fSize);
1429 HLTDebug("received preprocessor update command: detectors %s", modules.IsNull()?"ALL":modules.Data());
1430 int tmpResult=ReadPreprocessorValues(modules[0]==0?"ALL":modules.Data());
1432 HLTWarning("preprocessor update of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1436 // processing function needs to be called if there are no input data
1437 // blocks in order to make data source components working.
1438 bSkipDataProcessing&=~skipModeDefault;
1441 // data processing is not skipped if the component explicitly asks
1442 // for the private blocks
1443 if (fRequireSteeringBlocks) bSkipDataProcessing=0;
1445 AliHLTComponentBlockDataList blockData;
1446 if (iResult>=0 && !bSkipDataProcessing)
1447 { // dont delete, sets the scope for the stopwatch guard
1448 // do not use ALIHLTCOMPONENT_DA_STOPWATCH(); macro
1449 // in order to avoid 'shadowed variable' warning
1450 AliHLTStopwatchGuard swguard2(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)kSWDA)):NULL);
1451 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1452 } // end of the scope of the stopwatch guard
1453 if (iResult>=0 && !bSkipDataProcessing) {
1454 if (fOutputBlocks.size()>0) {
1455 // High Level interface
1457 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1458 // sync memory files and descriptors
1459 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1461 while (element!=fMemFiles.end() && iResult>=0) {
1463 if ((*element)->IsClosed()==0) {
1464 HLTWarning("memory file has not been closed, force flush");
1465 iResult=CloseMemoryFile(*element);
1472 // create the descriptor list
1473 if (blockData.size()>0) {
1474 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1477 if (compStats.size()>0 && IsDataEvent()) {
1478 int offset=AddComponentStatistics(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, compStats);
1479 if (offset>0) fOutputBufferFilled+=offset;
1481 if (bAddComponentTableEntry) {
1482 int offset=AddComponentTableEntry(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, parentComponentTables);
1483 if (offset>0) size+=offset;
1485 if (forwardedBlocks.size()>0) {
1486 fOutputBlocks.insert(fOutputBlocks.end(), forwardedBlocks.begin(), forwardedBlocks.end());
1488 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1489 size=fOutputBufferFilled;
1493 // Low Level interface
1494 if (compStats.size()>0) {
1495 int offset=AddComponentStatistics(blockData, fpOutputBuffer, fOutputBufferSize, size, compStats);
1496 if (offset>0) size+=offset;
1498 if (bAddComponentTableEntry) {
1499 int offset=AddComponentTableEntry(blockData, fpOutputBuffer, fOutputBufferSize, size, parentComponentTables);
1500 if (offset>0) size+=offset;
1502 if (forwardedBlocks.size()>0) {
1503 blockData.insert(blockData.end(), forwardedBlocks.begin(), forwardedBlocks.end());
1505 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1508 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1511 if (iResult<0 || bSkipDataProcessing) {
1515 CleanupInputObjects();
1516 if (iResult>=0 && IsDataEvent()) {
1517 IncrementEventCounter();
1519 if (outputBlockCnt==0) {
1520 // no output blocks, set size to 0
1526 int AliHLTComponent::AddComponentStatistics(AliHLTComponentBlockDataList& blocks,
1527 AliHLTUInt8_t* buffer,
1528 AliHLTUInt32_t bufferSize,
1529 AliHLTUInt32_t offset,
1530 AliHLTComponentStatisticsList& stats) const
1532 // see header file for function documentation
1534 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
1535 if (stats.size()==0) return -ENOENT;
1536 stats[0].fTotalOutputSize=offset;
1537 stats[0].fOutputBlockCount=blocks.size();
1539 stats[0].fTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*1000000);
1540 stats[0].fCTime=(AliHLTUInt32_t)(fpBenchmark->CpuTime()*1000000);
1542 if (offset+stats.size()*sizeof(AliHLTComponentStatistics)<=bufferSize) {
1543 AliHLTComponentBlockData bd;
1544 FillBlockData( bd );
1545 bd.fOffset = offset;
1546 bd.fSize = stats.size()*sizeof(AliHLTComponentStatistics);
1547 bd.fDataType = kAliHLTDataTypeComponentStatistics;
1548 bd.fSpecification = kAliHLTVoidDataSpec;
1549 unsigned int master=0;
1550 for (unsigned int i=1; i<blocks.size(); i++) {
1551 if (blocks[i].fSize>blocks[master].fSize &&
1552 !MatchExactly(blocks[i].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate))
1555 if (blocks.size()>0 && !MatchExactly(blocks[master].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate)) {
1556 // take the data origin of the biggest block as specification
1557 // this is similar to the treatment in the HOMER interface. For traditional
1558 // reasons, the bytes are swapped there on a little endian architecture, so
1559 // we do it as well.
1560 memcpy(&bd.fSpecification, &blocks[master].fDataType.fOrigin, sizeof(bd.fSpecification));
1561 #ifdef R__BYTESWAP // set on little endian architectures
1562 bd.fSpecification=((bd.fSpecification & 0xFFULL) << 24) |
1563 ((bd.fSpecification & 0xFF00ULL) << 8) |
1564 ((bd.fSpecification & 0xFF0000ULL) >> 8) |
1565 ((bd.fSpecification & 0xFF000000ULL) >> 24);
1568 memcpy(buffer+offset, &(stats[0]), bd.fSize);
1569 blocks.push_back(bd);
1573 if (blocks.size() && buffer && bufferSize && offset && stats.size()) {
1574 // get rid of warning
1580 int AliHLTComponent::AddComponentTableEntry(AliHLTComponentBlockDataList& blocks,
1581 AliHLTUInt8_t* buffer,
1582 AliHLTUInt32_t bufferSize,
1583 AliHLTUInt32_t offset,
1584 const vector<AliHLTUInt32_t>& parents) const
1586 // see header file for function documentation
1588 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
1589 // the payload consists of the AliHLTComponentTableEntry struct,
1590 // followed by a an array of 32bit crc chain ids and the component
1591 // description string
1592 unsigned int payloadSize=sizeof(AliHLTComponentTableEntry);
1593 payloadSize+=parents.size()*sizeof(AliHLTUInt32_t);
1595 // the component description has the following format:
1596 // chain-id{component-id:arguments}
1597 const char* componentId=const_cast<AliHLTComponent*>(this)->GetComponentID();
1598 unsigned int descriptionSize=fChainId.size()+1;
1599 descriptionSize+=2; // the '{}' around the component id
1600 descriptionSize+=strlen(componentId);
1601 descriptionSize+=1; // the ':' between component id and arguments
1602 descriptionSize+=fComponentArgs.size();
1604 payloadSize+=descriptionSize;
1605 if (buffer && (offset+payloadSize<=bufferSize)) {
1606 AliHLTUInt8_t* pTgt=buffer+offset;
1607 memset(pTgt, 0, payloadSize);
1610 AliHLTComponentTableEntry* pEntry=reinterpret_cast<AliHLTComponentTableEntry*>(pTgt);
1611 pEntry->fStructSize=sizeof(AliHLTComponentTableEntry);
1612 pEntry->fNofParents=parents.size();
1613 pEntry->fSizeDescription=descriptionSize;
1614 pTgt=pEntry->fBuffer;
1616 // write array of parents
1617 if (parents.size()>0) {
1618 unsigned int copy=parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type);
1619 memcpy(pTgt, &parents[0], parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type));
1623 // write component description
1624 memcpy(pTgt, fChainId.c_str(), fChainId.size());
1625 pTgt+=fChainId.size();
1627 memcpy(pTgt, componentId, strlen(componentId));
1628 pTgt+=strlen(componentId);
1630 memcpy(pTgt, fComponentArgs.c_str(), fComponentArgs.size());
1631 pTgt+=fComponentArgs.size();
1635 AliHLTComponentBlockData bd;
1636 FillBlockData( bd );
1637 bd.fOffset = offset;
1638 bd.fSize = payloadSize;
1639 bd.fDataType = kAliHLTDataTypeComponentTable;
1640 bd.fSpecification = fChainIdCrc;
1641 blocks.push_back(bd);
1645 if (blocks.size() && buffer && bufferSize && offset && parents.size()) {
1646 // get rid of warning
1652 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1657 // standard constructor (not for use)
1660 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1662 fpStopwatch(pStopwatch),
1667 // check for already existing guard
1668 if (fgpCurrent) fpPrec=fgpCurrent;
1671 // stop the preceeding guard if it controls a different stopwatch
1673 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1675 // start the stopwatch if the current guard controls a different one
1676 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1679 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
1685 // copy constructor not for use
1689 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1692 // assignment operator not for use
1699 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1701 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1705 // resume the preceeding guard if it controls a different stopwatch
1707 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1709 // stop the stopwatch if the current guard controls a different one
1710 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1712 // resume to the preceeding guard
1716 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1718 // see header file for function documentation
1719 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1720 return fpStopwatch!=pSucc?1:0;
1723 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1725 // see header file for function documentation
1726 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1727 return fpStopwatch!=pSucc?1:0;
1730 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
1732 // see header file for function documentation
1734 if (pSW!=NULL && type<kSWTypeCount) {
1735 if (fpStopwatches) {
1736 TObject* pObj=fpStopwatches->At((int)type);
1737 if (pSW==NULL // explicit reset
1738 || pObj==NULL) { // explicit set
1739 fpStopwatches->AddAt(pSW, (int)type);
1740 } else if (pObj!=pSW) {
1741 HLTWarning("stopwatch %d already set, reset first", (int)type);
1751 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1753 // see header file for function documentation
1754 if (pStopwatches==NULL) return -EINVAL;
1757 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1758 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1762 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1764 // see header file for function documentation
1765 if (fpRunDesc==NULL) return kAliHLTVoidRunNo;
1766 return fpRunDesc->fRunNo;
1769 AliHLTUInt32_t AliHLTComponent::GetRunType() const
1771 // see header file for function documentation
1772 if (fpRunDesc==NULL) return kAliHLTVoidRunType;
1773 return fpRunDesc->fRunType;
1776 bool AliHLTComponent::IsDataEvent(AliHLTUInt32_t* pTgt)
1778 // see header file for function documentation
1779 if (pTgt) *pTgt=fEventType;
1780 return (fEventType==gkAliEventTypeData ||
1781 fEventType==gkAliEventTypeDataReplay ||
1782 fEventType==gkAliEventTypeCalibration);
1785 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
1786 const char* structname, const char* eventname)
1788 // see header file for function documentation
1790 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1791 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1792 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1793 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
1794 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
1795 if (fpInputBlocks[iBlockNo].fSize!=copy) {
1796 HLTWarning("%s event: mismatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1797 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1799 if (copy!=iStructSize) {
1800 HLTWarning("%s event: mismatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1801 if (copy>iStructSize) {
1804 memset(pTgt, 0, iStructSize);
1807 memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
1811 HLTWarning("%s event: missing data block", eventname);
1817 HLTError("invalid struct");
1823 void AliHLTComponent::SetDDLBit(AliHLTEventDDL &list, Int_t ddlId, Bool_t state ) const
1825 // see header file for function documentation
1827 // -- Detector offset
1828 Int_t ddlIdBase = TMath::FloorNint( (Double_t) ddlId / 256.0 );
1830 // -- Word Base = 1. word of detector ( TPC has 8 words, TOF 3 )
1833 if ( ddlIdBase <= 3 )
1834 wordBase = ddlIdBase;
1835 else if ( ddlIdBase > 3 && ddlIdBase < 5 )
1836 wordBase = ddlIdBase + 7;
1838 wordBase = ddlIdBase + 9;
1840 // -- Bit index in Word
1841 Int_t bitIdx = ddlId % 32;
1844 Int_t wordIdx = wordBase;
1846 // -- if TPC (3) or TOD (5) add word idx
1847 if ( ( ddlIdBase == 3 ) || ( ddlIdBase == 5 ) ) {
1848 wordIdx += TMath::FloorNint( (Double_t) ( ddlId - ( ddlIdBase * 256 ) ) / 32.0 );
1851 // -- Set -- 'OR' word with bit mask;
1853 list.fList[wordIdx] |= ( 0x00000001 << bitIdx );
1854 // -- Unset -- 'AND' word with bit mask;
1856 list.fList[wordIdx] &= ( 0xFFFFFFFF ^ ( 0x00000001 << bitIdx ) );
1859 Int_t AliHLTComponent::GetFirstUsedDDLWord(AliHLTEventDDL &list) const
1861 // see header file for function documentation
1865 for ( Int_t wordNdx = 0 ; wordNdx < gkAliHLTDDLListSize ; wordNdx++ ) {
1867 if ( list.fList[wordNdx] != 0 && iResult == -1 ) {
1868 // check for special cases TPC and TOF
1869 if ( wordNdx > 3 && wordNdx <= 10 ) {
1873 else if ( wordNdx > 12 && wordNdx <= 14 ) {
1880 else if ( list.fList[wordNdx] != 0 && iResult >= 0 ) {
1881 HLTError( "DDLIDs for minimum of TWO detectors ( %d, %d ) set, this function works only for ONE detector.", iResult, wordNdx );
1890 AliHLTUInt32_t AliHLTComponent::CalculateChecksum(const AliHLTUInt8_t* buffer, int size)
1892 // see header file for function documentation
1893 AliHLTUInt32_t remainder = 0;
1894 const AliHLTUInt8_t crcwidth=(8*sizeof(AliHLTUInt32_t));
1895 const AliHLTUInt32_t topbit=1 << (crcwidth-1);
1896 const AliHLTUInt32_t polynomial=0xD8; /* 11011 followed by 0's */
1899 // http://www.netrino.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
1902 * Perform modulo-2 division, a byte at a time.
1904 for (int byte = 0; byte < size; ++byte)
1907 * Bring the next byte into the remainder.
1909 remainder ^= (buffer[byte] << (crcwidth - 8));
1912 * Perform modulo-2 division, a bit at a time.
1914 for (uint8_t bit = 8; bit > 0; --bit)
1917 * Try to divide the current data bit.
1919 if (remainder & topbit)
1921 remainder = (remainder << 1) ^ polynomial;
1925 remainder = (remainder << 1);
1931 * The final remainder is the CRC result.
1936 int AliHLTComponent::ExtractComponentTableEntry(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size,
1937 string& retChainId, string& retCompId, string& retCompArgs,
1938 vector<AliHLTUInt32_t>& parents)
1940 // see header file for function documentation
1943 retCompArgs.clear();
1945 if (!pBuffer || size==0) return 0;
1947 const AliHLTComponentTableEntry* pEntry=reinterpret_cast<const AliHLTComponentTableEntry*>(pBuffer);
1948 if (size<8/* the initial size of the structure*/ ||
1949 pEntry==NULL || pEntry->fStructSize<8) return -ENOMSG;
1950 const AliHLTUInt32_t* pParents=reinterpret_cast<const AliHLTUInt32_t*>(pEntry->fBuffer);
1951 const AliHLTUInt8_t* pEnd=pBuffer+size;
1953 if (pParents+pEntry->fNofParents>=reinterpret_cast<const AliHLTUInt32_t*>(pEnd)) return -ENODEV;
1954 for (unsigned int i=0; i<pEntry->fNofParents; i++, pParents++) {
1955 parents.push_back(*pParents);
1958 const char* pDescription=reinterpret_cast<const char*>(pParents);
1959 if (pDescription+pEntry->fSizeDescription>=reinterpret_cast<const char*>(pEnd) ||
1960 *(pDescription+pEntry->fSizeDescription)!=0) {
1964 TString descriptor=reinterpret_cast<const char*>(pDescription);
1968 TObjArray* pTokens=descriptor.Tokenize("{");
1971 if (pTokens->GetEntries()>n) {
1972 retChainId=((TObjString*)pTokens->At(n++))->GetString();
1974 if (pTokens->GetEntries()>n) {
1975 compId=((TObjString*)pTokens->At(n++))->GetString();
1979 if (!compId.IsNull() && (pTokens=compId.Tokenize(":"))!=NULL) {
1981 if (pTokens->GetEntries()>n) {
1982 compId=((TObjString*)pTokens->At(n++))->GetString();
1984 if (pTokens->GetEntries()>n) {
1985 compArgs=((TObjString*)pTokens->At(n++))->GetString();
1989 compId.ReplaceAll("}", "");
1990 compArgs.ReplaceAll("}", "");
1993 retCompArgs=compArgs;
1995 if (retChainId.size()==0) return -ENODATA;