BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Event/eformat/include/eformat/util.h
Go to the documentation of this file.
1// Dear emacs, this is -*- c++ -*-
2
3/**
4 * @file eformat/util.h
5 * @author <a href="mailto:Andre.dos.Anjos@cern.ch>André Rabello dos
6 * ANJOS</a>
7 * $Author: zhangy $
8 * $Revision: 1.1.1.1 $
9 * $Date: 2009/06/19 07:35:41 $
10 *
11 * @brief Defines a set of utilities common to many event operations.
12 */
13
14#ifndef EFORMAT_UTIL_H
15#define EFORMAT_UTIL_H
16
17#include "eformat/HeaderMarker.h"
18#include "eformat/WrongMarkerIssue.h"
19#include <cstdlib>
20#include <fstream>
21#include <stdint.h>
22
23namespace eformat {
24
25 /**
26 * This function will read the next fragment in a normal, already opened,
27 * std::fstream. The space for the fragment will be allocated dynamically and
28 * the user should free it. Otherwise, if the user wants to re-use a
29 * pre-allocated memory space, the second and third arguments can be given,
30 * in which case the function will check if the space is enough. If the space
31 * is not enough, NULL is returned.
32 *
33 * @param fs The input filestream
34 * @param addr The optional user allocated space
35 * @param size The optional size, in bytes, of the allocated space
36 */
37 uint32_t* next_fragment( std::fstream& fs, uint32_t* addr = 0, size_t size = 0 );
38
39 /**
40 * Returns pointers (to words) to the start of each ROD fragment block in a
41 * piece of memory. This is primarily intended for LVL2 supervisor usage.
42 *
43 * @warning This will invert the data order, meaning the last ROD will be the
44 * first in the vector to appear, due to the way the ROD readout has to
45 * happen (back to front of the stream). If that is not satisfactory, you
46 * have to sort back the vector yourself.
47 *
48 * @param block_start The start address of the data block you want to dig the
49 * ROD pointers from
50 * @param block_size The size of the block, in 32-bit words
51 * @param rod A (optional) pointer to set of pre-allocated pointers
52 * @param rod_size A (optional) pointer to set of pre-allocated size_t's,
53 * where the size of each ROD, in words, will be stored.
54 * @param max_count The maximum number of blocks to dig out from
55 * <tt>block_start</tt>, if rod is not NULL. If rod is NULL, this is
56 * meaningless.
57 *
58 * @return The number of ROD fragments the function actually found on the
59 * block, irrespective of max_count.
60 */
61 size_t find_rods( const uint32_t* block_start, size_t block_size, const uint32_t** rod = 0,
62 uint32_t* rod_size = 0, size_t max_count = 0 );
63
64 /**
65 * A generic method to find all fragments in a contiguous area of
66 * memory. These fragments cannot be RODFragments. For that, use the
67 * eformat::find_rods().
68 *
69 * @param marker The marker you are searching for.
70 * @param block_start A pointer to the block start
71 * @param block_size The overall size of this block, in 32-bit words
72 * @param frag A (optional) preallocated set of pointers to hold the found
73 * fragments. If frag is NULL, only counts the number of children.
74 * @param max_count The maximum number of fragments I'll search for, if frag
75 * is not NULL. If frag is NULL, this flag has no meaning.
76 *
77 * @return The number of fragments the function actually found, irrespective
78 * of max_count.
79 */
80 template <class TPointer>
81 size_t find_fragments( eformat::HeaderMarker marker, TPointer block_start, size_t block_size,
82 TPointer* frag = 0, size_t max_count = 0 );
83
84 /**
85 * Gets pointers to all ROB fragments from a fragment of a type which is not
86 * known in advance.
87 *
88 * @param fragment The top level fragment to extract the other ROBFragment's
89 * from.
90 * @param rod A pointer to set of pre-allocated pointers
91 * @param max_count The maximum number of blocks to dig out from
92 * <tt>block_start</tt>.
93 *
94 * @return The number of ROBFragments the function actually found
95 */
96 size_t get_robs( const uint32_t* fragment, const uint32_t** rob, size_t max_count );
97
98} // namespace eformat
99
100/**
101 * Prints in hexadecimal format, in an ostream
102 */
103#ifndef HEX
104# define HEX( m ) "0x" << std::hex << (int)m << std::dec << " (" << (int)m << ")"
105#endif
106
107template <class TPointer>
108size_t eformat::find_fragments( eformat::HeaderMarker marker, TPointer block_start,
109 size_t block_size, TPointer* frag, size_t max_count ) {
110 uint32_t counter = 0;
111 TPointer next = block_start;
112 TPointer endp = block_start;
113 endp += block_size;
114 while ( next < endp )
115 {
116 if ( next[0] != marker ) throw EFORMAT_WRONG_MARKER( next[0], marker );
117 if ( frag && counter < max_count ) frag[counter] = next;
118 ++counter;
119 next += next[1];
120 }
121 return counter;
122}
123
124#endif // EFORMAT_UTIL_H
#define fs
#define EFORMAT_WRONG_MARKER(current, expected)
size_t find_fragments(eformat::HeaderMarker marker, TPointer block_start, size_t block_size, TPointer *frag=0, size_t max_count=0)
uint32_t * next_fragment(std::fstream &fs, uint32_t *addr=0, size_t size=0)
Definition util.cxx:24
size_t get_robs(const uint32_t *fragment, const uint32_t **rob, size_t max_count)
Definition util.cxx:104
size_t find_rods(const uint32_t *block_start, size_t block_size, const uint32_t **rod=0, uint32_t *rod_size=0, size_t max_count=0)
Definition util.cxx:83