]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTComponent.cxx
HLTcalo module
[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 //  @note   The class is both used in Online (PubSub) and Offline (AliRoot)
25 //          context
26
27
28 //#include "AliHLTStdIncludes.h"
29 #include "AliHLTComponent.h"
30 #include "AliHLTComponentHandler.h"
31 #include "AliHLTMessage.h"
32 #include "AliHLTCTPData.h"
33 #include "AliHLTErrorGuard.h"
34 #include "AliHLTCDHWrapper.h"
35 #include "TString.h"
36 #include "TMath.h"
37 #include "TObjArray.h"
38 #include "TObjectTable.h"
39 #include "TClass.h"
40 #include "TStopwatch.h"
41 #include "TFormula.h"
42 #include "TUUID.h"
43 #include "TMD5.h"
44 #include "TRandom3.h"
45 #include "AliHLTMemoryFile.h"
46 #include "AliHLTMisc.h"
47 #include <cassert>
48 #include <ctime>
49 #include <stdint.h>
50
51 /**
52  * default compression level for ROOT objects
53  */
54 #define ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION 5
55 #define ALIHLTCOMPONENT_STATTIME_SCALER 1000000
56
57 /** ROOT macro for the implementation of ROOT specific class methods */
58 ClassImp(AliHLTComponent);
59
60 /** stopwatch macro using the stopwatch guard */
61 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
62 //#define ALIHLTCOMPONENT_STOPWATCH(type) 
63
64 /** stopwatch macro for operations of the base class */
65 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
66 /** stopwatch macro for operations of the detector algorithm (DA) */
67 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
68
69 AliHLTComponent::AliHLTComponent()
70   :
71   fEnvironment(),
72   fCurrentEvent(0),
73   fEventCount(-1),
74   fFailedEvents(0),
75   fCurrentEventData(),
76   fpInputBlocks(NULL),
77   fCurrentInputBlock(-1),
78   fSearchDataType(kAliHLTVoidDataType),
79   fClassName(),
80   fpInputObjects(NULL),
81   fpOutputBuffer(NULL),
82   fOutputBufferSize(0),
83   fOutputBufferFilled(0),
84   fOutputBlocks(),
85   fpStopwatches(new TObjArray(kSWTypeCount)),
86   fMemFiles(),
87   fpRunDesc(NULL),
88   fCDBSetRunNoFunc(NULL),
89   fChainId(),
90   fChainIdCrc(0),
91   fpBenchmark(NULL),
92   fFlags(0),
93   fEventType(gkAliEventTypeUnknown),
94   fComponentArgs(),
95   fEventDoneData(NULL),
96   fEventDoneDataSize(0),
97   fCompressionLevel(ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION)
98   , fLastObjectSize(0)
99   , fpCTPData(NULL)
100   , fPushbackPeriod(0)
101   , fLastPushBackTime(-1),
102   fEventModulo(-1)
103 {
104   // see header file for class documentation
105   // or
106   // refer to README to build package
107   // or
108   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
109   memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
110   if (fgpComponentHandler)
111     fgpComponentHandler->ScheduleRegister(this);
112   //SetLocalLoggingLevel(kHLTLogDefault);
113 }
114
115 AliHLTComponent::~AliHLTComponent()
116 {
117   // see header file for function documentation
118   if (fpBenchmark) delete fpBenchmark;
119   fpBenchmark=NULL;
120
121   CleanupInputObjects();
122   if (fpStopwatches!=NULL) delete fpStopwatches;
123   fpStopwatches=NULL;
124   AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
125   while (element!=fMemFiles.end()) {
126     if (*element) {
127       if ((*element)->IsClosed()==0) {
128         HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
129         // close but do not flush as we dont know whether the buffer is still valid
130         (*element)->CloseMemoryFile(0);
131       }
132       delete *element;
133       *element=NULL;
134     }
135     element++;
136   }
137   if (fpRunDesc) {
138     delete fpRunDesc;
139     fpRunDesc=NULL;
140   }
141   if (fEventDoneData)
142     delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
143   fEventDoneData=NULL;
144
145   if (fpCTPData) {
146     delete fpCTPData;
147   }
148   fpCTPData=NULL;
149 }
150
151 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
152
153 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite) 
154 {
155   // see header file for function documentation
156   int iResult=0;
157   if (fgpComponentHandler==NULL || bOverwrite!=0)
158     fgpComponentHandler=pCH;
159   else
160     iResult=-EPERM;
161   return iResult;
162 }
163
164 int AliHLTComponent::UnsetGlobalComponentHandler() 
165 {
166   // see header file for function documentation
167   return SetGlobalComponentHandler(NULL,1);
168 }
169
170 int AliHLTComponent::SetComponentEnvironment(const AliHLTAnalysisEnvironment* comenv, void* environParam)
171 {
172   // see header file for function documentation
173   HLTLogKeyword(fChainId.c_str());
174   int iResult=0;
175   if (comenv) {
176     memset(&fEnvironment, 0, sizeof(AliHLTAnalysisEnvironment));
177     memcpy(&fEnvironment, comenv, comenv->fStructSize<sizeof(AliHLTAnalysisEnvironment)?comenv->fStructSize:sizeof(AliHLTAnalysisEnvironment));
178     fEnvironment.fStructSize=sizeof(AliHLTAnalysisEnvironment);
179     fEnvironment.fParam=environParam;
180   }
181   return iResult;
182 }
183
184 int AliHLTComponent::Init(const AliHLTAnalysisEnvironment* comenv, void* environParam, int argc, const char** argv )
185 {
186   // see header file for function documentation
187   HLTLogKeyword(fChainId.c_str());
188   int iResult=0;
189   if (comenv) {
190     SetComponentEnvironment(comenv, environParam);
191   }
192   fPushbackPeriod=0;
193   fLastPushBackTime=-1;
194
195   fComponentArgs="";
196   const char** pArguments=NULL;
197   int iNofChildArgs=0;
198   TString argument="";
199   int bMissingParam=0;
200   if (argc>0) {
201     pArguments=new const char*[argc];
202     if (pArguments) {
203       for (int i=0; i<argc && iResult>=0; i++) {
204         if (fComponentArgs.size()>0) fComponentArgs+=" ";
205         fComponentArgs+=argv[i];
206         argument=argv[i];
207         if (argument.IsNull()) continue;
208
209         // benchmark
210         if (argument.CompareTo("-benchmark")==0) {
211
212           // -loglevel=
213         } else if (argument.BeginsWith("-loglevel=")) {
214           TString parameter=argument.ReplaceAll("-loglevel=", "");
215           parameter.Remove(TString::kLeading, ' '); // remove all blanks
216           if (parameter.BeginsWith("0x") &&
217               parameter.Replace(0,2,"",0).IsHex()) {
218             unsigned int loglevel=kHLTLogNone;
219             sscanf(parameter.Data(),"%x", &loglevel);
220             SetLocalLoggingLevel((AliHLTComponentLogSeverity)loglevel);
221           } else {
222             HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
223             iResult=-EINVAL;
224           }
225           // -object-compression=
226         } else if (argument.BeginsWith("-object-compression=")) {
227           argument.ReplaceAll("-object-compression=", "");
228           if (argument.IsDigit()) {
229             fCompressionLevel=argument.Atoi();
230             if (fCompressionLevel<0 || fCompressionLevel>9) {
231               HLTWarning("invalid compression level %d, setting to default %d", fCompressionLevel, ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION);
232               fCompressionLevel=ALIHLTCOMPONENT_DEFAULT_OBJECT_COMPRESSION;
233             }
234           } else {
235             HLTError("wrong parameter for argument -object-compression, number expected");
236           }
237           // -pushback-period=
238         } else if (argument.BeginsWith("-pushback-period=")) {
239           argument.ReplaceAll("-pushback-period=", "");
240           if (argument.IsDigit()) {
241             fPushbackPeriod=argument.Atoi();
242           } else {
243             HLTError("wrong parameter for argument -pushback-period, number expected");
244           }
245           // -event-modulo
246         } else if (argument.BeginsWith("-event-modulo=")) {
247           argument.ReplaceAll("-event-modulo=", "");
248           if (argument.IsDigit()) {
249             fEventModulo=argument.Atoi();
250             if (fEventModulo < 1)
251             {
252               fEventModulo = -1;
253               HLTError("number passed in -event-modulo must be a positive integer greater or equal to 1.");
254             }
255           } else {
256             HLTError("wrong parameter for argument -event-modulo, integer number expected");
257           }
258           // -disable-component-stat
259         } else if (argument.CompareTo("-disable-component-stat")==0) {
260           fFlags|=kDisableComponentStat;
261         } else {
262           pArguments[iNofChildArgs++]=argv[i];
263         }
264       }
265     } else {
266       iResult=-ENOMEM;
267     }
268   }
269   if (bMissingParam) {
270     HLTError("missing parameter for argument %s", argument.Data());
271     iResult=-EINVAL;
272   }
273   if (iResult>=0) {
274     iResult=CheckOCDBEntries();
275   }
276   if (iResult>=0) {
277     iResult=DoInit(iNofChildArgs, pArguments);
278   }
279   if (iResult>=0) {
280     fEventCount=0;
281
282     // find out if the component wants to get the steering events
283     // explicitly
284     AliHLTComponentDataTypeList inputDt;
285     GetInputDataTypes(inputDt);
286     bool bRequireSteeringBlocks=false;
287     for (AliHLTComponentDataTypeList::iterator dt=inputDt.begin();
288          dt!=inputDt.end() && !bRequireSteeringBlocks;
289          dt++) {
290       bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeSOR);
291       bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeRunType);
292       bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeEOR);
293       bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeDDL);
294       bRequireSteeringBlocks|=MatchExactly(*dt,kAliHLTDataTypeComponentStatistics);
295     }
296     if (bRequireSteeringBlocks) fFlags|=kRequireSteeringBlocks;
297   }
298   if (pArguments) delete [] pArguments;
299
300 #if defined(HLT_COMPONENT_STATISTICS)
301   // benchmarking stopwatch for the component statistics
302   fpBenchmark=new TStopwatch;
303 #endif // HLT_COMPONENT_STATISTICS
304
305   return iResult;
306 }
307
308 int AliHLTComponent::Deinit()
309 {
310   // see header file for function documentation
311   HLTLogKeyword(fChainId.c_str());
312   int iResult=0;
313   iResult=DoDeinit();
314   if (fpRunDesc) {
315     // TODO: the warning should be kept, but the condition is wrong since the
316     // AliHLTRunDesc is set before the SOR event in the SetRunDescription
317     // method. A couple of state flags should be defined but that is a bit more
318     // work to do. For the moment disable the warning (2009-07-01)
319     // 2009-09-08: now, the info is not cleared in the ProcessEvent, because it
320     // might be needed by components during the event processing.
321     //HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
322     AliHLTRunDesc* pRunDesc=fpRunDesc;
323     fpRunDesc=NULL;
324     delete pRunDesc;
325   }
326   if (fpCTPData) {
327     delete fpCTPData;
328   }
329   fpCTPData=NULL;
330
331   fEventCount=0;
332   fFlags=0;
333   return iResult;
334 }
335
336 int AliHLTComponent::InitCDB(const char* cdbPath, AliHLTComponentHandler* pHandler)
337 {
338   // see header file for function documentation
339   int iResult=0;
340   HLTInfo("Using CDB: %s", cdbPath);
341   if (pHandler) {
342   // I have to think about separating the library handling from the
343   // component handler. Requiring the component handler here is not
344   // the cleanest solution.
345   // We presume the library already to be loaded, which is the case
346   // because it is loaded in the initialization of the logging functionality
347   //
348   // find the symbol
349   AliHLTMiscInitCDB_t pFunc=(AliHLTMiscInitCDB_t)pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_INIT_CDB);
350   if (pFunc) {
351     TString path;
352     if (cdbPath && cdbPath[0]!=0) {
353       path=cdbPath;
354       // very temporary fix, have to check for other formats
355       if (!path.BeginsWith("local://")) {
356         path="local://";
357         path+=cdbPath;
358       }
359     }
360     if ((iResult=(*pFunc)(path.Data()))>=0) {
361       if (!(fCDBSetRunNoFunc=pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_SET_CDB_RUNNO))) {
362         Message(NULL, kHLTLogWarning, "AliHLTComponent::InitCDB", "init CDB",
363                 "can not find function to set CDB run no");
364       }
365     }
366   } else {
367     Message(NULL, kHLTLogError, "AliHLTComponent::InitCDB", "init CDB",
368             "can not find initialization function");
369     iResult=-ENOSYS;
370   }
371   } else {
372     iResult=-EINVAL;
373   }
374   return iResult;
375 }
376
377 int AliHLTComponent::SetCDBRunNo(int runNo)
378 {
379   // see header file for function documentation
380   if (!fCDBSetRunNoFunc) return 0;
381   return (*((AliHLTMiscSetCDBRunNo_t)fCDBSetRunNoFunc))(runNo);
382 }
383
384 int AliHLTComponent::SetRunDescription(const AliHLTRunDesc* desc, const char* /*runType*/)
385 {
386   // see header file for function documentation
387   if (!desc) return -EINVAL;
388   if (desc->fStructSize!=sizeof(AliHLTRunDesc)) {
389     HLTError("invalid size of RunDesc struct (%ul)", desc->fStructSize);
390     return -EINVAL;
391   }
392
393   if (!fpRunDesc) {
394     fpRunDesc=new AliHLTRunDesc;
395     if (!fpRunDesc) return -ENOMEM;
396     *fpRunDesc=kAliHLTVoidRunDesc;
397   }
398
399   if (fpRunDesc->fRunNo!=kAliHLTVoidRunNo && fpRunDesc->fRunNo!=desc->fRunNo) {
400     HLTWarning("Run description has already been set");
401   }
402   *fpRunDesc=*desc;
403   SetCDBRunNo(fpRunDesc->fRunNo);
404   // TODO: we have to decide about the runType
405   return 0;
406 }
407
408 int AliHLTComponent::SetComponentDescription(const char* desc)
409 {
410   // see header file for function documentation
411   int iResult=0;
412   if (!desc) return 0;
413
414   TString descriptor=desc;
415   TObjArray* pTokens=descriptor.Tokenize(" ");
416   if (pTokens) {
417     for (int i=0; i<pTokens->GetEntriesFast() && iResult>=0; i++) {
418       TString argument=pTokens->At(i++)->GetName();
419       if (!argument || argument.IsNull()) continue;
420
421       // chainid
422       if (argument.BeginsWith("chainid")) {
423         argument.ReplaceAll("chainid", "");
424         if (argument.BeginsWith("=")) {
425           fChainId=argument.Replace(0,1,"");
426           fChainIdCrc=CalculateChecksum((const AliHLTUInt8_t*)fChainId.c_str(), fChainId.length());
427           HLTDebug("setting component description: chain id %s crc 0x%8x", fChainId.c_str(), fChainIdCrc);
428         } else {
429           fChainId="";
430         }
431       } else {
432         HLTWarning("unknown component description %s", argument.Data());
433       }
434     }
435     delete pTokens;
436   }
437   
438   return iResult;
439 }
440
441 int AliHLTComponent::ConfigureFromArgumentString(int argc, const char** argv)
442 {
443   // Configure from an array of argument strings
444   // Function supports individual arguments in the argv array and arguments
445   // separated by blanks.
446   //
447   // Each argv entry can contain blanks, quotes and newlines. Newlines are interpreted
448   // as blanks. Enclosing quotes deactivate blank as delimiter.
449   // The separated arguments are stored in an array of strings, and the pointers to
450   // those strings in an array of pointers. The latter is used in the custom argument
451   // scan of the component.
452
453   int iResult=0;
454   vector<string> stringarray;   // array of argument copies
455   // array of pointers to the argument copies
456   // note: not necessarily in sync with the entries in stringarray
457   // can contain any pointer to valid arguments in arbitrary sequence
458   vector<const char*> ptrarray;
459
460   TString argument="";
461   int i=0;
462   for (i=0; i<argc && iResult>=0; i++) {
463     argument=argv[i];
464     if (argument.IsWhitespace()) continue;
465
466     // special handling for single component arguments ending with
467     // a sequence of blanks. All characters until the first occurence
468     // of a blank are removed. If the remainder contains only whitespaces
469     // the argument is a single argument, just having whitespaces at the end.
470     argument.Remove(0, argument.First(' '));
471     if (argument.IsWhitespace()) {
472       stringarray.push_back(argv[i]);
473       continue;
474     }
475
476     // outer loop checks for enclosing quotes
477     // extra blank to insert blank token before leading quotes, then
478     // quoted arguments are always the even ones
479     argument=" ";
480     argument+=argv[i];
481     // insert blank in consecutive quotes to correctly tokenize
482     argument.ReplaceAll("''", "' '");
483     // replace newlines by blanks
484     argument.ReplaceAll("\n", " ");
485     if (argument.IsNull()) continue;
486     TObjArray* pTokensQuote=argument.Tokenize("'");
487     if (pTokensQuote) {
488       if (pTokensQuote->GetEntriesFast()>0) {
489         for (int k=0; k<pTokensQuote->GetEntriesFast(); k++) {
490           argument=pTokensQuote->At(k)->GetName();
491           if (argument.IsWhitespace()) continue;
492           if (k%2) {
493             // every second entry is enclosed by quotes and thus
494             // one single argument
495             stringarray.push_back(argument.Data());
496           } else {
497     TObjArray* pTokens=argument.Tokenize(" ");
498     if (pTokens) {
499       if (pTokens->GetEntriesFast()>0) {
500         for (int n=0; n<pTokens->GetEntriesFast(); n++) {
501           TString data=pTokens->At(n)->GetName();
502           if (!data.IsNull() && !data.IsWhitespace()) {
503             stringarray.push_back(data.Data());
504           }
505         }
506       }
507       delete pTokens;
508     }
509           }
510         }
511       }
512       delete pTokensQuote;
513     }
514   }
515
516   // fill ptrarray; should be safe at this point
517   // since stringarray isn't modified any further
518   unsigned int idx;
519   for(idx=0; idx<stringarray.size(); ++idx) {
520     ptrarray.push_back(stringarray.at(idx).c_str());
521   }
522
523   for (i=0; (unsigned)i<ptrarray.size() && iResult>=0;) {
524     int result=ScanConfigurationArgument(ptrarray.size()-i, &ptrarray[i]);
525     if (result==0) {
526       HLTWarning("unknown component argument %s", ptrarray[i]);
527       i++;
528     } else if (result>0) {
529       i+=result;
530     } else {
531       iResult=result;
532       if (iResult==-EINVAL) {
533         HLTError("unknown argument %s", ptrarray[i]);
534       } else if (iResult==-EPROTO) {
535         HLTError("missing/wrong parameter for argument %s (%s)", ptrarray[i], (ptrarray.size()>(unsigned)i+1)?ptrarray[i+1]:"missing");
536       } else {
537         HLTError("scan of argument %s failed (%d)", ptrarray[i], iResult);
538       }
539     }
540   }
541
542   return iResult;
543 }
544
545 int AliHLTComponent::ConfigureFromCDBTObjString(const char* entries, const char* key)
546 {
547   // load a list of OCDB objects and configure from the objects
548   // can either be a TObjString or a TMap with a TObjString:TObjString key-value pair
549   int iResult=0;
550   TString arguments;
551   TString confEntries=entries;
552   TObjArray* pTokens=confEntries.Tokenize(" ");
553   if (pTokens) {
554     for (int n=0; n<pTokens->GetEntriesFast(); n++) {
555       const char* path=pTokens->At(n)->GetName();
556       const char* chainId=GetChainId();
557       HLTInfo("configure from entry \"%s\"%s%s, chain id %s", path, key?" key ":"",key?key:"", (chainId!=NULL && chainId[0]!=0)?chainId:"<none>");
558       TObject* pOCDBObject = LoadAndExtractOCDBObject(path, key);
559       if (pOCDBObject) {
560         TObjString* pString=dynamic_cast<TObjString*>(pOCDBObject);
561         if (!pString) {
562           TMap* pMap=dynamic_cast<TMap*>(pOCDBObject);
563           if (pMap) {
564             // this is the case where no key has been specified and the OCDB
565             // object is a TMap, search for the default key
566             TObject* pObject=pMap->GetValue("default");
567             if (pObject && (pString=dynamic_cast<TObjString*>(pObject))!=NULL) {
568               HLTInfo("using default key of TMap of configuration object \"%s\"", path);
569             } else {
570               HLTError("no default key available in TMap of configuration object \"%s\"", path);
571               iResult=-ENOENT;
572               break;
573             }
574           }
575         }
576
577         if (pString) {
578           HLTInfo("received configuration object string: \'%s\'", pString->GetName());
579           arguments+=pString->GetName();
580           arguments+=" ";
581         } else {
582           HLTError("configuration object \"%s\"%s%s has wrong type, required TObjString", path, key?" key ":"",key?key:"");
583           iResult=-EINVAL;
584         }
585       } else {
586         HLTError("can not fetch object \"%s\" from OCDB", path);
587         iResult=-ENOENT;
588       }
589     }
590     delete pTokens;
591   }
592   if (iResult>=0 && !arguments.IsNull())  {
593     const char* array=arguments.Data();
594     iResult=ConfigureFromArgumentString(1, &array);
595   }
596   return iResult;
597 }
598
599 TObject* AliHLTComponent::LoadAndExtractOCDBObject(const char* path, const char* key) const
600 {
601   // see header file for function documentation
602   AliCDBEntry* pEntry=AliHLTMisc::Instance().LoadOCDBEntry(path, GetRunNo());
603   if (!pEntry) return NULL;
604   TObject* pObject=AliHLTMisc::Instance().ExtractObject(pEntry);
605   TMap* pMap=dynamic_cast<TMap*>(pObject);
606   if (pMap && key) {
607     pObject=pMap->GetValue(key);
608     if (!pObject) {
609       pObject=pMap->GetValue("default");
610       if (pObject) {
611         HLTWarning("can not find object for key \"%s\" in TMap of configuration object \"%s\", using key \"default\"", key, path);
612       }
613     }
614     if (!pObject) {
615       HLTError("can not find object for key \"%s\" in TMap of configuration object \"%s\"", key, path);
616       return NULL;
617     }
618   }
619   return pObject;
620 }
621
622 int AliHLTComponent::DoInit( int /*argc*/, const char** /*argv*/)
623 {
624   // default implementation, childs can overload
625   HLTLogKeyword("dummy");
626   return 0;
627 }
628
629 int AliHLTComponent::DoDeinit()
630 {
631   // default implementation, childs can overload
632   HLTLogKeyword("dummy");
633   return 0;
634 }
635
636 int AliHLTComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/)
637 {
638   // default implementation, childs can overload
639   HLTLogKeyword("dummy");
640   return 0;
641 }
642
643 int AliHLTComponent::ReadPreprocessorValues(const char* /*modules*/)
644 {
645   // default implementation, childs can overload
646   HLTLogKeyword("dummy");
647   return 0;
648 }
649
650 int AliHLTComponent::ScanConfigurationArgument(int /*argc*/, const char** /*argv*/)
651 {
652   // default implementation, childs can overload
653   HLTLogKeyword("dummy");
654   HLTWarning("The function needs to be implemented by the component");
655   return 0;
656 }
657
658 int AliHLTComponent::StartOfRun()
659 {
660   // default implementation, childs can overload
661   HLTLogKeyword("dummy");
662   return 0;
663 }
664
665 int AliHLTComponent::EndOfRun()
666 {
667   // default implementation, childs can overload
668   HLTLogKeyword("dummy");
669   return 0;
670 }
671
672
673 int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
674 {
675   // default implementation, childs can overload
676   HLTLogKeyword("dummy");
677   return 0;
678 }
679
680 void AliHLTComponent::GetOCDBObjectDescription( TMap* const /*targetArray*/)
681 {
682   // default implementation, childs can overload
683   HLTLogKeyword("dummy");
684 }
685
686 int AliHLTComponent::CheckOCDBEntries(const TMap* const externList)
687 {
688   // check the availability of the OCDB entry descriptions in the TMap
689   //  key : complete OCDB path of the entry
690   //  value : auxiliary object - short description
691   // if the external map was not provided the function invokes
692   // interface function GetOCDBObjectDescription() to retrieve the list.
693   int iResult=0;
694   if (externList) {
695     iResult=AliHLTMisc::Instance().CheckOCDBEntries(externList);
696   } else {
697     TMap* pMap=new TMap;
698     if (pMap) {
699       pMap->SetOwnerKeyValue(kTRUE);
700       GetOCDBObjectDescription(pMap);
701       iResult=AliHLTMisc::Instance().CheckOCDBEntries(pMap);
702       delete pMap;
703       pMap=NULL;
704     }
705   }
706
707   return iResult;
708 }
709
710 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
711 {
712   // see header file for function documentation
713   memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
714   strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
715   strncat( output, ":", 1 );
716   strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
717 }
718
719 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
720 {
721   // see header file for function documentation
722   string out("");
723
724   // 'typeid' 'origin'
725   // aligned to 8 and 4 chars respectively, blocks enclosed in quotes and
726   // separated by blank e.g.
727   // 'DDL_RAW ' 'TPC '
728   if (mode==3) {
729     int i=0;
730     char tmp[8];
731     out+="'";
732     for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
733       unsigned char* puc=(unsigned char*)type.fID;
734       if (puc[i]<32)
735         sprintf(tmp, "\\%x", type.fID[i]);
736       else
737         sprintf(tmp, "%c", type.fID[i]);
738       out+=tmp;
739     }
740     out+="' '";
741     for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
742       unsigned char* puc=(unsigned char*)type.fOrigin;
743       if ((puc[i])<32)
744         sprintf(tmp, "\\%x", type.fOrigin[i]);
745       else
746         sprintf(tmp, "%c", type.fOrigin[i]);
747       out+=tmp;
748     }
749     out+="'";
750     return out;
751   }
752
753   // origin typeid as numbers separated by colon e.g.
754   // aligned to 8 and 4 chars respectively, all characters separated by
755   // quotes, e.g.
756   // '84'80'67'32':'68'68'76'95'82'65'87'32'
757   if (mode==2) {
758     int i=0;
759     char tmp[8];
760     for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
761       sprintf(tmp, "'%d", type.fOrigin[i]);
762       out+=tmp;
763     }
764     out+="':'";
765     for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
766       sprintf(tmp, "%d'", type.fID[i]);
767       out+=tmp;
768     }
769     return out;
770   }
771
772   // origin typeid separated by colon e.g.
773   // aligned to 8 and 4 chars respectively, all characters separated by
774   // quotes, e.g.
775   // 'T'P'C' ':'D'D'L'_'R'A'W' '
776   if (mode==1) {
777     int i=0;
778     char tmp[8];
779     for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
780       unsigned char* puc=(unsigned char*)type.fOrigin;
781       if ((puc[i])<32)
782         sprintf(tmp, "'\\%x", type.fOrigin[i]);
783       else
784         sprintf(tmp, "'%c", type.fOrigin[i]);
785       out+=tmp;
786     }
787     out+="':'";
788     for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
789       unsigned char* puc=(unsigned char*)type.fID;
790       if (puc[i]<32)
791         sprintf(tmp, "\\%x'", type.fID[i]);
792       else
793         sprintf(tmp, "%c'", type.fID[i]);
794       out+=tmp;
795     }
796     return out;
797   }
798
799   // origin typeid
800   // aligned to 8 and 4 chars respectively, separated by colon e.g.
801   // TPC :DDL_RAW 
802   if (type==kAliHLTVoidDataType) {
803     out="VOID:VOID";
804   } else {
805     // some gymnastics in order to avoid a '0' which is part of either or both
806     // ID and origin terminating the whole string. Unfortunately, string doesn't
807     // stop appending at the '0' if the number of elements to append was 
808     // explicitely specified
809     string tmp("");
810     tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
811     out.append(tmp.c_str());
812     out.append(":");
813     tmp="";
814     tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
815     out.append(tmp.c_str());
816   }
817   return out;
818 }
819
820
821 void* AliHLTComponent::AllocMemory( unsigned long size ) 
822 {
823   // see header file for function documentation
824   if (fEnvironment.fAllocMemoryFunc)
825     return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
826   HLTFatal("no memory allocation handler registered");
827   return NULL;
828 }
829
830 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
831                                               AliHLTComponentBlockData** outputBlocks ) 
832 {
833   // see header file for function documentation
834     if ( blockCount==NULL || outputBlocks==NULL )
835         return -EFAULT;
836     AliHLTUInt32_t count = blocks.size();
837     if ( !count )
838         {
839         *blockCount = 0;
840         *outputBlocks = NULL;
841         return 0;
842         }
843     *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
844     if ( !*outputBlocks )
845         return -ENOMEM;
846     for ( unsigned long i = 0; i < count; i++ ) {
847         (*outputBlocks)[i] = blocks[i];
848         if (MatchExactly(blocks[i].fDataType, kAliHLTAnyDataType)) {
849           (*outputBlocks)[i].fDataType=GetOutputDataType();
850           /* data type was set to the output data type by the PubSub AliRoot
851              Wrapper component, if data type of the block was ********:****.
852              Now handled by the component base class in order to have same
853              behavior when running embedded in AliRoot
854           memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
855           memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
856           */
857         }
858     }
859     *blockCount = count;
860     return 0;
861
862 }
863
864 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd ) const
865 {
866   // see header file for function documentation
867   if (fEnvironment.fGetEventDoneDataFunc)
868     return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
869   return -ENOSYS;
870 }
871
872 int AliHLTComponent::ReserveEventDoneData( unsigned long size )
873 {
874   // see header file for function documentation
875   int iResult=0;
876
877   unsigned long capacity=fEventDoneDataSize;
878   if (fEventDoneData) capacity-=sizeof(AliHLTComponentEventDoneData)+fEventDoneData->fDataSize;
879   if (size>capacity) {
880     unsigned long newSize=sizeof(AliHLTComponentEventDoneData)+size+(fEventDoneDataSize-capacity);
881     AliHLTComponentEventDoneData* newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[newSize] );
882     if (!newEDD)
883       return -ENOMEM;
884     newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
885     newEDD->fDataSize = 0;
886     newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
887     if (fEventDoneData) {
888       memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
889       newEDD->fDataSize = fEventDoneData->fDataSize;
890       delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
891     }
892     fEventDoneData = newEDD;
893     fEventDoneDataSize = newSize;
894   }
895   return iResult;
896
897 }
898
899 int AliHLTComponent::PushEventDoneData( AliHLTUInt32_t eddDataWord )
900 {
901   // see header file for function documentation
902   if (!fEventDoneData)
903     return -ENOMEM;
904   if (fEventDoneData->fDataSize+sizeof(AliHLTUInt32_t)>fEventDoneDataSize)
905     return -ENOSPC;
906   *reinterpret_cast<AliHLTUInt32_t*>((reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize)) = eddDataWord;
907   fEventDoneData->fDataSize += sizeof(AliHLTUInt32_t);
908   return 0;
909 }
910
911 void AliHLTComponent::ReleaseEventDoneData()
912 {
913    // see header file for function documentation
914  if (fEventDoneData)
915     delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
916   fEventDoneData = NULL;
917   fEventDoneDataSize = 0;
918 }
919
920
921 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList) 
922 {
923   // see header file for function documentation
924   int iResult=0;
925   if (pConsumer) {
926     AliHLTComponentDataTypeList itypes;
927     AliHLTComponentDataTypeList otypes;
928     otypes.push_back(GetOutputDataType());
929     if (MatchExactly(otypes[0],kAliHLTMultipleDataType)) {
930       otypes.clear();
931       int count=0;
932       if ((count=GetOutputDataTypes(otypes))>0) {
933       } else if (GetComponentType()!=kSink) {
934         HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
935       }
936     }
937     pConsumer->GetInputDataTypes(itypes);
938     AliHLTComponentDataTypeList::iterator otype=otypes.begin();
939     for (;otype!=otypes.end();otype++) {
940       //PrintDataTypeContent((*otype), "publisher \'%s\'");
941       if ((*otype)==(kAliHLTAnyDataType|kAliHLTDataOriginPrivate)) {
942         if (tgtList) tgtList->push_back(*otype);
943         iResult++;
944         continue;
945       }
946       
947       AliHLTComponentDataTypeList::iterator itype=itypes.begin();
948       for ( ; itype!=itypes.end() && (*itype)!=(*otype) ; itype++) {/* empty body */};
949       //if (itype!=itypes.end()) PrintDataTypeContent(*itype, "consumer \'%s\'");
950       if (itype!=itypes.end()) {
951         if (tgtList) tgtList->push_back(*otype);
952         iResult++;
953       }
954     }
955   } else {
956     iResult=-EINVAL;
957   }
958   return iResult;
959 }
960
961 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format)
962 {
963   // see header file for function documentation
964   const char* fmt="\'%s\'";
965   if (format) fmt=format;
966   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, Form(fmt, (DataType2Text(dt)).c_str()));
967   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, 
968                          Form("%x %x %x %x %x %x %x %x : %x %x %x %x", 
969                               dt.fID[0],
970                               dt.fID[1],
971                               dt.fID[2],
972                               dt.fID[3],
973                               dt.fID[4],
974                               dt.fID[5],
975                               dt.fID[6],
976                               dt.fID[7],
977                               dt.fOrigin[0],
978                               dt.fOrigin[1],
979                               dt.fOrigin[2],
980                               dt.fOrigin[3]));
981 }
982
983 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
984 {
985   // see header file for function documentation
986   blockData.fStructSize = sizeof(blockData);
987   FillShmData( blockData.fShmKey );
988   blockData.fOffset = ~(AliHLTUInt32_t)0;
989   blockData.fPtr = NULL;
990   blockData.fSize = 0;
991   FillDataType( blockData.fDataType );
992   blockData.fSpecification = kAliHLTVoidDataSpec;
993 }
994
995 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
996 {
997   // see header file for function documentation
998   shmData.fStructSize = sizeof(shmData);
999   shmData.fShmType = gkAliHLTComponentInvalidShmType;
1000   shmData.fShmID = gkAliHLTComponentInvalidShmID;
1001 }
1002
1003 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
1004 {
1005   // see header file for function documentation
1006   dataType=kAliHLTAnyDataType;
1007 }
1008
1009 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt) 
1010 {
1011   // see header file for function documentation
1012   memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
1013   memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
1014 }
1015
1016 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin) 
1017 {
1018   // see header file for function documentation
1019   tgtdt.fStructSize=sizeof(AliHLTComponentDataType);
1020   if (id) {
1021     memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
1022     strncpy(&tgtdt.fID[0], id, strlen(id)<(size_t)kAliHLTComponentDataTypefIDsize?strlen(id):kAliHLTComponentDataTypefIDsize);
1023   }
1024   if (origin) {
1025     memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
1026     strncpy(&tgtdt.fOrigin[0], origin, strlen(origin)<(size_t)kAliHLTComponentDataTypefOriginSize?strlen(origin):kAliHLTComponentDataTypefOriginSize);
1027   }
1028 }
1029
1030 void AliHLTComponent::SetDataType(AliHLTComponentDataType& dt, AliHLTUInt64_t id, AliHLTUInt32_t origin)
1031 {
1032   // see header file for function documentation
1033   dt.fStructSize=sizeof(AliHLTComponentDataType);
1034   assert(kAliHLTComponentDataTypefIDsize==sizeof(id));
1035   assert(kAliHLTComponentDataTypefOriginSize==sizeof(origin));
1036   memcpy(&dt.fID, &id, kAliHLTComponentDataTypefIDsize);
1037   memcpy(&dt.fOrigin, &origin, kAliHLTComponentDataTypefOriginSize);
1038 }
1039
1040 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
1041 {
1042   // see header file for function documentation
1043   memset(&evtData, 0, sizeof(AliHLTComponentEventData));
1044   evtData.fStructSize=sizeof(AliHLTComponentEventData);
1045   evtData.fEventID=kAliHLTVoidEventID;
1046   evtData.fEventCreation_s = kMaxUInt;
1047 }
1048
1049 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt) 
1050 {
1051   // see header file for function documentation
1052   TString msg;
1053   msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
1054   for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
1055    if (dt.fID[i]!=0) msg+=dt.fID[i];
1056    else msg+="\\0";
1057   }
1058   msg+="\" Origin=\"";
1059   for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
1060    if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
1061    else msg+="\\0";
1062   }
1063   msg+="\"";
1064   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
1065 }
1066
1067 int AliHLTComponent::GetEventCount() const
1068 {
1069   // see header file for function documentation
1070   return fEventCount;
1071 }
1072
1073 int AliHLTComponent::IncrementEventCounter()
1074 {
1075   // see header file for function documentation
1076   if (fEventCount>=0) fEventCount++;
1077   return fEventCount;
1078 }
1079
1080 int AliHLTComponent::GetNumberOfInputBlocks() const
1081 {
1082   // see header file for function documentation
1083   if (fpInputBlocks!=NULL) {
1084     return fCurrentEventData.fBlockCnt;
1085   }
1086   return 0;
1087 }
1088
1089 AliHLTEventID_t AliHLTComponent::GetEventId() const
1090 {
1091   // see header file for function documentation
1092   if (fpInputBlocks!=NULL) {
1093     return fCurrentEventData.fEventID;
1094   }
1095   return 0;
1096 }
1097
1098 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
1099                                                     const char* classname,
1100                                                     int bForce)
1101 {
1102   // see header file for function documentation
1103   ALIHLTCOMPONENT_BASE_STOPWATCH();
1104   fSearchDataType=dt;
1105   if (classname) fClassName=classname;
1106   else fClassName.clear();
1107   int idx=FindInputBlock(fSearchDataType, 0, 1);
1108   TObject* pObj=NULL;
1109   if (idx>=0) {
1110     HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
1111     if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
1112       fCurrentInputBlock=idx;
1113     } else {
1114     }
1115   }
1116   return pObj;
1117 }
1118
1119 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID, 
1120                                                     const char* dtOrigin,
1121                                                     const char* classname,
1122                                                     int         bForce)
1123 {
1124   // see header file for function documentation
1125   ALIHLTCOMPONENT_BASE_STOPWATCH();
1126   AliHLTComponentDataType dt;
1127   SetDataType(dt, dtID, dtOrigin);
1128   return GetFirstInputObject(dt, classname, bForce);
1129 }
1130
1131 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
1132 {
1133   // see header file for function documentation
1134   ALIHLTCOMPONENT_BASE_STOPWATCH();
1135   int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
1136   //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
1137   TObject* pObj=NULL;
1138   if (idx>=0) {
1139     if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
1140       fCurrentInputBlock=idx;
1141     }
1142   }
1143   return pObj;
1144 }
1145
1146 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
1147 {
1148   // see header file for function documentation
1149   int iResult=-ENOENT;
1150   if (fpInputBlocks!=NULL) {
1151     int idx=startIdx<0?0:startIdx;
1152     for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
1153       if (dt!=fpInputBlocks[idx].fDataType) continue;
1154
1155       if (bObject!=0) {
1156         if (fpInputBlocks[idx].fPtr==NULL) continue;
1157         AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
1158         if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
1159       }
1160       iResult=idx;
1161     }
1162   }
1163   return iResult;
1164 }
1165
1166 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
1167 {
1168   // see header file for function documentation
1169   TObject* pObj=NULL;
1170   if (fpInputBlocks!=NULL) {
1171     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
1172       if (fpInputBlocks[idx].fPtr) {
1173         AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
1174         if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
1175           HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
1176           AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
1177           TClass* objclass=msg.GetClass();
1178           pObj=msg.ReadObject(objclass);
1179           if (pObj && objclass) {
1180             HLTDebug("object %p type %s created", pObj, objclass->GetName());
1181           } else {
1182           }
1183           //} else {
1184         } else if (bForce!=0) {
1185           HLTError("size mismatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
1186         }
1187       } else {
1188         HLTFatal("block descriptor empty");
1189       }
1190     } else {
1191       HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
1192     }
1193   } else {
1194     HLTError("no input blocks available");
1195   }
1196   
1197   return pObj;
1198 }
1199
1200 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
1201 {
1202   // see header file for function documentation
1203   if (fpInputObjects==NULL) {
1204     fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
1205   }
1206   TObject* pObj=NULL;
1207   if (fpInputObjects) {
1208     pObj=fpInputObjects->At(idx);
1209     if (pObj==NULL) {
1210       pObj=CreateInputObject(idx, bForce);
1211       if (pObj) {
1212         fpInputObjects->AddAt(pObj, idx);
1213       }
1214     }
1215   } else {
1216     HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
1217   }
1218   return pObj;
1219 }
1220
1221 int AliHLTComponent::CleanupInputObjects()
1222 {
1223   // see header file for function documentation
1224   if (!fpInputObjects) return 0;
1225   TObjArray* array=fpInputObjects;
1226   fpInputObjects=NULL;
1227   for (int i=0; i<array->GetEntriesFast(); i++) {
1228     TObject* pObj=array->At(i);
1229     // grrr, garbage collection strikes back: When read via AliHLTMessage
1230     // (CreateInputObject), and written to a TFile afterwards, the
1231     // TFile::Close calls ROOOT's garbage collection. No clue why the
1232     // object ended up in the key list and needs to be deleted
1233     //
1234     // Matthias 09.11.2008 follow up
1235     // This approach doesn't actually work in all cases: the object table
1236     // can be switched off globally, the flag needs to be checked here as
1237     // well in order to avoid memory leaks.
1238     // This means we have to find another solution for the problem if it
1239     // pops up again.
1240     if (pObj &&
1241         (!TObject::GetObjectStat() || gObjectTable->PtrIsValid(pObj))) {
1242       delete pObj;
1243     }
1244   }
1245   delete array;
1246   return 0;
1247 }
1248
1249 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
1250 {
1251   // see header file for function documentation
1252   ALIHLTCOMPONENT_BASE_STOPWATCH();
1253   AliHLTComponentDataType dt=kAliHLTVoidDataType;
1254   int idx=fCurrentInputBlock;
1255   if (pObject) {
1256     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1257     } else {
1258       HLTError("unknown object %p", pObject);
1259     }
1260   }
1261   if (idx>=0) {
1262     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
1263       dt=fpInputBlocks[idx].fDataType;
1264     } else {
1265       HLTFatal("severe internal error, index out of range");
1266     }
1267   }
1268   return dt;
1269 }
1270
1271 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
1272 {
1273   // see header file for function documentation
1274   ALIHLTCOMPONENT_BASE_STOPWATCH();
1275   AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
1276   int idx=fCurrentInputBlock;
1277   if (pObject) {
1278     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1279     } else {
1280       HLTError("unknown object %p", pObject);
1281     }
1282   }
1283   if (idx>=0) {
1284     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
1285       iSpec=fpInputBlocks[idx].fSpecification;
1286     } else {
1287       HLTFatal("severe internal error, index out of range");
1288     }
1289   }
1290   return iSpec;
1291 }
1292
1293 int AliHLTComponent::Forward(const TObject* pObject)
1294 {
1295   // see header file for function documentation
1296   int iResult=0;
1297   int idx=fCurrentInputBlock;
1298   if (pObject) {
1299     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
1300     } else {
1301       HLTError("unknown object %p", pObject);
1302       iResult=-ENOENT;
1303     }
1304   }
1305   if (idx>=0) {
1306     fOutputBlocks.push_back(fpInputBlocks[idx]);
1307   }
1308   return iResult;
1309 }
1310
1311 int AliHLTComponent::Forward(const AliHLTComponentBlockData* pBlock)
1312 {
1313   // see header file for function documentation
1314   int iResult=0;
1315   int idx=fCurrentInputBlock;
1316   if (pBlock) {
1317     if ((idx=FindInputBlock(pBlock))>=0) {
1318     } else {
1319       HLTError("unknown Block %p", pBlock);
1320       iResult=-ENOENT;      
1321     }
1322   }
1323   if (idx>=0) {
1324     // check for fpInputBlocks pointer done in FindInputBlock
1325     fOutputBlocks.push_back(fpInputBlocks[idx]);
1326   }
1327   return iResult;
1328 }
1329
1330 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
1331 {
1332   // see header file for function documentation
1333   ALIHLTCOMPONENT_BASE_STOPWATCH();
1334   fSearchDataType=dt;
1335   fClassName.clear();
1336   int idx=FindInputBlock(fSearchDataType, 0);
1337   const AliHLTComponentBlockData* pBlock=NULL;
1338   if (idx>=0) {
1339     // check for fpInputBlocks pointer done in FindInputBlock
1340     pBlock=&fpInputBlocks[idx];
1341     fCurrentInputBlock=idx;
1342   }
1343   return pBlock;
1344 }
1345
1346 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID, 
1347                                                                     const char* dtOrigin)
1348 {
1349   // see header file for function documentation
1350   ALIHLTCOMPONENT_BASE_STOPWATCH();
1351   AliHLTComponentDataType dt;
1352   SetDataType(dt, dtID, dtOrigin);
1353   return GetFirstInputBlock(dt);
1354 }
1355
1356 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index) const
1357 {
1358   // see header file for function documentation
1359   ALIHLTCOMPONENT_BASE_STOPWATCH();
1360   assert( 0 <= index and index < (int)fCurrentEventData.fBlockCnt );
1361   return &fpInputBlocks[index];
1362 }
1363
1364 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
1365 {
1366   // see header file for function documentation
1367   ALIHLTCOMPONENT_BASE_STOPWATCH();
1368   int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
1369   const AliHLTComponentBlockData* pBlock=NULL;
1370   if (idx>=0) {
1371     // check for fpInputBlocks pointer done in FindInputBlock
1372     pBlock=&fpInputBlocks[idx];
1373     fCurrentInputBlock=idx;
1374   }
1375   return pBlock;
1376 }
1377
1378 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
1379 {
1380   // see header file for function documentation
1381   int iResult=-ENOENT;
1382   if (fpInputBlocks!=NULL) {
1383     if (pBlock) {
1384       if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
1385         iResult=(int)(pBlock-fpInputBlocks);
1386       }
1387     } else {
1388       iResult=-EINVAL;
1389     }
1390   }
1391   return iResult;
1392 }
1393
1394 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
1395 {
1396   // see header file for function documentation
1397   ALIHLTCOMPONENT_BASE_STOPWATCH();
1398   AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
1399   int idx=fCurrentInputBlock;
1400   if (pBlock) {
1401     if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
1402     } else {
1403       HLTError("unknown Block %p", pBlock);
1404     }
1405   }
1406   if (idx>=0) {
1407     // check for fpInputBlocks pointer done in FindInputBlock
1408     iSpec=fpInputBlocks[idx].fSpecification;
1409   }
1410   return iSpec;
1411 }
1412
1413 int AliHLTComponent::PushBack(const TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec, 
1414                               void* pHeader, int headerSize)
1415 {
1416   // see header file for function documentation
1417   ALIHLTCOMPONENT_BASE_STOPWATCH();
1418   int iResult=0;
1419   fLastObjectSize=0;
1420   if (fPushbackPeriod>0) {
1421     // suppress the output
1422     TDatime time;
1423     if (fLastPushBackTime<0 || (int)time.Get()-fLastPushBackTime<fPushbackPeriod) return 0;
1424   }
1425   if (pObject) {
1426     AliHLTMessage msg(kMESS_OBJECT);
1427     msg.SetCompressionLevel(fCompressionLevel);
1428     msg.WriteObject(pObject);
1429     Int_t iMsgLength=msg.Length();
1430     if (iMsgLength>0) {
1431       // Matthias Sep 2008
1432       // NOTE: AliHLTMessage does implement it's own SetLength method
1433       // which is not architecture independent. The original SetLength
1434       // stores the size always in network byte order.
1435       // I'm trying to remember the rational for that, might be that
1436       // it was just some lack of knowledge. Want to change this, but
1437       // has to be done carefullt to be backward compatible.
1438       msg.SetLength(); // sets the length to the first (reserved) word
1439
1440       // does nothing if the level is 0
1441       msg.Compress();
1442
1443       char *mbuf = msg.Buffer();
1444       if (msg.CompBuffer()) {
1445         msg.SetLength(); // set once more to have to byte order
1446         mbuf = msg.CompBuffer();
1447         iMsgLength = msg.CompLength();
1448       }
1449       assert(mbuf!=NULL);
1450       iResult=InsertOutputBlock(mbuf, iMsgLength, dt, spec, pHeader, headerSize);
1451       if (iResult>=0) {
1452         HLTDebug("object %s (%p) size %d compression %d inserted to output", pObject->ClassName(), pObject, iMsgLength, msg.GetCompressionLevel());
1453       }
1454       fLastObjectSize=iMsgLength;
1455     } else {
1456       HLTError("object serialization failed for object %p", pObject);
1457       iResult=-ENOMSG;
1458     }
1459   } else {
1460     iResult=-EINVAL;
1461   }
1462   return iResult;
1463 }
1464
1465 int AliHLTComponent::PushBack(const TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1466                               void* pHeader, int headerSize)
1467 {
1468   // see header file for function documentation
1469   ALIHLTCOMPONENT_BASE_STOPWATCH();
1470   AliHLTComponentDataType dt;
1471   SetDataType(dt, dtID, dtOrigin);
1472   return PushBack(pObject, dt, spec, pHeader, headerSize);
1473 }
1474
1475 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1476                               const void* pHeader, int headerSize)
1477 {
1478   // see header file for function documentation
1479   ALIHLTCOMPONENT_BASE_STOPWATCH();
1480   if (fPushbackPeriod>0) {
1481     // suppress the output
1482     TDatime time;
1483     if (fLastPushBackTime<0 || (int)time.Get()-fLastPushBackTime<fPushbackPeriod) return 0;
1484   }
1485
1486   return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
1487 }
1488
1489 int AliHLTComponent::PushBack(const void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
1490                               const void* pHeader, int headerSize)
1491 {
1492   // see header file for function documentation
1493   ALIHLTCOMPONENT_BASE_STOPWATCH();
1494   AliHLTComponentDataType dt;
1495   SetDataType(dt, dtID, dtOrigin);
1496   return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
1497 }
1498
1499 int AliHLTComponent::InsertOutputBlock(const void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
1500                                        const void* pHeader, int iHeaderSize)
1501 {
1502   // see header file for function documentation
1503   int iResult=0;
1504   int iBlkSize = iBufferSize + iHeaderSize;
1505
1506   if ((pBuffer!=NULL && iBufferSize>0) || (pHeader!=NULL && iHeaderSize>0)) {
1507     if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
1508       AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1509
1510       // copy header if provided but skip if the header is the target location
1511       // in that case it has already been copied
1512       if (pHeader!=NULL && pHeader!=pTgt) {
1513         memcpy(pTgt, pHeader, iHeaderSize);
1514       }
1515
1516       pTgt += (AliHLTUInt8_t) iHeaderSize;
1517
1518       // copy buffer if provided but skip if buffer is the target location
1519       // in that case it has already been copied
1520       if (pBuffer!=NULL && pBuffer!=pTgt) {
1521         memcpy(pTgt, pBuffer, iBufferSize);
1522         
1523         //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer); 
1524         //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
1525       }
1526       //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
1527     } else {
1528       if (fpOutputBuffer) {
1529         HLTError("too little space in output buffer: %d of %d, required %d", fOutputBufferSize-fOutputBufferFilled, fOutputBufferSize, iBlkSize);
1530       } else {
1531         HLTError("output buffer not available");
1532       }
1533       iResult=-ENOSPC;
1534     }
1535   }
1536   if (iResult>=0) {
1537     AliHLTComponentBlockData bd;
1538     FillBlockData( bd );
1539     bd.fOffset        = fOutputBufferFilled;
1540     bd.fSize          = iBlkSize;
1541     bd.fDataType      = dt;
1542     bd.fSpecification = spec;
1543     fOutputBlocks.push_back( bd );
1544     fOutputBufferFilled+=bd.fSize;
1545   }
1546
1547   return iResult;
1548 }
1549
1550 int AliHLTComponent::EstimateObjectSize(const TObject* pObject) const
1551 {
1552   // see header file for function documentation
1553   if (!pObject) return 0;
1554
1555   AliHLTMessage msg(kMESS_OBJECT);
1556   msg.WriteObject(pObject);
1557   return msg.Length();  
1558 }
1559
1560 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
1561                                                     const char* dtOrigin,
1562                                                     AliHLTUInt32_t spec)
1563 {
1564   // see header file for function documentation
1565   ALIHLTCOMPONENT_BASE_STOPWATCH();
1566   AliHLTComponentDataType dt;
1567   SetDataType(dt, dtID, dtOrigin);
1568   return CreateMemoryFile(capacity, dt, spec);
1569 }
1570
1571 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
1572                                                     const AliHLTComponentDataType& dt,
1573                                                     AliHLTUInt32_t spec)
1574 {
1575   // see header file for function documentation
1576   ALIHLTCOMPONENT_BASE_STOPWATCH();
1577   AliHLTMemoryFile* pFile=NULL;
1578   if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
1579     AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
1580     pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
1581     if (pFile) {
1582       unsigned int nofBlocks=fOutputBlocks.size();
1583       if (nofBlocks+1>fMemFiles.size()) {
1584         fMemFiles.resize(nofBlocks+1, NULL);
1585       }
1586       if (nofBlocks<fMemFiles.size()) {
1587         fMemFiles[nofBlocks]=pFile;
1588         AliHLTComponentBlockData bd;
1589         FillBlockData( bd );
1590         bd.fOffset        = fOutputBufferFilled;
1591         bd.fSize          = capacity;
1592         bd.fDataType      = dt;
1593         bd.fSpecification = spec;
1594         fOutputBufferFilled+=bd.fSize;
1595         fOutputBlocks.push_back( bd );
1596       } else {
1597         HLTError("can not allocate/grow object array");
1598         pFile->CloseMemoryFile(0);
1599         delete pFile;
1600         pFile=NULL;
1601       }
1602     }
1603   } else {
1604     HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
1605   }
1606   return pFile;
1607 }
1608
1609 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
1610                                                     const char* dtOrigin,
1611                                                     AliHLTUInt32_t spec,
1612                                                     float capacity)
1613 {
1614   // see header file for function documentation
1615   ALIHLTCOMPONENT_BASE_STOPWATCH();
1616   AliHLTComponentDataType dt;
1617   SetDataType(dt, dtID, dtOrigin);
1618   int size=fOutputBufferSize-fOutputBufferFilled;
1619   if (capacity<0 || capacity>1.0) {
1620     HLTError("invalid parameter: capacity %f", capacity);
1621     return NULL;
1622   }
1623   size=(int)(size*capacity);
1624   return CreateMemoryFile(size, dt, spec);
1625 }
1626
1627 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
1628                                                     AliHLTUInt32_t spec,
1629                                                     float capacity)
1630 {
1631   // see header file for function documentation
1632   ALIHLTCOMPONENT_BASE_STOPWATCH();
1633   int size=fOutputBufferSize-fOutputBufferFilled;
1634   if (capacity<0 || capacity>1.0) {
1635     HLTError("invalid parameter: capacity %f", capacity);
1636     return NULL;
1637   }
1638   size=(int)(size*capacity);
1639   return CreateMemoryFile(size, dt, spec);
1640 }
1641
1642 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
1643                            const char* key, int option)
1644 {
1645   // see header file for function documentation
1646   int iResult=0;
1647   if (pFile && pObject) {
1648     pFile->cd();
1649     iResult=pObject->Write(key, option);
1650     if (iResult>0) {
1651       // success
1652     } else {
1653       iResult=-pFile->GetErrno();
1654       if (iResult==-ENOSPC) {
1655         HLTError("error writing memory file, buffer too small");
1656       }
1657     }
1658   } else {
1659     iResult=-EINVAL;
1660   }
1661   return iResult;
1662 }
1663
1664 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
1665 {
1666   // see header file for function documentation
1667   int iResult=0;
1668   if (pFile) {
1669     AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1670     int i=0;
1671     while (element!=fMemFiles.end() && iResult>=0) {
1672       if (*element && *element==pFile) {
1673         iResult=pFile->CloseMemoryFile();
1674         
1675         // sync memory files and descriptors
1676         if (iResult>=0) {
1677           fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
1678         }
1679         delete *element;
1680         *element=NULL;
1681         return iResult;
1682       }
1683       element++; i++;
1684     }
1685     HLTError("can not find memory file %p", pFile);
1686     iResult=-ENOENT;
1687   } else {
1688     iResult=-EINVAL;
1689   }
1690   return iResult;
1691 }
1692
1693 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData edd)
1694 {
1695   // see header file for function documentation
1696   int iResult=0;
1697
1698   AliHLTComponentEventDoneData* newEDD = NULL;
1699   
1700   unsigned long newSize=edd.fDataSize;
1701   if (fEventDoneData)
1702     newSize += fEventDoneData->fDataSize;
1703
1704   if (newSize>fEventDoneDataSize) {
1705     newEDD = reinterpret_cast<AliHLTComponentEventDoneData*>( new AliHLTUInt8_t[ sizeof(AliHLTComponentEventDoneData)+newSize ] );
1706     if (!newEDD)
1707       return -ENOMEM;
1708     newEDD->fStructSize = sizeof(AliHLTComponentEventDoneData);
1709     newEDD->fDataSize = newSize;
1710     newEDD->fData = reinterpret_cast<AliHLTUInt8_t*>(newEDD)+newEDD->fStructSize;
1711     unsigned long long offset = 0;
1712     if (fEventDoneData) {
1713       memcpy( newEDD->fData, fEventDoneData->fData, fEventDoneData->fDataSize );
1714       offset += fEventDoneData->fDataSize;
1715     }
1716     memcpy( reinterpret_cast<AliHLTUInt8_t*>(newEDD->fData)+offset, edd.fData, edd.fDataSize );
1717     if (fEventDoneData)
1718       delete [] reinterpret_cast<AliHLTUInt8_t*>( fEventDoneData );
1719     fEventDoneData = newEDD;
1720     fEventDoneDataSize = newSize;
1721   }
1722   else if (fEventDoneData) {
1723     memcpy( reinterpret_cast<AliHLTUInt8_t*>(fEventDoneData->fData)+fEventDoneData->fDataSize, edd.fData, edd.fDataSize );
1724     fEventDoneData->fDataSize += edd.fDataSize;
1725   }
1726   else {
1727     HLTError("internal mismatch, fEventDoneData=%d but buffer is NULL", fEventDoneDataSize);
1728     iResult=-EFAULT;
1729   }
1730   return iResult;
1731 }
1732
1733 namespace
1734 {
1735   // helper function for std:sort, implements an operator<
1736   bool SortComponentStatisticsById(const AliHLTComponentStatistics& a, const AliHLTComponentStatistics& b)
1737   {
1738     return a.fId<b.fId;
1739   }
1740
1741   // helper function for std:sort
1742   bool SortComponentStatisticsDescendingByLevel(const AliHLTComponentStatistics& a, const AliHLTComponentStatistics& b)
1743   {
1744     return a.fId>b.fId;
1745   }
1746
1747   // helper class to define operator== between AliHLTComponentStatistics and AliHLTComponentStatistics.fId
1748   class AliHLTComponentStatisticsId {
1749   public:
1750     AliHLTComponentStatisticsId(AliHLTUInt32_t id) : fId(id) {}
1751     AliHLTComponentStatisticsId(const AliHLTComponentStatisticsId& src) : fId(src.fId) {}
1752     AliHLTComponentStatisticsId& operator=(const AliHLTComponentStatisticsId& src) {
1753       if (this==&src) return *this;
1754       fId=src.fId; return *this;
1755     }
1756     bool operator==(const AliHLTComponentStatistics& a) const {return a.fId==fId;}
1757   private:
1758     AliHLTComponentStatisticsId();
1759     AliHLTUInt32_t fId;
1760   };
1761
1762   // operator for std::find of AliHLTComponentStatistics by id
1763   bool operator==(const AliHLTComponentStatistics& a, const AliHLTComponentStatisticsId& b)
1764   {
1765     return b==a;
1766   }
1767
1768   bool AliHLTComponentStatisticsCompareIds(const AliHLTComponentStatistics& a, const AliHLTComponentStatistics& b)
1769   {
1770     return a.fId==b.fId;
1771   }
1772
1773   // helper class to define operator== between AliHLTComponentBlockData and AliHLTComponentBlockData.fSpecification
1774   class AliHLTComponentBlockDataSpecification {
1775   public:
1776     AliHLTComponentBlockDataSpecification(AliHLTUInt32_t specification) : fSpecification(specification) {}
1777     AliHLTComponentBlockDataSpecification(const AliHLTComponentBlockDataSpecification& src) : fSpecification(src.fSpecification) {}
1778     AliHLTComponentBlockDataSpecification& operator=(const AliHLTComponentBlockDataSpecification& src) {
1779       if (this==&src) return *this;
1780       fSpecification=src.fSpecification; return *this;
1781     }
1782     bool operator==(const AliHLTComponentBlockData& bd) const {return bd.fSpecification==fSpecification;}
1783   private:
1784     AliHLTComponentBlockDataSpecification();
1785     AliHLTUInt32_t fSpecification;
1786   };
1787
1788   // operator for std::find of AliHLTComponentBlockData by specification
1789   bool operator==(const AliHLTComponentBlockData& bd, const AliHLTComponentBlockDataSpecification& spec)
1790   {
1791     return spec==bd;
1792   }
1793
1794   // operator for std::find
1795   bool operator==(const AliHLTComponentBlockData& a, const AliHLTComponentBlockData& b)
1796   {
1797     if (!MatchExactly(a.fDataType,b.fDataType)) return false;
1798     return a.fSpecification==b.fSpecification;
1799   }
1800
1801 } // end of namespace
1802
1803 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1804                                    const AliHLTComponentBlockData* blocks, 
1805                                    AliHLTComponentTriggerData& trigData,
1806                                    AliHLTUInt8_t* outputPtr, 
1807                                    AliHLTUInt32_t& size,
1808                                    AliHLTUInt32_t& outputBlockCnt, 
1809                                    AliHLTComponentBlockData*& outputBlocks,
1810                                    AliHLTComponentEventDoneData*& edd )
1811 {
1812   // see header file for function documentation
1813   HLTLogKeyword(fChainId.c_str());
1814   ALIHLTCOMPONENT_BASE_STOPWATCH();
1815   int iResult=0;
1816   fCurrentEvent=evtData.fEventID;
1817   fCurrentEventData=evtData;
1818   fpInputBlocks=blocks;
1819   fCurrentInputBlock=-1;
1820   fSearchDataType=kAliHLTAnyDataType;
1821   fpOutputBuffer=outputPtr;
1822   fOutputBufferSize=size;
1823   fOutputBufferFilled=0;
1824   fOutputBlocks.clear();
1825   outputBlockCnt=0;
1826   outputBlocks=NULL;
1827
1828   AliHLTComponentBlockDataList forwardedBlocks;
1829
1830   // optional component statistics
1831   AliHLTComponentStatisticsList compStats;
1832   bool bAddComponentTableEntry=false;
1833   vector<AliHLTUInt32_t> parentComponentTables;
1834   int processingLevel=-1;
1835 #if defined(HLT_COMPONENT_STATISTICS)
1836   if ((fFlags&kDisableComponentStat)==0) {
1837     AliHLTComponentStatistics outputStat;
1838     memset(&outputStat, 0, sizeof(AliHLTComponentStatistics));
1839     outputStat.fStructSize=sizeof(AliHLTComponentStatistics);
1840     outputStat.fId=fChainIdCrc;
1841     if (fpBenchmark) {
1842       fpBenchmark->Stop();
1843       outputStat.fComponentCycleTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
1844       fpBenchmark->Reset();
1845       fpBenchmark->Start();
1846     }
1847     compStats.push_back(outputStat);
1848   }
1849 #endif // HLT_COMPONENT_STATISTICS
1850
1851   // data processing is skipped
1852   // -  if there are only steering events in the block list.
1853   //    For the sake of data source components data processing
1854   //    is not skipped if there is no block list at all or if it
1855   //    just contains the eventType block
1856   // - always skipped if the event is of type
1857   //   - gkAliEventTypeConfiguration
1858   //   - gkAliEventTypeReadPreprocessor
1859   const unsigned int skipModeDefault=0x1;
1860   const unsigned int skipModeForce=0x2;
1861   unsigned int bSkipDataProcessing=skipModeDefault;
1862
1863   // find special events
1864   if (fpInputBlocks && evtData.fBlockCnt>0) {
1865     // first look for all special events and execute in the appropriate
1866     // sequence afterwords
1867     int indexComConfEvent=-1;
1868     int indexUpdtDCSEvent=-1;
1869     int indexSOREvent=-1;
1870     int indexEOREvent=-1;
1871     int indexECSParamBlock=-1;
1872     for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1873       if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1874         indexSOREvent=i;
1875         // the AliHLTCalibrationProcessor relies on the SOR and EOR events
1876         bSkipDataProcessing&=~skipModeDefault;
1877       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeRunType) {
1878         // run type string
1879         // handling is not clear yet
1880         if (fpInputBlocks[i].fPtr) {
1881           HLTDebug("got run type \"%s\"\n", fpInputBlocks[i].fPtr);
1882         }
1883       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1884         indexEOREvent=i;
1885         // the calibration processor relies on the SOR and EOR events
1886         bSkipDataProcessing&=~skipModeDefault;
1887       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1888         // DDL list
1889         // this event is most likely deprecated
1890       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComConf) {
1891         indexComConfEvent=i;
1892       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeUpdtDCS) {
1893         indexUpdtDCSEvent=i;
1894       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEvent) {
1895         fEventType=fpInputBlocks[i].fSpecification;
1896         if (fEventType != gkAliEventTypeConfiguration and
1897             fEventType != gkAliEventTypeReadPreprocessor
1898            )
1899         {
1900           // We can actually get the event type from the CDH if it is valid.
1901           // Otherwise just use the specification of the input block.
1902           AliHLTCDHWrapper cdh;
1903           if (ExtractTriggerData(trigData, NULL, NULL, &cdh, NULL) == 0)
1904           {
1905             fEventType = ExtractEventTypeFromCDH(&cdh);
1906           }
1907         }
1908
1909         // skip always in case of gkAliEventTypeConfiguration
1910         if (fpInputBlocks[i].fSpecification==gkAliEventTypeConfiguration) bSkipDataProcessing|=skipModeForce;
1911
1912         // skip always in case of gkAliEventTypeReadPreprocessor
1913         if (fpInputBlocks[i].fSpecification==gkAliEventTypeReadPreprocessor) bSkipDataProcessing|=skipModeForce;
1914
1915         // never skip if the event type block is the only block
1916         if (evtData.fBlockCnt==1) bSkipDataProcessing&=~skipModeDefault;
1917
1918       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentStatistics) {
1919         if (compStats.size()>0) {
1920           AliHLTUInt8_t* pData=reinterpret_cast<AliHLTUInt8_t*>(fpInputBlocks[i].fPtr);
1921           for (AliHLTUInt32_t offset=0;
1922                offset+sizeof(AliHLTComponentStatistics)<=fpInputBlocks[i].fSize;
1923                offset+=sizeof(AliHLTComponentStatistics)) {
1924             AliHLTComponentStatistics* pStat=reinterpret_cast<AliHLTComponentStatistics*>(pData+offset);
1925             if (pStat && compStats[0].fLevel<=pStat->fLevel) {
1926               compStats[0].fLevel=pStat->fLevel+1;
1927             }
1928             if (find(compStats.begin(), compStats.end(), AliHLTComponentStatisticsId(pStat->fId))==compStats.end()) {
1929               compStats.push_back(*pStat);
1930             }
1931           }
1932         }
1933       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComponentTable) {
1934         AliHLTComponentBlockDataList::iterator element=forwardedBlocks.begin();
1935         while ((element=find(element, forwardedBlocks.end(), AliHLTComponentBlockDataSpecification(fpInputBlocks[i].fSpecification)))!=forwardedBlocks.end()) {
1936           if (element->fDataType==fpInputBlocks[i].fDataType) break;
1937           // TODO: think about some more checks inclusing also the actual data buffers
1938           // this has to handle multiplicity>1 in the online system, where all components
1939           // send the information on SOR, because this event is broadcasted
1940         }
1941         if (element==forwardedBlocks.end()) {
1942           forwardedBlocks.push_back(fpInputBlocks[i]);
1943         }
1944         parentComponentTables.push_back(fpInputBlocks[i].fSpecification);
1945         if (fpInputBlocks[i].fSize>=sizeof(AliHLTComponentTableEntry)) {
1946           const AliHLTComponentTableEntry* entry=reinterpret_cast<AliHLTComponentTableEntry*>(fpInputBlocks[i].fPtr);
1947           if (entry->fStructSize==sizeof(AliHLTComponentTableEntry)) {
1948             if (processingLevel<0 || processingLevel<=(int)entry->fLevel) 
1949               processingLevel=entry->fLevel+1;
1950           }
1951         }
1952       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeECSParam) {
1953         indexECSParamBlock=i;
1954       } else {
1955         // the processing function is called if there is at least one
1956         // non-steering data block. Steering blocks are not filtered out
1957         // for sake of performance 
1958         bSkipDataProcessing&=~skipModeDefault;
1959         if (compStats.size()>0) {
1960           compStats[0].fInputBlockCount++;
1961           compStats[0].fTotalInputSize+=fpInputBlocks[i].fSize;
1962         }
1963       }
1964     }
1965
1966     if (indexSOREvent>=0) {
1967       // start of run
1968       bAddComponentTableEntry=true;
1969       compStats.clear();   // no component statistics for SOR
1970       if (fpRunDesc==NULL) {
1971         fpRunDesc=new AliHLTRunDesc;
1972         if (fpRunDesc) *fpRunDesc=kAliHLTVoidRunDesc;
1973       }
1974       if (fpRunDesc) {
1975         AliHLTRunDesc rundesc;
1976         if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexSOREvent, "AliHLTRunDesc", "SOR"))>0) {
1977           if (fpRunDesc->fRunNo==kAliHLTVoidRunNo) {
1978             *fpRunDesc=rundesc;
1979             HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1980             SetCDBRunNo(fpRunDesc->fRunNo);
1981           } else if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1982             HLTWarning("already set run properties run no %d, ignoring SOR with run no %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1983           }
1984         }
1985       } else {
1986         iResult=-ENOMEM;
1987       }
1988
1989       if (indexECSParamBlock>=0) {
1990         if (fpInputBlocks[indexECSParamBlock].fSize>0) {
1991           const char* param=reinterpret_cast<const char*>(fpInputBlocks[indexECSParamBlock].fPtr);
1992           TString paramString;
1993           if (param[fpInputBlocks[indexECSParamBlock].fSize-1]!=0) {
1994             HLTWarning("ECS parameter string not terminated");
1995             paramString.Insert(0, param, fpInputBlocks[indexECSParamBlock].fSize);
1996             paramString+="";
1997           } else {
1998             paramString=param;
1999           }
2000           ScanECSParam(paramString.Data());
2001         } else {
2002           HLTWarning("empty ECS parameter received");
2003         }
2004       } else {
2005         // TODO: later on we might throw a warning here since the CTP trigger classes
2006         // should be mandatory
2007       }
2008     }
2009     if (indexEOREvent>=0) {
2010       fLastPushBackTime=0; // always send at EOR
2011       bAddComponentTableEntry=true;
2012       compStats.clear();   // no component statistics for EOR
2013       if (fpRunDesc!=NULL) {
2014         if (fpRunDesc) {
2015           AliHLTRunDesc rundesc;
2016           if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexEOREvent, "AliHLTRunDesc", "SOR"))>0) {
2017             if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
2018               HLTWarning("run no mismatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
2019             } else {
2020               HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
2021             }
2022           }
2023           // we do not unload the fpRunDesc struct here in order to have the run information
2024           // available during the event processing
2025           // https://savannah.cern.ch/bugs/?39711
2026           // the info will be cleared in DeInit
2027         }
2028       } else {
2029         HLTWarning("did not receive SOR, ignoring EOR");
2030       }
2031     }
2032     if (fEventType==gkAliEventTypeConfiguration) {
2033       if (indexComConfEvent>=0) {
2034       TString cdbEntry;
2035       if (indexComConfEvent>=0 && fpInputBlocks[indexComConfEvent].fPtr!=NULL && fpInputBlocks[indexComConfEvent].fSize>0) {
2036         cdbEntry.Append(reinterpret_cast<const char*>(fpInputBlocks[indexComConfEvent].fPtr), fpInputBlocks[indexComConfEvent].fSize);
2037       }
2038       HLTDebug("received component configuration command: entry %s", cdbEntry.IsNull()?"none":cdbEntry.Data());
2039       int tmpResult=Reconfigure(cdbEntry[0]==0?NULL:cdbEntry.Data(), fChainId.c_str());
2040       if (tmpResult<0) {
2041         HLTWarning("reconfiguration of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
2042       }
2043       } else {
2044         ALIHLTERRORGUARD(1, "incomplete Configure event, missing parameter data block");
2045       }
2046     }
2047     if (fEventType==gkAliEventTypeReadPreprocessor) {
2048       if (indexUpdtDCSEvent>=0) {
2049       TString modules;
2050       if (fpInputBlocks[indexUpdtDCSEvent].fPtr!=NULL && fpInputBlocks[indexUpdtDCSEvent].fSize>0) {
2051         modules.Append(reinterpret_cast<const char*>(fpInputBlocks[indexUpdtDCSEvent].fPtr), fpInputBlocks[indexUpdtDCSEvent].fSize);
2052       }
2053       HLTDebug("received preprocessor update command: detectors %s", modules.IsNull()?"ALL":modules.Data());
2054       int tmpResult=ReadPreprocessorValues(modules[0]==0?"ALL":modules.Data());
2055       if (tmpResult<0) {
2056         HLTWarning("preprocessor update of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
2057       }
2058       } else {
2059         ALIHLTERRORGUARD(1, "incomplete ReadPreprocessor event, missing parameter data block");
2060       }
2061     }
2062   } else {
2063     // processing function needs to be called if there are no input data
2064     // blocks in order to make data source components working.
2065     bSkipDataProcessing&=~skipModeDefault;
2066   }
2067
2068   // data processing is not skipped if the component explicitly asks
2069   // for the private blocks
2070   if ((fFlags&kRequireSteeringBlocks)!=0) bSkipDataProcessing=0;
2071
2072   // data processing is not skipped for data sources
2073   if (GetComponentType()==AliHLTComponent::kSource) bSkipDataProcessing=0;
2074
2075   if (fpCTPData) {
2076     // set the active triggers for this event
2077     fpCTPData->SetTriggers(trigData);
2078     // increment CTP trigger counters if available
2079     if (IsDataEvent()) fpCTPData->Increment(trigData);
2080   }
2081   
2082   // Check if the event processing should be skipped because of the
2083   // down scaling from the event modulo argument. Using a prime number
2084   // as pre divisor to pseudo-randomise the event number to get a more
2085   // uniform distribution.
2086   if (fEventModulo > 1)
2087   {
2088     bSkipDataProcessing |= ( ((AliHLTUInt64_t(fCurrentEvent) / AliHLTUInt64_t(4789)) % AliHLTUInt64_t(fEventModulo)) != 0 );
2089   }
2090
2091   AliHLTComponentBlockDataList blockData;
2092   if (iResult>=0 && !bSkipDataProcessing)
2093   { // dont delete, sets the scope for the stopwatch guard
2094     // do not use ALIHLTCOMPONENT_DA_STOPWATCH(); macro
2095     // in order to avoid 'shadowed variable' warning
2096     AliHLTStopwatchGuard swguard2(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)kSWDA)):NULL);
2097     AliHLTMisc::AliOnlineGuard onlineGuard;
2098     iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
2099   } // end of the scope of the stopwatch guard
2100   if (iResult>=0 && !bSkipDataProcessing) {
2101     if (fOutputBlocks.size()>0) {
2102       // High Level interface
2103
2104       //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());      
2105       // sync memory files and descriptors
2106       AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
2107       int i=0;
2108       while (element!=fMemFiles.end() && iResult>=0) {
2109         if (*element) {
2110           if ((*element)->IsClosed()==0) {
2111             HLTWarning("memory file has not been closed, force flush");
2112             iResult=CloseMemoryFile(*element);
2113           }
2114         }
2115         element++; i++;
2116       }
2117
2118       if (iResult>=0) {
2119         // create the descriptor list
2120         if (blockData.size()>0) {
2121           HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
2122           iResult=-EFAULT;
2123         } else {
2124           if (compStats.size()>0 && IsDataEvent()) {
2125             int offset=AddComponentStatistics(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, compStats);
2126             if (offset>0) fOutputBufferFilled+=offset;
2127           }
2128           if (bAddComponentTableEntry) {
2129             int offset=AddComponentTableEntry(fOutputBlocks, fpOutputBuffer, fOutputBufferSize, fOutputBufferFilled, parentComponentTables, processingLevel);
2130             if (offset>0) size+=offset;
2131           }
2132           if (forwardedBlocks.size()>0) {
2133             fOutputBlocks.insert(fOutputBlocks.end(), forwardedBlocks.begin(), forwardedBlocks.end());
2134           }
2135           iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
2136           size=fOutputBufferFilled;
2137         }
2138       }
2139     } else {
2140       // Low Level interface
2141       if (blockData.empty() && size!=0) {
2142         // set the size to zero because there is no announced data
2143         //HLTWarning("no output blocks added by component but output buffer filled %d of %d", size, fOutputBufferSize);
2144         size=0;
2145       }
2146
2147       if (compStats.size()>0) {
2148         int offset=AddComponentStatistics(blockData, fpOutputBuffer, fOutputBufferSize, size, compStats);
2149         if (offset>0) size+=offset;
2150       }
2151       if (bAddComponentTableEntry) {
2152         int offset=AddComponentTableEntry(blockData, fpOutputBuffer, fOutputBufferSize, size, parentComponentTables, processingLevel);
2153         if (offset>0) size+=offset;
2154       }
2155       if (forwardedBlocks.size()>0) {
2156         blockData.insert(blockData.end(), forwardedBlocks.begin(), forwardedBlocks.end());
2157       }
2158       iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
2159     }
2160     if (iResult<0) {
2161       HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
2162     }
2163   }
2164   if (iResult<0 || bSkipDataProcessing) {
2165     outputBlockCnt=0;
2166     outputBlocks=NULL;
2167   }
2168   CleanupInputObjects();
2169   if (iResult>=0 && IsDataEvent()) {
2170     IncrementEventCounter();
2171   }
2172   if (outputBlockCnt==0) {
2173     // no output blocks, set size to 0
2174     size=0;
2175   }
2176
2177   // reset the internal EventData struct
2178   FillEventData(fCurrentEventData);
2179
2180   // reset the active triggers
2181   if (fpCTPData) fpCTPData->SetTriggers(0);
2182
2183   // set the time for the pushback period
2184   if (fPushbackPeriod>0) {
2185     // suppress the output
2186     TDatime time;
2187     if (fLastPushBackTime<0) {
2188       // choose a random offset at beginning to equalize traffic for multiple instances
2189       // of the component
2190       gRandom->SetSeed(fChainIdCrc);
2191       fLastPushBackTime=time.Get();
2192       fLastPushBackTime-=gRandom->Integer(fPushbackPeriod);
2193     } else if ((int)time.Get()-fLastPushBackTime>=fPushbackPeriod) {
2194       fLastPushBackTime=time.Get();
2195     }
2196   }
2197
2198   return iResult;
2199 }
2200
2201 int  AliHLTComponent::AddComponentStatistics(AliHLTComponentBlockDataList& blocks, 
2202                                              AliHLTUInt8_t* buffer,
2203                                              AliHLTUInt32_t bufferSize,
2204                                              AliHLTUInt32_t offset,
2205                                              AliHLTComponentStatisticsList& stats) const
2206 {
2207   // see header file for function documentation
2208   int iResult=0;
2209   if ((fFlags&kDisableComponentStat)!=0) return 0;
2210 #if defined(HLT_COMPONENT_STATISTICS)
2211   if (stats.size()==0) return -ENOENT;
2212   // check if there is space for at least one entry
2213   if (offset+sizeof(AliHLTComponentStatistics)>bufferSize) return 0;
2214   stats[0].fTotalOutputSize=offset;
2215   stats[0].fOutputBlockCount=blocks.size();
2216   if (fpBenchmark) {
2217     fpBenchmark->Stop();
2218     stats[0].fTime=(AliHLTUInt32_t)(fpBenchmark->RealTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
2219     stats[0].fCTime=(AliHLTUInt32_t)(fpBenchmark->CpuTime()*ALIHLTCOMPONENT_STATTIME_SCALER);
2220     fpBenchmark->Continue();
2221   }
2222
2223   sort(stats.begin(), stats.end(), SortComponentStatisticsDescendingByLevel);
2224
2225   // shrink the number of entries if the buffer is too small
2226   if (offset+stats.size()*sizeof(AliHLTComponentStatistics)>bufferSize) {
2227     unsigned originalSize=stats.size();
2228     AliHLTUInt32_t removedLevel=0;
2229     do {
2230       // remove all entries of the level of the last entry
2231       removedLevel=stats.back().fLevel;
2232       AliHLTComponentStatisticsList::iterator element=stats.begin();
2233       element++;
2234       while (element!=stats.end()) {
2235         if (element->fLevel<=removedLevel) {
2236           element=stats.erase(element);
2237         } else {
2238           element++;
2239         }
2240       }
2241     } while (stats.size()>1 && 
2242              (offset+stats.size()*sizeof(AliHLTComponentStatistics)>bufferSize));
2243     HLTWarning("too little space in output buffer to add block of %d statistics entries (size %d), available %d, removed %d entries",
2244                originalSize, sizeof(AliHLTComponentStatistics), bufferSize-offset, originalSize-stats.size());
2245   } else {
2246     HLTDebug("adding block of %d statistics entries", stats.size());
2247   }
2248   assert(stats.size()>0);
2249   if (stats.size()==0) return 0;
2250
2251   if (offset+stats.size()*sizeof(AliHLTComponentStatistics)<=bufferSize) {
2252     AliHLTComponentBlockData bd;
2253     FillBlockData( bd );
2254     bd.fOffset        = offset;
2255     bd.fSize          = stats.size()*sizeof(AliHLTComponentStatistics);
2256     bd.fDataType      = kAliHLTDataTypeComponentStatistics;
2257     bd.fSpecification = fChainIdCrc;
2258     memcpy(buffer+offset, &(stats[0]), bd.fSize);
2259     blocks.push_back(bd);
2260     iResult=bd.fSize;
2261   }
2262 #else
2263   if (blocks.size() && buffer && bufferSize && offset && stats.size()) {
2264     // get rid of warning
2265   }
2266 #endif // HLT_COMPONENT_STATISTICS
2267   return iResult;
2268 }
2269
2270 int  AliHLTComponent::AddComponentTableEntry(AliHLTComponentBlockDataList& blocks, 
2271                                              AliHLTUInt8_t* buffer,
2272                                              AliHLTUInt32_t bufferSize,
2273                                              AliHLTUInt32_t offset,
2274                                              const vector<AliHLTUInt32_t>& parents,
2275                                              int processingLevel) const
2276 {
2277   // see header file for function documentation
2278   int iResult=0;
2279   if ((fFlags&kDisableComponentStat)!=0) return 0;
2280 #if defined(HLT_COMPONENT_STATISTICS)
2281   // the payload consists of the AliHLTComponentTableEntry struct,
2282   // followed by a an array of 32bit crc chain ids and the component
2283   // description string
2284   unsigned int payloadSize=sizeof(AliHLTComponentTableEntry);
2285   payloadSize+=parents.size()*sizeof(AliHLTUInt32_t);
2286
2287   // the component description has the following format:
2288   // chain-id{component-id:arguments}
2289   const char* componentId=const_cast<AliHLTComponent*>(this)->GetComponentID();
2290   unsigned int descriptionSize=fChainId.size()+1;
2291   descriptionSize+=2; // the '{}' around the component id
2292   descriptionSize+=strlen(componentId);
2293   descriptionSize+=1; // the ':' between component id and arguments
2294   descriptionSize+=fComponentArgs.size();
2295
2296   payloadSize+=descriptionSize;
2297   if (buffer && (offset+payloadSize<=bufferSize)) {
2298     AliHLTUInt8_t* pTgt=buffer+offset;
2299     memset(pTgt, 0, payloadSize);
2300
2301     // write entry
2302     AliHLTComponentTableEntry* pEntry=reinterpret_cast<AliHLTComponentTableEntry*>(pTgt);
2303     pEntry->fStructSize=sizeof(AliHLTComponentTableEntry);
2304     pEntry->fLevel=processingLevel>=0?processingLevel:0;
2305     pEntry->fNofParents=parents.size();
2306     pEntry->fSizeDescription=descriptionSize;
2307     pTgt=pEntry->fBuffer;
2308
2309     // write array of parents
2310     if (parents.size()>0) {
2311       unsigned int copy=parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type);
2312       memcpy(pTgt, &parents[0], parents.size()*sizeof(vector<AliHLTUInt32_t>::value_type));
2313       pTgt+=copy;
2314     }
2315
2316     // write component description
2317     memcpy(pTgt, fChainId.c_str(), fChainId.size());
2318     pTgt+=fChainId.size();
2319     *pTgt++='{';
2320     memcpy(pTgt, componentId, strlen(componentId));
2321     pTgt+=strlen(componentId);
2322     *pTgt++=':';
2323     memcpy(pTgt, fComponentArgs.c_str(), fComponentArgs.size());
2324     pTgt+=fComponentArgs.size();
2325     *pTgt++='}';
2326     *pTgt++=0;
2327
2328     AliHLTComponentBlockData bd;
2329     FillBlockData( bd );
2330     bd.fOffset        = offset;
2331     bd.fSize          = payloadSize;
2332     bd.fDataType      = kAliHLTDataTypeComponentTable;
2333     bd.fSpecification = fChainIdCrc;
2334     blocks.push_back(bd);
2335     iResult=bd.fSize;
2336   }
2337 #else
2338   if (blocks.size() && buffer && bufferSize && offset && parents.size() && processingLevel) {
2339     // get rid of warning
2340   }
2341 #endif // HLT_COMPONENT_STATISTICS
2342   return iResult;
2343 }
2344
2345 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
2346   :
2347   fpStopwatch(NULL),
2348   fpPrec(NULL)
2349 {
2350   // standard constructor (not for use)
2351 }
2352
2353 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
2354   :
2355   fpStopwatch(pStopwatch),
2356   fpPrec(NULL)
2357 {
2358   // constructor
2359
2360   // check for already existing guard
2361   if (fgpCurrent) fpPrec=fgpCurrent;
2362   fgpCurrent=this;
2363
2364   // stop the preceeding guard if it controls a different stopwatch
2365   int bStart=1;
2366   if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
2367
2368   // start the stopwatch if the current guard controls a different one
2369   if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
2370 }
2371
2372 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
2373   :
2374   fpStopwatch(NULL),
2375   fpPrec(NULL)
2376 {
2377   //
2378   // copy constructor not for use
2379   //
2380 }
2381
2382 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard& other)
2383 {
2384   //
2385   // assignment operator not for use
2386   //
2387   if (this==&other) return *this;
2388   fpStopwatch=NULL;
2389   fpPrec=NULL;
2390   return *this;
2391 }
2392
2393 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
2394
2395 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
2396 {
2397   // destructor
2398
2399   // resume the preceeding guard if it controls a different stopwatch
2400   int bStop=1;
2401   if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
2402
2403   // stop the stopwatch if the current guard controls a different one
2404   if (fpStopwatch && bStop==1) fpStopwatch->Stop();
2405
2406   // resume to the preceeding guard
2407   fgpCurrent=fpPrec;
2408 }
2409
2410 int AliHLTComponent::AliHLTStopwatchGuard::Hold(const TStopwatch* pSucc)
2411 {
2412   // see header file for function documentation
2413   if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
2414   return fpStopwatch!=pSucc?1:0;
2415 }
2416
2417 int AliHLTComponent::AliHLTStopwatchGuard::Resume(const TStopwatch* pSucc)
2418 {
2419   // see header file for function documentation
2420   if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
2421   return fpStopwatch!=pSucc?1:0;
2422 }
2423
2424 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type) 
2425 {
2426   // see header file for function documentation
2427   int iResult=0;
2428   if (pSW!=NULL && type<kSWTypeCount) {
2429     if (fpStopwatches) {
2430       TObject* pObj=fpStopwatches->At((int)type);
2431       if (pSW==NULL        // explicit reset
2432           || pObj==NULL) { // explicit set
2433         fpStopwatches->AddAt(pSW, (int)type);
2434       } else if (pObj!=pSW) {
2435         HLTWarning("stopwatch %d already set, reset first", (int)type);
2436         iResult=-EBUSY;
2437       }
2438     }
2439   } else {
2440     iResult=-EINVAL;
2441   }
2442   return iResult;
2443 }
2444
2445 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
2446 {
2447   // see header file for function documentation
2448   if (pStopwatches==NULL) return -EINVAL;
2449
2450   int iResult=0;
2451   for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntriesFast(); i++)
2452     SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
2453   return iResult;
2454 }
2455
2456 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
2457 {
2458   // see header file for function documentation
2459
2460   // 2010-02-11 OCDB is now the reliable source for the run number, it is
2461   // initialized either by aliroot or the external interface depending
2462   // on the environment. It turned out that the rundescriptor is not set
2463   // in the aliroot mode, resulting in an invalid run number. However this
2464   // did not cause problems until now. OCDB initialization has been revised
2465   // already in 10/2009. This was a remnant.
2466   // Have to check whether we get rid of the rundescriptor at some point.
2467   if (fpRunDesc==NULL) return AliHLTMisc::Instance().GetCDBRunNo();
2468   if (fpRunDesc->fRunNo!=(unsigned)AliHLTMisc::Instance().GetCDBRunNo()) {
2469     HLTWarning("run number mismatch: ocdb %d   run descriptor %d", AliHLTMisc::Instance().GetCDBRunNo(), fpRunDesc->fRunNo);
2470   }
2471   return fpRunDesc->fRunNo;
2472 }
2473
2474 AliHLTUInt32_t AliHLTComponent::GetRunType() const
2475 {
2476   // see header file for function documentation
2477   if (fpRunDesc==NULL) return kAliHLTVoidRunType;
2478   return fpRunDesc->fRunType;
2479 }
2480
2481
2482 AliHLTUInt32_t    AliHLTComponent::GetTimeStamp() const
2483 {
2484   // see header file for function documentation
2485   if (fCurrentEventData.fEventCreation_s < kMaxUInt ) { 
2486     return  fCurrentEventData.fEventCreation_s;
2487   }
2488   // using the actual UTC if the time stamp was not set by the framework
2489   return static_cast<AliHLTUInt32_t>(time(NULL));
2490 }
2491
2492 AliHLTUInt32_t    AliHLTComponent::GetPeriodNumber() const
2493 {
2494   // see header file for function documentation
2495   return (GetEventId()>>36)&0xfffffff;
2496 }
2497
2498 AliHLTUInt32_t    AliHLTComponent::GetOrbitNumber() const
2499 {
2500   // see header file for function documentation
2501   return (GetEventId()>>12)&0xffffff;
2502 }
2503
2504 AliHLTUInt16_t    AliHLTComponent::GetBunchCrossNumber() const
2505 {
2506   // see header file for function documentation
2507   return GetEventId()&0xfff;
2508 }
2509
2510 bool AliHLTComponent::IsDataEvent(AliHLTUInt32_t* pTgt) const
2511 {
2512   // see header file for function documentation
2513   if (pTgt) *pTgt=fEventType;
2514   return (fEventType==gkAliEventTypeData ||
2515           fEventType==gkAliEventTypeDataReplay);
2516 }
2517
2518 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
2519                                 const char* structname, const char* eventname)
2520 {
2521   // see header file for function documentation
2522   int iResult=0;
2523   if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
2524     if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
2525       AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
2526       if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
2527         AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
2528         if (fpInputBlocks[iBlockNo].fSize!=copy) {
2529           HLTWarning("%s event: mismatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
2530           if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
2531         }
2532         if (copy!=iStructSize) {
2533           HLTWarning("%s event: mismatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
2534           if (copy>iStructSize) {
2535             copy=iStructSize;
2536           } else {
2537             memset(pTgt, 0, iStructSize);
2538           }
2539         }
2540         memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
2541         *pTgt=iStructSize;
2542         iResult=copy;
2543       } else {
2544         HLTWarning("%s event: missing data block", eventname);
2545       }
2546     } else {
2547       iResult=-ENODATA;
2548     }
2549   } else {
2550     HLTError("invalid struct");
2551     iResult=-EINVAL;
2552   }
2553   return iResult;
2554 }
2555
2556 AliHLTUInt32_t AliHLTComponent::CalculateChecksum(const AliHLTUInt8_t* buffer, int size)
2557 {
2558   // see header file for function documentation
2559   AliHLTUInt32_t  remainder = 0; 
2560   const AliHLTUInt8_t crcwidth=(8*sizeof(AliHLTUInt32_t));
2561   const AliHLTUInt32_t topbit=1 << (crcwidth-1);
2562   const AliHLTUInt32_t polynomial=0xD8;  /* 11011 followed by 0's */
2563
2564   // code from
2565   // http://www.netrino.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
2566
2567   /*
2568    * Perform modulo-2 division, a byte at a time.
2569    */
2570   for (int byte = 0; byte < size; ++byte)
2571     {
2572       /*
2573        * Bring the next byte into the remainder.
2574        */
2575       remainder ^= (buffer[byte] << (crcwidth - 8));
2576
2577       /*
2578        * Perform modulo-2 division, a bit at a time.
2579        */
2580       for (uint8_t bit = 8; bit > 0; --bit)
2581         {
2582           /*
2583            * Try to divide the current data bit.
2584            */
2585           if (remainder & topbit)
2586             {
2587               remainder = (remainder << 1) ^ polynomial;
2588             }
2589           else
2590             {
2591               remainder = (remainder << 1);
2592             }
2593         }
2594     }
2595
2596   /*
2597    * The final remainder is the CRC result.
2598    */
2599   return (remainder);
2600 }
2601
2602 int AliHLTComponent::ExtractComponentTableEntry(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size,
2603                                                 string& retChainId, string& retCompId, string& retCompArgs,
2604                                                 vector<AliHLTUInt32_t>& parents, int& level)
2605 {
2606   // see header file for function documentation
2607   retChainId.clear();
2608   retCompId.clear();
2609   retCompArgs.clear();
2610   parents.clear();
2611   level=-1;
2612   if (!pBuffer || size==0) return 0;
2613
2614   const AliHLTComponentTableEntry* pEntry=reinterpret_cast<const AliHLTComponentTableEntry*>(pBuffer);
2615   if (size<8/* the initial size of the structure*/ ||
2616       pEntry==NULL || pEntry->fStructSize<8) return -ENOMSG;
2617
2618   if (pEntry->fStructSize!=sizeof(AliHLTComponentTableEntry)) return -EBADF;
2619   level=pEntry->fLevel;
2620   const AliHLTUInt32_t* pParents=reinterpret_cast<const AliHLTUInt32_t*>(pEntry->fBuffer);
2621   const AliHLTUInt8_t* pEnd=pBuffer+size;
2622
2623   if (pParents+pEntry->fNofParents>=reinterpret_cast<const AliHLTUInt32_t*>(pEnd)) return -ENODEV;
2624   for (unsigned int i=0; i<pEntry->fNofParents; i++, pParents++) {
2625     parents.push_back(*pParents);
2626   }
2627
2628   const char* pDescription=reinterpret_cast<const char*>(pParents);
2629   if (pDescription+pEntry->fSizeDescription>=reinterpret_cast<const char*>(pEnd) ||
2630       *(pDescription+pEntry->fSizeDescription)!=0) {
2631     return -EBADF;
2632   }
2633
2634   TString descriptor=reinterpret_cast<const char*>(pDescription);
2635   TString chainId;
2636   TString compId;
2637   TString compArgs;
2638   TObjArray* pTokens=descriptor.Tokenize("{");
2639   if (pTokens) {
2640     int n=0;
2641     if (pTokens->GetEntriesFast()>n) {
2642       retChainId=pTokens->At(n++)->GetName();
2643     }
2644     if (pTokens->GetEntriesFast()>n) {
2645       compId=pTokens->At(n++)->GetName();
2646     }
2647     delete pTokens;
2648   }
2649   if (!compId.IsNull() && (pTokens=compId.Tokenize(":"))!=NULL) {
2650     int n=0;
2651     if (pTokens->GetEntriesFast()>n) {
2652       compId=pTokens->At(n++)->GetName();
2653     }
2654     if (pTokens->GetEntriesFast()>n) {
2655       compArgs=pTokens->At(n++)->GetName();
2656     }
2657     delete pTokens;
2658   }
2659   compId.ReplaceAll("}", "");
2660   compArgs.ReplaceAll("}", "");
2661
2662   retCompId=compId;
2663   retCompArgs=compArgs;
2664
2665   if (retChainId.size()==0) return -ENODATA;
2666
2667   return 1;
2668 }
2669
2670 int AliHLTComponent::ExtractTriggerData(
2671     const AliHLTComponentTriggerData& trigData,
2672     const AliHLTUInt8_t (**attributes)[gkAliHLTBlockDAttributeCount],
2673     AliHLTUInt64_t* status,
2674     AliHLTCDHWrapper* const cdh,
2675     AliHLTReadoutList* readoutlist,
2676     bool printErrors
2677   )
2678 {
2679   // see header file for function documentation
2680   
2681   // Check that the trigger data structure is the correct size.
2682   if (trigData.fStructSize != sizeof(AliHLTComponentTriggerData))
2683   {
2684     if (printErrors)
2685     {
2686       AliHLTLogging log;
2687       log.LoggingVarargs(kHLTLogError, Class_Name(), FUNCTIONNAME(), __FILE__, __LINE__,
2688           "Invalid trigger structure size: %d but expected %d.", trigData.fStructSize, sizeof(AliHLTComponentTriggerData)
2689         );
2690     }
2691     return -ENOENT;
2692   }
2693   
2694   // Get the size of the AliHLTEventTriggerData structure without the readout list part.
2695   // The way we do this here should also handle memory alignment correctly.
2696   AliHLTEventTriggerData* dummy = NULL;
2697   size_t sizeWithoutReadout = (char*)(&dummy->fReadoutList) - (char*)(dummy);
2698   
2699   // Check that the trigger data pointer points to data of a size we can handle.
2700   // Either it is the size of AliHLTEventTriggerData or the size of the old
2701   // version of AliHLTEventTriggerData using AliHLTEventDDLV0.
2702   if (trigData.fDataSize != sizeof(AliHLTEventTriggerData) and
2703       trigData.fDataSize != sizeWithoutReadout + sizeof(AliHLTEventDDLV0)
2704      )
2705   {
2706     if (printErrors)
2707     {
2708       AliHLTLogging log;
2709       log.LoggingVarargs(kHLTLogError, Class_Name(), FUNCTIONNAME(), __FILE__, __LINE__,
2710           "Invalid trigger data size: %d but expected %d.", trigData.fDataSize, sizeof(AliHLTEventTriggerData)
2711         );
2712     }
2713     return -EBADF;
2714   }
2715   
2716   AliHLTEventTriggerData* evtData = reinterpret_cast<AliHLTEventTriggerData*>(trigData.fData);
2717   assert(evtData != NULL);
2718   
2719   // Check that the CDH has the right number of words.
2720   if (cdh != NULL and evtData->fCommonHeaderWordCnt != gkAliHLTCommonHeaderCount)
2721   {
2722     if (printErrors)
2723     {
2724       AliHLTLogging log;
2725       log.LoggingVarargs(kHLTLogError, Class_Name(), FUNCTIONNAME(), __FILE__, __LINE__,
2726           "Common Data Header (CDH) has wrong number of data words: %d but expected %d",
2727           evtData->fCommonHeaderWordCnt, gkAliHLTCommonHeaderCount
2728         );
2729     }
2730     return -EBADMSG;
2731   }
2732   
2733   // Check that the readout list has the correct count of words. i.e. something we can handle,
2734   if (readoutlist != NULL and
2735       evtData->fReadoutList.fCount != (unsigned)gkAliHLTDDLListSizeV0 and
2736       evtData->fReadoutList.fCount != (unsigned)gkAliHLTDDLListSizeV1
2737      )
2738   {
2739     if (printErrors)
2740     {
2741       AliHLTLogging log;
2742       log.LoggingVarargs(kHLTLogError, Class_Name(), FUNCTIONNAME(), __FILE__, __LINE__,
2743           "Readout list structure has wrong number of data words: %d but expected %d",
2744           evtData->fReadoutList.fCount, gkAliHLTDDLListSize
2745         );
2746     }
2747     return -EPROTO;
2748   }
2749   
2750   if (attributes != NULL)
2751   {
2752     *attributes = &evtData->fAttributes;
2753   }
2754   if (status != NULL)
2755   {
2756     *status = evtData->fHLTStatus;
2757   }
2758   if (cdh != NULL)
2759   {
2760     *cdh = &(evtData->fCommonHeader[0]);
2761   }
2762   if (readoutlist != NULL)
2763   {
2764     *readoutlist = AliHLTReadoutList(evtData->fReadoutList);
2765   }
2766   return 0;
2767 }
2768
2769 AliHLTUInt32_t AliHLTComponent::ExtractEventTypeFromCDH(const AliHLTCDHWrapper* const cdh)
2770 {
2771   // see header file for function documentation
2772   UChar_t l1msg = cdh->GetL1TriggerMessage();
2773   if ((l1msg & 0x1) == 0x0) return gkAliEventTypeData;
2774   // The L2SwC bit must be one if we got here, i.e. l1msg & 0x1 == 0x1.
2775   if (((l1msg >> 2) & 0xF) == 0xE) return gkAliEventTypeStartOfRun;
2776   if (((l1msg >> 2) & 0xF) == 0xF) return gkAliEventTypeEndOfRun;
2777   // Check the C1T bit to see if this is a calibration event,
2778   // if not then it must be some other software trigger event.
2779   if (((l1msg >> 6) & 0x1) == 0x1) return gkAliEventTypeCalibration;
2780   return gkAliEventTypeSoftware;
2781 }
2782
2783 int AliHLTComponent::LoggingVarargs(AliHLTComponentLogSeverity severity, 
2784                                     const char* originClass, const char* originFunc,
2785                                     const char* file, int line, ... ) const
2786 {
2787   // see header file for function documentation
2788   int iResult=0;
2789
2790   va_list args;
2791   va_start(args, line);
2792
2793   // logging function needs to be const in order to be called from const member functions
2794   // without problems. But at this point we face the problem with virtual members which
2795   // are not necessarily const.
2796   AliHLTComponent* nonconst=const_cast<AliHLTComponent*>(this);
2797   AliHLTLogging::SetLogString(this, ", %p", "%s (%s_pfmt_): ", 
2798                               fChainId[0]!=0?fChainId.c_str():nonconst->GetComponentID(),
2799                               nonconst->GetComponentID());
2800   iResult=SendMessage(severity, originClass, originFunc, file, line, AliHLTLogging::BuildLogString(NULL, args, true /*append*/));
2801   va_end(args);
2802
2803   return iResult;
2804 }
2805
2806 TUUID AliHLTComponent::GenerateGUID()
2807 {
2808   // Generates a globally unique identifier.
2809
2810   // Start by creating a new UUID. We cannot use the one automatically generated
2811   // by ROOT because the algorithm used will not guarantee unique IDs when generating
2812   // these UUIDs at a high rate in parallel.
2813   TUUID uuid;
2814   // We then use the generated UUID to form part of the random number seeds which
2815   // will be used to generate a proper random UUID. For good measure we use a MD5
2816   // hash also. Note that we want to use the TUUID class because it will combine the
2817   // host address information into the UUID. Using gSystem->GetHostByName() apparently
2818   // can cause problems on Windows machines with a firewall, because it always tries
2819   // to contact a DNS. The TUUID class handles this case appropriately.
2820   union
2821   {
2822     UChar_t buf[16];
2823     UShort_t word[8];
2824     UInt_t dword[4];
2825   };
2826   uuid.GetUUID(buf);
2827   TMD5 md5;
2828   md5.Update(buf, sizeof(buf));
2829   TMD5 md52 = md5;
2830   md5.Final(buf);
2831   dword[0] += gSystem->GetUid();
2832   dword[1] += gSystem->GetGid();
2833   dword[2] += gSystem->GetPid();
2834   for (int i = 0; i < 4; ++i)
2835   {
2836     gRandom->SetSeed(dword[i]);
2837     dword[i] = gRandom->Integer(0xFFFFFFFF);
2838   }
2839   md52.Update(buf, sizeof(buf));
2840   md52.Final(buf);
2841   // To keep to the standard we need to set the version and reserved bits.
2842   word[3] = (word[3] & 0x0FFF) | 0x4000;
2843   buf[8] = (buf[8] & 0x3F) | 0x80;
2844
2845   // Create the name of the new class and file.
2846   char uuidstr[64];
2847   sprintf(uuidstr, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2848     dword[0], word[2], word[3], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]
2849   );
2850
2851   uuid.SetUUID(uuidstr);
2852   return uuid;
2853 }
2854
2855 int AliHLTComponent::ScanECSParam(const char* ecsParam)
2856 {
2857   // see header file for function documentation
2858
2859   // format of the parameter string from ECS
2860   // <command>;<parameterkey>=<parametervalue>;<parameterkey>=<parametervalue>;...
2861   // search for a subset of the parameterkeys
2862   //   RUN_TYPE=
2863   //   RUN_NUMBER=
2864   //   HLT_IN_DDL_LIST=
2865   //   CTP_TRIGGER_CLASS=
2866   //   DATA_FORMAT_VERSION=
2867   //   BEAM_TYPE=
2868   //   HLT_OUT_DDL_LIST=
2869   //   HLT_TRIGGER_CODE=
2870   //   DETECTOR_LIST=
2871   //   HLT_MODE=
2872   // The command apears not to be sent by the online framework
2873   int iResult=0;
2874   TString string=ecsParam;
2875   TObjArray* parameter=string.Tokenize(";");
2876   if (parameter) {
2877     for (int i=0; i<parameter->GetEntriesFast(); i++) {
2878       TString entry=parameter->At(i)->GetName();
2879       HLTDebug("scanning ECS entry: %s", entry.Data());
2880       TObjArray* entryParams=entry.Tokenize("=");
2881       if (entryParams) {
2882         if (entryParams->GetEntriesFast()>1) {
2883           if ((((TObjString*)entryParams->At(0))->GetString()).CompareTo("CTP_TRIGGER_CLASS")==0) {
2884             int result=InitCTPTriggerClasses(entryParams->At(1)->GetName());
2885             if (iResult>=0 && result<0) iResult=result;
2886           } else {
2887             // TODO: scan the other parameters
2888             // e.g. consistency check of run number
2889           }
2890         }
2891         delete entryParams;
2892       }
2893     }
2894     delete parameter;
2895   }
2896
2897   return iResult;
2898 }
2899
2900 int AliHLTComponent::SetupCTPData()
2901 {
2902   // see header file for function documentation
2903   if (fpCTPData) delete fpCTPData;
2904   fpCTPData=new AliHLTCTPData;
2905   if (!fpCTPData) return -ENOMEM;
2906   return 0;
2907 }
2908
2909 int AliHLTComponent::InitCTPTriggerClasses(const char* ctpString)
2910 {
2911   // see header file for function documentation
2912   if (!fpCTPData) return 0; // silently accept as the component has to announce that it want's the CTP info
2913   return fpCTPData->InitCTPTriggerClasses(ctpString);
2914 }
2915
2916 bool AliHLTComponent::EvaluateCTPTriggerClass(const char* expression, AliHLTComponentTriggerData& trigData) const
2917 {
2918   // see header file for function documentation
2919   if (!fpCTPData) {
2920     static bool bWarningThrown=false;
2921     if (!bWarningThrown) HLTError("Trigger classes not initialized, use SetupCTPData from DoInit()");
2922     bWarningThrown=true;
2923     return false;
2924   }
2925
2926   return fpCTPData->EvaluateCTPTriggerClass(expression, trigData);
2927 }
2928
2929 int AliHLTComponent::CheckCTPTrigger(const char* name) const
2930 {
2931   // see header file for function documentation
2932   if (!fpCTPData) {
2933     static bool bWarningThrown=false;
2934     if (!bWarningThrown) HLTError("Trigger classes not initialized, use SetupCTPData from DoInit()");
2935     bWarningThrown=true;
2936     return false;
2937   }
2938
2939   return fpCTPData->CheckTrigger(name);
2940 }
2941
2942 Double_t AliHLTComponent::GetBz()
2943 {
2944   // Returns Bz.
2945   return AliHLTMisc::Instance().GetBz();
2946 }
2947
2948 Double_t AliHLTComponent::GetBz(const Double_t *r)
2949 {
2950   // Returns Bz (kG) at the point "r" .
2951   return AliHLTMisc::Instance().GetBz(r);
2952 }
2953
2954 void AliHLTComponent::GetBxByBz(const Double_t r[3], Double_t b[3])
2955 {
2956   // Returns Bx, By and Bz (kG) at the point "r" .
2957   AliHLTMisc::Instance().GetBxByBz(r, b);
2958 }