]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/BASE/AliHLTOUT.cxx
bugfix: library load failed due to typo, libHLTRec corrected to libHLTrec
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTOUT.cxx
index 2a6d96c178f652becec2394efa1cf49796d1954f..1850a160ce4bfd3654835fc448e002caf496d735 100644 (file)
@@ -1,35 +1,33 @@
 // $Id$
 
-/**************************************************************************
- * This file is property of and copyright by the ALICE HLT Project        * 
- * ALICE Experiment at CERN, All rights reserved.                         *
- *                                                                        *
- * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
- *                  for The ALICE HLT Project.                            *
- *                                                                        *
- * Permission to use, copy, modify and distribute this software and its   *
- * documentation strictly for non-commercial purposes is hereby granted   *
- * without fee, provided that the above copyright notice appears in all   *
- * copies and that both the copyright notice and this permission notice   *
- * appear in the supporting documentation. The authors make no claims     *
- * about the suitability of this software for any purpose. It is          *
- * provided "as is" without express or implied warranty.                  *
- **************************************************************************/
+//**************************************************************************
+//* This file is property of and copyright by the ALICE HLT Project        * 
+//* ALICE Experiment at CERN, All rights reserved.                         *
+//*                                                                        *
+//* Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
+//*                  for The ALICE HLT Project.                            *
+//*                                                                        *
+//* Permission to use, copy, modify and distribute this software and its   *
+//* documentation strictly for non-commercial purposes is hereby granted   *
+//* without fee, provided that the above copyright notice appears in all   *
+//* copies and that both the copyright notice and this permission notice   *
+//* appear in the supporting documentation. The authors make no claims     *
+//* about the suitability of this software for any purpose. It is          *
+//* provided "as is" without express or implied warranty.                  *
+//**************************************************************************
 
 /** @file   AliHLTOUT.cxx
     @author Matthias Richter
     @date   
-    @brief  The control class for HLTOUT data.                            */
-
-// see header file for class documentation
-// or
-// refer to README to build package
-// or
-// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
+    @brief  The control class for HLTOUT data.
+*/
 
 #include <cerrno>
 #include <cassert>
 #include "AliHLTOUT.h"
+#include "TSystem.h"
+#include "TClass.h"
+#include "TROOT.h"
 
 /** ROOT macro for the implementation of ROOT specific class methods */
 ClassImp(AliHLTOUT)
@@ -38,11 +36,14 @@ AliHLTOUT::AliHLTOUT()
   :
   fSearchDataType(kAliHLTVoidDataType),
   fSearchSpecification(kAliHLTVoidDataSpec),
-  fFlags(0),
+  fSearchHandlerType(AliHLTModuleAgent::kUnknownOutput),
+  fFlags(kSkipProcessed),
   fBlockDescList(),
-  fCurrent(fBlockDescList.begin()),
+  fCurrent(0),
   fpBuffer(NULL),
-  fDataHandlers()
+  fDataHandlers(),
+  fbVerbose(true),
+  fLog()
 {
   // see header file for class documentation
   // or
@@ -51,23 +52,26 @@ AliHLTOUT::AliHLTOUT()
   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
 }
 
-// definitions from ALICE internal note ALICE-INT-2002-010
-const unsigned char AliHLTOUT::fgkCDHStatusWord=4;
-const unsigned char AliHLTOUT::fgkCDHStatusFlagsOffset=12;
-
-// definitions from ALICE internal note ALICE-INT-2006-XXX
-const unsigned char AliHLTOUT::fgkCDHFlagsHLTDecision=6;
-const unsigned char AliHLTOUT::fgkCDHFlagsHLTPayload=7;
-
 AliHLTOUT::~AliHLTOUT()
 {
   // see header file for class documentation
+  if (CheckStatusFlag(kIsSubCollection)) {
+    fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "~AliHLTOUT" , __FILE__ , __LINE__ , "severe internal error: collection has not been released, potential crash due to invalid pointer");
+  }
 }
