]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTComponent.cxx
added data type 'UPDT_DCS:PRIV' for triggering of component update from Preprocessor...
[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 // see header file for class documentation
26 // or
27 // refer to README to build package
28 // or
29 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
30
31 #if __GNUC__>= 3
32 using namespace std;
33 #endif
34
35 //#include "AliHLTStdIncludes.h"
36 #include "AliHLTComponent.h"
37 #include "AliHLTComponentHandler.h"
38 #include "AliHLTMessage.h"
39 #include "TString.h"
40 #include "TMath.h"
41 #include "TObjArray.h"
42 #include "TObjectTable.h"
43 #include "TClass.h"
44 #include "TStopwatch.h"
45 #include "AliHLTMemoryFile.h"
46 #include "AliHLTMisc.h"
47 #include <cassert>
48
49 /** ROOT macro for the implementation of ROOT specific class methods */
50 ClassImp(AliHLTComponent);
51
52 /** stopwatch macro using the stopwatch guard */
53 #define ALIHLTCOMPONENT_STOPWATCH(type) AliHLTStopwatchGuard swguard(fpStopwatches!=NULL?reinterpret_cast<TStopwatch*>(fpStopwatches->At((int)type)):NULL)
54 //#define ALIHLTCOMPONENT_STOPWATCH(type) 
55
56 /** stopwatch macro for operations of the base class */
57 #define ALIHLTCOMPONENT_BASE_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWBase)
58 /** stopwatch macro for operations of the detector algorithm (DA) */
59 #define ALIHLTCOMPONENT_DA_STOPWATCH() ALIHLTCOMPONENT_STOPWATCH(kSWDA)
60
61 AliHLTComponent::AliHLTComponent()
62   :
63   fEnvironment(),
64   fCurrentEvent(0),
65   fEventCount(-1),
66   fFailedEvents(0),
67   fCurrentEventData(),
68   fpInputBlocks(NULL),
69   fCurrentInputBlock(-1),
70   fSearchDataType(kAliHLTVoidDataType),
71   fClassName(),
72   fpInputObjects(NULL),
73   fpOutputBuffer(NULL),
74   fOutputBufferSize(0),
75   fOutputBufferFilled(0),
76   fOutputBlocks(),
77   fpStopwatches(new TObjArray(kSWTypeCount)),
78   fMemFiles(),
79   fpRunDesc(NULL),
80   fpDDLList(NULL),
81   fCDBSetRunNoFunc(false),
82   fChainId()
83 {
84   // see header file for class documentation
85   // or
86   // refer to README to build package
87   // or
88   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
89   memset(&fEnvironment, 0, sizeof(AliHLTComponentEnvironment));
90   if (fgpComponentHandler)
91     fgpComponentHandler->ScheduleRegister(this);
92   //SetLocalLoggingLevel(kHLTLogDefault);
93 }
94
95 AliHLTComponent::~AliHLTComponent()
96 {
97   // see header file for function documentation
98   CleanupInputObjects();
99   if (fpStopwatches!=NULL) delete fpStopwatches;
100   fpStopwatches=NULL;
101   AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
102   while (element!=fMemFiles.end()) {
103     if (*element) {
104       if ((*element)->IsClosed()==0) {
105         HLTWarning("memory file has not been closed, possible data loss or incomplete buffer");
106         // close but do not flush as we dont know whether the buffer is still valid
107         (*element)->CloseMemoryFile(0);
108       }
109       delete *element;
110       *element=NULL;
111     }
112     element++;
113   }
114 }
115
116 AliHLTComponentHandler* AliHLTComponent::fgpComponentHandler=NULL;
117
118 int AliHLTComponent::SetGlobalComponentHandler(AliHLTComponentHandler* pCH, int bOverwrite) 
119 {
120   // see header file for function documentation
121   int iResult=0;
122   if (fgpComponentHandler==NULL || bOverwrite!=0)
123     fgpComponentHandler=pCH;
124   else
125     iResult=-EPERM;
126   return iResult;
127 }
128
129 int AliHLTComponent::UnsetGlobalComponentHandler() 
130 {
131   // see header file for function documentation
132   return SetGlobalComponentHandler(NULL,1);
133 }
134
135 int AliHLTComponent::Init( AliHLTComponentEnvironment* environ, void* environParam, int argc, const char** argv )
136 {
137   // see header file for function documentation
138   int iResult=0;
139   if (environ) {
140     memcpy(&fEnvironment, environ, sizeof(AliHLTComponentEnvironment));
141     fEnvironment.fParam=environParam;
142   }
143   const char** pArguments=NULL;
144   int iNofChildArgs=0;
145   TString argument="";
146   int bMissingParam=0;
147   if (argc>0) {
148     pArguments=new const char*[argc];
149     if (pArguments) {
150       for (int i=0; i<argc && iResult>=0; i++) {
151         argument=argv[i];
152         if (argument.IsNull()) continue;
153
154         // benchmark
155         if (argument.CompareTo("benchmark")==0) {
156
157           // loglevel
158         } else if (argument.CompareTo("loglevel")==0) {
159           if ((bMissingParam=(++i>=argc))) break;
160           TString parameter(argv[i]);
161           parameter.Remove(TString::kLeading, ' '); // remove all blanks
162           if (parameter.BeginsWith("0x") &&
163               parameter.Replace(0,2,"",0).IsHex()) {
164             AliHLTComponentLogSeverity loglevel=kHLTLogNone;
165             sscanf(parameter.Data(),"%x", (unsigned int*)&loglevel);
166             SetLocalLoggingLevel(loglevel);
167           } else {
168             HLTError("wrong parameter for argument %s, hex number expected", argument.Data());
169             iResult=-EINVAL;
170           }
171         } else {
172           pArguments[iNofChildArgs++]=argv[i];
173         }
174       }
175     } else {
176       iResult=-ENOMEM;
177     }
178   }
179   if (bMissingParam) {
180     HLTError("missing parameter for argument %s", argument.Data());
181     iResult=-EINVAL;
182   }
183   if (iResult>=0) {
184     iResult=DoInit(iNofChildArgs, pArguments);
185   }
186   if (iResult>=0) fEventCount=0;
187   if (pArguments) delete [] pArguments;
188   return iResult;
189 }
190
191 int AliHLTComponent::Deinit()
192 {
193   // see header file for function documentation
194   int iResult=0;
195   iResult=DoDeinit();
196   if (fpRunDesc) {
197     HLTWarning("did not receive EOR for run %d", fpRunDesc->fRunNo);
198     AliHLTRunDesc* pRunDesc=fpRunDesc;
199     fpRunDesc=NULL;
200     delete pRunDesc;
201   }
202   fEventCount=0;
203   return iResult;
204 }
205
206 int AliHLTComponent::InitCDB(const char* cdbPath, AliHLTComponentHandler* pHandler)
207 {
208   // see header file for function documentation
209   int iResult=0;
210   if (cdbPath, pHandler) {
211   // I have to think about separating the library handling from the
212   // component handler. Requiring the component hanlder here is not
213   // the cleanest solution.
214   // We presume the library already to be loaded
215   // find the symbol
216   AliHLTMiscInitCDB_t pFunc=(AliHLTMiscInitCDB_t)pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_INIT_CDB);
217   if (pFunc) {
218     TString path=cdbPath;
219     // very temporary fix, have to check for other formats
220     if (!path.BeginsWith("local://")) {
221       path="local://";
222       path+=cdbPath;
223     }
224     if ((iResult=(*pFunc)(path.Data()))>=0) {
225       if (!(fCDBSetRunNoFunc=pHandler->FindSymbol(ALIHLTMISC_LIBRARY, ALIHLTMISC_SET_CDB_RUNNO))) {
226         Message(NULL, kHLTLogWarning, "AliHLTComponent::InitCDB", "init CDB",
227                 "can not find function to set CDB run no");
228       }
229     }
230   } else {
231     Message(NULL, kHLTLogError, "AliHLTComponent::InitCDB", "init CDB",
232             "can not find initialization function");
233     iResult=-ENOSYS;
234   }
235   } else {
236     iResult=-EINVAL;
237   }
238   return iResult;
239 }
240
241 int AliHLTComponent::SetCDBRunNo(int runNo)
242 {
243   // see header file for function documentation
244   if (!fCDBSetRunNoFunc) return 0;
245   return (*((AliHLTMiscSetCDBRunNo_t)fCDBSetRunNoFunc))(runNo);
246 }
247
248 int AliHLTComponent::DoInit( int /*argc*/, const char** /*argv*/)
249 {
250   // default implementation, childs can overload
251   HLTLogKeyword("dummy");
252   return 0;
253 }
254
255 int AliHLTComponent::DoDeinit()
256 {
257   // default implementation, childs can overload
258   HLTLogKeyword("dummy");
259   return 0;
260 }
261
262 int AliHLTComponent::Reconfigure(const char* /*cdbEntry*/, const char* /*chainId*/)
263 {
264   // default implementation, childs can overload
265   HLTLogKeyword("dummy");
266   return 0;
267 }
268
269 int AliHLTComponent::ReadPreprocessorValues(const char* /*modules*/)
270 {
271   // default implementation, childs can overload
272   HLTLogKeyword("dummy");
273   return 0;
274 }
275
276 int AliHLTComponent::GetOutputDataTypes(AliHLTComponentDataTypeList& /*tgtList*/)
277 {
278   // default implementation, childs can overload
279   HLTLogKeyword("dummy");
280   return 0;
281 }
282
283 void AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, char output[kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2] ) const
284 {
285   // see header file for function documentation
286   memset( output, 0, kAliHLTComponentDataTypefIDsize+kAliHLTComponentDataTypefOriginSize+2 );
287   strncat( output, type.fOrigin, kAliHLTComponentDataTypefOriginSize );
288   strcat( output, ":" );
289   strncat( output, type.fID, kAliHLTComponentDataTypefIDsize );
290 }
291
292 string AliHLTComponent::DataType2Text( const AliHLTComponentDataType& type, int mode)
293 {
294   // see header file for function documentation
295   string out("");
296
297   if (mode==2) {
298     int i=0;
299     char tmp[8];
300     for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
301       sprintf(tmp, "'%d", type.fOrigin[i]);
302       out+=tmp;
303     }
304     out+="':'";
305     for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
306       sprintf(tmp, "%d'", type.fID[i]);
307       out+=tmp;
308     }
309     return out;
310   }
311
312   if (mode==1) {
313     int i=0;
314     char tmp[8];
315     for (i=0; i<kAliHLTComponentDataTypefOriginSize; i++) {
316       unsigned char* puc=(unsigned char*)type.fOrigin;
317       if ((puc[i])<32)
318         sprintf(tmp, "'\\%x", type.fOrigin[i]);
319       else
320         sprintf(tmp, "'%c", type.fOrigin[i]);
321       out+=tmp;
322     }
323     out+="':'";
324     for (i=0; i<kAliHLTComponentDataTypefIDsize; i++) {
325       unsigned char* puc=(unsigned char*)type.fID;
326       if (puc[i]<32)
327         sprintf(tmp, "\\%x'", type.fID[i]);
328       else
329         sprintf(tmp, "%c'", type.fID[i]);
330       out+=tmp;
331     }
332     return out;
333   }
334
335   if (type==kAliHLTVoidDataType) {
336     out="VOID:VOID";
337   } else {
338     // some gymnastics in order to avoid a '0' which is part of either or both
339     // ID and origin terminating the whole string. Unfortunately, string doesn't
340     // stop appending at the '0' if the number of elements to append was 
341     // explicitely specified
342     string tmp("");
343     tmp.append(type.fOrigin, kAliHLTComponentDataTypefOriginSize);
344     out.append(tmp.c_str());
345     out.append(":");
346     tmp="";
347     tmp.append(type.fID, kAliHLTComponentDataTypefIDsize);
348     out.append(tmp.c_str());
349   }
350   return out;
351 }
352
353
354 void* AliHLTComponent::AllocMemory( unsigned long size ) 
355 {
356   // see header file for function documentation
357   if (fEnvironment.fAllocMemoryFunc)
358     return (*fEnvironment.fAllocMemoryFunc)(fEnvironment.fParam, size );
359   HLTFatal("no memory allocation handler registered");
360   return NULL;
361 }
362
363 int AliHLTComponent::MakeOutputDataBlockList( const AliHLTComponentBlockDataList& blocks, AliHLTUInt32_t* blockCount,
364                                               AliHLTComponentBlockData** outputBlocks ) 
365 {
366   // see header file for function documentation
367     if ( blockCount==NULL || outputBlocks==NULL )
368         return -EFAULT;
369     AliHLTUInt32_t count = blocks.size();
370     if ( !count )
371         {
372         *blockCount = 0;
373         *outputBlocks = NULL;
374         return 0;
375         }
376     *outputBlocks = reinterpret_cast<AliHLTComponentBlockData*>( AllocMemory( sizeof(AliHLTComponentBlockData)*count ) );
377     if ( !*outputBlocks )
378         return -ENOMEM;
379     for ( unsigned long i = 0; i < count; i++ ) {
380         (*outputBlocks)[i] = blocks[i];
381         if (MatchExactly(blocks[i].fDataType, kAliHLTAnyDataType)) {
382           (*outputBlocks)[i].fDataType=GetOutputDataType();
383           /* data type was set to the output data type by the PubSub AliRoot
384              Wrapper component, if data type of the block was ********:****.
385              Now handled by the component base class in order to have same
386              behavior when running embedded in AliRoot
387           memset((*outputBlocks)[i].fDataType.fID, '*', kAliHLTComponentDataTypefIDsize);
388           memset((*outputBlocks)[i].fDataType.fOrigin, '*', kAliHLTComponentDataTypefOriginSize);
389           */
390         }
391     }
392     *blockCount = count;
393     return 0;
394
395 }
396
397 int AliHLTComponent::GetEventDoneData( unsigned long size, AliHLTComponentEventDoneData** edd ) 
398 {
399   // see header file for function documentation
400   if (fEnvironment.fGetEventDoneDataFunc)
401     return (*fEnvironment.fGetEventDoneDataFunc)(fEnvironment.fParam, fCurrentEvent, size, edd );
402   return -ENOSYS;
403 }
404
405 int AliHLTComponent::FindMatchingDataTypes(AliHLTComponent* pConsumer, AliHLTComponentDataTypeList* tgtList) 
406 {
407   // see header file for function documentation
408   int iResult=0;
409   if (pConsumer) {
410     AliHLTComponentDataTypeList itypes;
411     AliHLTComponentDataTypeList otypes;
412     otypes.push_back(GetOutputDataType());
413     if (otypes[0]==kAliHLTMultipleDataType) {
414       otypes.clear();
415       int count=0;
416       if ((count=GetOutputDataTypes(otypes))>0) {
417       } else if (GetComponentType()!=kSink) {
418         HLTWarning("component %s indicates multiple output data types but GetOutputDataTypes returns %d", GetComponentID(), count);
419       }
420     }
421     ((AliHLTComponent*)pConsumer)->GetInputDataTypes(itypes);
422     AliHLTComponentDataTypeList::iterator itype=itypes.begin();
423     while (itype!=itypes.end()) {
424       //PrintDataTypeContent((*itype), "consumer \'%s\'");
425       AliHLTComponentDataTypeList::iterator otype=otypes.begin();
426       while (otype!=otypes.end() && (*itype)!=(*otype)) otype++;
427       //if (otype!=otypes.end()) PrintDataTypeContent(*otype, "publisher \'%s\'");
428       if (otype!=otypes.end()) {
429         if (tgtList) tgtList->push_back(*itype);
430         iResult++;
431       }
432       itype++;
433     }
434   } else {
435     iResult=-EINVAL;
436   }
437   return iResult;
438 }
439
440 void AliHLTComponent::PrintDataTypeContent(AliHLTComponentDataType& dt, const char* format)
441 {
442   // see header file for function documentation
443   const char* fmt="\'%s\'";
444   if (format) fmt=format;
445   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, Form(fmt, (DataType2Text(dt)).c_str()));
446   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, 
447                          Form("%x %x %x %x %x %x %x %x : %x %x %x %x", 
448                               dt.fID[0],
449                               dt.fID[1],
450                               dt.fID[2],
451                               dt.fID[3],
452                               dt.fID[4],
453                               dt.fID[5],
454                               dt.fID[6],
455                               dt.fID[7],
456                               dt.fOrigin[0],
457                               dt.fOrigin[1],
458                               dt.fOrigin[2],
459                               dt.fOrigin[3]));
460 }
461
462 void AliHLTComponent::FillBlockData( AliHLTComponentBlockData& blockData )
463 {
464   // see header file for function documentation
465   blockData.fStructSize = sizeof(blockData);
466   FillShmData( blockData.fShmKey );
467   blockData.fOffset = ~(AliHLTUInt32_t)0;
468   blockData.fPtr = NULL;
469   blockData.fSize = 0;
470   FillDataType( blockData.fDataType );
471   blockData.fSpecification = kAliHLTVoidDataSpec;
472 }
473
474 void AliHLTComponent::FillShmData( AliHLTComponentShmData& shmData )
475 {
476   // see header file for function documentation
477   shmData.fStructSize = sizeof(shmData);
478   shmData.fShmType = gkAliHLTComponentInvalidShmType;
479   shmData.fShmID = gkAliHLTComponentInvalidShmID;
480 }
481
482 void AliHLTComponent::FillDataType( AliHLTComponentDataType& dataType )
483 {
484   // see header file for function documentation
485   dataType=kAliHLTAnyDataType;
486 }
487
488 void AliHLTComponent::CopyDataType(AliHLTComponentDataType& tgtdt, const AliHLTComponentDataType& srcdt) 
489 {
490   // see header file for function documentation
491   memcpy(&tgtdt.fID[0], &srcdt.fID[0], kAliHLTComponentDataTypefIDsize);
492   memcpy(&tgtdt.fOrigin[0], &srcdt.fOrigin[0], kAliHLTComponentDataTypefOriginSize);
493 }
494
495 void AliHLTComponent::SetDataType(AliHLTComponentDataType& tgtdt, const char* id, const char* origin) 
496 {
497   // see header file for function documentation
498   tgtdt.fStructSize=sizeof(AliHLTComponentDataType);
499   if (id) {
500     memset(&tgtdt.fID[0], 0, kAliHLTComponentDataTypefIDsize);
501     strncpy(&tgtdt.fID[0], id, strlen(id)<(size_t)kAliHLTComponentDataTypefIDsize?strlen(id):kAliHLTComponentDataTypefIDsize);
502   }
503   if (origin) {
504     memset(&tgtdt.fOrigin[0], 0, kAliHLTComponentDataTypefOriginSize);
505     strncpy(&tgtdt.fOrigin[0], origin, strlen(origin)<(size_t)kAliHLTComponentDataTypefOriginSize?strlen(origin):kAliHLTComponentDataTypefOriginSize);
506   }
507 }
508
509 void AliHLTComponent::SetDataType(AliHLTComponentDataType& dt, AliHLTUInt64_t id, AliHLTUInt32_t origin)
510 {
511   // see header file for function documentation
512   dt.fStructSize=sizeof(AliHLTComponentDataType);
513   assert(kAliHLTComponentDataTypefIDsize==sizeof(id));
514   assert(kAliHLTComponentDataTypefOriginSize==sizeof(origin));
515   memcpy(&dt.fID, &id, kAliHLTComponentDataTypefIDsize);
516   memcpy(&dt.fOrigin, &origin, kAliHLTComponentDataTypefOriginSize);
517 }
518
519 void AliHLTComponent::FillEventData(AliHLTComponentEventData& evtData)
520 {
521   // see header file for function documentation
522   memset(&evtData, 0, sizeof(AliHLTComponentEventData));
523   evtData.fStructSize=sizeof(AliHLTComponentEventData);
524 }
525
526 void AliHLTComponent::PrintComponentDataTypeInfo(const AliHLTComponentDataType& dt) 
527 {
528   // see header file for function documentation
529   TString msg;
530   msg.Form("AliHLTComponentDataType(%d): ID=\"", dt.fStructSize);
531   for ( int i = 0; i < kAliHLTComponentDataTypefIDsize; i++ ) {
532    if (dt.fID[i]!=0) msg+=dt.fID[i];
533    else msg+="\\0";
534   }
535   msg+="\" Origin=\"";
536   for ( int i = 0; i < kAliHLTComponentDataTypefOriginSize; i++ ) {
537    if (dt.fOrigin[i]!=0) msg+=dt.fOrigin[i];
538    else msg+="\\0";
539   }
540   msg+="\"";
541   AliHLTLogging::Message(NULL, kHLTLogNone, NULL , NULL, msg.Data());
542 }
543
544 int AliHLTComponent::GetEventCount() const
545 {
546   // see header file for function documentation
547   return fEventCount;
548 }
549
550 int AliHLTComponent::IncrementEventCounter()
551 {
552   // see header file for function documentation
553   if (fEventCount>=0) fEventCount++;
554   return fEventCount;
555 }
556
557 int AliHLTComponent::GetNumberOfInputBlocks() const
558 {
559   // see header file for function documentation
560   if (fpInputBlocks!=NULL) {
561     return fCurrentEventData.fBlockCnt;
562   }
563   return 0;
564 }
565
566 const TObject* AliHLTComponent::GetFirstInputObject(const AliHLTComponentDataType& dt,
567                                                     const char* classname,
568                                                     int bForce)
569 {
570   // see header file for function documentation
571   ALIHLTCOMPONENT_BASE_STOPWATCH();
572   fSearchDataType=dt;
573   if (classname) fClassName=classname;
574   else fClassName.clear();
575   int idx=FindInputBlock(fSearchDataType, 0, 1);
576   TObject* pObj=NULL;
577   if (idx>=0) {
578     HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(dt).c_str());
579     if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
580       fCurrentInputBlock=idx;
581     } else {
582     }
583   }
584   return pObj;
585 }
586
587 const TObject* AliHLTComponent::GetFirstInputObject(const char* dtID, 
588                                                     const char* dtOrigin,
589                                                     const char* classname,
590                                                     int         bForce)
591 {
592   // see header file for function documentation
593   ALIHLTCOMPONENT_BASE_STOPWATCH();
594   AliHLTComponentDataType dt;
595   SetDataType(dt, dtID, dtOrigin);
596   return GetFirstInputObject(dt, classname, bForce);
597 }
598
599 const TObject* AliHLTComponent::GetNextInputObject(int bForce)
600 {
601   // see header file for function documentation
602   ALIHLTCOMPONENT_BASE_STOPWATCH();
603   int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1, 1);
604   //HLTDebug("found block %d when searching for data type %s", idx, DataType2Text(fSearchDataType).c_str());
605   TObject* pObj=NULL;
606   if (idx>=0) {
607     if ((pObj=GetInputObject(idx, fClassName.c_str(), bForce))!=NULL) {
608       fCurrentInputBlock=idx;
609     }
610   }
611   return pObj;
612 }
613
614 int AliHLTComponent::FindInputBlock(const AliHLTComponentDataType& dt, int startIdx, int bObject) const
615 {
616   // see header file for function documentation
617   int iResult=-ENOENT;
618   if (fpInputBlocks!=NULL) {
619     int idx=startIdx<0?0:startIdx;
620     for ( ; (UInt_t)idx<fCurrentEventData.fBlockCnt && iResult==-ENOENT; idx++) {
621       if (bObject!=0) {
622         if (fpInputBlocks[idx].fPtr==NULL) continue;
623         AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
624         if (firstWord!=fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) continue;
625       }
626       if (dt==fpInputBlocks[idx].fDataType) {
627         iResult=idx;
628       }
629     }
630   }
631   return iResult;
632 }
633
634 TObject* AliHLTComponent::CreateInputObject(int idx, int bForce)
635 {
636   // see header file for function documentation
637   TObject* pObj=NULL;
638   if (fpInputBlocks!=NULL) {
639     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
640       if (fpInputBlocks[idx].fPtr) {
641         AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)fpInputBlocks[idx].fPtr);
642         if (firstWord==fpInputBlocks[idx].fSize-sizeof(AliHLTUInt32_t)) {
643           HLTDebug("create object from block %d size %d", idx, fpInputBlocks[idx].fSize);
644           AliHLTMessage msg(fpInputBlocks[idx].fPtr, fpInputBlocks[idx].fSize);
645           TClass* objclass=msg.GetClass();
646           pObj=msg.ReadObject(objclass);
647           if (pObj && objclass) {
648             HLTDebug("object %p type %s created", pObj, objclass->GetName());
649           } else {
650           }
651           //} else {
652         } else if (bForce!=0) {
653           HLTError("size missmatch: block size %d, indicated %d", fpInputBlocks[idx].fSize, firstWord+sizeof(AliHLTUInt32_t));
654         }
655       } else {
656         HLTFatal("block descriptor empty");
657       }
658     } else {
659       HLTError("index %d out of range %d", idx, fCurrentEventData.fBlockCnt);
660     }
661   } else {
662     HLTError("no input blocks available");
663   }
664   
665   return pObj;
666 }
667
668 TObject* AliHLTComponent::GetInputObject(int idx, const char* /*classname*/, int bForce)
669 {
670   // see header file for function documentation
671   if (fpInputObjects==NULL) {
672     fpInputObjects=new TObjArray(fCurrentEventData.fBlockCnt);
673   }
674   TObject* pObj=NULL;
675   if (fpInputObjects) {
676     pObj=fpInputObjects->At(idx);
677     if (pObj==NULL) {
678       pObj=CreateInputObject(idx, bForce);
679       if (pObj) {
680         fpInputObjects->AddAt(pObj, idx);
681       }
682     }
683   } else {
684     HLTFatal("memory allocation failed: TObjArray of size %d", fCurrentEventData.fBlockCnt);
685   }
686   return pObj;
687 }
688
689 int AliHLTComponent::CleanupInputObjects()
690 {
691   // see header file for function documentation
692   if (!fpInputObjects) return 0;
693   TObjArray* array=fpInputObjects;
694   fpInputObjects=NULL;
695   for (int i=0; i<array->GetEntries(); i++) {
696     TObject* pObj=array->At(i);
697     // grrr, garbage collection strikes back: When read via AliHLTMessage
698     // (CreateInputObject), and written to a TFile afterwards, the
699     // TFile::Close calls ROOOT's garbage collection. No clue why the
700     // object ended up in the key list and needs to be deleted
701     if (pObj && gObjectTable->PtrIsValid(pObj)) delete pObj;
702   }
703   delete array;
704   return 0;
705 }
706
707 AliHLTComponentDataType AliHLTComponent::GetDataType(const TObject* pObject)
708 {
709   // see header file for function documentation
710   ALIHLTCOMPONENT_BASE_STOPWATCH();
711   AliHLTComponentDataType dt=kAliHLTVoidDataType;
712   int idx=fCurrentInputBlock;
713   if (pObject) {
714     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
715     } else {
716       HLTError("unknown object %p", pObject);
717     }
718   }
719   if (idx>=0) {
720     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
721       dt=fpInputBlocks[idx].fDataType;
722     } else {
723       HLTFatal("severe internal error, index out of range");
724     }
725   }
726   return dt;
727 }
728
729 AliHLTUInt32_t AliHLTComponent::GetSpecification(const TObject* pObject)
730 {
731   // see header file for function documentation
732   ALIHLTCOMPONENT_BASE_STOPWATCH();
733   AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
734   int idx=fCurrentInputBlock;
735   if (pObject) {
736     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
737     } else {
738       HLTError("unknown object %p", pObject);
739     }
740   }
741   if (idx>=0) {
742     if ((UInt_t)idx<fCurrentEventData.fBlockCnt) {
743       iSpec=fpInputBlocks[idx].fSpecification;
744     } else {
745       HLTFatal("severe internal error, index out of range");
746     }
747   }
748   return iSpec;
749 }
750
751 int AliHLTComponent::Forward(const TObject* pObject)
752 {
753   // see header file for function documentation
754   int iResult=0;
755   int idx=fCurrentInputBlock;
756   if (pObject) {
757     if (fpInputObjects==NULL || (idx=fpInputObjects->IndexOf(pObject))>=0) {
758     } else {
759       HLTError("unknown object %p", pObject);
760       iResult=-ENOENT;
761     }
762   }
763   if (idx>=0) {
764     fOutputBlocks.push_back(fpInputBlocks[idx]);
765   }
766   return iResult;
767 }
768
769 int AliHLTComponent::Forward(const AliHLTComponentBlockData* pBlock)
770 {
771   // see header file for function documentation
772   int iResult=0;
773   int idx=fCurrentInputBlock;
774   if (pBlock) {
775     if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
776     } else {
777       HLTError("unknown Block %p", pBlock);
778       iResult=-ENOENT;      
779     }
780   }
781   if (idx>=0) {
782     // check for fpInputBlocks pointer done in FindInputBlock
783     fOutputBlocks.push_back(fpInputBlocks[idx]);
784   }
785   return iResult;
786 }
787
788 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const AliHLTComponentDataType& dt)
789 {
790   // see header file for function documentation
791   ALIHLTCOMPONENT_BASE_STOPWATCH();
792   fSearchDataType=dt;
793   fClassName.clear();
794   int idx=FindInputBlock(fSearchDataType, 0);
795   const AliHLTComponentBlockData* pBlock=NULL;
796   if (idx>=0) {
797     // check for fpInputBlocks pointer done in FindInputBlock
798     pBlock=&fpInputBlocks[idx];
799     fCurrentInputBlock=idx;
800   }
801   return pBlock;
802 }
803
804 const AliHLTComponentBlockData* AliHLTComponent::GetFirstInputBlock(const char* dtID, 
805                                                                     const char* dtOrigin)
806 {
807   // see header file for function documentation
808   ALIHLTCOMPONENT_BASE_STOPWATCH();
809   AliHLTComponentDataType dt;
810   SetDataType(dt, dtID, dtOrigin);
811   return GetFirstInputBlock(dt);
812 }
813
814 const AliHLTComponentBlockData* AliHLTComponent::GetInputBlock(int index)
815 {
816   // see header file for function documentation
817   ALIHLTCOMPONENT_BASE_STOPWATCH();
818   assert( 0 <= index and index < fCurrentEventData.fBlockCnt );
819   return &fpInputBlocks[index];
820 }
821
822 const AliHLTComponentBlockData* AliHLTComponent::GetNextInputBlock()
823 {
824   // see header file for function documentation
825   ALIHLTCOMPONENT_BASE_STOPWATCH();
826   int idx=FindInputBlock(fSearchDataType, fCurrentInputBlock+1);
827   const AliHLTComponentBlockData* pBlock=NULL;
828   if (idx>=0) {
829     // check for fpInputBlocks pointer done in FindInputBlock
830     pBlock=&fpInputBlocks[idx];
831     fCurrentInputBlock=idx;
832   }
833   return pBlock;
834 }
835
836 int AliHLTComponent::FindInputBlock(const AliHLTComponentBlockData* pBlock) const
837 {
838   // see header file for function documentation
839   int iResult=-ENOENT;
840   if (fpInputBlocks!=NULL) {
841     if (pBlock) {
842       if (pBlock>=fpInputBlocks && pBlock<fpInputBlocks+fCurrentEventData.fBlockCnt) {
843         iResult=(int)(pBlock-fpInputBlocks);
844       }
845     } else {
846       iResult=-EINVAL;
847     }
848   }
849   return iResult;
850 }
851
852 AliHLTUInt32_t AliHLTComponent::GetSpecification(const AliHLTComponentBlockData* pBlock)
853 {
854   // see header file for function documentation
855   ALIHLTCOMPONENT_BASE_STOPWATCH();
856   AliHLTUInt32_t iSpec=kAliHLTVoidDataSpec;
857   int idx=fCurrentInputBlock;
858   if (pBlock) {
859     if (fpInputObjects==NULL || (idx=FindInputBlock(pBlock))>=0) {
860     } else {
861       HLTError("unknown Block %p", pBlock);
862     }
863   }
864   if (idx>=0) {
865     // check for fpInputBlocks pointer done in FindInputBlock
866     iSpec=fpInputBlocks[idx].fSpecification;
867   }
868   return iSpec;
869 }
870
871 int AliHLTComponent::PushBack(TObject* pObject, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec, 
872                               void* pHeader, int headerSize)
873 {
874   // see header file for function documentation
875   ALIHLTCOMPONENT_BASE_STOPWATCH();
876   int iResult=0;
877   if (pObject) {
878     AliHLTMessage msg(kMESS_OBJECT);
879     msg.WriteObject(pObject);
880     Int_t iMsgLength=msg.Length();
881     if (iMsgLength>0) {
882       msg.SetLength(); // sets the length to the first (reserved) word
883       iResult=InsertOutputBlock(msg.Buffer(), iMsgLength, dt, spec, pHeader, headerSize);
884       if (iResult>=0) {
885         HLTDebug("object %s (%p) size %d inserted to output", pObject->ClassName(), pObject, iMsgLength);
886       }
887     } else {
888       HLTError("object serialization failed for object %p", pObject);
889       iResult=-ENOMSG;
890     }
891   } else {
892     iResult=-EINVAL;
893   }
894   return iResult;
895 }
896
897 int AliHLTComponent::PushBack(TObject* pObject, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
898                               void* pHeader, int headerSize)
899 {
900   // see header file for function documentation
901   ALIHLTCOMPONENT_BASE_STOPWATCH();
902   AliHLTComponentDataType dt;
903   SetDataType(dt, dtID, dtOrigin);
904   return PushBack(pObject, dt, spec, pHeader, headerSize);
905 }
906
907 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
908                               void* pHeader, int headerSize)
909 {
910   // see header file for function documentation
911   ALIHLTCOMPONENT_BASE_STOPWATCH();
912   return InsertOutputBlock(pBuffer, iSize, dt, spec, pHeader, headerSize);
913 }
914
915 int AliHLTComponent::PushBack(void* pBuffer, int iSize, const char* dtID, const char* dtOrigin, AliHLTUInt32_t spec,
916                               void* pHeader, int headerSize)
917 {
918   // see header file for function documentation
919   ALIHLTCOMPONENT_BASE_STOPWATCH();
920   AliHLTComponentDataType dt;
921   SetDataType(dt, dtID, dtOrigin);
922   return PushBack(pBuffer, iSize, dt, spec, pHeader, headerSize);
923 }
924
925 int AliHLTComponent::InsertOutputBlock(void* pBuffer, int iBufferSize, const AliHLTComponentDataType& dt, AliHLTUInt32_t spec,
926                               void* pHeader, int iHeaderSize)
927 {
928   // see header file for function documentation
929   int iResult=0;
930   int iBlkSize = iBufferSize + iHeaderSize;
931   if (pBuffer) {
932     if (fpOutputBuffer && iBlkSize<=(int)(fOutputBufferSize-fOutputBufferFilled)) {
933       AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
934       AliHLTComponentBlockData bd;
935       FillBlockData( bd );
936       bd.fOffset        = fOutputBufferFilled;
937       bd.fSize          = iBlkSize;
938       bd.fDataType      = dt;
939       bd.fSpecification = spec;
940       if (pHeader!=NULL && pHeader!=pTgt) {
941         memcpy(pTgt, pHeader, iHeaderSize);
942       }
943
944       pTgt += (AliHLTUInt8_t) iHeaderSize;
945
946       if (pBuffer!=NULL && pBuffer!=pTgt) {
947         memcpy(pTgt, pBuffer, iBufferSize);
948         
949         //AliHLTUInt32_t firstWord=*((AliHLTUInt32_t*)pBuffer); 
950         //HLTDebug("copy %d bytes from %p to output buffer %p, first word %#x", iBufferSize, pBuffer, pTgt, firstWord);
951       }
952       fOutputBufferFilled+=bd.fSize;
953       fOutputBlocks.push_back( bd );
954       //HLTDebug("buffer inserted to output: size %d data type %s spec %#x", iBlkSize, DataType2Text(dt).c_str(), spec);
955     } else {
956       if (fpOutputBuffer) {
957         HLTError("too little space in output buffer: %d, required %d", fOutputBufferSize-fOutputBufferFilled, iBlkSize);
958       } else {
959         HLTError("output buffer not available");
960       }
961       iResult=-ENOSPC;
962     }
963   } else {
964     iResult=-EINVAL;
965   }
966   return iResult;
967 }
968
969 int AliHLTComponent::EstimateObjectSize(TObject* pObject) const
970 {
971   // see header file for function documentation
972   if (!pObject) return -EINVAL;
973     AliHLTMessage msg(kMESS_OBJECT);
974     msg.WriteObject(pObject);
975     return msg.Length();  
976 }
977
978 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity, const char* dtID,
979                                                     const char* dtOrigin,
980                                                     AliHLTUInt32_t spec)
981 {
982   // see header file for function documentation
983   ALIHLTCOMPONENT_BASE_STOPWATCH();
984   AliHLTComponentDataType dt;
985   SetDataType(dt, dtID, dtOrigin);
986   return CreateMemoryFile(capacity, dt, spec);
987 }
988
989 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(int capacity,
990                                                     const AliHLTComponentDataType& dt,
991                                                     AliHLTUInt32_t spec)
992 {
993   // see header file for function documentation
994   ALIHLTCOMPONENT_BASE_STOPWATCH();
995   AliHLTMemoryFile* pFile=NULL;
996   if (capacity>=0 && static_cast<unsigned int>(capacity)<=fOutputBufferSize-fOutputBufferFilled){
997     AliHLTUInt8_t* pTgt=fpOutputBuffer+fOutputBufferFilled;
998     pFile=new AliHLTMemoryFile((char*)pTgt, capacity);
999     if (pFile) {
1000       unsigned int nofBlocks=fOutputBlocks.size();
1001       if (nofBlocks+1>fMemFiles.size()) {
1002         fMemFiles.resize(nofBlocks+1, NULL);
1003       }
1004       if (nofBlocks<fMemFiles.size()) {
1005         fMemFiles[nofBlocks]=pFile;
1006         AliHLTComponentBlockData bd;
1007         FillBlockData( bd );
1008         bd.fOffset        = fOutputBufferFilled;
1009         bd.fSize          = capacity;
1010         bd.fDataType      = dt;
1011         bd.fSpecification = spec;
1012         fOutputBufferFilled+=bd.fSize;
1013         fOutputBlocks.push_back( bd );
1014       } else {
1015         HLTError("can not allocate/grow object array");
1016         pFile->CloseMemoryFile(0);
1017         delete pFile;
1018         pFile=NULL;
1019       }
1020     }
1021   } else {
1022     HLTError("can not create memory file of size %d (%d available)", capacity, fOutputBufferSize-fOutputBufferFilled);
1023   }
1024   return pFile;
1025 }
1026
1027 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const char* dtID,
1028                                                     const char* dtOrigin,
1029                                                     AliHLTUInt32_t spec,
1030                                                     float capacity)
1031 {
1032   // see header file for function documentation
1033   ALIHLTCOMPONENT_BASE_STOPWATCH();
1034   AliHLTComponentDataType dt;
1035   SetDataType(dt, dtID, dtOrigin);
1036   int size=fOutputBufferSize-fOutputBufferFilled;
1037   if (capacity<0 || capacity>1.0) {
1038     HLTError("invalid parameter: capacity %f", capacity);
1039     return NULL;
1040   }
1041   size=(int)(size*capacity);
1042   return CreateMemoryFile(size, dt, spec);
1043 }
1044
1045 AliHLTMemoryFile* AliHLTComponent::CreateMemoryFile(const AliHLTComponentDataType& dt,
1046                                                     AliHLTUInt32_t spec,
1047                                                     float capacity)
1048 {
1049   // see header file for function documentation
1050   ALIHLTCOMPONENT_BASE_STOPWATCH();
1051   int size=fOutputBufferSize-fOutputBufferFilled;
1052   if (capacity<0 || capacity>1.0) {
1053     HLTError("invalid parameter: capacity %f", capacity);
1054     return NULL;
1055   }
1056   size=(int)(size*capacity);
1057   return CreateMemoryFile(size, dt, spec);
1058 }
1059
1060 int AliHLTComponent::Write(AliHLTMemoryFile* pFile, const TObject* pObject,
1061                            const char* key, int option)
1062 {
1063   // see header file for function documentation
1064   int iResult=0;
1065   if (pFile && pObject) {
1066     pFile->cd();
1067     iResult=pObject->Write(key, option);
1068     if (iResult>0) {
1069       // success
1070     } else {
1071       iResult=-pFile->GetErrno();
1072       if (iResult==-ENOSPC) {
1073         HLTError("error writing memory file, buffer too small");
1074       }
1075     }
1076   } else {
1077     iResult=-EINVAL;
1078   }
1079   return iResult;
1080 }
1081
1082 int AliHLTComponent::CloseMemoryFile(AliHLTMemoryFile* pFile)
1083 {
1084   // see header file for function documentation
1085   int iResult=0;
1086   if (pFile) {
1087     AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1088     int i=0;
1089     while (element!=fMemFiles.end() && iResult>=0) {
1090       if (*element && *element==pFile) {
1091         iResult=pFile->CloseMemoryFile();
1092         
1093         // sync memory files and descriptors
1094         if (iResult>=0) {
1095           fOutputBlocks[i].fSize=(*element)->GetSize()+(*element)->GetHeaderSize();
1096         }
1097         delete *element;
1098         *element=NULL;
1099         return iResult;
1100       }
1101       element++; i++;
1102     }
1103     HLTError("can not find memory file %p", pFile);
1104     iResult=-ENOENT;
1105   } else {
1106     iResult=-EINVAL;
1107   }
1108   return iResult;
1109 }
1110
1111 int AliHLTComponent::CreateEventDoneData(AliHLTComponentEventDoneData /*edd*/)
1112 {
1113   // see header file for function documentation
1114   int iResult=-ENOSYS;
1115   //#warning  function not yet implemented
1116   HLTWarning("function not yet implemented");
1117   return iResult;
1118 }
1119
1120 int AliHLTComponent::ProcessEvent( const AliHLTComponentEventData& evtData,
1121                                    const AliHLTComponentBlockData* blocks, 
1122                                    AliHLTComponentTriggerData& trigData,
1123                                    AliHLTUInt8_t* outputPtr, 
1124                                    AliHLTUInt32_t& size,
1125                                    AliHLTUInt32_t& outputBlockCnt, 
1126                                    AliHLTComponentBlockData*& outputBlocks,
1127                                    AliHLTComponentEventDoneData*& edd )
1128 {
1129   // see header file for function documentation
1130   ALIHLTCOMPONENT_BASE_STOPWATCH();
1131   int iResult=0;
1132   fCurrentEvent=evtData.fEventID;
1133   fCurrentEventData=evtData;
1134   fpInputBlocks=blocks;
1135   fCurrentInputBlock=-1;
1136   fSearchDataType=kAliHLTAnyDataType;
1137   fpOutputBuffer=outputPtr;
1138   fOutputBufferSize=size;
1139   fOutputBufferFilled=0;
1140   fOutputBlocks.clear();
1141
1142   bool bSkipDataProcessing=false;
1143   // find special events
1144   if (fpInputBlocks) {
1145     // first look for all special events and execute in the appropriate
1146     // sequence afterwords
1147     AliHLTUInt32_t eventType=gkAliEventTypeUnknown;
1148     int indexComConfEvent=-1;
1149     int indexUpdtDCSEvent=-1;
1150     int indexSOREvent=-1;
1151     int indexEOREvent=-1;
1152     for (unsigned int i=0; i<evtData.fBlockCnt && iResult>=0; i++) {
1153       if (fpInputBlocks[i].fDataType==kAliHLTDataTypeSOR) {
1154         indexSOREvent=i;
1155       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEOR) {
1156         indexEOREvent=i;
1157       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeDDL) {
1158         // DDL list
1159         // this event is most likely deprecated
1160       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeComConf) {
1161         indexComConfEvent=i;
1162       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeUpdtDCS) {
1163         indexUpdtDCSEvent=i;
1164       } else if (fpInputBlocks[i].fDataType==kAliHLTDataTypeEvent) {
1165         eventType=fpInputBlocks[i].fSpecification;
1166         bSkipDataProcessing=fpInputBlocks[i].fSpecification==gkAliEventTypeConfiguration;
1167       }
1168     }
1169     if (indexSOREvent>=0) {
1170       // start of run
1171       if (fpRunDesc==NULL) {
1172         fpRunDesc=new AliHLTRunDesc;
1173         if (fpRunDesc) {
1174           if ((iResult=CopyStruct(fpRunDesc, sizeof(AliHLTRunDesc), indexSOREvent, "AliHLTRunDesc", "SOR"))>0) {
1175             HLTDebug("set run decriptor, run no %d", fpRunDesc->fRunNo);
1176             SetCDBRunNo(fpRunDesc->fRunNo);
1177           }
1178         } else {
1179           iResult=-ENOMEM;
1180         }
1181       } else {
1182         HLTWarning("already received SOR event run no %d, ignoring SOR", fpRunDesc->fRunNo);
1183       }
1184     }
1185     if (indexEOREvent>=0) {
1186       if (fpRunDesc!=NULL) {
1187         if (fpRunDesc) {
1188           AliHLTRunDesc rundesc;
1189           if ((iResult=CopyStruct(&rundesc, sizeof(AliHLTRunDesc), indexEOREvent, "AliHLTRunDesc", "SOR"))>0) {
1190             if (fpRunDesc->fRunNo!=rundesc.fRunNo) {
1191               HLTWarning("run no missmatch: SOR %d, EOR %d", fpRunDesc->fRunNo, rundesc.fRunNo);
1192             } else {
1193               HLTDebug("EOR run no %d", fpRunDesc->fRunNo);
1194             }
1195           }
1196           AliHLTRunDesc* pRunDesc=fpRunDesc;
1197           fpRunDesc=NULL;
1198           delete pRunDesc;
1199         }
1200       } else {
1201         HLTWarning("did not receive SOR, ignoring EOR");
1202       }
1203     }
1204     if (indexComConfEvent>=0 || eventType==gkAliEventTypeConfiguration) {
1205       TString cdbEntry;
1206       if (indexComConfEvent>=0 && fpInputBlocks[indexComConfEvent].fPtr!=NULL && fpInputBlocks[indexComConfEvent].fSize>0) {
1207         cdbEntry.Append(reinterpret_cast<const char*>(fpInputBlocks[indexComConfEvent].fPtr), fpInputBlocks[indexComConfEvent].fSize);
1208       }
1209       HLTDebug("received component configuration command: entry %s", cdbEntry.IsNull()?"none":cdbEntry.Data());
1210       int tmpResult=Reconfigure(cdbEntry[0]==0?NULL:cdbEntry.Data(), fChainId.c_str());
1211       if (tmpResult<0) {
1212         HLTWarning("reconfiguration of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1213       }
1214     }
1215     if (indexUpdtDCSEvent>=0) {
1216       TString modules;
1217       if (fpInputBlocks[indexUpdtDCSEvent].fPtr!=NULL && fpInputBlocks[indexUpdtDCSEvent].fSize>0) {
1218         modules.Append(reinterpret_cast<const char*>(fpInputBlocks[indexUpdtDCSEvent].fPtr), fpInputBlocks[indexUpdtDCSEvent].fSize);
1219       }
1220       HLTDebug("received preprocessor update command: detectors %s", modules.IsNull()?"none":modules.Data());
1221       int tmpResult=ReadPreprocessorValues(modules[0]==0?NULL:modules.Data());
1222       if (tmpResult<0) {
1223         HLTWarning("preprocessor update of component %p (%s) failed with error code %d", this, GetComponentID(), tmpResult);
1224       }
1225     }
1226   }
1227   
1228   AliHLTComponentBlockDataList blockData;
1229   { // dont delete, sets the scope for the stopwatch guard
1230     ALIHLTCOMPONENT_DA_STOPWATCH();
1231     iResult=DoProcessing(evtData, blocks, trigData, outputPtr, size, blockData, edd);
1232   } // end of the scope of the stopwatch guard
1233   if (iResult>=0 && !bSkipDataProcessing) {
1234     if (fOutputBlocks.size()>0) {
1235       //HLTDebug("got %d block(s) via high level interface", fOutputBlocks.size());
1236       
1237       // sync memory files and descriptors
1238       AliHLTMemoryFilePList::iterator element=fMemFiles.begin();
1239       int i=0;
1240       while (element!=fMemFiles.end() && iResult>=0) {
1241         if (*element) {
1242           if ((*element)->IsClosed()==0) {
1243             HLTWarning("memory file has not been closed, force flush");
1244             iResult=CloseMemoryFile(*element);
1245           }
1246         }
1247         element++; i++;
1248       }
1249
1250       if (iResult>=0) {
1251         // create the descriptor list
1252         if (blockData.size()>0) {
1253           HLTError("low level and high interface must not be mixed; use PushBack methods to insert data blocks");
1254           iResult=-EFAULT;
1255         } else {
1256           iResult=MakeOutputDataBlockList(fOutputBlocks, &outputBlockCnt, &outputBlocks);
1257           size=fOutputBufferFilled;
1258         }
1259       }
1260     } else {
1261       iResult=MakeOutputDataBlockList(blockData, &outputBlockCnt, &outputBlocks);
1262     }
1263     if (iResult<0) {
1264       HLTFatal("component %s (%p): can not convert output block descriptor list", GetComponentID(), this);
1265     }
1266   }
1267   if (iResult<0 || bSkipDataProcessing) {
1268     outputBlockCnt=0;
1269     outputBlocks=NULL;
1270   }
1271   CleanupInputObjects();
1272   if (iResult>=0 && !bSkipDataProcessing) {
1273     IncrementEventCounter();
1274   }
1275   return iResult;
1276 }
1277
1278 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard()
1279   :
1280   fpStopwatch(NULL),
1281   fpPrec(NULL)
1282 {
1283   // standard constructor (not for use)
1284 }
1285
1286 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(TStopwatch* pStopwatch)
1287   :
1288   fpStopwatch(pStopwatch),
1289   fpPrec(NULL)
1290 {
1291   // constructor
1292
1293   // check for already existing guard
1294   if (fgpCurrent) fpPrec=fgpCurrent;
1295   fgpCurrent=this;
1296
1297   // stop the preceeding guard if it controls a different stopwatch
1298   int bStart=1;
1299   if (fpPrec && fpPrec!=this) bStart=fpPrec->Hold(fpStopwatch);
1300
1301   // start the stopwatch if the current guard controls a different one
1302   if (fpStopwatch && bStart==1) fpStopwatch->Start(kFALSE);
1303 }
1304
1305 AliHLTComponent::AliHLTStopwatchGuard::AliHLTStopwatchGuard(const AliHLTStopwatchGuard&)
1306   :
1307   fpStopwatch(NULL),
1308   fpPrec(NULL)
1309 {
1310   //
1311   // copy constructor not for use
1312   //
1313 }
1314
1315 AliHLTComponent::AliHLTStopwatchGuard& AliHLTComponent::AliHLTStopwatchGuard::operator=(const AliHLTStopwatchGuard&)
1316 {
1317   //
1318   // assignment operator not for use
1319   //
1320   fpStopwatch=NULL;
1321   fpPrec=NULL;
1322   return *this;
1323 }
1324
1325 AliHLTComponent::AliHLTStopwatchGuard* AliHLTComponent::AliHLTStopwatchGuard::fgpCurrent=NULL;
1326
1327 AliHLTComponent::AliHLTStopwatchGuard::~AliHLTStopwatchGuard()
1328 {
1329   // destructor
1330
1331   // resume the preceeding guard if it controls a different stopwatch
1332   int bStop=1;
1333   if (fpPrec && fpPrec!=this) bStop=fpPrec->Resume(fpStopwatch);
1334
1335   // stop the stopwatch if the current guard controls a different one
1336   if (fpStopwatch && bStop==1) fpStopwatch->Stop();
1337
1338   // resume to the preceeding guard
1339   fgpCurrent=fpPrec;
1340 }
1341
1342 int AliHLTComponent::AliHLTStopwatchGuard::Hold(TStopwatch* pSucc)
1343 {
1344   // see header file for function documentation
1345   if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Stop();
1346   return fpStopwatch!=pSucc?1:0;
1347 }
1348
1349 int AliHLTComponent::AliHLTStopwatchGuard::Resume(TStopwatch* pSucc)
1350 {
1351   // see header file for function documentation
1352   if (fpStopwatch!=NULL && fpStopwatch!=pSucc) fpStopwatch->Start(kFALSE);
1353   return fpStopwatch!=pSucc?1:0;
1354 }
1355
1356 int AliHLTComponent::SetStopwatch(TObject* pSW, AliHLTStopwatchType type) 
1357 {
1358   // see header file for function documentation
1359   int iResult=0;
1360   if (pSW!=NULL && type<kSWTypeCount) {
1361     if (fpStopwatches) {
1362       TObject* pObj=fpStopwatches->At((int)type);
1363       if (pSW==NULL        // explicit reset
1364           || pObj==NULL) { // explicit set
1365         fpStopwatches->AddAt(pSW, (int)type);
1366       } else if (pObj!=pSW) {
1367         HLTWarning("stopwatch %d already set, reset first", (int)type);
1368         iResult=-EBUSY;
1369       }
1370     }
1371   } else {
1372     iResult=-EINVAL;
1373   }
1374   return iResult;
1375 }
1376
1377 int AliHLTComponent::SetStopwatches(TObjArray* pStopwatches)
1378 {
1379   // see header file for function documentation
1380   if (pStopwatches==NULL) return -EINVAL;
1381
1382   int iResult=0;
1383   for (int i=0 ; i<(int)kSWTypeCount && pStopwatches->GetEntries(); i++)
1384     SetStopwatch(pStopwatches->At(i), (AliHLTStopwatchType)i);
1385   return iResult;
1386 }
1387
1388 AliHLTUInt32_t AliHLTComponent::GetRunNo() const
1389 {
1390   // see header file for function documentation
1391   if (fpRunDesc==NULL) return 0;
1392   return fpRunDesc->fRunNo;
1393 }
1394
1395 AliHLTUInt32_t AliHLTComponent::GetRunType() const
1396 {
1397   // see header file for function documentation
1398   if (fpRunDesc==NULL) return 0;
1399   return fpRunDesc->fRunType;
1400 }
1401
1402 int AliHLTComponent::CopyStruct(void* pStruct, unsigned int iStructSize, unsigned int iBlockNo,
1403                                 const char* structname, const char* eventname)
1404 {
1405   // see header file for function documentation
1406   int iResult=0;
1407   if (pStruct!=NULL && iStructSize>sizeof(AliHLTUInt32_t)) {
1408     if (fpInputBlocks!=NULL && iBlockNo<fCurrentEventData.fBlockCnt) {
1409       AliHLTUInt32_t* pTgt=(AliHLTUInt32_t*)pStruct;
1410       if (fpInputBlocks[iBlockNo].fPtr && fpInputBlocks[iBlockNo].fSize) {
1411         AliHLTUInt32_t copy=*((AliHLTUInt32_t*)fpInputBlocks[iBlockNo].fPtr);
1412         if (fpInputBlocks[iBlockNo].fSize!=copy) {
1413           HLTWarning("%s event: missmatch of block size (%d) and structure size (%d)", eventname, fpInputBlocks[iBlockNo].fSize, copy);
1414           if (copy>fpInputBlocks[iBlockNo].fSize) copy=fpInputBlocks[iBlockNo].fSize;
1415         }
1416         if (copy!=iStructSize) {
1417           HLTWarning("%s event: missmatch in %s version (data type version %d)", eventname, structname, ALIHLT_DATA_TYPES_VERSION);
1418           if (copy>iStructSize) {
1419             copy=iStructSize;
1420           } else {
1421             memset(pTgt, 0, iStructSize);
1422           }
1423         }
1424         memcpy(pTgt, fpInputBlocks[iBlockNo].fPtr, copy);
1425         *pTgt=iStructSize;
1426         iResult=copy;
1427       } else {
1428         HLTWarning("%s event: missing data block", eventname);
1429       }
1430     } else {
1431       iResult=-ENODATA;
1432     }
1433   } else {
1434     HLTError("invalid struct");
1435     iResult=-EINVAL;
1436   }
1437   return iResult;
1438 }
1439
1440 void AliHLTComponent::SetDDLBit(AliHLTEventDDL &list, Int_t ddlId, Bool_t state ) const
1441 {
1442   // see header file for function documentation
1443   
1444   // -- Detector offset
1445   Int_t ddlIdBase =  TMath::FloorNint( (Double_t) ddlId / 256.0 );
1446   
1447   // -- Word Base = 1. word of detector ( TPC has 8 words, TOF 3 ) 
1448   Int_t wordBase = 0;
1449
1450   if ( ddlIdBase <= 3 )
1451     wordBase = ddlIdBase;
1452   else if ( ddlIdBase > 3 && ddlIdBase < 5 )
1453     wordBase = ddlIdBase + 7;
1454   else 
1455     wordBase = ddlIdBase + 9;
1456
1457   // -- Bit index in Word
1458   Int_t bitIdx = ddlId % 32;
1459
1460   // -- Index of word
1461   Int_t wordIdx = wordBase;
1462
1463   // -- if TPC (3) or TOD (5) add word idx
1464   if ( ( ddlIdBase == 3 ) || ( ddlIdBase == 5 ) ) {
1465     wordIdx += TMath::FloorNint( (Double_t) ( ddlId - ( ddlIdBase * 256 ) ) / 32.0 );
1466   }
1467
1468   // -- Set -- 'OR' word with bit mask;
1469   if ( state )
1470     list.fList[wordIdx] |= ( 0x00000001 << bitIdx );
1471   // -- Unset -- 'AND' word with bit mask;
1472   else
1473     list.fList[wordIdx] &= ( 0xFFFFFFFF ^ ( 0x00000001 << bitIdx ) );
1474 }
1475
1476 Int_t AliHLTComponent::GetFirstUsedDDLWord(AliHLTEventDDL &list) const
1477 {
1478   // see header file for function documentation
1479
1480   Int_t iResult = -1;
1481
1482   for ( Int_t wordNdx = 0 ; wordNdx < gkAliHLTDDLListSize ; wordNdx++ ) {
1483
1484     if ( list.fList[wordNdx] != 0 && iResult == -1 ) {
1485       // check for special cases TPC and TOF
1486       if ( wordNdx > 3 && wordNdx <= 10 ) {
1487         wordNdx = 10;
1488         iResult = 3;
1489       }
1490       else if ( wordNdx > 12 && wordNdx <= 14 ) {
1491         wordNdx = 14;
1492         iResult = 12;
1493       }
1494       else
1495         iResult = wordNdx;
1496     }
1497     else if ( list.fList[wordNdx] != 0 && iResult >= 0 ) {
1498       HLTError( "DDLIDs for minimum of TWO detectors ( %d, %d ) set, this function works only for ONE detector.", iResult, wordNdx );
1499       iResult = -1;
1500       break;
1501     }
1502   }
1503
1504   return iResult;
1505 }