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"
43 /** ROOT macro for the implementation of ROOT specific class methods */
44 ClassImp(AliHLTComponent);
46 /** stopwatch macro using the stopwatch guard */
47 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
48 //#define ALIHLTCOMPONENT_STOPWATCH(type)
50 /** stopwatch macro for operations of the base class */
51 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
52 /** stopwatch macro for operations of the detector algorithm (DA) */
53 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
55 AliHLTComponent::AliHLTComponent()
63 fCurrentInputBlock(-1),
64 fSearchDataType(kAliHLTVoidDataType),
69 fOutputBufferFilled(0),
71 fpStopwatches(new TObjArray(kSWTypeCount)),
75 fCDBSetRunNoFunc(false),
79 fRequireSteeringBlocks(false),
80 fEventType(gkAliEventTypeUnknown)
82 // see header file for class documentation
84 // refer to README to build package
86 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
87 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
88 if (fgpComponentHandler)
89 fgpComponentHandler->ScheduleRegister(this);
90 //SetLocalLoggingLevel(kHLTLogDefault);
93 AliHLTComponent::~AliHLTComponent()
95 // see header file for function documentation
96 if (fpBenchmark) delete fpBenchmark;
99 CleanupInputObjects();
100 if (fpStopwatches!=NULL) delete fpStopwatches;
102 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
103 while (element!=fMemFiles.end()) {
105 if ((*element)->IsClosed()==0) {
106 HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
107 // close but do not flush as we dont know whether the buffer is still valid
108 (*element)->CloseMemoryFile(0);
121 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
123 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
125 // see header file for function documentation
127 if (fgpComponentHandler==NULL || bOverwrite!=0)
128 fgpComponentHandler=pCH;
134 int AliHLTComponent::UnsetGlobalComponentHandler()
136 // see header file for function documentation
137 return SetGlobalComponentHandler(NULL,1);
140 int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environParam, int argc, const char** argv )
142 // see header file for function documentation
143 HLTLogKeyword(GetComponentID());
146 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
147 memcpy(&fEnvironment, comenv, comenv->fStructSize<sizeof(AliHLTAnalysisEnvironment)?comenv->fStructSize:sizeof(AliHLTAnalysisEnvironment));
148 fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
149 fEnvironment.fParam=environParam;
151 const char** pArguments=NULL;
156 pArguments=new const char*[argc];
158 for (int i=0; i<argc && iResult>=0; i++) {
160 if (argument.IsNull()) continue;
163 if (argument.CompareTo("benchmark")==0) {
166 } else if (argument.CompareTo("loglevel")==0) {
167 if ((bMissingParam=(++i>=argc))) break;
168 TString parameter(argv[i]);
169 parameter.Remove(TString::kLeading, ' '); // remove all blanks
170 if (parameter.BeginsWith("0x") &&
171 parameter.Replace(0,2,"",0).IsHex()) {
172 AliHLTComponentLogSeverity loglevel=kHLTLogNone;
173 sscanf(parameter.Data(),"%x", (unsigned int*)&loglevel);
174 SetLocalLoggingLevel(loglevel);
176 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
180 pArguments[iNofChildArgs++]=argv[i];
188 HLTError("missing parameter for argument %s", argument.Data());
192 iResult=DoInit(iNofChildArgs, pArguments);
197 // find out if the component wants to get the steering events
199 AliHLTComponentDataTypeList inputDt;
200 GetInputDataTypes(inputDt);
201 for (AliHLTComponentDataTypeList::iterator dt=inputDt.begin();
202 dt!=inputDt.end() && !fRequireSteeringBlocks;
204 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeSOR);
205 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeRunType);
206 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeEOR);
207 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeDDL);
208 fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeComponentStatistics);
211 if (pArguments) delete [] pArguments;
213 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
214 // benchmarking stopwatch for the component statistics
215 fpBenchmark=new TStopwatch;
217 fpBenchmark->Start();
224 int AliHLTComponent::Deinit()
226 // see header file for function documentation
227 HLTLogKeyword(GetComponentID());
231 HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
232 AliHLTRunDesc* pRunDesc=fpRunDesc;
240 int AliHLTComponent::InitCDB(const char* cdbPath, AliHLTComponentHandler* pHandler)
242 // see header file for function documentation
245 // I have to think about separating the library handling from the
246 // component handler. Requiring the component hanlder here is not
247 // the cleanest solution.
248 // We presume the library already to be loaded
250 AliHLTMiscInitCDB_t pFunc=(AliHLTMiscInitCDB_t)pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_INIT_CDB);
253 if (cdbPath && cdbPath[0]!=0) {
255 // very temporary fix, have to check for other formats
256 if (!path.BeginsWith("local://")) {
261 if ((iResult=(*pFunc)(path.Data()))>=0) {
262 if (!(fCDBSetRunNoFunc=pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_SET_CDB_RUNNO))) {
263 Message(NULL, kHLTLogWarning, "AliHLTComponent::InitCDB", "init CDB",
264 "can not find function to set CDB run no");
268 Message(NULL, kHLTLogError, "AliHLTComponent::InitCDB", "init CDB",
269 "can not find initialization function");
278 int AliHLTComponent::SetCDBRunNo(int runNo)
280 // see header file for function documentation
281 if (!fCDBSetRunNoFunc) return 0;
282 return (*((AliHLTMiscSetCDBRunNo_t)fCDBSetRunNoFunc))(runNo);
285 int AliHLTComponent::SetRunDescription(const AliHLTRunDesc* desc, const char* /*runType*/)
287 // see header file for function documentation
288 if (!desc) return -EINVAL;
289 if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
290 HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
295 fpRunDesc=new AliHLTRunDesc;
296 if (!fpRunDesc) return -ENOMEM;
297 *fpRunDesc=kAliHLTVoidRunDesc;
300 if (fpRunDesc->fRunNo!=kAliHLTVoidRunNo && fpRunDesc->fRunNo!=desc->fRunNo) {
301 HLTWarning("Run description has already been set");
304 SetCDBRunNo(fpRunDesc->fRunNo);
305 // TODO: we have to decide about the runType
309 int AliHLTComponent::SetComponentDescription(const char* desc)
311 // see header file for function documentation
315 TString descriptor=desc;
316 TObjArray* pTokens=descriptor.Tokenize(" ");
318 for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
319 TString argument=((TObjString*)pTokens->At(i++))->GetString();
320 if (!argument || argument.IsNull()) continue;
323 if (argument.BeginsWith("chainid")) {
324 argument.ReplaceAll("chainid", "");
325 if (argument.BeginsWith("=")) {
326 fChainId=argument.Replace(0,1,"");
327 fChainIdCrc=CalculateChecksum((const AliHLTUInt8_t*)fChainId.c_str(), fChainId.length());
328 HLTDebug("setting component description: chain id %s crc 0x%8x", fChainId.c_str(), fChainIdCrc);
333 HLTWarning("unknown component description %s", argument.Data());
341 int AliHLTComponent::DoInit( int /*argc*/, const char** /*argv*/)
343 // default implementation, childs can overload
344 HLTLogKeyword("dummy");
348 int AliHLTComponent::DoDeinit()
350 // default implementation, childs can overload
351 HLTLogKeyword("dummy");
355 int AliHLTComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/)
357 // default implementation, childs can overload
358 HLTLogKeyword("dummy");
362 int AliHLTComponent::ReadPreprocessorValues(const char* /*modules*/)
364 // default implementation, childs can overload
365 HLTLogKeyword("dummy");
369 int AliHLTComponent::StartOfRun()
371 // default implementation, childs can overload
372 HLTLogKeyword("dummy");
376 int AliHLTComponent::EndOfRun()
378 // default implementation, childs can overload
379 HLTLogKeyword("dummy");
384 int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
386 // default implementation, childs can overload
387 HLTLogKeyword("dummy");
391 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
393 // see header file for function documentation
394 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
395 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
396 strcat( output, ":" );
397 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
400 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
402 // see header file for function documentation
408 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
409 sprintf(tmp, "'%d", type.fOrigin[i]);
413 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
414 sprintf(tmp, "%d'", type.fID[i]);
423 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
424 unsigned char* puc=(unsigned char*)type.fOrigin;
426 sprintf(tmp, "'\\%x", type.fOrigin[i]);
428 sprintf(tmp, "'%c", type.fOrigin[i]);
432 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
433 unsigned char* puc=(unsigned char*)type.fID;
435 sprintf(tmp, "\\%x'", type.fID[i]);
437 sprintf(tmp, "%c'", type.fID[i]);
443 if (type==kAliHLTVoidDataType) {
446 // some gymnastics in order to avoid a '0' which is part of either or both
447 // ID and origin terminating the whole string. Unfortunately, string doesn't
448 // stop appending at the '0' if the number of elements to append was
449 // explicitely specified
451 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
452 out.append(tmp.c_str());
455 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
456 out.append(tmp.c_str());
462 void* AliHLTComponent::AllocMemory( unsigned long size )
464 // see header file for function documentation
465 if (fEnvironment.fAllocMemoryFunc)
466 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
467 HLTFatal("no memory allocation handler registered");
471 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
472 AliHLTComponentBlockData** outputBlocks )
474 // see header file for function documentation
475 if ( blockCount==NULL || outputBlocks==NULL )
477 AliHLTUInt32_t count = blocks.size();
481 *outputBlocks = NULL;
484 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
485 if ( !*outputBlocks )
487 for ( unsigned long i = 0; i < count; i++ ) {
488 (*outputBlocks)[i] = blocks[i];
489 if (MatchExactly(blocks[i].fDataType, kAliHLTAnyDataType)) {
490 (*outputBlocks)[i].fDataType=GetOutputDataType();
491 /* data type was set to the output data type by the PubSub AliRoot
492 Wrapper component, if data type of the block was ********:****.
493 Now handled by the component base class in order to have same
494 behavior when running embedded in AliRoot
495 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
496 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
505 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
507 // see header file for function documentation
508 if (fEnvironment.fGetEventDoneDataFunc)
509 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
513 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
515 // see header file for function documentation
518 AliHLTComponentDataTypeList itypes;
519 AliHLTComponentDataTypeList otypes;
520 otypes.push_back(GetOutputDataType());
521 if (otypes[0]==kAliHLTMultipleDataType) {
524 if ((count=GetOutputDataTypes(otypes))>0) {
525 } else if (GetComponentType()!=kSink) {
526 HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
529 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(itypes);
530 AliHLTComponentDataTypeList::iterator otype=otypes.begin();
531 for (;otype!=otypes.end();otype++) {
532 //PrintDataTypeContent((*otype), "publisher \'%s\'");
533 if ((*otype)==(kAliHLTAnyDataType|kAliHLTDataOriginPrivate)) {
534 if (tgtList) tgtList->push_back(*otype);
539 AliHLTComponentDataTypeList::iterator itype=itypes.begin();
540 for (;itype!=itypes.end() && (*itype)!=(*otype); itype++);
541 //if (itype!=itypes.end()) PrintDataTypeContent(*itype, "consumer \'%s\'");
542 if (itype!=itypes.end()) {
543 if (tgtList) tgtList->push_back(*otype);
553 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format)
555 // see header file for function documentation
556 const char* fmt="\'%s\'";
557 if (format) fmt=format;
558 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, Form(fmt, (DataType2Text(dt)).c_str()));
559 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL,
560 Form("%x %x %x %x %x %x %x %x : %x %x %x %x",
575 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
577 // see header file for function documentation
578 blockData.fStructSize = sizeof(blockData);
579 FillShmData( blockData.fShmKey );
580 blockData.fOffset = ~(AliHLTUInt32_t)0;
581 blockData.fPtr = NULL;
583 FillDataType( blockData.fDataType );
584 blockData.fSpecification = kAliHLTVoidDataSpec;
587 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
589 // see header file for function documentation
590 shmData.fStructSize = sizeof(shmData);
591 shmData.fShmType = gkAliHLTComponentInvalidShmType;
592 shmData.fShmID = gkAliHLTComponentInvalidShmID;
595 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
597 // see header file for function documentation
598 dataType=kAliHLTAnyDataType;
601 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
603 // see header file for function documentation
604 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
605 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
608 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
610 // see header file for function documentation
611 tgtdt.fStructSize=sizeof(AliHLTComponentDataType);
613 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
614 strncpy(&tgtdt.fID[0], id, strlen(id)<(size_t)kAliHLTComponentDataTypefIDsize?strlen(id):kAliHLTComponentDataTypefIDsize);
617 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
618 strncpy(&tgtdt.fOrigin[0], origin, strlen(origin)<(size_t)kAliHLTComponentDataTypefOriginSize?strlen(origin):kAliHLTComponentDataTypefOriginSize);
622 void AliHLTComponent::SetDataType(AliHLTComponentDataType& dt, AliHLTUInt64_t id, AliHLTUInt32_t origin)
624 // see header file for function documentation
625 dt.fStructSize=sizeof(AliHLTComponentDataType);
626 assert(kAliHLTComponentDataTypefIDsize==sizeof(id));
627 assert(kAliHLTComponentDataTypefOriginSize==sizeof(origin));
628 memcpy(&dt.fID, &id, kAliHLTComponentDataTypefIDsize);
629 memcpy(&dt.fOrigin, &origin, kAliHLTComponentDataTypefOriginSize);
632 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
634 // see header file for function documentation
635 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
636 evtData.fStructSize=sizeof(AliHLTComponentEventData);
637 evtData.fEventID=kAliHLTVoidEventID;
640 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
642 // see header file for function documentation
644 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
645 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
646 if (dt.fID[i]!=0) msg+=dt.fID[i];
650 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
651 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
655 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
658 int AliHLTComponent::GetEventCount() const
660 // see header file for function documentation
664 int AliHLTComponent::IncrementEventCounter()
666 // see header file for function documentation
667 if (fEventCount>=0) fEventCount++;
671 int AliHLTComponent::GetNumberOfInputBlocks() const
673 // see header file for function documentation
674 if (fpInputBlocks!=NULL) {
675 return fCurrentEventData.fBlockCnt;
680 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
681 const char* classname,
684 // see header file for function documentation
685 ALIHLTCOMPONENT_BASE_STOPWATCH();
687 if (classname) fClassName=classname;
688 else fClassName.clear();
689 int idx=FindInputBlock(fSearchDataType, 0, 1);
692 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
693 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
694 fCurrentInputBlock=idx;
701 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
702 const char* dtOrigin,
703 const char* classname,
706 // see header file for function documentation
707 ALIHLTCOMPONENT_BASE_STOPWATCH();
708 AliHLTComponentDataType dt;
709 SetDataType(dt, dtID, dtOrigin);
710 return GetFirstInputObject(dt, classname, bForce);
713 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
715 // see header file for function documentation
716 ALIHLTCOMPONENT_BASE_STOPWATCH();
717 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
718 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
721 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
722 fCurrentInputBlock=idx;
728 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
730 // see header file for function documentation
732 if (fpInputBlocks!=NULL) {
733 int idx=startIdx<0?0:startIdx;
734 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
735 if (dt!=fpInputBlocks[idx].fDataType) continue;
738 if (fpInputBlocks[idx].fPtr==NULL) continue;
739 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
740 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
748 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
750 // see header file for function documentation
752 if (fpInputBlocks!=NULL) {
753 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
754 if (fpInputBlocks[idx].fPtr) {
755 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
756 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
757 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
758 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
759 TClass* objclass=msg.GetClass();
760 pObj=msg.ReadObject(objclass);
761 if (pObj && objclass) {
762 HLTDebug("object %p type %s created", pObj, objclass->GetName());
766 } else if (bForce!=0) {
767 HLTError("size mismatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
770 HLTFatal("block descriptor empty");
773 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
776 HLTError("no input blocks available");
782 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
784 // see header file for function documentation
785 if (fpInputObjects==NULL) {
786 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
789 if (fpInputObjects) {
790 pObj=fpInputObjects->At(idx);
792 pObj=CreateInputObject(idx, bForce);
794 fpInputObjects->AddAt(pObj, idx);
798 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
803 int AliHLTComponent::CleanupInputObjects()
805 // see header file for function documentation
806 if (!fpInputObjects) return 0;
807 TObjArray* array=fpInputObjects;
809 for (int i=0; i<array->GetEntries(); i++) {
810 TObject* pObj=array->At(i);
811 // grrr, garbage collection strikes back: When read via AliHLTMessage
812 // (CreateInputObject), and written to a TFile afterwards, the
813 // TFile::Close calls ROOOT's garbage collection. No clue why the
814 // object ended up in the key list and needs to be deleted
815 if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
821 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
823 // see header file for function documentation
824 ALIHLTCOMPONENT_BASE_STOPWATCH();
825 AliHLTComponentDataType dt=kAliHLTVoidDataType;
826 int idx=fCurrentInputBlock;
828 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
830 HLTError("unknown object %p", pObject);
834 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
835 dt=fpInputBlocks[idx].fDataType;
837 HLTFatal("severe internal error, index out of range");
843 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
845 // see header file for function documentation
846 ALIHLTCOMPONENT_BASE_STOPWATCH();
847 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
848 int idx=fCurrentInputBlock;
850 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
852 HLTError("unknown object %p", pObject);
856 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
857 iSpec=fpInputBlocks[idx].fSpecification;
859 HLTFatal("severe internal error, index out of range");
865 int AliHLTComponent::Forward(const TObject* pObject)
867 // see header file for function documentation
869 int idx=fCurrentInputBlock;
871 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
873 HLTError("unknown object %p", pObject);
878 fOutputBlocks.push_back(fpInputBlocks[idx]);
883 int AliHLTComponent::Forward(const AliHLTComponentBlockData* pBlock)
885 // see header file for function documentation
887 int idx=fCurrentInputBlock;
889 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
891 HLTError("unknown Block %p", pBlock);
896 // check for fpInputBlocks pointer done in FindInputBlock
897 fOutputBlocks.push_back(fpInputBlocks[idx]);
902 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
904 // see header file for function documentation
905 ALIHLTCOMPONENT_BASE_STOPWATCH();
908 int idx=FindInputBlock(fSearchDataType, 0);
909 const AliHLTComponentBlockData* pBlock=NULL;
911 // check for fpInputBlocks pointer done in FindInputBlock
912 pBlock=&fpInputBlocks[idx];
913 fCurrentInputBlock=idx;
918 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
919 const char* dtOrigin)
921 // see header file for function documentation
922 ALIHLTCOMPONENT_BASE_STOPWATCH();
923 AliHLTComponentDataType dt;
924 SetDataType(dt, dtID, dtOrigin);
925 return GetFirstInputBlock(dt);
928 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index) const
930 // see header file for function documentation
931 ALIHLTCOMPONENT_BASE_STOPWATCH();
932 assert( 0 <= index and index < (int)fCurrentEventData.fBlockCnt );
933 return &fpInputBlocks[index];
936 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
938 // see header file for function documentation
939 ALIHLTCOMPONENT_BASE_STOPWATCH();
940 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
941 const AliHLTComponentBlockData* pBlock=NULL;
943 // check for fpInputBlocks pointer done in FindInputBlock
944 pBlock=&fpInputBlocks[idx];
945 fCurrentInputBlock=idx;
950 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
952 // see header file for function documentation
954 if (fpInputBlocks!=NULL) {
956 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
957 iResult=(int)(pBlock-fpInputBlocks);
966 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
968 // see header file for function documentation
969 ALIHLTCOMPONENT_BASE_STOPWATCH();
970 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
971 int idx=fCurrentInputBlock;
973 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
975 HLTError("unknown Block %p", pBlock);
979 // check for fpInputBlocks pointer done in FindInputBlock
980 iSpec=fpInputBlocks[idx].fSpecification;
985 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
986 void* pHeader, int headerSize)
988 // see header file for function documentation
989 ALIHLTCOMPONENT_BASE_STOPWATCH();
992 AliHLTMessage msg(kMESS_OBJECT);
993 msg.WriteObject(pObject);
994 Int_t iMsgLength=msg.Length();
996 msg.SetLength(); // sets the length to the first (reserved) word
997 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
999 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
1002 HLTError("object serialization failed for object %p", pObject);
1011 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1012 void* pHeader, int headerSize)
1014 // see header file for function documentation
1015 ALIHLTCOMPONENT_BASE_STOPWATCH();
1016 AliHLTComponentDataType dt;
1017 SetDataType(dt, dtID, dtOrigin);
1018 return PushBack(pObject, dt, spec, pHeader, headerSize);
1021 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1022 void* pHeader, int headerSize)
1024 // see header file for function documentation
1025 ALIHLTCOMPONENT_BASE_STOPWATCH();
1026 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
1029 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1030 void* pHeader, int headerSize)
1032 // see header file for function documentation
1033 ALIHLTCOMPONENT_BASE_STOPWATCH();
1034 AliHLTComponentDataType dt;
1035 SetDataType(dt, dtID, dtOrigin);
1036 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
1039 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1040 void* pHeader, int iHeaderSize)
1042 // see header file for function documentation
1044 int iBlkSize = iBufferSize + iHeaderSize;
1046 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
1047 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1048 AliHLTComponentBlockData bd;
1049 FillBlockData( bd );
1050 bd.fOffset = fOutputBufferFilled;
1051 bd.fSize = iBlkSize;
1053 bd.fSpecification = spec;
1054 if (pHeader!=NULL && pHeader!=pTgt) {
1055 memcpy(pTgt, pHeader, iHeaderSize);
1058 pTgt += (AliHLTUInt8_t) iHeaderSize;
1060 if (pBuffer!=NULL && pBuffer!=pTgt) {
1061 memcpy(pTgt, pBuffer, iBufferSize);
1063 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
1064 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
1066 fOutputBufferFilled+=bd.fSize;
1067 fOutputBlocks.push_back( bd );
1068 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
1070 if (fpOutputBuffer) {
1071 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
1073 HLTError("output buffer not available");
1083 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
1085 // see header file for function documentation
1086 if (!pObject) return -EINVAL;
1087 AliHLTMessage msg(kMESS_OBJECT);
1088 msg.WriteObject(pObject);
1089 return msg.Length();
1092 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
1093 const char* dtOrigin,
1094 AliHLTUInt32_t spec)
1096 // see header file for function documentation
1097 ALIHLTCOMPONENT_BASE_STOPWATCH();
1098 AliHLTComponentDataType dt;
1099 SetDataType(dt, dtID, dtOrigin);
1100 return CreateMemoryFile(capacity, dt, spec);
1103 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
1104 const AliHLTComponentDataType& dt,
1105 AliHLTUInt32_t spec)
1107 // see header file for function documentation
1108 ALIHLTCOMPONENT_BASE_STOPWATCH();
1109 AliHLTMemoryFile* pFile=NULL;
1110 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
1111 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1112 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
1114 unsigned int nofBlocks=fOutputBlocks.size();
1115 if (nofBlocks+1>fMemFiles.size()) {
1116 fMemFiles.resize(nofBlocks+1, NULL);
1118 if (nofBlocks<fMemFiles.size()) {
1119 fMemFiles[nofBlocks]=pFile;
1120 AliHLTComponentBlockData bd;
1121 FillBlockData( bd );
1122 bd.fOffset = fOutputBufferFilled;
1123 bd.fSize = capacity;
1125 bd.fSpecification = spec;
1126 fOutputBufferFilled+=bd.fSize;
1127 fOutputBlocks.push_back( bd );
1129 HLTError("can not allocate/grow object array");
1130 pFile->CloseMemoryFile(0);
1136 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
1141 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
1142 const char* dtOrigin,
1143 AliHLTUInt32_t spec,
1146 // see header file for function documentation
1147 ALIHLTCOMPONENT_BASE_STOPWATCH();
1148 AliHLTComponentDataType dt;
1149 SetDataType(dt, dtID, dtOrigin);
1150 int size=fOutputBufferSize-fOutputBufferFilled;
1151 if (capacity<0 || capacity>1.0) {
1152 HLTError("invalid parameter: capacity %f", capacity);
1155 size=(int)(size*capacity);
1156 return CreateMemoryFile(size, dt, spec);
1159 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
1160 AliHLTUInt32_t spec,
1163 // see header file for function documentation
1164 ALIHLTCOMPONENT_BASE_STOPWATCH();
1165 int size=fOutputBufferSize-fOutputBufferFilled;
1166 if (capacity<0 || capacity>1.0) {
1167 HLTError("invalid parameter: capacity %f", capacity);
1170 size=(int)(size*capacity);
1171 return CreateMemoryFile(size, dt, spec);
1174 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
1175 const char* key, int option)
1177 // see header file for function documentation
1179 if (pFile && pObject) {
1181 iResult=pObject->Write(key, option);
1185 iResult=-pFile->GetErrno();
1186 if (iResult==-ENOSPC) {
1187 HLTError("error writing memory file, buffer too small");
1196 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
1198 // see header file for function documentation
1201 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1203 while (element!=fMemFiles.end() && iResult>=0) {
1204 if (*element && *element==pFile) {
1205 iResult=pFile->CloseMemoryFile();
1207 // sync memory files and descriptors
1209 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
1217 HLTError("can not find memory file %p", pFile);
1225 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData /*edd*/)
1227 // see header file for function documentation
1228 int iResult=-ENOSYS;
1229 //#warning function not yet implemented
1230 HLTWarning("function not yet implemented");
1234 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1235 const AliHLTComponentBlockData* blocks,
1236 AliHLTComponentTriggerData& trigData,
1237 AliHLTUInt8_t* outputPtr,
1238 AliHLTUInt32_t& size,
1239 AliHLTUInt32_t& outputBlockCnt,
1240 AliHLTComponentBlockData*& outputBlocks,
1241 AliHLTComponentEventDoneData*& edd )
1243 // see header file for function documentation
1244 HLTLogKeyword(GetComponentID());
1245 ALIHLTCOMPONENT_BASE_STOPWATCH();
1247 fCurrentEvent=evtData.fEventID;
1248 fCurrentEventData=evtData;
1249 fpInputBlocks=blocks;
1250 fCurrentInputBlock=-1;
1251 fSearchDataType=kAliHLTAnyDataType;
1252 fpOutputBuffer=outputPtr;
1253 fOutputBufferSize=size;
1254 fOutputBufferFilled=0;
1255 fOutputBlocks.clear();
1258 AliHLTComponentStatisticsList compStats;
1259 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
1260 AliHLTComponentStatistics outputStat;
1261 memset(&outputStat, 0, sizeof(AliHLTComponentStatistics));
1262 outputStat.fId=fChainIdCrc;
1263 compStats.push_back(outputStat);
1265 fpBenchmark->Reset();
1266 fpBenchmark->Start();
1270 // data processing is skipped
1271 // - if there are only steering events in the block list.
1272 // For the sake of data source components data processing
1273 // is not skipped if there is no block list at all or if it
1274 // just contains the eventType block
1275 // - always skipped if the event is of type
1276 // - gkAliEventTypeConfiguration
1277 // - gkAliEventTypeReadPreprocessor
1278 const unsigned int skipModeDefault=0x1;
1279 const unsigned int skipModeForce=0x2;
1280 unsigned int bSkipDataProcessing=skipModeDefault;
1282 // find special events
1283 if (fpInputBlocks && evtData.fBlockCnt>0) {
1284 // first look for all special events and execute in the appropriate
1285 // sequence afterwords
1286 int indexComConfEvent=-1;
1287 int indexUpdtDCSEvent=-1;
1288 int indexSOREvent=-1;
1289 int indexEOREvent=-1;
1290 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1291 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1293 // the AliHLTCalibrationProcessor relies on the SOR and EOR events
1294 bSkipDataProcessing&=~skipModeDefault;
1295 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeRunType) {
1297 // handling is not clear yet
1298 if (fpInputBlocks[i].fPtr) {
1299 HLTDebug("got run type \"%s\"\n", fpInputBlocks[i].fPtr);
1301 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1303 // the calibration processor relies on the SOR and EOR events
1304 bSkipDataProcessing&=~skipModeDefault;
1305 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1307 // this event is most likely deprecated
1308 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComConf) {
1309 indexComConfEvent=i;
1310 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeUpdtDCS) {
1311 indexUpdtDCSEvent=i;
1312 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEvent) {
1313 fEventType=fpInputBlocks[i].fSpecification;
1315 // skip always in case of gkAliEventTypeConfiguration
1316 if (fpInputBlocks[i].fSpecification==gkAliEventTypeConfiguration) bSkipDataProcessing|=skipModeForce;
1318 // skip always in case of gkAliEventTypeReadPreprocessor
1319 if (fpInputBlocks[i].fSpecification==gkAliEventTypeReadPreprocessor) bSkipDataProcessing|=skipModeForce;
1321 // never skip if the event type block is the only block
1322 if (evtData.fBlockCnt==1) bSkipDataProcessing&=~skipModeDefault;
1324 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentStatistics) {
1325 if (compStats.size()>0) {
1326 AliHLTUInt8_t* pData=reinterpret_cast<AliHLTUInt8_t*>(fpInputBlocks[i].fPtr);
1327 for (AliHLTUInt32_t offset=0;
1328 offset+sizeof(AliHLTComponentStatistics)<=fpInputBlocks[i].fSize;
1329 offset+=sizeof(AliHLTComponentStatistics)) {
1330 AliHLTComponentStatistics* pStat=reinterpret_cast<AliHLTComponentStatistics*>(pData+offset);
1331 if (pStat && compStats[0].fLevel<=pStat->fLevel) {
1332 compStats[0].fLevel=pStat->fLevel+1;
1334 compStats.push_back(*pStat);
1338 // the processing function is called if there is at least one
1339 // non-steering data block. Steering blocks are not filtered out
1340 // for sake of performance
1341 bSkipDataProcessing&=~skipModeDefault;
1342 if (compStats.size()>0) {
1343 compStats[0].fInputBlockCount++;
1344 compStats[0].fTotalInputSize+=fpInputBlocks[i].fSize;
1349 if (indexSOREvent>=0) {
1351 if (fpRunDesc==NULL) {
1352 fpRunDesc=new AliHLTRunDesc;
1353 if (fpRunDesc) *fpRunDesc=kAliHLTVoidRunDesc;
1356 AliHLTRunDesc rundesc;
1357 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexSOREvent, "AliHLTRunDesc", "SOR"))>0) {
1358 if (fpRunDesc->fRunNo==kAliHLTVoidRunNo) {
1360 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1361 SetCDBRunNo(fpRunDesc->fRunNo);
1362 } else if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1363 HLTWarning("already set run properties run no %d, ignoring SOR with run no %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1370 if (indexEOREvent>=0) {
1371 if (fpRunDesc!=NULL) {
1373 AliHLTRunDesc rundesc;
1374 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexEOREvent, "AliHLTRunDesc", "SOR"))>0) {
1375 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1376 HLTWarning("run no mismatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1378 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1381 AliHLTRunDesc* pRunDesc=fpRunDesc;
1386 HLTWarning("did not receive SOR, ignoring EOR");
1389 if (indexComConfEvent>=0 || fEventType==gkAliEventTypeConfiguration) {
1391 if (indexComConfEvent>=0 && fpInputBlocks[indexComConfEvent].fPtr!=NULL && fpInputBlocks[indexComConfEvent].fSize>0) {
1392 cdbEntry.Append(reinterpret_cast<const char*>(fpInputBlocks[indexComConfEvent].fPtr), fpInputBlocks[indexComConfEvent].fSize);
1394 HLTDebug("received component configuration command: entry %s", cdbEntry.IsNull()?"none":cdbEntry.Data());
1395 int tmpResult=Reconfigure(cdbEntry[0]==0?NULL:cdbEntry.Data(), fChainId.c_str());
1397 HLTWarning("reconfiguration of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1400 if (indexUpdtDCSEvent>=0 || fEventType==gkAliEventTypeReadPreprocessor) {
1402 if (fpInputBlocks[indexUpdtDCSEvent].fPtr!=NULL && fpInputBlocks[indexUpdtDCSEvent].fSize>0) {
1403 modules.Append(reinterpret_cast<const char*>(fpInputBlocks[indexUpdtDCSEvent].fPtr), fpInputBlocks[indexUpdtDCSEvent].fSize);
1405 HLTDebug("received preprocessor update command: detectors %s", modules.IsNull()?"ALL":modules.Data());
1406 int tmpResult=ReadPreprocessorValues(modules[0]==0?"ALL":modules.Data());
1408 HLTWarning("preprocessor update of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1412 // processing function needs to be called if there are no input data
1413 // blocks in order to make data source components working.
1414 bSkipDataProcessing&=~skipModeDefault;
1417 // data processing is not skipped if the component explicitly asks
1418 // for the private blocks
1419 if (fRequireSteeringBlocks) bSkipDataProcessing=0;
1421 AliHLTComponentBlockDataList blockData;
1422 if (iResult>=0 && !bSkipDataProcessing)
1423 { // dont delete, sets the scope for the stopwatch guard
1424 // do not use ALIHLTCOMPONENT_DA_STOPWATCH(); macro
1425 // in order to avoid 'shadowed variable' warning
1426 AliHLTStopwatchGuard swguard2(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)kSWDA)):NULL);
1427 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1428 } // end of the scope of the stopwatch guard
1429 if (iResult>=0 && !bSkipDataProcessing) {
1430 if (fOutputBlocks.size()>0) {
1431 // High Level interface
1433 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1434 // sync memory files and descriptors
1435 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1437 while (element!=fMemFiles.end() && iResult>=0) {
1439 if ((*element)->IsClosed()==0) {
1440 HLTWarning("memory file has not been closed, force flush");
1441 iResult=CloseMemoryFile(*element);
1448 // create the descriptor list
1449 if (blockData.size()>0) {
1450 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1453 if (compStats.size()>0) {
1454 int offset=AddComponentStatistics(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, compStats);
1455 if (offset>0) fOutputBufferFilled+=offset;
1457 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1458 size=fOutputBufferFilled;
1462 // Low Level interface
1463 if (compStats.size()>0) {
1464 int offset=AddComponentStatistics(blockData, fpOutputBuffer, fOutputBufferSize, size, compStats);
1465 if (offset>0) size+=offset;
1467 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1470 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1473 if (iResult<0 || bSkipDataProcessing) {
1477 CleanupInputObjects();
1478 if (iResult>=0 && IsDataEvent()) {
1479 IncrementEventCounter();
1481 if (outputBlockCnt==0) {
1482 // no output blocks, set size to 0
1488 int AliHLTComponent::AddComponentStatistics(AliHLTComponentBlockDataList& blocks,
1489 AliHLTUInt8_t* buffer,
1490 AliHLTUInt32_t bufferSize,
1491 AliHLTUInt32_t offset,
1492 AliHLTComponentStatisticsList& stats) const
1494 // see header file for function documentation
1496 if (stats.size()==0) return -ENOENT;
1497 stats[0].fTotalOutputSize=offset;
1498 stats[0].fOutputBlockCount=blocks.size();
1500 stats[0].fTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*1000000);
1501 stats[0].fCTime=(AliHLTUInt32_t)(fpBenchmark->CpuTime()*1000000);
1503 if (offset+stats.size()*sizeof(AliHLTComponentStatistics)<=bufferSize) {
1504 AliHLTComponentBlockData bd;
1505 FillBlockData( bd );
1506 bd.fOffset = offset;
1507 bd.fSize = stats.size()*sizeof(AliHLTComponentStatistics);
1508 bd.fDataType = kAliHLTDataTypeComponentStatistics;
1509 bd.fSpecification = kAliHLTVoidDataSpec;
1510 unsigned int master=0;
1511 for (unsigned int i=1; i<blocks.size(); i++) {
1512 if (blocks[i].fSize>blocks[master].fSize &&
1513 !MatchExactly(blocks[i].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate))
1516 if (blocks.size()>0 && !MatchExactly(blocks[master].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate)) {
1517 // take the data origin of the biggest block as specification
1518 // this is similar to the treatment in the HOMER interface. For traditional
1519 // reasons, the bytes are swapped there on a little endian architecture, so
1520 // we do it as well.
1521 memcpy(&bd.fSpecification, &blocks[master].fDataType.fOrigin, sizeof(bd.fSpecification));
1522 #ifdef R__BYTESWAP // set on little endian architectures
1523 bd.fSpecification=((bd.fSpecification & 0xFFULL) << 24) |
1524 ((bd.fSpecification & 0xFF00ULL) << 8) |
1525 ((bd.fSpecification & 0xFF0000ULL) >> 8) |
1526 ((bd.fSpecification & 0xFF000000ULL) >> 24);
1529 memcpy(buffer+offset, &(stats[0]), bd.fSize);
1530 blocks.push_back(bd);
1536 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1541 // standard constructor (not for use)
1544 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1546 fpStopwatch(pStopwatch),
1551 // check for already existing guard
1552 if (fgpCurrent) fpPrec=fgpCurrent;
1555 // stop the preceeding guard if it controls a different stopwatch
1557 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1559 // start the stopwatch if the current guard controls a different one
1560 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1563 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
1569 // copy constructor not for use
1573 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1576 // assignment operator not for use
1583 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1585 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1589 // resume the preceeding guard if it controls a different stopwatch
1591 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1593 // stop the stopwatch if the current guard controls a different one
1594 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1596 // resume to the preceeding guard
1600 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1602 // see header file for function documentation
1603 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1604 return fpStopwatch!=pSucc?1:0;
1607 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1609 // see header file for function documentation
1610 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1611 return fpStopwatch!=pSucc?1:0;
1614 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
1616 // see header file for function documentation
1618 if (pSW!=NULL && type<kSWTypeCount) {
1619 if (fpStopwatches) {
1620 TObject* pObj=fpStopwatches->At((int)type);
1621 if (pSW==NULL // explicit reset
1622 || pObj==NULL) { // explicit set
1623 fpStopwatches->AddAt(pSW, (int)type);
1624 } else if (pObj!=pSW) {
1625 HLTWarning("stopwatch %d already set, reset first", (int)type);
1635 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1637 // see header file for function documentation
1638 if (pStopwatches==NULL) return -EINVAL;
1641 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1642 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1646 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1648 // see header file for function documentation
1649 if (fpRunDesc==NULL) return kAliHLTVoidRunNo;
1650 return fpRunDesc->fRunNo;
1653 AliHLTUInt32_t AliHLTComponent::GetRunType() const
1655 // see header file for function documentation
1656 if (fpRunDesc==NULL) return kAliHLTVoidRunType;
1657 return fpRunDesc->fRunType;
1660 bool AliHLTComponent::IsDataEvent(AliHLTUInt32_t* pTgt)
1662 // see header file for function documentation
1663 if (pTgt) *pTgt=fEventType;
1664 return (fEventType==gkAliEventTypeData ||
1665 fEventType==gkAliEventTypeDataReplay ||
1666 fEventType==gkAliEventTypeCalibration);
1669 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
1670 const char* structname, const char* eventname)
1672 // see header file for function documentation
1674 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1675 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1676 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1677 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
1678 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
1679 if (fpInputBlocks[iBlockNo].fSize!=copy) {
1680 HLTWarning("%s event: mismatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1681 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1683 if (copy!=iStructSize) {
1684 HLTWarning("%s event: mismatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1685 if (copy>iStructSize) {
1688 memset(pTgt, 0, iStructSize);
1691 memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
1695 HLTWarning("%s event: missing data block", eventname);
1701 HLTError("invalid struct");
1707 void AliHLTComponent::SetDDLBit(AliHLTEventDDL &list, Int_t ddlId, Bool_t state ) const
1709 // see header file for function documentation
1711 // -- Detector offset
1712 Int_t ddlIdBase = TMath::FloorNint( (Double_t) ddlId / 256.0 );
1714 // -- Word Base = 1. word of detector ( TPC has 8 words, TOF 3 )
1717 if ( ddlIdBase <= 3 )
1718 wordBase = ddlIdBase;
1719 else if ( ddlIdBase > 3 && ddlIdBase < 5 )
1720 wordBase = ddlIdBase + 7;
1722 wordBase = ddlIdBase + 9;
1724 // -- Bit index in Word
1725 Int_t bitIdx = ddlId % 32;
1728 Int_t wordIdx = wordBase;
1730 // -- if TPC (3) or TOD (5) add word idx
1731 if ( ( ddlIdBase == 3 ) || ( ddlIdBase == 5 ) ) {
1732 wordIdx += TMath::FloorNint( (Double_t) ( ddlId - ( ddlIdBase * 256 ) ) / 32.0 );
1735 // -- Set -- 'OR' word with bit mask;
1737 list.fList[wordIdx] |= ( 0x00000001 << bitIdx );
1738 // -- Unset -- 'AND' word with bit mask;
1740 list.fList[wordIdx] &= ( 0xFFFFFFFF ^ ( 0x00000001 << bitIdx ) );
1743 Int_t AliHLTComponent::GetFirstUsedDDLWord(AliHLTEventDDL &list) const
1745 // see header file for function documentation
1749 for ( Int_t wordNdx = 0 ; wordNdx < gkAliHLTDDLListSize ; wordNdx++ ) {
1751 if ( list.fList[wordNdx] != 0 && iResult == -1 ) {
1752 // check for special cases TPC and TOF
1753 if ( wordNdx > 3 && wordNdx <= 10 ) {
1757 else if ( wordNdx > 12 && wordNdx <= 14 ) {
1764 else if ( list.fList[wordNdx] != 0 && iResult >= 0 ) {
1765 HLTError( "DDLIDs for minimum of TWO detectors ( %d, %d ) set, this function works only for ONE detector.", iResult, wordNdx );
1774 AliHLTUInt32_t AliHLTComponent::CalculateChecksum(const AliHLTUInt8_t* buffer, int size)
1776 // see header file for function documentation
1777 AliHLTUInt32_t remainder = 0;
1778 const AliHLTUInt8_t crcwidth=(8*sizeof(AliHLTUInt32_t));
1779 const AliHLTUInt32_t topbit=1 << (crcwidth-1);
1780 const AliHLTUInt32_t polynomial=0xD8; /* 11011 followed by 0's */
1783 // http://www.netrino.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
1786 * Perform modulo-2 division, a byte at a time.
1788 for (int byte = 0; byte < size; ++byte)
1791 * Bring the next byte into the remainder.
1793 remainder ^= (buffer[byte] << (crcwidth - 8));
1796 * Perform modulo-2 division, a bit at a time.
1798 for (uint8_t bit = 8; bit > 0; --bit)
1801 * Try to divide the current data bit.
1803 if (remainder & topbit)
1805 remainder = (remainder << 1) ^ polynomial;
1809 remainder = (remainder << 1);
1815 * The final remainder is the CRC result.