Project
Loading...
Searching...
No Matches
rawdump.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
16#include "DumpBuffer.h"
21#include "boost/program_options.hpp"
22#include <cstdint>
23#include <fmt/format.h>
24#include <fstream>
25#include <gsl/span>
26#include <iostream>
27#include <map>
28#include <optional>
29#include <rapidjson/document.h>
30#include <rapidjson/stringbuffer.h>
31#include <rapidjson/writer.h>
32#include <string>
33
34namespace po = boost::program_options;
35
36namespace o2::header
37{
38extern std::ostream& operator<<(std::ostream&, const o2::header::RAWDataHeaderV4&);
39}
40
41using namespace o2::mch::raw;
43
45{
46 public:
47 DumpOptions(unsigned int maxNofRDHs, bool showRDHs, bool jsonOutput)
48 : mMaxNofRDHs{maxNofRDHs == 0 ? std::numeric_limits<unsigned int>::max() : maxNofRDHs}, mShowRDHs{showRDHs}, mJSON{jsonOutput} {}
49
50 unsigned int maxNofRDHs() const
51 {
52 return mMaxNofRDHs;
53 }
54
55 bool showRDHs() const
56 {
57 return mShowRDHs;
58 }
59
60 bool json() const
61 {
62 return mJSON;
63 }
64
65 std::optional<uint16_t> cruId() const
66 {
67 return mCruId;
68 }
69
70 void cruId(uint16_t c) { mCruId = c; }
71
72 private:
73 unsigned int mMaxNofRDHs;
74 bool mShowRDHs;
75 bool mJSON;
76 std::optional<uint16_t> mCruId{std::nullopt};
77};
78
80 double mean{0};
81 double rms{0};
82 double q{0};
83 int n{0};
84
85 void incr(int v)
86 {
87 n++;
88 auto newMean = mean + (v - mean) / n;
89 q += (v - newMean) * (v - mean);
90 rms = sqrt(q / n);
91 mean = newMean;
92 }
93};
94
95std::ostream& operator<<(std::ostream& os, const ChannelStat& s)
96{
97 os << fmt::format("MEAN {:7.3f} RMS {:7.3f} NSAMPLES {:5d} ", s.mean, s.rms, s.n);
98 return os;
99}
100
101std::map<std::string, ChannelStat> rawdump(std::string input, DumpOptions opt)
102{
103 std::ifstream in(input.c_str(), std::ios::binary);
104 if (!in.good()) {
105 std::cout << "could not open file " << input << "\n";
106 return {};
107 }
108 constexpr size_t pageSize = 8192;
109
110 std::array<std::byte, pageSize> buffer;
111 gsl::span<const std::byte> page(buffer);
112
113 size_t nrdhs{0};
114
115 const auto patchPage = [&](gsl::span<std::byte> rdhBuffer) {
116 auto rdhPtr = const_cast<void*>(reinterpret_cast<const void*>(rdhBuffer.data()));
117 nrdhs++;
118 auto cruId = o2::raw::RDHUtils::getCRUID(rdhPtr);
119 if (opt.cruId().has_value()) {
120 cruId = opt.cruId().value();
122 }
123 auto endpoint = o2::raw::RDHUtils::getEndPointID(rdhPtr);
124 o2::raw::RDHUtils::setFEEID(rdhPtr, cruId * 2 + endpoint);
125 if (opt.showRDHs()) {
126 std::cout << nrdhs << "--\n";
128 }
129 };
130
131 struct Counters {
132 std::map<std::string, int> uniqueDS;
133 std::map<std::string, int> uniqueChannel;
134 std::map<std::string, ChannelStat> statChannel;
135 int ndigits{0};
136 } counters;
137
138 const auto channelHandler =
139 [&](DsElecId dsId, uint8_t channel, o2::mch::raw::SampaCluster sc) {
140 auto s = asString(dsId);
141 counters.uniqueDS[s]++;
142 auto ch = fmt::format("{}-CH{}", s, channel);
143 counters.uniqueChannel[ch]++;
144 auto& chanstat = counters.statChannel[ch];
145 if (sc.isClusterSum()) {
146 chanstat.incr(sc.chargeSum);
147 } else {
148 for (auto d = 0; d < sc.nofSamples(); d++) {
149 chanstat.incr(sc.samples[d]);
150 }
151 }
152 counters.ndigits++;
153 };
154
156
157 size_t npages{0};
158 uint64_t bytesRead{0};
159
160 char* cbuffer = reinterpret_cast<char*>(&buffer[0]);
161 char* cpayload = reinterpret_cast<char*>(&buffer[sizeof(o2::header::RDHAny)]);
162 const void* rdhPtr = reinterpret_cast<const void*>(&buffer[0]);
163
164 while (npages < opt.maxNofRDHs()) {
165
166 // read the next RDH, stop if no more data is available
167 in.read(cbuffer, sizeof(o2::header::RDHAny));
168 if (in.fail()) {
169 break;
170 }
171
172 auto readSize = o2::raw::RDHUtils::getOffsetToNext(rdhPtr) - sizeof(o2::header::RDHAny);
173 in.read(cpayload, readSize);
174 if (in.fail()) {
175 break;
176 }
177
178 npages++;
179 bytesRead += in.gcount();
180 if (!decode) {
181 DecodedDataHandlers handlers;
182 handlers.sampaChannelHandler = channelHandler;
183 decode = createPageDecoder(page, handlers);
184 }
185 patchPage(buffer);
186 decode(page);
187 }
188
189 if (!opt.json()) {
190 std::cout << counters.ndigits << " digits seen - " << nrdhs << " RDHs seen - " << npages << " npages read\n";
191 std::cout << "#unique DS=" << counters.uniqueDS.size() << " #unique Channel=" << counters.uniqueChannel.size() << "\n";
192 }
193
194 return counters.statChannel;
195}
196
197void output(const std::map<std::string, ChannelStat>& channels)
198{
199 rapidjson::StringBuffer buffer;
200 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
201
202 writer.StartObject();
203 writer.Key("channels");
204 writer.StartArray();
205 for (auto s : channels) {
206 writer.StartObject();
207 writer.Key("id");
208 writer.String(s.first.c_str());
209 writer.Key("ped");
210 writer.Double(s.second.mean);
211 writer.Key("noise");
212 writer.Double(s.second.rms);
213 writer.Key("nof_samples");
214 writer.Int(s.second.n);
215 writer.EndObject();
216 }
217 writer.EndArray();
218 writer.EndObject();
219 std::cout << buffer.GetString() << "\n";
220}
221
222int main(int argc, char* argv[])
223{
224 std::string prefix;
225 std::vector<int> detElemIds;
226 std::string inputFile;
227 po::variables_map vm;
228 po::options_description generic("Generic options");
229 unsigned int nrdhs{0};
230 bool showRDHs{false};
231 bool userLogic{false}; // default format is bareformat...
232 bool chargeSum{false}; //... in sample mode
233 bool jsonOutput{false};
234
235 // clang-format off
236 generic.add_options()
237 ("help,h", "produce help message")
238 ("input-file,i", po::value<std::string>(&inputFile)->required(), "input file name")
239 ("nrdhs,n", po::value<unsigned int>(&nrdhs), "number of RDHs to go through")
240 ("showRDHs,s",po::bool_switch(&showRDHs),"show RDHs")
241 ("chargeSum,c",po::bool_switch(&chargeSum),"charge sum format")
242 ("json,j",po::bool_switch(&jsonOutput),"output means and rms in json format")
243 ("cru",po::value<uint16_t>(),"force cruId")
244 ;
245 // clang-format on
246
247 po::options_description cmdline;
248 cmdline.add(generic);
249
250 po::store(po::command_line_parser(argc, argv).options(cmdline).run(), vm);
251
252 if (vm.count("help")) {
253 std::cout << generic << "\n";
254 return 2;
255 }
256
257 try {
258 po::notify(vm);
259 } catch (boost::program_options::error& e) {
260 std::cout << "Error: " << e.what() << "\n";
261 exit(1);
262 }
263
264 DumpOptions opt(nrdhs, showRDHs, jsonOutput);
265
266 if (vm.count("cru")) {
267 opt.cruId(vm["cru"].as<uint16_t>());
268 }
269
270 auto statChannel = rawdump(inputFile, opt);
271
272 if (jsonOutput) {
273 output(statChannel);
274 }
275 return 0;
276}
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
std::map< std::string, ChannelStat > rawdump(std::string input, DumpOptions opt)
Definition rawdump.cxx:101
Definition of the RAW Data Header.
uint8_t endpoint
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
std::optional< uint16_t > cruId() const
Definition rawdump.cxx:65
void cruId(uint16_t c)
Definition rawdump.cxx:70
unsigned int maxNofRDHs() const
Definition rawdump.cxx:50
bool showRDHs() const
Definition rawdump.cxx:55
DumpOptions(unsigned int maxNofRDHs, bool showRDHs, bool jsonOutput)
Definition rawdump.cxx:47
bool json() const
Definition rawdump.cxx:60
GLdouble n
Definition glcorearb.h:1982
GLuint buffer
Definition glcorearb.h:655
GLint GLint GLsizei GLuint * counters
Definition glcorearb.h:3985
const GLdouble * v
Definition glcorearb.h:832
O2 data header classes and API, v0.1.
Definition DetID.h:49
std::ostream & operator<<(std::ostream &os, const o2::header::RDHAny &rdh)
Definition RDHManip.cxx:84
std::function< void(Page buffer)> PageDecoder
Definition PageDecoder.h:28
PageDecoder createPageDecoder(RawBuffer rdhBuffer, DecodedDataHandlers decodedDataHandlers)
will be called for each decoded Sampa packet and in case of decoding errors
std::string asString(const SampaCluster &sc)
int showRDHs(gsl::span< const std::byte > buffer)
Dump the RDHs found in the buffer.
Definition RDHManip.cxx:215
std::ostream & operator<<(std::ostream &stream, o2::InteractionRecord const &ir)
Defining DataPointCompositeObject explicitly as copiable.
double rms
Definition rawdump.cxx:81
void incr(int v)
Definition rawdump.cxx:85
double mean
Definition rawdump.cxx:80
double q
Definition rawdump.cxx:82
Piece of data for one Sampa channel.
static void setLinkID(H &rdh, uint8_t v, NOTPTR(H))
Definition RDHUtils.h:256
static void printRDH(const RDHv4 &rdh)
Definition RDHUtils.cxx:26
static void setFEEID(RDHv4 &rdh, uint16_t v)
Definition RDHUtils.h:146
constexpr size_t max
#define main
coder decode(ctfImage, triggersD, clustersD)
std::vector< ChannelData > channels