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