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