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.
40 class AliHLTOUT : public AliHLTLogging {
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 * Delete an instance of the HLTOUT.
63 * Helper function to transparently access classes from the
64 * libHLTrec library. Before deleting, the availability of the
67 static void Delete(AliHLTOUT* pInstance);
70 * Locking guard for the AliHLTOUT object.
71 * If the object is locked, the selection of data blocks can not be changed.
73 class AliHLTOUTLockGuard {
76 AliHLTOUTLockGuard(AliHLTOUT* pInstance) : fpInstance(pInstance)
77 {if (fpInstance) fpInstance->SetStatusFlag(kLocked);}
80 {if (fpInstance) fpInstance->ClearStatusFlag(kLocked);}
83 /** standard constructor prohibited */
85 /** copy constructor prohibited */
86 AliHLTOUTLockGuard(const AliHLTOUTLockGuard&);
87 /** assignment operator prohibited */
88 AliHLTOUTLockGuard& operator=(const AliHLTOUTLockGuard&);
90 /** the AliHLTOUT instance the guard is locking */
91 AliHLTOUT* fpInstance; //!transient
95 * The HLT OUT Event Header.
96 * Defined between HLT and DAQ.
98 struct AliHLTOUTEventHeader {
99 /**Total length of the data in bytes, including HLT event header, excluding CDH. */
100 AliHLTUInt32_t fLength; //! see above
101 /** version of the header */
102 AliHLTUInt32_t fVersion; //! see above
103 /** High 32 bit word of event id */
104 AliHLTUInt32_t fEventIDHigh; //! see above
105 /** Low 32 bit word of event id */
106 AliHLTUInt32_t fEventIDLow; //! see above
110 /// versions 1 of the HLT header
112 /// versions 2 of the HLT header
117 /// size of HLT decision in data format version 1: 29x4
118 kSizeDecisionVersion1 = 116,
119 /// size of HLT decision in data format version 2: 30x4
120 kSizeDecisionVersion2 = 120
123 // definitions from ALICE internal notes ALICE-INT-2002-010 and
124 // ALICE-INT-2006-XXX
126 /** the 32bit word in the CDH containing the status flags */
127 kCDHStatusWord=4, //! see above
128 /** start of the flags in word fgkCDHStatusWord */
129 kCDHStatusFlagsOffset=12, //! see above
130 /** bit indicating HLT decision in the HLTOUT*/
131 kCDHFlagsHLTDecision=6, //! see above
132 /** bit indicating HLT payload in the HLTOUT*/
133 kCDHFlagsHLTPayload=7 //! see above
139 class AliHLTOUTBlockDescriptor {
141 AliHLTOUTBlockDescriptor(AliHLTComponentDataType dt, AliHLTUInt32_t spec, AliHLTUInt32_t index, AliHLTOUT* pCollection)
142 : fDataType(dt), fSpecification(spec), fIndex(index), fSelected(false), fProcessed(false), fpCollection(pCollection) {};
143 AliHLTOUTBlockDescriptor(const AliHLTOUTBlockDescriptor& src)
144 : fDataType(src.fDataType), fSpecification(src.fSpecification), fIndex(src.fIndex), fSelected(false), fProcessed(false), fpCollection(src.fpCollection) {}
145 AliHLTOUTBlockDescriptor& operator=(const AliHLTOUTBlockDescriptor& src)
146 { fDataType=src.fDataType; fSpecification=src.fSpecification; fIndex=src.fIndex; fSelected=false; fProcessed=false; fpCollection=src.fpCollection; return *this; }
147 ~AliHLTOUTBlockDescriptor() {}
149 operator AliHLTComponentDataType() const {return fDataType;}
150 operator AliHLTUInt32_t() const {return fSpecification;}
151 int operator==(AliHLTComponentDataType dt) const {return dt==fDataType;}
152 int operator==(AliHLTUInt32_t spec) const {return spec==fSpecification;}
153 int operator==(AliHLTOUT* collection) const {return collection==fpCollection;}
155 AliHLTUInt32_t GetIndex() const {return fIndex;}
157 bool IsSelected() const {return fSelected;}
158 void Select(bool selected=true) {fSelected=selected;}
159 bool IsProcessed() const {return fSelected;}
160 void MarkProcessed() {fProcessed=true;}
163 * Get the data buffer
164 * @param pBuffer [out] buffer of the selected data block
165 * @param size [out] size of the selected data block
167 int GetDataBuffer(const AliHLTUInt8_t* &pBuffer, AliHLTUInt32_t& size) {
168 if (fpCollection) return fpCollection->GetDataBuffer(GetIndex(), pBuffer, size);
173 /** data type of the block */
174 AliHLTComponentDataType fDataType; //!transient
175 /** data specification of the block */
176 AliHLTUInt32_t fSpecification; //!transient
177 /** index in the data stream */
178 AliHLTUInt32_t fIndex; //!transient
179 /** selection flag */
180 bool fSelected; //!transient
181 /** processed flag */
182 bool fProcessed; //!transient
183 /** the collection */
184 AliHLTOUT* fpCollection; //!transient
187 class AliHLTOUTHandlerListEntry {
189 AliHLTOUTHandlerListEntry(AliHLTOUTHandler* pHandler,
190 AliHLTModuleAgent::AliHLTOUTHandlerDesc& handlerDesc,
191 AliHLTModuleAgent* pAgent,
192 AliHLTUInt32_t index);
194 /** copy constructor for vector handling */
195 AliHLTOUTHandlerListEntry(const AliHLTOUTHandlerListEntry& src);
197 /** assignment operator for vector handling */
198 AliHLTOUTHandlerListEntry& operator=(const AliHLTOUTHandlerListEntry& src);
200 ~AliHLTOUTHandlerListEntry();
202 static const AliHLTOUTHandlerListEntry fgkVoidHandlerListEntry; //! initializer
204 operator AliHLTOUTHandler*() const {return fpHandler;}
206 // please note that fpHandlerDesc is really a pointer and is created
207 // in the constructor. Thats why it is dereferenced here. The pointer
208 // type is on purpose, even though it is a bit confusing with the
209 // argument by reference in the AliHLTOUTHandlerListEntry constructor.
210 operator const AliHLTModuleAgent::AliHLTOUTHandlerDesc&() const
211 {return fpHandlerDesc?*fpHandlerDesc:AliHLTModuleAgent::fgkVoidHandlerDesc;}
212 operator AliHLTModuleAgent*() const {return fpAgent;}
215 * Two list entries are considered to be equal if the handlers
218 bool operator==(const AliHLTOUTHandlerListEntry& entry) const;
220 bool operator==(const AliHLTModuleAgent::AliHLTOUTHandlerType handlerType) const;
222 AliHLTUInt32_t operator[](int i) const;
225 * Add a block index to this descriptor.
226 * One descriptor can serve multiple blocks if the agent returns the
227 * same handler for all of the blocks. Instead of creating a new entry
228 * the block index ist just added
230 void AddIndex(AliHLTUInt32_t index);
233 * Add all indexes of the descriptor.
235 void AddIndex(AliHLTOUTHandlerListEntry &desc);
238 * Check if an index is served by this descriptor.
239 * @return true if the index is in the table
241 bool HasIndex(AliHLTUInt32_t index) const;
244 /** standard constructor prohibited */
245 AliHLTOUTHandlerListEntry();
247 /** pointer to the handler */
248 AliHLTOUTHandler* fpHandler; //! transient
250 /** pointer to handler description */
251 AliHLTModuleAgent::AliHLTOUTHandlerDesc* fpHandlerDesc; //! transient
253 /** pointer to module agent */
254 AliHLTModuleAgent* fpAgent; //! transient
256 /** list of block indexes */
257 AliHLTOUTIndexList fBlocks; //!transient
260 typedef vector<AliHLTOUTHandlerListEntry> AliHLTOUTHandlerListEntryVector;
261 typedef vector<AliHLTOUTBlockDescriptor> AliHLTOUTBlockDescriptorVector;
262 typedef vector<AliHLTOUT*> AliHLTOUTPVector;
265 * Selection guard for the AliHLTOUT object.
266 * If the object is locked, the selection of data blocks can not be changed.
268 class AliHLTOUTSelectionGuard {
271 AliHLTOUTSelectionGuard(AliHLTOUT* pInstance) : fpInstance(pInstance)
272 {if (fpInstance) fpInstance->SelectDataBlock();}
274 AliHLTOUTSelectionGuard(AliHLTOUT* pInstance, const AliHLTOUTHandlerListEntry* pHandlerDesc) : fpInstance(pInstance)
275 {if (fpInstance) fpInstance->SelectDataBlocks(pHandlerDesc);}
277 ~AliHLTOUTSelectionGuard()
278 {if (fpInstance) fpInstance->DisableBlockSelection();}
281 /** standard constructor prohibited */
282 AliHLTOUTSelectionGuard();
283 /** copy constructor prohibited */
284 AliHLTOUTSelectionGuard(const AliHLTOUTSelectionGuard&);
285 /** assignment operator prohibited */
286 AliHLTOUTSelectionGuard& operator=(const AliHLTOUTSelectionGuard&);
288 /** the AliHLTOUT instance the guard is locking */
289 AliHLTOUT* fpInstance; //!transient
293 * Init for processing.
294 * The HLTOUT is scanned for all available data blocks and the
295 * AliHLTOUTHandler objects for the data blocks are created according
296 * to the module agents (see AliHLTModuleAgent).
301 * Get number of data blocks in the HLTOUT data
303 int GetNofDataBlocks();
306 * Select the first data block of a certain data type and specification.
307 * The selection criteria can be of kAliHLTAnyDataType and/or
308 * kAliHLTVoidDataSpec in order to be ignored and just match any data block.
310 * The search criteria can be combined with a handler type (e.g. kRawReader)
311 * @param dt [in] data type to match <br>
312 * @param spec [in] data specification to match <br>
313 * @param handlerType [in] type of the handler
314 * @return block index: >= 0 if success, -ENOENT if no block found <br>
315 * neg. error code if failed <br>
316 * -EPERM if access denied (object locked)
318 int SelectFirstDataBlock(AliHLTComponentDataType dt=kAliHLTAnyDataType,
319 AliHLTUInt32_t spec=kAliHLTVoidDataSpec,
320 AliHLTModuleAgent::AliHLTOUTHandlerType handlerType=AliHLTModuleAgent::kUnknownOutput,
321 bool skipProcessed=true);
324 * Select the next data block of data type and specification of the previous
325 * call to @ref SelectFirstDataBlock.
326 * @return block index: >= 0 if success, -ENOENT if no block found <br>
327 * neg. error code if failed <br>
328 * -EPERM if access denied (object locked)
330 int SelectNextDataBlock();
333 * Get properties of the selected data block.
334 * @param dt [out] data type of the selected block
335 * @param spec [out] data specification of the selected block
337 int GetDataBlockDescription(AliHLTComponentDataType& dt, AliHLTUInt32_t& spec);
340 * Get handler description of the current data block.
342 const AliHLTOUTHandlerListEntry& GetDataBlockHandlerDesc();
345 * Get handler type of the selected data block.
346 * @return handler type for the selected data block
348 AliHLTModuleAgent::AliHLTOUTHandlerType GetDataBlockHandlerType();
351 * Get the index of the current data block.
352 * @return index, AliHLTOUTInvalidIndex if no block selected
354 AliHLTUInt32_t GetDataBlockIndex();
357 * Get buffer of the selected data block.
358 * @param pBuffer [out] buffer of the selected data block
359 * @param size [out] size of the selected data block
361 int GetDataBuffer(const AliHLTUInt8_t* &pBuffer, AliHLTUInt32_t& size);
364 * Release buffer after use.
365 * @param pBuffer [in] buffer of the selected data block
367 int ReleaseDataBuffer(const AliHLTUInt8_t* pBuffer);
370 * Add the current data block to the selection.
371 * Note: enables also the block selection
373 int SelectDataBlock();
376 * Add the all data blocks of a certain handler to the selection.
377 * Note: enables also the block selection
379 int SelectDataBlocks(const AliHLTOUTHandlerListEntry* pHandlerDesc);
382 * Enable the selection of data blocks.
384 int EnableBlockSelection();
387 * Disable the selection of data blocks.
389 int DisableBlockSelection();
392 * Reset the data block selection.
393 * Resets the selection list, none of the blocks is selected.
395 int ResetBlockSelection();
398 * Mark the current block as processed.
400 int MarkDataBlockProcessed();
403 * Mark all data blocks of a certain handler processed.
405 int MarkDataBlocksProcessed(const AliHLTOUTHandlerListEntry* pHandlerDesc);
408 * Add a sub collection to the HLTOUT.
410 int AddSubCollection(AliHLTOUT* pCollection);
413 * Release a previously added sub collection.
415 int ReleaseSubCollection(AliHLTOUT* pCollection);
418 * Get module agent for the selected data block.
420 AliHLTModuleAgent* GetAgent();
423 * Get handler for the selected data block.
425 AliHLTOUTHandler* GetHandler();
428 * Convert data buffer to ESD.
429 * The buffer is supposed to describe a streamed AliESDEvent object.
430 * If no target object is specified, the ESD is written to a file AliHLTdetESDs.root,
431 * where 'det' is derived from the data type origin. Each time the function is invoked
432 * a new event is created. Dummy events are added if the previous events did not contain
434 * The function needs AliRoot and might not be implemented by all AliHLTOUT
436 * a data block of this specification.
437 * @param pBuffer [in] the data buffer
438 * @param size [in] data buffer size
439 * @param dt [in] data type of the block
440 * @param tgtesd [out] optional target
442 virtual int WriteESD(const AliHLTUInt8_t* pBuffer, AliHLTUInt32_t size, AliHLTComponentDataType dt, AliESDEvent* tgtesd=NULL) const;
444 enum AliHLTOUTByteOrder {
445 /** no data block selected */
446 kInvalidByteOrder=-1,
452 enum AliHLTOUTDataType {
462 * Check byte order of selected block
464 AliHLTOUTByteOrder CheckByteOrder();
467 * Check alignment of selected block
469 int CheckAlignment(AliHLTOUT::AliHLTOUTDataType type);
472 * Helper function to byte swap a 64 bit value.
474 static AliHLTUInt64_t ByteSwap64(AliHLTUInt64_t src);
477 * Helper function to byte swap a 32 bit value.
479 static AliHLTUInt32_t ByteSwap32(AliHLTUInt32_t src);
482 * Insert a handler item.
483 * The current list entries are checked if the handler is already in
484 * the list. It is added if not in the list, otherwise the block index
485 * is added to the existing entry.
486 * @param list the handler list
487 * @param entry handler list entry
488 * @return 0 if added, EEXIST (non negative!) if merged with existing entry <br>
489 * neg. error code if failed
491 static int InsertHandler(AliHLTOUTHandlerListEntryVector& list, const AliHLTOUTHandlerListEntry &entry);
495 * Add a block descriptor.
496 * This is done by the child classes generating the index. The AliHLTOUT
497 * object must be locked for index generation.
498 * @param desc the block descriptor
499 * @return 0 if success, -EPERM if access denied
501 int AddBlockDescriptor(const AliHLTOUTBlockDescriptor desc);
504 * Print output or suppress.
506 bool BeVerbose() {return fbVerbose;}
511 void SwitchVerbosity(bool verbose) {fbVerbose=verbose;}
514 /** copy constructor prohibited */
515 AliHLTOUT(const AliHLTOUT&);
516 /** assignment operator prohibited */
517 AliHLTOUT& operator=(const AliHLTOUT&);
520 * Internal status flags
523 /** the HLTOUT object is locked with the current data block selection */
525 /** childs can add block descriptors */
527 /** user of the data block has checked the byte order */
528 kByteOrderChecked = 0x4,
529 /** warning on byte order missmatch has been printed */
530 kByteOrderWarning = 0x8,
531 /** user of the data block has checked the alignment */
532 kAlignmentChecked = 0x10,
533 /** warning on alignment missmatch has been printed */
534 kAlignmentWarning = 0x20,
535 /** enable block selection list */
536 kBlockSelection = 0x40,
537 /** skip processed data blocks */
538 kSkipProcessed = 0x80,
539 /** marked as sub collection */
540 kIsSubCollection = 0x100
544 * Generate the index of the HLTOUT data.
545 * Must be implemented by the child classes.
547 virtual int GenerateIndex()=0;
550 * Find AliHLTOUTHandler objects for the data blocks.
551 * The available AliHLTModuleAgents are probed whether they provide
552 * handlers for data processing.
557 * Get the data buffer
558 * @param index [in] index of the block
559 * @param pBuffer [out] buffer of the selected data block
560 * @param size [out] size of the selected data block
562 virtual int GetDataBuffer(AliHLTUInt32_t index, const AliHLTUInt8_t* &pBuffer,
563 AliHLTUInt32_t& size)=0;
566 * Check byte order of data block
568 virtual AliHLTOUTByteOrder CheckBlockByteOrder(AliHLTUInt32_t index)=0;
571 * Check alignment of data block
573 virtual int CheckBlockAlignment(AliHLTUInt32_t index, AliHLTOUT::AliHLTOUTDataType type)=0;
576 * Select the data block of data type and specification of the previous
577 * call to @ref SelectFirstDataBlock. Core function of @ref SelectFirstDataBlock
578 * and @ref SelectNextDataBlock, starts to find a block at the current list
581 * The data block is searched from the conditions of fSearchDataType,
582 * fSearchSpecification, fSearchHandlerType and the selection list.
584 * @return identifier >=0 if success, neg. error code if failed <br>
585 * -ENOENT if no block found <br>
586 * -EPERM if access denied (object locked)
588 int FindAndSelectDataBlock();
592 * @param flag flag to set
593 * @return current status flags
595 unsigned int SetStatusFlag(unsigned int flag) {return fFlags|=flag;}
599 * @param flag flag to clear
600 * @return current status flags
602 unsigned int ClearStatusFlag(unsigned int flag) {return fFlags&=~flag;}
606 * @param flag flag to check
607 * @return 1 if flag is set
609 int CheckStatusFlag(unsigned int flag) const {return (fFlags&flag)==flag;}
612 * Find handler description for a certain block index.
614 const AliHLTOUTHandlerListEntry& FindHandlerDesc(AliHLTUInt32_t blockIndex);
617 * Internal New function for the external HLTOUT instances.
618 * Currently supported classes are AliHLTOUTRawReader and
619 * AliHLTOUTDigitReader, both implemented in libHLTrec.so.
621 static AliHLTOUT* New(const char* classname);
624 * Set the RawReader as parameter.
625 * The function is for internal use only in conjunction with the
628 virtual void SetParam(AliRawReader* pRawReader);
631 * Set the RunLoader as parameter
632 * The function is for internal use only in conjunction with the
635 virtual void SetParam(TTree* pDigitTree, int event=-1);
637 /** data type for the current block search, set from @ref SelectFirstDataBlock */
638 AliHLTComponentDataType fSearchDataType; //!transient
640 /** data specification for the current block search */
641 AliHLTUInt32_t fSearchSpecification; //!transient
643 /** handler type for the current block search */
644 AliHLTModuleAgent::AliHLTOUTHandlerType fSearchHandlerType; // !transient
646 /** instance flags: locked, collecting, ... */
647 unsigned int fFlags; //!transient
649 /** list of block descriptors */
650 AliHLTOUTBlockDescriptorVector fBlockDescList; //!transient
652 /** current position in the list */
653 unsigned int fCurrent; //!transient
655 /** data buffer under processing */
656 const AliHLTUInt8_t* fpBuffer; //!transient
658 /** list of AliHLTOUTHandlers */
659 AliHLTOUTHandlerListEntryVector fDataHandlers; // !transient
661 /** verbose or silent output */
662 bool fbVerbose; //!transient
664 ClassDef(AliHLTOUT, 3)