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