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),
86 // see header file for class documentation
88 // refer to README to build package
90 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
91 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
92 if (fgpComponentHandler)
93 fgpComponentHandler->ScheduleRegister(this);
94 //SetLocalLoggingLevel(kHLTLogDefault);
97 AliHLTComponent::~AliHLTComponent()
99 // see header file for function documentation
100 if (fpBenchmark) delete fpBenchmark;
103 CleanupInputObjects();
104 if (fpStopwatches!=NULL) delete fpStopwatches;
106 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
107 while (element!=fMemFiles.end()) {
109 if ((*element)->IsClosed()==0) {
110 HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
111 // close but do not flush as we dont know whether the buffer is still valid
112 (*element)->CloseMemoryFile(0);
124 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
127 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
129 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
131 // see header file for function documentation
133 if (fgpComponentHandler==NULL || bOverwrite!=0)
134 fgpComponentHandler=pCH;
140 int AliHLTComponent::UnsetGlobalComponentHandler()
142 // see header file for function documentation
143 return SetGlobalComponentHandler(NULL,1);
146 int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environParam, int argc, const char** argv )
148 // see header file for function documentation
149 HLTLogKeyword(GetComponentID());
152 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
153 memcpy(&fEnvironment, comenv, comenv->fStructSize<sizeof(AliHLTAnalysisEnvironment)?comenv->fStructSize:sizeof(AliHLTAnalysisEnvironment));
154 fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
155 fEnvironment.fParam=environParam;
158 const char** pArguments=NULL;
163 pArguments=new const char*[argc];
165 for (int i=0; i<argc && iResult>=0; i++) {
166 if (fComponentArgs.size()>0) fComponentArgs+=" ";
167 fComponentArgs+=argv[i];
169 if (argument.IsNull()) continue;
172 if (argument.CompareTo("benchmark")==0) {
175 } else if (argument.CompareTo("loglevel")==0) {
176 if ((bMissingParam=(++i>=argc))) break;
177 TString parameter(argv[i]);
178 parameter.Remove(TString::kLeading, ' '); // remove all blanks
179 if (parameter.BeginsWith("0x") &&
180 parameter.Replace(0,2,"",0).IsHex()) {
181 unsigned int loglevel=kHLTLogNone;
182 sscanf(parameter.Data(),"%x", &loglevel);
183 SetLocalLoggingLevel((AliHLTComponentLogSeverity)loglevel);
185 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
189 pArguments[iNofChildArgs++]=argv[i];
197 HLTError("missing parameter for argument %s", argument.Data());
201 iResult=DoInit(iNofChildArgs, pArguments);
206 // find out if the component wants to get the steering events
208 AliHLTComponentDataTypeList inputDt;
209 GetInputDataTypes(inputDt);
210 for (AliHLTComponentDataTypeList::iterator dt=inputDt.begin();
211 dt!=inputDt.end() && !fRequireSteeringBlocks;
213 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeSOR);
214 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeRunType);
215 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeEOR);
216 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeDDL);
217 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeComponentStatistics);
220 if (pArguments) delete [] pArguments;
222 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
223 // benchmarking stopwatch for the component statistics
224 fpBenchmark=new TStopwatch;
226 fpBenchmark->Start();
233 int AliHLTComponent::Deinit()
235 // see header file for function documentation
236 HLTLogKeyword(GetComponentID());
240 HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
241 AliHLTRunDesc* pRunDesc=fpRunDesc;
249 int AliHLTComponent::InitCDB(const char* cdbPath, AliHLTComponentHandler* pHandler)
251 // see header file for function documentation
254 // I have to think about separating the library handling from the
255 // component handler. Requiring the component hanlder here is not
256 // the cleanest solution.
257 // We presume the library already to be loaded
259 AliHLTMiscInitCDB_t pFunc=(AliHLTMiscInitCDB_t)pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_INIT_CDB);
262 if (cdbPath && cdbPath[0]!=0) {
264 // very temporary fix, have to check for other formats
265 if (!path.BeginsWith("local://")) {
270 if ((iResult=(*pFunc)(path.Data()))>=0) {
271 if (!(fCDBSetRunNoFunc=pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_SET_CDB_RUNNO))) {
272 Message(NULL, kHLTLogWarning, "AliHLTComponent::InitCDB", "init CDB",
273 "can not find function to set CDB run no");
277 Message(NULL, kHLTLogError, "AliHLTComponent::InitCDB", "init CDB",
278 "can not find initialization function");
287 int AliHLTComponent::SetCDBRunNo(int runNo)
289 // see header file for function documentation
290 if (!fCDBSetRunNoFunc) return 0;
291 return (*((AliHLTMiscSetCDBRunNo_t)fCDBSetRunNoFunc))(runNo);
294 int AliHLTComponent::SetRunDescription(const AliHLTRunDesc* desc, const char* /*runType*/)
296 // see header file for function documentation
297 if (!desc) return -EINVAL;
298 if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
299 HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
304 fpRunDesc=new AliHLTRunDesc;
305 if (!fpRunDesc) return -ENOMEM;
306 *fpRunDesc=kAliHLTVoidRunDesc;
309 if (fpRunDesc->fRunNo!=kAliHLTVoidRunNo && fpRunDesc->fRunNo!=desc->fRunNo) {
310 HLTWarning("Run description has already been set");
313 SetCDBRunNo(fpRunDesc->fRunNo);
314 // TODO: we have to decide about the runType
318 int AliHLTComponent::SetComponentDescription(const char* desc)
320 // see header file for function documentation
324 TString descriptor=desc;
325 TObjArray* pTokens=descriptor.Tokenize(" ");
327 for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
328 TString argument=((TObjString*)pTokens->At(i++))->GetString();
329 if (!argument || argument.IsNull()) continue;
332 if (argument.BeginsWith("chainid")) {
333 argument.ReplaceAll("chainid", "");
334 if (argument.BeginsWith("=")) {
335 fChainId=argument.Replace(0,1,"");
336 fChainIdCrc=CalculateChecksum((const AliHLTUInt8_t*)fChainId.c_str(), fChainId.length());
337 HLTDebug("setting component description: chain id %s crc 0x%8x", fChainId.c_str(), fChainIdCrc);
342 HLTWarning("unknown component description %s", argument.Data());
350 int AliHLTComponent::DoInit( int /*argc*/, const char** /*argv*/)
352 // default implementation, childs can overload
353 HLTLogKeyword("dummy");
357 int AliHLTComponent::DoDeinit()
359 // default implementation, childs can overload
360 HLTLogKeyword("dummy");
364 int AliHLTComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/)
366 // default implementation, childs can overload
367 HLTLogKeyword("dummy");
371 int AliHLTComponent::ReadPreprocessorValues(const char* /*modules*/)
373 // default implementation, childs can overload
374 HLTLogKeyword("dummy");
378 int AliHLTComponent::StartOfRun()
380 // default implementation, childs can overload
381 HLTLogKeyword("dummy");
385 int AliHLTComponent::EndOfRun()
387 // default implementation, childs can overload
388 HLTLogKeyword("dummy");
393 int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
395 // default implementation, childs can overload
396 HLTLogKeyword("dummy");
400 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
402 // see header file for function documentation
403 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
404 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
405 strcat( output, ":" );
406 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
409 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
411 // see header file for function documentation
417 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
418 sprintf(tmp, "'%d", type.fOrigin[i]);
422 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
423 sprintf(tmp, "%d'", type.fID[i]);
432 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
433 unsigned char* puc=(unsigned char*)type.fOrigin;
435 sprintf(tmp, "'\\%x", type.fOrigin[i]);
437 sprintf(tmp, "'%c", type.fOrigin[i]);
441 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
442 unsigned char* puc=(unsigned char*)type.fID;
444 sprintf(tmp, "\\%x'", type.fID[i]);
446 sprintf(tmp, "%c'", type.fID[i]);
452 if (type==kAliHLTVoidDataType) {
455 // some gymnastics in order to avoid a '0' which is part of either or both
456 // ID and origin terminating the whole string. Unfortunately, string doesn't
457 // stop appending at the '0' if the number of elements to append was
458 // explicitely specified
460 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
461 out.append(tmp.c_str());
464 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
465 out.append(tmp.c_str());
471 void* AliHLTComponent::AllocMemory( unsigned long size )
473 // see header file for function documentation
474 if (fEnvironment.fAllocMemoryFunc)
475 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
476 HLTFatal("no memory allocation handler registered");
480 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
481 AliHLTComponentBlockData** outputBlocks )
483 // see header file for function documentation
484 if ( blockCount==NULL || outputBlocks==NULL )
486 AliHLTUInt32_t count = blocks.size();
490 *outputBlocks = NULL;
493 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
494 if ( !*outputBlocks )
496 for ( unsigned long i = 0; i < count; i++ ) {
497 (*outputBlocks)[i] = blocks[i];
498 if (MatchExactly(blocks[i].fDataType, kAliHLTAnyDataType)) {
499 (*outputBlocks)[i].fDataType=GetOutputDataType();
500 /* data type was set to the output data type by the PubSub AliRoot
501 Wrapper component, if data type of the block was ********:****.
502 Now handled by the component base class in order to have same
503 behavior when running embedded in AliRoot
504 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
505 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
514 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
516 // see header file for function documentation
517 if (fEnvironment.fGetEventDoneDataFunc)
518 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
522 int AliHLTComponent::ReserveEventDoneData( unsigned long size )
524 // see header file for function documentation
528 if (size>fEventDoneDataSize) {
529 AliHLTComponentEventDoneData* newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[ sizeof(AliHLTComponentEventDoneData)+size ] );
532 newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
533 newEDD->fDataSize = 0;
534 newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
535 if (fEventDoneData) {
536 memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
537 newEDD->fDataSize = fEventDoneData->fDataSize;
538 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
540 fEventDoneData = newEDD;
541 fEventDoneDataSize = size;
547 int AliHLTComponent::PushEventDoneData( AliHLTUInt32_t eddDataWord )
551 if (fEventDoneData->fDataSize+sizeof(AliHLTUInt32_t)>fEventDoneDataSize)
553 *reinterpret_cast<AliHLTUInt32_t*>((reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize)) = eddDataWord;
554 fEventDoneData->fDataSize += sizeof(AliHLTUInt32_t);
558 void AliHLTComponent::ReleaseEventDoneData()
561 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
562 fEventDoneData = NULL;
563 fEventDoneDataSize = 0;
567 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
569 // see header file for function documentation
572 AliHLTComponentDataTypeList itypes;
573 AliHLTComponentDataTypeList otypes;
574 otypes.push_back(GetOutputDataType());
575 if (otypes[0]==kAliHLTMultipleDataType) {
578 if ((count=GetOutputDataTypes(otypes))>0) {
579 } else if (GetComponentType()!=kSink) {
580 HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
583 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(itypes);
584 AliHLTComponentDataTypeList::iterator otype=otypes.begin();
585 for (;otype!=otypes.end();otype++) {
586 //PrintDataTypeContent((*otype), "publisher \'%s\'");
587 if ((*otype)==(kAliHLTAnyDataType|kAliHLTDataOriginPrivate)) {
588 if (tgtList) tgtList->push_back(*otype);
593 AliHLTComponentDataTypeList::iterator itype=itypes.begin();
594 for (;itype!=itypes.end() && (*itype)!=(*otype); itype++);
595 //if (itype!=itypes.end()) PrintDataTypeContent(*itype, "consumer \'%s\'");
596 if (itype!=itypes.end()) {
597 if (tgtList) tgtList->push_back(*otype);
607 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format)
609 // see header file for function documentation
610 const char* fmt="\'%s\'";
611 if (format) fmt=format;
612 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, Form(fmt, (DataType2Text(dt)).c_str()));
613 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL,
614 Form("%x %x %x %x %x %x %x %x : %x %x %x %x",
629 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
631 // see header file for function documentation
632 blockData.fStructSize = sizeof(blockData);
633 FillShmData( blockData.fShmKey );
634 blockData.fOffset = ~(AliHLTUInt32_t)0;
635 blockData.fPtr = NULL;
637 FillDataType( blockData.fDataType );
638 blockData.fSpecification = kAliHLTVoidDataSpec;
641 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
643 // see header file for function documentation
644 shmData.fStructSize = sizeof(shmData);
645 shmData.fShmType = gkAliHLTComponentInvalidShmType;
646 shmData.fShmID = gkAliHLTComponentInvalidShmID;
649 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
651 // see header file for function documentation
652 dataType=kAliHLTAnyDataType;
655 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
657 // see header file for function documentation
658 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
659 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
662 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
664 // see header file for function documentation
665 tgtdt.fStructSize=sizeof(AliHLTComponentDataType);
667 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
668 strncpy(&tgtdt.fID[0], id, strlen(id)<(size_t)kAliHLTComponentDataTypefIDsize?strlen(id):kAliHLTComponentDataTypefIDsize);
671 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
672 strncpy(&tgtdt.fOrigin[0], origin, strlen(origin)<(size_t)kAliHLTComponentDataTypefOriginSize?strlen(origin):kAliHLTComponentDataTypefOriginSize);
676 void AliHLTComponent::SetDataType(AliHLTComponentDataType& dt, AliHLTUInt64_t id, AliHLTUInt32_t origin)
678 // see header file for function documentation
679 dt.fStructSize=sizeof(AliHLTComponentDataType);
680 assert(kAliHLTComponentDataTypefIDsize==sizeof(id));
681 assert(kAliHLTComponentDataTypefOriginSize==sizeof(origin));
682 memcpy(&dt.fID, &id, kAliHLTComponentDataTypefIDsize);
683 memcpy(&dt.fOrigin, &origin, kAliHLTComponentDataTypefOriginSize);
686 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
688 // see header file for function documentation
689 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
690 evtData.fStructSize=sizeof(AliHLTComponentEventData);
691 evtData.fEventID=kAliHLTVoidEventID;
694 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
696 // see header file for function documentation
698 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
699 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
700 if (dt.fID[i]!=0) msg+=dt.fID[i];
704 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
705 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
709 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
712 int AliHLTComponent::GetEventCount() const
714 // see header file for function documentation
718 int AliHLTComponent::IncrementEventCounter()
720 // see header file for function documentation
721 if (fEventCount>=0) fEventCount++;
725 int AliHLTComponent::GetNumberOfInputBlocks() const
727 // see header file for function documentation
728 if (fpInputBlocks!=NULL) {
729 return fCurrentEventData.fBlockCnt;
734 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
735 const char* classname,
738 // see header file for function documentation
739 ALIHLTCOMPONENT_BASE_STOPWATCH();
741 if (classname) fClassName=classname;
742 else fClassName.clear();
743 int idx=FindInputBlock(fSearchDataType, 0, 1);
746 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
747 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
748 fCurrentInputBlock=idx;
755 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
756 const char* dtOrigin,
757 const char* classname,
760 // see header file for function documentation
761 ALIHLTCOMPONENT_BASE_STOPWATCH();
762 AliHLTComponentDataType dt;
763 SetDataType(dt, dtID, dtOrigin);
764 return GetFirstInputObject(dt, classname, bForce);
767 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
769 // see header file for function documentation
770 ALIHLTCOMPONENT_BASE_STOPWATCH();
771 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
772 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
775 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
776 fCurrentInputBlock=idx;
782 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
784 // see header file for function documentation
786 if (fpInputBlocks!=NULL) {
787 int idx=startIdx<0?0:startIdx;
788 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
789 if (dt!=fpInputBlocks[idx].fDataType) continue;
792 if (fpInputBlocks[idx].fPtr==NULL) continue;
793 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
794 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
802 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
804 // see header file for function documentation
806 if (fpInputBlocks!=NULL) {
807 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
808 if (fpInputBlocks[idx].fPtr) {
809 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
810 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
811 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
812 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
813 TClass* objclass=msg.GetClass();
814 pObj=msg.ReadObject(objclass);
815 if (pObj && objclass) {
816 HLTDebug("object %p type %s created", pObj, objclass->GetName());
820 } else if (bForce!=0) {
821 HLTError("size mismatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
824 HLTFatal("block descriptor empty");
827 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
830 HLTError("no input blocks available");
836 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
838 // see header file for function documentation
839 if (fpInputObjects==NULL) {
840 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
843 if (fpInputObjects) {
844 pObj=fpInputObjects->At(idx);
846 pObj=CreateInputObject(idx, bForce);
848 fpInputObjects->AddAt(pObj, idx);
852 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
857 int AliHLTComponent::CleanupInputObjects()
859 // see header file for function documentation
860 if (!fpInputObjects) return 0;
861 TObjArray* array=fpInputObjects;
863 for (int i=0; i<array->GetEntries(); i++) {
864 TObject* pObj=array->At(i);
865 // grrr, garbage collection strikes back: When read via AliHLTMessage
866 // (CreateInputObject), and written to a TFile afterwards, the
867 // TFile::Close calls ROOOT's garbage collection. No clue why the
868 // object ended up in the key list and needs to be deleted
869 if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
875 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
877 // see header file for function documentation
878 ALIHLTCOMPONENT_BASE_STOPWATCH();
879 AliHLTComponentDataType dt=kAliHLTVoidDataType;
880 int idx=fCurrentInputBlock;
882 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
884 HLTError("unknown object %p", pObject);
888 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
889 dt=fpInputBlocks[idx].fDataType;
891 HLTFatal("severe internal error, index out of range");
897 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
899 // see header file for function documentation
900 ALIHLTCOMPONENT_BASE_STOPWATCH();
901 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
902 int idx=fCurrentInputBlock;
904 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
906 HLTError("unknown object %p", pObject);
910 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
911 iSpec=fpInputBlocks[idx].fSpecification;
913 HLTFatal("severe internal error, index out of range");
919 int AliHLTComponent::Forward(const TObject* pObject)
921 // see header file for function documentation
923 int idx=fCurrentInputBlock;
925 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
927 HLTError("unknown object %p", pObject);
932 fOutputBlocks.push_back(fpInputBlocks[idx]);
937 int AliHLTComponent::Forward(const AliHLTComponentBlockData* pBlock)
939 // see header file for function documentation
941 int idx=fCurrentInputBlock;
943 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
945 HLTError("unknown Block %p", pBlock);
950 // check for fpInputBlocks pointer done in FindInputBlock
951 fOutputBlocks.push_back(fpInputBlocks[idx]);
956 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
958 // see header file for function documentation
959 ALIHLTCOMPONENT_BASE_STOPWATCH();
962 int idx=FindInputBlock(fSearchDataType, 0);
963 const AliHLTComponentBlockData* pBlock=NULL;
965 // check for fpInputBlocks pointer done in FindInputBlock
966 pBlock=&fpInputBlocks[idx];
967 fCurrentInputBlock=idx;
972 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
973 const char* dtOrigin)
975 // see header file for function documentation
976 ALIHLTCOMPONENT_BASE_STOPWATCH();
977 AliHLTComponentDataType dt;
978 SetDataType(dt, dtID, dtOrigin);
979 return GetFirstInputBlock(dt);
982 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index) const
984 // see header file for function documentation
985 ALIHLTCOMPONENT_BASE_STOPWATCH();
986 assert( 0 <= index and index < (int)fCurrentEventData.fBlockCnt );
987 return &fpInputBlocks[index];
990 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
992 // see header file for function documentation
993 ALIHLTCOMPONENT_BASE_STOPWATCH();
994 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
995 const AliHLTComponentBlockData* pBlock=NULL;
997 // check for fpInputBlocks pointer done in FindInputBlock
998 pBlock=&fpInputBlocks[idx];
999 fCurrentInputBlock=idx;
1004 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
1006 // see header file for function documentation
1007 int iResult=-ENOENT;
1008 if (fpInputBlocks!=NULL) {
1010 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
1011 iResult=(int)(pBlock-fpInputBlocks);
1020 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
1022 // see header file for function documentation
1023 ALIHLTCOMPONENT_BASE_STOPWATCH();
1024 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
1025 int idx=fCurrentInputBlock;
1027 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
1029 HLTError("unknown Block %p", pBlock);
1033 // check for fpInputBlocks pointer done in FindInputBlock
1034 iSpec=fpInputBlocks[idx].fSpecification;
1039 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1040 void* pHeader, int headerSize)
1042 // see header file for function documentation
1043 ALIHLTCOMPONENT_BASE_STOPWATCH();
1046 AliHLTMessage msg(kMESS_OBJECT);
1047 msg.WriteObject(pObject);
1048 Int_t iMsgLength=msg.Length();
1050 msg.SetLength(); // sets the length to the first (reserved) word
1051 assert(msg.Buffer()!=NULL);
1052 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
1054 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
1057 HLTError("object serialization failed for object %p", pObject);
1066 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1067 void* pHeader, int headerSize)
1069 // see header file for function documentation
1070 ALIHLTCOMPONENT_BASE_STOPWATCH();
1071 AliHLTComponentDataType dt;
1072 SetDataType(dt, dtID, dtOrigin);
1073 return PushBack(pObject, dt, spec, pHeader, headerSize);
1076 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1077 const void* pHeader, int headerSize)
1079 // see header file for function documentation
1080 ALIHLTCOMPONENT_BASE_STOPWATCH();
1081 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
1084 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1085 const void* pHeader, int headerSize)
1087 // see header file for function documentation
1088 ALIHLTCOMPONENT_BASE_STOPWATCH();
1089 AliHLTComponentDataType dt;
1090 SetDataType(dt, dtID, dtOrigin);
1091 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
1094 int AliHLTComponent::InsertOutputBlock(const void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1095 const void* pHeader, int iHeaderSize)
1097 // see header file for function documentation
1099 int iBlkSize = iBufferSize + iHeaderSize;
1101 if ((pBuffer!=NULL && iBufferSize>0) || (pHeader!=NULL && iHeaderSize>0)) {
1102 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
1103 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1105 // copy header if provided but skip if the header is the target location
1106 // in that case it has already been copied
1107 if (pHeader!=NULL && pHeader!=pTgt) {
1108 memcpy(pTgt, pHeader, iHeaderSize);
1111 pTgt += (AliHLTUInt8_t) iHeaderSize;
1113 // copy buffer if provided but skip if buffer is the target location
1114 // in that case it has already been copied
1115 if (pBuffer!=NULL && pBuffer!=pTgt) {
1116 memcpy(pTgt, pBuffer, iBufferSize);
1118 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
1119 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
1121 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
1123 if (fpOutputBuffer) {
1124 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
1126 HLTError("output buffer not available");
1132 AliHLTComponentBlockData bd;
1133 FillBlockData( bd );
1134 bd.fOffset = fOutputBufferFilled;
1135 bd.fSize = iBlkSize;
1137 bd.fSpecification = spec;
1138 fOutputBlocks.push_back( bd );
1139 fOutputBufferFilled+=bd.fSize;
1145 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
1147 // see header file for function documentation
1148 if (!pObject) return -EINVAL;
1149 AliHLTMessage msg(kMESS_OBJECT);
1150 msg.WriteObject(pObject);
1151 return msg.Length();
1154 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
1155 const char* dtOrigin,
1156 AliHLTUInt32_t spec)
1158 // see header file for function documentation
1159 ALIHLTCOMPONENT_BASE_STOPWATCH();
1160 AliHLTComponentDataType dt;
1161 SetDataType(dt, dtID, dtOrigin);
1162 return CreateMemoryFile(capacity, dt, spec);
1165 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
1166 const AliHLTComponentDataType& dt,
1167 AliHLTUInt32_t spec)
1169 // see header file for function documentation
1170 ALIHLTCOMPONENT_BASE_STOPWATCH();
1171 AliHLTMemoryFile* pFile=NULL;
1172 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
1173 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1174 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
1176 unsigned int nofBlocks=fOutputBlocks.size();
1177 if (nofBlocks+1>fMemFiles.size()) {
1178 fMemFiles.resize(nofBlocks+1, NULL);
1180 if (nofBlocks<fMemFiles.size()) {
1181 fMemFiles[nofBlocks]=pFile;
1182 AliHLTComponentBlockData bd;
1183 FillBlockData( bd );
1184 bd.fOffset = fOutputBufferFilled;
1185 bd.fSize = capacity;
1187 bd.fSpecification = spec;
1188 fOutputBufferFilled+=bd.fSize;
1189 fOutputBlocks.push_back( bd );
1191 HLTError("can not allocate/grow object array");
1192 pFile->CloseMemoryFile(0);
1198 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
1203 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
1204 const char* dtOrigin,
1205 AliHLTUInt32_t spec,
1208 // see header file for function documentation
1209 ALIHLTCOMPONENT_BASE_STOPWATCH();
1210 AliHLTComponentDataType dt;
1211 SetDataType(dt, dtID, dtOrigin);
1212 int size=fOutputBufferSize-fOutputBufferFilled;
1213 if (capacity<0 || capacity>1.0) {
1214 HLTError("invalid parameter: capacity %f", capacity);
1217 size=(int)(size*capacity);
1218 return CreateMemoryFile(size, dt, spec);
1221 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
1222 AliHLTUInt32_t spec,
1225 // see header file for function documentation
1226 ALIHLTCOMPONENT_BASE_STOPWATCH();
1227 int size=fOutputBufferSize-fOutputBufferFilled;
1228 if (capacity<0 || capacity>1.0) {
1229 HLTError("invalid parameter: capacity %f", capacity);
1232 size=(int)(size*capacity);
1233 return CreateMemoryFile(size, dt, spec);
1236 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
1237 const char* key, int option)
1239 // see header file for function documentation
1241 if (pFile && pObject) {
1243 iResult=pObject->Write(key, option);
1247 iResult=-pFile->GetErrno();
1248 if (iResult==-ENOSPC) {
1249 HLTError("error writing memory file, buffer too small");
1258 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
1260 // see header file for function documentation
1263 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1265 while (element!=fMemFiles.end() && iResult>=0) {
1266 if (*element && *element==pFile) {
1267 iResult=pFile->CloseMemoryFile();
1269 // sync memory files and descriptors
1271 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
1279 HLTError("can not find memory file %p", pFile);
1287 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
1289 // see header file for function documentation
1292 AliHLTComponentEventDoneData* newEDD = NULL;
1294 unsigned long newSize=edd.fDataSize;
1296 newSize += fEventDoneData->fDataSize;
1298 if (newSize>fEventDoneDataSize) {
1299 newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[ sizeof(AliHLTComponentEventDoneData)+newSize ] );
1302 newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
1303 newEDD->fDataSize = newSize;
1304 newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
1305 unsigned long long offset = 0;
1306 if (fEventDoneData) {
1307 memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
1308 offset += fEventDoneData->fDataSize;
1310 memcpy( reinterpret_cast<AliHLTUInt8_t*>(newEDD->fData)+offset, edd.fData, edd.fDataSize );
1312 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
1313 fEventDoneData = newEDD;
1314 fEventDoneDataSize = newSize;
1317 memcpy( reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize, edd.fData, edd.fDataSize );
1318 fEventDoneData->fDataSize += edd.fDataSize;
1323 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1324 const AliHLTComponentBlockData* blocks,
1325 AliHLTComponentTriggerData& trigData,
1326 AliHLTUInt8_t* outputPtr,
1327 AliHLTUInt32_t& size,
1328 AliHLTUInt32_t& outputBlockCnt,
1329 AliHLTComponentBlockData*& outputBlocks,
1330 AliHLTComponentEventDoneData*& edd )
1332 // see header file for function documentation
1333 HLTLogKeyword(GetComponentID());
1334 ALIHLTCOMPONENT_BASE_STOPWATCH();
1336 fCurrentEvent=evtData.fEventID;
1337 fCurrentEventData=evtData;
1338 fpInputBlocks=blocks;
1339 fCurrentInputBlock=-1;
1340 fSearchDataType=kAliHLTAnyDataType;
1341 fpOutputBuffer=outputPtr;
1342 fOutputBufferSize=size;
1343 fOutputBufferFilled=0;
1344 fOutputBlocks.clear();
1348 AliHLTComponentBlockDataList forwardedBlocks;
1350 // optional component statistics
1351 AliHLTComponentStatisticsList compStats;
1352 bool bAddComponentTableEntry=false;
1353 vector<AliHLTUInt32_t> parentComponentTables;
1354 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
1355 AliHLTComponentStatistics outputStat;
1356 memset(&outputStat, 0, sizeof(AliHLTComponentStatistics));
1357 outputStat.fId=fChainIdCrc;
1358 compStats.push_back(outputStat);
1360 fpBenchmark->Reset();
1361 fpBenchmark->Start();
1365 // data processing is skipped
1366 // - if there are only steering events in the block list.
1367 // For the sake of data source components data processing
1368 // is not skipped if there is no block list at all or if it
1369 // just contains the eventType block
1370 // - always skipped if the event is of type
1371 // - gkAliEventTypeConfiguration
1372 // - gkAliEventTypeReadPreprocessor
1373 const unsigned int skipModeDefault=0x1;
1374 const unsigned int skipModeForce=0x2;
1375 unsigned int bSkipDataProcessing=skipModeDefault;
1377 // find special events
1378 if (fpInputBlocks && evtData.fBlockCnt>0) {
1379 // first look for all special events and execute in the appropriate
1380 // sequence afterwords
1381 int indexComConfEvent=-1;
1382 int indexUpdtDCSEvent=-1;
1383 int indexSOREvent=-1;
1384 int indexEOREvent=-1;
1385 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1386 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1388 // the AliHLTCalibrationProcessor relies on the SOR and EOR events
1389 bSkipDataProcessing&=~skipModeDefault;
1390 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeRunType) {
1392 // handling is not clear yet
1393 if (fpInputBlocks[i].fPtr) {
1394 HLTDebug("got run type \"%s\"\n", fpInputBlocks[i].fPtr);
1396 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1398 // the calibration processor relies on the SOR and EOR events
1399 bSkipDataProcessing&=~skipModeDefault;
1400 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1402 // this event is most likely deprecated
1403 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComConf) {
1404 indexComConfEvent=i;
1405 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeUpdtDCS) {
1406 indexUpdtDCSEvent=i;
1407 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEvent) {
1408 fEventType=fpInputBlocks[i].fSpecification;
1410 // skip always in case of gkAliEventTypeConfiguration
1411 if (fpInputBlocks[i].fSpecification==gkAliEventTypeConfiguration) bSkipDataProcessing|=skipModeForce;
1413 // skip always in case of gkAliEventTypeReadPreprocessor
1414 if (fpInputBlocks[i].fSpecification==gkAliEventTypeReadPreprocessor) bSkipDataProcessing|=skipModeForce;
1416 // never skip if the event type block is the only block
1417 if (evtData.fBlockCnt==1) bSkipDataProcessing&=~skipModeDefault;
1419 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentStatistics) {
1420 if (compStats.size()>0) {
1421 AliHLTUInt8_t* pData=reinterpret_cast<AliHLTUInt8_t*>(fpInputBlocks[i].fPtr);
1422 for (AliHLTUInt32_t offset=0;
1423 offset+sizeof(AliHLTComponentStatistics)<=fpInputBlocks[i].fSize;
1424 offset+=sizeof(AliHLTComponentStatistics)) {
1425 AliHLTComponentStatistics* pStat=reinterpret_cast<AliHLTComponentStatistics*>(pData+offset);
1426 if (pStat && compStats[0].fLevel<=pStat->fLevel) {
1427 compStats[0].fLevel=pStat->fLevel+1;
1429 compStats.push_back(*pStat);
1432 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentTable) {
1433 forwardedBlocks.push_back(fpInputBlocks[i]);
1434 parentComponentTables.push_back(fpInputBlocks[i].fSpecification);
1436 // the processing function is called if there is at least one
1437 // non-steering data block. Steering blocks are not filtered out
1438 // for sake of performance
1439 bSkipDataProcessing&=~skipModeDefault;
1440 if (compStats.size()>0) {
1441 compStats[0].fInputBlockCount++;
1442 compStats[0].fTotalInputSize+=fpInputBlocks[i].fSize;
1447 if (indexSOREvent>=0) {
1449 bAddComponentTableEntry=true;
1450 if (fpRunDesc==NULL) {
1451 fpRunDesc=new AliHLTRunDesc;
1452 if (fpRunDesc) *fpRunDesc=kAliHLTVoidRunDesc;
1455 AliHLTRunDesc rundesc;
1456 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexSOREvent, "AliHLTRunDesc", "SOR"))>0) {
1457 if (fpRunDesc->fRunNo==kAliHLTVoidRunNo) {
1459 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1460 SetCDBRunNo(fpRunDesc->fRunNo);
1461 } else if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1462 HLTWarning("already set run properties run no %d, ignoring SOR with run no %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1469 if (indexEOREvent>=0) {
1470 bAddComponentTableEntry=true;
1471 if (fpRunDesc!=NULL) {
1473 AliHLTRunDesc rundesc;
1474 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexEOREvent, "AliHLTRunDesc", "SOR"))>0) {
1475 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1476 HLTWarning("run no mismatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1478 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1481 AliHLTRunDesc* pRunDesc=fpRunDesc;
1486 HLTWarning("did not receive SOR, ignoring EOR");
1489 if (indexComConfEvent>=0 || fEventType==gkAliEventTypeConfiguration) {
1491 if (indexComConfEvent>=0 && fpInputBlocks[indexComConfEvent].fPtr!=NULL && fpInputBlocks[indexComConfEvent].fSize>0) {
1492 cdbEntry.Append(reinterpret_cast<const char*>(fpInputBlocks[indexComConfEvent].fPtr), fpInputBlocks[indexComConfEvent].fSize);
1494 HLTDebug("received component configuration command: entry %s", cdbEntry.IsNull()?"none":cdbEntry.Data());
1495 int tmpResult=Reconfigure(cdbEntry[0]==0?NULL:cdbEntry.Data(), fChainId.c_str());
1497 HLTWarning("reconfiguration of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1500 if (indexUpdtDCSEvent>=0 || fEventType==gkAliEventTypeReadPreprocessor) {
1502 if (fpInputBlocks[indexUpdtDCSEvent].fPtr!=NULL && fpInputBlocks[indexUpdtDCSEvent].fSize>0) {
1503 modules.Append(reinterpret_cast<const char*>(fpInputBlocks[indexUpdtDCSEvent].fPtr), fpInputBlocks[indexUpdtDCSEvent].fSize);
1505 HLTDebug("received preprocessor update command: detectors %s", modules.IsNull()?"ALL":modules.Data());
1506 int tmpResult=ReadPreprocessorValues(modules[0]==0?"ALL":modules.Data());
1508 HLTWarning("preprocessor update of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1512 // processing function needs to be called if there are no input data
1513 // blocks in order to make data source components working.
1514 bSkipDataProcessing&=~skipModeDefault;
1517 // data processing is not skipped if the component explicitly asks
1518 // for the private blocks
1519 if (fRequireSteeringBlocks) bSkipDataProcessing=0;
1521 AliHLTComponentBlockDataList blockData;
1522 if (iResult>=0 && !bSkipDataProcessing)
1523 { // dont delete, sets the scope for the stopwatch guard
1524 // do not use ALIHLTCOMPONENT_DA_STOPWATCH(); macro
1525 // in order to avoid 'shadowed variable' warning
1526 AliHLTStopwatchGuard swguard2(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)kSWDA)):NULL);
1527 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1528 } // end of the scope of the stopwatch guard
1529 if (iResult>=0 && !bSkipDataProcessing) {
1530 if (fOutputBlocks.size()>0) {
1531 // High Level interface
1533 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1534 // sync memory files and descriptors
1535 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1537 while (element!=fMemFiles.end() && iResult>=0) {
1539 if ((*element)->IsClosed()==0) {
1540 HLTWarning("memory file has not been closed, force flush");
1541 iResult=CloseMemoryFile(*element);
1548 // create the descriptor list
1549 if (blockData.size()>0) {
1550 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1553 if (compStats.size()>0 && IsDataEvent()) {
1554 int offset=AddComponentStatistics(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, compStats);
1555 if (offset>0) fOutputBufferFilled+=offset;
1557 if (bAddComponentTableEntry) {
1558 int offset=AddComponentTableEntry(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, parentComponentTables);
1559 if (offset>0) size+=offset;
1561 if (forwardedBlocks.size()>0) {
1562 fOutputBlocks.insert(fOutputBlocks.end(), forwardedBlocks.begin(), forwardedBlocks.end());
1564 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1565 size=fOutputBufferFilled;
1569 // Low Level interface
1570 if (compStats.size()>0) {
1571 int offset=AddComponentStatistics(blockData, fpOutputBuffer, fOutputBufferSize, size, compStats);
1572 if (offset>0) size+=offset;
1574 if (bAddComponentTableEntry) {
1575 int offset=AddComponentTableEntry(blockData, fpOutputBuffer, fOutputBufferSize, size, parentComponentTables);
1576 if (offset>0) size+=offset;
1578 if (forwardedBlocks.size()>0) {
1579 blockData.insert(blockData.end(), forwardedBlocks.begin(), forwardedBlocks.end());
1581 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1584 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1587 if (iResult<0 || bSkipDataProcessing) {
1591 CleanupInputObjects();
1592 if (iResult>=0 && IsDataEvent()) {
1593 IncrementEventCounter();
1595 if (outputBlockCnt==0) {
1596 // no output blocks, set size to 0
1602 int AliHLTComponent::AddComponentStatistics(AliHLTComponentBlockDataList& blocks,
1603 AliHLTUInt8_t* buffer,
1604 AliHLTUInt32_t bufferSize,
1605 AliHLTUInt32_t offset,
1606 AliHLTComponentStatisticsList& stats) const
1608 // see header file for function documentation
1610 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
1611 if (stats.size()==0) return -ENOENT;
1612 stats[0].fTotalOutputSize=offset;
1613 stats[0].fOutputBlockCount=blocks.size();
1615 stats[0].fTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*1000000);
1616 stats[0].fCTime=(AliHLTUInt32_t)(fpBenchmark->CpuTime()*1000000);
1618 if (offset+stats.size()*sizeof(AliHLTComponentStatistics)<=bufferSize) {
1619 AliHLTComponentBlockData bd;
1620 FillBlockData( bd );
1621 bd.fOffset = offset;
1622 bd.fSize = stats.size()*sizeof(AliHLTComponentStatistics);
1623 bd.fDataType = kAliHLTDataTypeComponentStatistics;
1624 bd.fSpecification = kAliHLTVoidDataSpec;
1625 unsigned int master=0;
1626 for (unsigned int i=1; i<blocks.size(); i++) {
1627 if (blocks[i].fSize>blocks[master].fSize &&
1628 !MatchExactly(blocks[i].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate))
1631 if (blocks.size()>0 && !MatchExactly(blocks[master].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate)) {
1632 // take the data origin of the biggest block as specification
1633 // this is similar to the treatment in the HOMER interface. For traditional
1634 // reasons, the bytes are swapped there on a little endian architecture, so
1635 // we do it as well.
1636 memcpy(&bd.fSpecification, &blocks[master].fDataType.fOrigin, sizeof(bd.fSpecification));
1637 #ifdef R__BYTESWAP // set on little endian architectures
1638 bd.fSpecification=((bd.fSpecification & 0xFFULL) << 24) |
1639 ((bd.fSpecification & 0xFF00ULL) << 8) |
1640 ((bd.fSpecification & 0xFF0000ULL) >> 8) |
1641 ((bd.fSpecification & 0xFF000000ULL) >> 24);
1644 memcpy(buffer+offset, &(stats[0]), bd.fSize);
1645 blocks.push_back(bd);
1649 if (blocks.size() && buffer && bufferSize && offset && stats.size()) {
1650 // get rid of warning
1656 int AliHLTComponent::AddComponentTableEntry(AliHLTComponentBlockDataList& blocks,
1657 AliHLTUInt8_t* buffer,
1658 AliHLTUInt32_t bufferSize,
1659 AliHLTUInt32_t offset,
1660 const vector<AliHLTUInt32_t>& parents) const
1662 // see header file for function documentation
1664 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
1665 // the payload consists of the AliHLTComponentTableEntry struct,
1666 // followed by a an array of 32bit crc chain ids and the component
1667 // description string
1668 unsigned int payloadSize=sizeof(AliHLTComponentTableEntry);
1669 payloadSize+=parents.size()*sizeof(AliHLTUInt32_t);
1671 // the component description has the following format:
1672 // chain-id{component-id:arguments}
1673 const char* componentId=const_cast<AliHLTComponent*>(this)->GetComponentID();
1674 unsigned int descriptionSize=fChainId.size()+1;
1675 descriptionSize+=2; // the '{}' around the component id
1676 descriptionSize+=strlen(componentId);
1677 descriptionSize+=1; // the ':' between component id and arguments
1678 descriptionSize+=fComponentArgs.size();
1680 payloadSize+=descriptionSize;
1681 if (buffer && (offset+payloadSize<=bufferSize)) {
1682 AliHLTUInt8_t* pTgt=buffer+offset;
1683 memset(pTgt, 0, payloadSize);
1686 AliHLTComponentTableEntry* pEntry=reinterpret_cast<AliHLTComponentTableEntry*>(pTgt);
1687 pEntry->fStructSize=sizeof(AliHLTComponentTableEntry);
1688 pEntry->fNofParents=parents.size();
1689 pEntry->fSizeDescription=descriptionSize;
1690 pTgt=pEntry->fBuffer;
1692 // write array of parents
1693 if (parents.size()>0) {
1694 unsigned int copy=parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type);
1695 memcpy(pTgt, &parents[0], parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type));
1699 // write component description
1700 memcpy(pTgt, fChainId.c_str(), fChainId.size());
1701 pTgt+=fChainId.size();
1703 memcpy(pTgt, componentId, strlen(componentId));
1704 pTgt+=strlen(componentId);
1706 memcpy(pTgt, fComponentArgs.c_str(), fComponentArgs.size());
1707 pTgt+=fComponentArgs.size();
1711 AliHLTComponentBlockData bd;
1712 FillBlockData( bd );
1713 bd.fOffset = offset;
1714 bd.fSize = payloadSize;
1715 bd.fDataType = kAliHLTDataTypeComponentTable;
1716 bd.fSpecification = fChainIdCrc;
1717 blocks.push_back(bd);
1721 if (blocks.size() && buffer && bufferSize && offset && parents.size()) {
1722 // get rid of warning
1728 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1733 // standard constructor (not for use)
1736 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1738 fpStopwatch(pStopwatch),
1743 // check for already existing guard
1744 if (fgpCurrent) fpPrec=fgpCurrent;
1747 // stop the preceeding guard if it controls a different stopwatch
1749 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1751 // start the stopwatch if the current guard controls a different one
1752 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1755 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
1761 // copy constructor not for use
1765 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1768 // assignment operator not for use
1775 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1777 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1781 // resume the preceeding guard if it controls a different stopwatch
1783 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1785 // stop the stopwatch if the current guard controls a different one
1786 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1788 // resume to the preceeding guard
1792 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1794 // see header file for function documentation
1795 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1796 return fpStopwatch!=pSucc?1:0;
1799 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1801 // see header file for function documentation
1802 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1803 return fpStopwatch!=pSucc?1:0;
1806 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
1808 // see header file for function documentation
1810 if (pSW!=NULL && type<kSWTypeCount) {
1811 if (fpStopwatches) {
1812 TObject* pObj=fpStopwatches->At((int)type);
1813 if (pSW==NULL // explicit reset
1814 || pObj==NULL) { // explicit set
1815 fpStopwatches->AddAt(pSW, (int)type);
1816 } else if (pObj!=pSW) {
1817 HLTWarning("stopwatch %d already set, reset first", (int)type);
1827 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1829 // see header file for function documentation
1830 if (pStopwatches==NULL) return -EINVAL;
1833 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1834 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1838 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1840 // see header file for function documentation
1841 if (fpRunDesc==NULL) return kAliHLTVoidRunNo;
1842 return fpRunDesc->fRunNo;
1845 AliHLTUInt32_t AliHLTComponent::GetRunType() const
1847 // see header file for function documentation
1848 if (fpRunDesc==NULL) return kAliHLTVoidRunType;
1849 return fpRunDesc->fRunType;
1852 bool AliHLTComponent::IsDataEvent(AliHLTUInt32_t* pTgt)
1854 // see header file for function documentation
1855 if (pTgt) *pTgt=fEventType;
1856 return (fEventType==gkAliEventTypeData ||
1857 fEventType==gkAliEventTypeDataReplay ||
1858 fEventType==gkAliEventTypeCalibration);
1861 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
1862 const char* structname, const char* eventname)
1864 // see header file for function documentation
1866 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1867 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1868 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1869 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
1870 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
1871 if (fpInputBlocks[iBlockNo].fSize!=copy) {
1872 HLTWarning("%s event: mismatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1873 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1875 if (copy!=iStructSize) {
1876 HLTWarning("%s event: mismatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1877 if (copy>iStructSize) {
1880 memset(pTgt, 0, iStructSize);
1883 memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
1887 HLTWarning("%s event: missing data block", eventname);
1893 HLTError("invalid struct");
1899 void AliHLTComponent::SetDDLBit(AliHLTEventDDL &list, Int_t ddlId, Bool_t state ) const
1901 // see header file for function documentation
1903 // -- Detector offset
1904 Int_t ddlIdBase = TMath::FloorNint( (Double_t) ddlId / 256.0 );
1906 // -- Word Base = 1. word of detector ( TPC has 8 words, TOF 3 )
1909 if ( ddlIdBase <= 3 )
1910 wordBase = ddlIdBase;
1911 else if ( ddlIdBase > 3 && ddlIdBase < 5 )
1912 wordBase = ddlIdBase + 7;
1914 wordBase = ddlIdBase + 9;
1916 // -- Bit index in Word
1917 Int_t bitIdx = ddlId % 32;
1920 Int_t wordIdx = wordBase;
1922 // -- if TPC (3) or TOD (5) add word idx
1923 if ( ( ddlIdBase == 3 ) || ( ddlIdBase == 5 ) ) {
1924 wordIdx += TMath::FloorNint( (Double_t) ( ddlId - ( ddlIdBase * 256 ) ) / 32.0 );
1927 // -- Set -- 'OR' word with bit mask;
1929 list.fList[wordIdx] |= ( 0x00000001 << bitIdx );
1930 // -- Unset -- 'AND' word with bit mask;
1932 list.fList[wordIdx] &= ( 0xFFFFFFFF ^ ( 0x00000001 << bitIdx ) );
1935 Int_t AliHLTComponent::GetFirstUsedDDLWord(AliHLTEventDDL &list) const
1937 // see header file for function documentation
1941 for ( Int_t wordNdx = 0 ; wordNdx < gkAliHLTDDLListSize ; wordNdx++ ) {
1943 if ( list.fList[wordNdx] != 0 && iResult == -1 ) {
1944 // check for special cases TPC and TOF
1945 if ( wordNdx > 3 && wordNdx <= 10 ) {
1949 else if ( wordNdx > 12 && wordNdx <= 14 ) {
1956 else if ( list.fList[wordNdx] != 0 && iResult >= 0 ) {
1957 HLTError( "DDLIDs for minimum of TWO detectors ( %d, %d ) set, this function works only for ONE detector.", iResult, wordNdx );
1966 AliHLTUInt32_t AliHLTComponent::CalculateChecksum(const AliHLTUInt8_t* buffer, int size)
1968 // see header file for function documentation
1969 AliHLTUInt32_t remainder = 0;
1970 const AliHLTUInt8_t crcwidth=(8*sizeof(AliHLTUInt32_t));
1971 const AliHLTUInt32_t topbit=1 << (crcwidth-1);
1972 const AliHLTUInt32_t polynomial=0xD8; /* 11011 followed by 0's */
1975 // http://www.netrino.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
1978 * Perform modulo-2 division, a byte at a time.
1980 for (int byte = 0; byte < size; ++byte)
1983 * Bring the next byte into the remainder.
1985 remainder ^= (buffer[byte] << (crcwidth - 8));
1988 * Perform modulo-2 division, a bit at a time.
1990 for (uint8_t bit = 8; bit > 0; --bit)
1993 * Try to divide the current data bit.
1995 if (remainder & topbit)
1997 remainder = (remainder << 1) ^ polynomial;
2001 remainder = (remainder << 1);
2007 * The final remainder is the CRC result.
2012 int AliHLTComponent::ExtractComponentTableEntry(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size,
2013 string& retChainId, string& retCompId, string& retCompArgs,
2014 vector<AliHLTUInt32_t>& parents)
2016 // see header file for function documentation
2019 retCompArgs.clear();
2021 if (!pBuffer || size==0) return 0;
2023 const AliHLTComponentTableEntry* pEntry=reinterpret_cast<const AliHLTComponentTableEntry*>(pBuffer);
2024 if (size<8/* the initial size of the structure*/ ||
2025 pEntry==NULL || pEntry->fStructSize<8) return -ENOMSG;
2026 const AliHLTUInt32_t* pParents=reinterpret_cast<const AliHLTUInt32_t*>(pEntry->fBuffer);
2027 const AliHLTUInt8_t* pEnd=pBuffer+size;
2029 if (pParents+pEntry->fNofParents>=reinterpret_cast<const AliHLTUInt32_t*>(pEnd)) return -ENODEV;
2030 for (unsigned int i=0; i<pEntry->fNofParents; i++, pParents++) {
2031 parents.push_back(*pParents);
2034 const char* pDescription=reinterpret_cast<const char*>(pParents);
2035 if (pDescription+pEntry->fSizeDescription>=reinterpret_cast<const char*>(pEnd) ||
2036 *(pDescription+pEntry->fSizeDescription)!=0) {
2040 TString descriptor=reinterpret_cast<const char*>(pDescription);
2044 TObjArray* pTokens=descriptor.Tokenize("{");
2047 if (pTokens->GetEntries()>n) {
2048 retChainId=((TObjString*)pTokens->At(n++))->GetString();
2050 if (pTokens->GetEntries()>n) {
2051 compId=((TObjString*)pTokens->At(n++))->GetString();
2055 if (!compId.IsNull() && (pTokens=compId.Tokenize(":"))!=NULL) {
2057 if (pTokens->GetEntries()>n) {
2058 compId=((TObjString*)pTokens->At(n++))->GetString();
2060 if (pTokens->GetEntries()>n) {
2061 compArgs=((TObjString*)pTokens->At(n++))->GetString();
2065 compId.ReplaceAll("}", "");
2066 compArgs.ReplaceAll("}", "");
2069 retCompArgs=compArgs;
2071 if (retChainId.size()==0) return -ENODATA;