CGEM BOSS 6.6.5.f
BESIII Offline Software System
Loading...
Searching...
No Matches
util.cxx
Go to the documentation of this file.
1//Dear emacs, this is -*- c++ -*-
2
3/**
4 * @file util.cxx
5 * @author <a href="mailto:[email protected]>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 Implements a set of utilities common to many event operations.
12 */
13
14#include "eformat/util.h"
15#include "eformat/ROBFragment.h"
16#include "eformat/ROSFragment.h"
17#include "eformat/SubDetectorFragment.h"
18#include "eformat/FullEventFragment.h"
19#include "ers/StreamFactory.h"
20#include "eformat/WrongMarkerIssue.h"
21
22uint32_t* eformat::next_fragment (std::fstream& fs, uint32_t* addr,
23 size_t size)
24{
25 using namespace eformat;
26
27 off_t offset = fs.tellg();
28 ERS_DEBUG_3("Current stream position is 0x%lx", offset);
29
30 uint32_t data[2];
31 if (fs && fs.good() && ! fs.eof()) {
32 //soft check, so we don't make mistakes
33 fs.read((char*)data, 8);
34 if (!fs.good() || fs.eof()) return 0; // end-of-file
35 switch((HeaderMarker)data[0]) {
36 case FULL_EVENT:
37 case SUB_DETECTOR:
38 case ROS:
39 case ROB:
40 case ROD:
41 break;
42 default:
43 //stop!
44 std::cout << "Word at offset " << HEX(offset) << " is not one of "
45 << HEX(FULL_EVENT) << ", "
46 << HEX(SUB_DETECTOR) << ", "
47 << HEX(ROS) << ", "
48 << HEX(ROB) << "or "
49 << HEX(ROS) << ". Stopping execution..." << std::endl;
50 return 0;
51 }
52 }
53 else return 0; //file is over
54
55 //data[1] is the fragment size, in words. Take it and read the fragment
56 ERS_DEBUG_3("Resetting stream position to 0x%lx...", offset);
57 fs.seekg(offset);
58 if (addr && (size >= (data[1]*4))) {
59 //space is preallocated and checked
60 ERS_DEBUG_3("Reading fragment data...");
61 fs.read((char*)addr, data[1]*4);
62 ERS_DEBUG_2("Size of fragment readout is %u bytes", 4*data[1]);
63 ERS_DEBUG_3("Stream position is 0x%lx", offset = fs.tellg());
64 return addr;
65 }
66 else if (addr) {
67 std::cout << "The fragment at offset " << HEX(offset) << " has "
68 << data[1]*4 << " bytes and you provided only "
69 << size << " bytes in your buffer. Stopping execution..."
70 << std::endl;
71 return 0;
72 }
73
74 //if I get here, is because I have to allocate space myself
75 ERS_DEBUG_3("Allocating fragment data storage of size %ud bytes", 4*data[1]);
76 uint32_t* retval = new uint32_t[data[1]];
77 ERS_DEBUG_3("Reading fragment data...");
78 fs.read((char*)retval, data[1]*4);
79 ERS_DEBUG_2("Size of fragment readout is %u bytes", 4*data[1]);
80 ERS_DEBUG_3("Stream position is 0x%lx", offset = fs.tellg());
81 return retval;
82}
83
84size_t eformat::find_rods (const uint32_t* block_start, size_t block_size,
85 const uint32_t** rod, uint32_t* rod_size,
86 size_t max_count)
87{
88 const uint32_t* block_end = block_start + block_size;
89 size_t counter = 0;
90 while (block_end > block_start) {
91 uint32_t curr_rod_size = 12; //< base size for a ROD as eformat 2.4
92 curr_rod_size += *(block_end-3) + *(block_end-2); //status and data sizes
93 block_end -= curr_rod_size;
94 if (rod && counter < max_count) {
95 if (block_end[0] != eformat::ROD)
96 throw EFORMAT_WRONG_MARKER(block_end[0], eformat::ROD);
97 rod_size[counter] = curr_rod_size;
98 rod[counter] = block_end;
99 }
100 ++counter;
101 }
102 return counter;
103}
104
105size_t eformat::get_robs (const uint32_t* fragment, const uint32_t** rob,
106 size_t max_count)
107{
108 using namespace eformat;
109 ERS_DEBUG_1("Getting all ROB's from 0x%x...", fragment[0]);
110
111 size_t counter = 0;
112 switch ((eformat::HeaderMarker)(fragment[0])) {
113 case ROD:
114 return 0;
115 case ROB:
116 {
117 if ( max_count > 0 ) {
118 rob[0] = fragment;
119 ++counter;
120 }
121 else return 0; //not enough space
122 }
123 break;
124 case ROS:
125 {
127 counter += ros.children(rob, max_count);
128 ERS_DEBUG_1("ROS 0x%x contains %d ROB's", ros.source_id(), counter);
129 }
130 break;
131 case SUB_DETECTOR:
132 {
134 const uint32_t* ros[256];
135 size_t ros_counter = sd.children(ros, 256);
136 for (size_t i=0; i<ros_counter; ++i)
137 counter += get_robs(ros[i], &rob[counter], max_count - counter);
138 ERS_DEBUG_1("Subdetector 0x%x contains %d ROB's", sd.source_id(),
139 counter);
140 }
141 break;
142 case FULL_EVENT:
143 {
145 const uint32_t* sd[64];
146 size_t sd_counter = fe.children(sd, 64);
147 for (size_t i=0; i<sd_counter; ++i)
148 counter += get_robs(sd[i], &rob[counter], max_count - counter);
149 ERS_DEBUG_1("Fullevent 0x%x contains %d ROB's", fe.source_id(), counter);
150 }
151 break;
152 }
153
154 return counter;
155}
TTree * data
#define EFORMAT_WRONG_MARKER(current, expected)
virtual uint32_t children(TPointer *p, size_t max) const
uint32_t * next_fragment(std::fstream &fs, uint32_t *addr=0, size_t size=0)
Definition: util.cxx:22
size_t get_robs(const uint32_t *fragment, const uint32_t **rob, size_t max_count)
Definition: util.cxx:105
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:84