+AliHLTOUT* AliHLTOUT::fgGlobalInstance=NULL;
 
 int AliHLTOUT::Init()
 {
   // see header file for class documentation
   int iResult=0;
+
+  // ignore if already initialized
+  if (fBlockDescList.size()>0) {
+    fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "Init" , __FILE__ , __LINE__ , "instance %p already initialized, skipping ...", this);
+    return 0;
+  }
+
   SetStatusFlag(kCollecting);
   if ((iResult=GenerateIndex())>=0) {
     if ((iResult=InitHandlers())>=0) {
@@ -84,22 +88,23 @@ int AliHLTOUT::GetNofDataBlocks()
 }
 
 int AliHLTOUT::SelectFirstDataBlock(AliHLTComponentDataType dt, AliHLTUInt32_t spec,
-                                   AliHLTModuleAgent::AliHLTOUTHandlerType handlerType)
+                                   AliHLTModuleAgent::AliHLTOUTHandlerType handlerType,
+                                   bool skipProcessed)
 {
   // see header file for class documentation
-  if (CheckStatusFlag(kLocked)) return -EPERM;
-  fCurrent=fBlockDescList.begin();
+  fCurrent=0;
   fSearchDataType=dt;
   fSearchSpecification=spec;
-  //fSearchHandlerType=handlerType;
+  fSearchHandlerType=handlerType;
+  if (skipProcessed) SetStatusFlag(kSkipProcessed);
+  else ClearStatusFlag(kSkipProcessed);
   return FindAndSelectDataBlock();
 }
 
 int AliHLTOUT::SelectNextDataBlock()
 {
   // see header file for class documentation
-  if (CheckStatusFlag(kLocked)) return -EPERM;
-  if (fCurrent==fBlockDescList.end()) return -ENOENT;
+  if (fCurrent>=fBlockDescList.size()) return -ENOENT;
   fCurrent++;
   return FindAndSelectDataBlock();
 }
@@ -109,14 +114,16 @@ int AliHLTOUT::FindAndSelectDataBlock()
   // see header file for class documentation
   if (CheckStatusFlag(kLocked)) return -EPERM;
   int iResult=-ENOENT;
