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. */
24 // @note The class is both used in Online (PubSub) and Offline (AliRoot)
32 //#include "AliHLTStdIncludes.h"
33 #include "AliHLTComponent.h"
34 #include "AliHLTComponentHandler.h"
35 #include "AliHLTMessage.h"
36 #include "AliHLTCTPData.h"
39 #include "TObjArray.h"
40 #include "TObjectTable.h"
42 #include "TStopwatch.h"
44 #include "AliHLTMemoryFile.h"
45 #include "AliHLTMisc.h"
51 * default compression level for ROOT objects
53 #define ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION 5
54 #define ALIHLTCOMPONENT_STATTIME_SCALER 1000000
56 /** ROOT macro for the implementation of ROOT specific class methods */
57 ClassImp(AliHLTComponent);
59 /** stopwatch macro using the stopwatch guard */
60 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
61 //#define ALIHLTCOMPONENT_STOPWATCH(type)
63 /** stopwatch macro for operations of the base class */
64 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
65 /** stopwatch macro for operations of the detector algorithm (DA) */
66 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
68 AliHLTComponent::AliHLTComponent()
76 fCurrentInputBlock(-1),
77 fSearchDataType(kAliHLTVoidDataType),
82 fOutputBufferFilled(0),
84 fpStopwatches(new TObjArray(kSWTypeCount)),
88 fCDBSetRunNoFunc(false),
93 fEventType(gkAliEventTypeUnknown),
96 fEventDoneDataSize(0),
97 fCompressionLevel(ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION)
101 , fLastPushBackTime(-1)
103 // see header file for class documentation
105 // refer to README to build package
107 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
108 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
109 if (fgpComponentHandler)
110 fgpComponentHandler->ScheduleRegister(this);
111 //SetLocalLoggingLevel(kHLTLogDefault);
114 AliHLTComponent::~AliHLTComponent()
116 // see header file for function documentation
117 if (fpBenchmark) delete fpBenchmark;
120 CleanupInputObjects();
121 if (fpStopwatches!=NULL) delete fpStopwatches;
123 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
124 while (element!=fMemFiles.end()) {
126 if ((*element)->IsClosed()==0) {
127 HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
128 // close but do not flush as we dont know whether the buffer is still valid
129 (*element)->CloseMemoryFile(0);
141 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
150 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
152 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
154 // see header file for function documentation
156 if (fgpComponentHandler==NULL || bOverwrite!=0)
157 fgpComponentHandler=pCH;
163 int AliHLTComponent::UnsetGlobalComponentHandler()
165 // see header file for function documentation
166 return SetGlobalComponentHandler(NULL,1);
169 int AliHLTComponent::SetComponentEnvironment(const AliHLTAnalysisEnvironment* comenv, void* environParam)
171 // see header file for function documentation
172 HLTLogKeyword(fChainId.c_str());
175 memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
176 memcpy(&fEnvironment, comenv, comenv->fStructSize<sizeof(AliHLTAnalysisEnvironment)?comenv->fStructSize:sizeof(AliHLTAnalysisEnvironment));
177 fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
178 fEnvironment.fParam=environParam;
183 int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environParam, int argc, const char** argv )
185 // see header file for function documentation
186 HLTLogKeyword(fChainId.c_str());
189 SetComponentEnvironment(comenv, environParam);
192 fLastPushBackTime=-1;
195 const char** pArguments=NULL;
200 pArguments=new const char*[argc];
202 for (int i=0; i<argc && iResult>=0; i++) {
203 if (fComponentArgs.size()>0) fComponentArgs+=" ";
204 fComponentArgs+=argv[i];
206 if (argument.IsNull()) continue;
209 if (argument.CompareTo("-benchmark")==0) {
212 } else if (argument.BeginsWith("-loglevel=")) {
213 TString parameter=argument.ReplaceAll("-loglevel=", "");
214 parameter.Remove(TString::kLeading, ' '); // remove all blanks
215 if (parameter.BeginsWith("0x") &&
216 parameter.Replace(0,2,"",0).IsHex()) {
217 unsigned int loglevel=kHLTLogNone;
218 sscanf(parameter.Data(),"%x", &loglevel);
219 SetLocalLoggingLevel((AliHLTComponentLogSeverity)loglevel);
221 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
224 // -object-compression=
225 } else if (argument.BeginsWith("-object-compression=")) {
226 argument.ReplaceAll("-object-compression=", "");
227 if (argument.IsDigit()) {
228 fCompressionLevel=argument.Atoi();
229 if (fCompressionLevel<0 || fCompressionLevel>9) {
230 HLTWarning("invalid compression level %d, setting to default %d", fCompressionLevel, ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION);
231 fCompressionLevel=ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION;
234 HLTError("wrong parameter for argument -object-compression, number expected");
237 } else if (argument.BeginsWith("-pushback-period=")) {
238 argument.ReplaceAll("-pushback-period=", "");
239 if (argument.IsDigit()) {
240 fPushbackPeriod=argument.Atoi();
242 HLTError("wrong parameter for argument -pushback-period, number expected");
244 // -disable-component-stat
245 } else if (argument.CompareTo("-disable-component-stat")==0) {
246 fFlags|=kDisableComponentStat;
248 pArguments[iNofChildArgs++]=argv[i];
256 HLTError("missing parameter for argument %s", argument.Data());
260 iResult=DoInit(iNofChildArgs, pArguments);
265 // find out if the component wants to get the steering events
267 AliHLTComponentDataTypeList inputDt;
268 GetInputDataTypes(inputDt);
269 bool bRequireSteeringBlocks=false;
270 for (AliHLTComponentDataTypeList::iterator dt=inputDt.begin();
271 dt!=inputDt.end() && !bRequireSteeringBlocks;
273 bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeSOR);
274 bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeRunType);
275 bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeEOR);
276 bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeDDL);
277 bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeComponentStatistics);
279 if (bRequireSteeringBlocks) fFlags|=kRequireSteeringBlocks;
281 if (pArguments) delete [] pArguments;
283 #if defined(HLT_COMPONENT_STATISTICS)
284 // benchmarking stopwatch for the component statistics
285 fpBenchmark=new TStopwatch;
286 #endif // HLT_COMPONENT_STATISTICS
291 int AliHLTComponent::Deinit()
293 // see header file for function documentation
294 HLTLogKeyword(fChainId.c_str());
298 // TODO: the warning should be kept, but the condition is wrong since the
299 // AliHLTRunDesc is set before the SOR event in the SetRunDescription
300 // method. A couple of state flags should be defined but that is a bit more
301 // work to do. For the moment disable the warning (2009-07-01)
302 // 2009-09-08: now, the info is not cleared in the ProcessEvent, because it
303 // might be needed by components during the event processing.
304 //HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
305 AliHLTRunDesc* pRunDesc=fpRunDesc;
319 int AliHLTComponent::InitCDB(const char* cdbPath, AliHLTComponentHandler* pHandler)
321 // see header file for function documentation
323 HLTInfo("Using CDB: %s", cdbPath);
325 // I have to think about separating the library handling from the
326 // component handler. Requiring the component handler here is not
327 // the cleanest solution.
328 // We presume the library already to be loaded, which is the case
329 // because it is loaded in the initialization of the logging functionality
332 AliHLTMiscInitCDB_t pFunc=(AliHLTMiscInitCDB_t)pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_INIT_CDB);
335 if (cdbPath && cdbPath[0]!=0) {
337 // very temporary fix, have to check for other formats
338 if (!path.BeginsWith("local://")) {
343 if ((iResult=(*pFunc)(path.Data()))>=0) {
344 if (!(fCDBSetRunNoFunc=pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_SET_CDB_RUNNO))) {
345 Message(NULL, kHLTLogWarning, "AliHLTComponent::InitCDB", "init CDB",
346 "can not find function to set CDB run no");
350 Message(NULL, kHLTLogError, "AliHLTComponent::InitCDB", "init CDB",
351 "can not find initialization function");
360 int AliHLTComponent::SetCDBRunNo(int runNo)
362 // see header file for function documentation
363 if (!fCDBSetRunNoFunc) return 0;
364 return (*((AliHLTMiscSetCDBRunNo_t)fCDBSetRunNoFunc))(runNo);
367 int AliHLTComponent::SetRunDescription(const AliHLTRunDesc* desc, const char* /*runType*/)
369 // see header file for function documentation
370 if (!desc) return -EINVAL;
371 if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
372 HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
377 fpRunDesc=new AliHLTRunDesc;
378 if (!fpRunDesc) return -ENOMEM;
379 *fpRunDesc=kAliHLTVoidRunDesc;
382 if (fpRunDesc->fRunNo!=kAliHLTVoidRunNo && fpRunDesc->fRunNo!=desc->fRunNo) {
383 HLTWarning("Run description has already been set");
386 SetCDBRunNo(fpRunDesc->fRunNo);
387 // TODO: we have to decide about the runType
391 int AliHLTComponent::SetComponentDescription(const char* desc)
393 // see header file for function documentation
397 TString descriptor=desc;
398 TObjArray* pTokens=descriptor.Tokenize(" ");
400 for (int i=0; i<pTokens->GetEntriesFast() && iResult>=0; i++) {
401 TString argument=((TObjString*)pTokens->At(i++))->GetString();
402 if (!argument || argument.IsNull()) continue;
405 if (argument.BeginsWith("chainid")) {
406 argument.ReplaceAll("chainid", "");
407 if (argument.BeginsWith("=")) {
408 fChainId=argument.Replace(0,1,"");
409 fChainIdCrc=CalculateChecksum((const AliHLTUInt8_t*)fChainId.c_str(), fChainId.length());
410 HLTDebug("setting component description: chain id %s crc 0x%8x", fChainId.c_str(), fChainIdCrc);
415 HLTWarning("unknown component description %s", argument.Data());
424 int AliHLTComponent::ConfigureFromArgumentString(int argc, const char** argv)
426 // see header file for function documentation
428 vector<const char*> array;
429 TObjArray choppedArguments;
432 for (i=0; i<argc && iResult>=0; i++) {
434 if (argument.IsNull()) continue;
435 TObjArray* pTokens=argument.Tokenize(" ");
437 if (pTokens->GetEntriesFast()>0) {
438 for (int n=0; n<pTokens->GetEntriesFast(); n++) {
439 choppedArguments.AddLast(pTokens->At(n));
440 TString data=((TObjString*)pTokens->At(n))->GetString();
441 if (!data.IsNull()) {
442 array.push_back(data.Data());
445 pTokens->SetOwner(kFALSE);
451 for (i=0; (unsigned)i<array.size() && iResult>=0;) {
452 int result=ScanConfigurationArgument(array.size()-i, &array[i]);
454 HLTWarning("unknown component argument %s", array[i]);
456 } else if (result>0) {
460 if (iResult==-EINVAL) {
461 HLTError("unknown argument %s", array[i]);
462 } else if (iResult==-EPROTO) {
463 HLTError("missing/wrong parameter for argument %s (%s)", array[i], (array.size()>(unsigned)i+1)?array[i+1]:"missing");
465 HLTError("scan of argument %s failed (%d)", array[i], iResult);
473 int AliHLTComponent::ConfigureFromCDBTObjString(const char* entries)
475 // see header file for function documentation
478 TString confEntries=entries;
479 TObjArray* pTokens=confEntries.Tokenize(" ");
481 for (int n=0; n<pTokens->GetEntriesFast(); n++) {
482 const char* path=((TObjString*)pTokens->At(n))->GetString().Data();
483 const char* chainId=GetChainId();
484 HLTInfo("configure from entry %s, chain id %s", path, (chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
485 TObject* pOCDBObject = LoadAndExtractOCDBObject(path);
487 TObjString* pString=dynamic_cast<TObjString*>(pOCDBObject);
489 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
490 arguments+=pString->GetString().Data();
493 HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
497 HLTError("can not fetch object \"%s\" from OCDB", path);
503 if (iResult>=0 && !arguments.IsNull()) {
504 const char* array=arguments.Data();
505 iResult=ConfigureFromArgumentString(1, &array);
510 TObject* AliHLTComponent::LoadAndExtractOCDBObject(const char* path, int version, int subVersion)
512 // see header file for function documentation
513 AliCDBEntry* pEntry=AliHLTMisc::Instance().LoadOCDBEntry(path, GetRunNo(), version, subVersion);
514 if (!pEntry) return NULL;
515 return AliHLTMisc::Instance().ExtractObject(pEntry);
518 int AliHLTComponent::DoInit( int /*argc*/, const char** /*argv*/)
520 // default implementation, childs can overload
521 HLTLogKeyword("dummy");
525 int AliHLTComponent::DoDeinit()
527 // default implementation, childs can overload
528 HLTLogKeyword("dummy");
532 int AliHLTComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/)
534 // default implementation, childs can overload
535 HLTLogKeyword("dummy");
539 int AliHLTComponent::ReadPreprocessorValues(const char* /*modules*/)
541 // default implementation, childs can overload
542 HLTLogKeyword("dummy");
546 int AliHLTComponent::ScanConfigurationArgument(int /*argc*/, const char** /*argv*/)
548 // default implementation, childs can overload
549 HLTLogKeyword("dummy");
550 HLTWarning("The function needs to be implemented by the component");
554 int AliHLTComponent::StartOfRun()
556 // default implementation, childs can overload
557 HLTLogKeyword("dummy");
561 int AliHLTComponent::EndOfRun()
563 // default implementation, childs can overload
564 HLTLogKeyword("dummy");
569 int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
571 // default implementation, childs can overload
572 HLTLogKeyword("dummy");
576 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
578 // see header file for function documentation
579 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
580 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
581 strcat( output, ":" );
582 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
585 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
587 // see header file for function documentation
593 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
594 sprintf(tmp, "'%d", type.fOrigin[i]);
598 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
599 sprintf(tmp, "%d'", type.fID[i]);
608 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
609 unsigned char* puc=(unsigned char*)type.fOrigin;
611 sprintf(tmp, "'\\%x", type.fOrigin[i]);
613 sprintf(tmp, "'%c", type.fOrigin[i]);
617 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
618 unsigned char* puc=(unsigned char*)type.fID;
620 sprintf(tmp, "\\%x'", type.fID[i]);
622 sprintf(tmp, "%c'", type.fID[i]);
628 if (type==kAliHLTVoidDataType) {
631 // some gymnastics in order to avoid a '0' which is part of either or both
632 // ID and origin terminating the whole string. Unfortunately, string doesn't
633 // stop appending at the '0' if the number of elements to append was
634 // explicitely specified
636 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
637 out.append(tmp.c_str());
640 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
641 out.append(tmp.c_str());
647 void* AliHLTComponent::AllocMemory( unsigned long size )
649 // see header file for function documentation
650 if (fEnvironment.fAllocMemoryFunc)
651 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
652 HLTFatal("no memory allocation handler registered");
656 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
657 AliHLTComponentBlockData** outputBlocks )
659 // see header file for function documentation
660 if ( blockCount==NULL || outputBlocks==NULL )
662 AliHLTUInt32_t count = blocks.size();
666 *outputBlocks = NULL;
669 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
670 if ( !*outputBlocks )
672 for ( unsigned long i = 0; i < count; i++ ) {
673 (*outputBlocks)[i] = blocks[i];
674 if (MatchExactly(blocks[i].fDataType, kAliHLTAnyDataType)) {
675 (*outputBlocks)[i].fDataType=GetOutputDataType();
676 /* data type was set to the output data type by the PubSub AliRoot
677 Wrapper component, if data type of the block was ********:****.
678 Now handled by the component base class in order to have same
679 behavior when running embedded in AliRoot
680 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
681 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
690 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd ) const
692 // see header file for function documentation
693 if (fEnvironment.fGetEventDoneDataFunc)
694 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
698 int AliHLTComponent::ReserveEventDoneData( unsigned long size )
700 // see header file for function documentation
703 unsigned long capacity=fEventDoneDataSize;
704 if (fEventDoneData) capacity-=sizeof(AliHLTComponentEventDoneData)+fEventDoneData->fDataSize;
706 unsigned long newSize=sizeof(AliHLTComponentEventDoneData)+size+(fEventDoneDataSize-capacity);
707 AliHLTComponentEventDoneData* newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[newSize] );
710 newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
711 newEDD->fDataSize = 0;
712 newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
713 if (fEventDoneData) {
714 memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
715 newEDD->fDataSize = fEventDoneData->fDataSize;
716 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
718 fEventDoneData = newEDD;
719 fEventDoneDataSize = newSize;
725 int AliHLTComponent::PushEventDoneData( AliHLTUInt32_t eddDataWord )
727 // see header file for function documentation
730 if (fEventDoneData->fDataSize+sizeof(AliHLTUInt32_t)>fEventDoneDataSize)
732 *reinterpret_cast<AliHLTUInt32_t*>((reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize)) = eddDataWord;
733 fEventDoneData->fDataSize += sizeof(AliHLTUInt32_t);
737 void AliHLTComponent::ReleaseEventDoneData()
739 // see header file for function documentation
741 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
742 fEventDoneData = NULL;
743 fEventDoneDataSize = 0;
747 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
749 // see header file for function documentation
752 AliHLTComponentDataTypeList itypes;
753 AliHLTComponentDataTypeList otypes;
754 otypes.push_back(GetOutputDataType());
755 if (MatchExactly(otypes[0],kAliHLTMultipleDataType)) {
758 if ((count=GetOutputDataTypes(otypes))>0) {
759 } else if (GetComponentType()!=kSink) {
760 HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
763 pConsumer->GetInputDataTypes(itypes);
764 AliHLTComponentDataTypeList::iterator otype=otypes.begin();
765 for (;otype!=otypes.end();otype++) {
766 //PrintDataTypeContent((*otype), "publisher \'%s\'");
767 if ((*otype)==(kAliHLTAnyDataType|kAliHLTDataOriginPrivate)) {
768 if (tgtList) tgtList->push_back(*otype);
773 AliHLTComponentDataTypeList::iterator itype=itypes.begin();
774 for ( ; itype!=itypes.end() && (*itype)!=(*otype) ; itype++) {/* empty body */};
775 //if (itype!=itypes.end()) PrintDataTypeContent(*itype, "consumer \'%s\'");
776 if (itype!=itypes.end()) {
777 if (tgtList) tgtList->push_back(*otype);
787 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format)
789 // see header file for function documentation
790 const char* fmt="\'%s\'";
791 if (format) fmt=format;
792 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, Form(fmt, (DataType2Text(dt)).c_str()));
793 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL,
794 Form("%x %x %x %x %x %x %x %x : %x %x %x %x",
809 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
811 // see header file for function documentation
812 blockData.fStructSize = sizeof(blockData);
813 FillShmData( blockData.fShmKey );
814 blockData.fOffset = ~(AliHLTUInt32_t)0;
815 blockData.fPtr = NULL;
817 FillDataType( blockData.fDataType );
818 blockData.fSpecification = kAliHLTVoidDataSpec;
821 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
823 // see header file for function documentation
824 shmData.fStructSize = sizeof(shmData);
825 shmData.fShmType = gkAliHLTComponentInvalidShmType;
826 shmData.fShmID = gkAliHLTComponentInvalidShmID;
829 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
831 // see header file for function documentation
832 dataType=kAliHLTAnyDataType;
835 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
837 // see header file for function documentation
838 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
839 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
842 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
844 // see header file for function documentation
845 tgtdt.fStructSize=sizeof(AliHLTComponentDataType);
847 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
848 strncpy(&tgtdt.fID[0], id, strlen(id)<(size_t)kAliHLTComponentDataTypefIDsize?strlen(id):kAliHLTComponentDataTypefIDsize);
851 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
852 strncpy(&tgtdt.fOrigin[0], origin, strlen(origin)<(size_t)kAliHLTComponentDataTypefOriginSize?strlen(origin):kAliHLTComponentDataTypefOriginSize);
856 void AliHLTComponent::SetDataType(AliHLTComponentDataType& dt, AliHLTUInt64_t id, AliHLTUInt32_t origin)
858 // see header file for function documentation
859 dt.fStructSize=sizeof(AliHLTComponentDataType);
860 assert(kAliHLTComponentDataTypefIDsize==sizeof(id));
861 assert(kAliHLTComponentDataTypefOriginSize==sizeof(origin));
862 memcpy(&dt.fID, &id, kAliHLTComponentDataTypefIDsize);
863 memcpy(&dt.fOrigin, &origin, kAliHLTComponentDataTypefOriginSize);
866 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
868 // see header file for function documentation
869 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
870 evtData.fStructSize=sizeof(AliHLTComponentEventData);
871 evtData.fEventID=kAliHLTVoidEventID;
874 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
876 // see header file for function documentation
878 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
879 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
880 if (dt.fID[i]!=0) msg+=dt.fID[i];
884 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
885 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
889 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
892 int AliHLTComponent::GetEventCount() const
894 // see header file for function documentation
898 int AliHLTComponent::IncrementEventCounter()
900 // see header file for function documentation
901 if (fEventCount>=0) fEventCount++;
905 int AliHLTComponent::GetNumberOfInputBlocks() const
907 // see header file for function documentation
908 if (fpInputBlocks!=NULL) {
909 return fCurrentEventData.fBlockCnt;
914 AliHLTEventID_t AliHLTComponent::GetEventId() const
916 // see header file for function documentation
917 if (fpInputBlocks!=NULL) {
918 return fCurrentEventData.fEventID;
923 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
924 const char* classname,
927 // see header file for function documentation
928 ALIHLTCOMPONENT_BASE_STOPWATCH();
930 if (classname) fClassName=classname;
931 else fClassName.clear();
932 int idx=FindInputBlock(fSearchDataType, 0, 1);
935 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
936 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
937 fCurrentInputBlock=idx;
944 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
945 const char* dtOrigin,
946 const char* classname,
949 // see header file for function documentation
950 ALIHLTCOMPONENT_BASE_STOPWATCH();
951 AliHLTComponentDataType dt;
952 SetDataType(dt, dtID, dtOrigin);
953 return GetFirstInputObject(dt, classname, bForce);
956 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
958 // see header file for function documentation
959 ALIHLTCOMPONENT_BASE_STOPWATCH();
960 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
961 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
964 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
965 fCurrentInputBlock=idx;
971 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
973 // see header file for function documentation
975 if (fpInputBlocks!=NULL) {
976 int idx=startIdx<0?0:startIdx;
977 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
978 if (dt!=fpInputBlocks[idx].fDataType) continue;
981 if (fpInputBlocks[idx].fPtr==NULL) continue;
982 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
983 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
991 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
993 // see header file for function documentation
995 if (fpInputBlocks!=NULL) {
996 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
997 if (fpInputBlocks[idx].fPtr) {
998 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
999 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
1000 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
1001 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
1002 TClass* objclass=msg.GetClass();
1003 pObj=msg.ReadObject(objclass);
1004 if (pObj && objclass) {
1005 HLTDebug("object %p type %s created", pObj, objclass->GetName());
1009 } else if (bForce!=0) {
1010 HLTError("size mismatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
1013 HLTFatal("block descriptor empty");
1016 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
1019 HLTError("no input blocks available");
1025 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
1027 // see header file for function documentation
1028 if (fpInputObjects==NULL) {
1029 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
1032 if (fpInputObjects) {
1033 pObj=fpInputObjects->At(idx);
1035 pObj=CreateInputObject(idx, bForce);
1037 fpInputObjects->AddAt(pObj, idx);
1041 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
1046 int AliHLTComponent::CleanupInputObjects()
1048 // see header file for function documentation
1049 if (!fpInputObjects) return 0;
1050 TObjArray* array=fpInputObjects;
1051 fpInputObjects=NULL;
1052 for (int i=0; i<array->GetEntriesFast(); i++) {
1053 TObject* pObj=array->At(i);
1054 // grrr, garbage collection strikes back: When read via AliHLTMessage
1055 // (CreateInputObject), and written to a TFile afterwards, the
1056 // TFile::Close calls ROOOT's garbage collection. No clue why the
1057 // object ended up in the key list and needs to be deleted
1059 // Matthias 09.11.2008 follow up
1060 // This approach doesn't actually work in all cases: the object table
1061 // can be switched off globally, the flag needs to be checked here as
1062 // well in order to avoid memory leaks.
1063 // This means we have to find another solution for the problem if it
1066 (!TObject::GetObjectStat() || gObjectTable->PtrIsValid(pObj))) {
1074 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
1076 // see header file for function documentation
1077 ALIHLTCOMPONENT_BASE_STOPWATCH();
1078 AliHLTComponentDataType dt=kAliHLTVoidDataType;
1079 int idx=fCurrentInputBlock;
1081 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1083 HLTError("unknown object %p", pObject);
1087 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
1088 dt=fpInputBlocks[idx].fDataType;
1090 HLTFatal("severe internal error, index out of range");
1096 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
1098 // see header file for function documentation
1099 ALIHLTCOMPONENT_BASE_STOPWATCH();
1100 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
1101 int idx=fCurrentInputBlock;
1103 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1105 HLTError("unknown object %p", pObject);
1109 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
1110 iSpec=fpInputBlocks[idx].fSpecification;
1112 HLTFatal("severe internal error, index out of range");
1118 int AliHLTComponent::Forward(const TObject* pObject)
1120 // see header file for function documentation
1122 int idx=fCurrentInputBlock;
1124 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1126 HLTError("unknown object %p", pObject);
1131 fOutputBlocks.push_back(fpInputBlocks[idx]);
1136 int AliHLTComponent::Forward(const AliHLTComponentBlockData* pBlock)
1138 // see header file for function documentation
1140 int idx=fCurrentInputBlock;
1142 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
1144 HLTError("unknown Block %p", pBlock);
1149 // check for fpInputBlocks pointer done in FindInputBlock
1150 fOutputBlocks.push_back(fpInputBlocks[idx]);
1155 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
1157 // see header file for function documentation
1158 ALIHLTCOMPONENT_BASE_STOPWATCH();
1161 int idx=FindInputBlock(fSearchDataType, 0);
1162 const AliHLTComponentBlockData* pBlock=NULL;
1164 // check for fpInputBlocks pointer done in FindInputBlock
1165 pBlock=&fpInputBlocks[idx];
1166 fCurrentInputBlock=idx;
1171 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
1172 const char* dtOrigin)
1174 // see header file for function documentation
1175 ALIHLTCOMPONENT_BASE_STOPWATCH();
1176 AliHLTComponentDataType dt;
1177 SetDataType(dt, dtID, dtOrigin);
1178 return GetFirstInputBlock(dt);
1181 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index) const
1183 // see header file for function documentation
1184 ALIHLTCOMPONENT_BASE_STOPWATCH();
1185 assert( 0 <= index and index < (int)fCurrentEventData.fBlockCnt );
1186 return &fpInputBlocks[index];
1189 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
1191 // see header file for function documentation
1192 ALIHLTCOMPONENT_BASE_STOPWATCH();
1193 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
1194 const AliHLTComponentBlockData* pBlock=NULL;
1196 // check for fpInputBlocks pointer done in FindInputBlock
1197 pBlock=&fpInputBlocks[idx];
1198 fCurrentInputBlock=idx;
1203 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
1205 // see header file for function documentation
1206 int iResult=-ENOENT;
1207 if (fpInputBlocks!=NULL) {
1209 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
1210 iResult=(int)(pBlock-fpInputBlocks);
1219 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
1221 // see header file for function documentation
1222 ALIHLTCOMPONENT_BASE_STOPWATCH();
1223 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
1224 int idx=fCurrentInputBlock;
1226 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
1228 HLTError("unknown Block %p", pBlock);
1232 // check for fpInputBlocks pointer done in FindInputBlock
1233 iSpec=fpInputBlocks[idx].fSpecification;
1238 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1239 void* pHeader, int headerSize)
1241 // see header file for function documentation
1242 ALIHLTCOMPONENT_BASE_STOPWATCH();
1245 if (fPushbackPeriod>0) {
1246 // suppress the output
1248 if (fLastPushBackTime<0 || (int)time.Get()-fLastPushBackTime<fPushbackPeriod) return 0;
1251 AliHLTMessage msg(kMESS_OBJECT);
1252 msg.SetCompressionLevel(fCompressionLevel);
1253 msg.WriteObject(pObject);
1254 Int_t iMsgLength=msg.Length();
1256 // Matthias Sep 2008
1257 // NOTE: AliHLTMessage does implement it's own SetLength method
1258 // which is not architecture independent. The original SetLength
1259 // stores the size always in network byte order.
1260 // I'm trying to remember the rational for that, might be that
1261 // it was just some lack of knowledge. Want to change this, but
1262 // has to be done carefullt to be backward compatible.
1263 msg.SetLength(); // sets the length to the first (reserved) word
1265 // does nothing if the level is 0
1268 char *mbuf = msg.Buffer();
1269 if (msg.CompBuffer()) {
1270 msg.SetLength(); // set once more to have to byte order
1271 mbuf = msg.CompBuffer();
1272 iMsgLength = msg.CompLength();
1275 iResult=InsertOutputBlock(mbuf, iMsgLength, dt, spec, pHeader, headerSize);
1277 HLTDebug("object %s (%p) size %d compression %d inserted to output", pObject->ClassName(), pObject, iMsgLength, msg.GetCompressionLevel());
1279 fLastObjectSize=iMsgLength;
1281 HLTError("object serialization failed for object %p", pObject);
1290 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1291 void* pHeader, int headerSize)
1293 // see header file for function documentation
1294 ALIHLTCOMPONENT_BASE_STOPWATCH();
1295 AliHLTComponentDataType dt;
1296 SetDataType(dt, dtID, dtOrigin);
1297 return PushBack(pObject, dt, spec, pHeader, headerSize);
1300 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1301 const void* pHeader, int headerSize)
1303 // see header file for function documentation
1304 ALIHLTCOMPONENT_BASE_STOPWATCH();
1305 if (fPushbackPeriod>0) {
1306 // suppress the output
1308 if (fLastPushBackTime<0 || (int)time.Get()-fLastPushBackTime<fPushbackPeriod) return 0;
1311 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
1314 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1315 const void* pHeader, int headerSize)
1317 // see header file for function documentation
1318 ALIHLTCOMPONENT_BASE_STOPWATCH();
1319 AliHLTComponentDataType dt;
1320 SetDataType(dt, dtID, dtOrigin);
1321 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
1324 int AliHLTComponent::InsertOutputBlock(const void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1325 const void* pHeader, int iHeaderSize)
1327 // see header file for function documentation
1329 int iBlkSize = iBufferSize + iHeaderSize;
1331 if ((pBuffer!=NULL && iBufferSize>0) || (pHeader!=NULL && iHeaderSize>0)) {
1332 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
1333 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1335 // copy header if provided but skip if the header is the target location
1336 // in that case it has already been copied
1337 if (pHeader!=NULL && pHeader!=pTgt) {
1338 memcpy(pTgt, pHeader, iHeaderSize);
1341 pTgt += (AliHLTUInt8_t) iHeaderSize;
1343 // copy buffer if provided but skip if buffer is the target location
1344 // in that case it has already been copied
1345 if (pBuffer!=NULL && pBuffer!=pTgt) {
1346 memcpy(pTgt, pBuffer, iBufferSize);
1348 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
1349 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
1351 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
1353 if (fpOutputBuffer) {
1354 HLTError("too little space in output buffer: %d of %d, required %d", fOutputBufferSize-fOutputBufferFilled, fOutputBufferSize, iBlkSize);
1356 HLTError("output buffer not available");
1362 AliHLTComponentBlockData bd;
1363 FillBlockData( bd );
1364 bd.fOffset = fOutputBufferFilled;
1365 bd.fSize = iBlkSize;
1367 bd.fSpecification = spec;
1368 fOutputBlocks.push_back( bd );
1369 fOutputBufferFilled+=bd.fSize;
1375 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
1377 // see header file for function documentation
1378 if (!pObject) return -EINVAL;
1379 AliHLTMessage msg(kMESS_OBJECT);
1380 msg.WriteObject(pObject);
1381 return msg.Length();
1384 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
1385 const char* dtOrigin,
1386 AliHLTUInt32_t spec)
1388 // see header file for function documentation
1389 ALIHLTCOMPONENT_BASE_STOPWATCH();
1390 AliHLTComponentDataType dt;
1391 SetDataType(dt, dtID, dtOrigin);
1392 return CreateMemoryFile(capacity, dt, spec);
1395 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
1396 const AliHLTComponentDataType& dt,
1397 AliHLTUInt32_t spec)
1399 // see header file for function documentation
1400 ALIHLTCOMPONENT_BASE_STOPWATCH();
1401 AliHLTMemoryFile* pFile=NULL;
1402 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
1403 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1404 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
1406 unsigned int nofBlocks=fOutputBlocks.size();
1407 if (nofBlocks+1>fMemFiles.size()) {
1408 fMemFiles.resize(nofBlocks+1, NULL);
1410 if (nofBlocks<fMemFiles.size()) {
1411 fMemFiles[nofBlocks]=pFile;
1412 AliHLTComponentBlockData bd;
1413 FillBlockData( bd );
1414 bd.fOffset = fOutputBufferFilled;
1415 bd.fSize = capacity;
1417 bd.fSpecification = spec;
1418 fOutputBufferFilled+=bd.fSize;
1419 fOutputBlocks.push_back( bd );
1421 HLTError("can not allocate/grow object array");
1422 pFile->CloseMemoryFile(0);
1428 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
1433 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
1434 const char* dtOrigin,
1435 AliHLTUInt32_t spec,
1438 // see header file for function documentation
1439 ALIHLTCOMPONENT_BASE_STOPWATCH();
1440 AliHLTComponentDataType dt;
1441 SetDataType(dt, dtID, dtOrigin);
1442 int size=fOutputBufferSize-fOutputBufferFilled;
1443 if (capacity<0 || capacity>1.0) {
1444 HLTError("invalid parameter: capacity %f", capacity);
1447 size=(int)(size*capacity);
1448 return CreateMemoryFile(size, dt, spec);
1451 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
1452 AliHLTUInt32_t spec,
1455 // see header file for function documentation
1456 ALIHLTCOMPONENT_BASE_STOPWATCH();
1457 int size=fOutputBufferSize-fOutputBufferFilled;
1458 if (capacity<0 || capacity>1.0) {
1459 HLTError("invalid parameter: capacity %f", capacity);
1462 size=(int)(size*capacity);
1463 return CreateMemoryFile(size, dt, spec);
1466 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
1467 const char* key, int option)
1469 // see header file for function documentation
1471 if (pFile && pObject) {
1473 iResult=pObject->Write(key, option);
1477 iResult=-pFile->GetErrno();
1478 if (iResult==-ENOSPC) {
1479 HLTError("error writing memory file, buffer too small");
1488 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
1490 // see header file for function documentation
1493 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1495 while (element!=fMemFiles.end() && iResult>=0) {
1496 if (*element && *element==pFile) {
1497 iResult=pFile->CloseMemoryFile();
1499 // sync memory files and descriptors
1501 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
1509 HLTError("can not find memory file %p", pFile);
1517 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
1519 // see header file for function documentation
1522 AliHLTComponentEventDoneData* newEDD = NULL;
1524 unsigned long newSize=edd.fDataSize;
1526 newSize += fEventDoneData->fDataSize;
1528 if (newSize>fEventDoneDataSize) {
1529 newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[ sizeof(AliHLTComponentEventDoneData)+newSize ] );
1532 newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
1533 newEDD->fDataSize = newSize;
1534 newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
1535 unsigned long long offset = 0;
1536 if (fEventDoneData) {
1537 memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
1538 offset += fEventDoneData->fDataSize;
1540 memcpy( reinterpret_cast<AliHLTUInt8_t*>(newEDD->fData)+offset, edd.fData, edd.fDataSize );
1542 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
1543 fEventDoneData = newEDD;
1544 fEventDoneDataSize = newSize;
1547 memcpy( reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize, edd.fData, edd.fDataSize );
1548 fEventDoneData->fDataSize += edd.fDataSize;
1553 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1554 const AliHLTComponentBlockData* blocks,
1555 AliHLTComponentTriggerData& trigData,
1556 AliHLTUInt8_t* outputPtr,
1557 AliHLTUInt32_t& size,
1558 AliHLTUInt32_t& outputBlockCnt,
1559 AliHLTComponentBlockData*& outputBlocks,
1560 AliHLTComponentEventDoneData*& edd )
1562 // see header file for function documentation
1563 HLTLogKeyword(fChainId.c_str());
1564 ALIHLTCOMPONENT_BASE_STOPWATCH();
1566 fCurrentEvent=evtData.fEventID;
1567 fCurrentEventData=evtData;
1568 fpInputBlocks=blocks;
1569 fCurrentInputBlock=-1;
1570 fSearchDataType=kAliHLTAnyDataType;
1571 fpOutputBuffer=outputPtr;
1572 fOutputBufferSize=size;
1573 fOutputBufferFilled=0;
1574 fOutputBlocks.clear();
1578 AliHLTComponentBlockDataList forwardedBlocks;
1580 // optional component statistics
1581 AliHLTComponentStatisticsList compStats;
1582 bool bAddComponentTableEntry=false;
1583 vector<AliHLTUInt32_t> parentComponentTables;
1584 #if defined(HLT_COMPONENT_STATISTICS)
1585 if ((fFlags&kDisableComponentStat)==0) {
1586 AliHLTComponentStatistics outputStat;
1587 memset(&outputStat, 0, sizeof(AliHLTComponentStatistics));
1588 outputStat.fStructSize=sizeof(AliHLTComponentStatistics);
1589 outputStat.fId=fChainIdCrc;
1591 fpBenchmark->Stop();
1592 outputStat.fComponentCycleTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
1593 fpBenchmark->Reset();
1594 fpBenchmark->Start();
1596 compStats.push_back(outputStat);
1598 #endif // HLT_COMPONENT_STATISTICS
1600 // data processing is skipped
1601 // - if there are only steering events in the block list.
1602 // For the sake of data source components data processing
1603 // is not skipped if there is no block list at all or if it
1604 // just contains the eventType block
1605 // - always skipped if the event is of type
1606 // - gkAliEventTypeConfiguration
1607 // - gkAliEventTypeReadPreprocessor
1608 const unsigned int skipModeDefault=0x1;
1609 const unsigned int skipModeForce=0x2;
1610 unsigned int bSkipDataProcessing=skipModeDefault;
1612 // find special events
1613 if (fpInputBlocks && evtData.fBlockCnt>0) {
1614 // first look for all special events and execute in the appropriate
1615 // sequence afterwords
1616 int indexComConfEvent=-1;
1617 int indexUpdtDCSEvent=-1;
1618 int indexSOREvent=-1;
1619 int indexEOREvent=-1;
1620 int indexECSParamBlock=-1;
1621 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1622 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1624 // the AliHLTCalibrationProcessor relies on the SOR and EOR events
1625 bSkipDataProcessing&=~skipModeDefault;
1626 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeRunType) {
1628 // handling is not clear yet
1629 if (fpInputBlocks[i].fPtr) {
1630 HLTDebug("got run type \"%s\"\n", fpInputBlocks[i].fPtr);
1632 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1634 // the calibration processor relies on the SOR and EOR events
1635 bSkipDataProcessing&=~skipModeDefault;
1636 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1638 // this event is most likely deprecated
1639 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComConf) {
1640 indexComConfEvent=i;
1641 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeUpdtDCS) {
1642 indexUpdtDCSEvent=i;
1643 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEvent) {
1644 fEventType=fpInputBlocks[i].fSpecification;
1646 // skip always in case of gkAliEventTypeConfiguration
1647 if (fpInputBlocks[i].fSpecification==gkAliEventTypeConfiguration) bSkipDataProcessing|=skipModeForce;
1649 // skip always in case of gkAliEventTypeReadPreprocessor
1650 if (fpInputBlocks[i].fSpecification==gkAliEventTypeReadPreprocessor) bSkipDataProcessing|=skipModeForce;
1652 // never skip if the event type block is the only block
1653 if (evtData.fBlockCnt==1) bSkipDataProcessing&=~skipModeDefault;
1655 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentStatistics) {
1656 if (compStats.size()>0) {
1657 AliHLTUInt8_t* pData=reinterpret_cast<AliHLTUInt8_t*>(fpInputBlocks[i].fPtr);
1658 for (AliHLTUInt32_t offset=0;
1659 offset+sizeof(AliHLTComponentStatistics)<=fpInputBlocks[i].fSize;
1660 offset+=sizeof(AliHLTComponentStatistics)) {
1661 AliHLTComponentStatistics* pStat=reinterpret_cast<AliHLTComponentStatistics*>(pData+offset);
1662 if (pStat && compStats[0].fLevel<=pStat->fLevel) {
1663 compStats[0].fLevel=pStat->fLevel+1;
1665 compStats.push_back(*pStat);
1668 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentTable) {
1669 forwardedBlocks.push_back(fpInputBlocks[i]);
1670 parentComponentTables.push_back(fpInputBlocks[i].fSpecification);
1671 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeECSParam) {
1672 indexECSParamBlock=i;
1674 // the processing function is called if there is at least one
1675 // non-steering data block. Steering blocks are not filtered out
1676 // for sake of performance
1677 bSkipDataProcessing&=~skipModeDefault;
1678 if (compStats.size()>0) {
1679 compStats[0].fInputBlockCount++;
1680 compStats[0].fTotalInputSize+=fpInputBlocks[i].fSize;
1685 if (indexSOREvent>=0) {
1687 bAddComponentTableEntry=true;
1688 if (fpRunDesc==NULL) {
1689 fpRunDesc=new AliHLTRunDesc;
1690 if (fpRunDesc) *fpRunDesc=kAliHLTVoidRunDesc;
1693 AliHLTRunDesc rundesc;
1694 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexSOREvent, "AliHLTRunDesc", "SOR"))>0) {
1695 if (fpRunDesc->fRunNo==kAliHLTVoidRunNo) {
1697 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1698 SetCDBRunNo(fpRunDesc->fRunNo);
1699 } else if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1700 HLTWarning("already set run properties run no %d, ignoring SOR with run no %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1707 if (indexECSParamBlock>=0) {
1708 if (fpInputBlocks[indexECSParamBlock].fSize>0) {
1709 const char* param=reinterpret_cast<const char*>(fpInputBlocks[indexECSParamBlock].fPtr);
1710 TString paramString;
1711 if (param[fpInputBlocks[indexECSParamBlock].fSize-1]!=0) {
1712 HLTWarning("ECS parameter string not terminated");
1713 paramString.Insert(0, param, fpInputBlocks[indexECSParamBlock].fSize);
1718 ScanECSParam(paramString.Data());
1720 HLTWarning("empty ECS parameter received");
1723 // TODO: later on we might throw a warning here since the CTP trigger classes
1724 // should be mandatory
1727 if (indexEOREvent>=0) {
1728 fLastPushBackTime=0; // always send at EOR
1729 bAddComponentTableEntry=true;
1730 if (fpRunDesc!=NULL) {
1732 AliHLTRunDesc rundesc;
1733 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexEOREvent, "AliHLTRunDesc", "SOR"))>0) {
1734 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1735 HLTWarning("run no mismatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1737 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1740 // we do not unload the fpRunDesc struct here in order to have the run information
1741 // available during the event processing
1742 // https://savannah.cern.ch/bugs/?39711
1743 // the info will be cleared in DeInit
1746 HLTWarning("did not receive SOR, ignoring EOR");
1749 if (indexComConfEvent>=0 || fEventType==gkAliEventTypeConfiguration) {
1751 if (indexComConfEvent>=0 && fpInputBlocks[indexComConfEvent].fPtr!=NULL && fpInputBlocks[indexComConfEvent].fSize>0) {
1752 cdbEntry.Append(reinterpret_cast<const char*>(fpInputBlocks[indexComConfEvent].fPtr), fpInputBlocks[indexComConfEvent].fSize);
1754 HLTDebug("received component configuration command: entry %s", cdbEntry.IsNull()?"none":cdbEntry.Data());
1755 int tmpResult=Reconfigure(cdbEntry[0]==0?NULL:cdbEntry.Data(), fChainId.c_str());
1757 HLTWarning("reconfiguration of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1760 if (indexUpdtDCSEvent>=0 || fEventType==gkAliEventTypeReadPreprocessor) {
1762 if (fpInputBlocks[indexUpdtDCSEvent].fPtr!=NULL && fpInputBlocks[indexUpdtDCSEvent].fSize>0) {
1763 modules.Append(reinterpret_cast<const char*>(fpInputBlocks[indexUpdtDCSEvent].fPtr), fpInputBlocks[indexUpdtDCSEvent].fSize);
1765 HLTDebug("received preprocessor update command: detectors %s", modules.IsNull()?"ALL":modules.Data());
1766 int tmpResult=ReadPreprocessorValues(modules[0]==0?"ALL":modules.Data());
1768 HLTWarning("preprocessor update of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1772 // processing function needs to be called if there are no input data
1773 // blocks in order to make data source components working.
1774 bSkipDataProcessing&=~skipModeDefault;
1777 // data processing is not skipped if the component explicitly asks
1778 // for the private blocks
1779 if ((fFlags&kRequireSteeringBlocks)!=0) bSkipDataProcessing=0;
1782 // set the active triggers for this event
1783 fpCTPData->SetTriggers(trigData);
1784 // increment CTP trigger counters if available
1785 if (IsDataEvent()) fpCTPData->Increment(trigData);
1788 AliHLTComponentBlockDataList blockData;
1789 if (iResult>=0 && !bSkipDataProcessing)
1790 { // dont delete, sets the scope for the stopwatch guard
1791 // do not use ALIHLTCOMPONENT_DA_STOPWATCH(); macro
1792 // in order to avoid 'shadowed variable' warning
1793 AliHLTStopwatchGuard swguard2(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)kSWDA)):NULL);
1794 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1795 } // end of the scope of the stopwatch guard
1796 if (iResult>=0 && !bSkipDataProcessing) {
1797 if (fOutputBlocks.size()>0) {
1798 // High Level interface
1800 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1801 // sync memory files and descriptors
1802 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1804 while (element!=fMemFiles.end() && iResult>=0) {
1806 if ((*element)->IsClosed()==0) {
1807 HLTWarning("memory file has not been closed, force flush");
1808 iResult=CloseMemoryFile(*element);
1815 // create the descriptor list
1816 if (blockData.size()>0) {
1817 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1820 if (compStats.size()>0 && IsDataEvent()) {
1821 int offset=AddComponentStatistics(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, compStats);
1822 if (offset>0) fOutputBufferFilled+=offset;
1824 if (bAddComponentTableEntry) {
1825 int offset=AddComponentTableEntry(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, parentComponentTables);
1826 if (offset>0) size+=offset;
1828 if (forwardedBlocks.size()>0) {
1829 fOutputBlocks.insert(fOutputBlocks.end(), forwardedBlocks.begin(), forwardedBlocks.end());
1831 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1832 size=fOutputBufferFilled;
1836 // Low Level interface
1837 if (compStats.size()>0) {
1838 int offset=AddComponentStatistics(blockData, fpOutputBuffer, fOutputBufferSize, size, compStats);
1839 if (offset>0) size+=offset;
1841 if (bAddComponentTableEntry) {
1842 int offset=AddComponentTableEntry(blockData, fpOutputBuffer, fOutputBufferSize, size, parentComponentTables);
1843 if (offset>0) size+=offset;
1845 if (forwardedBlocks.size()>0) {
1846 blockData.insert(blockData.end(), forwardedBlocks.begin(), forwardedBlocks.end());
1848 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1851 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1854 if (iResult<0 || bSkipDataProcessing) {
1858 CleanupInputObjects();
1859 if (iResult>=0 && IsDataEvent()) {
1860 IncrementEventCounter();
1862 if (outputBlockCnt==0) {
1863 // no output blocks, set size to 0
1867 // reset the internal EventData struct
1868 FillEventData(fCurrentEventData);
1870 // reset the active triggers
1871 if (fpCTPData) fpCTPData->SetTriggers(0);
1873 // set the time for the pushback period
1874 if (fPushbackPeriod>0) {
1875 // suppress the output
1877 if (fLastPushBackTime<0 || (int)time.Get()-fLastPushBackTime>=fPushbackPeriod) {
1878 fLastPushBackTime=time.Get();
1885 int AliHLTComponent::AddComponentStatistics(AliHLTComponentBlockDataList& blocks,
1886 AliHLTUInt8_t* buffer,
1887 AliHLTUInt32_t bufferSize,
1888 AliHLTUInt32_t offset,
1889 AliHLTComponentStatisticsList& stats) const
1891 // see header file for function documentation
1893 if ((fFlags&kDisableComponentStat)!=0) return 0;
1894 #if defined(HLT_COMPONENT_STATISTICS)
1895 if (stats.size()==0) return -ENOENT;
1896 // check if there is space for at least one entry
1897 if (offset+sizeof(AliHLTComponentStatistics)>bufferSize) return 0;
1898 stats[0].fTotalOutputSize=offset;
1899 stats[0].fOutputBlockCount=blocks.size();
1901 fpBenchmark->Stop();
1902 stats[0].fTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
1903 stats[0].fCTime=(AliHLTUInt32_t)(fpBenchmark->CpuTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
1904 fpBenchmark->Continue();
1906 if (offset+stats.size()*sizeof(AliHLTComponentStatistics)>bufferSize) {
1907 AliHLTUInt32_t removedLevel=0;
1909 // remove all entries of the level of the last entry
1910 removedLevel=stats.back().fLevel;
1911 AliHLTComponentStatisticsList::iterator element=stats.begin();
1913 while (element!=stats.end()) {
1914 if (element->fLevel<=removedLevel) {
1915 element=stats.erase(element);
1920 } while (stats.size()>1 &&
1921 (offset+stats.size()*sizeof(AliHLTComponentStatistics)>bufferSize));
1923 assert(stats.size()>0);
1924 if (stats.size()==0) return 0;
1926 if (offset+stats.size()*sizeof(AliHLTComponentStatistics)<=bufferSize) {
1927 AliHLTComponentBlockData bd;
1928 FillBlockData( bd );
1929 bd.fOffset = offset;
1930 bd.fSize = stats.size()*sizeof(AliHLTComponentStatistics);
1931 bd.fDataType = kAliHLTDataTypeComponentStatistics;
1932 bd.fSpecification = kAliHLTVoidDataSpec;
1933 unsigned int master=0;
1934 for (unsigned int i=1; i<blocks.size(); i++) {
1935 if (blocks[i].fSize>blocks[master].fSize &&
1936 !MatchExactly(blocks[i].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate))
1939 if (blocks.size()>0 && !MatchExactly(blocks[master].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate)) {
1940 // take the data origin of the biggest block as specification
1941 // this is similar to the treatment in the HOMER interface. For traditional
1942 // reasons, the bytes are swapped there on a little endian architecture, so
1943 // we do it as well.
1944 memcpy(&bd.fSpecification, &blocks[master].fDataType.fOrigin, sizeof(bd.fSpecification));
1945 #ifdef R__BYTESWAP // set on little endian architectures
1946 bd.fSpecification=((bd.fSpecification & 0xFFULL) << 24) |
1947 ((bd.fSpecification & 0xFF00ULL) << 8) |
1948 ((bd.fSpecification & 0xFF0000ULL) >> 8) |
1949 ((bd.fSpecification & 0xFF000000ULL) >> 24);
1952 memcpy(buffer+offset, &(stats[0]), bd.fSize);
1953 blocks.push_back(bd);
1957 if (blocks.size() && buffer && bufferSize && offset && stats.size()) {
1958 // get rid of warning
1960 #endif // HLT_COMPONENT_STATISTICS
1964 int AliHLTComponent::AddComponentTableEntry(AliHLTComponentBlockDataList& blocks,
1965 AliHLTUInt8_t* buffer,
1966 AliHLTUInt32_t bufferSize,
1967 AliHLTUInt32_t offset,
1968 const vector<AliHLTUInt32_t>& parents) const
1970 // see header file for function documentation
1972 if ((fFlags&kDisableComponentStat)!=0) return 0;
1973 #if defined(HLT_COMPONENT_STATISTICS)
1974 // the payload consists of the AliHLTComponentTableEntry struct,
1975 // followed by a an array of 32bit crc chain ids and the component
1976 // description string
1977 unsigned int payloadSize=sizeof(AliHLTComponentTableEntry);
1978 payloadSize+=parents.size()*sizeof(AliHLTUInt32_t);
1980 // the component description has the following format:
1981 // chain-id{component-id:arguments}
1982 const char* componentId=const_cast<AliHLTComponent*>(this)->GetComponentID();
1983 unsigned int descriptionSize=fChainId.size()+1;
1984 descriptionSize+=2; // the '{}' around the component id
1985 descriptionSize+=strlen(componentId);
1986 descriptionSize+=1; // the ':' between component id and arguments
1987 descriptionSize+=fComponentArgs.size();
1989 payloadSize+=descriptionSize;
1990 if (buffer && (offset+payloadSize<=bufferSize)) {
1991 AliHLTUInt8_t* pTgt=buffer+offset;
1992 memset(pTgt, 0, payloadSize);
1995 AliHLTComponentTableEntry* pEntry=reinterpret_cast<AliHLTComponentTableEntry*>(pTgt);
1996 pEntry->fStructSize=sizeof(AliHLTComponentTableEntry);
1997 pEntry->fNofParents=parents.size();
1998 pEntry->fSizeDescription=descriptionSize;
1999 pTgt=pEntry->fBuffer;
2001 // write array of parents
2002 if (parents.size()>0) {
2003 unsigned int copy=parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type);
2004 memcpy(pTgt, &parents[0], parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type));
2008 // write component description
2009 memcpy(pTgt, fChainId.c_str(), fChainId.size());
2010 pTgt+=fChainId.size();
2012 memcpy(pTgt, componentId, strlen(componentId));
2013 pTgt+=strlen(componentId);
2015 memcpy(pTgt, fComponentArgs.c_str(), fComponentArgs.size());
2016 pTgt+=fComponentArgs.size();
2020 AliHLTComponentBlockData bd;
2021 FillBlockData( bd );
2022 bd.fOffset = offset;
2023 bd.fSize = payloadSize;
2024 bd.fDataType = kAliHLTDataTypeComponentTable;
2025 bd.fSpecification = fChainIdCrc;
2026 blocks.push_back(bd);
2030 if (blocks.size() && buffer && bufferSize && offset && parents.size()) {
2031 // get rid of warning
2033 #endif // HLT_COMPONENT_STATISTICS
2037 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
2042 // standard constructor (not for use)
2045 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
2047 fpStopwatch(pStopwatch),
2052 // check for already existing guard
2053 if (fgpCurrent) fpPrec=fgpCurrent;
2056 // stop the preceeding guard if it controls a different stopwatch
2058 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
2060 // start the stopwatch if the current guard controls a different one
2061 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
2064 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
2070 // copy constructor not for use
2074 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
2077 // assignment operator not for use
2084 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
2086 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
2090 // resume the preceeding guard if it controls a different stopwatch
2092 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
2094 // stop the stopwatch if the current guard controls a different one
2095 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
2097 // resume to the preceeding guard
2101 int AliHLTComponent::AliHLTStopwatchGuard::Hold(const TStopwatch* pSucc)
2103 // see header file for function documentation
2104 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
2105 return fpStopwatch!=pSucc?1:0;
2108 int AliHLTComponent::AliHLTStopwatchGuard::Resume(const TStopwatch* pSucc)
2110 // see header file for function documentation
2111 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
2112 return fpStopwatch!=pSucc?1:0;
2115 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
2117 // see header file for function documentation
2119 if (pSW!=NULL && type<kSWTypeCount) {
2120 if (fpStopwatches) {
2121 TObject* pObj=fpStopwatches->At((int)type);
2122 if (pSW==NULL // explicit reset
2123 || pObj==NULL) { // explicit set
2124 fpStopwatches->AddAt(pSW, (int)type);
2125 } else if (pObj!=pSW) {
2126 HLTWarning("stopwatch %d already set, reset first", (int)type);
2136 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
2138 // see header file for function documentation
2139 if (pStopwatches==NULL) return -EINVAL;
2142 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntriesFast(); i++)
2143 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
2147 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
2149 // see header file for function documentation
2151 // 2010-02-11 OCDB is now the reliable source for the run number, it is
2152 // initialized either by aliroot or the external interface depending
2153 // on the environment. It turned out that the rundescriptor is not set
2154 // in the aliroot mode, resulting in an invalid run number. However this
2155 // did not cause problems until now. OCDB initialization has been revised
2156 // already in 10/2009. This was a remnant.
2157 // Have to check whether we get rid of the rundescriptor at some point.
2158 if (fpRunDesc==NULL) return AliHLTMisc::Instance().GetCDBRunNo();
2159 if (fpRunDesc->fRunNo!=(unsigned)AliHLTMisc::Instance().GetCDBRunNo()) {
2160 HLTWarning("run number mismatch: ocdb %d run descriptor %d", AliHLTMisc::Instance().GetCDBRunNo(), fpRunDesc->fRunNo);
2162 return fpRunDesc->fRunNo;
2165 AliHLTUInt32_t AliHLTComponent::GetRunType() const
2167 // see header file for function documentation
2168 if (fpRunDesc==NULL) return kAliHLTVoidRunType;
2169 return fpRunDesc->fRunType;
2172 AliHLTUInt32_t AliHLTComponent::GetTimeStamp() const
2174 // see header file for function documentation
2175 if (fCurrentEventData.fEventCreation_s) {
2176 return fCurrentEventData.fEventCreation_s;
2178 // using the actual UTC if the time stamp was not set by the framework
2179 return static_cast<AliHLTUInt32_t>(time(NULL));
2182 AliHLTUInt32_t AliHLTComponent::GetPeriodNumber() const
2184 // see header file for function documentation
2185 return (GetEventId()>>36)&0xfffffff;
2188 AliHLTUInt32_t AliHLTComponent::GetOrbitNumber() const
2190 // see header file for function documentation
2191 return (GetEventId()>>12)&0xffffff;
2194 AliHLTUInt16_t AliHLTComponent::GetBunchCrossNumber() const
2196 // see header file for function documentation
2197 return GetEventId()&0xfff;
2200 bool AliHLTComponent::IsDataEvent(AliHLTUInt32_t* pTgt) const
2202 // see header file for function documentation
2203 if (pTgt) *pTgt=fEventType;
2204 return (fEventType==gkAliEventTypeData ||
2205 fEventType==gkAliEventTypeDataReplay ||
2206 fEventType==gkAliEventTypeCalibration);
2209 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
2210 const char* structname, const char* eventname)
2212 // see header file for function documentation
2214 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
2215 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
2216 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
2217 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
2218 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
2219 if (fpInputBlocks[iBlockNo].fSize!=copy) {
2220 HLTWarning("%s event: mismatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
2221 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
2223 if (copy!=iStructSize) {
2224 HLTWarning("%s event: mismatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
2225 if (copy>iStructSize) {
2228 memset(pTgt, 0, iStructSize);
2231 memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
2235 HLTWarning("%s event: missing data block", eventname);
2241 HLTError("invalid struct");
2247 void AliHLTComponent::SetDDLBit(AliHLTEventDDL &list, Int_t ddlId, Bool_t state ) const
2249 // see header file for function documentation
2251 // -- Detector offset
2252 Int_t ddlIdBase = TMath::FloorNint( (Double_t) ddlId / 256.0 );
2254 // -- Word Base = 1. word of detector ( TPC has 8 words, TOF 3 )
2257 if ( ddlIdBase <= 3 )
2258 wordBase = ddlIdBase;
2259 else if ( ddlIdBase > 3 && ddlIdBase < 5 )
2260 wordBase = ddlIdBase + 7;
2262 wordBase = ddlIdBase + 9;
2264 // -- Bit index in Word
2265 Int_t bitIdx = ddlId % 32;
2268 Int_t wordIdx = wordBase;
2270 // -- if TPC (3) or TOD (5) add word idx
2271 if ( ( ddlIdBase == 3 ) || ( ddlIdBase == 5 ) ) {
2272 wordIdx += TMath::FloorNint( (Double_t) ( ddlId - ( ddlIdBase * 256 ) ) / 32.0 );
2275 // -- Set -- 'OR' word with bit mask;
2277 list.fList[wordIdx] |= ( 0x00000001 << bitIdx );
2278 // -- Unset -- 'AND' word with bit mask;
2280 list.fList[wordIdx] &= ( 0xFFFFFFFF ^ ( 0x00000001 << bitIdx ) );
2283 Int_t AliHLTComponent::GetFirstUsedDDLWord(AliHLTEventDDL &list) const
2285 // see header file for function documentation
2289 for ( Int_t wordNdx = 0 ; wordNdx < gkAliHLTDDLListSize ; wordNdx++ ) {
2291 if ( list.fList[wordNdx] != 0 && iResult == -1 ) {
2292 // check for special cases TPC and TOF
2293 if ( wordNdx > 3 && wordNdx <= 10 ) {
2297 else if ( wordNdx > 12 && wordNdx <= 14 ) {
2304 else if ( list.fList[wordNdx] != 0 && iResult >= 0 ) {
2305 HLTError( "DDLIDs for minimum of TWO detectors ( %d, %d ) set, this function works only for ONE detector.", iResult, wordNdx );
2314 AliHLTUInt32_t AliHLTComponent::CalculateChecksum(const AliHLTUInt8_t* buffer, int size)
2316 // see header file for function documentation
2317 AliHLTUInt32_t remainder = 0;
2318 const AliHLTUInt8_t crcwidth=(8*sizeof(AliHLTUInt32_t));
2319 const AliHLTUInt32_t topbit=1 << (crcwidth-1);
2320 const AliHLTUInt32_t polynomial=0xD8; /* 11011 followed by 0's */
2323 // http://www.netrino.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
2326 * Perform modulo-2 division, a byte at a time.
2328 for (int byte = 0; byte < size; ++byte)
2331 * Bring the next byte into the remainder.
2333 remainder ^= (buffer[byte] << (crcwidth - 8));
2336 * Perform modulo-2 division, a bit at a time.
2338 for (uint8_t bit = 8; bit > 0; --bit)
2341 * Try to divide the current data bit.
2343 if (remainder & topbit)
2345 remainder = (remainder << 1) ^ polynomial;
2349 remainder = (remainder << 1);
2355 * The final remainder is the CRC result.
2360 int AliHLTComponent::ExtractComponentTableEntry(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size,
2361 string& retChainId, string& retCompId, string& retCompArgs,
2362 vector<AliHLTUInt32_t>& parents)
2364 // see header file for function documentation
2367 retCompArgs.clear();
2369 if (!pBuffer || size==0) return 0;
2371 const AliHLTComponentTableEntry* pEntry=reinterpret_cast<const AliHLTComponentTableEntry*>(pBuffer);
2372 if (size<8/* the initial size of the structure*/ ||
2373 pEntry==NULL || pEntry->fStructSize<8) return -ENOMSG;
2374 const AliHLTUInt32_t* pParents=reinterpret_cast<const AliHLTUInt32_t*>(pEntry->fBuffer);
2375 const AliHLTUInt8_t* pEnd=pBuffer+size;
2377 if (pParents+pEntry->fNofParents>=reinterpret_cast<const AliHLTUInt32_t*>(pEnd)) return -ENODEV;
2378 for (unsigned int i=0; i<pEntry->fNofParents; i++, pParents++) {
2379 parents.push_back(*pParents);
2382 const char* pDescription=reinterpret_cast<const char*>(pParents);
2383 if (pDescription+pEntry->fSizeDescription>=reinterpret_cast<const char*>(pEnd) ||
2384 *(pDescription+pEntry->fSizeDescription)!=0) {
2388 TString descriptor=reinterpret_cast<const char*>(pDescription);
2392 TObjArray* pTokens=descriptor.Tokenize("{");
2395 if (pTokens->GetEntriesFast()>n) {
2396 retChainId=((TObjString*)pTokens->At(n++))->GetString();
2398 if (pTokens->GetEntriesFast()>n) {
2399 compId=((TObjString*)pTokens->At(n++))->GetString();
2403 if (!compId.IsNull() && (pTokens=compId.Tokenize(":"))!=NULL) {
2405 if (pTokens->GetEntriesFast()>n) {
2406 compId=((TObjString*)pTokens->At(n++))->GetString();
2408 if (pTokens->GetEntriesFast()>n) {
2409 compArgs=((TObjString*)pTokens->At(n++))->GetString();
2413 compId.ReplaceAll("}", "");
2414 compArgs.ReplaceAll("}", "");
2417 retCompArgs=compArgs;
2419 if (retChainId.size()==0) return -ENODATA;
2424 int AliHLTComponent::LoggingVarargs(AliHLTComponentLogSeverity severity,
2425 const char* originClass, const char* originFunc,
2426 const char* file, int line, ... ) const
2428 // see header file for function documentation
2432 va_start(args, line);
2434 // logging function needs to be const in order to be called from const member functions
2435 // without problems. But at this point we face the problem with virtual members which
2436 // are not necessarily const.
2437 AliHLTComponent* nonconst=const_cast<AliHLTComponent*>(this);
2438 AliHLTLogging::SetLogString(this, ", %p", "%s (%s_pfmt_): ",
2439 fChainId[0]!=0?fChainId.c_str():nonconst->GetComponentID(),
2440 nonconst->GetComponentID());
2441 iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args, true /*append*/));
2447 int AliHLTComponent::ScanECSParam(const char* ecsParam)
2449 // see header file for function documentation
2451 // format of the parameter string from ECS
2452 // <command>;<parameterkey>=<parametervalue>;<parameterkey>=<parametervalue>;...
2453 // search for a subset of the parameterkeys
2457 // CTP_TRIGGER_CLASS=
2458 // DATA_FORMAT_VERSION=
2460 // HLT_OUT_DDL_LIST=
2461 // HLT_TRIGGER_CODE=
2464 // The command apears not to be sent by the online framework
2466 TString string=ecsParam;
2467 TObjArray* parameter=string.Tokenize(";");
2469 for (int i=0; i<parameter->GetEntriesFast(); i++) {
2470 TString entry=((TObjString*)parameter->At(i))->GetString();
2471 HLTDebug("scanning ECS entry: %s", entry.Data());
2472 TObjArray* entryParams=entry.Tokenize("=");
2474 if (entryParams->GetEntriesFast()>1) {
2475 if ((((TObjString*)entryParams->At(0))->GetString()).CompareTo("CTP_TRIGGER_CLASS")==0) {
2476 int result=InitCTPTriggerClasses((((TObjString*)entryParams->At(1))->GetString()).Data());
2477 if (iResult>=0 && result<0) iResult=result;
2479 // TODO: scan the other parameters
2480 // e.g. consistency check of run number
2492 int AliHLTComponent::SetupCTPData()
2494 // see header file for function documentation
2495 if (fpCTPData) delete fpCTPData;
2496 fpCTPData=new AliHLTCTPData;
2497 if (!fpCTPData) return -ENOMEM;
2501 int AliHLTComponent::InitCTPTriggerClasses(const char* ctpString)
2503 // see header file for function documentation
2504 if (!fpCTPData) return 0; // silently accept as the component has to announce that it want's the CTP info
2505 return fpCTPData->InitCTPTriggerClasses(ctpString);
2508 bool AliHLTComponent::EvaluateCTPTriggerClass(const char* expression, AliHLTComponentTriggerData& trigData) const
2510 // see header file for function documentation
2512 static bool bWarningThrown=false;
2513 if (!bWarningThrown) HLTError("Trigger classes not initialized, use SetupCTPData from DoInit()");
2514 bWarningThrown=true;
2518 return fpCTPData->EvaluateCTPTriggerClass(expression, trigData);
2521 Double_t AliHLTComponent::GetBz()
2524 return AliHLTMisc::Instance().GetBz();
2527 Double_t AliHLTComponent::GetBz(const Double_t *r)
2529 // Returns Bz (kG) at the point "r" .
2530 return AliHLTMisc::Instance().GetBz(r);
2533 void AliHLTComponent::GetBxByBz(const Double_t r[3], Double_t b[3])
2535 // Returns Bx, By and Bz (kG) at the point "r" .
2536 AliHLTMisc::Instance().GetBxByBz(r, b);