initialization of CDB in wrapper interface;added treatment of reconfiguration event
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponent.cxx
CommitLineData
f23a6e1a 1// $Id$
2
3/**************************************************************************
9be2600f 4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
f23a6e1a 6 * *
9be2600f 7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * Timm Steinbeck <timm@kip.uni-heidelberg.de> *
9 * for The ALICE HLT Project. *
f23a6e1a 10 * *
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 **************************************************************************/
19
bfccbf68 20/** @file AliHLTComponent.cxx
21 @author Matthias Richter, Timm Steinbeck
22 @date
23 @brief Base class implementation for HLT components. */
f23a6e1a 24
3a7c0444 25// see header file for class documentation
26// or
27// refer to README to build package
28// or
29// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
0c0c9d99 31#if __GNUC__>= 3
f23a6e1a 32using namespace std;
33#endif
34
66043029 35//#include "AliHLTStdIncludes.h"
f23a6e1a 36#include "AliHLTComponent.h"
37#include "AliHLTComponentHandler.h"
a655eae3 38#include "AliHLTMessage.h"
70ed7d01 39#include "TString.h"
ed504011 40#include "TMath.h"
a655eae3 41#include "TObjArray.h"
79c114b5 42#include "TObjectTable.h"
a655eae3 43#include "TClass.h"
90ebac25 44#include "TStopwatch.h"
79c114b5 45#include "AliHLTMemoryFile.h"
579d9eb7 46#include "AliHLTMisc.h"
ec25e4ca 47#include <cassert>
f23a6e1a 48
b22e91eb 49/** ROOT macro for the implementation of ROOT specific class methods */
90ebac25 50ClassImp(AliHLTComponent);
51
52/** stopwatch macro using the stopwatch guard */
53#define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
54//#define ALIHLTCOMPONENT_STOPWATCH(type)
55
56/** stopwatch macro for operations of the base class */
57#define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
58/** stopwatch macro for operations of the detector algorithm (DA) */
59#define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
f23a6e1a 60
f23a6e1a 61AliHLTComponent::AliHLTComponent()
85869391 62 :
53feaef5 63 fEnvironment(),
3cde846d 64 fCurrentEvent(0),
a655eae3 65 fEventCount(-1),
66 fFailedEvents(0),
67 fCurrentEventData(),
68 fpInputBlocks(NULL),
69 fCurrentInputBlock(-1),
70 fSearchDataType(kAliHLTVoidDataType),
71 fClassName(),
72 fpInputObjects(NULL),
73 fpOutputBuffer(NULL),
74 fOutputBufferSize(0),
75 fOutputBufferFilled(0),
90ebac25 76 fOutputBlocks(),
79c114b5 77 fpStopwatches(new TObjArray(kSWTypeCount)),
559631d5 78 fMemFiles(),
79 fpRunDesc(NULL),
579d9eb7 80 fpDDLList(NULL),
81 fCDBInitialized(false),
82 fChainId()
70ed7d01 83{
84 // see header file for class documentation
85 // or
86 // refer to README to build package
87 // or
88 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
f23a6e1a 89 memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
70ed7d01 90 if (fgpComponentHandler)
91 fgpComponentHandler->ScheduleRegister(this);
7c781d33 92 //SetLocalLoggingLevel(kHLTLogDefault);
70ed7d01 93}
94
f23a6e1a 95AliHLTComponent::~AliHLTComponent()
96{
70ed7d01 97 // see header file for function documentation
8451168b 98 CleanupInputObjects();
4498d7d1 99 if (fpStopwatches!=NULL) delete fpStopwatches;
100 fpStopwatches=NULL;
2be3f004 101 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
79c114b5 102 while (element!=fMemFiles.end()) {
103 if (*element) {
104 if ((*element)->IsClosed()==0) {
105 HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
106 // close but do not flush as we dont know whether the buffer is still valid
29312178 107 (*element)->CloseMemoryFile(0);
79c114b5 108 }
109 delete *element;
110 *element=NULL;
111 }
112 element++;
113 }
f23a6e1a 114}
115
70ed7d01 116AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
b22e91eb 117
85869391 118int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite)
119{
70ed7d01 120 // see header file for function documentation
85869391 121 int iResult=0;
70ed7d01 122 if (fgpComponentHandler==NULL || bOverwrite!=0)
123 fgpComponentHandler=pCH;
85869391 124 else
125 iResult=-EPERM;
126 return iResult;
127}
128
70ed7d01 129int AliHLTComponent::UnsetGlobalComponentHandler()
130{
131 // see header file for function documentation
85869391 132 return SetGlobalComponentHandler(NULL,1);
133}
134
70ed7d01 135int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
f23a6e1a 136{
70ed7d01 137 // see header file for function documentation
f23a6e1a 138 int iResult=0;
139 if (environ) {
140 memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
70ed7d01 141 fEnvironment.fParam=environParam;
f23a6e1a 142 }
8451168b 143 const char** pArguments=NULL;
144 int iNofChildArgs=0;
145 TString argument="";
146 int bMissingParam=0;
147 if (argc>0) {
148 pArguments=new const char*[argc];
149 if (pArguments) {
150 for (int i=0; i<argc && iResult>=0; i++) {
151 argument=argv[i];
152 if (argument.IsNull()) continue;
153
154 // benchmark
155 if (argument.CompareTo("benchmark")==0) {
156
157 // loglevel
158 } else if (argument.CompareTo("loglevel")==0) {
159 if ((bMissingParam=(++i>=argc))) break;
160 TString parameter(argv[i]);
161 parameter.Remove(TString::kLeading, ' '); // remove all blanks
162 if (parameter.BeginsWith("0x") &&
163 parameter.Replace(0,2,"",0).IsHex()) {
164 AliHLTComponentLogSeverity loglevel=kHLTLogNone;
7a5ccd96 165 sscanf(parameter.Data(),"%x", (unsigned int*)&loglevel);
8451168b 166 SetLocalLoggingLevel(loglevel);
167 } else {
168 HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
169 iResult=-EINVAL;
170 }
171 } else {
172 pArguments[iNofChildArgs++]=argv[i];
173 }
174 }
175 } else {
176 iResult=-ENOMEM;
177 }
178 }
179 if (bMissingParam) {
180 HLTError("missing parameter for argument %s", argument.Data());
181 iResult=-EINVAL;
182 }
183 if (iResult>=0) {
184 iResult=DoInit(iNofChildArgs, pArguments);
185 }
3cde846d 186 if (iResult>=0) fEventCount=0;
8451168b 187 if (pArguments) delete [] pArguments;
f23a6e1a 188 return iResult;
189}
190
191int AliHLTComponent::Deinit()
192{
70ed7d01 193 // see header file for function documentation
f23a6e1a 194 int iResult=0;
195 iResult=DoDeinit();
559631d5 196 if (fpRunDesc) {
197 HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
198 AliHLTRunDesc* pRunDesc=fpRunDesc;
199 fpRunDesc=NULL;
200 delete pRunDesc;
201 }
579d9eb7 202 fEventCount=0;
f23a6e1a 203 return iResult;
204}
fa2e9b7c 205
579d9eb7 206int AliHLTComponent::InitCDB(const char* cdbPath)
53feaef5 207{
579d9eb7 208 int iResult=0;
209 // we presume the library already to be loaded
210 // find the symbol
211 AliHLTComponentHandler cHandler;
212 AliHLTMiscInitCDB_t pFunc=(AliHLTMiscInitCDB_t)cHandler.FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_INIT_CDB);
213 if (pFunc) {
214 iResult=(*pFunc)(cdbPath);
215 fCDBInitialized=iResult>=0;
216 } else {
217 Message(NULL, kHLTLogError, "AliHLTComponent::InitCDB", "init CDB",
218 "can not find initialization function");
219 iResult=-ENOSYS;
53feaef5 220 }
579d9eb7 221 return iResult;
222}
223
224int AliHLTComponent::SetCDBRunNo(int runNo)
225{
226 int iResult=0;
227 if (!fCDBInitialized) return iResult;
228 // we presume the library already to be loaded
229 // find the symbol
230 AliHLTComponentHandler cHandler;
231 AliHLTMiscSetCDBRunNo_t pFunc=(AliHLTMiscSetCDBRunNo_t)cHandler.FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_SET_CDB_RUNNO);
232 if (pFunc) {
233 iResult=(*pFunc)(runNo);
234 } else {
235 Message(NULL, kHLTLogError, "AliHLTComponent::SetCDBRunNo", "init CDB",
236 "can not find initialization function");
237 iResult=-ENOSYS;
238 }
239 return iResult;
240}
241
242int AliHLTComponent::DoInit( int /*argc*/, const char** /*argv*/)
243{
244 // default implementation, childs can overload
53feaef5 245 return 0;
246}
247
248int AliHLTComponent::DoDeinit()
249{
579d9eb7 250 // default implementation, childs can overload
251 return 0;
252}
253
254int AliHLTComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/)
255{
256 // default implementation, childs can overload
53feaef5 257 return 0;
258}
259
2be3f004 260int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
de6593d0 261{
3a7c0444 262 HLTLogKeyword("dummy");
de6593d0 263 return 0;
264}
265
70ed7d01 266void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
267{
268 // see header file for function documentation
9ce4bf4a 269 memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
270 strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
271 strcat( output, ":" );
272 strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
fa2e9b7c 273}
274
fbdb63fd 275string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
9ce4bf4a 276{
70ed7d01 277 // see header file for function documentation
9ce4bf4a 278 string out("");
fbdb63fd 279
280 if (mode==2) {
281 int i=0;
282 char tmp[8];
283 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
284 sprintf(tmp, "'%d", type.fOrigin[i]);
285 out+=tmp;
286 }
287 out+="':'";
288 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
289 sprintf(tmp, "%d'", type.fID[i]);
290 out+=tmp;
291 }
292 return out;
293 }
294
295 if (mode==1) {
296 int i=0;
297 char tmp[8];
298 for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
299 unsigned char* puc=(unsigned char*)type.fOrigin;
300 if ((puc[i])<32)
301 sprintf(tmp, "'\\%x", type.fOrigin[i]);
302 else
303 sprintf(tmp, "'%c", type.fOrigin[i]);
304 out+=tmp;
305 }
306 out+="':'";
307 for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
308 unsigned char* puc=(unsigned char*)type.fID;
309 if (puc[i]<32)
310 sprintf(tmp, "\\%x'", type.fID[i]);
311 else
312 sprintf(tmp, "%c'", type.fID[i]);
313 out+=tmp;
314 }
315 return out;
316 }
317
9ce4bf4a 318 if (type==kAliHLTVoidDataType) {
319 out="VOID:VOID";
320 } else {
3cde846d 321 // some gymnastics in order to avoid a '0' which is part of either or both
322 // ID and origin terminating the whole string. Unfortunately, string doesn't
323 // stop appending at the '0' if the number of elements to append was
324 // explicitely specified
325 string tmp("");
326 tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
327 out.append(tmp.c_str());
9ce4bf4a 328 out.append(":");
3cde846d 329 tmp="";
330 tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
331 out.append(tmp.c_str());
9ce4bf4a 332 }
333 return out;
334}
335
336
70ed7d01 337void* AliHLTComponent::AllocMemory( unsigned long size )
338{
339 // see header file for function documentation
85869391 340 if (fEnvironment.fAllocMemoryFunc)
341 return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
9ce4bf4a 342 HLTFatal("no memory allocation handler registered");
85869391 343 return NULL;
344}
345
2be3f004 346int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
70ed7d01 347 AliHLTComponentBlockData** outputBlocks )
348{
349 // see header file for function documentation
9ce4bf4a 350 if ( blockCount==NULL || outputBlocks==NULL )
2d7ff710 351 return -EFAULT;
fa2e9b7c 352 AliHLTUInt32_t count = blocks.size();
353 if ( !count )
354 {
355 *blockCount = 0;
356 *outputBlocks = NULL;
357 return 0;
358 }
8ede8717 359 *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
fa2e9b7c 360 if ( !*outputBlocks )
2d7ff710 361 return -ENOMEM;
ca8524df 362 for ( unsigned long i = 0; i < count; i++ ) {
fa2e9b7c 363 (*outputBlocks)[i] = blocks[i];
ca8524df 364 if (blocks[i].fDataType==kAliHLTAnyDataType) {
5f5b708b 365 (*outputBlocks)[i].fDataType=GetOutputDataType();
366 /* data type was set to the output data type by the PubSub AliRoot
367 Wrapper component, if data type of the block was ********:****.
368 Now handled by the component base class in order to have same
369 behavior when running embedded in AliRoot
ca8524df 370 memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
371 memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
5f5b708b 372 */
ca8524df 373 }
374 }
fa2e9b7c 375 *blockCount = count;
376 return 0;
377
378}
0c0c9d99 379
70ed7d01 380int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd )
381{
382 // see header file for function documentation
85869391 383 if (fEnvironment.fGetEventDoneDataFunc)
384 return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
385 return -ENOSYS;
386}
387
2be3f004 388int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList)
0c0c9d99 389{
70ed7d01 390 // see header file for function documentation
0c0c9d99 391 int iResult=0;
392 if (pConsumer) {
8a106878 393 AliHLTComponentDataTypeList itypes;
394 AliHLTComponentDataTypeList otypes;
395 otypes.push_back(GetOutputDataType());
396 if (otypes[0]==kAliHLTMultipleDataType) {
397 otypes.clear();
398 int count=0;
399 if ((count=GetOutputDataTypes(otypes))>0) {
400 } else if (GetComponentType()!=kSink) {
401 HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
402 }
403 }
404 ((AliHLTComponent*)pConsumer)->GetInputDataTypes(itypes);
405 AliHLTComponentDataTypeList::iterator itype=itypes.begin();
406 while (itype!=itypes.end()) {
407 //PrintDataTypeContent((*itype), "consumer \'%s\'");
408 AliHLTComponentDataTypeList::iterator otype=otypes.begin();
409 while (otype!=otypes.end() && (*itype)!=(*otype)) otype++;
410 //if (otype!=otypes.end()) PrintDataTypeContent(*otype, "publisher \'%s\'");
411 if (otype!=otypes.end() ||
412 (*itype)==kAliHLTAnyDataType) {
413 if (tgtList) tgtList->push_back(*itype);
0c0c9d99 414 iResult++;
0c0c9d99 415 }
8a106878 416 itype++;
0c0c9d99 417 }
418 } else {
419 iResult=-EINVAL;
420 }
421 return iResult;
422}
2d7ff710 423
5f5b708b 424void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
425{
66043029 426 // see header file for function documentation
5f5b708b 427 const char* fmt="publisher \'%s\'";
428 if (format) fmt=format;
429 HLTMessage(fmt, (DataType2Text(dt)).c_str());
430 HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x",
431 dt.fID[0],
432 dt.fID[1],
433 dt.fID[2],
434 dt.fID[3],
435 dt.fID[4],
436 dt.fID[5],
437 dt.fID[6],
438 dt.fID[7],
439 dt.fOrigin[0],
440 dt.fOrigin[1],
441 dt.fOrigin[2],
442 dt.fOrigin[3]);
443}
444
fbdb63fd 445void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
70ed7d01 446{
447 // see header file for function documentation
2d7ff710 448 blockData.fStructSize = sizeof(blockData);
449 FillShmData( blockData.fShmKey );
450 blockData.fOffset = ~(AliHLTUInt32_t)0;
451 blockData.fPtr = NULL;
452 blockData.fSize = 0;
453 FillDataType( blockData.fDataType );
a655eae3 454 blockData.fSpecification = kAliHLTVoidDataSpec;
2d7ff710 455}
456
fbdb63fd 457void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
70ed7d01 458{
459 // see header file for function documentation
2d7ff710 460 shmData.fStructSize = sizeof(shmData);
461 shmData.fShmType = gkAliHLTComponentInvalidShmType;
462 shmData.fShmID = gkAliHLTComponentInvalidShmID;
463}
464
fbdb63fd 465void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
70ed7d01 466{
467 // see header file for function documentation
ca8524df 468 dataType=kAliHLTAnyDataType;
2d7ff710 469}
470
70ed7d01 471void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt)
472{
473 // see header file for function documentation
2d7ff710 474 memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
475 memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
476}
477
70ed7d01 478void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin)
479{
480 // see header file for function documentation
7e3efc8f 481 tgtdt.fStructSize=sizeof(AliHLTComponentDataType);
482 if (id) {
483 memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
d76bc02a 484 strncpy(&tgtdt.fID[0], id, strlen(id)<(size_t)kAliHLTComponentDataTypefIDsize?strlen(id):kAliHLTComponentDataTypefIDsize);
7e3efc8f 485 }
486 if (origin) {
487 memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
d76bc02a 488 strncpy(&tgtdt.fOrigin[0], origin, strlen(origin)<(size_t)kAliHLTComponentDataTypefOriginSize?strlen(origin):kAliHLTComponentDataTypefOriginSize);
7e3efc8f 489 }
2d7ff710 490}
9ce4bf4a 491
492void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
493{
70ed7d01 494 // see header file for function documentation
9ce4bf4a 495 memset(&evtData, 0, sizeof(AliHLTComponentEventData));
496 evtData.fStructSize=sizeof(AliHLTComponentEventData);
497}
498
70ed7d01 499void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt)
500{
501 // see header file for function documentation
9ce4bf4a 502 TString msg;
503 msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
504 for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
505 if (dt.fID[i]!=0) msg+=dt.fID[i];
506 else msg+="\\0";
507 }
508 msg+="\" Origin=\"";
509 for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
510 if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
511 else msg+="\\0";
512 }
513 msg+="\"";
3cde846d 514 AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
9ce4bf4a 515}
516
70ed7d01 517int AliHLTComponent::GetEventCount() const
3cde846d 518{
70ed7d01 519 // see header file for function documentation
3cde846d 520 return fEventCount;
521}
522
523int AliHLTComponent::IncrementEventCounter()
524{
70ed7d01 525 // see header file for function documentation
3cde846d 526 if (fEventCount>=0) fEventCount++;
527 return fEventCount;
528}
529
66043029 530int AliHLTComponent::GetNumberOfInputBlocks() const
a655eae3 531{
532 // see header file for function documentation
533 if (fpInputBlocks!=NULL) {
534 return fCurrentEventData.fBlockCnt;
535 }
536 return 0;
537}
538
539const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
540 const char* classname,
541 int bForce)
542{
543 // see header file for function documentation
90ebac25 544 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 545 fSearchDataType=dt;
546 if (classname) fClassName=classname;
547 else fClassName.clear();
1edbbe49 548 int idx=FindInputBlock(fSearchDataType, 0, 1);
a655eae3 549 TObject* pObj=NULL;
550 if (idx>=0) {
79c114b5 551 HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
a655eae3 552 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
553 fCurrentInputBlock=idx;
554 } else {
555 }
556 }
557 return pObj;
558}
559
560const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID,
561 const char* dtOrigin,
562 const char* classname,
563 int bForce)
564{
565 // see header file for function documentation
90ebac25 566 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 567 AliHLTComponentDataType dt;
568 SetDataType(dt, dtID, dtOrigin);
569 return GetFirstInputObject(dt, classname, bForce);
570}
571
572const TObject* AliHLTComponent::GetNextInputObject(int bForce)
573{
574 // see header file for function documentation
90ebac25 575 ALIHLTCOMPONENT_BASE_STOPWATCH();
1edbbe49 576 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
a655eae3 577 //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
578 TObject* pObj=NULL;
579 if (idx>=0) {
580 if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
581 fCurrentInputBlock=idx;
582 }
583 }
584 return pObj;
585}
586
1edbbe49 587int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
a655eae3 588{
589 // see header file for function documentation
590 int iResult=-ENOENT;
591 if (fpInputBlocks!=NULL) {
592 int idx=startIdx<0?0:startIdx;
4b98eadb 593 for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
1edbbe49 594 if (bObject!=0) {
595 if (fpInputBlocks[idx].fPtr==NULL) continue;
3294f81a 596 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
597 if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
1edbbe49 598 }
329cbc9c 599 if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt ||
600 (memcmp(dt.fID, kAliHLTAnyDataTypeID, kAliHLTComponentDataTypefIDsize)==0 &&
601 memcmp(dt.fOrigin, fpInputBlocks[idx].fDataType.fOrigin, kAliHLTComponentDataTypefOriginSize)==0) ||
602 (memcmp(dt.fID, fpInputBlocks[idx].fDataType.fID, kAliHLTComponentDataTypefIDsize)==0) &&
603 memcmp(dt.fOrigin, kAliHLTDataOriginAny, kAliHLTComponentDataTypefOriginSize)==0) {
a655eae3 604 iResult=idx;
605 }
606 }
607 }
608 return iResult;
609}
610
611TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
612{
613 // see header file for function documentation
614 TObject* pObj=NULL;
615 if (fpInputBlocks!=NULL) {
4b98eadb 616 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
a655eae3 617 if (fpInputBlocks[idx].fPtr) {
3294f81a 618 AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
a655eae3 619 if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
8451168b 620 HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
3294f81a 621 AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
66043029 622 TClass* objclass=msg.GetClass();
623 pObj=msg.ReadObject(objclass);
624 if (pObj && objclass) {
625 HLTDebug("object %p type %s created", pObj, objclass->GetName());
a655eae3 626 } else {
627 }
1edbbe49 628 //} else {
629 } else if (bForce!=0) {
a655eae3 630 HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
631 }
632 } else {
633 HLTFatal("block descriptor empty");
634 }
635 } else {
636 HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
637 }
638 } else {
639 HLTError("no input blocks available");
640 }
641
642 return pObj;
643}
644
298ef463 645TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
a655eae3 646{
647 // see header file for function documentation
648 if (fpInputObjects==NULL) {
649 fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
650 }
651 TObject* pObj=NULL;
652 if (fpInputObjects) {
653 pObj=fpInputObjects->At(idx);
654 if (pObj==NULL) {
655 pObj=CreateInputObject(idx, bForce);
656 if (pObj) {
657 fpInputObjects->AddAt(pObj, idx);
658 }
659 }
660 } else {
661 HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
662 }
663 return pObj;
664}
665
8451168b 666int AliHLTComponent::CleanupInputObjects()
667{
66043029 668 // see header file for function documentation
8451168b 669 if (!fpInputObjects) return 0;
670 TObjArray* array=fpInputObjects;
671 fpInputObjects=NULL;
672 for (int i=0; i<array->GetEntries(); i++) {
673 TObject* pObj=array->At(i);
79c114b5 674 // grrr, garbage collection strikes back: When read via AliHLTMessage
675 // (CreateInputObject), and written to a TFile afterwards, the
676 // TFile::Close calls ROOOT's garbage collection. No clue why the
677 // object ended up in the key list and needs to be deleted
678 if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
8451168b 679 }
680 delete array;
90ebac25 681 return 0;
8451168b 682}
683
a655eae3 684AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
685{
686 // see header file for function documentation
90ebac25 687 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 688 AliHLTComponentDataType dt=kAliHLTVoidDataType;
689 int idx=fCurrentInputBlock;
690 if (pObject) {
691 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
692 } else {
693 HLTError("unknown object %p", pObject);
694 }
695 }
696 if (idx>=0) {
4b98eadb 697 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
a655eae3 698 dt=fpInputBlocks[idx].fDataType;
699 } else {
700 HLTFatal("severe internal error, index out of range");
701 }
702 }
703 return dt;
704}
705
706AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
707{
708 // see header file for function documentation
90ebac25 709 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 710 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
711 int idx=fCurrentInputBlock;
712 if (pObject) {
713 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
714 } else {
715 HLTError("unknown object %p", pObject);
716 }
717 }
718 if (idx>=0) {
4b98eadb 719 if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
a655eae3 720 iSpec=fpInputBlocks[idx].fSpecification;
721 } else {
722 HLTFatal("severe internal error, index out of range");
723 }
724 }
725 return iSpec;
726}
727
c7e9e2f2 728int AliHLTComponent::Forward(const TObject* pObject)
729{
730 // see header file for function documentation
731 int iResult=0;
732 int idx=fCurrentInputBlock;
733 if (pObject) {
734 if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
735 } else {
736 HLTError("unknown object %p", pObject);
737 iResult=-ENOENT;
738 }
739 }
740 if (idx>=0) {
741 fOutputBlocks.push_back(fpInputBlocks[idx]);
742 }
743 return iResult;
744}
745
746int AliHLTComponent::Forward(const AliHLTComponentBlockData* pBlock)
747{
748 // see header file for function documentation
749 int iResult=0;
750 int idx=fCurrentInputBlock;
751 if (pBlock) {
752 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
753 } else {
754 HLTError("unknown Block %p", pBlock);
755 iResult=-ENOENT;
756 }
757 }
758 if (idx>=0) {
759 // check for fpInputBlocks pointer done in FindInputBlock
760 fOutputBlocks.push_back(fpInputBlocks[idx]);
761 }
762 return iResult;
763}
764
a655eae3 765const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
766{
767 // see header file for function documentation
90ebac25 768 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 769 fSearchDataType=dt;
770 fClassName.clear();
771 int idx=FindInputBlock(fSearchDataType, 0);
772 const AliHLTComponentBlockData* pBlock=NULL;
773 if (idx>=0) {
774 // check for fpInputBlocks pointer done in FindInputBlock
775 pBlock=&fpInputBlocks[idx];
1edbbe49 776 fCurrentInputBlock=idx;
a655eae3 777 }
778 return pBlock;
779}
780
781const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID,
782 const char* dtOrigin)
783{
784 // see header file for function documentation
90ebac25 785 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 786 AliHLTComponentDataType dt;
787 SetDataType(dt, dtID, dtOrigin);
788 return GetFirstInputBlock(dt);
789}
790
ec25e4ca 791const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index)
792{
793 // see header file for function documentation
794 ALIHLTCOMPONENT_BASE_STOPWATCH();
795 assert( 0 <= index and index < fCurrentEventData.fBlockCnt );
796 return &fpInputBlocks[index];
797}
798
a655eae3 799const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
800{
801 // see header file for function documentation
90ebac25 802 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 803 int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
804 const AliHLTComponentBlockData* pBlock=NULL;
805 if (idx>=0) {
806 // check for fpInputBlocks pointer done in FindInputBlock
807 pBlock=&fpInputBlocks[idx];
1edbbe49 808 fCurrentInputBlock=idx;
a655eae3 809 }
810 return pBlock;
811}
812
66043029 813int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
a655eae3 814{
815 // see header file for function documentation
816 int iResult=-ENOENT;
817 if (fpInputBlocks!=NULL) {
818 if (pBlock) {
819 if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
132ca004 820 iResult=(int)(pBlock-fpInputBlocks);
a655eae3 821 }
822 } else {
823 iResult=-EINVAL;
824 }
825 }
826 return iResult;
827}
828
829AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
830{
831 // see header file for function documentation
90ebac25 832 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 833 AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
834 int idx=fCurrentInputBlock;
835 if (pBlock) {
836 if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
837 } else {
838 HLTError("unknown Block %p", pBlock);
839 }
840 }
841 if (idx>=0) {
842 // check for fpInputBlocks pointer done in FindInputBlock
843 iSpec=fpInputBlocks[idx].fSpecification;
844 }
845 return iSpec;
846}
847
79c114b5 848int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
849 void* pHeader, int headerSize)
a655eae3 850{
851 // see header file for function documentation
90ebac25 852 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 853 int iResult=0;
854 if (pObject) {
855 AliHLTMessage msg(kMESS_OBJECT);
856 msg.WriteObject(pObject);
857 Int_t iMsgLength=msg.Length();
858 if (iMsgLength>0) {
859 msg.SetLength(); // sets the length to the first (reserved) word
79c114b5 860 iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
a655eae3 861 if (iResult>=0) {
8451168b 862 HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
a655eae3 863 }
864 } else {
865 HLTError("object serialization failed for object %p", pObject);
866 iResult=-ENOMSG;
867 }
868 } else {
869 iResult=-EINVAL;
870 }
871 return iResult;
872}
873
79c114b5 874int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
875 void* pHeader, int headerSize)
a655eae3 876{
877 // see header file for function documentation
90ebac25 878 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 879 AliHLTComponentDataType dt;
880 SetDataType(dt, dtID, dtOrigin);
79c114b5 881 return PushBack(pObject, dt, spec, pHeader, headerSize);
a655eae3 882}
883
9d9ffd37 884int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
885 void* pHeader, int headerSize)
a655eae3 886{
887 // see header file for function documentation
90ebac25 888 ALIHLTCOMPONENT_BASE_STOPWATCH();
9d9ffd37 889 return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
a655eae3 890}
891
9d9ffd37 892int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
893 void* pHeader, int headerSize)
a655eae3 894{
895 // see header file for function documentation
90ebac25 896 ALIHLTCOMPONENT_BASE_STOPWATCH();
a655eae3 897 AliHLTComponentDataType dt;
898 SetDataType(dt, dtID, dtOrigin);
9d9ffd37 899 return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
a655eae3 900}
901
79c114b5 902int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
903 void* pHeader, int iHeaderSize)
a655eae3 904{
905 // see header file for function documentation
906 int iResult=0;
79c114b5 907 int iBlkSize = iBufferSize + iHeaderSize;
a655eae3 908 if (pBuffer) {
79c114b5 909 if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
a655eae3 910 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
911 AliHLTComponentBlockData bd;
912 FillBlockData( bd );
913 bd.fOffset = fOutputBufferFilled;
79c114b5 914 bd.fSize = iBlkSize;
a655eae3 915 bd.fDataType = dt;
916 bd.fSpecification = spec;
79c114b5 917 if (pHeader!=NULL && pHeader!=pTgt) {
918 memcpy(pTgt, pHeader, iHeaderSize);
919 }
920
921 pTgt += (AliHLTUInt8_t) iHeaderSize;
922
a655eae3 923 if (pBuffer!=NULL && pBuffer!=pTgt) {
79c114b5 924 memcpy(pTgt, pBuffer, iBufferSize);
925
4b98eadb 926 //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer);
79c114b5 927 //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
a655eae3 928 }
929 fOutputBufferFilled+=bd.fSize;
930 fOutputBlocks.push_back( bd );
79c114b5 931 //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
a655eae3 932 } else {
933 if (fpOutputBuffer) {
79c114b5 934 HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
a655eae3 935 } else {
936 HLTError("output buffer not available");
937 }
938 iResult=-ENOSPC;
939 }
940 } else {
941 iResult=-EINVAL;
942 }
943 return iResult;
944}
945
8451168b 946int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
947{
66043029 948 // see header file for function documentation
8451168b 949 if (!pObject) return -EINVAL;
950 AliHLTMessage msg(kMESS_OBJECT);
951 msg.WriteObject(pObject);
952 return msg.Length();
953}
954
79c114b5 955AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
956 const char* dtOrigin,
957 AliHLTUInt32_t spec)
958{
959 // see header file for function documentation
960 ALIHLTCOMPONENT_BASE_STOPWATCH();
961 AliHLTComponentDataType dt;
962 SetDataType(dt, dtID, dtOrigin);
963 return CreateMemoryFile(capacity, dt, spec);
964}
965
966AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
967 const AliHLTComponentDataType& dt,
968 AliHLTUInt32_t spec)
969{
970 // see header file for function documentation
971 ALIHLTCOMPONENT_BASE_STOPWATCH();
972 AliHLTMemoryFile* pFile=NULL;
83fec083 973 if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
79c114b5 974 AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
975 pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
976 if (pFile) {
83fec083 977 unsigned int nofBlocks=fOutputBlocks.size();
79c114b5 978 if (nofBlocks+1>fMemFiles.size()) {
979 fMemFiles.resize(nofBlocks+1, NULL);
980 }
981 if (nofBlocks<fMemFiles.size()) {
982 fMemFiles[nofBlocks]=pFile;
983 AliHLTComponentBlockData bd;
984 FillBlockData( bd );
985 bd.fOffset = fOutputBufferFilled;
79c114b5 986 bd.fSize = capacity;
987 bd.fDataType = dt;
988 bd.fSpecification = spec;
989 fOutputBufferFilled+=bd.fSize;
990 fOutputBlocks.push_back( bd );
991 } else {
992 HLTError("can not allocate/grow object array");
29312178 993 pFile->CloseMemoryFile(0);
79c114b5 994 delete pFile;
995 pFile=NULL;
996 }
997 }
998 } else {
999 HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
1000 }
1001 return pFile;
1002}
1003
1004AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
1005 const char* dtOrigin,
1006 AliHLTUInt32_t spec,
1007 float capacity)
1008{
1009 // see header file for function documentation
1010 ALIHLTCOMPONENT_BASE_STOPWATCH();
1011 AliHLTComponentDataType dt;
1012 SetDataType(dt, dtID, dtOrigin);
1013 int size=fOutputBufferSize-fOutputBufferFilled;
1014 if (capacity<0 || capacity>1.0) {
1015 HLTError("invalid parameter: capacity %f", capacity);
1016 return NULL;
1017 }
1018 size=(int)(size*capacity);
1019 return CreateMemoryFile(size, dt, spec);
1020}
1021
1022AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
1023 AliHLTUInt32_t spec,
1024 float capacity)
1025{
1026 // see header file for function documentation
1027 ALIHLTCOMPONENT_BASE_STOPWATCH();
1028 int size=fOutputBufferSize-fOutputBufferFilled;
1029 if (capacity<0 || capacity>1.0) {
1030 HLTError("invalid parameter: capacity %f", capacity);
1031 return NULL;
1032 }
1033 size=(int)(size*capacity);
1034 return CreateMemoryFile(size, dt, spec);
1035}
1036
1037int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
1038 const char* key, int option)
1039{
3a7c0444 1040 // see header file for function documentation
79c114b5 1041 int iResult=0;
1042 if (pFile && pObject) {
1043 pFile->cd();
1044 iResult=pObject->Write(key, option);
1045 if (iResult>0) {
1046 // success
1047 } else {
1048 iResult=-pFile->GetErrno();
1049 if (iResult==-ENOSPC) {
1050 HLTError("error writing memory file, buffer too small");
1051 }
1052 }
1053 } else {
1054 iResult=-EINVAL;
1055 }
1056 return iResult;
1057}
1058
1059int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
1060{
3a7c0444 1061 // see header file for function documentation
79c114b5 1062 int iResult=0;
1063 if (pFile) {
2be3f004 1064 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
79c114b5 1065 int i=0;
1066 while (element!=fMemFiles.end() && iResult>=0) {
1067 if (*element && *element==pFile) {
29312178 1068 iResult=pFile->CloseMemoryFile();
79c114b5 1069
1070 // sync memory files and descriptors
1071 if (iResult>=0) {
1072 fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
1073 }
1074 delete *element;
1075 *element=NULL;
1076 return iResult;
1077 }
1078 element++; i++;
1079 }
1080 HLTError("can not find memory file %p", pFile);
1081 iResult=-ENOENT;
1082 } else {
1083 iResult=-EINVAL;
1084 }
1085 return iResult;
1086}
1087
29312178 1088int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData /*edd*/)
a655eae3 1089{
1090 // see header file for function documentation
1091 int iResult=-ENOSYS;
1092 //#warning function not yet implemented
1093 HLTWarning("function not yet implemented");
1094 return iResult;
1095}
1096
3cde846d 1097int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1098 const AliHLTComponentBlockData* blocks,
1099 AliHLTComponentTriggerData& trigData,
1100 AliHLTUInt8_t* outputPtr,
1101 AliHLTUInt32_t& size,
1102 AliHLTUInt32_t& outputBlockCnt,
1103 AliHLTComponentBlockData*& outputBlocks,
1104 AliHLTComponentEventDoneData*& edd )
1105{
70ed7d01 1106 // see header file for function documentation
90ebac25 1107 ALIHLTCOMPONENT_BASE_STOPWATCH();
3cde846d 1108 int iResult=0;
1109 fCurrentEvent=evtData.fEventID;
a655eae3 1110 fCurrentEventData=evtData;
1111 fpInputBlocks=blocks;
1112 fCurrentInputBlock=-1;
1113 fSearchDataType=kAliHLTAnyDataType;
1114 fpOutputBuffer=outputPtr;
1115 fOutputBufferSize=size;
1116 fOutputBufferFilled=0;
1117 fOutputBlocks.clear();
559631d5 1118
1119 // find special events
1120 if (fpInputBlocks) {
579d9eb7 1121 // first look for all special events and execute in the appropriate
1122 // sequence afterwords
1123 int indexComConfEvent=-1;
1124 int indexSOREvent=-1;
1125 int indexEOREvent=-1;
83fec083 1126 for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
559631d5 1127 if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
579d9eb7 1128 indexSOREvent=i;
1129 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1130 indexEOREvent=i;
1131 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1132 // DDL list
1133 // this event is most likely deprecated
1134 } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComConf) {
1135 indexComConfEvent=i;
1136 }
1137 }
1138 if (indexSOREvent>=0) {
1139 // start of run
1140 if (fpRunDesc==NULL) {
1141 fpRunDesc=new AliHLTRunDesc;
1142 if (fpRunDesc) {
1143 if ((iResult=CopyStruct(fpRunDesc, sizeof(AliHLTRunDesc), indexSOREvent, "AliHLTRunDesc", "SOR"))>0) {
1144 HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1145 SetCDBRunNo(fpRunDesc->fRunNo);
559631d5 1146 }
1147 } else {
579d9eb7 1148 iResult=-ENOMEM;
559631d5 1149 }
579d9eb7 1150 } else {
1151 HLTWarning("already received SOR event run no %d, ignoring SOR", fpRunDesc->fRunNo);
1152 }
1153 }
1154 if (indexEOREvent>=0) {
1155 if (fpRunDesc!=NULL) {
1156 if (fpRunDesc) {
1157 AliHLTRunDesc rundesc;
1158 if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexEOREvent, "AliHLTRunDesc", "SOR"))>0) {
1159 if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1160 HLTWarning("run no missmatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1161 } else {
1162 HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
559631d5 1163 }
559631d5 1164 }
579d9eb7 1165 AliHLTRunDesc* pRunDesc=fpRunDesc;
1166 fpRunDesc=NULL;
1167 delete pRunDesc;
559631d5 1168 }
579d9eb7 1169 } else {
1170 HLTWarning("did not receive SOR, ignoring EOR");
1171 }
1172 }
1173 if (indexComConfEvent>=0) {
1174 TString cdbEntry;
1175 if (fpInputBlocks[indexComConfEvent].fPtr!=NULL && fpInputBlocks[indexComConfEvent].fSize>0) {
1176 cdbEntry.Append(reinterpret_cast<const char*>(fpInputBlocks[indexComConfEvent].fPtr), fpInputBlocks[indexComConfEvent].fSize);
1177 }
1178 HLTDebug("received component configuration command: entry %s", cdbEntry.IsNull()?"none":cdbEntry.Data());
1179 int tmpResult=Reconfigure(cdbEntry[0]==0?NULL:cdbEntry.Data(), fChainId.c_str());
1180 if (tmpResult<0) {
1181 HLTWarning("reconfiguration of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
559631d5 1182 }
1183 }
1184 }
a655eae3 1185
2be3f004 1186 AliHLTComponentBlockDataList blockData;
90ebac25 1187 { // dont delete, sets the scope for the stopwatch guard
1188 ALIHLTCOMPONENT_DA_STOPWATCH();
1189 iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1190 } // end of the scope of the stopwatch guard
a655eae3 1191 if (iResult>=0) {
1192 if (fOutputBlocks.size()>0) {
1193 //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
79c114b5 1194
1195 // sync memory files and descriptors
2be3f004 1196 AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
79c114b5 1197 int i=0;
1198 while (element!=fMemFiles.end() && iResult>=0) {
1199 if (*element) {
1200 if ((*element)->IsClosed()==0) {
1201 HLTWarning("memory file has not been closed, force flush");
1202 iResult=CloseMemoryFile(*element);
1203 }
1204 }
1205 element++; i++;
1206 }
1207
1208 if (iResult>=0) {
1209 // create the descriptor list
1210 if (blockData.size()>0) {
1211 HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1212 iResult=-EFAULT;
1213 } else {
1214 iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1215 size=fOutputBufferFilled;
1216 }
a655eae3 1217 }
1218 } else {
1219 iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1220 }
1221 if (iResult<0) {
1222 HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1223 }
1224 }
1225 if (iResult<0) {
1226 outputBlockCnt=0;
1227 outputBlocks=NULL;
1228 }
8451168b 1229 CleanupInputObjects();
f8bc6d99 1230 if (iResult>=0) {
1231 IncrementEventCounter();
1232 }
3cde846d 1233 return iResult;
1234}
a655eae3 1235
90ebac25 1236AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1237 :
1238 fpStopwatch(NULL),
1239 fpPrec(NULL)
1240{
1241 // standard constructor (not for use)
1242}
1243
1244AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1245 :
1246 fpStopwatch(pStopwatch),
1247 fpPrec(NULL)
1248{
1249 // constructor
1250
1251 // check for already existing guard
1252 if (fgpCurrent) fpPrec=fgpCurrent;
1253 fgpCurrent=this;
1254
1255 // stop the preceeding guard if it controls a different stopwatch
1256 int bStart=1;
1257 if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1258
1259 // start the stopwatch if the current guard controls a different one
1260 if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1261}
1262
e419b223 1263AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
90ebac25 1264 :
1265 fpStopwatch(NULL),
1266 fpPrec(NULL)
1267{
e419b223 1268 //
1269 // copy constructor not for use
1270 //
1271}
1272
1273AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1274{
1275 //
1276 // assignment operator not for use
1277 //
1278 fpStopwatch=NULL;
1279 fpPrec=NULL;
1280 return *this;
90ebac25 1281}
1282
1283AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1284
1285AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1286{
1287 // destructor
1288
1289 // resume the preceeding guard if it controls a different stopwatch
1290 int bStop=1;
1291 if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1292
1293 // stop the stopwatch if the current guard controls a different one
1294 if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1295
1296 // resume to the preceeding guard
1297 fgpCurrent=fpPrec;
1298}
1299
1300int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1301{
1302 // see header file for function documentation
1303 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1304 return fpStopwatch!=pSucc?1:0;
1305}
1306
1307int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1308{
1309 // see header file for function documentation
1310 if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1311 return fpStopwatch!=pSucc?1:0;
1312}
1313
1314int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type)
1315{
1316 // see header file for function documentation
1317 int iResult=0;
1318 if (pSW!=NULL && type<kSWTypeCount) {
1319 if (fpStopwatches) {
1320 TObject* pObj=fpStopwatches->At((int)type);
1321 if (pSW==NULL // explicit reset
1322 || pObj==NULL) { // explicit set
1323 fpStopwatches->AddAt(pSW, (int)type);
1324 } else if (pObj!=pSW) {
1325 HLTWarning("stopwatch %d already set, reset first", (int)type);
1326 iResult=-EBUSY;
1327 }
1328 }
1329 } else {
1330 iResult=-EINVAL;
1331 }
1332 return iResult;
1333}
1334
1335int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1336{
1337 // see header file for function documentation
1338 if (pStopwatches==NULL) return -EINVAL;
1339
1340 int iResult=0;
1341 for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1342 SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1343 return iResult;
1344}
559631d5 1345
1346AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1347{
29312178 1348 // see header file for function documentation
559631d5 1349 if (fpRunDesc==NULL) return 0;
1350 return fpRunDesc->fRunNo;
1351}
1352
1353AliHLTUInt32_t AliHLTComponent::GetRunType() const
1354{
29312178 1355 // see header file for function documentation
559631d5 1356 if (fpRunDesc==NULL) return 0;
1357 return fpRunDesc->fRunType;
1358}
1359
83fec083 1360int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
559631d5 1361 const char* structname, const char* eventname)
1362{
29312178 1363 // see header file for function documentation
559631d5 1364 int iResult=0;
1365 if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1366 if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1367 AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1368 if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
3294f81a 1369 AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
559631d5 1370 if (fpInputBlocks[iBlockNo].fSize!=copy) {
1371 HLTWarning("%s event: missmatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1372 if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1373 }
1374 if (copy!=iStructSize) {
1375 HLTWarning("%s event: missmatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1376 if (copy>iStructSize) {
1377 copy=iStructSize;
1378 } else {
1379 memset(pTgt, 0, iStructSize);
1380 }
1381 }
3294f81a 1382 memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
559631d5 1383 *pTgt=iStructSize;
1384 iResult=copy;
1385 } else {
1386 HLTWarning("%s event: missing data block", eventname);
1387 }
1388 } else {
1389 iResult=-ENODATA;
1390 }
1391 } else {
1392 HLTError("invalid struct");
1393 iResult=-EINVAL;
1394 }
1395 return iResult;
1396}
ed504011 1397
1398void AliHLTComponent::SetDDLBit(AliHLTEventDDL &list, Int_t ddlId, Bool_t state ) const
1399{
1400 // see header file for function documentation
1401
1402 // -- Detector offset
1403 Int_t ddlIdBase = TMath::FloorNint( (Double_t) ddlId / 256.0 );
1404
1405 // -- Word Base = 1. word of detector ( TPC has 8 words, TOF 3 )
1406 Int_t wordBase = 0;
1407
1408 if ( ddlIdBase <= 3 )
1409 wordBase = ddlIdBase;
1410 else if ( ddlIdBase > 3 && ddlIdBase < 5 )
1411 wordBase = ddlIdBase + 7;
1412 else
1413 wordBase = ddlIdBase + 9;
1414
1415 // -- Bit index in Word
1416 Int_t bitIdx = ddlId % 32;
1417
1418 // -- Index of word
1419 Int_t wordIdx = wordBase;
1420
1421 // -- if TPC (3) or TOD (5) add word idx
1422 if ( ( ddlIdBase == 3 ) || ( ddlIdBase == 5 ) ) {
1423 wordIdx += TMath::FloorNint( (Double_t) ( ddlId - ( ddlIdBase * 256 ) ) / 32.0 );
1424 }
1425
1426 // -- Set -- 'OR' word with bit mask;
1427 if ( state )
1428 list.fList[wordIdx] |= ( 0x00000001 << bitIdx );
1429 // -- Unset -- 'AND' word with bit mask;
1430 else
1431 list.fList[wordIdx] &= ( 0xFFFFFFFF ^ ( 0x00000001 << bitIdx ) );
1432}
1433
1434Int_t AliHLTComponent::GetFirstUsedDDLWord(AliHLTEventDDL &list) const
1435{
1436 // see header file for function documentation
1437
1438 Int_t iResult = -1;
1439
1440 for ( Int_t wordNdx = 0 ; wordNdx < gkAliHLTDDLListSize ; wordNdx++ ) {
1441
1442 if ( list.fList[wordNdx] != 0 && iResult == -1 ) {
1443 // check for special cases TPC and TOF
1444 if ( wordNdx > 3 && wordNdx <= 10 ) {
1445 wordNdx = 10;
1446 iResult = 3;
1447 }
1448 else if ( wordNdx > 12 && wordNdx <= 14 ) {
1449 wordNdx = 14;
1450 iResult = 12;
1451 }
1452 else
1453 iResult = wordNdx;
1454 }
1455 else if ( list.fList[wordNdx] != 0 && iResult >= 0 ) {
1456 HLTError( "DDLIDs for minimum of TWO detectors ( %d, %d ) set, this function works only for ONE detector.", iResult, wordNdx );
1457 iResult = -1;
1458 break;
1459 }
1460 }
1461
1462 return iResult;
1463}