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