Project
Loading...
Searching...
No Matches
raw-checker.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
16
17#include <iostream>
18#include <fstream>
19#include <sstream>
20#include <string>
21#include <vector>
22#include "boost/program_options.hpp"
26#include "MIDRaw/CrateMasks.h"
27#include "MIDRaw/Decoder.h"
29#include "MIDRaw/FEEIdConfig.h"
32
33namespace po = boost::program_options;
34
35std::string getOutFilename(const char* inFilename, const char* outDir)
36{
37 std::string basename(inFilename);
38 std::string fdir = "./";
39 auto pos = basename.find_last_of("/");
40 if (pos != std::string::npos) {
41 basename.erase(0, pos + 1);
42 fdir = inFilename;
43 fdir.erase(pos);
44 }
45 basename.insert(0, "check_");
46 basename += ".txt";
47 std::string outputDir(outDir);
48 if (outputDir.empty()) {
49 outputDir = fdir;
50 }
51 if (outputDir.back() != '/') {
52 outputDir += "/";
53 }
54 std::string outFilename = outputDir + basename;
55 return outFilename;
56}
57
58int process(po::variables_map& vm)
59{
60 std::vector<std::string> inputfiles{vm["input"].as<std::vector<std::string>>()};
61
62 std::unique_ptr<o2::mid::Decoder> decoder{nullptr};
64
65 o2::mid::FEEIdConfig feeIdConfig;
66 if (vm.count("feeId-config-file")) {
67 feeIdConfig = o2::mid::FEEIdConfig(vm["feeId-config-file"].as<std::string>().c_str());
68 }
69
70 o2::mid::ElectronicsDelay electronicsDelay;
71 if (vm.count("electronics-delay-file")) {
72 electronicsDelay = o2::mid::readElectronicsDelay(vm["electronics-delay-file"].as<std::string>().c_str());
73 checker.setElectronicsDelay(electronicsDelay);
74 }
75
76 if (vm.count("sync-trigger")) {
77 checker.setSyncTrigger(vm["sync-trigger"].as<uint32_t>());
78 }
79
80 o2::mid::CrateMasks crateMasks;
81 if (vm.count("crate-masks-file")) {
82 crateMasks = o2::mid::CrateMasks(vm["crate-masks-file"].as<std::string>().c_str());
83 }
84 checker.init(crateMasks);
85
86 auto nHBs = vm["nHBs"].as<unsigned long int>();
87 auto nMaxErrors = vm["max-errors"].as<unsigned long int>();
88
89 for (auto& filename : inputfiles) {
90 o2::mid::RawFileReader rawFileReader;
91 if (!rawFileReader.init(filename.c_str())) {
92 return 2;
93 }
94 if (vm.count("custom-memory-size")) {
95 rawFileReader.setCustomPayloadSize(vm["custom-memory-size"].as<uint16_t>());
96 }
97 std::string outFilename = getOutFilename(filename.c_str(), vm["output-dir"].as<std::string>().c_str());
98 std::ofstream outFile(outFilename.c_str());
99 if (!outFile.is_open()) {
100 std::cout << "Error: cannot create " << outFilename << std::endl;
101 return 2;
102 }
103 std::cout << "Writing output to: " << outFilename << " ..." << std::endl;
104
105 std::vector<o2::mid::ROBoard> data;
106 std::vector<o2::mid::ROFRecord> rofRecords;
107 std::vector<o2::mid::ROFRecord> hbRecords;
108
109 checker.clear();
110 unsigned long int iHB = 0;
111 std::stringstream summary;
112 while (rawFileReader.readHB(vm.count("only-closed-HBs") > 0)) {
113 if (!decoder) {
114 auto const* rdhPtr = reinterpret_cast<const o2::header::RDHAny*>(rawFileReader.getData().data());
115 decoder = o2::mid::createDecoder(*rdhPtr, true, electronicsDelay, crateMasks, feeIdConfig);
116 }
117 decoder->process(rawFileReader.getData());
118 rawFileReader.clear();
119 size_t offset = data.size();
120 data.insert(data.end(), decoder->getData().begin(), decoder->getData().end());
121 for (auto& rof : decoder->getROFRecords()) {
122 rofRecords.emplace_back(rof.interactionRecord, rof.eventType, rof.firstEntry + offset, rof.nEntries);
123 }
124 o2::InteractionRecord hb(0, iHB);
125 hbRecords.emplace_back(hb, o2::mid::EventType::Calib, offset, decoder->getData().size());
126 ++iHB;
127 if ((nHBs > 0 && iHB >= nHBs)) {
128 break;
129 }
130 if (!checker.process(data, rofRecords, hbRecords)) {
131 outFile << checker.getDebugMessage() << "\n";
132 }
133 data.clear();
134 rofRecords.clear();
135 hbRecords.clear();
136
137 if (checker.getNEventsFaulty() >= nMaxErrors) {
138 summary << "Too many errors found: abort check!\n";
139 break;
140 }
141 }
142 // Check the remaining data
143 if (data.size() > 0 && !checker.process(data, rofRecords, hbRecords)) {
144 outFile << checker.getDebugMessage() << "\n";
145 }
146 summary << "Number of busy raised: " << checker.getNBusyRaised() << "\n";
147 summary << "Fraction of faulty events: " << checker.getNEventsFaulty() << " / " << checker.getNEventsProcessed() << " = " << static_cast<double>(checker.getNEventsFaulty()) / ((checker.getNEventsProcessed() > 0) ? static_cast<double>(checker.getNEventsProcessed()) : 1.) << "\n";
148 outFile << summary.str();
149 std::cout << summary.str();
150
151 outFile.close();
152 }
153
154 return 0;
155}
156
157int main(int argc, char* argv[])
158{
159 po::variables_map vm;
160 po::options_description generic("Generic options");
161
162 // clang-format off
163 generic.add_options()
164 ("help", "produce help message")
165 ("nHBs", po::value<unsigned long int>()->default_value(0),"Number of HBs read")
166 ("only-closed-HBs", po::value<bool>()->implicit_value(true),"Return only closed HBs")
167 ("custom-memory-size", po::value<uint16_t>()->implicit_value(0x2000 - 0x100),"Ignore read RDH. Use custom memory size")
168 ("max-errors", po::value<unsigned long int>()->default_value(10000),"Maximum number of errors before aborting")
169 ("feeId-config-file", po::value<std::string>(),"Filename with crate FEE ID correspondence")
170 ("crate-masks-file", po::value<std::string>(),"Filename with crate masks")
171 ("electronics-delay-file", po::value<std::string>(),"Filename with electronics delay")
172 ("output-dir", po::value<std::string>()->default_value(""),"Output directory")
173 ("sync-trigger", po::value<unsigned int>(),"Trigger used for synchronisation (default is orbit 0x1)");
174
175
176 po::options_description hidden("hidden options");
177 hidden.add_options()
178 ("input", po::value<std::vector<std::string>>(),"Input filename");
179 // clang-format on
180
181 po::options_description cmdline;
182 cmdline.add(generic).add(hidden);
183
184 po::positional_options_description pos;
185 pos.add("input", -1);
186
187 po::store(po::command_line_parser(argc, argv).options(cmdline).positional(pos).run(), vm);
188 po::notify(vm);
189
190 if (vm.count("help")) {
191 std::cout << "Usage: " << argv[0] << " <input_raw_filename> [input_raw_filename_1 ...]\n";
192 std::cout << generic << std::endl;
193 return 2;
194 }
195 if (vm.count("input") == 0) {
196 std::cout << "no input file specified" << std::endl;
197 return 1;
198 }
199
200 return process(vm);
201}
MID crate masks.
MID raw data decoder.
Delay parameters for MID electronics.
Hardware Id to FeeId mapper.
MID raw file reader.
Definition of the MID event record.
Structure to store the readout board information.
Class to check the raw data.
uint16_t pos
Definition RawData.h:3
void setSyncTrigger(uint32_t syncTrigger)
void setElectronicsDelay(const ElectronicsDelay &electronicsDelay)
Sets the delay in the electronics.
bool process(gsl::span< const ROBoard > localBoards, gsl::span< const ROFRecord > rofRecords, gsl::span< const ROFRecord > pageRecords)
void clear(bool all=false)
void init(const CrateMasks &masks)
std::string getDebugMessage() const
Gets the debug message.
unsigned int getNEventsFaulty() const
Gets the number of faulty events.
unsigned int getNEventsProcessed() const
Gets the number of processed events.
unsigned int getNBusyRaised() const
Gets the number of busy raised.
void setCustomPayloadSize(uint16_t memorySize=0x2000, uint16_t offsetToNext=0x2000)
bool readHB(bool sendCompleteHBs=false)
const std::vector< uint8_t > & getData()
Gets the vector of data.
bool init(const char *inFilename, bool readContinuous=false)
GLboolean * data
Definition glcorearb.h:298
GLintptr offset
Definition glcorearb.h:660
std::unique_ptr< Decoder > createDecoder(const o2::header::RDHAny &rdh, bool isDebugMode, const ElectronicsDelay &electronicsDelay, const CrateMasks &crateMasks, const FEEIdConfig &feeIdConfig)
Definition Decoder.cxx:63
ElectronicsDelay readElectronicsDelay(const char *filename)
std::string filename()
std::string getOutFilename(const char *inFilename, const char *outDir)
#define main