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