-  while (fCurrent!=fBlockDescList.end() && iResult==-ENOENT) {
-    if ((*fCurrent)==fSearchDataType &&
-       fSearchSpecification==kAliHLTVoidDataSpec || (*fCurrent)==fSearchSpecification &&
-       1/*fSearchHandlerType==AliHLTModuleAgent::kUnknownOutput*/) {
-      iResult=fCurrent->GetIndex();
+  while (fCurrent<fBlockDescList.size() && iResult==-ENOENT) {
+    if (fBlockDescList[fCurrent]==fSearchDataType &&
+       (fSearchSpecification==kAliHLTVoidDataSpec || fBlockDescList[fCurrent]==fSearchSpecification) &&
+       (fSearchHandlerType==AliHLTModuleAgent::kUnknownOutput || FindHandlerDesc(fCurrent)==fSearchHandlerType) &&
+       (!CheckStatusFlag(kBlockSelection) || fBlockDescList[fCurrent].IsSelected()) &&
+       (!CheckStatusFlag(kSkipProcessed) || !fBlockDescList[fCurrent].IsProcessed())) {
+      iResult=fBlockDescList[fCurrent].GetIndex();
       // TODO: check the byte order on the current system and the byte order of the
-      // data block, print warning when missmatch and user did not check
-      //AliHLTOUTByteOrder_t blockBO=CheckByteOrder();
+      // data block, print warning when mismatch and user did not check
+      //AliHLTOUTByteOrder blockBO=CheckByteOrder();
       CheckByteOrder();
       /*
        if (blockBO!=fByteOrder) {
@@ -127,7 +134,7 @@ int AliHLTOUT::FindAndSelectDataBlock()
       ClearStatusFlag(kByteOrderChecked);
 
       // TODO: check the alignment on the current system and the alignment of the
-      // data block, print warning when missmatch and user did not check
+      // data block, print warning when mismatch and user did not check
       ClearStatusFlag(kAlignmentChecked);
 
       break;
@@ -141,19 +148,33 @@ int AliHLTOUT::GetDataBlockDescription(AliHLTComponentDataType& dt, AliHLTUInt32
 {
   // see header file for class documentation
   int iResult=-ENOENT;
-  if (fCurrent!=fBlockDescList.end()) {
+  if (fCurrent<fBlockDescList.size()) {
     iResult=0;
-    dt=(*fCurrent);
-    spec=(*fCurrent);
+    dt=fBlockDescList[fCurrent];
+    spec=fBlockDescList[fCurrent];
   }
   return iResult;
 }
 
+const AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::GetDataBlockHandlerDesc()
+{
+  // see header file for class documentation
+  return FindHandlerDesc(fCurrent);
+}
+
+AliHLTModuleAgent::AliHLTOUTHandlerType AliHLTOUT::GetDataBlockHandlerType()
+{
+  // see header file for class documentation
+  AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=FindHandlerDesc(fCurrent);
+  AliHLTModuleAgent::AliHLTOUTHandlerType type=desc;
+  return type;
+}
+
 AliHLTUInt32_t AliHLTOUT::GetDataBlockIndex()
 {
   // see header file for class documentation
-  if (fCurrent==fBlockDescList.end()) return AliHLTOUTInvalidIndex;
-  return fCurrent->GetIndex();
+  if (fCurrent>=fBlockDescList.size()) return AliHLTOUTInvalidIndex;
+  return fBlockDescList[fCurrent].GetIndex();
 }
 
 int AliHLTOUT::GetDataBuffer(const AliHLTUInt8_t* &pBuffer, AliHLTUInt32_t& size)
@@ -162,8 +183,8 @@ int AliHLTOUT::GetDataBuffer(const AliHLTUInt8_t* &pBuffer, AliHLTUInt32_t& size
   int iResult=-ENOENT;
   pBuffer=NULL;
   size=0;
-  if (fCurrent!=fBlockDescList.end()) {
-    if ((iResult=GetDataBuffer((*fCurrent).GetIndex(), pBuffer, size))>=0) {
+  if (fCurrent<fBlockDescList.size()) {
+    if ((iResult=fBlockDescList[fCurrent].GetDataBuffer(pBuffer, size))>=0) {
       fpBuffer=pBuffer;
     }
   }
@@ -177,11 +198,34 @@ int AliHLTOUT::ReleaseDataBuffer(const AliHLTUInt8_t* pBuffer)
   if (pBuffer==fpBuffer) {
     fpBuffer=NULL;
   } else {
-    HLTWarning("buffer %p does not match the provided one %p", pBuffer, fpBuffer);
+    fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "ReleaseDataBuffer" , __FILE__ , __LINE__ , "buffer %p does not match the provided one %p", pBuffer, fpBuffer);
   }
   return iResult;  
 }
 
+AliHLTModuleAgent* AliHLTOUT::GetAgent()
+{
+  // see header file for class documentation
+  AliHLTModuleAgent* pAgent=NULL;
+  pAgent=FindHandlerDesc(fCurrent);
+  return pAgent;
+}
+
+AliHLTOUTHandler* AliHLTOUT::GetHandler()
+{
+  // see header file for class documentation
+  AliHLTOUTHandler* pHandler=NULL;
+  pHandler=FindHandlerDesc(fCurrent);
+  return pHandler;
+}
+
+int AliHLTOUT::WriteESD(const AliHLTUInt8_t* /*pBuffer*/, AliHLTUInt32_t /*size*/, AliHLTComponentDataType /*dt*/, AliESDEvent* /*tgtesd*/) const
+{
+  // see header file for class documentation
+  fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "WriteESD" , __FILE__ , __LINE__ , "method not implemented in base class");
+  return -ENOSYS;
+}
+
 int AliHLTOUT::AddBlockDescriptor(const AliHLTOUTBlockDescriptor desc)
 {
   // see header file for class documentation
@@ -191,23 +235,23 @@ int AliHLTOUT::AddBlockDescriptor(const AliHLTOUTBlockDescriptor desc)
   return iResult;  
 }
 
-AliHLTOUT::AliHLTOUTByteOrder_t AliHLTOUT::CheckByteOrder()
+AliHLTOUT::AliHLTOUTByteOrder AliHLTOUT::CheckByteOrder()
 {
   // see header file for class documentation
-  if (fCurrent!=fBlockDescList.end()) {
+  if (fCurrent<fBlockDescList.size()) {
     SetStatusFlag(kByteOrderChecked);
-    AliHLTOUT::AliHLTOUTByteOrder_t order=CheckBlockByteOrder((*fCurrent).GetIndex());
+    AliHLTOUT::AliHLTOUTByteOrder order=CheckBlockByteOrder(fBlockDescList[fCurrent].GetIndex());
     return order;
   }
   return kInvalidByteOrder;
 }
 
-int AliHLTOUT::CheckAlignment(AliHLTOUT::AliHLTOUTDataType_t type)
+int AliHLTOUT::CheckAlignment(AliHLTOUT::AliHLTOUTDataType type)
 {
   // see header file for class documentation
-  if (fCurrent!=fBlockDescList.end()) {
+  if (fCurrent<fBlockDescList.size()) {
     SetStatusFlag(kAlignmentChecked);
-    int alignment=CheckBlockAlignment((*fCurrent).GetIndex(), type);
+    int alignment=CheckBlockAlignment(fBlockDescList[fCurrent].GetIndex(), type);
     return alignment;
   }
   return -ENOENT;
@@ -218,24 +262,40 @@ int AliHLTOUT::InitHandlers()
   // see header file for class documentation
   int iResult=0;
   AliHLTOUTIndexList remnants;
-  for (iResult=SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec); iResult>=0; iResult=SelectNextDataBlock()) {
+  int iCount=0;
+  for (int havedata=SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec); havedata>=0; havedata=SelectNextDataBlock()) {
+    iCount++;
     remnants.push_back(GetDataBlockIndex());
     AliHLTComponentDataType dt=kAliHLTVoidDataType;
     AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
-    if ((iResult=GetDataBlockDescription(dt, spec))<0) break;
+    if (GetDataBlockDescription(dt, spec)<0) break;
+    bool bHaveHandler=false;
     for (AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent(); pAgent && iResult>=0; pAgent=AliHLTModuleAgent::GetNextAgent()) {
       AliHLTModuleAgent::AliHLTOUTHandlerDesc handlerDesc;
-      if (pAgent->GetHandlerDescription(dt, spec, &handlerDesc)>0) {
+      if (pAgent->GetHandlerDescription(dt, spec, handlerDesc)>0) {
        AliHLTOUTHandlerListEntry entry(pAgent->GetOutputHandler(dt, spec), handlerDesc, pAgent, GetDataBlockIndex());
-       iResult=InsertHandler(entry);
+       InsertHandler(fDataHandlers, entry);
        remnants.pop_back();
+       bHaveHandler=true;
        break;
       }
     }
+    if (!bHaveHandler && (dt==kAliHLTDataTypeESDObject || dt==kAliHLTDataTypeESDTree)) {
+      // ESDs are handled by the framework
+      remnants.pop_back();
+    }
+  }
+
+  // warning if some of the data blocks are not selected by the kAliHLTAnyDataType
+  // criterion
+  if (GetNofDataBlocks()>iCount) {
+    fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "InitHandlers" , __FILE__ , __LINE__ , "incomplete data type in %d out of %d data block(s)", GetNofDataBlocks()-iCount, GetNofDataBlocks());
   }
+
+  // warning if handler not found
   if (remnants.size()>0) {
-    HLTWarning("no handlers found for %d data blocks out of %d", remnants.size(), GetNofDataBlocks());
-    vector<AliHLTOUTBlockDescriptor>::iterator block=fBlockDescList.begin();
+    fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "InitHandlers" , __FILE__ , __LINE__ , "no handlers found for %d data blocks out of %d", remnants.size(), iCount);
+    AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
     for (AliHLTOUTIndexList::iterator element=remnants.begin(); element!=remnants.end(); element++) {
       for (int trials=0; trials<2; trials++) {
        do {
@@ -249,65 +309,160 @@ int AliHLTOUT::InitHandlers()
       }
       assert(block!=fBlockDescList.end());
       if (block!=fBlockDescList.end()) {
-       HLTDebug("   %s", AliHLTComponent::DataType2Text((AliHLTComponentDataType)*block).c_str());
+       //HLTDebug("   %s", AliHLTComponent::DataType2Text((AliHLTComponentDataType)*block).c_str());
       }
     }
   }
   return iResult;
 }
 
-int AliHLTOUT::InsertHandler(const AliHLTOUTHandlerListEntry &entry)
+int AliHLTOUT::InsertHandler(AliHLTOUTHandlerListEntryVector& list, const AliHLTOUTHandlerListEntry &entry)
 {
   // see header file for class documentation
   int iResult=0;
-  vector<AliHLTOUTHandlerListEntry>::iterator element=fDataHandlers.begin();
-  while (element!=fDataHandlers.end()) {
+  AliHLTOUTHandlerListEntryVector::iterator element=list.begin();
+  for (; element!=list.end();
+        element++) {
     if (entry==(*element)) break;
-    element++;
   }
-  if (element==fDataHandlers.end()) {
-    fDataHandlers.push_back(entry);
+  if (element==list.end()) {
+    list.push_back(entry);
   } else {
-    
+    element->AddIndex(const_cast<AliHLTOUTHandlerListEntry&>(entry));
+  }
+  return iResult;
+}
+
+int AliHLTOUT::FillHandlerList(AliHLTOUTHandlerListEntryVector& list, AliHLTModuleAgent::AliHLTOUTHandlerType handlerType)
+{
+  // see header file for class documentation
+  int iResult=0;
+  for (iResult=SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec, handlerType);
+       iResult>=0;
+       iResult=SelectNextDataBlock()) {
+    AliHLTComponentDataType dt=kAliHLTVoidDataType;
+    AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
+    GetDataBlockDescription(dt, spec);
+    AliHLTOUTHandler* pHandler=GetHandler();
+    if (!pHandler) {
+      fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "FillHandlerList" , __FILE__ , __LINE__ , 
+                        "missing HLTOUT handler for block of type kChain: agent %s, data type %s, specification %#x, ... skipping data block",
+                        GetAgent()?GetAgent()->GetModuleId():"invalid",
+                        AliHLTComponent::DataType2Text(dt).c_str(), spec);
+    } else {
+      InsertHandler(list, GetDataBlockHandlerDesc());
+    }
+  }
+  // TODO: the return value of SelectFirst/NextDataBlock must be
+  // changed in order to avoid this check
+  if (iResult==-ENOENT) iResult=0;
+
+  return iResult;
+}
+
+int AliHLTOUT::RemoveEmptyDuplicateHandlers(AliHLTOUTHandlerListEntryVector& list)
+{
+  // see header file for class documentation
+  int iResult=0;
+  AliHLTOUTHandlerListEntryVector::iterator element=list.begin();
+  while (element!=list.end()) {
+    if (element->IsEmpty()) {
+      AliHLTOUTHandler* pHandler=*element;
+      AliHLTModuleAgent* pAgent=*element;
+      AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*element;
+      if (FindHandler(list, desc)>=0) {
+       element=list.erase(element);
+       if (pAgent) {
+         pAgent->DeleteOutputHandler(pHandler);
+       }
+       // we are already at the next element
+       continue;
+      }
+    }
+    element++;
   }
   return iResult;
 }
 
+int AliHLTOUT::FindHandler(AliHLTOUTHandlerListEntryVector& list, const AliHLTModuleAgent::AliHLTOUTHandlerDesc desc)
+{
+  // see header file for class documentation
+  for (int i=0; i<(int)list.size(); i++) {
+    if (list[i]==desc) return i;
+  }
+  return -ENOENT;
+}
+
+int AliHLTOUT::InvalidateBlocks(AliHLTOUTHandlerListEntryVector& list)
+{
+  // see header file for class documentation
+  for (AliHLTOUTHandlerListEntryVector::iterator element=list.begin();
+        element!=list.end();
+        element++) {
+    element->InvalidateBlocks();
+  }
+  return 0;
+}
+
+const AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::FindHandlerDesc(AliHLTUInt32_t blockIndex)
+{
+  // see header file for class documentation
+  if (blockIndex<fBlockDescList.size()) {
+    return fBlockDescList[blockIndex].GetHandlerDesc();
+  }
+  return const_cast<AliHLTOUT::AliHLTOUTHandlerListEntry&>(AliHLTOUT::AliHLTOUTHandlerListEntry::fgkVoidHandlerListEntry);
+}
+
+AliHLTOUT::AliHLTOUTHandlerListEntry::AliHLTOUTHandlerListEntry()
+  :
+  fpHandler(NULL),
+  fpHandlerDesc(NULL),
+  fpAgent(NULL),
+  fBlocks()
+{
+  // see header file for class documentation
+}
+
 AliHLTOUT::AliHLTOUTHandlerListEntry::AliHLTOUTHandlerListEntry(AliHLTOUTHandler* pHandler, 
                                                                AliHLTModuleAgent::AliHLTOUTHandlerDesc& handlerDesc,
                                                                AliHLTModuleAgent* pAgent,
                                                                AliHLTUInt32_t index)
   :
   fpHandler(pHandler),
-  fHandlerDesc(handlerDesc),
+  fpHandlerDesc(new AliHLTModuleAgent::AliHLTOUTHandlerDesc),
   fpAgent(pAgent),
   fBlocks()
 {
   // see header file for class documentation
+  *fpHandlerDesc=handlerDesc;
   fBlocks.push_back(index);
 }
 
 AliHLTOUT::AliHLTOUTHandlerListEntry::AliHLTOUTHandlerListEntry(const AliHLTOUTHandlerListEntry& src)
   :
   fpHandler(src.fpHandler),
-  fHandlerDesc(src.fHandlerDesc),
+  fpHandlerDesc(new AliHLTModuleAgent::AliHLTOUTHandlerDesc),
   fpAgent(src.fpAgent),
   fBlocks()
 {
   // see header file for class documentation
+  *fpHandlerDesc=*src.fpHandlerDesc;
   fBlocks.assign(src.fBlocks.begin(), src.fBlocks.end());
 }
 
 AliHLTOUT::AliHLTOUTHandlerListEntry::~AliHLTOUTHandlerListEntry()
 {
   // see header file for class documentation
+  if (fpHandlerDesc) delete fpHandlerDesc;
+  fpHandlerDesc=NULL;
 }
 
 AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::AliHLTOUTHandlerListEntry::operator=(const AliHLTOUTHandlerListEntry& src)
 {
   // see header file for class documentation
   fpHandler=src.fpHandler;
-  fHandlerDesc=src.fHandlerDesc;
+  if (src.fpHandlerDesc)
+    *fpHandlerDesc=*src.fpHandlerDesc;
   fpAgent=src.fpAgent;
   fBlocks.assign(src.fBlocks.begin(), src.fBlocks.end());
   return *this;
@@ -316,14 +471,39 @@ AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::AliHLTOUTHandlerListEntry::oper
 AliHLTUInt32_t AliHLTOUT::AliHLTOUTHandlerListEntry::operator[](int i) const
 {
   // see header file for class documentation
-  return fBlocks.size()>i?fBlocks[i]:AliHLTOUTInvalidIndex;
+  return (int)fBlocks.size()>i?fBlocks[i]:AliHLTOUTInvalidIndex;
 }
 
 bool AliHLTOUT::AliHLTOUTHandlerListEntry::operator==(const AliHLTOUTHandlerListEntry& entry) const
 {
   // see header file for class documentation
-  assert(0); // not yet implemented
-  return false;
+  if (entry.fpHandler!=fpHandler || fpHandler==NULL) return false;
+  assert(entry.fpAgent==fpAgent);
+  if (entry.fpAgent!=fpAgent) return false;
+  return true;
+}
+
+bool AliHLTOUT::AliHLTOUTHandlerListEntry::operator==(const AliHLTModuleAgent::AliHLTOUTHandlerType handlerType) const
+{
+  // see header file for class documentation
+  if (!fpHandlerDesc) return false;
+  return *fpHandlerDesc==handlerType;
+}
+
+bool AliHLTOUT::AliHLTOUTHandlerListEntry::operator==(const AliHLTModuleAgent::AliHLTOUTHandlerDesc desc) const
+{
+  // see header file for class documentation
+  if (!fpHandlerDesc) return false;
+  return *fpHandlerDesc==desc;
+}
+
+void AliHLTOUT::AliHLTOUTHandlerListEntry::AddIndex(AliHLTOUT::AliHLTOUTHandlerListEntry &desc)
+{
+  // see header file for class documentation
+  AliHLTOUTIndexList::iterator element;
+  for (element=desc.fBlocks.begin(); element!=desc.fBlocks.end(); element++) {
+    AddIndex(*element);
+  }  
 }
 
 void AliHLTOUT::AliHLTOUTHandlerListEntry::AddIndex(AliHLTUInt32_t index)
@@ -331,3 +511,308 @@ void AliHLTOUT::AliHLTOUTHandlerListEntry::AddIndex(AliHLTUInt32_t index)
   // see header file for class documentation
   fBlocks.push_back(index);
 }
+
+bool AliHLTOUT::AliHLTOUTHandlerListEntry::HasIndex(AliHLTUInt32_t index) const
+{
+  // see header file for class documentation
+  AliHLTOUTIndexList::iterator element;
+  for (unsigned int i=0; i<fBlocks.size(); i++) {
+    if (fBlocks[i]==index) return true;
+  }
+  return false;
+}
+
+const AliHLTOUT::AliHLTOUTHandlerListEntry AliHLTOUT::AliHLTOUTHandlerListEntry::fgkVoidHandlerListEntry;
+
+AliHLTUInt64_t AliHLTOUT::ByteSwap64(AliHLTUInt64_t src)
+{
+  // see header file for class documentation
+  return ((src & 0xFFULL) << 56) | 
+    ((src & 0xFF00ULL) << 40) | 
+    ((src & 0xFF0000ULL) << 24) | 
+    ((src & 0xFF000000ULL) << 8) | 
+    ((src & 0xFF00000000ULL) >> 8) | 
+    ((src & 0xFF0000000000ULL) >> 24) | 
+    ((src & 0xFF000000000000ULL) >>  40) | 
+    ((src & 0xFF00000000000000ULL) >> 56);
+}
+
+AliHLTUInt32_t AliHLTOUT::ByteSwap32(AliHLTUInt32_t src)
+{
+  // see header file for class documentation
+  return ((src & 0xFFULL) << 24) | 
+    ((src & 0xFF00ULL) << 8) | 
+    ((src & 0xFF0000ULL) >> 8) | 
+    ((src & 0xFF000000ULL) >> 24);
+}
+
+AliHLTOUT* AliHLTOUT::New(AliRawReader* pRawReader)
+{
+  // see header file for class documentation
+  AliHLTOUT* instance=New("AliHLTOUTRawReader");
+  if (instance) {
+    instance->SetParam(pRawReader);
+  }
+  return instance;
+}
+
+AliHLTOUT* AliHLTOUT::New(TTree* pDigitTree, int event)
+{
+  // see header file for class documentation
+  AliHLTOUT* instance=New("AliHLTOUTDigitReader");
+  if (instance) {
+    instance->SetParam(pDigitTree, event);
+  }
+  return instance;
+}
+
+AliHLTOUT* AliHLTOUT::New(const char* classname)
+{
+  // see header file for class documentation
+  int iLibResult=0;
+  AliHLTOUT* instance=NULL;
+  AliHLTLogging log;
+  TClass* pCl=NULL;
+  ROOT::NewFunc_t pNewFunc=NULL;
+  do {
+    pCl=TClass::GetClass(classname);
+  } while (!pCl && (iLibResult=gSystem->Load("libHLTrec.so"))==0);
+  if (iLibResult>=0) {
+    if (pCl && (pNewFunc=pCl->GetNew())!=NULL) {
+      void* p=(*pNewFunc)(NULL);
+      if (p) {
+       instance=reinterpret_cast<AliHLTOUT*>(p);
+       if (!instance) {
+         log.Logging(kHLTLogError, "AliHLTOUT::New", "HLTOUT handling", "type cast to AliHLTOUT instance failed");
+       }
+      } else {
+       log.Logging(kHLTLogError, "AliHLTOUT::New", "HLTOUT handling", "can not create AliHLTOUT instance from class descriptor");
+      }
+    } else {
+      log.Logging(kHLTLogError, "AliHLTOUT::New", "HLTOUT handling", "can not find AliHLTOUT class descriptor");
+    }
+  } else {
+    log.Logging(kHLTLogError, "AliHLTOUT::New", "HLTOUT handling", "can not load libHLTrec library");
+  }
+  return instance;
+}
+
+void AliHLTOUT::Delete(AliHLTOUT* pInstance)
+{
+  // see header file for class documentation
+  if (!pInstance) return;
+  if (pInstance==fgGlobalInstance) return;
+
+  // check if the library is still there in order to have the
+  // destructor available
+  TClass* pCl1=TClass::GetClass("AliHLTOUTRawReader");
+  TClass* pCl2=TClass::GetClass("AliHLTOUTDigitReader");
+  if (!pCl1 && !pCl2) {
+    AliHLTLogging log;
+    log.Logging(kHLTLogError, "AliHLTOUT::Delete", "HLTOUT handling", "potential memory leak: libHLTrec library not available, skipping destruction %p", pInstance);    
+    return;
+  }
+
+  delete pInstance;  
+}
+
+void AliHLTOUT::SetParam(AliRawReader* /*pRawReader*/)
+{
+  // see header file for class documentation
+  // default implementation, we should never get here
+  // this function can only be called from the class itsself and
+  // is intended to be used with the New functions. If we get into
+  // the default implementation there is a class mismatch.
+  assert(0);
+  fLog.LoggingVarargs(kHLTLogFatal, "AliHLTOUT", "SetParam" , __FILE__ , __LINE__ , "severe internal error: class mismatch");
+}
+
+void AliHLTOUT::SetParam(TTree* /*pDigitTree*/, int /*event*/)
+{
+  // see header file for class documentation
+  // default implementation, we should never get here
+  // this function can only be called from the class itsself and
+  // is intended to be used with the New functions. If we get into
+  // the default implementation there is a class mismatch.
+  assert(0);
+  fLog.LoggingVarargs(kHLTLogFatal, "AliHLTOUT", "SetParam" , __FILE__ , __LINE__ , "severe internal error: class mismatch");
+}
+
+int AliHLTOUT::SelectDataBlock()
+{
+  // see header file for class documentation
+  int iResult=0;
+  if (fCurrent>=fBlockDescList.size()) return 0;
+  fBlockDescList[fCurrent].Select(true);
+  EnableBlockSelection();
+  return iResult;
+}
+
+int AliHLTOUT::SelectDataBlocks(const AliHLTOUTHandlerListEntry* pHandlerDesc)
+{
+  // see header file for class documentation
+  int iResult=0;
+  if (!pHandlerDesc) return 0;
+
+  AliHLTOUTBlockDescriptorVector::iterator element;
+  for (AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
+       block!=fBlockDescList.end();
+       block++) {
+    if (block->GetHandlerDesc()==*pHandlerDesc && pHandlerDesc->HasIndex(block->GetIndex()))
+      block->Select(true);
+    else
+      block->Select(false);
+  }
+  EnableBlockSelection();
+  
+  return iResult;
+}
+
+int AliHLTOUT::EnableBlockSelection()
+{
+  // see header file for class documentation
+  SetStatusFlag(kBlockSelection);
+  return 0;
+}
+
+int AliHLTOUT::DisableBlockSelection()
+{
+  // see header file for class documentation
+  ClearStatusFlag(kBlockSelection);
+  return 0;
+}
+
+int AliHLTOUT::ResetBlockSelection()
+{
+  // see header file for class documentation
+  for (AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
+       block!=fBlockDescList.end();
+       block++) {
+    block->Select(false);
+  }
+  return 0;
+}
+
+int AliHLTOUT::MarkDataBlockProcessed()
+{
+  // see header file for class documentation
+  int iResult=0;
+  if (fCurrent>=fBlockDescList.size()) return 0;
+  fBlockDescList[fCurrent].MarkProcessed();
+  return iResult;
+}
+
+int AliHLTOUT::MarkDataBlocksProcessed(const AliHLTOUTHandlerListEntry* pHandlerDesc)
+{
+  // see header file for class documentation
+  int iResult=0;
+  if (!pHandlerDesc) return 0;
+
+  AliHLTOUTBlockDescriptorVector::iterator element;
+  for (AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
+       block!=fBlockDescList.end();
+       block++) {
+    if (block->GetHandlerDesc()==*pHandlerDesc && pHandlerDesc->HasIndex(block->GetIndex()))
+      block->MarkProcessed();
+  }
+  
+  return iResult;
+}
+
+int AliHLTOUT::AddSubCollection(AliHLTOUT* pCollection)
+{
+  // see header file for class documentation
+  int iResult=0;
+  if (!pCollection) return 0;
+
+  SetStatusFlag(kCollecting);  
+  int index=-1;
+  for (index=pCollection->SelectFirstDataBlock();
+       index>=0;
+       index=pCollection->SelectNextDataBlock()) {
+    AliHLTComponentDataType dt=kAliHLTVoidDataType;
+    AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
+    pCollection->GetDataBlockDescription(dt, spec);  
+    AliHLTOUTBlockDescriptor desc(dt, spec, index, pCollection);
+    AddBlockDescriptor(desc);
+    iResult++;
+  }
+  if (iResult>0) {
+    if (CheckStatusFlag(kIsSubCollection)) {
+      fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "AddSubCollection" , __FILE__ , __LINE__ , "HLTOUT object %p has already been added as sub-collection", pCollection);
+    } else {
+      pCollection->SetStatusFlag(kIsSubCollection);
+    }
+  }
+  ClearStatusFlag(kCollecting);  
+
+  return iResult;
+}
+
+int AliHLTOUT::ReleaseSubCollection(AliHLTOUT* pCollection)
+{
+  // see header file for class documentation
+  int iResult=0;
+  if (!pCollection) return 0;
+
+  AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
+  while (block!=fBlockDescList.end()) {
+    if ((*block)==pCollection) {
+      block=fBlockDescList.erase(block);
+      continue;
+    }
+    block++;
+  }
+  pCollection->ClearStatusFlag(kIsSubCollection);
+
+  return iResult;
+}
+
+int AliHLTOUT::Reset()
+{
+  // see header file for class documentation
+  int iResult=0;
+  AliHLTOUTPVector subCollections;
+  AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
+  while (block!=fBlockDescList.end()) {
+    if (!((*block)==this)) {
+      AliHLTOUTPVector::iterator collection=subCollections.begin();
+      for (; collection!=subCollections.end(); collection++)
+       if((*block)==*collection) break;
+      if (collection==subCollections.end())
+       subCollections.push_back(block->GetCollection());
+    }
+    block=fBlockDescList.erase(block);
+  }
+
+  for (AliHLTOUTPVector::iterator collection=subCollections.begin(); 
+       collection!=subCollections.end(); collection++) {
+    (*collection)->Reset();
+    (*collection)->ClearStatusFlag(kIsSubCollection);
+  }
+
+  ResetInput();
+
+  return iResult;
+}
+
+int AliHLTOUT::ResetInput()
+{
+  // default implementation, nothing to do
+  return 0;
+}
+
+const AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::AliHLTOUTBlockDescriptor::GetHandlerDesc()
+{
+  // see header file for class documentation
+  if (fpCollection) {
+    AliHLTOUTHandlerListEntryVector::iterator element=fpCollection->fDataHandlers.begin();
+    while (element!=fpCollection->fDataHandlers.end()) {
+      if (element->HasIndex(GetIndex())) {
+       return *element;
+      }
+      element++;
+    }
+  }
+  return const_cast<AliHLTOUT::AliHLTOUTHandlerListEntry&>(AliHLTOUT::AliHLTOUTHandlerListEntry::fgkVoidHandlerListEntry);
+}