Project
Loading...
Searching...
No Matches
readGBTFrames.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 <boost/program_options.hpp>
18#include <iostream>
19#include <iomanip>
20
21#include <thread>
22#include <mutex>
23#include <chrono>
24
28
29namespace bpo = boost::program_options;
30
31std::mutex mtx;
32
33bool isVector1(std::vector<int>& vec)
34{
35 for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
36 if ((*it) != 1) {
37 return false;
38 }
39 }
40 return true;
41}
42
43void addData(o2::tpc::GBTFrameContainer& container, std::string& infile, int frames, std::string rorcFlavor, int& done)
44{
45 done = 0;
46 mtx.lock();
47 std::cout << infile << std::endl;
48 mtx.unlock();
49
50 container.addGBTFramesFromBinaryFile(infile, rorcFlavor, frames);
51
52 done = 1;
53}
54
55void readData(o2::tpc::GBTFrameContainer& container, std::vector<std::ofstream*>& outfiles, int& run, int& done)
56{
57 done = 0;
58 std::vector<o2::tpc::HalfSAMPAData> data;
59 int i;
60 while (!run) {
61 std::this_thread::sleep_for(std::chrono::microseconds{100});
62 while (container.getData(data)) {
63 for (i = 0; i < 5; ++i) {
64 std::cout << data[i] << std::endl;
65 // if (outfiles[i] != nullptr) {
66 // outfiles[i]->write(reinterpret_cast<const char*>(&data[i].getData()[0]), 16*sizeof(data[i].getData()[0]));
68 // }
69 }
70 data.clear();
71 }
72 }
73 done = 1;
74}
75
76int main(int argc, char* argv[])
77{
78 // Arguments parsing
79 std::vector<unsigned> size(1, 0);
80 std::vector<unsigned> CRU(1, 0);
81 std::vector<unsigned> link(1, 0);
82 std::vector<std::string> infile(1, "NOFILE");
83 std::vector<std::string> outfile(1, "NOFILE");
84 std::string adcInFile = "NOFILE";
85 std::string rorcFlavor = "grorc";
86 bool checkAdcClock = false;
87 bool compileAdcValues = false;
88 bool keepGbtFrames = false;
89 bool printGbtFrames = false;
90 int frames = -1;
91
92 bpo::variables_map vm;
93 bpo::options_description desc("Allowed options");
94 // clang-format off
95 desc.add_options()
96 ("help,h", "Produce help message.")
97 ("infile,i", bpo::value<std::vector<std::string>>(&infile), "Input data files")
98 ("outfile,o", bpo::value<std::vector<std::string>>(&outfile), "Output data files")
99 (",n", bpo::value<int>(&frames), "Frames to read")
100 (",s", bpo::value<std::vector<unsigned>>(&size), "Container sizes")
101 ("CRU", bpo::value<std::vector<unsigned>>(&CRU), "CRUs")
102 ("link", bpo::value<std::vector<unsigned>>(&link), "links")
103 ("clock,c", bpo::bool_switch(&checkAdcClock), "check ADC clock")
104 ("ADC,a", bpo::bool_switch(&compileAdcValues), "compiles the ADC values")
105 ("keep,k", bpo::bool_switch(&keepGbtFrames), "keeps the GBT frames in memory")
106 ("type,t", bpo::value<std::string>(&rorcFlavor), R"(Type of RORC which generated binary file ("trorc" or "grorc"))")
107 ("print,p", bpo::bool_switch(&printGbtFrames), "print GBT frames")
108 ("file,f", bpo::value<std::string>(&adcInFile), "ADC input file");
109 // clang-format on
110 bpo::store(parse_command_line(argc, argv, desc), vm);
111 bpo::notify(vm);
112
113 // help
114 if (vm.count("help")) {
115 std::cout << desc << std::endl;
116 return EXIT_SUCCESS;
117 }
118
119 if (adcInFile == "NOFILE") {
120
121 // Actual "work"
122 std::chrono::time_point<std::chrono::system_clock> start, end;
123 std::vector<o2::tpc::GBTFrameContainer*> container;
124 unsigned iSize;
125 unsigned iCRU;
126 unsigned iLink;
127 std::vector<int> addData_done(infile.size(), 0);
128 std::vector<int> reading_done(infile.size(), 0);
129 for (int i = 0; i < infile.size(); ++i) {
130 if (size.size() >= infile.size()) {
131 iSize = size[i];
132 } else {
133 iSize = size[0];
134 }
135 if (CRU.size() >= infile.size()) {
136 iCRU = CRU[i];
137 } else {
138 iCRU = CRU[0];
139 }
140 if (link.size() >= infile.size()) {
141 iLink = link[i];
142 } else {
143 iLink = link[0];
144 }
145
146 container.push_back(new o2::tpc::GBTFrameContainer(iSize, iCRU, iLink));
147 container.back()->setEnableAdcClockWarning(checkAdcClock);
148 container.back()->setEnableSyncPatternWarning(false);
149 container.back()->setEnableStoreGBTFrames(keepGbtFrames);
150 container.back()->setEnableCompileAdcValues(compileAdcValues);
151 }
152
153 std::vector<std::thread> threads;
154
155 start = std::chrono::system_clock::now();
156 for (int i = 0; i < infile.size(); ++i) {
157 if (infile[i] != "NOFILE") {
158 threads.emplace_back(addData, std::ref(*container[i]), std::ref(infile[i]), frames, rorcFlavor, std::ref(addData_done[i]));
159 }
160 }
161
162 std::vector<std::vector<std::ofstream*>> outfiles(infile.size());
163 std::ofstream* out;
164 for (int i = 0; i < infile.size(); ++i) {
165 for (int j = 0; j < 3; ++j) {
166 std::string outname;
167 if (i >= outfile.size()) {
168 outfiles[i].push_back(nullptr);
169 } else if (outfile[i] == "NOFILE") {
170 outfiles[i].push_back(nullptr);
171 } else {
172 outname = outfile[i];
173 outname += "_SAMPA_";
174 outname += std::to_string(j);
175 outname += "_LOW";
176 outname += ".adc";
177
178 out = new std::ofstream(outname, std::ios::out | std::ios::binary);
179 outfiles[i].push_back(out);
180 }
181
182 if (j == 2) {
183 continue;
184 }
185 outname = "";
186 if (i >= outfile.size()) {
187 outfiles[i].push_back(nullptr);
188 } else if (outfile[i] == "NOFILE") {
189 outfiles[i].push_back(nullptr);
190 } else {
191 outname = outfile[i];
192 outname += "_SAMPA_";
193 outname += std::to_string(j);
194 outname += "_HIGH";
195 outname += ".adc";
196
197 out = new std::ofstream(outname, std::ios::out | std::ios::binary);
198 outfiles[i].push_back(out);
199 }
200 }
201 threads.emplace_back(readData, std::ref(*container[i]), std::ref(outfiles[i]), std::ref(addData_done[i]), std::ref(reading_done[i]));
202 }
203
204 std::this_thread::sleep_for(std::chrono::seconds{1});
205 std::cout << std::endl;
206 std::cout << " Container | stored frames | processed frames | avail. ADC values " << std::endl;
207 std::cout << "-----------|---------------|------------------|-------------------" << std::endl;
208 while (not isVector1(reading_done)) {
209 std::this_thread::sleep_for(std::chrono::seconds{1});
210 for (std::vector<o2::tpc::GBTFrameContainer*>::iterator it = container.begin(); it != container.end(); ++it) {
211 mtx.lock();
212 std::cout << " " << std::right
213 << std::setw(9) << std::distance(container.begin(), it) << " | "
214 << std::setw(13) << (*it)->getSize() << " | "
215 << std::setw(16) << (*it)->getNFramesAnalyzed() << " | "
216 << std::setw(17) << (*it)->getNentries() << std::endl;
217 mtx.unlock();
218 }
219 }
220
221 for (auto& aThread : threads) {
222 aThread.join();
223 }
224 end = std::chrono::system_clock::now();
225
226 std::cout << std::endl
227 << std::endl;
228 std::cout << "Summary:" << std::endl;
229
230 unsigned framesProcessed = 0;
231 for (std::vector<o2::tpc::GBTFrameContainer*>::iterator it = container.begin(); it != container.end(); ++it) {
232 framesProcessed += (*it)->getNFramesAnalyzed();
233 std::cout << "Container " << std::distance(container.begin(), it) << " analyzed " << (*it)->getNFramesAnalyzed() << " GBT Frames" << std::endl;
234 delete (*it);
235 }
236 std::chrono::duration<float> elapsed_seconds = end - start;
237 std::cout << "In total: " << framesProcessed << " Frames processed in " << elapsed_seconds.count() << "s => " << framesProcessed / elapsed_seconds.count() << " frames/s" << std::endl;
238
239 unsigned word3, word2, word1, word0;
240 if (printGbtFrames) {
241 for (std::vector<o2::tpc::GBTFrameContainer*>::iterator it = container.begin(); it != container.end(); ++it) {
242 std::cout << "Container " << std::distance(container.begin(), it) << ":" << std::endl;
243 for (std::vector<o2::tpc::GBTFrame>::iterator itt = (*it)->begin(); itt != (*it)->end(); ++itt) {
244 itt->getGBTFrame(word3, word2, word1, word0);
245 std::cout << *itt << " ";
246 std::cout << std::hex << std::setfill('0') << std::right << std::setw(8) << word3 << " "
247 << std::setfill('0') << std::right << std::setw(8) << word2 << " "
248 << std::setfill('0') << std::right << std::setw(8) << word1 << " "
249 << std::setfill('0') << std::right << std::setw(8) << word0 << std::dec << std::endl;
250 }
251 }
252 }
253
254 for (std::vector<std::vector<std::ofstream*>>::iterator it = outfiles.begin(); it != outfiles.end(); ++it) {
255 for (std::vector<std::ofstream*>::iterator itt = (*it).begin(); itt != (*it).end(); ++itt) {
256 if ((*itt) != nullptr) {
257 (*itt)->close();
258 delete (*itt);
259 }
260 }
261 }
262
263 } else {
264 std::cout << "Reading from file " << adcInFile << std::endl;
265 std::ifstream inFile(adcInFile, std::ios::in | std::ios::binary);
266
267 if (!inFile.is_open()) {
268 std::cout << "ERROR: can't read file " << adcInFile << std::endl;
269 return EXIT_FAILURE;
270 }
271
272 short adcValues[16];
273 int i;
274 // while (!inFile.eof()) {
275 for (int j = 0; j < 100; ++j) {
276 inFile.read(reinterpret_cast<char*>(&adcValues[0]), 16 * sizeof(adcValues[0]));
277 // inFile.read((char*)&adcValues,16*sizeof(short));
278 for (i = 0; i < 16; ++i) {
279 std::cout << adcValues[i] << "\t";
280 }
281 std::cout << std::endl;
282 }
283 }
284
285 return EXIT_SUCCESS;
286}
Definition of the TPC Digit.
Container class for the GBT Frames.
int32_t i
std::vector< uint16_t > adcValues
bool done
Timeframe settings Average distance in cm of events merged into Randomize distance around average distance of merged events Simulate correct bunch interactions instead of placing only the average number of events A value[n] sets TFlen for[n] collisions in Number of bunches per trainsort Number of bunch trains Instantaneous interaction rate Do not simulate border Total number of collisions to be placed in the interior of all time frames(excluding borders)") AddOption(eventStride
Class for data from one half SAMPA.
uint32_t j
Definition RawData.h:0
GBT Frame container class.
bool getData(std::vector< Digit > &container)
void addGBTFramesFromBinaryFile(std::string fileName, std::string type="grorc", int frames=-1)
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
GLboolean * data
Definition glcorearb.h:298
GLuint start
Definition glcorearb.h:469
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
void readData(o2::tpc::GBTFrameContainer &container, std::vector< std::ofstream * > &outfiles, int &run, int &done)
void addData(o2::tpc::GBTFrameContainer &container, std::string &infile, int frames, std::string rorcFlavor, int &done)
std::mutex mtx
bool isVector1(std::vector< int > &vec)
#define main
std::vector< o2::ctf::BufferType > vec