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