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