]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTComponent.cxx
correcting allocation of EventDoneData in order to support multiple calls
[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 "TMath.h"
35 #include "TObjArray.h"
36 #include "TObjectTable.h"
37 #include "TClass.h"
38 #include "TStopwatch.h"
39 #include "TFormula.h"
40 #include "AliHLTMemoryFile.h"
41 #include "AliHLTMisc.h"
42 #include <cassert>
43 #include <stdint.h>
44
45 /**
46  * default compression level for ROOT objects
47  */
48 #define ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION 5
49 #define ALIHLTCOMPONENT_STATTIME_SCALER 1000000
50
51 /** ROOT macro for the implementation of ROOT specific class methods */
52 ClassImp(AliHLTComponent);
53
54 /** stopwatch macro using the stopwatch guard */
55 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
56 //#define ALIHLTCOMPONENT_STOPWATCH(type) 
57
58 /** stopwatch macro for operations of the base class */
59 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
60 /** stopwatch macro for operations of the detector algorithm (DA) */
61 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
62
63 AliHLTComponent::AliHLTComponent()
64   :
65   fEnvironment(),
66   fCurrentEvent(0),
67   fEventCount(-1),
68   fFailedEvents(0),
69   fCurrentEventData(),
70   fpInputBlocks(NULL),
71   fCurrentInputBlock(-1),
72   fSearchDataType(kAliHLTVoidDataType),
73   fClassName(),
74   fpInputObjects(NULL),
75   fpOutputBuffer(NULL),
76   fOutputBufferSize(0),
77   fOutputBufferFilled(0),
78   fOutputBlocks(),
79   fpStopwatches(new TObjArray(kSWTypeCount)),
80   fMemFiles(),
81   fpRunDesc(NULL),
82   fpDDLList(NULL),
83   fCDBSetRunNoFunc(false),
84   fChainId(),
85   fChainIdCrc(0),
86   fpBenchmark(NULL),
87   fRequireSteeringBlocks(false),
88   fEventType(gkAliEventTypeUnknown),
89   fComponentArgs(),
90   fEventDoneData(NULL),
91   fEventDoneDataSize(0),
92   fCompressionLevel(ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION)
93   , fLastObjectSize(0)
94   , fpTriggerClasses(NULL)
95 {
96   // see header file for class documentation
97   // or
98   // refer to README to build package
99   // or
100   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
101   memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
102   if (fgpComponentHandler)
103     fgpComponentHandler->ScheduleRegister(this);
104   //SetLocalLoggingLevel(kHLTLogDefault);
105 }
106
107 AliHLTComponent::~AliHLTComponent()
108 {
109   // see header file for function documentation
110   if (fpBenchmark) delete fpBenchmark;
111   fpBenchmark=NULL;
112
113   CleanupInputObjects();
114   if (fpStopwatches!=NULL) delete fpStopwatches;
115   fpStopwatches=NULL;
116   AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
117   while (element!=fMemFiles.end()) {
118     if (*element) {
119       if ((*element)->IsClosed()==0) {
120         HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
121         // close but do not flush as we dont know whether the buffer is still valid
122         (*element)->CloseMemoryFile(0);
123       }
124       delete *element;
125       *element=NULL;
126     }
127     element++;
128   }
129   if (fpRunDesc) {
130     delete fpRunDesc;
131     fpRunDesc=NULL;
132   }
133   if (fEventDoneData)
134     delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
135   fEventDoneData=NULL;
136
137   if (fpTriggerClasses) {
138     fpTriggerClasses->Delete();
139     delete fpTriggerClasses;
140   }
141   fpTriggerClasses=NULL;
142 }
143
144 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
145
146 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite) 
147 {
148   // see header file for function documentation
149   int iResult=0;
150   if (fgpComponentHandler==NULL || bOverwrite!=0)
151     fgpComponentHandler=pCH;
152   else
153     iResult=-EPERM;
154   return iResult;
155 }
156
157 int AliHLTComponent::UnsetGlobalComponentHandler() 
158 {
159   // see header file for function documentation
160   return SetGlobalComponentHandler(NULL,1);
161 }
162
163 int AliHLTComponent::SetComponentEnvironment(const AliHLTAnalysisEnvironment* comenv, void* environParam)
164 {
165   // see header file for function documentation
166   HLTLogKeyword(fChainId.c_str());
167   int iResult=0;
168   if (comenv) {
169     memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
170     memcpy(&fEnvironment, comenv, comenv->fStructSize<sizeof(AliHLTAnalysisEnvironment)?comenv->fStructSize:sizeof(AliHLTAnalysisEnvironment));
171     fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
172     fEnvironment.fParam=environParam;
173   }
174   return iResult;
175 }
176
177 int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environParam, int argc, const char** argv )
178 {
179   // see header file for function documentation
180   HLTLogKeyword(fChainId.c_str());
181   int iResult=0;
182   if (comenv) {
183     SetComponentEnvironment(comenv, environParam);
184   }
185   fComponentArgs="";
186   const char** pArguments=NULL;
187   int iNofChildArgs=0;
188   TString argument="";
189   int bMissingParam=0;
190   if (argc>0) {
191     pArguments=new const char*[argc];
192     if (pArguments) {
193       for (int i=0; i<argc && iResult>=0; i++) {
194         if (fComponentArgs.size()>0) fComponentArgs+=" ";
195         fComponentArgs+=argv[i];
196         argument=argv[i];
197         if (argument.IsNull()) continue;
198
199         // benchmark
200         if (argument.CompareTo("-benchmark")==0) {
201
202           // -loglevel=
203         } else if (argument.BeginsWith("-loglevel=")) {
204           TString parameter=argument.ReplaceAll("-loglevel=", "");
205           parameter.Remove(TString::kLeading, ' '); // remove all blanks
206           if (parameter.BeginsWith("0x") &&
207               parameter.Replace(0,2,"",0).IsHex()) {
208             unsigned int loglevel=kHLTLogNone;
209             sscanf(parameter.Data(),"%x", &loglevel);
210             SetLocalLoggingLevel((AliHLTComponentLogSeverity)loglevel);
211           } else {
212             HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
213             iResult=-EINVAL;
214           }
215           // -object-compression=
216         } else if (argument.BeginsWith("-object-compression=")) {
217           argument.ReplaceAll("-object-compression=", "");
218           if (argument.IsDigit()) {
219             fCompressionLevel=argument.Atoi();
220             if (fCompressionLevel<0 || fCompressionLevel>9) {
221               HLTWarning("invalid compression level %d, setting to default %d", fCompressionLevel, ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION);
222               fCompressionLevel=ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION;
223             }
224           } else {
225             HLTError("wrong parameter for argument -object-compression, number expected");
226           }
227         } else {
228           pArguments[iNofChildArgs++]=argv[i];
229         }
230       }
231     } else {
232       iResult=-ENOMEM;
233     }
234   }
235   if (bMissingParam) {
236     HLTError("missing parameter for argument %s", argument.Data());
237     iResult=-EINVAL;
238   }
239   if (iResult>=0) {
240     iResult=DoInit(iNofChildArgs, pArguments);
241   }
242   if (iResult>=0) {
243     fEventCount=0;
244
245     // find out if the component wants to get the steering events
246     // explicitly
247     AliHLTComponentDataTypeList inputDt;
248     GetInputDataTypes(inputDt);
249     for (AliHLTComponentDataTypeList::iterator dt=inputDt.begin();
250          dt!=inputDt.end() && !fRequireSteeringBlocks;
251          dt++) {
252       fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeSOR);
253       fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeRunType);
254       fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeEOR);
255       fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeDDL);
256       fRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeComponentStatistics);
257     }
258   }
259   if (pArguments) delete [] pArguments;
260
261 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
262   // benchmarking stopwatch for the component statistics
263   fpBenchmark=new TStopwatch;
264 #endif
265
266   return iResult;
267 }
268
269 int AliHLTComponent::Deinit()
270 {
271   // see header file for function documentation
272   HLTLogKeyword(fChainId.c_str());
273   int iResult=0;
274   iResult=DoDeinit();
275   if (fpRunDesc) {
276     // TODO: the warning should be kept, but the condition is wrong since the
277     // AliHLTRunDesc is set before the SOR event in the SetRunDescription
278     // method. A couple of state flags should be defined but that is a bit more
279     // work to do. For the moment disable the warning (2009-07-01)
280     //HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
281     AliHLTRunDesc* pRunDesc=fpRunDesc;
282     fpRunDesc=NULL;
283     delete pRunDesc;
284   }
285   if (fpTriggerClasses) {
286     fpTriggerClasses->Delete();
287     delete fpTriggerClasses;
288   }
289   fpTriggerClasses=NULL;
290
291   fEventCount=0;
292   return iResult;
293 }
294
295 int AliHLTComponent::InitCDB(const char* cdbPath, AliHLTComponentHandler* pHandler)
296 {
297   // see header file for function documentation
298   int iResult=0;
299   HLTInfo("Using CDB: %s", cdbPath);
300   if (pHandler) {
301   // I have to think about separating the library handling from the
302   // component handler. Requiring the component handler here is not
303   // the cleanest solution.
304   // We presume the library already to be loaded, which is the case
305   // because it is loaded in the initialization of the logging functionality
306   //
307   // find the symbol
308   AliHLTMiscInitCDB_t pFunc=(AliHLTMiscInitCDB_t)pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_INIT_CDB);
309   if (pFunc) {
310     TString path;
311     if (cdbPath && cdbPath[0]!=0) {
312       path=cdbPath;
313       // very temporary fix, have to check for other formats
314       if (!path.BeginsWith("local://")) {
315         path="local://";
316         path+=cdbPath;
317       }
318     }
319     if ((iResult=(*pFunc)(path.Data()))>=0) {
320       if (!(fCDBSetRunNoFunc=pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_SET_CDB_RUNNO))) {
321         Message(NULL, kHLTLogWarning, "AliHLTComponent::InitCDB", "init CDB",
322                 "can not find function to set CDB run no");
323       }
324     }
325   } else {
326     Message(NULL, kHLTLogError, "AliHLTComponent::InitCDB", "init CDB",
327             "can not find initialization function");
328     iResult=-ENOSYS;
329   }
330   } else {
331     iResult=-EINVAL;
332   }
333   return iResult;
334 }
335
336 int AliHLTComponent::SetCDBRunNo(int runNo)
337 {
338   // see header file for function documentation
339   if (!fCDBSetRunNoFunc) return 0;
340   return (*((AliHLTMiscSetCDBRunNo_t)fCDBSetRunNoFunc))(runNo);
341 }
342
343 int AliHLTComponent::SetRunDescription(const AliHLTRunDesc* desc, const char* /*runType*/)
344 {
345   // see header file for function documentation
346   if (!desc) return -EINVAL;
347   if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
348     HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
349     return -EINVAL;
350   }
351
352   if (!fpRunDesc) {
353     fpRunDesc=new AliHLTRunDesc;
354     if (!fpRunDesc) return -ENOMEM;
355     *fpRunDesc=kAliHLTVoidRunDesc;
356   }
357
358   if (fpRunDesc->fRunNo!=kAliHLTVoidRunNo && fpRunDesc->fRunNo!=desc->fRunNo) {
359     HLTWarning("Run description has already been set");
360   }
361   *fpRunDesc=*desc;
362   SetCDBRunNo(fpRunDesc->fRunNo);
363   // TODO: we have to decide about the runType
364   return 0;
365 }
366
367 int AliHLTComponent::SetComponentDescription(const char* desc)
368 {
369   // see header file for function documentation
370   int iResult=0;
371   if (!desc) return 0;
372
373   TString descriptor=desc;
374   TObjArray* pTokens=descriptor.Tokenize(" ");
375   if (pTokens) {
376     for (int i=0; i<pTokens->GetEntries() && iResult>=0; i++) {
377       TString argument=((TObjString*)pTokens->At(i++))->GetString();
378       if (!argument || argument.IsNull()) continue;
379
380       // chainid
381       if (argument.BeginsWith("chainid")) {
382         argument.ReplaceAll("chainid", "");
383         if (argument.BeginsWith("=")) {
384           fChainId=argument.Replace(0,1,"");
385           fChainIdCrc=CalculateChecksum((const AliHLTUInt8_t*)fChainId.c_str(), fChainId.length());
386           HLTDebug("setting component description: chain id %s crc 0x%8x", fChainId.c_str(), fChainIdCrc);
387         } else {
388           fChainId="";
389         }
390       } else {
391         HLTWarning("unknown component description %s", argument.Data());
392       }
393     }
394   }
395   
396   return iResult;
397 }
398
399 int AliHLTComponent::ConfigureFromArgumentString(int argc, const char** argv)
400 {
401   // see header file for function documentation
402   int iResult=0;
403   vector<const char*> array;
404   TObjArray choppedArguments;
405   TString argument="";
406   int i=0;
407   for (i=0; i<argc && iResult>=0; i++) {
408     argument=argv[i];
409     if (argument.IsNull()) continue;
410     TObjArray* pTokens=argument.Tokenize(" ");
411     if (pTokens) {
412       if (pTokens->GetEntriesFast()>0) {
413         for (int n=0; n<pTokens->GetEntriesFast(); n++) {
414           choppedArguments.AddLast(pTokens->At(n));
415           TString data=((TObjString*)pTokens->At(n))->GetString();
416           if (!data.IsNull()) {
417             array.push_back(data.Data());
418           }
419         }
420         pTokens->SetOwner(kFALSE);
421       }
422       delete pTokens;
423     }
424   }
425
426   for (i=0; (unsigned)i<array.size() && iResult>=0;) {
427     int result=ScanConfigurationArgument(array.size()-i, &array[i]);
428     if (result==0) {
429       HLTWarning("unknown component argument %s", array[i]);
430       i++;
431     } else if (result>0) {
432       i+=result;
433     } else {
434       iResult=result;
435       if (iResult==-EINVAL) {
436         HLTError("unknown argument %s", array[i]);
437       } else if (iResult==-EPROTO) {
438         HLTError("missing/wrong parameter for argument %s (%s)", array[i], (array.size()>(unsigned)i+1)?array[i+1]:"missing");
439       } else {
440         HLTError("scan of argument %s failed (%d)", array[i], iResult);
441       }
442     }
443   }
444
445   return iResult;
446 }
447
448 int AliHLTComponent::ConfigureFromCDBTObjString(const char* entries)
449 {
450   // see header file for function documentation
451   int iResult=0;
452   TString arguments;
453   TString confEntries=entries;
454   TObjArray* pTokens=confEntries.Tokenize(" ");
455   if (pTokens) {
456     for (int n=0; n<pTokens->GetEntriesFast(); n++) {
457       const char* path=((TObjString*)pTokens->At(n))->GetString().Data();
458       const char* chainId=GetChainId();
459       HLTInfo("configure from entry %s, chain id %s", path, (chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
460       TObject* pOCDBObject = LoadAndExtractOCDBObject(path);
461       if (pOCDBObject) {
462         TObjString* pString=dynamic_cast<TObjString*>(pOCDBObject);
463         if (pString) {
464           HLTInfo("received configuration object string: \'%s\'", pString->GetString().Data());
465           arguments+=pString->GetString().Data();
466           arguments+=" ";
467         } else {
468           HLTError("configuration object \"%s\" has wrong type, required TObjString", path);
469           iResult=-EINVAL;
470         }
471       } else {
472         HLTError("can not fetch object \"%s\" from OCDB", path);
473         iResult=-ENOENT;
474       }
475     }
476     delete pTokens;
477   }
478   if (iResult>=0 && !arguments.IsNull())  {
479     const char* array=arguments.Data();
480     iResult=ConfigureFromArgumentString(1, &array);
481   }
482   return iResult;
483 }
484
485 TObject* AliHLTComponent::LoadAndExtractOCDBObject(const char* path, int version, int subVersion)
486 {
487   // see header file for function documentation
488   AliCDBEntry* pEntry=AliHLTMisc::Instance().LoadOCDBEntry(path, GetRunNo(), version, subVersion);
489   if (!pEntry) return NULL;
490   return AliHLTMisc::Instance().ExtractObject(pEntry);
491 }
492
493 int AliHLTComponent::DoInit( int /*argc*/, const char** /*argv*/)
494 {
495   // default implementation, childs can overload
496   HLTLogKeyword("dummy");
497   return 0;
498 }
499
500 int AliHLTComponent::DoDeinit()
501 {
502   // default implementation, childs can overload
503   HLTLogKeyword("dummy");
504   return 0;
505 }
506
507 int AliHLTComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/)
508 {
509   // default implementation, childs can overload
510   HLTLogKeyword("dummy");
511   return 0;
512 }
513
514 int AliHLTComponent::ReadPreprocessorValues(const char* /*modules*/)
515 {
516   // default implementation, childs can overload
517   HLTLogKeyword("dummy");
518   return 0;
519 }
520
521 int AliHLTComponent::ScanConfigurationArgument(int /*argc*/, const char** /*argv*/)
522 {
523   // default implementation, childs can overload
524   HLTLogKeyword("dummy");
525   HLTWarning("The function needs to be implemented by the component");
526   return 0;
527 }
528
529 int AliHLTComponent::StartOfRun()
530 {
531   // default implementation, childs can overload
532   HLTLogKeyword("dummy");
533   return 0;
534 }
535
536 int AliHLTComponent::EndOfRun()
537 {
538   // default implementation, childs can overload
539   HLTLogKeyword("dummy");
540   return 0;
541 }
542
543
544 int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
545 {
546   // default implementation, childs can overload
547   HLTLogKeyword("dummy");
548   return 0;
549 }
550
551 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
552 {
553   // see header file for function documentation
554   memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
555   strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
556   strcat( output, ":" );
557   strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
558 }
559
560 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
561 {
562   // see header file for function documentation
563   string out("");
564
565   if (mode==2) {
566     int i=0;
567     char tmp[8];
568     for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
569       sprintf(tmp, "'%d", type.fOrigin[i]);
570       out+=tmp;
571     }
572     out+="':'";
573     for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
574       sprintf(tmp, "%d'", type.fID[i]);
575       out+=tmp;
576     }
577     return out;
578   }
579
580   if (mode==1) {
581     int i=0;
582     char tmp[8];
583     for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
584       unsigned char* puc=(unsigned char*)type.fOrigin;
585       if ((puc[i])<32)
586         sprintf(tmp, "'\\%x", type.fOrigin[i]);
587       else
588         sprintf(tmp, "'%c", type.fOrigin[i]);
589       out+=tmp;
590     }
591     out+="':'";
592     for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
593       unsigned char* puc=(unsigned char*)type.fID;
594       if (puc[i]<32)
595         sprintf(tmp, "\\%x'", type.fID[i]);
596       else
597         sprintf(tmp, "%c'", type.fID[i]);
598       out+=tmp;
599     }
600     return out;
601   }
602
603   if (type==kAliHLTVoidDataType) {
604     out="VOID:VOID";
605   } else {
606     // some gymnastics in order to avoid a '0' which is part of either or both
607     // ID and origin terminating the whole string. Unfortunately, string doesn't
608     // stop appending at the '0' if the number of elements to append was 
609     // explicitely specified
610     string tmp("");
611     tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
612     out.append(tmp.c_str());
613     out.append(":");
614     tmp="";
615     tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
616     out.append(tmp.c_str());
617   }
618   return out;
619 }
620
621
622 void* AliHLTComponent::AllocMemory( unsigned long size ) 
623 {
624   // see header file for function documentation
625   if (fEnvironment.fAllocMemoryFunc)
626     return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
627   HLTFatal("no memory allocation handler registered");
628   return NULL;
629 }
630
631 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
632                                               AliHLTComponentBlockData** outputBlocks ) 
633 {
634   // see header file for function documentation
635     if ( blockCount==NULL || outputBlocks==NULL )
636         return -EFAULT;
637     AliHLTUInt32_t count = blocks.size();
638     if ( !count )
639         {
640         *blockCount = 0;
641         *outputBlocks = NULL;
642         return 0;
643         }
644     *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
645     if ( !*outputBlocks )
646         return -ENOMEM;
647     for ( unsigned long i = 0; i < count; i++ ) {
648         (*outputBlocks)[i] = blocks[i];
649         if (MatchExactly(blocks[i].fDataType, kAliHLTAnyDataType)) {
650           (*outputBlocks)[i].fDataType=GetOutputDataType();
651           /* data type was set to the output data type by the PubSub AliRoot
652              Wrapper component, if data type of the block was ********:****.
653              Now handled by the component base class in order to have same
654              behavior when running embedded in AliRoot
655           memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
656           memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
657           */
658         }
659     }
660     *blockCount = count;
661     return 0;
662
663 }
664
665 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd ) 
666 {
667   // see header file for function documentation
668   if (fEnvironment.fGetEventDoneDataFunc)
669     return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
670   return -ENOSYS;
671 }
672
673 int AliHLTComponent::ReserveEventDoneData( unsigned long size )
674 {
675   // see header file for function documentation
676   int iResult=0;
677
678   unsigned long capacity=fEventDoneDataSize;
679   if (fEventDoneData) capacity-=sizeof(AliHLTComponentEventDoneData)+fEventDoneData->fDataSize;
680   if (size>capacity) {
681     unsigned long newSize=sizeof(AliHLTComponentEventDoneData)+size+(fEventDoneDataSize-capacity);
682     AliHLTComponentEventDoneData* newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[newSize] );
683     if (!newEDD)
684       return -ENOMEM;
685     newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
686     newEDD->fDataSize = 0;
687     newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
688     if (fEventDoneData) {
689       memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
690       newEDD->fDataSize = fEventDoneData->fDataSize;
691       delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
692     }
693     fEventDoneData = newEDD;
694     fEventDoneDataSize = newSize;
695   }
696   return iResult;
697
698 }
699
700 int AliHLTComponent::PushEventDoneData( AliHLTUInt32_t eddDataWord )
701 {
702   if (!fEventDoneData)
703     return -ENOMEM;
704   if (fEventDoneData->fDataSize+sizeof(AliHLTUInt32_t)>fEventDoneDataSize)
705     return -ENOSPC;
706   *reinterpret_cast<AliHLTUInt32_t*>((reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize)) = eddDataWord;
707   fEventDoneData->fDataSize += sizeof(AliHLTUInt32_t);
708   return 0;
709 }
710
711 void AliHLTComponent::ReleaseEventDoneData()
712 {
713   if (fEventDoneData)
714     delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
715   fEventDoneData = NULL;
716   fEventDoneDataSize = 0;
717 }
718
719
720 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList) 
721 {
722   // see header file for function documentation
723   int iResult=0;
724   if (pConsumer) {
725     AliHLTComponentDataTypeList itypes;
726     AliHLTComponentDataTypeList otypes;
727     otypes.push_back(GetOutputDataType());
728     if (MatchExactly(otypes[0],kAliHLTMultipleDataType)) {
729       otypes.clear();
730       int count=0;
731       if ((count=GetOutputDataTypes(otypes))>0) {
732       } else if (GetComponentType()!=kSink) {
733         HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
734       }
735     }
736     ((AliHLTComponent*)pConsumer)->GetInputDataTypes(itypes);
737     AliHLTComponentDataTypeList::iterator otype=otypes.begin();
738     for (;otype!=otypes.end();otype++) {
739       //PrintDataTypeContent((*otype), "publisher \'%s\'");
740       if ((*otype)==(kAliHLTAnyDataType|kAliHLTDataOriginPrivate)) {
741         if (tgtList) tgtList->push_back(*otype);
742         iResult++;
743         continue;
744       }
745       
746       AliHLTComponentDataTypeList::iterator itype=itypes.begin();
747       for ( ; itype!=itypes.end() && (*itype)!=(*otype) ; itype++) {/* empty body */};
748       //if (itype!=itypes.end()) PrintDataTypeContent(*itype, "consumer \'%s\'");
749       if (itype!=itypes.end()) {
750         if (tgtList) tgtList->push_back(*otype);
751         iResult++;
752       }
753     }
754   } else {
755     iResult=-EINVAL;
756   }
757   return iResult;
758 }
759
760 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format)
761 {
762   // see header file for function documentation
763   const char* fmt="\'%s\'";
764   if (format) fmt=format;
765   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, Form(fmt, (DataType2Text(dt)).c_str()));
766   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, 
767                          Form("%x %x %x %x %x %x %x %x : %x %x %x %x", 
768                               dt.fID[0],
769                               dt.fID[1],
770                               dt.fID[2],
771                               dt.fID[3],
772                               dt.fID[4],
773                               dt.fID[5],
774                               dt.fID[6],
775                               dt.fID[7],
776                               dt.fOrigin[0],
777                               dt.fOrigin[1],
778                               dt.fOrigin[2],
779                               dt.fOrigin[3]));
780 }
781
782 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
783 {
784   // see header file for function documentation
785   blockData.fStructSize = sizeof(blockData);
786   FillShmData( blockData.fShmKey );
787   blockData.fOffset = ~(AliHLTUInt32_t)0;
788   blockData.fPtr = NULL;
789   blockData.fSize = 0;
790   FillDataType( blockData.fDataType );
791   blockData.fSpecification = kAliHLTVoidDataSpec;
792 }
793
794 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
795 {
796   // see header file for function documentation
797   shmData.fStructSize = sizeof(shmData);
798   shmData.fShmType = gkAliHLTComponentInvalidShmType;
799   shmData.fShmID = gkAliHLTComponentInvalidShmID;
800 }
801
802 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
803 {
804   // see header file for function documentation
805   dataType=kAliHLTAnyDataType;
806 }
807
808 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt) 
809 {
810   // see header file for function documentation
811   memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
812   memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
813 }
814
815 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin) 
816 {
817   // see header file for function documentation
818   tgtdt.fStructSize=sizeof(AliHLTComponentDataType);
819   if (id) {
820     memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
821     strncpy(&tgtdt.fID[0], id, strlen(id)<(size_t)kAliHLTComponentDataTypefIDsize?strlen(id):kAliHLTComponentDataTypefIDsize);
822   }
823   if (origin) {
824     memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
825     strncpy(&tgtdt.fOrigin[0], origin, strlen(origin)<(size_t)kAliHLTComponentDataTypefOriginSize?strlen(origin):kAliHLTComponentDataTypefOriginSize);
826   }
827 }
828
829 void AliHLTComponent::SetDataType(AliHLTComponentDataType& dt, AliHLTUInt64_t id, AliHLTUInt32_t origin)
830 {
831   // see header file for function documentation
832   dt.fStructSize=sizeof(AliHLTComponentDataType);
833   assert(kAliHLTComponentDataTypefIDsize==sizeof(id));
834   assert(kAliHLTComponentDataTypefOriginSize==sizeof(origin));
835   memcpy(&dt.fID, &id, kAliHLTComponentDataTypefIDsize);
836   memcpy(&dt.fOrigin, &origin, kAliHLTComponentDataTypefOriginSize);
837 }
838
839 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
840 {
841   // see header file for function documentation
842   memset(&evtData, 0, sizeof(AliHLTComponentEventData));
843   evtData.fStructSize=sizeof(AliHLTComponentEventData);
844   evtData.fEventID=kAliHLTVoidEventID;
845 }
846
847 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt) 
848 {
849   // see header file for function documentation
850   TString msg;
851   msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
852   for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
853    if (dt.fID[i]!=0) msg+=dt.fID[i];
854    else msg+="\\0";
855   }
856   msg+="\" Origin=\"";
857   for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
858    if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
859    else msg+="\\0";
860   }
861   msg+="\"";
862   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
863 }
864
865 int AliHLTComponent::GetEventCount() const
866 {
867   // see header file for function documentation
868   return fEventCount;
869 }
870
871 int AliHLTComponent::IncrementEventCounter()
872 {
873   // see header file for function documentation
874   if (fEventCount>=0) fEventCount++;
875   return fEventCount;
876 }
877
878 int AliHLTComponent::GetNumberOfInputBlocks() const
879 {
880   // see header file for function documentation
881   if (fpInputBlocks!=NULL) {
882     return fCurrentEventData.fBlockCnt;
883   }
884   return 0;
885 }
886
887 AliHLTEventID_t AliHLTComponent::GetEventId() const
888 {
889   // see header file for function documentation
890   if (fpInputBlocks!=NULL) {
891     return fCurrentEventData.fEventID;
892   }
893   return 0;
894 }
895
896 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
897                                                     const char* classname,
898                                                     int bForce)
899 {
900   // see header file for function documentation
901   ALIHLTCOMPONENT_BASE_STOPWATCH();
902   fSearchDataType=dt;
903   if (classname) fClassName=classname;
904   else fClassName.clear();
905   int idx=FindInputBlock(fSearchDataType, 0, 1);
906   TObject* pObj=NULL;
907   if (idx>=0) {
908     HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
909     if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
910       fCurrentInputBlock=idx;
911     } else {
912     }
913   }
914   return pObj;
915 }
916
917 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID, 
918                                                     const char* dtOrigin,
919                                                     const char* classname,
920                                                     int         bForce)
921 {
922   // see header file for function documentation
923   ALIHLTCOMPONENT_BASE_STOPWATCH();
924   AliHLTComponentDataType dt;
925   SetDataType(dt, dtID, dtOrigin);
926   return GetFirstInputObject(dt, classname, bForce);
927 }
928
929 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
930 {
931   // see header file for function documentation
932   ALIHLTCOMPONENT_BASE_STOPWATCH();
933   int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
934   //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
935   TObject* pObj=NULL;
936   if (idx>=0) {
937     if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
938       fCurrentInputBlock=idx;
939     }
940   }
941   return pObj;
942 }
943
944 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
945 {
946   // see header file for function documentation
947   int iResult=-ENOENT;
948   if (fpInputBlocks!=NULL) {
949     int idx=startIdx<0?0:startIdx;
950     for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
951       if (dt!=fpInputBlocks[idx].fDataType) continue;
952
953       if (bObject!=0) {
954         if (fpInputBlocks[idx].fPtr==NULL) continue;
955         AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
956         if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
957       }
958       iResult=idx;
959     }
960   }
961   return iResult;
962 }
963
964 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
965 {
966   // see header file for function documentation
967   TObject* pObj=NULL;
968   if (fpInputBlocks!=NULL) {
969     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
970       if (fpInputBlocks[idx].fPtr) {
971         AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
972         if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
973           HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
974           AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
975           TClass* objclass=msg.GetClass();
976           pObj=msg.ReadObject(objclass);
977           if (pObj && objclass) {
978             HLTDebug("object %p type %s created", pObj, objclass->GetName());
979           } else {
980           }
981           //} else {
982         } else if (bForce!=0) {
983           HLTError("size mismatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
984         }
985       } else {
986         HLTFatal("block descriptor empty");
987       }
988     } else {
989       HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
990     }
991   } else {
992     HLTError("no input blocks available");
993   }
994   
995   return pObj;
996 }
997
998 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
999 {
1000   // see header file for function documentation
1001   if (fpInputObjects==NULL) {
1002     fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
1003   }
1004   TObject* pObj=NULL;
1005   if (fpInputObjects) {
1006     pObj=fpInputObjects->At(idx);
1007     if (pObj==NULL) {
1008       pObj=CreateInputObject(idx, bForce);
1009       if (pObj) {
1010         fpInputObjects->AddAt(pObj, idx);
1011       }
1012     }
1013   } else {
1014     HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
1015   }
1016   return pObj;
1017 }
1018
1019 int AliHLTComponent::CleanupInputObjects()
1020 {
1021   // see header file for function documentation
1022   if (!fpInputObjects) return 0;
1023   TObjArray* array=fpInputObjects;
1024   fpInputObjects=NULL;
1025   for (int i=0; i<array->GetEntries(); i++) {
1026     TObject* pObj=array->At(i);
1027     // grrr, garbage collection strikes back: When read via AliHLTMessage
1028     // (CreateInputObject), and written to a TFile afterwards, the
1029     // TFile::Close calls ROOOT's garbage collection. No clue why the
1030     // object ended up in the key list and needs to be deleted
1031     //
1032     // Matthias 09.11.2008 follow up
1033     // This approach doesn't actually work in all cases: the object table
1034     // can be switched off globally, the flag needs to be checked here as
1035     // well in order to avoid memory leaks.
1036     // This means we have to find another solution for the problem if it
1037     // pops up again.
1038     if (pObj &&
1039         (!TObject::GetObjectStat() || gObjectTable->PtrIsValid(pObj))) {
1040       delete pObj;
1041     }
1042   }
1043   delete array;
1044   return 0;
1045 }
1046
1047 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
1048 {
1049   // see header file for function documentation
1050   ALIHLTCOMPONENT_BASE_STOPWATCH();
1051   AliHLTComponentDataType dt=kAliHLTVoidDataType;
1052   int idx=fCurrentInputBlock;
1053   if (pObject) {
1054     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1055     } else {
1056       HLTError("unknown object %p", pObject);
1057     }
1058   }
1059   if (idx>=0) {
1060     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
1061       dt=fpInputBlocks[idx].fDataType;
1062     } else {
1063       HLTFatal("severe internal error, index out of range");
1064     }
1065   }
1066   return dt;
1067 }
1068
1069 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
1070 {
1071   // see header file for function documentation
1072   ALIHLTCOMPONENT_BASE_STOPWATCH();
1073   AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
1074   int idx=fCurrentInputBlock;
1075   if (pObject) {
1076     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1077     } else {
1078       HLTError("unknown object %p", pObject);
1079     }
1080   }
1081   if (idx>=0) {
1082     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
1083       iSpec=fpInputBlocks[idx].fSpecification;
1084     } else {
1085       HLTFatal("severe internal error, index out of range");
1086     }
1087   }
1088   return iSpec;
1089 }
1090
1091 int AliHLTComponent::Forward(const TObject* pObject)
1092 {
1093   // see header file for function documentation
1094   int iResult=0;
1095   int idx=fCurrentInputBlock;
1096   if (pObject) {
1097     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1098     } else {
1099       HLTError("unknown object %p", pObject);
1100       iResult=-ENOENT;
1101     }
1102   }
1103   if (idx>=0) {
1104     fOutputBlocks.push_back(fpInputBlocks[idx]);
1105   }
1106   return iResult;
1107 }
1108
1109 int AliHLTComponent::Forward(const AliHLTComponentBlockData* pBlock)
1110 {
1111   // see header file for function documentation
1112   int iResult=0;
1113   int idx=fCurrentInputBlock;
1114   if (pBlock) {
1115     if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
1116     } else {
1117       HLTError("unknown Block %p", pBlock);
1118       iResult=-ENOENT;      
1119     }
1120   }
1121   if (idx>=0) {
1122     // check for fpInputBlocks pointer done in FindInputBlock
1123     fOutputBlocks.push_back(fpInputBlocks[idx]);
1124   }
1125   return iResult;
1126 }
1127
1128 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
1129 {
1130   // see header file for function documentation
1131   ALIHLTCOMPONENT_BASE_STOPWATCH();
1132   fSearchDataType=dt;
1133   fClassName.clear();
1134   int idx=FindInputBlock(fSearchDataType, 0);
1135   const AliHLTComponentBlockData* pBlock=NULL;
1136   if (idx>=0) {
1137     // check for fpInputBlocks pointer done in FindInputBlock
1138     pBlock=&fpInputBlocks[idx];
1139     fCurrentInputBlock=idx;
1140   }
1141   return pBlock;
1142 }
1143
1144 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID, 
1145                                                                     const char* dtOrigin)
1146 {
1147   // see header file for function documentation
1148   ALIHLTCOMPONENT_BASE_STOPWATCH();
1149   AliHLTComponentDataType dt;
1150   SetDataType(dt, dtID, dtOrigin);
1151   return GetFirstInputBlock(dt);
1152 }
1153
1154 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index) const
1155 {
1156   // see header file for function documentation
1157   ALIHLTCOMPONENT_BASE_STOPWATCH();
1158   assert( 0 <= index and index < (int)fCurrentEventData.fBlockCnt );
1159   return &fpInputBlocks[index];
1160 }
1161
1162 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
1163 {
1164   // see header file for function documentation
1165   ALIHLTCOMPONENT_BASE_STOPWATCH();
1166   int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
1167   const AliHLTComponentBlockData* pBlock=NULL;
1168   if (idx>=0) {
1169     // check for fpInputBlocks pointer done in FindInputBlock
1170     pBlock=&fpInputBlocks[idx];
1171     fCurrentInputBlock=idx;
1172   }
1173   return pBlock;
1174 }
1175
1176 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
1177 {
1178   // see header file for function documentation
1179   int iResult=-ENOENT;
1180   if (fpInputBlocks!=NULL) {
1181     if (pBlock) {
1182       if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
1183         iResult=(int)(pBlock-fpInputBlocks);
1184       }
1185     } else {
1186       iResult=-EINVAL;
1187     }
1188   }
1189   return iResult;
1190 }
1191
1192 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
1193 {
1194   // see header file for function documentation
1195   ALIHLTCOMPONENT_BASE_STOPWATCH();
1196   AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
1197   int idx=fCurrentInputBlock;
1198   if (pBlock) {
1199     if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
1200     } else {
1201       HLTError("unknown Block %p", pBlock);
1202     }
1203   }
1204   if (idx>=0) {
1205     // check for fpInputBlocks pointer done in FindInputBlock
1206     iSpec=fpInputBlocks[idx].fSpecification;
1207   }
1208   return iSpec;
1209 }
1210
1211 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec, 
1212                               void* pHeader, int headerSize)
1213 {
1214   // see header file for function documentation
1215   ALIHLTCOMPONENT_BASE_STOPWATCH();
1216   int iResult=0;
1217   fLastObjectSize=0;
1218   if (pObject) {
1219     AliHLTMessage msg(kMESS_OBJECT);
1220     msg.SetCompressionLevel(fCompressionLevel);
1221     msg.WriteObject(pObject);
1222     Int_t iMsgLength=msg.Length();
1223     if (iMsgLength>0) {
1224       // Matthias Sep 2008
1225       // NOTE: AliHLTMessage does implement it's own SetLength method
1226       // which is not architecture independent. The original SetLength
1227       // stores the size always in network byte order.
1228       // I'm trying to remember the rational for that, might be that
1229       // it was just some lack of knowledge. Want to change this, but
1230       // has to be done carefullt to be backward compatible.
1231       msg.SetLength(); // sets the length to the first (reserved) word
1232
1233       // does nothing if the level is 0
1234       msg.Compress();
1235
1236       char *mbuf = msg.Buffer();
1237       if (msg.CompBuffer()) {
1238         msg.SetLength(); // set once more to have to byte order
1239         mbuf = msg.CompBuffer();
1240         iMsgLength = msg.CompLength();
1241       }
1242       assert(mbuf!=NULL);
1243       iResult=InsertOutputBlock(mbuf, iMsgLength, dt, spec, pHeader, headerSize);
1244       if (iResult>=0) {
1245         HLTDebug("object %s (%p) size %d compression %d inserted to output", pObject->ClassName(), pObject, iMsgLength, msg.GetCompressionLevel());
1246       }
1247       fLastObjectSize=iMsgLength;
1248     } else {
1249       HLTError("object serialization failed for object %p", pObject);
1250       iResult=-ENOMSG;
1251     }
1252   } else {
1253     iResult=-EINVAL;
1254   }
1255   return iResult;
1256 }
1257
1258 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1259                               void* pHeader, int headerSize)
1260 {
1261   // see header file for function documentation
1262   ALIHLTCOMPONENT_BASE_STOPWATCH();
1263   AliHLTComponentDataType dt;
1264   SetDataType(dt, dtID, dtOrigin);
1265   return PushBack(pObject, dt, spec, pHeader, headerSize);
1266 }
1267
1268 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1269                               const void* pHeader, int headerSize)
1270 {
1271   // see header file for function documentation
1272   ALIHLTCOMPONENT_BASE_STOPWATCH();
1273   return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
1274 }
1275
1276 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1277                               const void* pHeader, int headerSize)
1278 {
1279   // see header file for function documentation
1280   ALIHLTCOMPONENT_BASE_STOPWATCH();
1281   AliHLTComponentDataType dt;
1282   SetDataType(dt, dtID, dtOrigin);
1283   return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
1284 }
1285
1286 int AliHLTComponent::InsertOutputBlock(const void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1287                                        const void* pHeader, int iHeaderSize)
1288 {
1289   // see header file for function documentation
1290   int iResult=0;
1291   int iBlkSize = iBufferSize + iHeaderSize;
1292
1293   if ((pBuffer!=NULL && iBufferSize>0) || (pHeader!=NULL && iHeaderSize>0)) {
1294     if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
1295       AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1296
1297       // copy header if provided but skip if the header is the target location
1298       // in that case it has already been copied
1299       if (pHeader!=NULL && pHeader!=pTgt) {
1300         memcpy(pTgt, pHeader, iHeaderSize);
1301       }
1302
1303       pTgt += (AliHLTUInt8_t) iHeaderSize;
1304
1305       // copy buffer if provided but skip if buffer is the target location
1306       // in that case it has already been copied
1307       if (pBuffer!=NULL && pBuffer!=pTgt) {
1308         memcpy(pTgt, pBuffer, iBufferSize);
1309         
1310         //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer); 
1311         //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
1312       }
1313       //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
1314     } else {
1315       if (fpOutputBuffer) {
1316         HLTError("too little space in output buffer: %d of %d, required %d", fOutputBufferSize-fOutputBufferFilled, fOutputBufferSize, iBlkSize);
1317       } else {
1318         HLTError("output buffer not available");
1319       }
1320       iResult=-ENOSPC;
1321     }
1322   }
1323   if (iResult>=0) {
1324     AliHLTComponentBlockData bd;
1325     FillBlockData( bd );
1326     bd.fOffset        = fOutputBufferFilled;
1327     bd.fSize          = iBlkSize;
1328     bd.fDataType      = dt;
1329     bd.fSpecification = spec;
1330     fOutputBlocks.push_back( bd );
1331     fOutputBufferFilled+=bd.fSize;
1332   }
1333
1334   return iResult;
1335 }
1336
1337 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
1338 {
1339   // see header file for function documentation
1340   if (!pObject) return -EINVAL;
1341     AliHLTMessage msg(kMESS_OBJECT);
1342     msg.WriteObject(pObject);
1343     return msg.Length();  
1344 }
1345
1346 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
1347                                                     const char* dtOrigin,
1348                                                     AliHLTUInt32_t spec)
1349 {
1350   // see header file for function documentation
1351   ALIHLTCOMPONENT_BASE_STOPWATCH();
1352   AliHLTComponentDataType dt;
1353   SetDataType(dt, dtID, dtOrigin);
1354   return CreateMemoryFile(capacity, dt, spec);
1355 }
1356
1357 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
1358                                                     const AliHLTComponentDataType& dt,
1359                                                     AliHLTUInt32_t spec)
1360 {
1361   // see header file for function documentation
1362   ALIHLTCOMPONENT_BASE_STOPWATCH();
1363   AliHLTMemoryFile* pFile=NULL;
1364   if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
1365     AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1366     pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
1367     if (pFile) {
1368       unsigned int nofBlocks=fOutputBlocks.size();
1369       if (nofBlocks+1>fMemFiles.size()) {
1370         fMemFiles.resize(nofBlocks+1, NULL);
1371       }
1372       if (nofBlocks<fMemFiles.size()) {
1373         fMemFiles[nofBlocks]=pFile;
1374         AliHLTComponentBlockData bd;
1375         FillBlockData( bd );
1376         bd.fOffset        = fOutputBufferFilled;
1377         bd.fSize          = capacity;
1378         bd.fDataType      = dt;
1379         bd.fSpecification = spec;
1380         fOutputBufferFilled+=bd.fSize;
1381         fOutputBlocks.push_back( bd );
1382       } else {
1383         HLTError("can not allocate/grow object array");
1384         pFile->CloseMemoryFile(0);
1385         delete pFile;
1386         pFile=NULL;
1387       }
1388     }
1389   } else {
1390     HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
1391   }
1392   return pFile;
1393 }
1394
1395 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
1396                                                     const char* dtOrigin,
1397                                                     AliHLTUInt32_t spec,
1398                                                     float capacity)
1399 {
1400   // see header file for function documentation
1401   ALIHLTCOMPONENT_BASE_STOPWATCH();
1402   AliHLTComponentDataType dt;
1403   SetDataType(dt, dtID, dtOrigin);
1404   int size=fOutputBufferSize-fOutputBufferFilled;
1405   if (capacity<0 || capacity>1.0) {
1406     HLTError("invalid parameter: capacity %f", capacity);
1407     return NULL;
1408   }
1409   size=(int)(size*capacity);
1410   return CreateMemoryFile(size, dt, spec);
1411 }
1412
1413 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
1414                                                     AliHLTUInt32_t spec,
1415                                                     float capacity)
1416 {
1417   // see header file for function documentation
1418   ALIHLTCOMPONENT_BASE_STOPWATCH();
1419   int size=fOutputBufferSize-fOutputBufferFilled;
1420   if (capacity<0 || capacity>1.0) {
1421     HLTError("invalid parameter: capacity %f", capacity);
1422     return NULL;
1423   }
1424   size=(int)(size*capacity);
1425   return CreateMemoryFile(size, dt, spec);
1426 }
1427
1428 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
1429                            const char* key, int option)
1430 {
1431   // see header file for function documentation
1432   int iResult=0;
1433   if (pFile && pObject) {
1434     pFile->cd();
1435     iResult=pObject->Write(key, option);
1436     if (iResult>0) {
1437       // success
1438     } else {
1439       iResult=-pFile->GetErrno();
1440       if (iResult==-ENOSPC) {
1441         HLTError("error writing memory file, buffer too small");
1442       }
1443     }
1444   } else {
1445     iResult=-EINVAL;
1446   }
1447   return iResult;
1448 }
1449
1450 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
1451 {
1452   // see header file for function documentation
1453   int iResult=0;
1454   if (pFile) {
1455     AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1456     int i=0;
1457     while (element!=fMemFiles.end() && iResult>=0) {
1458       if (*element && *element==pFile) {
1459         iResult=pFile->CloseMemoryFile();
1460         
1461         // sync memory files and descriptors
1462         if (iResult>=0) {
1463           fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
1464         }
1465         delete *element;
1466         *element=NULL;
1467         return iResult;
1468       }
1469       element++; i++;
1470     }
1471     HLTError("can not find memory file %p", pFile);
1472     iResult=-ENOENT;
1473   } else {
1474     iResult=-EINVAL;
1475   }
1476   return iResult;
1477 }
1478
1479 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
1480 {
1481   // see header file for function documentation
1482   int iResult=0;
1483
1484   AliHLTComponentEventDoneData* newEDD = NULL;
1485   
1486   unsigned long newSize=edd.fDataSize;
1487   if (fEventDoneData)
1488     newSize += fEventDoneData->fDataSize;
1489
1490   if (newSize>fEventDoneDataSize) {
1491     newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[ sizeof(AliHLTComponentEventDoneData)+newSize ] );
1492     if (!newEDD)
1493       return -ENOMEM;
1494     newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
1495     newEDD->fDataSize = newSize;
1496     newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
1497     unsigned long long offset = 0;
1498     if (fEventDoneData) {
1499       memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
1500       offset += fEventDoneData->fDataSize;
1501     }
1502     memcpy( reinterpret_cast<AliHLTUInt8_t*>(newEDD->fData)+offset, edd.fData, edd.fDataSize );
1503     if (fEventDoneData)
1504       delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
1505     fEventDoneData = newEDD;
1506     fEventDoneDataSize = newSize;
1507   }
1508   else {
1509     memcpy( reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize, edd.fData, edd.fDataSize );
1510     fEventDoneData->fDataSize += edd.fDataSize;
1511   }
1512   return iResult;
1513 }
1514
1515 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1516                                    const AliHLTComponentBlockData* blocks, 
1517                                    AliHLTComponentTriggerData& trigData,
1518                                    AliHLTUInt8_t* outputPtr, 
1519                                    AliHLTUInt32_t& size,
1520                                    AliHLTUInt32_t& outputBlockCnt, 
1521                                    AliHLTComponentBlockData*& outputBlocks,
1522                                    AliHLTComponentEventDoneData*& edd )
1523 {
1524   // see header file for function documentation
1525   HLTLogKeyword(fChainId.c_str());
1526   ALIHLTCOMPONENT_BASE_STOPWATCH();
1527   int iResult=0;
1528   fCurrentEvent=evtData.fEventID;
1529   fCurrentEventData=evtData;
1530   fpInputBlocks=blocks;
1531   fCurrentInputBlock=-1;
1532   fSearchDataType=kAliHLTAnyDataType;
1533   fpOutputBuffer=outputPtr;
1534   fOutputBufferSize=size;
1535   fOutputBufferFilled=0;
1536   fOutputBlocks.clear();
1537   outputBlockCnt=0;
1538   outputBlocks=NULL;
1539
1540   AliHLTComponentBlockDataList forwardedBlocks;
1541
1542   // optional component statistics
1543   AliHLTComponentStatisticsList compStats;
1544   bool bAddComponentTableEntry=false;
1545   vector<AliHLTUInt32_t> parentComponentTables;
1546 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
1547   AliHLTComponentStatistics outputStat;
1548   memset(&outputStat, 0, sizeof(AliHLTComponentStatistics));
1549   outputStat.fStructSize=sizeof(AliHLTComponentStatistics);
1550   outputStat.fId=fChainIdCrc;
1551   if (fpBenchmark) {
1552     fpBenchmark->Stop();
1553     outputStat.fComponentCycleTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
1554     fpBenchmark->Reset();
1555     fpBenchmark->Start();
1556   }
1557   compStats.push_back(outputStat);
1558 #endif
1559
1560   // data processing is skipped
1561   // -  if there are only steering events in the block list.
1562   //    For the sake of data source components data processing
1563   //    is not skipped if there is no block list at all or if it
1564   //    just contains the eventType block
1565   // - always skipped if the event is of type
1566   //   - gkAliEventTypeConfiguration
1567   //   - gkAliEventTypeReadPreprocessor
1568   const unsigned int skipModeDefault=0x1;
1569   const unsigned int skipModeForce=0x2;
1570   unsigned int bSkipDataProcessing=skipModeDefault;
1571
1572   // find special events
1573   if (fpInputBlocks && evtData.fBlockCnt>0) {
1574     // first look for all special events and execute in the appropriate
1575     // sequence afterwords
1576     int indexComConfEvent=-1;
1577     int indexUpdtDCSEvent=-1;
1578     int indexSOREvent=-1;
1579     int indexEOREvent=-1;
1580     int indexECSParamBlock=-1;
1581     for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1582       if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1583         indexSOREvent=i;
1584         // the AliHLTCalibrationProcessor relies on the SOR and EOR events
1585         bSkipDataProcessing&=~skipModeDefault;
1586       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeRunType) {
1587         // run type string
1588         // handling is not clear yet
1589         if (fpInputBlocks[i].fPtr) {
1590           HLTDebug("got run type \"%s\"\n", fpInputBlocks[i].fPtr);
1591         }
1592       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1593         indexEOREvent=i;
1594         // the calibration processor relies on the SOR and EOR events
1595         bSkipDataProcessing&=~skipModeDefault;
1596       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1597         // DDL list
1598         // this event is most likely deprecated
1599       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComConf) {
1600         indexComConfEvent=i;
1601       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeUpdtDCS) {
1602         indexUpdtDCSEvent=i;
1603       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEvent) {
1604         fEventType=fpInputBlocks[i].fSpecification;
1605
1606         // skip always in case of gkAliEventTypeConfiguration
1607         if (fpInputBlocks[i].fSpecification==gkAliEventTypeConfiguration) bSkipDataProcessing|=skipModeForce;
1608
1609         // skip always in case of gkAliEventTypeReadPreprocessor
1610         if (fpInputBlocks[i].fSpecification==gkAliEventTypeReadPreprocessor) bSkipDataProcessing|=skipModeForce;
1611
1612         // never skip if the event type block is the only block
1613         if (evtData.fBlockCnt==1) bSkipDataProcessing&=~skipModeDefault;
1614
1615       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentStatistics) {
1616         if (compStats.size()>0) {
1617           AliHLTUInt8_t* pData=reinterpret_cast<AliHLTUInt8_t*>(fpInputBlocks[i].fPtr);
1618           for (AliHLTUInt32_t offset=0;
1619                offset+sizeof(AliHLTComponentStatistics)<=fpInputBlocks[i].fSize;
1620                offset+=sizeof(AliHLTComponentStatistics)) {
1621             AliHLTComponentStatistics* pStat=reinterpret_cast<AliHLTComponentStatistics*>(pData+offset);
1622             if (pStat && compStats[0].fLevel<=pStat->fLevel) {
1623               compStats[0].fLevel=pStat->fLevel+1;
1624             }
1625             compStats.push_back(*pStat);
1626           }
1627         }
1628       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentTable) {
1629         forwardedBlocks.push_back(fpInputBlocks[i]);
1630         parentComponentTables.push_back(fpInputBlocks[i].fSpecification);
1631       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeECSParam) {
1632         indexECSParamBlock=i;
1633       } else {
1634         // the processing function is called if there is at least one
1635         // non-steering data block. Steering blocks are not filtered out
1636         // for sake of performance 
1637         bSkipDataProcessing&=~skipModeDefault;
1638         if (compStats.size()>0) {
1639           compStats[0].fInputBlockCount++;
1640           compStats[0].fTotalInputSize+=fpInputBlocks[i].fSize;
1641         }
1642       }
1643     }
1644
1645     if (indexSOREvent>=0) {
1646       // start of run
1647       bAddComponentTableEntry=true;
1648       if (fpRunDesc==NULL) {
1649         fpRunDesc=new AliHLTRunDesc;
1650         if (fpRunDesc) *fpRunDesc=kAliHLTVoidRunDesc;
1651       }
1652       if (fpRunDesc) {
1653         AliHLTRunDesc rundesc;
1654         if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexSOREvent, "AliHLTRunDesc", "SOR"))>0) {
1655           if (fpRunDesc->fRunNo==kAliHLTVoidRunNo) {
1656             *fpRunDesc=rundesc;
1657             HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1658             SetCDBRunNo(fpRunDesc->fRunNo);
1659           } else if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1660             HLTWarning("already set run properties run no %d, ignoring SOR with run no %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1661           }
1662         }
1663       } else {
1664         iResult=-ENOMEM;
1665       }
1666
1667       if (indexECSParamBlock>=0) {
1668         if (fpInputBlocks[indexECSParamBlock].fSize>0) {
1669           const char* param=reinterpret_cast<const char*>(fpInputBlocks[indexECSParamBlock].fPtr);
1670           TString paramString;
1671           if (param[fpInputBlocks[indexECSParamBlock].fSize-1]!=0) {
1672             HLTWarning("ECS parameter string not terminated");
1673             paramString.Insert(0, param, fpInputBlocks[indexECSParamBlock].fSize);
1674             paramString+="";
1675           } else {
1676             paramString=param;
1677           }
1678           ScanECSParam(paramString.Data());
1679         } else {
1680           HLTWarning("empty ECS parameter received");
1681         }
1682       } else {
1683         // TODO: later on we might throw a warning here since the CTP trigger classes
1684         // should be mandatory
1685       }
1686     }
1687     if (indexEOREvent>=0) {
1688       bAddComponentTableEntry=true;
1689       if (fpRunDesc!=NULL) {
1690         if (fpRunDesc) {
1691           AliHLTRunDesc rundesc;
1692           if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexEOREvent, "AliHLTRunDesc", "SOR"))>0) {
1693             if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1694               HLTWarning("run no mismatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1695             } else {
1696               HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1697             }
1698           }
1699           AliHLTRunDesc* pRunDesc=fpRunDesc;
1700           fpRunDesc=NULL;
1701           delete pRunDesc;
1702         }
1703       } else {
1704         HLTWarning("did not receive SOR, ignoring EOR");
1705       }
1706     }
1707     if (indexComConfEvent>=0 || fEventType==gkAliEventTypeConfiguration) {
1708       TString cdbEntry;
1709       if (indexComConfEvent>=0 && fpInputBlocks[indexComConfEvent].fPtr!=NULL && fpInputBlocks[indexComConfEvent].fSize>0) {
1710         cdbEntry.Append(reinterpret_cast<const char*>(fpInputBlocks[indexComConfEvent].fPtr), fpInputBlocks[indexComConfEvent].fSize);
1711       }
1712       HLTDebug("received component configuration command: entry %s", cdbEntry.IsNull()?"none":cdbEntry.Data());
1713       int tmpResult=Reconfigure(cdbEntry[0]==0?NULL:cdbEntry.Data(), fChainId.c_str());
1714       if (tmpResult<0) {
1715         HLTWarning("reconfiguration of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1716       }
1717     }
1718     if (indexUpdtDCSEvent>=0 || fEventType==gkAliEventTypeReadPreprocessor) {
1719       TString modules;
1720       if (fpInputBlocks[indexUpdtDCSEvent].fPtr!=NULL && fpInputBlocks[indexUpdtDCSEvent].fSize>0) {
1721         modules.Append(reinterpret_cast<const char*>(fpInputBlocks[indexUpdtDCSEvent].fPtr), fpInputBlocks[indexUpdtDCSEvent].fSize);
1722       }
1723       HLTDebug("received preprocessor update command: detectors %s", modules.IsNull()?"ALL":modules.Data());
1724       int tmpResult=ReadPreprocessorValues(modules[0]==0?"ALL":modules.Data());
1725       if (tmpResult<0) {
1726         HLTWarning("preprocessor update of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1727       }
1728     }
1729   } else {
1730     // processing function needs to be called if there are no input data
1731     // blocks in order to make data source components working.
1732     bSkipDataProcessing&=~skipModeDefault;
1733   }
1734
1735   // data processing is not skipped if the component explicitly asks
1736   // for the private blocks
1737   if (fRequireSteeringBlocks) bSkipDataProcessing=0;
1738
1739   AliHLTComponentBlockDataList blockData;
1740   if (iResult>=0 && !bSkipDataProcessing)
1741   { // dont delete, sets the scope for the stopwatch guard
1742     // do not use ALIHLTCOMPONENT_DA_STOPWATCH(); macro
1743     // in order to avoid 'shadowed variable' warning
1744     AliHLTStopwatchGuard swguard2(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)kSWDA)):NULL);
1745     iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1746   } // end of the scope of the stopwatch guard
1747   if (iResult>=0 && !bSkipDataProcessing) {
1748     if (fOutputBlocks.size()>0) {
1749       // High Level interface
1750
1751       //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());      
1752       // sync memory files and descriptors
1753       AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1754       int i=0;
1755       while (element!=fMemFiles.end() && iResult>=0) {
1756         if (*element) {
1757           if ((*element)->IsClosed()==0) {
1758             HLTWarning("memory file has not been closed, force flush");
1759             iResult=CloseMemoryFile(*element);
1760           }
1761         }
1762         element++; i++;
1763       }
1764
1765       if (iResult>=0) {
1766         // create the descriptor list
1767         if (blockData.size()>0) {
1768           HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1769           iResult=-EFAULT;
1770         } else {
1771           if (compStats.size()>0 && IsDataEvent()) {
1772             int offset=AddComponentStatistics(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, compStats);
1773             if (offset>0) fOutputBufferFilled+=offset;
1774           }
1775           if (bAddComponentTableEntry) {
1776             int offset=AddComponentTableEntry(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, parentComponentTables);
1777             if (offset>0) size+=offset;
1778           }
1779           if (forwardedBlocks.size()>0) {
1780             fOutputBlocks.insert(fOutputBlocks.end(), forwardedBlocks.begin(), forwardedBlocks.end());
1781           }
1782           iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1783           size=fOutputBufferFilled;
1784         }
1785       }
1786     } else {
1787       // Low Level interface
1788       if (compStats.size()>0) {
1789         int offset=AddComponentStatistics(blockData, fpOutputBuffer, fOutputBufferSize, size, compStats);
1790         if (offset>0) size+=offset;
1791       }
1792       if (bAddComponentTableEntry) {
1793         int offset=AddComponentTableEntry(blockData, fpOutputBuffer, fOutputBufferSize, size, parentComponentTables);
1794         if (offset>0) size+=offset;
1795       }
1796       if (forwardedBlocks.size()>0) {
1797         blockData.insert(blockData.end(), forwardedBlocks.begin(), forwardedBlocks.end());
1798       }
1799       iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1800     }
1801     if (iResult<0) {
1802       HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1803     }
1804   }
1805   if (iResult<0 || bSkipDataProcessing) {
1806     outputBlockCnt=0;
1807     outputBlocks=NULL;
1808   }
1809   CleanupInputObjects();
1810   if (iResult>=0 && IsDataEvent()) {
1811     IncrementEventCounter();
1812   }
1813   if (outputBlockCnt==0) {
1814     // no output blocks, set size to 0
1815     size=0;
1816   }
1817
1818   // reset the internal EventData struct
1819   FillEventData(fCurrentEventData);
1820   return iResult;
1821 }
1822
1823 int  AliHLTComponent::AddComponentStatistics(AliHLTComponentBlockDataList& blocks, 
1824                                              AliHLTUInt8_t* buffer,
1825                                              AliHLTUInt32_t bufferSize,
1826                                              AliHLTUInt32_t offset,
1827                                              AliHLTComponentStatisticsList& stats) const
1828 {
1829   // see header file for function documentation
1830   int iResult=0;
1831 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
1832   if (stats.size()==0) return -ENOENT;
1833   // check if there is space for at least one entry
1834   if (offset+sizeof(AliHLTComponentStatistics)>bufferSize) return 0;
1835   stats[0].fTotalOutputSize=offset;
1836   stats[0].fOutputBlockCount=blocks.size();
1837   if (fpBenchmark) {
1838     fpBenchmark->Stop();
1839     stats[0].fTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
1840     stats[0].fCTime=(AliHLTUInt32_t)(fpBenchmark->CpuTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
1841     fpBenchmark->Continue();
1842   }
1843   if (offset+stats.size()*sizeof(AliHLTComponentStatistics)>bufferSize) {
1844     AliHLTUInt32_t removedLevel=0;
1845     do {
1846       // remove all entries of the level of the last entry
1847       removedLevel=stats.back().fLevel;
1848       AliHLTComponentStatisticsList::iterator element=stats.begin();
1849       element++;
1850       while (element!=stats.end()) {
1851         if (element->fLevel<=removedLevel) {
1852           element=stats.erase(element);
1853         } else {
1854           element++;
1855         }
1856       }
1857     } while (stats.size()>1 && 
1858              (offset+stats.size()*sizeof(AliHLTComponentStatistics)>bufferSize));
1859   }
1860   assert(stats.size()>0);
1861   if (stats.size()==0) return 0;
1862
1863   if (offset+stats.size()*sizeof(AliHLTComponentStatistics)<=bufferSize) {
1864     AliHLTComponentBlockData bd;
1865     FillBlockData( bd );
1866     bd.fOffset        = offset;
1867     bd.fSize          = stats.size()*sizeof(AliHLTComponentStatistics);
1868     bd.fDataType      = kAliHLTDataTypeComponentStatistics;
1869     bd.fSpecification = kAliHLTVoidDataSpec;
1870     unsigned int master=0;
1871     for (unsigned int i=1; i<blocks.size(); i++) {
1872       if (blocks[i].fSize>blocks[master].fSize && 
1873           !MatchExactly(blocks[i].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate))
1874         master=i;
1875     }
1876     if (blocks.size()>0 && !MatchExactly(blocks[master].fDataType, kAliHLTVoidDataType|kAliHLTDataOriginPrivate)) {
1877       // take the data origin of the biggest block as specification
1878       // this is similar to the treatment in the HOMER interface. For traditional
1879       // reasons, the bytes are swapped there on a little endian architecture, so
1880       // we do it as well.
1881       memcpy(&bd.fSpecification, &blocks[master].fDataType.fOrigin, sizeof(bd.fSpecification));
1882 #ifdef R__BYTESWAP // set on little endian architectures
1883       bd.fSpecification=((bd.fSpecification & 0xFFULL) << 24) | 
1884         ((bd.fSpecification & 0xFF00ULL) << 8) | 
1885         ((bd.fSpecification & 0xFF0000ULL) >> 8) | 
1886         ((bd.fSpecification & 0xFF000000ULL) >> 24);
1887 #endif
1888     }
1889     memcpy(buffer+offset, &(stats[0]), bd.fSize);
1890     blocks.push_back(bd);
1891     iResult=bd.fSize;
1892   }
1893 #else
1894   if (blocks.size() && buffer && bufferSize && offset && stats.size()) {
1895     // get rid of warning
1896   }
1897 #endif
1898   return iResult;
1899 }
1900
1901 int  AliHLTComponent::AddComponentTableEntry(AliHLTComponentBlockDataList& blocks, 
1902                                              AliHLTUInt8_t* buffer,
1903                                              AliHLTUInt32_t bufferSize,
1904                                              AliHLTUInt32_t offset,
1905                                              const vector<AliHLTUInt32_t>& parents) const
1906 {
1907   // see header file for function documentation
1908   int iResult=0;
1909 #if defined(__DEBUG) || defined(HLT_COMPONENT_STATISTICS)
1910   // the payload consists of the AliHLTComponentTableEntry struct,
1911   // followed by a an array of 32bit crc chain ids and the component
1912   // description string
1913   unsigned int payloadSize=sizeof(AliHLTComponentTableEntry);
1914   payloadSize+=parents.size()*sizeof(AliHLTUInt32_t);
1915
1916   // the component description has the following format:
1917   // chain-id{component-id:arguments}
1918   const char* componentId=const_cast<AliHLTComponent*>(this)->GetComponentID();
1919   unsigned int descriptionSize=fChainId.size()+1;
1920   descriptionSize+=2; // the '{}' around the component id
1921   descriptionSize+=strlen(componentId);
1922   descriptionSize+=1; // the ':' between component id and arguments
1923   descriptionSize+=fComponentArgs.size();
1924
1925   payloadSize+=descriptionSize;
1926   if (buffer && (offset+payloadSize<=bufferSize)) {
1927     AliHLTUInt8_t* pTgt=buffer+offset;
1928     memset(pTgt, 0, payloadSize);
1929
1930     // write entry
1931     AliHLTComponentTableEntry* pEntry=reinterpret_cast<AliHLTComponentTableEntry*>(pTgt);
1932     pEntry->fStructSize=sizeof(AliHLTComponentTableEntry);
1933     pEntry->fNofParents=parents.size();
1934     pEntry->fSizeDescription=descriptionSize;
1935     pTgt=pEntry->fBuffer;
1936
1937     // write array of parents
1938     if (parents.size()>0) {
1939       unsigned int copy=parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type);
1940       memcpy(pTgt, &parents[0], parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type));
1941       pTgt+=copy;
1942     }
1943
1944     // write component description
1945     memcpy(pTgt, fChainId.c_str(), fChainId.size());
1946     pTgt+=fChainId.size();
1947     *pTgt++='{';
1948     memcpy(pTgt, componentId, strlen(componentId));
1949     pTgt+=strlen(componentId);
1950     *pTgt++=':';
1951     memcpy(pTgt, fComponentArgs.c_str(), fComponentArgs.size());
1952     pTgt+=fComponentArgs.size();
1953     *pTgt++='}';
1954     *pTgt++=0;
1955
1956     AliHLTComponentBlockData bd;
1957     FillBlockData( bd );
1958     bd.fOffset        = offset;
1959     bd.fSize          = payloadSize;
1960     bd.fDataType      = kAliHLTDataTypeComponentTable;
1961     bd.fSpecification = fChainIdCrc;
1962     blocks.push_back(bd);
1963     iResult=bd.fSize;
1964   }
1965 #else
1966   if (blocks.size() && buffer && bufferSize && offset && parents.size()) {
1967     // get rid of warning
1968   }
1969  #endif
1970   return iResult;
1971 }
1972
1973 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1974   :
1975   fpStopwatch(NULL),
1976   fpPrec(NULL)
1977 {
1978   // standard constructor (not for use)
1979 }
1980
1981 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1982   :
1983   fpStopwatch(pStopwatch),
1984   fpPrec(NULL)
1985 {
1986   // constructor
1987
1988   // check for already existing guard
1989   if (fgpCurrent) fpPrec=fgpCurrent;
1990   fgpCurrent=this;
1991
1992   // stop the preceeding guard if it controls a different stopwatch
1993   int bStart=1;
1994   if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1995
1996   // start the stopwatch if the current guard controls a different one
1997   if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1998 }
1999
2000 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
2001   :
2002   fpStopwatch(NULL),
2003   fpPrec(NULL)
2004 {
2005   //
2006   // copy constructor not for use
2007   //
2008 }
2009
2010 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
2011 {
2012   //
2013   // assignment operator not for use
2014   //
2015   fpStopwatch=NULL;
2016   fpPrec=NULL;
2017   return *this;
2018 }
2019
2020 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
2021
2022 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
2023 {
2024   // destructor
2025
2026   // resume the preceeding guard if it controls a different stopwatch
2027   int bStop=1;
2028   if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
2029
2030   // stop the stopwatch if the current guard controls a different one
2031   if (fpStopwatch && bStop==1) fpStopwatch->Stop();
2032
2033   // resume to the preceeding guard
2034   fgpCurrent=fpPrec;
2035 }
2036
2037 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
2038 {
2039   // see header file for function documentation
2040   if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
2041   return fpStopwatch!=pSucc?1:0;
2042 }
2043
2044 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
2045 {
2046   // see header file for function documentation
2047   if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
2048   return fpStopwatch!=pSucc?1:0;
2049 }
2050
2051 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type) 
2052 {
2053   // see header file for function documentation
2054   int iResult=0;
2055   if (pSW!=NULL && type<kSWTypeCount) {
2056     if (fpStopwatches) {
2057       TObject* pObj=fpStopwatches->At((int)type);
2058       if (pSW==NULL        // explicit reset
2059           || pObj==NULL) { // explicit set
2060         fpStopwatches->AddAt(pSW, (int)type);
2061       } else if (pObj!=pSW) {
2062         HLTWarning("stopwatch %d already set, reset first", (int)type);
2063         iResult=-EBUSY;
2064       }
2065     }
2066   } else {
2067     iResult=-EINVAL;
2068   }
2069   return iResult;
2070 }
2071
2072 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
2073 {
2074   // see header file for function documentation
2075   if (pStopwatches==NULL) return -EINVAL;
2076
2077   int iResult=0;
2078   for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
2079     SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
2080   return iResult;
2081 }
2082
2083 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
2084 {
2085   // see header file for function documentation
2086   if (fpRunDesc==NULL) return kAliHLTVoidRunNo;
2087   return fpRunDesc->fRunNo;
2088 }
2089
2090 AliHLTUInt32_t AliHLTComponent::GetRunType() const
2091 {
2092   // see header file for function documentation
2093   if (fpRunDesc==NULL) return kAliHLTVoidRunType;
2094   return fpRunDesc->fRunType;
2095 }
2096
2097 bool AliHLTComponent::IsDataEvent(AliHLTUInt32_t* pTgt)
2098 {
2099   // see header file for function documentation
2100   if (pTgt) *pTgt=fEventType;
2101   return (fEventType==gkAliEventTypeData ||
2102           fEventType==gkAliEventTypeDataReplay ||
2103           fEventType==gkAliEventTypeCalibration);
2104 }
2105
2106 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
2107                                 const char* structname, const char* eventname)
2108 {
2109   // see header file for function documentation
2110   int iResult=0;
2111   if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
2112     if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
2113       AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
2114       if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
2115         AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
2116         if (fpInputBlocks[iBlockNo].fSize!=copy) {
2117           HLTWarning("%s event: mismatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
2118           if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
2119         }
2120         if (copy!=iStructSize) {
2121           HLTWarning("%s event: mismatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
2122           if (copy>iStructSize) {
2123             copy=iStructSize;
2124           } else {
2125             memset(pTgt, 0, iStructSize);
2126           }
2127         }
2128         memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
2129         *pTgt=iStructSize;
2130         iResult=copy;
2131       } else {
2132         HLTWarning("%s event: missing data block", eventname);
2133       }
2134     } else {
2135       iResult=-ENODATA;
2136     }
2137   } else {
2138     HLTError("invalid struct");
2139     iResult=-EINVAL;
2140   }
2141   return iResult;
2142 }
2143
2144 void AliHLTComponent::SetDDLBit(AliHLTEventDDL &list, Int_t ddlId, Bool_t state ) const
2145 {
2146   // see header file for function documentation
2147   
2148   // -- Detector offset
2149   Int_t ddlIdBase =  TMath::FloorNint( (Double_t) ddlId / 256.0 );
2150   
2151   // -- Word Base = 1. word of detector ( TPC has 8 words, TOF 3 ) 
2152   Int_t wordBase = 0;
2153
2154   if ( ddlIdBase <= 3 )
2155     wordBase = ddlIdBase;
2156   else if ( ddlIdBase > 3 && ddlIdBase < 5 )
2157     wordBase = ddlIdBase + 7;
2158   else 
2159     wordBase = ddlIdBase + 9;
2160
2161   // -- Bit index in Word
2162   Int_t bitIdx = ddlId % 32;
2163
2164   // -- Index of word
2165   Int_t wordIdx = wordBase;
2166
2167   // -- if TPC (3) or TOD (5) add word idx
2168   if ( ( ddlIdBase == 3 ) || ( ddlIdBase == 5 ) ) {
2169     wordIdx += TMath::FloorNint( (Double_t) ( ddlId - ( ddlIdBase * 256 ) ) / 32.0 );
2170   }
2171
2172   // -- Set -- 'OR' word with bit mask;
2173   if ( state )
2174     list.fList[wordIdx] |= ( 0x00000001 << bitIdx );
2175   // -- Unset -- 'AND' word with bit mask;
2176   else
2177     list.fList[wordIdx] &= ( 0xFFFFFFFF ^ ( 0x00000001 << bitIdx ) );
2178 }
2179
2180 Int_t AliHLTComponent::GetFirstUsedDDLWord(AliHLTEventDDL &list) const
2181 {
2182   // see header file for function documentation
2183
2184   Int_t iResult = -1;
2185
2186   for ( Int_t wordNdx = 0 ; wordNdx < gkAliHLTDDLListSize ; wordNdx++ ) {
2187
2188     if ( list.fList[wordNdx] != 0 && iResult == -1 ) {
2189       // check for special cases TPC and TOF
2190       if ( wordNdx > 3 && wordNdx <= 10 ) {
2191         wordNdx = 10;
2192         iResult = 3;
2193       }
2194       else if ( wordNdx > 12 && wordNdx <= 14 ) {
2195         wordNdx = 14;
2196         iResult = 12;
2197       }
2198       else
2199         iResult = wordNdx;
2200     }
2201     else if ( list.fList[wordNdx] != 0 && iResult >= 0 ) {
2202       HLTError( "DDLIDs for minimum of TWO detectors ( %d, %d ) set, this function works only for ONE detector.", iResult, wordNdx );
2203       iResult = -1;
2204       break;
2205     }
2206   }
2207
2208   return iResult;
2209 }
2210
2211 AliHLTUInt32_t AliHLTComponent::CalculateChecksum(const AliHLTUInt8_t* buffer, int size)
2212 {
2213   // see header file for function documentation
2214   AliHLTUInt32_t  remainder = 0; 
2215   const AliHLTUInt8_t crcwidth=(8*sizeof(AliHLTUInt32_t));
2216   const AliHLTUInt32_t topbit=1 << (crcwidth-1);
2217   const AliHLTUInt32_t polynomial=0xD8;  /* 11011 followed by 0's */
2218
2219   // code from
2220   // http://www.netrino.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
2221
2222   /*
2223    * Perform modulo-2 division, a byte at a time.
2224    */
2225   for (int byte = 0; byte < size; ++byte)
2226     {
2227       /*
2228        * Bring the next byte into the remainder.
2229        */
2230       remainder ^= (buffer[byte] << (crcwidth - 8));
2231
2232       /*
2233        * Perform modulo-2 division, a bit at a time.
2234        */
2235       for (uint8_t bit = 8; bit > 0; --bit)
2236         {
2237           /*
2238            * Try to divide the current data bit.
2239            */
2240           if (remainder & topbit)
2241             {
2242               remainder = (remainder << 1) ^ polynomial;
2243             }
2244           else
2245             {
2246               remainder = (remainder << 1);
2247             }
2248         }
2249     }
2250
2251   /*
2252    * The final remainder is the CRC result.
2253    */
2254   return (remainder);
2255 }
2256
2257 int AliHLTComponent::ExtractComponentTableEntry(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size,
2258                                                 string& retChainId, string& retCompId, string& retCompArgs,
2259                                                 vector<AliHLTUInt32_t>& parents)
2260 {
2261   // see header file for function documentation
2262   retChainId.clear();
2263   retCompId.clear();
2264   retCompArgs.clear();
2265   parents.clear();
2266   if (!pBuffer || size==0) return 0;
2267
2268   const AliHLTComponentTableEntry* pEntry=reinterpret_cast<const AliHLTComponentTableEntry*>(pBuffer);
2269   if (size<8/* the initial size of the structure*/ ||
2270       pEntry==NULL || pEntry->fStructSize<8) return -ENOMSG;
2271   const AliHLTUInt32_t* pParents=reinterpret_cast<const AliHLTUInt32_t*>(pEntry->fBuffer);
2272   const AliHLTUInt8_t* pEnd=pBuffer+size;
2273
2274   if (pParents+pEntry->fNofParents>=reinterpret_cast<const AliHLTUInt32_t*>(pEnd)) return -ENODEV;
2275   for (unsigned int i=0; i<pEntry->fNofParents; i++, pParents++) {
2276     parents.push_back(*pParents);
2277   }
2278
2279   const char* pDescription=reinterpret_cast<const char*>(pParents);
2280   if (pDescription+pEntry->fSizeDescription>=reinterpret_cast<const char*>(pEnd) ||
2281       *(pDescription+pEntry->fSizeDescription)!=0) {
2282     return -EBADF;
2283   }
2284
2285   TString descriptor=reinterpret_cast<const char*>(pDescription);
2286   TString chainId;
2287   TString compId;
2288   TString compArgs;
2289   TObjArray* pTokens=descriptor.Tokenize("{");
2290   if (pTokens) {
2291     int n=0;
2292     if (pTokens->GetEntries()>n) {
2293       retChainId=((TObjString*)pTokens->At(n++))->GetString();
2294     }
2295     if (pTokens->GetEntries()>n) {
2296       compId=((TObjString*)pTokens->At(n++))->GetString();
2297     }
2298     delete pTokens;
2299   }
2300   if (!compId.IsNull() && (pTokens=compId.Tokenize(":"))!=NULL) {
2301     int n=0;
2302     if (pTokens->GetEntries()>n) {
2303       compId=((TObjString*)pTokens->At(n++))->GetString();
2304     }
2305     if (pTokens->GetEntries()>n) {
2306       compArgs=((TObjString*)pTokens->At(n++))->GetString();
2307     }
2308     delete pTokens;
2309   }
2310   compId.ReplaceAll("}", "");
2311   compArgs.ReplaceAll("}", "");
2312
2313   retCompId=compId;
2314   retCompArgs=compArgs;
2315
2316   if (retChainId.size()==0) return -ENODATA;
2317
2318   return 1;
2319 }
2320
2321 int AliHLTComponent::LoggingVarargs(AliHLTComponentLogSeverity severity, 
2322                                     const char* originClass, const char* originFunc,
2323                                     const char* file, int line, ... ) const
2324 {
2325   // see header file for function documentation
2326   int iResult=0;
2327
2328   va_list args;
2329   va_start(args, line);
2330
2331   // logging function needs to be const in order to be called from const member functions
2332   // without problems. But at this point we face the problem with virtual members which
2333   // are not necessarily const.
2334   AliHLTComponent* nonconst=const_cast<AliHLTComponent*>(this);
2335   AliHLTLogging::SetLogString("%s (%s, %p): ", 
2336                               fChainId[0]!=0?fChainId.c_str():nonconst->GetComponentID(),
2337                               nonconst->GetComponentID(), this);
2338   iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args, true /*append*/));
2339   va_end(args);
2340
2341   return iResult;
2342 }
2343
2344 int AliHLTComponent::ScanECSParam(const char* ecsParam)
2345 {
2346   // see header file for function documentation
2347
2348   // format of the parameter string from ECS
2349   // <command>;<parameterkey>=<parametervalue>;<parameterkey>=<parametervalue>;...
2350   // search for a subset of the parameterkeys
2351   int iResult=0;
2352   TString string=ecsParam;
2353   TObjArray* parameter=string.Tokenize(";");
2354   if (parameter) {
2355     for (int i=0; i<parameter->GetEntries(); i++) {
2356       TString entry=((TObjString*)parameter->At(i))->GetString();
2357       HLTDebug("scanning ECS entry: %s", entry.Data());
2358       TObjArray* entryParams=entry.Tokenize("=");
2359       if (entryParams) {
2360         if (entryParams->GetEntries()>1) {
2361           if ((((TObjString*)entryParams->At(0))->GetString()).CompareTo("CTP_TRIGGER_CLASS")==0) {
2362             int result=InitCTPTriggerClasses((((TObjString*)entryParams->At(1))->GetString()).Data());
2363             if (iResult>=0 && result<0) iResult=result;
2364           } else {
2365             // TODO: scan the other parameters
2366             // e.g. consistency check of run number
2367           }
2368         }
2369         delete entryParams;
2370       }
2371     }
2372     delete parameter;
2373   }
2374
2375   return iResult;
2376 }
2377
2378 int AliHLTComponent::InitCTPTriggerClasses(const char* ctpString)
2379 {
2380   // see header file for function documentation
2381   if (!ctpString) return -EINVAL;
2382
2383   if (fpTriggerClasses) {
2384     fpTriggerClasses->Delete();
2385   } else {
2386     fpTriggerClasses=new TObjArray(gkNCTPTriggerClasses);
2387   }
2388   if (!fpTriggerClasses) return -ENOMEM;
2389
2390   // general format of the CTP_TRIGGER_CLASS parameter
2391   // <bit position>:<Trigger class identifier string>:<detector-id-nr>-<detector-id-nr>-...,<bit position>:<Trigger class identifier string>:<detector-id-nr>-<detector-id-nr>-...,...
2392   // the detector ids are ignored for the moment
2393   HLTDebug(": %s", ctpString);
2394   TString string=ctpString;
2395   TObjArray* classEntries=string.Tokenize(",");
2396   if (classEntries) {
2397     for (int i=0; i<classEntries->GetEntries(); i++) {
2398       TString entry=((TObjString*)classEntries->At(i))->GetString();
2399       TObjArray* entryParams=entry.Tokenize(":");
2400       if (entryParams) {
2401         if (entryParams->GetEntries()==3 &&
2402             (((TObjString*)entryParams->At(0))->GetString()).IsDigit()) {
2403           int index=(((TObjString*)entryParams->At(0))->GetString()).Atoi();
2404           if (index<gkNCTPTriggerClasses) {
2405             fpTriggerClasses->AddAt(new TNamed("TriggerClass", (((TObjString*)entryParams->At(1))->GetString()).Data()), index);
2406           } else {
2407             // the trigger bitfield is fixed to 50 bits (gkNCTPTriggerClasses)
2408             HLTError("invalid trigger class entry %s, index width of trigger bitfield", entry.Data());
2409           }
2410         } else {
2411           HLTError("invalid trigger class entry %s", entry.Data());
2412         }
2413         delete entryParams;
2414       }
2415     }
2416     delete classEntries;
2417   }
2418   return 0;
2419 }
2420
2421 bool AliHLTComponent::EvaluateCTPTriggerClass(const char* expression, AliHLTComponentTriggerData& trigData) const
2422 {
2423   // see header file for function documentation
2424   if (!fpTriggerClasses) {
2425     HLTError("trigger classes not initialized");
2426     return false;
2427   }
2428
2429   if (trigData.fDataSize != sizeof(AliHLTEventTriggerData)) {
2430     HLTError("invalid trigger data size: %d expected %d", trigData.fDataSize, sizeof(AliHLTEventTriggerData));
2431     return false;
2432   }
2433
2434   // trigger mask is 50 bit wide and is stored in word 5 and 6 of the CDH
2435   AliHLTEventTriggerData* evtData=reinterpret_cast<AliHLTEventTriggerData*>(trigData.fData);
2436   AliHLTUInt64_t triggerMask=evtData->fCommonHeader[6];
2437   triggerMask<<=32;
2438   triggerMask|=evtData->fCommonHeader[5];
2439
2440   // use a TFormula to interprete the expression
2441   // all classname are replaced by '[n]' which means the n'th parameter in the formula
2442   // the parameters are set to 0 or 1 depending on the bit in the trigger mask
2443   //
2444   // TODO: this will most likely fail for class names like 'base', 'baseA', 'baseB'
2445   // the class names must be fully unique, none must be contained as substring in
2446   // another class name. Probably not needed for the moment but needs to be extended.
2447   vector<Double_t> par;
2448   TString condition=expression;
2449   for (int i=0; i<gkNCTPTriggerClasses; i++) {
2450     if (fpTriggerClasses->At(i)) {
2451       TString className=fpTriggerClasses->At(i)->GetTitle();
2452       //HLTDebug("checking trigger class %s", className.Data());
2453       if (condition.Contains(className)) {
2454         TString replace; replace.Form("[%d]", par.size());
2455         //HLTDebug("replacing %s with %s in \"%s\"", className.Data(), replace.Data(), condition.Data());
2456         condition.ReplaceAll(className, replace);
2457         if (triggerMask&((AliHLTUInt64_t)0x1<<i)) par.push_back(1.0);
2458         else par.push_back(0.0);
2459       }
2460     }
2461   }
2462
2463   TFormula form("trigger expression", condition);
2464   if (form.Compile()!=0) {
2465     HLTError("invalid expression %s", expression);
2466     return false;
2467   }
2468   if (form.EvalPar(&par[0], &par[0])>0.5) return true;
2469   return false;
2470 }