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