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"
37 #include "AliRawDataHeader.h"
40 #include "TObjArray.h"
41 #include "TObjectTable.h"
43 #include "TStopwatch.h"
45 #include "AliHLTMemoryFile.h"
46 #include "AliHLTMisc.h"
52 * default compression level for ROOT objects
54 #define ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION 5
55 #define ALIHLTCOMPONENT_STATTIME_SCALER 1000000
57 /** ROOT macro for the implementation of ROOT specific class methods */
58 ClassImp(AliHLTComponent);
60 /** stopwatch macro using the stopwatch guard */
61 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
62 //#define ALIHLTCOMPONENT_STOPWATCH(type)
64 /** stopwatch macro for operations of the base class */
65 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
66 /** stopwatch macro for operations of the detector algorithm (DA) */
67 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
69 AliHLTComponent::AliHLTComponent()
77 fCurrentInputBlock(-1),
78 fSearchDataType(kAliHLTVoidDataType),
83 fOutputBufferFilled(0),
85 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=CheckOCDBEntries();
263 iResult=DoInit(iNofChildArgs, pArguments);
268 // find out if the component wants to get the steering events
270 AliHLTComponentDataTypeList inputDt;
271 GetInputDataTypes(inputDt);
272 bool bRequireSteeringBlocks=false;
273 for (AliHLTComponentDataTypeList::iterator dt=inputDt.begin();
274 dt!=inputDt.end() && !bRequireSteeringBlocks;
276 bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeSOR);
277 bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeRunType);
278 bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeEOR);
279 bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeDDL);
280 bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeComponentStatistics);
282 if (bRequireSteeringBlocks) fFlags|=kRequireSteeringBlocks;
284 if (pArguments) delete [] pArguments;
286 #if defined(HLT_COMPONENT_STATISTICS)
287 // benchmarking stopwatch for the component statistics
288 fpBenchmark=new TStopwatch;
289 #endif // HLT_COMPONENT_STATISTICS
294 int AliHLTComponent::Deinit()
296 // see header file for function documentation
297 HLTLogKeyword(fChainId.c_str());
301 // TODO: the warning should be kept, but the condition is wrong since the
302 // AliHLTRunDesc is set before the SOR event in the SetRunDescription
303 // method. A couple of state flags should be defined but that is a bit more
304 // work to do. For the moment disable the warning (2009-07-01)
305 // 2009-09-08: now, the info is not cleared in the ProcessEvent, because it
306 // might be needed by components during the event processing.
307 //HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
308 AliHLTRunDesc* pRunDesc=fpRunDesc;
322 int AliHLTComponent::InitCDB(const char* cdbPath, AliHLTComponentHandler* pHandler)
324 // see header file for function documentation
326 HLTInfo("Using CDB: %s", cdbPath);
328 // I have to think about separating the library handling from the
329 // component handler. Requiring the component handler here is not
330 // the cleanest solution.
331 // We presume the library already to be loaded, which is the case
332 // because it is loaded in the initialization of the logging functionality
335 AliHLTMiscInitCDB_t pFunc=(AliHLTMiscInitCDB_t)pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_INIT_CDB);
338 if (cdbPath && cdbPath[0]!=0) {
340 // very temporary fix, have to check for other formats
341 if (!path.BeginsWith("local://")) {
346 if ((iResult=(*pFunc)(path.Data()))>=0) {
347 if (!(fCDBSetRunNoFunc=pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_SET_CDB_RUNNO))) {
348 Message(NULL, kHLTLogWarning, "AliHLTComponent::InitCDB", "init CDB",
349 "can not find function to set CDB run no");
353 Message(NULL, kHLTLogError, "AliHLTComponent::InitCDB", "init CDB",
354 "can not find initialization function");
363 int AliHLTComponent::SetCDBRunNo(int runNo)
365 // see header file for function documentation
366 if (!fCDBSetRunNoFunc) return 0;
367 return (*((AliHLTMiscSetCDBRunNo_t)fCDBSetRunNoFunc))(runNo);
370 int AliHLTComponent::SetRunDescription(const AliHLTRunDesc* desc, const char* /*runType*/)
372 // see header file for function documentation
373 if (!desc) return -EINVAL;
374 if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
375 HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
380 fpRunDesc=new AliHLTRunDesc;
381 if (!fpRunDesc) return -ENOMEM;
382 *fpRunDesc=kAliHLTVoidRunDesc;
385 if (fpRunDesc->fRunNo!=kAliHLTVoidRunNo && fpRunDesc->fRunNo!=desc->fRunNo) {
386 HLTWarning("Run description has already been set");
389 SetCDBRunNo(fpRunDesc->fRunNo);
390 // TODO: we have to decide about the runType
394 int AliHLTComponent::SetComponentDescription(const char* desc)
396 // see header file for function documentation
400 TString descriptor=desc;
401 TObjArray* pTokens=descriptor.Tokenize(" ");
403 for (int i=0; i<pTokens->GetEntriesFast() && iResult>=0; i++) {
404 TString argument=((TObjString*)pTokens->At(i++))->GetString();
405 if (!argument || argument.IsNull()) continue;
408 if (argument.BeginsWith("chainid")) {
409 argument.ReplaceAll("chainid", "");
410 if (argument.BeginsWith("=")) {
411 fChainId=argument.Replace(0,1,"");
412 fChainIdCrc=CalculateChecksum((const AliHLTUInt8_t*)fChainId.c_str(), fChainId.length());
413 HLTDebug("setting component description: chain id %s crc 0x%8x", fChainId.c_str(), fChainIdCrc);
418 HLTWarning("unknown component description %s", argument.Data());
427 int AliHLTComponent::ConfigureFromArgumentString(int argc, const char** argv)
429 // see header file for function documentation
431 vector<const char*> array;
432 TObjArray choppedArguments;
435 for (i=0; i<argc && iResult>=0; i++) {
437 if (argument.IsNull()) continue;
438 TObjArray* pTokens=argument.Tokenize(" ");
440 if (pTokens->GetEntriesFast()>0) {
441 for (int n=0; n<pTokens->GetEntriesFast(); n++) {
442 choppedArguments.AddLast(pTokens->At(n));
443 TString data=((TObjString*)pTokens->At(n))->GetString();
444 if (!data.IsNull()) {
445 array.push_back(data.Data());
448 pTokens->SetOwner(kFALSE);
454 for (i=0; (unsigned)i<array.size() && iResult>=0;) {
455 int result=ScanConfigurationArgument(array.size()-i, &array[i]);
457 HLTWarning("unknown component argument %s", array[i]);
459 } else if (result>0) {
463 if (iResult==-EINVAL) {
464 HLTError("unknown argument %s", array[i]);
465 } else if (iResult==-EPROTO) {
466 HLTError("missing/wrong parameter for argument %s (%s)", array[i], (array.size()>(unsigned)i+1)?array[i+1]:"missing");
468 HLTError("scan of argument %s failed (%d)", array[i], iResult);
476 int AliHLTComponent::ConfigureFromCDBTObjString(const char* entries)
478 // see header file for function documentation
481 TString confEntries=entries;
482 TObjArray* pTokens=confEntries.Tokenize(" ");
484 for (int n=0; n<pTokens->GetEntriesFast(); n++) {
485 const char* path=((TObjString*)pTokens->At(n))->GetString().Data();
486 const char* chainId=GetChainId();
487 HLTInfo("configure from entry %s, chain id %s", path, (chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
488 TObject* pOCDBObject = LoadAndExtractOCDBObject(path);
490 TObjString* pString=dynamic_cast<TObjString*>(pOCDBObject);
492 HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
493 arguments+=pString->GetString().Data();
496 HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
500 HLTError("can not fetch object \"%s\" from OCDB", path);
506 if (iResult>=0 && !arguments.IsNull()) {
507 const char* array=arguments.Data();
508 iResult=ConfigureFromArgumentString(1, &array);
513 TObject* AliHLTComponent::LoadAndExtractOCDBObject(const char* path, int version, int subVersion)
515 // see header file for function documentation
516 AliCDBEntry* pEntry=AliHLTMisc::Instance().LoadOCDBEntry(path, GetRunNo(), version, subVersion);
517 if (!pEntry) return NULL;
518 return AliHLTMisc::Instance().ExtractObject(pEntry);
521 int AliHLTComponent::DoInit( int /*argc*/, const char** /*argv*/)
523 // default implementation, childs can overload
524 HLTLogKeyword("dummy");
528 int AliHLTComponent::DoDeinit()
530 // default implementation, childs can overload
531 HLTLogKeyword("dummy");
535 int AliHLTComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/)
537 // default implementation, childs can overload
538 HLTLogKeyword("dummy");
542 int AliHLTComponent::ReadPreprocessorValues(const char* /*modules*/)
544 // default implementation, childs can overload
545 HLTLogKeyword("dummy");
549 int AliHLTComponent::ScanConfigurationArgument(int /*argc*/, const char** /*argv*/)
551 // default implementation, childs can overload
552 HLTLogKeyword("dummy");
553 HLTWarning("The function needs to be implemented by the component");
557 int AliHLTComponent::StartOfRun()
559 // default implementation, childs can overload
560 HLTLogKeyword("dummy");
564 int AliHLTComponent::EndOfRun()
566 // default implementation, childs can overload
567 HLTLogKeyword("dummy");
572 int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
574 // default implementation, childs can overload
575 HLTLogKeyword("dummy");
579 void AliHLTComponent::GetOCDBObjectDescription( TMap* const /*targetArray*/)
581 // default implementation, childs can overload
582 HLTLogKeyword("dummy");
585 int AliHLTComponent::CheckOCDBEntries(const TMap* const externList)
587 // check the availability of the OCDB entry descriptions in the TMap
588 // key : complete OCDB path of the entry
589 // value : auxiliary object - short description
590 // if the external map was not provided the function invokes
591 // interface function GetOCDBObjectDescription() to retrieve the list.
594 iResult=AliHLTMisc::Instance().CheckOCDBEntries(externList);
598 pMap->SetOwnerKeyValue(kTRUE);
599 GetOCDBObjectDescription(pMap);
600 iResult=AliHLTMisc::Instance().CheckOCDBEntries(pMap);
609 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
611 // see header file for function documentation
612 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
613 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
614 strcat( output, ":" );
615 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
618 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
620 // see header file for function documentation
626 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
627 sprintf(tmp, "'%d", type.fOrigin[i]);
631 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
632 sprintf(tmp, "%d'", type.fID[i]);
641 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
642 unsigned char* puc=(unsigned char*)type.fOrigin;
644 sprintf(tmp, "'\\%x", type.fOrigin[i]);
646 sprintf(tmp, "'%c", type.fOrigin[i]);
650 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
651 unsigned char* puc=(unsigned char*)type.fID;
653 sprintf(tmp, "\\%x'", type.fID[i]);
655 sprintf(tmp, "%c'", type.fID[i]);
661 if (type==kAliHLTVoidDataType) {
664 // some gymnastics in order to avoid a '0' which is part of either or both
665 // ID and origin terminating the whole string. Unfortunately, string doesn't
666 // stop appending at the '0' if the number of elements to append was
667 // explicitely specified
669 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
670 out.append(tmp.c_str());
673 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
674 out.append(tmp.c_str());
680 void* AliHLTComponent::AllocMemory( unsigned long size )
682 // see header file for function documentation
683 if (fEnvironment.fAllocMemoryFunc)
684 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
685 HLTFatal("no memory allocation handler registered");
689 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
690 AliHLTComponentBlockData** outputBlocks )
692 // see header file for function documentation
693 if ( blockCount==NULL || outputBlocks==NULL )
695 AliHLTUInt32_t count = blocks.size();
699 *outputBlocks = NULL;
702 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
703 if ( !*outputBlocks )
705 for ( unsigned long i = 0; i < count; i++ ) {
706 (*outputBlocks)[i] = blocks[i];
707 if (MatchExactly(blocks[i].fDataType, kAliHLTAnyDataType)) {
708 (*outputBlocks)[i].fDataType=GetOutputDataType();
709 /* data type was set to the output data type by the PubSub AliRoot
710 Wrapper component, if data type of the block was ********:****.
711 Now handled by the component base class in order to have same
712 behavior when running embedded in AliRoot
713 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
714 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
723 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd ) const
725 // see header file for function documentation
726 if (fEnvironment.fGetEventDoneDataFunc)
727 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
731 int AliHLTComponent::ReserveEventDoneData( unsigned long size )
733 // see header file for function documentation
736 unsigned long capacity=fEventDoneDataSize;
737 if (fEventDoneData) capacity-=sizeof(AliHLTComponentEventDoneData)+fEventDoneData->fDataSize;
739 unsigned long newSize=sizeof(AliHLTComponentEventDoneData)+size+(fEventDoneDataSize-capacity);
740 AliHLTComponentEventDoneData* newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[newSize] );
743 newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
744 newEDD->fDataSize = 0;
745 newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
746 if (fEventDoneData) {
747 memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
748 newEDD->fDataSize = fEventDoneData->fDataSize;
749 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
751 fEventDoneData = newEDD;
752 fEventDoneDataSize = newSize;
758 int AliHLTComponent::PushEventDoneData( AliHLTUInt32_t eddDataWord )
760 // see header file for function documentation
763 if (fEventDoneData->fDataSize+sizeof(AliHLTUInt32_t)>fEventDoneDataSize)
765 *reinterpret_cast<AliHLTUInt32_t*>((reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize)) = eddDataWord;
766 fEventDoneData->fDataSize += sizeof(AliHLTUInt32_t);
770 void AliHLTComponent::ReleaseEventDoneData()
772 // see header file for function documentation
774 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
775 fEventDoneData = NULL;
776 fEventDoneDataSize = 0;
780 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
782 // see header file for function documentation
785 AliHLTComponentDataTypeList itypes;
786 AliHLTComponentDataTypeList otypes;
787 otypes.push_back(GetOutputDataType());
788 if (MatchExactly(otypes[0],kAliHLTMultipleDataType)) {
791 if ((count=GetOutputDataTypes(otypes))>0) {
792 } else if (GetComponentType()!=kSink) {
793 HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
796 pConsumer->GetInputDataTypes(itypes);
797 AliHLTComponentDataTypeList::iterator otype=otypes.begin();
798 for (;otype!=otypes.end();otype++) {
799 //PrintDataTypeContent((*otype), "publisher \'%s\'");
800 if ((*otype)==(kAliHLTAnyDataType|kAliHLTDataOriginPrivate)) {
801 if (tgtList) tgtList->push_back(*otype);
806 AliHLTComponentDataTypeList::iterator itype=itypes.begin();
807 for ( ; itype!=itypes.end() && (*itype)!=(*otype) ; itype++) {/* empty body */};
808 //if (itype!=itypes.end()) PrintDataTypeContent(*itype, "consumer \'%s\'");
809 if (itype!=itypes.end()) {
810 if (tgtList) tgtList->push_back(*otype);
820 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format)
822 // see header file for function documentation
823 const char* fmt="\'%s\'";
824 if (format) fmt=format;
825 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, Form(fmt, (DataType2Text(dt)).c_str()));
826 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL,
827 Form("%x %x %x %x %x %x %x %x : %x %x %x %x",
842 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
844 // see header file for function documentation
845 blockData.fStructSize = sizeof(blockData);
846 FillShmData( blockData.fShmKey );
847 blockData.fOffset = ~(AliHLTUInt32_t)0;
848 blockData.fPtr = NULL;
850 FillDataType( blockData.fDataType );
851 blockData.fSpecification = kAliHLTVoidDataSpec;
854 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
856 // see header file for function documentation
857 shmData.fStructSize = sizeof(shmData);
858 shmData.fShmType = gkAliHLTComponentInvalidShmType;
859 shmData.fShmID = gkAliHLTComponentInvalidShmID;
862 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
864 // see header file for function documentation
865 dataType=kAliHLTAnyDataType;
868 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
870 // see header file for function documentation
871 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
872 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
875 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
877 // see header file for function documentation
878 tgtdt.fStructSize=sizeof(AliHLTComponentDataType);
880 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
881 strncpy(&tgtdt.fID[0], id, strlen(id)<(size_t)kAliHLTComponentDataTypefIDsize?strlen(id):kAliHLTComponentDataTypefIDsize);
884 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
885 strncpy(&tgtdt.fOrigin[0], origin, strlen(origin)<(size_t)kAliHLTComponentDataTypefOriginSize?strlen(origin):kAliHLTComponentDataTypefOriginSize);
889 void AliHLTComponent::SetDataType(AliHLTComponentDataType& dt, AliHLTUInt64_t id, AliHLTUInt32_t origin)
891 // see header file for function documentation
892 dt.fStructSize=sizeof(AliHLTComponentDataType);
893 assert(kAliHLTComponentDataTypefIDsize==sizeof(id));
894 assert(kAliHLTComponentDataTypefOriginSize==sizeof(origin));
895 memcpy(&dt.fID, &id, kAliHLTComponentDataTypefIDsize);
896 memcpy(&dt.fOrigin, &origin, kAliHLTComponentDataTypefOriginSize);
899 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
901 // see header file for function documentation
902 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
903 evtData.fStructSize=sizeof(AliHLTComponentEventData);
904 evtData.fEventID=kAliHLTVoidEventID;
907 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
909 // see header file for function documentation
911 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
912 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
913 if (dt.fID[i]!=0) msg+=dt.fID[i];
917 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
918 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
922 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
925 int AliHLTComponent::GetEventCount() const
927 // see header file for function documentation
931 int AliHLTComponent::IncrementEventCounter()
933 // see header file for function documentation
934 if (fEventCount>=0) fEventCount++;
938 int AliHLTComponent::GetNumberOfInputBlocks() const
940 // see header file for function documentation
941 if (fpInputBlocks!=NULL) {
942 return fCurrentEventData.fBlockCnt;
947 AliHLTEventID_t AliHLTComponent::GetEventId() const
949 // see header file for function documentation
950 if (fpInputBlocks!=NULL) {
951 return fCurrentEventData.fEventID;
956 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
957 const char* classname,
960 // see header file for function documentation
961 ALIHLTCOMPONENT_BASE_STOPWATCH();
963 if (classname) fClassName=classname;
964 else fClassName.clear();
965 int idx=FindInputBlock(fSearchDataType, 0, 1);
968 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
969 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
970 fCurrentInputBlock=idx;
977 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
978 const char* dtOrigin,
979 const char* classname,
982 // see header file for function documentation
983 ALIHLTCOMPONENT_BASE_STOPWATCH();
984 AliHLTComponentDataType dt;
985 SetDataType(dt, dtID, dtOrigin);
986 return GetFirstInputObject(dt, classname, bForce);
989 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
991 // see header file for function documentation
992 ALIHLTCOMPONENT_BASE_STOPWATCH();
993 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
994 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
997 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
998 fCurrentInputBlock=idx;
1004 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
1006 // see header file for function documentation
1007 int iResult=-ENOENT;
1008 if (fpInputBlocks!=NULL) {
1009 int idx=startIdx<0?0:startIdx;
1010 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
1011 if (dt!=fpInputBlocks[idx].fDataType) continue;
1014 if (fpInputBlocks[idx].fPtr==NULL) continue;
1015 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
1016 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
1024 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
1026 // see header file for function documentation
1028 if (fpInputBlocks!=NULL) {
1029 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
1030 if (fpInputBlocks[idx].fPtr) {
1031 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
1032 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
1033 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
1034 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
1035 TClass* objclass=msg.GetClass();
1036 pObj=msg.ReadObject(objclass);
1037 if (pObj && objclass) {
1038 HLTDebug("object %p type %s created", pObj, objclass->GetName());
1042 } else if (bForce!=0) {
1043 HLTError("size mismatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
1046 HLTFatal("block descriptor empty");
1049 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
1052 HLTError("no input blocks available");
1058 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
1060 // see header file for function documentation
1061 if (fpInputObjects==NULL) {
1062 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
1065 if (fpInputObjects) {
1066 pObj=fpInputObjects->At(idx);
1068 pObj=CreateInputObject(idx, bForce);
1070 fpInputObjects->AddAt(pObj, idx);
1074 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
1079 int AliHLTComponent::CleanupInputObjects()
1081 // see header file for function documentation
1082 if (!fpInputObjects) return 0;
1083 TObjArray* array=fpInputObjects;
1084 fpInputObjects=NULL;
1085 for (int i=0; i<array->GetEntriesFast(); i++) {
1086 TObject* pObj=array->At(i);
1087 // grrr, garbage collection strikes back: When read via AliHLTMessage
1088 // (CreateInputObject), and written to a TFile afterwards, the
1089 // TFile::Close calls ROOOT's garbage collection. No clue why the
1090 // object ended up in the key list and needs to be deleted
1092 // Matthias 09.11.2008 follow up
1093 // This approach doesn't actually work in all cases: the object table
1094 // can be switched off globally, the flag needs to be checked here as
1095 // well in order to avoid memory leaks.
1096 // This means we have to find another solution for the problem if it
1099 (!TObject::GetObjectStat() || gObjectTable->PtrIsValid(pObj))) {
1107 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
1109 // see header file for function documentation
1110 ALIHLTCOMPONENT_BASE_STOPWATCH();
1111 AliHLTComponentDataType dt=kAliHLTVoidDataType;
1112 int idx=fCurrentInputBlock;
1114 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1116 HLTError("unknown object %p", pObject);
1120 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
1121 dt=fpInputBlocks[idx].fDataType;
1123 HLTFatal("severe internal error, index out of range");
1129 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
1131 // see header file for function documentation
1132 ALIHLTCOMPONENT_BASE_STOPWATCH();
1133 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
1134 int idx=fCurrentInputBlock;
1136 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1138 HLTError("unknown object %p", pObject);
1142 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
1143 iSpec=fpInputBlocks[idx].fSpecification;
1145 HLTFatal("severe internal error, index out of range");
1151 int AliHLTComponent::Forward(const TObject* pObject)
1153 // see header file for function documentation
1155 int idx=fCurrentInputBlock;
1157 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1159 HLTError("unknown object %p", pObject);
1164 fOutputBlocks.push_back(fpInputBlocks[idx]);
1169 int AliHLTComponent::Forward(const AliHLTComponentBlockData* pBlock)
1171 // see header file for function documentation
1173 int idx=fCurrentInputBlock;
1175 if ((idx=FindInputBlock(pBlock))>=0) {
1177 HLTError("unknown Block %p", pBlock);
1182 // check for fpInputBlocks pointer done in FindInputBlock
1183 fOutputBlocks.push_back(fpInputBlocks[idx]);
1188 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
1190 // see header file for function documentation
1191 ALIHLTCOMPONENT_BASE_STOPWATCH();
1194 int idx=FindInputBlock(fSearchDataType, 0);
1195 const AliHLTComponentBlockData* pBlock=NULL;
1197 // check for fpInputBlocks pointer done in FindInputBlock
1198 pBlock=&fpInputBlocks[idx];
1199 fCurrentInputBlock=idx;
1204 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
1205 const char* dtOrigin)
1207 // see header file for function documentation
1208 ALIHLTCOMPONENT_BASE_STOPWATCH();
1209 AliHLTComponentDataType dt;
1210 SetDataType(dt, dtID, dtOrigin);
1211 return GetFirstInputBlock(dt);
1214 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index) const
1216 // see header file for function documentation
1217 ALIHLTCOMPONENT_BASE_STOPWATCH();
1218 assert( 0 <= index and index < (int)fCurrentEventData.fBlockCnt );
1219 return &fpInputBlocks[index];
1222 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
1224 // see header file for function documentation
1225 ALIHLTCOMPONENT_BASE_STOPWATCH();
1226 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
1227 const AliHLTComponentBlockData* pBlock=NULL;
1229 // check for fpInputBlocks pointer done in FindInputBlock
1230 pBlock=&fpInputBlocks[idx];
1231 fCurrentInputBlock=idx;
1236 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
1238 // see header file for function documentation
1239 int iResult=-ENOENT;
1240 if (fpInputBlocks!=NULL) {
1242 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
1243 iResult=(int)(pBlock-fpInputBlocks);
1252 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
1254 // see header file for function documentation
1255 ALIHLTCOMPONENT_BASE_STOPWATCH();
1256 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
1257 int idx=fCurrentInputBlock;
1259 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
1261 HLTError("unknown Block %p", pBlock);
1265 // check for fpInputBlocks pointer done in FindInputBlock
1266 iSpec=fpInputBlocks[idx].fSpecification;
1271 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1272 void* pHeader, int headerSize)
1274 // see header file for function documentation
1275 ALIHLTCOMPONENT_BASE_STOPWATCH();
1278 if (fPushbackPeriod>0) {
1279 // suppress the output
1281 if (fLastPushBackTime<0 || (int)time.Get()-fLastPushBackTime<fPushbackPeriod) return 0;
1284 AliHLTMessage msg(kMESS_OBJECT);
1285 msg.SetCompressionLevel(fCompressionLevel);
1286 msg.WriteObject(pObject);
1287 Int_t iMsgLength=msg.Length();
1289 // Matthias Sep 2008
1290 // NOTE: AliHLTMessage does implement it's own SetLength method
1291 // which is not architecture independent. The original SetLength
1292 // stores the size always in network byte order.
1293 // I'm trying to remember the rational for that, might be that
1294 // it was just some lack of knowledge. Want to change this, but
1295 // has to be done carefullt to be backward compatible.
1296 msg.SetLength(); // sets the length to the first (reserved) word
1298 // does nothing if the level is 0
1301 char *mbuf = msg.Buffer();
1302 if (msg.CompBuffer()) {
1303 msg.SetLength(); // set once more to have to byte order
1304 mbuf = msg.CompBuffer();
1305 iMsgLength = msg.CompLength();
1308 iResult=InsertOutputBlock(mbuf, iMsgLength, dt, spec, pHeader, headerSize);
1310 HLTDebug("object %s (%p) size %d compression %d inserted to output", pObject->ClassName(), pObject, iMsgLength, msg.GetCompressionLevel());
1312 fLastObjectSize=iMsgLength;
1314 HLTError("object serialization failed for object %p", pObject);
1323 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1324 void* pHeader, int headerSize)
1326 // see header file for function documentation
1327 ALIHLTCOMPONENT_BASE_STOPWATCH();
1328 AliHLTComponentDataType dt;
1329 SetDataType(dt, dtID, dtOrigin);
1330 return PushBack(pObject, dt, spec, pHeader, headerSize);
1333 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1334 const void* pHeader, int headerSize)
1336 // see header file for function documentation
1337 ALIHLTCOMPONENT_BASE_STOPWATCH();
1338 if (fPushbackPeriod>0) {
1339 // suppress the output
1341 if (fLastPushBackTime<0 || (int)time.Get()-fLastPushBackTime<fPushbackPeriod) return 0;
1344 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
1347 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1348 const void* pHeader, int headerSize)
1350 // see header file for function documentation
1351 ALIHLTCOMPONENT_BASE_STOPWATCH();
1352 AliHLTComponentDataType dt;
1353 SetDataType(dt, dtID, dtOrigin);
1354 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
1357 int AliHLTComponent::InsertOutputBlock(const void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1358 const void* pHeader, int iHeaderSize)
1360 // see header file for function documentation
1362 int iBlkSize = iBufferSize + iHeaderSize;
1364 if ((pBuffer!=NULL && iBufferSize>0) || (pHeader!=NULL && iHeaderSize>0)) {
1365 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
1366 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1368 // copy header if provided but skip if the header is the target location
1369 // in that case it has already been copied
1370 if (pHeader!=NULL && pHeader!=pTgt) {
1371 memcpy(pTgt, pHeader, iHeaderSize);
1374 pTgt += (AliHLTUInt8_t) iHeaderSize;
1376 // copy buffer if provided but skip if buffer is the target location
1377 // in that case it has already been copied
1378 if (pBuffer!=NULL && pBuffer!=pTgt) {
1379 memcpy(pTgt, pBuffer, iBufferSize);
1381 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
1382 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
1384 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
1386 if (fpOutputBuffer) {
1387 HLTError("too little space in output buffer: %d of %d, required %d", fOutputBufferSize-fOutputBufferFilled, fOutputBufferSize, iBlkSize);
1389 HLTError("output buffer not available");
1395 AliHLTComponentBlockData bd;
1396 FillBlockData( bd );
1397 bd.fOffset = fOutputBufferFilled;
1398 bd.fSize = iBlkSize;
1400 bd.fSpecification = spec;
1401 fOutputBlocks.push_back( bd );
1402 fOutputBufferFilled+=bd.fSize;
1408 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
1410 // see header file for function documentation
1411 if (!pObject) return 0;
1413 AliHLTMessage msg(kMESS_OBJECT);
1414 msg.WriteObject(pObject);
1415 return msg.Length();
1418 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
1419 const char* dtOrigin,
1420 AliHLTUInt32_t spec)
1422 // see header file for function documentation
1423 ALIHLTCOMPONENT_BASE_STOPWATCH();
1424 AliHLTComponentDataType dt;
1425 SetDataType(dt, dtID, dtOrigin);
1426 return CreateMemoryFile(capacity, dt, spec);
1429 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
1430 const AliHLTComponentDataType& dt,
1431 AliHLTUInt32_t spec)
1433 // see header file for function documentation
1434 ALIHLTCOMPONENT_BASE_STOPWATCH();
1435 AliHLTMemoryFile* pFile=NULL;
1436 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
1437 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1438 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
1440 unsigned int nofBlocks=fOutputBlocks.size();
1441 if (nofBlocks+1>fMemFiles.size()) {
1442 fMemFiles.resize(nofBlocks+1, NULL);
1444 if (nofBlocks<fMemFiles.size()) {
1445 fMemFiles[nofBlocks]=pFile;
1446 AliHLTComponentBlockData bd;
1447 FillBlockData( bd );
1448 bd.fOffset = fOutputBufferFilled;
1449 bd.fSize = capacity;
1451 bd.fSpecification = spec;
1452 fOutputBufferFilled+=bd.fSize;
1453 fOutputBlocks.push_back( bd );
1455 HLTError("can not allocate/grow object array");
1456 pFile->CloseMemoryFile(0);
1462 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
1467 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
1468 const char* dtOrigin,
1469 AliHLTUInt32_t spec,
1472 // see header file for function documentation
1473 ALIHLTCOMPONENT_BASE_STOPWATCH();
1474 AliHLTComponentDataType dt;
1475 SetDataType(dt, dtID, dtOrigin);
1476 int size=fOutputBufferSize-fOutputBufferFilled;
1477 if (capacity<0 || capacity>1.0) {
1478 HLTError("invalid parameter: capacity %f", capacity);
1481 size=(int)(size*capacity);
1482 return CreateMemoryFile(size, dt, spec);
1485 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
1486 AliHLTUInt32_t spec,
1489 // see header file for function documentation
1490 ALIHLTCOMPONENT_BASE_STOPWATCH();
1491 int size=fOutputBufferSize-fOutputBufferFilled;
1492 if (capacity<0 || capacity>1.0) {
1493 HLTError("invalid parameter: capacity %f", capacity);
1496 size=(int)(size*capacity);
1497 return CreateMemoryFile(size, dt, spec);
1500 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
1501 const char* key, int option)
1503 // see header file for function documentation
1505 if (pFile && pObject) {
1507 iResult=pObject->Write(key, option);
1511 iResult=-pFile->GetErrno();
1512 if (iResult==-ENOSPC) {
1513 HLTError("error writing memory file, buffer too small");
1522 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
1524 // see header file for function documentation
1527 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1529 while (element!=fMemFiles.end() && iResult>=0) {
1530 if (*element && *element==pFile) {
1531 iResult=pFile->CloseMemoryFile();
1533 // sync memory files and descriptors
1535 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
1543 HLTError("can not find memory file %p", pFile);
1551 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
1553 // see header file for function documentation
1556 AliHLTComponentEventDoneData* newEDD = NULL;
1558 unsigned long newSize=edd.fDataSize;
1560 newSize += fEventDoneData->fDataSize;
1562 if (newSize>fEventDoneDataSize) {
1563 newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[ sizeof(AliHLTComponentEventDoneData)+newSize ] );
1566 newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
1567 newEDD->fDataSize = newSize;
1568 newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
1569 unsigned long long offset = 0;
1570 if (fEventDoneData) {
1571 memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
1572 offset += fEventDoneData->fDataSize;
1574 memcpy( reinterpret_cast<AliHLTUInt8_t*>(newEDD->fData)+offset, edd.fData, edd.fDataSize );
1576 delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
1577 fEventDoneData = newEDD;
1578 fEventDoneDataSize = newSize;
1581 memcpy( reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize, edd.fData, edd.fDataSize );
1582 fEventDoneData->fDataSize += edd.fDataSize;
1587 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1588 const AliHLTComponentBlockData* blocks,
1589 AliHLTComponentTriggerData& trigData,
1590 AliHLTUInt8_t* outputPtr,
1591 AliHLTUInt32_t& size,
1592 AliHLTUInt32_t& outputBlockCnt,
1593 AliHLTComponentBlockData*& outputBlocks,
1594 AliHLTComponentEventDoneData*& edd )
1596 // see header file for function documentation
1597 HLTLogKeyword(fChainId.c_str());
1598 ALIHLTCOMPONENT_BASE_STOPWATCH();
1600 fCurrentEvent=evtData.fEventID;
1601 fCurrentEventData=evtData;
1602 fpInputBlocks=blocks;
1603 fCurrentInputBlock=-1;
1604 fSearchDataType=kAliHLTAnyDataType;
1605 fpOutputBuffer=outputPtr;
1606 fOutputBufferSize=size;
1607 fOutputBufferFilled=0;
1608 fOutputBlocks.clear();
1612 AliHLTComponentBlockDataList forwardedBlocks;
1614 // optional component statistics
1615 AliHLTComponentStatisticsList compStats;
1616 bool bAddComponentTableEntry=false;
1617 vector<AliHLTUInt32_t> parentComponentTables;
1618 #if defined(HLT_COMPONENT_STATISTICS)
1619 if ((fFlags&kDisableComponentStat)==0) {
1620 AliHLTComponentStatistics outputStat;
1621 memset(&outputStat, 0, sizeof(AliHLTComponentStatistics));
1622 outputStat.fStructSize=sizeof(AliHLTComponentStatistics);
1623 outputStat.fId=fChainIdCrc;
1625 fpBenchmark->Stop();
1626 outputStat.fComponentCycleTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
1627 fpBenchmark->Reset();
1628 fpBenchmark->Start();
1630 compStats.push_back(outputStat);
1632 #endif // HLT_COMPONENT_STATISTICS
1634 // data processing is skipped
1635 // - if there are only steering events in the block list.
1636 // For the sake of data source components data processing
1637 // is not skipped if there is no block list at all or if it
1638 // just contains the eventType block
1639 // - always skipped if the event is of type
1640 // - gkAliEventTypeConfiguration
1641 // - gkAliEventTypeReadPreprocessor
1642 const unsigned int skipModeDefault=0x1;
1643 const unsigned int skipModeForce=0x2;
1644 unsigned int bSkipDataProcessing=skipModeDefault;
1646 // find special events
1647 if (fpInputBlocks && evtData.fBlockCnt>0) {
1648 // first look for all special events and execute in the appropriate
1649 // sequence afterwords
1650 int indexComConfEvent=-1;
1651 int indexUpdtDCSEvent=-1;
1652 int indexSOREvent=-1;
1653 int indexEOREvent=-1;
1654 int indexECSParamBlock=-1;
1655 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1656 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1658 // the AliHLTCalibrationProcessor relies on the SOR and EOR events
1659 bSkipDataProcessing&=~skipModeDefault;
1660 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeRunType) {
1662 // handling is not clear yet
1663 if (fpInputBlocks[i].fPtr) {
1664 HLTDebug("got run type \"%s\"\n", fpInputBlocks[i].fPtr);
1666 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1668 // the calibration processor relies on the SOR and EOR events
1669 bSkipDataProcessing&=~skipModeDefault;
1670 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1672 // this event is most likely deprecated
1673 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComConf) {
1674 indexComConfEvent=i;
1675 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeUpdtDCS) {
1676 indexUpdtDCSEvent=i;
1677 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEvent) {
1678 fEventType=fpInputBlocks[i].fSpecification;
1680 // skip always in case of gkAliEventTypeConfiguration
1681 if (fpInputBlocks[i].fSpecification==gkAliEventTypeConfiguration) bSkipDataProcessing|=skipModeForce;
1683 // skip always in case of gkAliEventTypeReadPreprocessor
1684 if (fpInputBlocks[i].fSpecification==gkAliEventTypeReadPreprocessor) bSkipDataProcessing|=skipModeForce;
1686 // never skip if the event type block is the only block
1687 if (evtData.fBlockCnt==1) bSkipDataProcessing&=~skipModeDefault;
1689 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentStatistics) {
1690 if (compStats.size()>0) {
1691 AliHLTUInt8_t* pData=reinterpret_cast<AliHLTUInt8_t*>(fpInputBlocks[i].fPtr);
1692 for (AliHLTUInt32_t offset=0;
1693 offset+sizeof(AliHLTComponentStatistics)<=fpInputBlocks[i].fSize;
1694 offset+=sizeof(AliHLTComponentStatistics)) {
1695 AliHLTComponentStatistics* pStat=reinterpret_cast<AliHLTComponentStatistics*>(pData+offset);
1696 if (pStat && compStats[0].fLevel<=pStat->fLevel) {
1697 compStats[0].fLevel=pStat->fLevel+1;
1699 compStats.push_back(*pStat);
1702 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentTable) {
1703 forwardedBlocks.push_back(fpInputBlocks[i]);
1704 parentComponentTables.push_back(fpInputBlocks[i].fSpecification);
1705 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeECSParam) {
1706 indexECSParamBlock=i;
1708 // the processing function is called if there is at least one
1709 // non-steering data block. Steering blocks are not filtered out
1710 // for sake of performance
1711 bSkipDataProcessing&=~skipModeDefault;
1712 if (compStats.size()>0) {
1713 compStats[0].fInputBlockCount++;
1714 compStats[0].fTotalInputSize+=fpInputBlocks[i].fSize;
1719 if (indexSOREvent>=0) {
1721 bAddComponentTableEntry=true;
1722 if (fpRunDesc==NULL) {
1723 fpRunDesc=new AliHLTRunDesc;
1724 if (fpRunDesc) *fpRunDesc=kAliHLTVoidRunDesc;
1727 AliHLTRunDesc rundesc;
1728 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexSOREvent, "AliHLTRunDesc", "SOR"))>0) {
1729 if (fpRunDesc->fRunNo==kAliHLTVoidRunNo) {
1731 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1732 SetCDBRunNo(fpRunDesc->fRunNo);
1733 } else if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1734 HLTWarning("already set run properties run no %d, ignoring SOR with run no %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1741 if (indexECSParamBlock>=0) {
1742 if (fpInputBlocks[indexECSParamBlock].fSize>0) {
1743 const char* param=reinterpret_cast<const char*>(fpInputBlocks[indexECSParamBlock].fPtr);
1744 TString paramString;
1745 if (param[fpInputBlocks[indexECSParamBlock].fSize-1]!=0) {
1746 HLTWarning("ECS parameter string not terminated");
1747 paramString.Insert(0, param, fpInputBlocks[indexECSParamBlock].fSize);
1752 ScanECSParam(paramString.Data());
1754 HLTWarning("empty ECS parameter received");
1757 // TODO: later on we might throw a warning here since the CTP trigger classes
1758 // should be mandatory
1761 if (indexEOREvent>=0) {
1762 fLastPushBackTime=0; // always send at EOR
1763 bAddComponentTableEntry=true;
1764 if (fpRunDesc!=NULL) {
1766 AliHLTRunDesc rundesc;
1767 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexEOREvent, "AliHLTRunDesc", "SOR"))>0) {
1768 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1769 HLTWarning("run no mismatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1771 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1774 // we do not unload the fpRunDesc struct here in order to have the run information
1775 // available during the event processing
1776 // https://savannah.cern.ch/bugs/?39711
1777 // the info will be cleared in DeInit
1780 HLTWarning("did not receive SOR, ignoring EOR");
1783 if (indexComConfEvent>=0 || fEventType==gkAliEventTypeConfiguration) {
1785 if (indexComConfEvent>=0 && fpInputBlocks[indexComConfEvent].fPtr!=NULL && fpInputBlocks[indexComConfEvent].fSize>0) {
1786 cdbEntry.Append(reinterpret_cast<const char*>(fpInputBlocks[indexComConfEvent].fPtr), fpInputBlocks[indexComConfEvent].fSize);
1788 HLTDebug("received component configuration command: entry %s", cdbEntry.IsNull()?"none":cdbEntry.Data());
1789 int tmpResult=Reconfigure(cdbEntry[0]==0?NULL:cdbEntry.Data(), fChainId.c_str());
1791 HLTWarning("reconfiguration of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1794 if (indexUpdtDCSEvent>=0 || fEventType==gkAliEventTypeReadPreprocessor) {
1796 if (fpInputBlocks[indexUpdtDCSEvent].fPtr!=NULL && fpInputBlocks[indexUpdtDCSEvent].fSize>0) {
1797 modules.Append(reinterpret_cast<const char*>(fpInputBlocks[indexUpdtDCSEvent].fPtr), fpInputBlocks[indexUpdtDCSEvent].fSize);
1799 HLTDebug("received preprocessor update command: detectors %s", modules.IsNull()?"ALL":modules.Data());
1800 int tmpResult=ReadPreprocessorValues(modules[0]==0?"ALL":modules.Data());
1802 HLTWarning("preprocessor update of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1806 // processing function needs to be called if there are no input data
1807 // blocks in order to make data source components working.
1808 bSkipDataProcessing&=~skipModeDefault;
1811 // data processing is not skipped if the component explicitly asks
1812 // for the private blocks
1813 if ((fFlags&kRequireSteeringBlocks)!=0) bSkipDataProcessing=0;
1816 // set the active triggers for this event
1817 fpCTPData->SetTriggers(trigData);
1818 // increment CTP trigger counters if available
1819 if (IsDataEvent()) fpCTPData->Increment(trigData);
1822 AliHLTComponentBlockDataList blockData;
1823 if (iResult>=0 && !bSkipDataProcessing)
1824 { // dont delete, sets the scope for the stopwatch guard
1825 // do not use ALIHLTCOMPONENT_DA_STOPWATCH(); macro
1826 // in order to avoid 'shadowed variable' warning
1827 AliHLTStopwatchGuard swguard2(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)kSWDA)):NULL);
1828 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1829 } // end of the scope of the stopwatch guard
1830 if (iResult>=0 && !bSkipDataProcessing) {
1831 if (fOutputBlocks.size()>0) {
1832 // High Level interface
1834 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1835 // sync memory files and descriptors
1836 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1838 while (element!=fMemFiles.end() && iResult>=0) {
1840 if ((*element)->IsClosed()==0) {
1841 HLTWarning("memory file has not been closed, force flush");
1842 iResult=CloseMemoryFile(*element);
1849 // create the descriptor list
1850 if (blockData.size()>0) {
1851 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1854 if (compStats.size()>0 && IsDataEvent()) {
1855 int offset=AddComponentStatistics(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, compStats);
1856 if (offset>0) fOutputBufferFilled+=offset;
1858 if (bAddComponentTableEntry) {
1859 int offset=AddComponentTableEntry(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, parentComponentTables);
1860 if (offset>0) size+=offset;
1862 if (forwardedBlocks.size()>0) {
1863 fOutputBlocks.insert(fOutputBlocks.end(), forwardedBlocks.begin(), forwardedBlocks.end());
1865 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1866 size=fOutputBufferFilled;
1870 // Low Level interface
1871 if (compStats.size()>0) {
1872 int offset=AddComponentStatistics(blockData, fpOutputBuffer, fOutputBufferSize, size, compStats);
1873 if (offset>0) size+=offset;
1875 if (bAddComponentTableEntry) {
1876 int offset=AddComponentTableEntry(blockData, fpOutputBuffer, fOutputBufferSize, size, parentComponentTables);
1877 if (offset>0) size+=offset;
1879 if (forwardedBlocks.size()>0) {
1880 blockData.insert(blockData.end(), forwardedBlocks.begin(), forwardedBlocks.end());
1882 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1885 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1888 if (iResult<0 || bSkipDataProcessing) {
1892 CleanupInputObjects();
1893 if (iResult>=0 && IsDataEvent()) {
1894 IncrementEventCounter();
1896 if (outputBlockCnt==0) {
1897 // no output blocks, set size to 0
1901 // reset the internal EventData struct
1902 FillEventData(fCurrentEventData);
1904 // reset the active triggers
1905 if (fpCTPData) fpCTPData->SetTriggers(0);
1907 // set the time for the pushback period
1908 if (fPushbackPeriod>0) {
1909 // suppress the output
1911 if (fLastPushBackTime<0 || (int)time.Get()-fLastPushBackTime>=fPushbackPeriod) {
1912 fLastPushBackTime=time.Get();
1919 int AliHLTComponent::AddComponentStatistics(AliHLTComponentBlockDataList& blocks,
1920 AliHLTUInt8_t* buffer,
1921 AliHLTUInt32_t bufferSize,
1922 AliHLTUInt32_t offset,
1923 AliHLTComponentStatisticsList& stats) const
1925 // see header file for function documentation
1927 if ((fFlags&kDisableComponentStat)!=0) return 0;
1928 #if defined(HLT_COMPONENT_STATISTICS)
1929 if (stats.size()==0) return -ENOENT;
1930 // check if there is space for at least one entry
1931 if (offset+sizeof(AliHLTComponentStatistics)>bufferSize) return 0;
1932 stats[0].fTotalOutputSize=offset;
1933 stats[0].fOutputBlockCount=blocks.size();
1935 fpBenchmark->Stop();
1936 stats[0].fTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
1937 stats[0].fCTime=(AliHLTUInt32_t)(fpBenchmark->CpuTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
1938 fpBenchmark->Continue();
1940 if (offset+stats.size()*sizeof(AliHLTComponentStatistics)>bufferSize) {
1941 AliHLTUInt32_t removedLevel=0;
1943 // remove all entries of the level of the last entry
1944 removedLevel=stats.back().fLevel;
1945 AliHLTComponentStatisticsList::iterator element=stats.begin();
1947 while (element!=stats.end()) {
1948 if (element->fLevel<=removedLevel) {
1949 element=stats.erase(element);
1954 } while (stats.size()>1 &&
1955 (offset+stats.size()*sizeof(AliHLTComponentStatistics)>bufferSize));
1957 assert(stats.size()>0);
1958 if (stats.size()==0) return 0;
1960 if (offset+stats.size()*sizeof(AliHLTComponentStatistics)<=bufferSize) {
1961 AliHLTComponentBlockData bd;
1962 FillBlockData( bd );
1963 bd.fOffset = offset;
1964 bd.fSize = stats.size()*sizeof(AliHLTComponentStatistics);
1965 bd.fDataType = kAliHLTDataTypeComponentStatistics;
1966 bd.fSpecification = kAliHLTVoidDataSpec;
1967 unsigned int master=0;
1968 for (unsigned int i=1; i<blocks.size(); i++) {
1969 if (blocks[i].fSize>blocks[master].fSize &&
1970 !MatchExactly(blocks[i].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate))
1973 if (blocks.size()>0 && !MatchExactly(blocks[master].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate)) {
1974 // take the data origin of the biggest block as specification
1975 // this is similar to the treatment in the HOMER interface. For traditional
1976 // reasons, the bytes are swapped there on a little endian architecture, so
1977 // we do it as well.
1978 memcpy(&bd.fSpecification, &blocks[master].fDataType.fOrigin, sizeof(bd.fSpecification));
1979 #ifdef R__BYTESWAP // set on little endian architectures
1980 bd.fSpecification=((bd.fSpecification & 0xFFULL) << 24) |
1981 ((bd.fSpecification & 0xFF00ULL) << 8) |
1982 ((bd.fSpecification & 0xFF0000ULL) >> 8) |
1983 ((bd.fSpecification & 0xFF000000ULL) >> 24);
1986 memcpy(buffer+offset, &(stats[0]), bd.fSize);
1987 blocks.push_back(bd);
1991 if (blocks.size() && buffer && bufferSize && offset && stats.size()) {
1992 // get rid of warning
1994 #endif // HLT_COMPONENT_STATISTICS
1998 int AliHLTComponent::AddComponentTableEntry(AliHLTComponentBlockDataList& blocks,
1999 AliHLTUInt8_t* buffer,
2000 AliHLTUInt32_t bufferSize,
2001 AliHLTUInt32_t offset,
2002 const vector<AliHLTUInt32_t>& parents) const
2004 // see header file for function documentation
2006 if ((fFlags&kDisableComponentStat)!=0) return 0;
2007 #if defined(HLT_COMPONENT_STATISTICS)
2008 // the payload consists of the AliHLTComponentTableEntry struct,
2009 // followed by a an array of 32bit crc chain ids and the component
2010 // description string
2011 unsigned int payloadSize=sizeof(AliHLTComponentTableEntry);
2012 payloadSize+=parents.size()*sizeof(AliHLTUInt32_t);
2014 // the component description has the following format:
2015 // chain-id{component-id:arguments}
2016 const char* componentId=const_cast<AliHLTComponent*>(this)->GetComponentID();
2017 unsigned int descriptionSize=fChainId.size()+1;
2018 descriptionSize+=2; // the '{}' around the component id
2019 descriptionSize+=strlen(componentId);
2020 descriptionSize+=1; // the ':' between component id and arguments
2021 descriptionSize+=fComponentArgs.size();
2023 payloadSize+=descriptionSize;
2024 if (buffer && (offset+payloadSize<=bufferSize)) {
2025 AliHLTUInt8_t* pTgt=buffer+offset;
2026 memset(pTgt, 0, payloadSize);
2029 AliHLTComponentTableEntry* pEntry=reinterpret_cast<AliHLTComponentTableEntry*>(pTgt);
2030 pEntry->fStructSize=sizeof(AliHLTComponentTableEntry);
2031 pEntry->fNofParents=parents.size();
2032 pEntry->fSizeDescription=descriptionSize;
2033 pTgt=pEntry->fBuffer;
2035 // write array of parents
2036 if (parents.size()>0) {
2037 unsigned int copy=parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type);
2038 memcpy(pTgt, &parents[0], parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type));
2042 // write component description
2043 memcpy(pTgt, fChainId.c_str(), fChainId.size());
2044 pTgt+=fChainId.size();
2046 memcpy(pTgt, componentId, strlen(componentId));
2047 pTgt+=strlen(componentId);
2049 memcpy(pTgt, fComponentArgs.c_str(), fComponentArgs.size());
2050 pTgt+=fComponentArgs.size();
2054 AliHLTComponentBlockData bd;
2055 FillBlockData( bd );
2056 bd.fOffset = offset;
2057 bd.fSize = payloadSize;
2058 bd.fDataType = kAliHLTDataTypeComponentTable;
2059 bd.fSpecification = fChainIdCrc;
2060 blocks.push_back(bd);
2064 if (blocks.size() && buffer && bufferSize && offset && parents.size()) {
2065 // get rid of warning
2067 #endif // HLT_COMPONENT_STATISTICS
2071 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
2076 // standard constructor (not for use)
2079 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
2081 fpStopwatch(pStopwatch),
2086 // check for already existing guard
2087 if (fgpCurrent) fpPrec=fgpCurrent;
2090 // stop the preceeding guard if it controls a different stopwatch
2092 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
2094 // start the stopwatch if the current guard controls a different one
2095 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
2098 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
2104 // copy constructor not for use
2108 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
2111 // assignment operator not for use
2118 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
2120 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
2124 // resume the preceeding guard if it controls a different stopwatch
2126 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
2128 // stop the stopwatch if the current guard controls a different one
2129 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
2131 // resume to the preceeding guard
2135 int AliHLTComponent::AliHLTStopwatchGuard::Hold(const TStopwatch* pSucc)
2137 // see header file for function documentation
2138 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
2139 return fpStopwatch!=pSucc?1:0;
2142 int AliHLTComponent::AliHLTStopwatchGuard::Resume(const TStopwatch* pSucc)
2144 // see header file for function documentation
2145 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
2146 return fpStopwatch!=pSucc?1:0;
2149 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
2151 // see header file for function documentation
2153 if (pSW!=NULL && type<kSWTypeCount) {
2154 if (fpStopwatches) {
2155 TObject* pObj=fpStopwatches->At((int)type);
2156 if (pSW==NULL // explicit reset
2157 || pObj==NULL) { // explicit set
2158 fpStopwatches->AddAt(pSW, (int)type);
2159 } else if (pObj!=pSW) {
2160 HLTWarning("stopwatch %d already set, reset first", (int)type);
2170 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
2172 // see header file for function documentation
2173 if (pStopwatches==NULL) return -EINVAL;
2176 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntriesFast(); i++)
2177 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
2181 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
2183 // see header file for function documentation
2185 // 2010-02-11 OCDB is now the reliable source for the run number, it is
2186 // initialized either by aliroot or the external interface depending
2187 // on the environment. It turned out that the rundescriptor is not set
2188 // in the aliroot mode, resulting in an invalid run number. However this
2189 // did not cause problems until now. OCDB initialization has been revised
2190 // already in 10/2009. This was a remnant.
2191 // Have to check whether we get rid of the rundescriptor at some point.
2192 if (fpRunDesc==NULL) return AliHLTMisc::Instance().GetCDBRunNo();
2193 if (fpRunDesc->fRunNo!=(unsigned)AliHLTMisc::Instance().GetCDBRunNo()) {
2194 HLTWarning("run number mismatch: ocdb %d run descriptor %d", AliHLTMisc::Instance().GetCDBRunNo(), fpRunDesc->fRunNo);
2196 return fpRunDesc->fRunNo;
2199 AliHLTUInt32_t AliHLTComponent::GetRunType() const
2201 // see header file for function documentation
2202 if (fpRunDesc==NULL) return kAliHLTVoidRunType;
2203 return fpRunDesc->fRunType;
2206 AliHLTUInt32_t AliHLTComponent::GetTimeStamp() const
2208 // see header file for function documentation
2209 if (fCurrentEventData.fEventCreation_s) {
2210 return fCurrentEventData.fEventCreation_s;
2212 // using the actual UTC if the time stamp was not set by the framework
2213 return static_cast<AliHLTUInt32_t>(time(NULL));
2216 AliHLTUInt32_t AliHLTComponent::GetPeriodNumber() const
2218 // see header file for function documentation
2219 return (GetEventId()>>36)&0xfffffff;
2222 AliHLTUInt32_t AliHLTComponent::GetOrbitNumber() const
2224 // see header file for function documentation
2225 return (GetEventId()>>12)&0xffffff;
2228 AliHLTUInt16_t AliHLTComponent::GetBunchCrossNumber() const
2230 // see header file for function documentation
2231 return GetEventId()&0xfff;
2234 bool AliHLTComponent::IsDataEvent(AliHLTUInt32_t* pTgt) const
2236 // see header file for function documentation
2237 if (pTgt) *pTgt=fEventType;
2238 return (fEventType==gkAliEventTypeData ||
2239 fEventType==gkAliEventTypeDataReplay ||
2240 fEventType==gkAliEventTypeCalibration);
2243 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
2244 const char* structname, const char* eventname)
2246 // see header file for function documentation
2248 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
2249 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
2250 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
2251 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
2252 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
2253 if (fpInputBlocks[iBlockNo].fSize!=copy) {
2254 HLTWarning("%s event: mismatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
2255 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
2257 if (copy!=iStructSize) {
2258 HLTWarning("%s event: mismatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
2259 if (copy>iStructSize) {
2262 memset(pTgt, 0, iStructSize);
2265 memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
2269 HLTWarning("%s event: missing data block", eventname);
2275 HLTError("invalid struct");
2281 AliHLTUInt32_t AliHLTComponent::CalculateChecksum(const AliHLTUInt8_t* buffer, int size)
2283 // see header file for function documentation
2284 AliHLTUInt32_t remainder = 0;
2285 const AliHLTUInt8_t crcwidth=(8*sizeof(AliHLTUInt32_t));
2286 const AliHLTUInt32_t topbit=1 << (crcwidth-1);
2287 const AliHLTUInt32_t polynomial=0xD8; /* 11011 followed by 0's */
2290 // http://www.netrino.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
2293 * Perform modulo-2 division, a byte at a time.
2295 for (int byte = 0; byte < size; ++byte)
2298 * Bring the next byte into the remainder.
2300 remainder ^= (buffer[byte] << (crcwidth - 8));
2303 * Perform modulo-2 division, a bit at a time.
2305 for (uint8_t bit = 8; bit > 0; --bit)
2308 * Try to divide the current data bit.
2310 if (remainder & topbit)
2312 remainder = (remainder << 1) ^ polynomial;
2316 remainder = (remainder << 1);
2322 * The final remainder is the CRC result.
2327 int AliHLTComponent::ExtractComponentTableEntry(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size,
2328 string& retChainId, string& retCompId, string& retCompArgs,
2329 vector<AliHLTUInt32_t>& parents)
2331 // see header file for function documentation
2334 retCompArgs.clear();
2336 if (!pBuffer || size==0) return 0;
2338 const AliHLTComponentTableEntry* pEntry=reinterpret_cast<const AliHLTComponentTableEntry*>(pBuffer);
2339 if (size<8/* the initial size of the structure*/ ||
2340 pEntry==NULL || pEntry->fStructSize<8) return -ENOMSG;
2341 const AliHLTUInt32_t* pParents=reinterpret_cast<const AliHLTUInt32_t*>(pEntry->fBuffer);
2342 const AliHLTUInt8_t* pEnd=pBuffer+size;
2344 if (pParents+pEntry->fNofParents>=reinterpret_cast<const AliHLTUInt32_t*>(pEnd)) return -ENODEV;
2345 for (unsigned int i=0; i<pEntry->fNofParents; i++, pParents++) {
2346 parents.push_back(*pParents);
2349 const char* pDescription=reinterpret_cast<const char*>(pParents);
2350 if (pDescription+pEntry->fSizeDescription>=reinterpret_cast<const char*>(pEnd) ||
2351 *(pDescription+pEntry->fSizeDescription)!=0) {
2355 TString descriptor=reinterpret_cast<const char*>(pDescription);
2359 TObjArray* pTokens=descriptor.Tokenize("{");
2362 if (pTokens->GetEntriesFast()>n) {
2363 retChainId=((TObjString*)pTokens->At(n++))->GetString();
2365 if (pTokens->GetEntriesFast()>n) {
2366 compId=((TObjString*)pTokens->At(n++))->GetString();
2370 if (!compId.IsNull() && (pTokens=compId.Tokenize(":"))!=NULL) {
2372 if (pTokens->GetEntriesFast()>n) {
2373 compId=((TObjString*)pTokens->At(n++))->GetString();
2375 if (pTokens->GetEntriesFast()>n) {
2376 compArgs=((TObjString*)pTokens->At(n++))->GetString();
2380 compId.ReplaceAll("}", "");
2381 compArgs.ReplaceAll("}", "");
2384 retCompArgs=compArgs;
2386 if (retChainId.size()==0) return -ENODATA;
2391 int AliHLTComponent::ExtractTriggerData(
2392 const AliHLTComponentTriggerData& trigData,
2393 const AliHLTUInt8_t (**attributes)[gkAliHLTBlockDAttributeCount],
2394 AliHLTUInt64_t* status,
2395 const AliRawDataHeader** cdh,
2396 AliHLTReadoutList* readoutlist,
2400 // see header file for function documentation
2402 // Check that the trigger data structure is the correct size.
2403 if (trigData.fStructSize != sizeof(AliHLTComponentTriggerData))
2408 log.LoggingVarargs(kHLTLogError, Class_Name(), FUNCTIONNAME(), __FILE__, __LINE__,
2409 "Invalid trigger structure size: %d but expected %d.", trigData.fStructSize, sizeof(AliHLTComponentTriggerData)
2415 // Get the size of the AliHLTEventTriggerData structure without the readout list part.
2416 // The way we do this here should also handle memory alignment correctly.
2417 AliHLTEventTriggerData* dummy = NULL;
2418 size_t sizeWithoutReadout = (char*)(&dummy->fReadoutList) - (char*)(dummy);
2420 // Check that the trigger data pointer points to data of a size we can handle.
2421 // Either it is the size of AliHLTEventTriggerData or the size of the old
2422 // version of AliHLTEventTriggerData using AliHLTEventDDLV0.
2423 if (trigData.fDataSize != sizeof(AliHLTEventTriggerData) and
2424 trigData.fDataSize != sizeWithoutReadout + sizeof(AliHLTEventDDLV0)
2430 log.LoggingVarargs(kHLTLogError, Class_Name(), FUNCTIONNAME(), __FILE__, __LINE__,
2431 "Invalid trigger data size: %d but expected %d.", trigData.fDataSize, sizeof(AliHLTEventTriggerData)
2437 AliHLTEventTriggerData* evtData = reinterpret_cast<AliHLTEventTriggerData*>(trigData.fData);
2438 assert(evtData != NULL);
2440 // Check that the CDH has 8 words.
2441 if (cdh != NULL and evtData->fCommonHeaderWordCnt != 8)
2446 log.LoggingVarargs(kHLTLogError, Class_Name(), FUNCTIONNAME(), __FILE__, __LINE__,
2447 "Common Data Header (CDH) has wrong number of data words: %d but expected %d",
2448 evtData->fCommonHeaderWordCnt, sizeof(AliRawDataHeader)/sizeof(AliHLTUInt32_t)
2454 // Check that the readout list has the correct count of words. i.e. something we can handle,
2455 if (readoutlist != NULL and
2456 evtData->fReadoutList.fCount != (unsigned)gkAliHLTDDLListSizeV0 and
2457 evtData->fReadoutList.fCount != (unsigned)gkAliHLTDDLListSizeV1
2463 log.LoggingVarargs(kHLTLogError, Class_Name(), FUNCTIONNAME(), __FILE__, __LINE__,
2464 "Readout list structure has wrong number of data words: %d but expected %d",
2465 evtData->fReadoutList.fCount, gkAliHLTDDLListSize
2471 if (attributes != NULL)
2473 *attributes = &evtData->fAttributes;
2477 *status = evtData->fHLTStatus;
2481 const AliRawDataHeader* cdhptr = reinterpret_cast<const AliRawDataHeader*>(&evtData->fCommonHeader);
2484 if (readoutlist != NULL)
2486 *readoutlist = AliHLTReadoutList(evtData->fReadoutList);
2491 int AliHLTComponent::LoggingVarargs(AliHLTComponentLogSeverity severity,
2492 const char* originClass, const char* originFunc,
2493 const char* file, int line, ... ) const
2495 // see header file for function documentation
2499 va_start(args, line);
2501 // logging function needs to be const in order to be called from const member functions
2502 // without problems. But at this point we face the problem with virtual members which
2503 // are not necessarily const.
2504 AliHLTComponent* nonconst=const_cast<AliHLTComponent*>(this);
2505 AliHLTLogging::SetLogString(this, ", %p", "%s (%s_pfmt_): ",
2506 fChainId[0]!=0?fChainId.c_str():nonconst->GetComponentID(),
2507 nonconst->GetComponentID());
2508 iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args, true /*append*/));
2514 int AliHLTComponent::ScanECSParam(const char* ecsParam)
2516 // see header file for function documentation
2518 // format of the parameter string from ECS
2519 // <command>;<parameterkey>=<parametervalue>;<parameterkey>=<parametervalue>;...
2520 // search for a subset of the parameterkeys
2524 // CTP_TRIGGER_CLASS=
2525 // DATA_FORMAT_VERSION=
2527 // HLT_OUT_DDL_LIST=
2528 // HLT_TRIGGER_CODE=
2531 // The command apears not to be sent by the online framework
2533 TString string=ecsParam;
2534 TObjArray* parameter=string.Tokenize(";");
2536 for (int i=0; i<parameter->GetEntriesFast(); i++) {
2537 TString entry=((TObjString*)parameter->At(i))->GetString();
2538 HLTDebug("scanning ECS entry: %s", entry.Data());
2539 TObjArray* entryParams=entry.Tokenize("=");
2541 if (entryParams->GetEntriesFast()>1) {
2542 if ((((TObjString*)entryParams->At(0))->GetString()).CompareTo("CTP_TRIGGER_CLASS")==0) {
2543 int result=InitCTPTriggerClasses((((TObjString*)entryParams->At(1))->GetString()).Data());
2544 if (iResult>=0 && result<0) iResult=result;
2546 // TODO: scan the other parameters
2547 // e.g. consistency check of run number
2559 int AliHLTComponent::SetupCTPData()
2561 // see header file for function documentation
2562 if (fpCTPData) delete fpCTPData;
2563 fpCTPData=new AliHLTCTPData;
2564 if (!fpCTPData) return -ENOMEM;
2568 int AliHLTComponent::InitCTPTriggerClasses(const char* ctpString)
2570 // see header file for function documentation
2571 if (!fpCTPData) return 0; // silently accept as the component has to announce that it want's the CTP info
2572 return fpCTPData->InitCTPTriggerClasses(ctpString);
2575 bool AliHLTComponent::EvaluateCTPTriggerClass(const char* expression, AliHLTComponentTriggerData& trigData) const
2577 // see header file for function documentation
2579 static bool bWarningThrown=false;
2580 if (!bWarningThrown) HLTError("Trigger classes not initialized, use SetupCTPData from DoInit()");
2581 bWarningThrown=true;
2585 return fpCTPData->EvaluateCTPTriggerClass(expression, trigData);
2588 int AliHLTComponent::CheckCTPTrigger(const char* name) const
2590 // see header file for function documentation
2592 static bool bWarningThrown=false;
2593 if (!bWarningThrown) HLTError("Trigger classes not initialized, use SetupCTPData from DoInit()");
2594 bWarningThrown=true;
2598 return fpCTPData->CheckTrigger(name);
2601 Double_t AliHLTComponent::GetBz()
2604 return AliHLTMisc::Instance().GetBz();
2607 Double_t AliHLTComponent::GetBz(const Double_t *r)
2609 // Returns Bz (kG) at the point "r" .
2610 return AliHLTMisc::Instance().GetBz(r);
2613 void AliHLTComponent::GetBxByBz(const Double_t r[3], Double_t b[3])
2615 // Returns Bx, By and Bz (kG) at the point "r" .
2616 AliHLTMisc::Instance().GetBxByBz(r, b);