]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/BASE/AliHLTOUT.cxx
adding virtual method GetMaxSignal; including Qmax and sigmas in filling of tree
[u/mrichter/AliRoot.git] / HLT / BASE / AliHLTOUT.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 //*                  for The ALICE HLT Project.                            *
9 //*                                                                        *
10 //* Permission to use, copy, modify and distribute this software and its   *
11 //* documentation strictly for non-commercial purposes is hereby granted   *
12 //* without fee, provided that the above copyright notice appears in all   *
13 //* copies and that both the copyright notice and this permission notice   *
14 //* appear in the supporting documentation. The authors make no claims     *
15 //* about the suitability of this software for any purpose. It is          *
16 //* provided "as is" without express or implied warranty.                  *
17 //**************************************************************************
18
19 /** @file   AliHLTOUT.cxx
20     @author Matthias Richter
21     @date   
22     @brief  The control class for HLTOUT data.
23 */
24
25 #include <cerrno>
26 #include <cassert>
27 #include "AliHLTOUT.h"
28 #include "AliHLTMessage.h"
29 #include "AliHLTMisc.h"
30 #include "TSystem.h"
31 #include "TClass.h"
32 #include "TROOT.h"
33
34 /** ROOT macro for the implementation of ROOT specific class methods */
35 ClassImp(AliHLTOUT)
36
37 AliHLTOUT::AliHLTOUT()
38   :
39   fSearchDataType(kAliHLTVoidDataType),
40   fSearchSpecification(kAliHLTVoidDataSpec),
41   fSearchHandlerType(AliHLTModuleAgent::kUnknownOutput),
42   fFlags(kSkipProcessed),
43   fBlockDescList(),
44   fCurrent(0),
45   fpBuffer(NULL),
46   fDataHandlers(),
47   fbVerbose(true),
48   fLog()
49   , fpDataObject(NULL)
50   , fpObjectBuffer(NULL)
51   , fObjectBufferSize(0)
52   , fCurrentEventId(kAliHLTVoidEventID)
53 {
54   // see header file for class documentation
55   // or
56   // refer to README to build package
57   // or
58   // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
59 }
60
61 AliHLTOUT::~AliHLTOUT()
62 {
63   // see header file for class documentation
64   if (CheckStatusFlag(kIsSubCollection)) {
65     fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "~AliHLTOUT" , __FILE__ , __LINE__ , "severe internal error: collection has not been released, potential crash due to invalid pointer");
66   }
67
68   if (fpDataObject) {
69     fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "GetDataObject" , __FILE__ , __LINE__ , "data object has not been released, potential memory leak");
70   }
71   fpDataObject=NULL;
72 }
73 AliHLTOUT* AliHLTOUT::fgGlobalInstance=NULL;
74
75 int AliHLTOUT::Init()
76 {
77   // see header file for class documentation
78   int iResult=0;
79
80   // ignore if already initialized
81   if (fBlockDescList.size()>0) {
82     return 0;
83   }
84
85   SetStatusFlag(kCollecting);
86   if ((iResult=GenerateIndex())>=0) {
87     if ((iResult=InitHandlers())>=0) {
88     }
89   }
90   ClearStatusFlag(kCollecting);
91   return iResult;
92 }
93
94 int AliHLTOUT::GetNofDataBlocks()
95 {
96   // see header file for class documentation
97   return fBlockDescList.size();
98 }
99
100 int AliHLTOUT::SelectFirstDataBlock(AliHLTComponentDataType dt, AliHLTUInt32_t spec,
101                                     AliHLTModuleAgent::AliHLTOUTHandlerType handlerType,
102                                     bool skipProcessed)
103 {
104   // see header file for class documentation
105   fCurrent=0;
106   fSearchDataType=dt;
107   fSearchSpecification=spec;
108   fSearchHandlerType=handlerType;
109   if (skipProcessed) SetStatusFlag(kSkipProcessed);
110   else ClearStatusFlag(kSkipProcessed);
111   return FindAndSelectDataBlock();
112 }
113
114 int AliHLTOUT::SelectNextDataBlock()
115 {
116   // see header file for class documentation
117   if (fCurrent>=fBlockDescList.size()) return -ENOENT;
118   fCurrent++;
119   return FindAndSelectDataBlock();
120 }
121
122 int AliHLTOUT::FindAndSelectDataBlock()
123 {
124   // see header file for class documentation
125   if (CheckStatusFlag(kLocked)) return -EPERM;
126   int iResult=-ENOENT;
127   while (fCurrent<fBlockDescList.size() && iResult==-ENOENT) {
128     if (fBlockDescList[fCurrent]==fSearchDataType &&
129         (fSearchSpecification==kAliHLTVoidDataSpec || fBlockDescList[fCurrent]==fSearchSpecification) &&
130         (fSearchHandlerType==AliHLTModuleAgent::kUnknownOutput || FindHandlerDesc(fCurrent)==fSearchHandlerType) &&
131         (!CheckStatusFlag(kBlockSelection) || fBlockDescList[fCurrent].IsSelected()) &&
132         (!CheckStatusFlag(kSkipProcessed) || !fBlockDescList[fCurrent].IsProcessed())) {
133       iResult=fBlockDescList[fCurrent].GetIndex();
134       // TODO: check the byte order on the current system and the byte order of the
135       // data block, print warning when mismatch and user did not check
136       //AliHLTOUTByteOrder blockBO=CheckByteOrder();
137       CheckByteOrder();
138       /*
139         if (blockBO!=fByteOrder) {
140         SetStatusFlag(kByteOrderWarning);
141
142         }
143        */
144       ClearStatusFlag(kByteOrderChecked);
145
146       // TODO: check the alignment on the current system and the alignment of the
147       // data block, print warning when mismatch and user did not check
148       ClearStatusFlag(kAlignmentChecked);
149
150       break;
151     }
152     fCurrent++;
153   }
154   return iResult;
155 }
156
157 int AliHLTOUT::GetDataBlockDescription(AliHLTComponentDataType& dt, AliHLTUInt32_t& spec)
158 {
159   // see header file for class documentation
160   int iResult=-ENOENT;
161   if (fCurrent<fBlockDescList.size()) {
162     iResult=0;
163     dt=fBlockDescList[fCurrent];
164     spec=fBlockDescList[fCurrent];
165   }
166   return iResult;
167 }
168
169 const AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::GetDataBlockHandlerDesc()
170 {
171   // see header file for class documentation
172   return FindHandlerDesc(fCurrent);
173 }
174
175 AliHLTModuleAgent::AliHLTOUTHandlerType AliHLTOUT::GetDataBlockHandlerType()
176 {
177   // see header file for class documentation
178   AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=FindHandlerDesc(fCurrent);
179   AliHLTModuleAgent::AliHLTOUTHandlerType type=desc;
180   return type;
181 }
182
183 AliHLTUInt32_t AliHLTOUT::GetDataBlockIndex()
184 {
185   // see header file for class documentation
186   if (fCurrent>=fBlockDescList.size()) return AliHLTOUTInvalidIndex;
187   return fBlockDescList[fCurrent].GetIndex();
188 }
189
190 int AliHLTOUT::GetDataBuffer(const AliHLTUInt8_t* &pBuffer, AliHLTUInt32_t& size)
191 {
192   // see header file for class documentation
193   int iResult=-ENOENT;
194   pBuffer=NULL;
195   size=0;
196   if (fCurrent<fBlockDescList.size()) {
197     if ((iResult=fBlockDescList[fCurrent].GetDataBuffer(pBuffer, size))>=0) {
198       fpBuffer=pBuffer;
199     }
200   }
201   return iResult;  
202 }
203
204 int AliHLTOUT::ReleaseDataBuffer(const AliHLTUInt8_t* pBuffer)
205 {
206   // see header file for class documentation
207   int iResult=0;
208   if (pBuffer==fpBuffer) {
209     fpBuffer=NULL;
210   } else {
211     fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "ReleaseDataBuffer" , __FILE__ , __LINE__ , "buffer %p does not match the provided one %p", pBuffer, fpBuffer);
212   }
213   return iResult;  
214 }
215
216 AliHLTModuleAgent* AliHLTOUT::GetAgent()
217 {
218   // see header file for class documentation
219   AliHLTModuleAgent* pAgent=NULL;
220   pAgent=FindHandlerDesc(fCurrent);
221   return pAgent;
222 }
223
224 AliHLTOUTHandler* AliHLTOUT::GetHandler()
225 {
226   // see header file for class documentation
227   AliHLTOUTHandler* pHandler=NULL;
228   pHandler=FindHandlerDesc(fCurrent);
229   return pHandler;
230 }
231
232 int AliHLTOUT::WriteESD(const AliHLTUInt8_t* /*pBuffer*/, AliHLTUInt32_t /*size*/, AliHLTComponentDataType /*dt*/, AliESDEvent* /*tgtesd*/) const
233 {
234   // see header file for class documentation
235   fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "WriteESD" , __FILE__ , __LINE__ , "method not implemented in base class");
236   return -ENOSYS;
237 }
238
239 int AliHLTOUT::AddBlockDescriptor(const AliHLTOUTBlockDescriptor desc)
240 {
241   // see header file for class documentation
242   if (!CheckStatusFlag(kCollecting)) return -EPERM;
243   int iResult=0;
244   fBlockDescList.push_back(desc);
245   return iResult;  
246 }
247
248 AliHLTOUT::AliHLTOUTByteOrder AliHLTOUT::CheckByteOrder()
249 {
250   // see header file for class documentation
251   if (fCurrent<fBlockDescList.size()) {
252     SetStatusFlag(kByteOrderChecked);
253     AliHLTOUT::AliHLTOUTByteOrder order=CheckBlockByteOrder(fBlockDescList[fCurrent].GetIndex());
254     return order;
255   }
256   return kInvalidByteOrder;
257 }
258
259 int AliHLTOUT::CheckAlignment(AliHLTOUT::AliHLTOUTDataType type)
260 {
261   // see header file for class documentation
262   if (fCurrent<fBlockDescList.size()) {
263     SetStatusFlag(kAlignmentChecked);
264     int alignment=CheckBlockAlignment(fBlockDescList[fCurrent].GetIndex(), type);
265     return alignment;
266   }
267   return -ENOENT;
268 }
269
270 int AliHLTOUT::InitHandlers()
271 {
272   // see header file for class documentation
273   int iResult=0;
274   AliHLTOUTIndexList remnants;
275   int iCount=0;
276   for (int havedata=SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec); havedata>=0; havedata=SelectNextDataBlock()) {
277     iCount++;
278     remnants.push_back(GetDataBlockIndex());
279     AliHLTComponentDataType dt=kAliHLTVoidDataType;
280     AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
281     if (GetDataBlockDescription(dt, spec)<0) break;
282     bool bHaveHandler=false;
283     for (AliHLTModuleAgent* pAgent=AliHLTModuleAgent::GetFirstAgent(); pAgent && iResult>=0; pAgent=AliHLTModuleAgent::GetNextAgent()) {
284       AliHLTModuleAgent::AliHLTOUTHandlerDesc handlerDesc;
285       if (pAgent->GetHandlerDescription(dt, spec, handlerDesc)>0) {
286         AliHLTOUTHandlerListEntry entry(pAgent->GetOutputHandler(dt, spec), handlerDesc, pAgent, GetDataBlockIndex());
287         InsertHandler(fDataHandlers, entry);
288         remnants.pop_back();
289         bHaveHandler=true;
290         break;
291       }
292     }
293     if (!bHaveHandler && (dt==kAliHLTDataTypeESDObject || dt==kAliHLTDataTypeESDTree)) {
294       // ESDs are handled by the framework
295       remnants.pop_back();
296     }
297   }
298
299   // warning if some of the data blocks are not selected by the kAliHLTAnyDataType
300   // criterion
301   if (GetNofDataBlocks()>iCount) {
302     fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "InitHandlers" , __FILE__ , __LINE__ , "incomplete data type in %d out of %d data block(s)", GetNofDataBlocks()-iCount, GetNofDataBlocks());
303   }
304
305   // warning if handler not found
306   if (remnants.size()>0) {
307     fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "InitHandlers" , __FILE__ , __LINE__ , "no handlers found for %d data blocks out of %d", remnants.size(), iCount);
308     AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
309     for (AliHLTOUTIndexList::iterator element=remnants.begin();
310          element!=remnants.end() && block!=fBlockDescList.end();
311          element++) {
312       for (int trials=0; trials<2; trials++) {
313         do {
314           // we start searching the index from the current position in the block list
315           if ((*block).GetIndex()==*element) break;
316         } while ((++block)!=fBlockDescList.end());
317         if (block==fBlockDescList.end()) {
318           // rewind and try again
319           block=fBlockDescList.begin();
320         }
321       }
322       assert(block!=fBlockDescList.end());
323     }
324   }
325   return iResult;
326 }
327
328 int AliHLTOUT::InsertHandler(AliHLTOUTHandlerListEntryVector& list, const AliHLTOUTHandlerListEntry &entry)
329 {
330   // see header file for class documentation
331   int iResult=0;
332   AliHLTOUTHandlerListEntryVector::iterator element=list.begin();
333   for (; element!=list.end();
334          element++) {
335     if (entry==(*element)) break;
336   }
337   if (element==list.end()) {
338     list.push_back(entry);
339   } else {
340     element->AddIndex(const_cast<AliHLTOUTHandlerListEntry&>(entry));
341   }
342   return iResult;
343 }
344
345 int AliHLTOUT::FillHandlerList(AliHLTOUTHandlerListEntryVector& list, AliHLTModuleAgent::AliHLTOUTHandlerType handlerType)
346 {
347   // see header file for class documentation
348   int iResult=0;
349   for (iResult=SelectFirstDataBlock(kAliHLTAnyDataType, kAliHLTVoidDataSpec, handlerType);
350        iResult>=0;
351        iResult=SelectNextDataBlock()) {
352     AliHLTComponentDataType dt=kAliHLTVoidDataType;
353     AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
354     GetDataBlockDescription(dt, spec);
355     AliHLTOUTHandler* pHandler=GetHandler();
356     if (!pHandler) {
357       fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "FillHandlerList" , __FILE__ , __LINE__ , 
358                          "missing HLTOUT handler for block of type kChain: agent %s, data type %s, specification %#x, ... skipping data block",
359                          GetAgent()?GetAgent()->GetModuleId():"invalid",
360                          AliHLTComponent::DataType2Text(dt).c_str(), spec);
361     } else {
362       InsertHandler(list, GetDataBlockHandlerDesc());
363     }
364   }
365   // TODO: the return value of SelectFirst/NextDataBlock must be
366   // changed in order to avoid this check
367   if (iResult==-ENOENT) iResult=0;
368
369   return iResult;
370 }
371
372 int AliHLTOUT::RemoveEmptyDuplicateHandlers(AliHLTOUTHandlerListEntryVector& list)
373 {
374   // see header file for class documentation
375   int iResult=0;
376   AliHLTOUTHandlerListEntryVector::iterator element=list.begin();
377   while (element!=list.end()) {
378     if (element->IsEmpty()) {
379       AliHLTOUTHandler* pHandler=*element;
380       AliHLTModuleAgent* pAgent=*element;
381       AliHLTModuleAgent::AliHLTOUTHandlerDesc desc=*element;
382       if (FindHandler(list, desc)>=0) {
383         element=list.erase(element);
384         if (pAgent) {
385           pAgent->DeleteOutputHandler(pHandler);
386         }
387         // we are already at the next element
388         continue;
389       }
390     }
391     element++;
392   }
393   return iResult;
394 }
395
396 int AliHLTOUT::FindHandler(AliHLTOUTHandlerListEntryVector& list, const AliHLTModuleAgent::AliHLTOUTHandlerDesc desc)
397 {
398   // see header file for class documentation
399   for (int i=0; i<(int)list.size(); i++) {
400     if (list[i]==desc) return i;
401   }
402   return -ENOENT;
403 }
404
405 int AliHLTOUT::InvalidateBlocks(AliHLTOUTHandlerListEntryVector& list)
406 {
407   // see header file for class documentation
408   for (AliHLTOUTHandlerListEntryVector::iterator element=list.begin();
409          element!=list.end();
410          element++) {
411     element->InvalidateBlocks();
412   }
413   return 0;
414 }
415
416 const AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::FindHandlerDesc(AliHLTUInt32_t blockIndex)
417 {
418   // see header file for class documentation
419   if (blockIndex<fBlockDescList.size()) {
420     return fBlockDescList[blockIndex].GetHandlerDesc();
421   }
422   return const_cast<AliHLTOUT::AliHLTOUTHandlerListEntry&>(AliHLTOUT::AliHLTOUTHandlerListEntry::fgkVoidHandlerListEntry);
423 }
424
425 AliHLTOUT::AliHLTOUTHandlerListEntry::AliHLTOUTHandlerListEntry()
426   :
427   fpHandler(NULL),
428   fpHandlerDesc(NULL),
429   fpAgent(NULL),
430   fBlocks()
431 {
432   // see header file for class documentation
433 }
434
435 AliHLTOUT::AliHLTOUTHandlerListEntry::AliHLTOUTHandlerListEntry(AliHLTOUTHandler* pHandler, 
436                                                                 AliHLTModuleAgent::AliHLTOUTHandlerDesc& handlerDesc,
437                                                                 AliHLTModuleAgent* pAgent,
438                                                                 AliHLTUInt32_t index)
439   :
440   fpHandler(pHandler),
441   fpHandlerDesc(new AliHLTModuleAgent::AliHLTOUTHandlerDesc),
442   fpAgent(pAgent),
443   fBlocks()
444 {
445   // see header file for class documentation
446   *fpHandlerDesc=handlerDesc;
447   fBlocks.push_back(index);
448 }
449
450 AliHLTOUT::AliHLTOUTHandlerListEntry::AliHLTOUTHandlerListEntry(const AliHLTOUTHandlerListEntry& src)
451   :
452   fpHandler(src.fpHandler),
453   fpHandlerDesc(new AliHLTModuleAgent::AliHLTOUTHandlerDesc),
454   fpAgent(src.fpAgent),
455   fBlocks()
456 {
457   // see header file for class documentation
458   *fpHandlerDesc=*src.fpHandlerDesc;
459   fBlocks.assign(src.fBlocks.begin(), src.fBlocks.end());
460 }
461
462 AliHLTOUT::AliHLTOUTHandlerListEntry::~AliHLTOUTHandlerListEntry()
463 {
464   // see header file for class documentation
465   if (fpHandlerDesc) delete fpHandlerDesc;
466   fpHandlerDesc=NULL;
467 }
468
469 AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::AliHLTOUTHandlerListEntry::operator=(const AliHLTOUTHandlerListEntry& src)
470 {
471   // see header file for class documentation
472   fpHandler=src.fpHandler;
473   if (src.fpHandlerDesc)
474     *fpHandlerDesc=*src.fpHandlerDesc;
475   fpAgent=src.fpAgent;
476   fBlocks.assign(src.fBlocks.begin(), src.fBlocks.end());
477   return *this;
478 }
479
480 AliHLTUInt32_t AliHLTOUT::AliHLTOUTHandlerListEntry::operator[](int i) const
481 {
482   // see header file for class documentation
483   return (int)fBlocks.size()>i?fBlocks[i]:AliHLTOUTInvalidIndex;
484 }
485
486 bool AliHLTOUT::AliHLTOUTHandlerListEntry::operator==(const AliHLTOUTHandlerListEntry& entry) const
487 {
488   // see header file for class documentation
489   if (entry.fpHandler!=fpHandler || fpHandler==NULL) return false;
490   assert(entry.fpAgent==fpAgent);
491   if (entry.fpAgent!=fpAgent) return false;
492   return true;
493 }
494
495 bool AliHLTOUT::AliHLTOUTHandlerListEntry::operator==(const AliHLTModuleAgent::AliHLTOUTHandlerType handlerType) const
496 {
497   // see header file for class documentation
498   if (!fpHandlerDesc) return false;
499   return *fpHandlerDesc==handlerType;
500 }
501
502 bool AliHLTOUT::AliHLTOUTHandlerListEntry::operator==(const AliHLTModuleAgent::AliHLTOUTHandlerDesc desc) const
503 {
504   // see header file for class documentation
505   if (!fpHandlerDesc) return false;
506   return *fpHandlerDesc==desc;
507 }
508
509 void AliHLTOUT::AliHLTOUTHandlerListEntry::AddIndex(AliHLTOUT::AliHLTOUTHandlerListEntry &desc)
510 {
511   // see header file for class documentation
512   AliHLTOUTIndexList::iterator element;
513   for (element=desc.fBlocks.begin(); element!=desc.fBlocks.end(); element++) {
514     AddIndex(*element);
515   }  
516 }
517
518 void AliHLTOUT::AliHLTOUTHandlerListEntry::AddIndex(AliHLTUInt32_t index)
519 {
520   // see header file for class documentation
521   fBlocks.push_back(index);
522 }
523
524 bool AliHLTOUT::AliHLTOUTHandlerListEntry::HasIndex(AliHLTUInt32_t index) const
525 {
526   // see header file for class documentation
527   AliHLTOUTIndexList::iterator element;
528   for (unsigned int i=0; i<fBlocks.size(); i++) {
529     if (fBlocks[i]==index) return true;
530   }
531   return false;
532 }
533
534 const AliHLTOUT::AliHLTOUTHandlerListEntry AliHLTOUT::AliHLTOUTHandlerListEntry::fgkVoidHandlerListEntry;
535
536 AliHLTUInt64_t AliHLTOUT::ByteSwap64(AliHLTUInt64_t src)
537 {
538   // see header file for class documentation
539   return ((src & 0xFFULL) << 56) | 
540     ((src & 0xFF00ULL) << 40) | 
541     ((src & 0xFF0000ULL) << 24) | 
542     ((src & 0xFF000000ULL) << 8) | 
543     ((src & 0xFF00000000ULL) >> 8) | 
544     ((src & 0xFF0000000000ULL) >> 24) | 
545     ((src & 0xFF000000000000ULL) >>  40) | 
546     ((src & 0xFF00000000000000ULL) >> 56);
547 }
548
549 AliHLTUInt32_t AliHLTOUT::ByteSwap32(AliHLTUInt32_t src)
550 {
551   // see header file for class documentation
552   return ((src & 0xFFULL) << 24) | 
553     ((src & 0xFF00ULL) << 8) | 
554     ((src & 0xFF0000ULL) >> 8) | 
555     ((src & 0xFF000000ULL) >> 24);
556 }
557
558 AliHLTOUT* AliHLTOUT::New(AliRawReader* pRawReader)
559 {
560   // see header file for class documentation
561   AliHLTOUT* instance=AliHLTMisc::LoadInstance((AliHLTOUT*)0, "AliHLTOUTRawReader", "libHLTrec.so");
562   if (instance) {
563     instance->SetParam(pRawReader);
564   }
565   return instance;
566 }
567
568 AliHLTOUT* AliHLTOUT::New(TTree* pDigitTree, int event)
569 {
570   // see header file for class documentation
571   AliHLTOUT* instance=AliHLTMisc::LoadInstance((AliHLTOUT*)0, "AliHLTOUTDigitReader", "libHLTrec.so");
572   if (instance) {
573     instance->SetParam(pDigitTree, event);
574   }
575   return instance;
576 }
577
578 AliHLTOUT* AliHLTOUT::New(const char* filename, int event)
579 {
580   // see header file for class documentation
581   AliHLTOUT* instance=AliHLTMisc::LoadInstance((AliHLTOUT*)0, "AliHLTOUTDigitReader", "libHLTrec.so");
582   if (instance) {
583     instance->SetParam(filename, event);
584   }
585   return instance;
586 }
587
588 void AliHLTOUT::Delete(AliHLTOUT* pInstance)
589 {
590   // see header file for class documentation
591   if (!pInstance) return;
592   if (pInstance==fgGlobalInstance) return;
593
594   // check if the library is still there in order to have the
595   // destructor available
596   TClass* pCl1=TClass::GetClass("AliHLTOUTRawReader");
597   TClass* pCl2=TClass::GetClass("AliHLTOUTDigitReader");
598   if (!pCl1 && !pCl2) {
599     AliHLTLogging log;
600     log.Logging(kHLTLogError, "AliHLTOUT::Delete", "HLTOUT handling", "potential memory leak: libHLTrec library not available, skipping destruction %p", pInstance);    
601     return;
602   }
603
604   delete pInstance;  
605 }
606
607 void AliHLTOUT::SetParam(AliRawReader* /*pRawReader*/)
608 {
609   // see header file for class documentation
610   // default implementation, we should never get here
611   // this function can only be called from the class itsself and
612   // is intended to be used with the New functions. If we get into
613   // the default implementation there is a class mismatch.
614   assert(0);
615   fLog.LoggingVarargs(kHLTLogFatal, "AliHLTOUT", "SetParam" , __FILE__ , __LINE__ , "severe internal error: class mismatch");
616 }
617
618 void AliHLTOUT::SetParam(TTree* /*pDigitTree*/, int /*event*/)
619 {
620   // see header file for class documentation
621   // default implementation, we should never get here
622   // this function can only be called from the class itsself and
623   // is intended to be used with the New functions. If we get into
624   // the default implementation there is a class mismatch.
625   assert(0);
626   fLog.LoggingVarargs(kHLTLogFatal, "AliHLTOUT", "SetParam" , __FILE__ , __LINE__ , "severe internal error: class mismatch");
627 }
628
629 void AliHLTOUT::SetParam(const char* /*filename*/, int /*event*/)
630 {
631   // see header file for class documentation
632   // default implementation, we should never get here
633   // this function can only be called from the class itsself and
634   // is intended to be used with the New functions. If we get into
635   // the default implementation there is a class mismatch.
636   assert(0);
637   fLog.LoggingVarargs(kHLTLogFatal, "AliHLTOUT", "SetParam" , __FILE__ , __LINE__ , "severe internal error: class mismatch");
638 }
639
640 int AliHLTOUT::SelectDataBlock()
641 {
642   // see header file for class documentation
643   int iResult=0;
644   if (fCurrent>=fBlockDescList.size()) return 0;
645   fBlockDescList[fCurrent].Select(true);
646   EnableBlockSelection();
647   return iResult;
648 }
649
650 int AliHLTOUT::SelectDataBlocks(const AliHLTOUTHandlerListEntry* pHandlerEntry)
651 {
652   // see header file for class documentation
653   int iResult=0;
654   if (!pHandlerEntry) return 0;
655
656   AliHLTModuleAgent* pAgent=*pHandlerEntry;
657   AliHLTLogging log;
658   log.Logging(kHLTLogDebug, "AliHLTOUT::SelectDataBlocks", "HLTOUT handling", "selecting blocks for handler %s", pAgent->GetModuleId());
659   AliHLTOUTBlockDescriptorVector::iterator element;
660   for (AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
661        block!=fBlockDescList.end();
662        block++) {
663     if (block->GetHandlerDesc()==*pHandlerEntry && pHandlerEntry->HasIndex(block->GetIndex())) {
664       block->Select(true);
665       log.Logging(kHLTLogDebug, "AliHLTOUT::SelectDataBlocks", "HLTOUT handling", "   select block %s", AliHLTComponent::DataType2Text(*block).c_str());
666     } else {
667       log.Logging(kHLTLogDebug, "AliHLTOUT::SelectDataBlocks", "HLTOUT handling", "   skip block %s", AliHLTComponent::DataType2Text(*block).c_str());
668       block->Select(false);
669     }
670   }
671   EnableBlockSelection();
672
673   // Matthias 2009-07-03 bugfix: the fCurrent position was not reset at that
674   // place. Also I think the data type and specification must be set in order
675   // to make SelectFirst/NextDataBlock working on the selected collection
676   // of data blocks
677   AliHLTModuleAgent::AliHLTOUTHandlerDesc pHandlerDesc=*pHandlerEntry; 
678   fSearchDataType=pHandlerDesc;
679   fSearchSpecification=kAliHLTVoidDataSpec;
680   fSearchHandlerType=pHandlerDesc;
681   fCurrent=0;
682   
683   return iResult;
684 }
685
686 int AliHLTOUT::EnableBlockSelection()
687 {
688   // see header file for class documentation
689   SetStatusFlag(kBlockSelection);
690   return 0;
691 }
692
693 int AliHLTOUT::DisableBlockSelection()
694 {
695   // see header file for class documentation
696   ClearStatusFlag(kBlockSelection);
697   return 0;
698 }
699
700 int AliHLTOUT::ResetBlockSelection()
701 {
702   // see header file for class documentation
703   for (AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
704        block!=fBlockDescList.end();
705        block++) {
706     block->Select(false);
707   }
708   return 0;
709 }
710
711 int AliHLTOUT::MarkDataBlockProcessed()
712 {
713   // see header file for class documentation
714   int iResult=0;
715   if (fCurrent>=fBlockDescList.size()) return 0;
716   fBlockDescList[fCurrent].MarkProcessed();
717   return iResult;
718 }
719
720 int AliHLTOUT::MarkDataBlocksProcessed(const AliHLTOUTHandlerListEntry* pHandlerDesc)
721 {
722   // see header file for class documentation
723   int iResult=0;
724   if (!pHandlerDesc) return 0;
725
726   AliHLTOUTBlockDescriptorVector::iterator element;
727   for (AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
728        block!=fBlockDescList.end();
729        block++) {
730     if (block->GetHandlerDesc()==*pHandlerDesc && pHandlerDesc->HasIndex(block->GetIndex()))
731       block->MarkProcessed();
732   }
733   
734   return iResult;
735 }
736
737 int AliHLTOUT::AddSubCollection(AliHLTOUT* pCollection)
738 {
739   // see header file for class documentation
740   int iResult=0;
741   if (!pCollection) return 0;
742
743   SetStatusFlag(kCollecting);  
744   int index=-1;
745   for (index=pCollection->SelectFirstDataBlock();
746        index>=0;
747        index=pCollection->SelectNextDataBlock()) {
748     AliHLTComponentDataType dt=kAliHLTVoidDataType;
749     AliHLTUInt32_t spec=kAliHLTVoidDataSpec;
750     pCollection->GetDataBlockDescription(dt, spec);  
751     AliHLTOUTBlockDescriptor desc(dt, spec, index, pCollection);
752     AddBlockDescriptor(desc);
753     iResult++;
754   }
755   if (iResult>0) {
756     if (CheckStatusFlag(kIsSubCollection)) {
757       fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "AddSubCollection" , __FILE__ , __LINE__ , "HLTOUT object %p has already been added as sub-collection", pCollection);
758     } else {
759       pCollection->SetStatusFlag(kIsSubCollection);
760     }
761   }
762   ClearStatusFlag(kCollecting);  
763
764   return iResult;
765 }
766
767 int AliHLTOUT::ReleaseSubCollection(AliHLTOUT* pCollection)
768 {
769   // see header file for class documentation
770   int iResult=0;
771   if (!pCollection) return 0;
772
773   AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
774   while (block!=fBlockDescList.end()) {
775     if ((*block)==pCollection) {
776       block=fBlockDescList.erase(block);
777       continue;
778     }
779     block++;
780   }
781   pCollection->ClearStatusFlag(kIsSubCollection);
782
783   return iResult;
784 }
785
786 int AliHLTOUT::Reset()
787 {
788   // see header file for class documentation
789   int iResult=0;
790   AliHLTOUTPVector subCollections;
791   AliHLTOUTBlockDescriptorVector::iterator block=fBlockDescList.begin();
792   while (block!=fBlockDescList.end()) {
793     if (!((*block)==this)) {
794       AliHLTOUTPVector::iterator collection=subCollections.begin();
795       for (; collection!=subCollections.end(); collection++)
796         if((*block)==*collection) break;
797       if (collection==subCollections.end())
798         subCollections.push_back(block->GetCollection());
799     }
800     block=fBlockDescList.erase(block);
801   }
802
803   for (AliHLTOUTPVector::iterator collection=subCollections.begin(); 
804        collection!=subCollections.end(); collection++) {
805     (*collection)->Reset();
806     (*collection)->ClearStatusFlag(kIsSubCollection);
807   }
808
809   ResetInput();
810   fCurrentEventId=kAliHLTVoidEventID;
811
812   return iResult;
813 }
814
815 int AliHLTOUT::ResetInput()
816 {
817   // default implementation, nothing to do
818   return 0;
819 }
820
821 const AliHLTOUT::AliHLTOUTHandlerListEntry& AliHLTOUT::AliHLTOUTBlockDescriptor::GetHandlerDesc()
822 {
823   // see header file for class documentation
824   if (fpCollection) {
825     AliHLTOUTHandlerListEntryVector::iterator element=fpCollection->fDataHandlers.begin();
826     while (element!=fpCollection->fDataHandlers.end()) {
827       if (element->HasIndex(GetIndex())) {
828         return *element;
829       }
830       element++;
831     }
832   }
833   return const_cast<AliHLTOUT::AliHLTOUTHandlerListEntry&>(AliHLTOUT::AliHLTOUTHandlerListEntry::fgkVoidHandlerListEntry);
834 }
835
836 TObject* AliHLTOUT::GetDataObject()
837 {
838   // see header file for class documentation
839   if (fpDataObject) {
840     fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "GetDataObject" , __FILE__ , __LINE__ , "data object has not been released, potential memory leak");
841     ReleaseDataBuffer(fpObjectBuffer);
842   }
843   fpObjectBuffer=NULL;
844   fObjectBufferSize=0;
845   fpDataObject=NULL;
846
847   if (GetDataBuffer(fpObjectBuffer, fObjectBufferSize)>=0) {
848     fpDataObject=AliHLTMessage::Extract(fpObjectBuffer, fObjectBufferSize);
849   } else {
850     fLog.LoggingVarargs(kHLTLogError, "AliHLTOUT", "GetDataObject" , __FILE__ , __LINE__ , "can not fetch data buffer");    
851   }
852
853   return fpDataObject;
854 }
855
856 int AliHLTOUT::ReleaseDataObject(TObject* pObject)
857 {
858   // see header file for class documentation
859   if (!pObject) return -EINVAL;
860   if (pObject!=fpDataObject) {
861     fLog.LoggingVarargs(kHLTLogError, "AliHLTOUT", "GetDataObject" , __FILE__ , __LINE__ , "attempt to release wrong data object %p, expected %p", pObject, fpDataObject);
862     return -EINVAL;
863   }
864
865   delete fpDataObject;
866   fpDataObject=NULL;
867   ReleaseDataBuffer(fpObjectBuffer);
868   fpObjectBuffer=NULL;
869   fObjectBufferSize=0;
870
871   return 0;
872 }
873
874 void AliHLTOUT::SetEventId(AliHLTUInt64_t id)
875 {
876   // see header file for class documentation
877   if (fCurrentEventId!=kAliHLTVoidEventID && fCurrentEventId!=id) {
878     fLog.LoggingVarargs(kHLTLogWarning, "AliHLTOUT", "SetEventId" , __FILE__ , __LINE__ , "event id was already set to 0x%llx, setting now to 0x%llx", fCurrentEventId, id);
879   }
880   fCurrentEventId=id;
881 }