]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTComponent.cxx
Coding conventions
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTComponent.cxx
1 // $Id$
2
3 /**************************************************************************
4  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
5  *                                                                        *
6  * Authors: Matthias Richter <Matthias.Richter@ift.uib.no>                *
7  *          Timm Steinbeck <timm@kip.uni-heidelberg.de>                   *
8  *          for The ALICE Off-line Project.                               *
9  *                                                                        *
10  * Permission to use, copy, modify and distribute this software and its   *
11  * documentation strictly for non-commercial purposes is hereby granted   *
12  * without fee, provided that the above copyright notice appears in all   *
13  * copies and that both the copyright notice and this permission notice   *
14  * appear in the supporting documentation. The authors make no claims     *
15  * about the suitability of this software for any purpose. It is          *
16  * provided "as is" without express or implied warranty.                  *
17  **************************************************************************/
18
19 /** @file   AliHLTComponent.cxx
20     @author Matthias Richter, Timm Steinbeck
21     @date   
22     @brief  Base class implementation for HLT components. */
23
24 #if __GNUC__>= 3
25 using namespace std;
26 #endif
27
28 //#include "AliHLTStdIncludes.h"
29 #include "AliHLTComponent.h"
30 #include "AliHLTComponentHandler.h"
31 #include "AliHLTMessage.h"
32 #include "TString.h"
33 #include "TObjArray.h"
34 #include "TClass.h"
35 #include "TStopwatch.h"
36
37 /** ROOT macro for the implementation of ROOT specific class methods */
38 ClassImp(AliHLTComponent);
39
40 /** stopwatch macro using the stopwatch guard */
41 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
42 //#define ALIHLTCOMPONENT_STOPWATCH(type) 
43
44 /** stopwatch macro for operations of the base class */
45 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
46 /** stopwatch macro for operations of the detector algorithm (DA) */
47 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
48
49 AliHLTComponent::AliHLTComponent()
50   :
51   fEnvironment(),
52   fCurrentEvent(0),
53   fEventCount(-1),
54   fFailedEvents(0),
55   fCurrentEventData(),
56   fpInputBlocks(NULL),
57   fCurrentInputBlock(-1),
58   fSearchDataType(kAliHLTVoidDataType),
59   fClassName(),
60   fpInputObjects(NULL),
61   fpOutputBuffer(NULL),
62   fOutputBufferSize(0),
63   fOutputBufferFilled(0),
64   fOutputBlocks(),
65   fpStopwatches(new TObjArray(kSWTypeCount))
66 {
67   // see header file for class documentation
68   // or
69   // refer to README to build package
70   // or
71   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
72   memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
73   if (fgpComponentHandler)
74     fgpComponentHandler->ScheduleRegister(this);
75   SetLocalLoggingLevel(kHLTLogDefault);
76 }
77
78 AliHLTComponent::AliHLTComponent(const AliHLTComponent&)
79   :
80   AliHLTLogging(),
81   fEnvironment(),
82   fCurrentEvent(0),
83   fEventCount(-1),
84   fFailedEvents(0),
85   fCurrentEventData(),
86   fpInputBlocks(NULL),
87   fCurrentInputBlock(-1),
88   fSearchDataType(kAliHLTVoidDataType),
89   fClassName(),
90   fpInputObjects(NULL),
91   fpOutputBuffer(NULL),
92   fOutputBufferSize(0),
93   fOutputBufferFilled(0),
94   fOutputBlocks(),
95   fpStopwatches(NULL)
96 {
97   // see header file for class documentation
98   HLTFatal("copy constructor untested");
99 }
100
101 AliHLTComponent& AliHLTComponent::operator=(const AliHLTComponent&)
102
103   // see header file for class documentation
104   HLTFatal("assignment operator untested");
105   return *this;
106 }
107
108 AliHLTComponent::~AliHLTComponent()
109 {
110   // see header file for function documentation
111   CleanupInputObjects();
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   return iResult;
195 }
196
197 int AliHLTComponent::DoInit( int argc, const char** argv )
198 {
199   // see header file for function documentation
200   if (argc==0 && argv==NULL) {
201     // this is currently just to get rid of the warning "unused parameter"
202   }
203   fEventCount=0;
204   return 0;
205 }
206
207 int AliHLTComponent::DoDeinit()
208 {
209   // see header file for function documentation
210   fEventCount=0;
211   return 0;
212 }
213
214 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
215 {
216   // see header file for function documentation
217   memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
218   strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
219   strcat( output, ":" );
220   strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
221 }
222
223 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type )
224 {
225   // see header file for function documentation
226   string out("");
227   
228   if (type==kAliHLTVoidDataType) {
229     out="VOID:VOID";
230   } else {
231     // some gymnastics in order to avoid a '0' which is part of either or both
232     // ID and origin terminating the whole string. Unfortunately, string doesn't
233     // stop appending at the '0' if the number of elements to append was 
234     // explicitely specified
235     string tmp("");
236     tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
237     out.append(tmp.c_str());
238     out.append(":");
239     tmp="";
240     tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
241     out.append(tmp.c_str());
242   }
243   return out;
244 }
245
246
247 void* AliHLTComponent::AllocMemory( unsigned long size ) 
248 {
249   // see header file for function documentation
250   if (fEnvironment.fAllocMemoryFunc)
251     return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
252   HLTFatal("no memory allocation handler registered");
253   return NULL;
254 }
255
256 int AliHLTComponent::MakeOutputDataBlockList( const vector<AliHLTComponentBlockData>& blocks, AliHLTUInt32_t* blockCount,
257                                               AliHLTComponentBlockData** outputBlocks ) 
258 {
259   // see header file for function documentation
260     if ( blockCount==NULL || outputBlocks==NULL )
261         return -EFAULT;
262     AliHLTUInt32_t count = blocks.size();
263     if ( !count )
264         {
265         *blockCount = 0;
266         *outputBlocks = NULL;
267         return 0;
268         }
269     *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
270     if ( !*outputBlocks )
271         return -ENOMEM;
272     for ( unsigned long i = 0; i < count; i++ ) {
273         (*outputBlocks)[i] = blocks[i];
274         if (blocks[i].fDataType==kAliHLTAnyDataType) {
275           (*outputBlocks)[i].fDataType=GetOutputDataType();
276           /* data type was set to the output data type by the PubSub AliRoot
277              Wrapper component, if data type of the block was ********:****.
278              Now handled by the component base class in order to have same
279              behavior when running embedded in AliRoot
280           memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
281           memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
282           */
283         }
284     }
285     *blockCount = count;
286     return 0;
287
288 }
289
290 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd ) 
291 {
292   // see header file for function documentation
293   if (fEnvironment.fGetEventDoneDataFunc)
294     return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
295   return -ENOSYS;
296 }
297
298 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, vector<AliHLTComponentDataType>* tgtList) 
299 {
300   // see header file for function documentation
301   int iResult=0;
302   if (pConsumer) {
303     vector<AliHLTComponentDataType> ctlist;
304     ((AliHLTComponent*)pConsumer)->GetInputDataTypes(ctlist);
305     vector<AliHLTComponentDataType>::iterator type=ctlist.begin();
306     //AliHLTComponentDataType ouptdt=GetOutputDataType();
307     //PrintDataTypeContent(ouptdt, "publisher \'%s\'");
308     while (type!=ctlist.end() && iResult==0) {
309       //PrintDataTypeContent((*type), "consumer \'%s\'");
310       if ((*type)==GetOutputDataType() ||
311           (*type)==kAliHLTAnyDataType) {
312         if (tgtList) tgtList->push_back(*type);
313         iResult++;
314         // this loop has to be changed in case of multiple output types
315         break;
316       }
317       type++;
318     }
319   } else {
320     iResult=-EINVAL;
321   }
322   return iResult;
323 }
324
325 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format) const
326 {
327   // see header file for function documentation
328   const char* fmt="publisher \'%s\'";
329   if (format) fmt=format;
330   HLTMessage(fmt, (DataType2Text(dt)).c_str());
331   HLTMessage("%x %x %x %x %x %x %x %x : %x %x %x %x", 
332              dt.fID[0],
333              dt.fID[1],
334              dt.fID[2],
335              dt.fID[3],
336              dt.fID[4],
337              dt.fID[5],
338              dt.fID[6],
339              dt.fID[7],
340              dt.fOrigin[0],
341              dt.fOrigin[1],
342              dt.fOrigin[2],
343              dt.fOrigin[3]);
344 }
345
346 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData ) const
347 {
348   // see header file for function documentation
349   blockData.fStructSize = sizeof(blockData);
350   FillShmData( blockData.fShmKey );
351   blockData.fOffset = ~(AliHLTUInt32_t)0;
352   blockData.fPtr = NULL;
353   blockData.fSize = 0;
354   FillDataType( blockData.fDataType );
355   blockData.fSpecification = kAliHLTVoidDataSpec;
356 }
357
358 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData ) const
359 {
360   // see header file for function documentation
361   shmData.fStructSize = sizeof(shmData);
362   shmData.fShmType = gkAliHLTComponentInvalidShmType;
363   shmData.fShmID = gkAliHLTComponentInvalidShmID;
364 }
365
366 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType ) const
367 {
368   // see header file for function documentation
369   dataType=kAliHLTAnyDataType;
370 }
371
372 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt) 
373 {
374   // see header file for function documentation
375   memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
376   memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
377 }
378
379 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin) 
380 {
381   // see header file for function documentation
382   tgtdt.fStructSize = sizeof(AliHLTComponentDataType);
383   memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
384   memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
385
386   if ((int)strlen(id)>kAliHLTComponentDataTypefIDsize) {
387     HLTWarning("data type id %s is too long, truncated to %d", id, kAliHLTComponentDataTypefIDsize);
388   }
389   strncpy(&tgtdt.fID[0], id, kAliHLTComponentDataTypefIDsize);
390
391   if ((int)strlen(origin)>kAliHLTComponentDataTypefOriginSize) {
392     HLTWarning("data type origin %s is too long, truncated to %d", origin, kAliHLTComponentDataTypefOriginSize);
393   }
394   strncpy(&tgtdt.fOrigin[0], origin, kAliHLTComponentDataTypefOriginSize);
395 }
396
397 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
398 {
399   // see header file for function documentation
400   memset(&evtData, 0, sizeof(AliHLTComponentEventData));
401   evtData.fStructSize=sizeof(AliHLTComponentEventData);
402 }
403
404 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt) 
405 {
406   // see header file for function documentation
407   TString msg;
408   msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
409   for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
410    if (dt.fID[i]!=0) msg+=dt.fID[i];
411    else msg+="\\0";
412   }
413   msg+="\" Origin=\"";
414   for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
415    if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
416    else msg+="\\0";
417   }
418   msg+="\"";
419   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
420 }
421
422 int AliHLTComponent::GetEventCount() const
423 {
424   // see header file for function documentation
425   return fEventCount;
426 }
427
428 int AliHLTComponent::IncrementEventCounter()
429 {
430   // see header file for function documentation
431   if (fEventCount>=0) fEventCount++;
432   return fEventCount;
433 }
434
435 int AliHLTComponent::GetNumberOfInputBlocks() const
436 {
437   // see header file for function documentation
438   if (fpInputBlocks!=NULL) {
439     return fCurrentEventData.fBlockCnt;
440   }
441   return 0;
442 }
443
444 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
445                                                     const char* classname,
446                                                     int bForce)
447 {
448   // see header file for function documentation
449   ALIHLTCOMPONENT_BASE_STOPWATCH();
450   fSearchDataType=dt;
451   if (classname) fClassName=classname;
452   else fClassName.clear();
453   int idx=FindInputBlock(fSearchDataType, 0);
454   HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
455   TObject* pObj=NULL;
456   if (idx>=0) {
457     if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
458       fCurrentInputBlock=idx;
459     } else {
460     }
461   }
462   return pObj;
463 }
464
465 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID, 
466                                                     const char* dtOrigin,
467                                                     const char* classname,
468                                                     int         bForce)
469 {
470   // see header file for function documentation
471   ALIHLTCOMPONENT_BASE_STOPWATCH();
472   AliHLTComponentDataType dt;
473   SetDataType(dt, dtID, dtOrigin);
474   return GetFirstInputObject(dt, classname, bForce);
475 }
476
477 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
478 {
479   // see header file for function documentation
480   ALIHLTCOMPONENT_BASE_STOPWATCH();
481   int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
482   //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
483   TObject* pObj=NULL;
484   if (idx>=0) {
485     if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
486       fCurrentInputBlock=idx;
487     }
488   }
489   return pObj;
490 }
491
492 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx) const
493 {
494   // see header file for function documentation
495   int iResult=-ENOENT;
496   if (fpInputBlocks!=NULL) {
497     int idx=startIdx<0?0:startIdx;
498     for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
499       if (dt == kAliHLTAnyDataType || fpInputBlocks[idx].fDataType == dt) {
500         iResult=idx;
501       }
502     }
503   }
504   return iResult;
505 }
506
507 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
508 {
509   // see header file for function documentation
510   TObject* pObj=NULL;
511   if (fpInputBlocks!=NULL) {
512     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
513       if (fpInputBlocks[idx].fPtr) {
514         AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
515         if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
516           HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
517           AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
518           TClass* objclass=msg.GetClass();
519           pObj=msg.ReadObject(objclass);
520           if (pObj && objclass) {
521             HLTDebug("object %p type %s created", pObj, objclass->GetName());
522           } else {
523           }
524         } else {
525           //    } else if (bForce!=0) {
526           HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
527         }
528       } else {
529         HLTFatal("block descriptor empty");
530       }
531     } else {
532       HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
533     }
534   } else {
535     HLTError("no input blocks available");
536   }
537   
538   return pObj;
539 }
540
541 TObject* AliHLTComponent::GetInputObject(int idx, const char* classname, int bForce)
542 {
543   // see header file for function documentation
544   if (fpInputObjects==NULL) {
545     fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
546   }
547   TObject* pObj=NULL;
548   if (fpInputObjects) {
549     pObj=fpInputObjects->At(idx);
550     if (pObj==NULL) {
551       pObj=CreateInputObject(idx, bForce);
552       if (pObj) {
553         fpInputObjects->AddAt(pObj, idx);
554       }
555     }
556   } else {
557     HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
558   }
559   return pObj;
560 }
561
562 int AliHLTComponent::CleanupInputObjects()
563 {
564   // see header file for function documentation
565   if (!fpInputObjects) return 0;
566   TObjArray* array=fpInputObjects;
567   fpInputObjects=NULL;
568   for (int i=0; i<array->GetEntries(); i++) {
569     TObject* pObj=array->At(i);
570     if (pObj) delete pObj;
571   }
572   delete array;
573   return 0;
574 }
575
576 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
577 {
578   // see header file for function documentation
579   ALIHLTCOMPONENT_BASE_STOPWATCH();
580   AliHLTComponentDataType dt=kAliHLTVoidDataType;
581   int idx=fCurrentInputBlock;
582   if (pObject) {
583     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
584     } else {
585       HLTError("unknown object %p", pObject);
586     }
587   }
588   if (idx>=0) {
589     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
590       dt=fpInputBlocks[idx].fDataType;
591     } else {
592       HLTFatal("severe internal error, index out of range");
593     }
594   }
595   return dt;
596 }
597
598 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
599 {
600   // see header file for function documentation
601   ALIHLTCOMPONENT_BASE_STOPWATCH();
602   AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
603   int idx=fCurrentInputBlock;
604   if (pObject) {
605     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
606     } else {
607       HLTError("unknown object %p", pObject);
608     }
609   }
610   if (idx>=0) {
611     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
612       iSpec=fpInputBlocks[idx].fSpecification;
613     } else {
614       HLTFatal("severe internal error, index out of range");
615     }
616   }
617   return iSpec;
618 }
619
620 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
621 {
622   // see header file for function documentation
623   ALIHLTCOMPONENT_BASE_STOPWATCH();
624   fSearchDataType=dt;
625   fClassName.clear();
626   int idx=FindInputBlock(fSearchDataType, 0);
627   const AliHLTComponentBlockData* pBlock=NULL;
628   if (idx>=0) {
629     // check for fpInputBlocks pointer done in FindInputBlock
630     pBlock=&fpInputBlocks[idx];
631   }
632   return pBlock;
633 }
634
635 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID, 
636                                                                     const char* dtOrigin)
637 {
638   // see header file for function documentation
639   ALIHLTCOMPONENT_BASE_STOPWATCH();
640   AliHLTComponentDataType dt;
641   SetDataType(dt, dtID, dtOrigin);
642   return GetFirstInputBlock(dt);
643 }
644
645 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
646 {
647   // see header file for function documentation
648   ALIHLTCOMPONENT_BASE_STOPWATCH();
649   int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
650   const AliHLTComponentBlockData* pBlock=NULL;
651   if (idx>=0) {
652     // check for fpInputBlocks pointer done in FindInputBlock
653     pBlock=&fpInputBlocks[idx];
654   }
655   return pBlock;
656 }
657
658 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
659 {
660   // see header file for function documentation
661   int iResult=-ENOENT;
662   if (fpInputBlocks!=NULL) {
663     if (pBlock) {
664       if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
665         iResult=(int)(pBlock-fpInputBlocks);
666       }
667     } else {
668       iResult=-EINVAL;
669     }
670   }
671   return iResult;
672 }
673
674 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
675 {
676   // see header file for function documentation
677   ALIHLTCOMPONENT_BASE_STOPWATCH();
678   AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
679   int idx=fCurrentInputBlock;
680   if (pBlock) {
681     if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
682     } else {
683       HLTError("unknown Block %p", pBlock);
684     }
685   }
686   if (idx>=0) {
687     // check for fpInputBlocks pointer done in FindInputBlock
688     iSpec=fpInputBlocks[idx].fSpecification;
689   }
690   return iSpec;
691 }
692
693 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
694 {
695   // see header file for function documentation
696   ALIHLTCOMPONENT_BASE_STOPWATCH();
697   int iResult=0;
698   if (pObject) {
699     AliHLTMessage msg(kMESS_OBJECT);
700     msg.WriteObject(pObject);
701     Int_t iMsgLength=msg.Length();
702     if (iMsgLength>0) {
703       msg.SetLength(); // sets the length to the first (reserved) word
704       iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec);
705       if (iResult>=0) {
706         HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
707       }
708     } else {
709       HLTError("object serialization failed for object %p", pObject);
710       iResult=-ENOMSG;
711     }
712   } else {
713     iResult=-EINVAL;
714   }
715   return iResult;
716 }
717
718 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
719 {
720   // see header file for function documentation
721   ALIHLTCOMPONENT_BASE_STOPWATCH();
722   AliHLTComponentDataType dt;
723   SetDataType(dt, dtID, dtOrigin);
724   return PushBack(pObject, dt, spec);
725 }
726
727 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
728 {
729   // see header file for function documentation
730   ALIHLTCOMPONENT_BASE_STOPWATCH();
731   return InsertOutputBlock(pBuffer, iSize, dt, spec);
732 }
733
734 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec)
735 {
736   // see header file for function documentation
737   ALIHLTCOMPONENT_BASE_STOPWATCH();
738   AliHLTComponentDataType dt;
739   SetDataType(dt, dtID, dtOrigin);
740   return PushBack(pBuffer, iSize, dt, spec);
741 }
742
743 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec)
744 {
745   // see header file for function documentation
746   int iResult=0;
747   if (pBuffer) {
748     if (fpOutputBuffer && iSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
749       AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
750       AliHLTComponentBlockData bd;
751       FillBlockData( bd );
752       bd.fOffset        = fOutputBufferFilled;
753       bd.fPtr           = pTgt;
754       bd.fSize          = iSize;
755       bd.fDataType      = dt;
756       bd.fSpecification = spec;
757       if (pBuffer!=NULL && pBuffer!=pTgt) {
758         memcpy(pTgt, pBuffer, iSize);
759         //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer); 
760         //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iSize, pBuffer, pTgt, firstWord);
761       }
762       fOutputBufferFilled+=bd.fSize;
763       fOutputBlocks.push_back( bd );
764       //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iSize, DataType2Text(dt).c_str(), spec);
765     } else {
766       if (fpOutputBuffer) {
767         HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iSize);
768       } else {
769         HLTError("output buffer not available");
770       }
771       iResult=-ENOSPC;
772     }
773   } else {
774     iResult=-EINVAL;
775   }
776   return iResult;
777 }
778
779 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
780 {
781   // see header file for function documentation
782   if (!pObject) return -EINVAL;
783     AliHLTMessage msg(kMESS_OBJECT);
784     msg.WriteObject(pObject);
785     return msg.Length();  
786 }
787
788 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
789 {
790   // see header file for function documentation
791   int iResult=-ENOSYS;
792   //#warning  function not yet implemented
793   HLTWarning("function not yet implemented");
794   return iResult;
795 }
796
797 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
798                                    const AliHLTComponentBlockData* blocks, 
799                                    AliHLTComponentTriggerData& trigData,
800                                    AliHLTUInt8_t* outputPtr, 
801                                    AliHLTUInt32_t& size,
802                                    AliHLTUInt32_t& outputBlockCnt, 
803                                    AliHLTComponentBlockData*& outputBlocks,
804                                    AliHLTComponentEventDoneData*& edd )
805 {
806   // see header file for function documentation
807   ALIHLTCOMPONENT_BASE_STOPWATCH();
808   int iResult=0;
809   fCurrentEvent=evtData.fEventID;
810   fCurrentEventData=evtData;
811   fpInputBlocks=blocks;
812   fCurrentInputBlock=-1;
813   fSearchDataType=kAliHLTAnyDataType;
814   fpOutputBuffer=outputPtr;
815   fOutputBufferSize=size;
816   fOutputBufferFilled=0;
817   fOutputBlocks.clear();
818   
819   vector<AliHLTComponentBlockData> blockData;
820   { // dont delete, sets the scope for the stopwatch guard
821     ALIHLTCOMPONENT_DA_STOPWATCH();
822     iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
823   } // end of the scope of the stopwatch guard
824   if (iResult>=0) {
825     if (fOutputBlocks.size()>0) {
826       //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
827       if (blockData.size()>0) {
828         HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
829         iResult=-EFAULT;
830       } else {
831         iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
832         size=fOutputBufferFilled;
833       }
834     } else {
835       iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
836     }
837     if (iResult<0) {
838       HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
839     }
840   }
841   if (iResult<0) {
842     outputBlockCnt=0;
843     outputBlocks=NULL;
844   }
845   CleanupInputObjects();
846   IncrementEventCounter();
847   return iResult;
848 }
849
850 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
851   :
852   fpStopwatch(NULL),
853   fpPrec(NULL)
854 {
855   // standard constructor (not for use)
856 }
857
858 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
859   :
860   fpStopwatch(pStopwatch),
861   fpPrec(NULL)
862 {
863   // constructor
864
865   // check for already existing guard
866   if (fgpCurrent) fpPrec=fgpCurrent;
867   fgpCurrent=this;
868
869   // stop the preceeding guard if it controls a different stopwatch
870   int bStart=1;
871   if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
872
873   // start the stopwatch if the current guard controls a different one
874   if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
875 }
876
877 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(AliHLTStopwatchGuard&)
878   :
879   fpStopwatch(NULL),
880   fpPrec(NULL)
881 {
882   // copy constructor (not for use)
883 }
884
885 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
886
887 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
888 {
889   // destructor
890
891   // resume the preceeding guard if it controls a different stopwatch
892   int bStop=1;
893   if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
894
895   // stop the stopwatch if the current guard controls a different one
896   if (fpStopwatch && bStop==1) fpStopwatch->Stop();
897
898   // resume to the preceeding guard
899   fgpCurrent=fpPrec;
900 }
901
902 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
903 {
904   // see header file for function documentation
905   if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
906   return fpStopwatch!=pSucc?1:0;
907 }
908
909 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
910 {
911   // see header file for function documentation
912   if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
913   return fpStopwatch!=pSucc?1:0;
914 }
915
916 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type) 
917 {
918   // see header file for function documentation
919   int iResult=0;
920   if (pSW!=NULL && type<kSWTypeCount) {
921     if (fpStopwatches) {
922       TObject* pObj=fpStopwatches->At((int)type);
923       if (pSW==NULL        // explicit reset
924           || pObj==NULL) { // explicit set
925         fpStopwatches->AddAt(pSW, (int)type);
926       } else if (pObj!=pSW) {
927         HLTWarning("stopwatch %d already set, reset first", (int)type);
928         iResult=-EBUSY;
929       }
930     }
931   } else {
932     iResult=-EINVAL;
933   }
934   return iResult;
935 }
936
937 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
938 {
939   // see header file for function documentation
940   if (pStopwatches==NULL) return -EINVAL;
941
942   int iResult=0;
943   for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
944     SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
945   return iResult;
946 }