6 //* This file is property of and copyright by the ALICE HLT Project *
7 //* ALICE Experiment at CERN, All rights reserved. *
8 //* See cxx source for full Copyright notice *
11 @author Matthias Richter
13 @brief The control class for HLTOUT data.
17 #include "AliHLTLogging.h"
18 #include "AliHLTModuleAgent.h"
20 class AliHLTOUTHandler;
21 class AliHLTOUTHandlerDesc; // AliHLTModuleAgent.h
23 class AliHLTReconstructor;
27 #define AliHLTOUTInvalidIndex (~(AliHLTUInt32_t)0)
29 typedef vector<AliHLTUInt32_t> AliHLTOUTIndexList;
33 * The control class for HLTOUT data.
34 * The output of the HLT, either from the HLTOUT nodes or simulated output,
35 * is transferred and stored in the HOMER format. The AliHLTOUT class
36 * implements scanning of the HOMER data for all HLTOUT DDL links and
37 * abstracts access to the complete HLTOUT data.
42 /** standard constructor */
44 /** standard destructor */
48 * Create an AliHLTOUTRawReader instance.
49 * Helper function to transparently access classes from the
52 static AliHLTOUT* New(AliRawReader* pRawReader);
55 * Create an AliHLTOUTDigitReader instance
56 * Helper function to transparently access classes from the
59 static AliHLTOUT* New(TTree* pDigitTree, int event=-1);
62 * Get the global instance.
63 * The global instance is set temporarily by the AliHLTOUTGlobalInstanceGuard
64 * mainly for the sake of data input to an analysis chain. The
65 * AliHLTOUTPublisherComponent objects can access the global instance.
67 static AliHLTOUT* GetGlobalInstance() {return fgGlobalInstance;}
70 * Delete an instance of the HLTOUT.
71 * Helper function to transparently access classes from the
72 * libHLTrec library. Before deleting, the availability of the
75 static void Delete(AliHLTOUT* pInstance);
78 * Locking guard for the AliHLTOUT object.
79 * If the object is locked, the selection of data blocks can not be changed.
81 class AliHLTOUTLockGuard {
84 AliHLTOUTLockGuard(AliHLTOUT* pInstance) : fpInstance(pInstance)
85 {if (fpInstance) fpInstance->SetStatusFlag(kLocked);}
88 {if (fpInstance) fpInstance->ClearStatusFlag(kLocked);}
91 /** standard constructor prohibited */
93 /** copy constructor prohibited */
94 AliHLTOUTLockGuard(const AliHLTOUTLockGuard&);
95 /** assignment operator prohibited */
96 AliHLTOUTLockGuard& operator=(const AliHLTOUTLockGuard&);
98 /** the AliHLTOUT instance the guard is locking */
99 AliHLTOUT* fpInstance; //!transient
103 * Guard for handling of global AliHLTOUT instance.
104 * The global HLTOUT instance can be set for certain steps of the
105 * processing. The initial objective is to support input to the
106 * AliHLTOUTPublisherComponent running in a kChain handler.
108 * The Guard restores the original instance when closed.
110 class AliHLTOUTGlobalInstanceGuard {
113 AliHLTOUTGlobalInstanceGuard(AliHLTOUT* pInstance) : fpLastInstance(AliHLTOUT::fgGlobalInstance)
114 {AliHLTOUT::fgGlobalInstance=pInstance;}
116 ~AliHLTOUTGlobalInstanceGuard()
117 {AliHLTOUT::fgGlobalInstance=fpLastInstance;}
120 /** standard constructor prohibited */
121 AliHLTOUTGlobalInstanceGuard();
122 /** copy constructor prohibited */
123 AliHLTOUTGlobalInstanceGuard(const AliHLTOUTGlobalInstanceGuard&);
124 /** assignment operator prohibited */
125 AliHLTOUTGlobalInstanceGuard& operator=(const AliHLTOUTGlobalInstanceGuard&);
127 /** the AliHLTOUT instance the guard is locking */
128 AliHLTOUT* fpLastInstance; //!transient
132 * The HLT OUT Event Header.
133 * Defined between HLT and DAQ.
135 struct AliHLTOUTEventHeader {
136 /**Total length of the data in bytes, including HLT event header, excluding CDH. */
137 AliHLTUInt32_t fLength; //! see above
138 /** version of the header */
139 AliHLTUInt32_t fVersion; //! see above
140 /** High 32 bit word of event id */
141 AliHLTUInt32_t fEventIDHigh; //! see above
142 /** Low 32 bit word of event id */
143 AliHLTUInt32_t fEventIDLow; //! see above
147 /// versions 1 of the HLT header
149 /// versions 2 of the HLT header
154 /// size of HLT decision in data format version 1: 29x4
155 kSizeDecisionVersion1 = 116,
156 /// size of HLT decision in data format version 2: 30x4
157 kSizeDecisionVersion2 = 120
160 // definitions from ALICE internal notes ALICE-INT-2002-010 and
161 // ALICE-INT-2006-XXX
163 /** the 32bit word in the CDH containing the status flags */
164 kCDHStatusWord=4, //! see above
165 /** start of the flags in word fgkCDHStatusWord */
166 kCDHStatusFlagsOffset=12, //! see above
167 /** bit indicating HLT decision in the HLTOUT*/
168 kCDHFlagsHLTDecision=6, //! see above
169 /** bit indicating HLT payload in the HLTOUT*/
170 kCDHFlagsHLTPayload=7 //! see above
176 class AliHLTOUTBlockDescriptor {
178 AliHLTOUTBlockDescriptor(AliHLTComponentDataType dt, AliHLTUInt32_t spec, AliHLTUInt32_t index, AliHLTOUT* pCollection)
179 : fDataType(dt), fSpecification(spec), fIndex(index), fSelected(false), fProcessed(false), fpCollection(pCollection) {};
180 AliHLTOUTBlockDescriptor(const AliHLTOUTBlockDescriptor& src)
181 : fDataType(src.fDataType), fSpecification(src.fSpecification), fIndex(src.fIndex), fSelected(false), fProcessed(false), fpCollection(src.fpCollection) {}
182 AliHLTOUTBlockDescriptor& operator=(const AliHLTOUTBlockDescriptor& src)
183 { fDataType=src.fDataType; fSpecification=src.fSpecification; fIndex=src.fIndex; fSelected=false; fProcessed=false; fpCollection=src.fpCollection; return *this; }
184 ~AliHLTOUTBlockDescriptor() {}
186 operator AliHLTComponentDataType() const {return fDataType;}
187 operator AliHLTUInt32_t() const {return fSpecification;}
188 int operator==(AliHLTComponentDataType dt) const {return dt==fDataType;}
189 int operator==(AliHLTUInt32_t spec) const {return spec==fSpecification;}
190 int operator==(AliHLTOUT* collection) const {return collection==fpCollection;}
192 AliHLTUInt32_t GetIndex() const {return fIndex;}
193 AliHLTOUT* GetCollection() const {return fpCollection;}
195 bool IsSelected() const {return fSelected;}
196 void Select(bool selected=true) {fSelected=selected;}
197 bool IsProcessed() const {return fProcessed;}
198 void MarkProcessed() {fProcessed=true;}
201 * Get the data buffer
202 * @param pBuffer [out] buffer of the selected data block
203 * @param size [out] size of the selected data block
205 int GetDataBuffer(const AliHLTUInt8_t* &pBuffer, AliHLTUInt32_t& size) {
206 if (fpCollection) return fpCollection->GetDataBuffer(GetIndex(), pBuffer, size);
211 /** data type of the block */
212 AliHLTComponentDataType fDataType; //!transient
213 /** data specification of the block */
214 AliHLTUInt32_t fSpecification; //!transient
215 /** index in the data stream */
216 AliHLTUInt32_t fIndex; //!transient
217 /** selection flag */
218 bool fSelected; //!transient
219 /** processed flag */
220 bool fProcessed; //!transient
221 /** the collection */
222 AliHLTOUT* fpCollection; //!transient
225 class AliHLTOUTHandlerListEntry {
227 AliHLTOUTHandlerListEntry(AliHLTOUTHandler* pHandler,
228 AliHLTModuleAgent::AliHLTOUTHandlerDesc& handlerDesc,
229 AliHLTModuleAgent* pAgent,
230 AliHLTUInt32_t index);
232 /** copy constructor for vector handling */
233 AliHLTOUTHandlerListEntry(const AliHLTOUTHandlerListEntry& src);
235 /** assignment operator for vector handling */
236 AliHLTOUTHandlerListEntry& operator=(const AliHLTOUTHandlerListEntry& src);
238 ~AliHLTOUTHandlerListEntry();
240 static const AliHLTOUTHandlerListEntry fgkVoidHandlerListEntry; //! initializer
242 operator AliHLTOUTHandler*() const {return fpHandler;}
244 // please note that fpHandlerDesc is really a pointer and is created
245 // in the constructor. Thats why it is dereferenced here. The pointer
246 // type is on purpose, even though it is a bit confusing with the
247 // argument by reference in the AliHLTOUTHandlerListEntry constructor.
248 operator const AliHLTModuleAgent::AliHLTOUTHandlerDesc&() const
249 {return fpHandlerDesc?*fpHandlerDesc:AliHLTModuleAgent::fgkVoidHandlerDesc;}
250 operator AliHLTModuleAgent*() const {return fpAgent;}
253 * Two list entries are considered to be equal if the handlers
256 bool operator==(const AliHLTOUTHandlerListEntry& entry) const;
258 bool operator==(const AliHLTModuleAgent::AliHLTOUTHandlerType handlerType) const;
261 * Compare the handler descriptor of this list entry with another
264 bool operator==(const AliHLTModuleAgent::AliHLTOUTHandlerDesc desc) const;
266 AliHLTUInt32_t operator[](int i) const;
269 * Add a block index to this descriptor.
270 * One descriptor can serve multiple blocks if the agent returns the
271 * same handler for all of the blocks. Instead of creating a new entry
272 * the block index ist just added
274 void AddIndex(AliHLTUInt32_t index);
277 * Add all indices of the descriptor.
279 void AddIndex(AliHLTOUTHandlerListEntry &desc);
282 * Check if an index is served by this descriptor.
283 * @return true if the index is in the table
285 bool HasIndex(AliHLTUInt32_t index) const;
288 * Invalidate all block indices
290 void InvalidateBlocks() {fBlocks.clear();}
293 * Check whether the entry has valid blocks.
295 bool IsEmpty() {return fBlocks.size()==0;}
298 /** standard constructor prohibited */
299 AliHLTOUTHandlerListEntry();
301 /** pointer to the handler */
302 AliHLTOUTHandler* fpHandler; //! transient
304 /** pointer to handler description */
305 AliHLTModuleAgent::AliHLTOUTHandlerDesc* fpHandlerDesc; //! transient
307 /** pointer to module agent */
308 AliHLTModuleAgent* fpAgent; //! transient
310 /** list of block indices */
311 AliHLTOUTIndexList fBlocks; //!transient
314 typedef vector<AliHLTOUTHandlerListEntry> AliHLTOUTHandlerListEntryVector;
315 typedef vector<AliHLTOUTBlockDescriptor> AliHLTOUTBlockDescriptorVector;
316 typedef vector<AliHLTOUT*> AliHLTOUTPVector;
319 * Selection guard for the AliHLTOUT object.
320 * If the object is locked, the selection of data blocks can not be changed.
322 class AliHLTOUTSelectionGuard {
325 AliHLTOUTSelectionGuard(AliHLTOUT* pInstance) : fpInstance(pInstance)
326 {if (fpInstance) fpInstance->SelectDataBlock();}
328 AliHLTOUTSelectionGuard(AliHLTOUT* pInstance, const AliHLTOUTHandlerListEntry* pHandlerDesc) : fpInstance(pInstance)
329 {if (fpInstance) fpInstance->SelectDataBlocks(pHandlerDesc);}
331 ~AliHLTOUTSelectionGuard()
332 {if (fpInstance) fpInstance->DisableBlockSelection();}
335 /** standard constructor prohibited */
336 AliHLTOUTSelectionGuard();
337 /** copy constructor prohibited */
338 AliHLTOUTSelectionGuard(const AliHLTOUTSelectionGuard&);
339 /** assignment operator prohibited */
340 AliHLTOUTSelectionGuard& operator=(const AliHLTOUTSelectionGuard&);
342 /** the AliHLTOUT instance the guard is locking */
343 AliHLTOUT* fpInstance; //!transient
347 * Init for processing.
348 * The HLTOUT is scanned for all available data blocks and the
349 * AliHLTOUTHandler objects for the data blocks are created according
350 * to the module agents (see AliHLTModuleAgent).
355 * Reset and clear all data block descriptors.
356 * @note Since sub-collections are only referred via the block
357 * descriptors, all information on sub-collections is also lost.
358 * Child classes should implement ResetInput in order to cleanup
364 * Get number of data blocks in the HLTOUT data
366 int GetNofDataBlocks();
369 * Select the first data block of a certain data type and specification.
370 * The selection criteria can be of kAliHLTAnyDataType and/or
371 * kAliHLTVoidDataSpec in order to be ignored and just match any data block.
373 * The search criteria can be combined with a handler type (e.g. kRawReader)
374 * @param dt [in] data type to match <br>
375 * @param spec [in] data specification to match <br>
376 * @param handlerType [in] type of the handler
377 * @param skipProcessed [in] skip all block marked processed
378 * @return block index: >= 0 if success, -ENOENT if no block found <br>
379 * neg. error code if failed <br>
380 * -EPERM if access denied (object locked)
382 int SelectFirstDataBlock(AliHLTComponentDataType dt=kAliHLTAnyDataType,
383 AliHLTUInt32_t spec=kAliHLTVoidDataSpec,
384 AliHLTModuleAgent::AliHLTOUTHandlerType handlerType=AliHLTModuleAgent::kUnknownOutput,
385 bool skipProcessed=true);
388 * Select the next data block of data type and specification of the previous
389 * call to @ref SelectFirstDataBlock.
390 * @return block index: >= 0 if success, -ENOENT if no block found <br>
391 * neg. error code if failed <br>
392 * -EPERM if access denied (object locked)
394 int SelectNextDataBlock();
397 * Get properties of the selected data block.
398 * @param dt [out] data type of the selected block
399 * @param spec [out] data specification of the selected block
401 int GetDataBlockDescription(AliHLTComponentDataType& dt, AliHLTUInt32_t& spec);
404 * Get handler description of the current data block.
406 const AliHLTOUTHandlerListEntry& GetDataBlockHandlerDesc();
409 * Get handler type of the selected data block.
410 * @return handler type for the selected data block
412 AliHLTModuleAgent::AliHLTOUTHandlerType GetDataBlockHandlerType();
415 * Get the index of the current data block.
416 * @return index, AliHLTOUTInvalidIndex if no block selected
418 AliHLTUInt32_t GetDataBlockIndex();
421 * Get buffer of the selected data block.
422 * @param pBuffer [out] buffer of the selected data block
423 * @param size [out] size of the selected data block
425 int GetDataBuffer(const AliHLTUInt8_t* &pBuffer, AliHLTUInt32_t& size);
428 * Release buffer after use.
429 * @param pBuffer [in] buffer of the selected data block
431 int ReleaseDataBuffer(const AliHLTUInt8_t* pBuffer);
434 * Add the current data block to the selection.
435 * Note: enables also the block selection
437 int SelectDataBlock();
440 * Add the all data blocks of a certain handler to the selection.
441 * Note: enables also the block selection
443 int SelectDataBlocks(const AliHLTOUTHandlerListEntry* pHandlerDesc);
446 * Enable the selection of data blocks.
448 int EnableBlockSelection();
451 * Disable the selection of data blocks.
453 int DisableBlockSelection();
456 * Reset the data block selection.
457 * Resets the selection list, none of the blocks is selected.
459 int ResetBlockSelection();
462 * Mark the current block as processed.
464 int MarkDataBlockProcessed();
467 * Mark all data blocks of a certain handler processed.
469 int MarkDataBlocksProcessed(const AliHLTOUTHandlerListEntry* pHandlerDesc);
472 * Add a sub collection to the HLTOUT.
474 int AddSubCollection(AliHLTOUT* pCollection);
477 * Release a previously added sub collection.
479 int ReleaseSubCollection(AliHLTOUT* pCollection);
482 * Get module agent for the selected data block.
484 AliHLTModuleAgent* GetAgent();
487 * Get handler for the selected data block.
489 AliHLTOUTHandler* GetHandler();
492 * Convert data buffer to ESD.
493 * The buffer is supposed to describe a streamed AliESDEvent object.
494 * If no target object is specified, the ESD is written to a file AliHLTdetESDs.root,
495 * where 'det' is derived from the data type origin. Each time the function is invoked
496 * a new event is created. Dummy events are added if the previous events did not contain
498 * The function needs AliRoot and might not be implemented by all AliHLTOUT
500 * a data block of this specification.
501 * @param pBuffer [in] the data buffer
502 * @param size [in] data buffer size
503 * @param dt [in] data type of the block
504 * @param tgtesd [out] optional target
506 virtual int WriteESD(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size, AliHLTComponentDataType dt, AliESDEvent* tgtesd=NULL) const;
508 enum AliHLTOUTByteOrder {
509 /** no data block selected */
510 kInvalidByteOrder=-1,
516 enum AliHLTOUTDataType {
526 * Check byte order of selected block
528 AliHLTOUTByteOrder CheckByteOrder();
531 * Check alignment of selected block
533 int CheckAlignment(AliHLTOUT::AliHLTOUTDataType type);
536 * Helper function to byte swap a 64 bit value.
538 static AliHLTUInt64_t ByteSwap64(AliHLTUInt64_t src);
541 * Helper function to byte swap a 32 bit value.
543 static AliHLTUInt32_t ByteSwap32(AliHLTUInt32_t src);
546 * Insert a handler item.
547 * The current list entries are checked if the handler is already in
548 * the list. It is added if not in the list, otherwise the block index
549 * is added to the existing entry.
550 * @param list the handler list
551 * @param entry handler list entry
552 * @return 0 if added, EEXIST (non negative!) if merged with existing entry <br>
553 * neg. error code if failed
555 static int InsertHandler(AliHLTOUTHandlerListEntryVector& list, const AliHLTOUTHandlerListEntry &entry);
558 * Insert all handlers of the specified type to the list.
560 int FillHandlerList(AliHLTOUTHandlerListEntryVector& list, AliHLTModuleAgent::AliHLTOUTHandlerType handlerType);
563 * Remove empty items which have a duplicate in the list.
565 static int RemoveEmptyDuplicateHandlers(AliHLTOUTHandlerListEntryVector& list);
568 * Find an entry of a certain description in the list.
569 * @return index of the entry if found, -ENOENT if not found
571 static int FindHandler(AliHLTOUTHandlerListEntryVector& list, const AliHLTModuleAgent::AliHLTOUTHandlerDesc desc);
574 * Invalidate all blocks of the handlers in the list
576 static int InvalidateBlocks(AliHLTOUTHandlerListEntryVector& list);
580 * Add a block descriptor.
581 * This is done by the child classes generating the index. The AliHLTOUT
582 * object must be locked for index generation.
583 * @param desc the block descriptor
584 * @return 0 if success, -EPERM if access denied
586 int AddBlockDescriptor(const AliHLTOUTBlockDescriptor desc);
589 * Print output or suppress.
591 bool BeVerbose() {return fbVerbose;}
596 void SwitchVerbosity(bool verbose) {fbVerbose=verbose;}
599 /** copy constructor prohibited */
600 AliHLTOUT(const AliHLTOUT&);
601 /** assignment operator prohibited */
602 AliHLTOUT& operator=(const AliHLTOUT&);
605 * Internal status flags
608 /** the HLTOUT object is locked with the current data block selection */
610 /** childs can add block descriptors */
612 /** user of the data block has checked the byte order */
613 kByteOrderChecked = 0x4,
614 /** warning on byte order missmatch has been printed */
615 kByteOrderWarning = 0x8,
616 /** user of the data block has checked the alignment */
617 kAlignmentChecked = 0x10,
618 /** warning on alignment missmatch has been printed */
619 kAlignmentWarning = 0x20,
620 /** enable block selection list */
621 kBlockSelection = 0x40,
622 /** skip processed data blocks */
623 kSkipProcessed = 0x80,
624 /** marked as sub collection */
625 kIsSubCollection = 0x100
629 * Generate the index of the HLTOUT data.
630 * Must be implemented by the child classes.
632 virtual int GenerateIndex()=0;
635 * Find AliHLTOUTHandler objects for the data blocks.
636 * The available AliHLTModuleAgents are probed whether they provide
637 * handlers for data processing.
642 * Cleanup and reset the data input.
644 virtual int ResetInput();
647 * Get the data buffer
648 * @param index [in] index of the block
649 * @param pBuffer [out] buffer of the selected data block
650 * @param size [out] size of the selected data block
652 virtual int GetDataBuffer(AliHLTUInt32_t index, const AliHLTUInt8_t* &pBuffer,
653 AliHLTUInt32_t& size)=0;
656 * Check byte order of data block
658 virtual AliHLTOUTByteOrder CheckBlockByteOrder(AliHLTUInt32_t index)=0;
661 * Check alignment of data block
663 virtual int CheckBlockAlignment(AliHLTUInt32_t index, AliHLTOUT::AliHLTOUTDataType type)=0;
666 * Select the data block of data type and specification of the previous
667 * call to @ref SelectFirstDataBlock. Core function of @ref SelectFirstDataBlock
668 * and @ref SelectNextDataBlock, starts to find a block at the current list
671 * The data block is searched from the conditions of fSearchDataType,
672 * fSearchSpecification, fSearchHandlerType and the selection list.
674 * @return identifier >=0 if success, neg. error code if failed <br>
675 * -ENOENT if no block found <br>
676 * -EPERM if access denied (object locked)
678 int FindAndSelectDataBlock();
682 * @param flag flag to set
683 * @return current status flags
685 unsigned int SetStatusFlag(unsigned int flag) {return fFlags|=flag;}
689 * @param flag flag to clear
690 * @return current status flags
692 unsigned int ClearStatusFlag(unsigned int flag) {return fFlags&=~flag;}
696 * @param flag flag to check
697 * @return 1 if flag is set
699 int CheckStatusFlag(unsigned int flag) const {return (fFlags&flag)==flag;}
702 * Find handler description for a certain block index.
704 const AliHLTOUTHandlerListEntry& FindHandlerDesc(AliHLTUInt32_t blockIndex);
707 * Internal New function for the external HLTOUT instances.
708 * Currently supported classes are AliHLTOUTRawReader and
709 * AliHLTOUTDigitReader, both implemented in libHLTrec.so.
711 static AliHLTOUT* New(const char* classname);
714 * Set the RawReader as parameter.
715 * The function is for internal use only in conjunction with the
718 virtual void SetParam(AliRawReader* pRawReader);
721 * Set the RunLoader as parameter
722 * The function is for internal use only in conjunction with the
725 virtual void SetParam(TTree* pDigitTree, int event=-1);
727 /** data type for the current block search, set from @ref SelectFirstDataBlock */
728 AliHLTComponentDataType fSearchDataType; //!transient
730 /** data specification for the current block search */
731 AliHLTUInt32_t fSearchSpecification; //!transient
733 /** handler type for the current block search */
734 AliHLTModuleAgent::AliHLTOUTHandlerType fSearchHandlerType; // !transient
736 /** instance flags: locked, collecting, ... */
737 unsigned int fFlags; //!transient
739 /** list of block descriptors */
740 AliHLTOUTBlockDescriptorVector fBlockDescList; //!transient
742 /** current position in the list */
743 unsigned int fCurrent; //!transient
745 /** data buffer under processing */
746 const AliHLTUInt8_t* fpBuffer; //!transient
748 /** list of AliHLTOUTHandlers */
749 AliHLTOUTHandlerListEntryVector fDataHandlers; // !transient
751 /** verbose or silent output */
752 bool fbVerbose; //!transient
754 /** gobal instance set for certain steps of the analysis */
755 static AliHLTOUT* fgGlobalInstance; //! transient
757 /** logging methods */
758 AliHLTLogging fLog; //! transient
760 ClassDef(AliHLTOUT, 4)