SimpleEventBuilder/provider.cxx

135 lines
4.5 KiB
C++
Raw Normal View History

2023-08-06 15:10:18 +00:00
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <random>
#include <sys/types.h>
#include <unistd.h>
#define START_OF_HEADER_MARKER 0xee1234ee
#define MIN_FRAGMENTS 0
#define MAX_FRAGMENTS 100
typedef struct Header {
uint32_t startOfHeaderMarker;
uint32_t headerSize;
uint32_t fragmentSize;
uint32_t runNumber;
uint32_t detectorEventNumber;
uint32_t numberOfStatusElements;
uint32_t *statusElementsArray;
friend std::ostream& operator <<(std::ostream& os, Header const& header)
{
return os << std::setw(8) << std::setfill('0') << header.startOfHeaderMarker << '\n'
<< std::setw(8) << std::setfill('0') << header.headerSize << '\n'
<< std::setw(8) << std::setfill('0') << header.fragmentSize << '\n'
<< std::setw(8) << std::setfill('0') << header.runNumber << '\n'
<< std::setw(8) << std::setfill('0') << header.detectorEventNumber << '\n'
<< std::setw(8) << std::setfill('0') << header.numberOfStatusElements << '\n';
}
} Header;
typedef struct Fragment {
Header header;
uint32_t *payloadElements;
} Fragment;
enum ERROR_CODES {
INCORRECT_ERROR = (1 << 0),
CORRUPTED_ERROR = (1 << 1),
MISSING_DATA_ERROR = (1 << 2),
TIMEOUT_ERROR = (1 << 3)
};
/*
int rndError(float p){
float rndFloat = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
return rndFloat < p;
}*/
int main(int argc, char* argv[]) {
//will use it later to obtain random number from hardware
std::random_device rd;
std::mt19937 generator(42);
std::discrete_distribution<uint8_t> discrete_distr({0.9, 0.1});
std::normal_distribution<float> normal_distr(25., 10.);
std::uniform_int_distribution<uint32_t> unif_int_distib;
std::uniform_real_distribution<float> unif_float_distrib(0.5, 2);
//commenting all old_style generation
//srand (static_cast <unsigned> (42));
bool out_condition = true;
uint32_t event_number = 0;
uint32_t run_number = 0;
if (argc > 1) {
run_number = atoi(argv[1]);
}
while (true) {
Header header;
header.startOfHeaderMarker = START_OF_HEADER_MARKER;
header.runNumber = run_number;
header.detectorEventNumber = event_number;
event_number++;
header.numberOfStatusElements = 0;
//if swap to old style just change with function above and prob 0.1 for 1
uint8_t incorrectError = discrete_distr(generator);
uint8_t corruptedError = discrete_distr(generator);
uint8_t missingDataError = discrete_distr(generator);
uint8_t timeoutError = discrete_distr(generator);
uint32_t firstStatusElement = 0x0;
if (incorrectError) firstStatusElement |= INCORRECT_ERROR;
if (corruptedError) firstStatusElement |= CORRUPTED_ERROR;
if (missingDataError) firstStatusElement |= MISSING_DATA_ERROR;
if (timeoutError) firstStatusElement |= TIMEOUT_ERROR;
if (firstStatusElement != 0x0) {
header.numberOfStatusElements = 1;
header.statusElementsArray = new uint32_t[header.numberOfStatusElements];
header.statusElementsArray[0] = firstStatusElement;
}
uint32_t payload_size = std::max(std::min(static_cast<int>(std::round(normal_distr(generator))), MAX_FRAGMENTS), MIN_FRAGMENTS);
header.headerSize = 6 + header.numberOfStatusElements;
header.fragmentSize = header.headerSize + payload_size;
Fragment fragment;
fragment.header = header;
fragment.payloadElements = new uint32_t[payload_size];
for (uint32_t i = 0; i < payload_size; i++) {
fragment.payloadElements[i] = unif_int_distib(generator);
}
std::cout << std::hex << std::setw(8) << std::setfill('0');
std::cout << fragment.header;
for (uint8_t i = 0; i < fragment.header.numberOfStatusElements; i++) {
std::cout << "status element " << std::setw(8) << std::setfill('0') << fragment.header.statusElementsArray[i] << std::endl;
}
for (uint32_t i = 0; i < payload_size; i++) {
std::cout << "payload element " << std::setw(8) << std::setfill('0') << fragment.payloadElements[i] << std::endl;
}
std::cout << std::endl;
if (header.numberOfStatusElements > 0){
delete [] fragment.header.statusElementsArray;
}
delete [] fragment.payloadElements;
sleep(unif_float_distrib(generator));
}
return 0;
}