forked from mcoquet642/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataPointGenerator.cxx
More file actions
147 lines (131 loc) · 5.35 KB
/
DataPointGenerator.cxx
File metadata and controls
147 lines (131 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#include "DetectorsDCS/AliasExpander.h"
#include "DetectorsDCS/DataPointGenerator.h"
#include "DetectorsDCS/DataPointCreator.h"
#include "DetectorsDCS/DataPointCompositeObject.h"
#include "DetectorsDCS/StringUtils.h"
#include "Framework/Logger.h"
#include <fmt/format.h>
#include <random>
#include <utility>
#include <type_traits>
#include <cstdint>
namespace
{
std::pair<uint32_t, uint16_t> getDate(const std::string& refDate)
{
uint32_t seconds;
if (refDate.empty()) {
auto current = std::time(nullptr);
auto t = std::localtime(¤t);
seconds = mktime(t);
} else {
std::tm t{};
std::istringstream ss(refDate);
ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S");
if (ss.fail()) { // let's see if it was passed as a TDatime, as SQL string
std::tm tt{};
std::istringstream sss(refDate);
sss >> std::get_time(&tt, "%Y-%m-%d %H:%M:%S");
if (sss.fail()) {
std::tm ttt{};
std::istringstream ssss(refDate);
ssss >> std::get_time(&tt, "%Y-%B-%d %H:%M:%S");
if (ssss.fail()) {
LOG(ERROR) << "We cannot parse the date";
}
seconds = mktime(&ttt);
} else {
seconds = mktime(&tt);
}
} else {
seconds = mktime(&t);
}
}
uint16_t msec = 5;
return std::make_pair(seconds, msec);
}
} // namespace
namespace o2::dcs
{
//std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
template <typename T>
std::vector<o2::dcs::DataPointCompositeObject>
generateRandomDataPoints(const std::vector<std::string>& aliases,
T minValue, T maxValue, std::string refDate)
{
std::vector<o2::dcs::DataPointCompositeObject> dpcoms;
static_assert(std::is_arithmetic<T>::value, "T must be an arithmetic type");
typedef typename std::conditional<std::is_integral<T>::value,
std::uniform_int_distribution<T>,
std::uniform_real_distribution<T>>::type distType;
std::random_device rd;
std::mt19937 mt(rd());
distType dist{minValue, maxValue};
auto [seconds, msec] = getDate(refDate);
for (auto alias : expandAliases(aliases)) {
auto value = dist(mt);
dpcoms.emplace_back(o2::dcs::createDataPointCompositeObject(alias, value, seconds, msec));
}
return dpcoms;
}
// only specialize the functions for the types we support :
//
// - double
// - float
// - uint32_t
// - int32_t
// - char
// - bool
//
// - std::string
template std::vector<o2::dcs::DataPointCompositeObject> generateRandomDataPoints<double>(const std::vector<std::string>& aliases, double minValue, double maxValue, std::string);
template std::vector<o2::dcs::DataPointCompositeObject> generateRandomDataPoints<float>(const std::vector<std::string>& aliases, float minValue, float maxValue, std::string);
template std::vector<o2::dcs::DataPointCompositeObject> generateRandomDataPoints<uint32_t>(const std::vector<std::string>& aliases, uint32_t minValue, uint32_t maxValue, std::string);
template std::vector<o2::dcs::DataPointCompositeObject> generateRandomDataPoints<int32_t>(const std::vector<std::string>& aliases, int32_t minValue, int32_t maxValue, std::string);
template std::vector<o2::dcs::DataPointCompositeObject> generateRandomDataPoints<char>(const std::vector<std::string>& aliases, char minValue, char maxValue, std::string);
/** Need a specific specialization for bool as got into trouble compiling uniform_int_distribution<bool>
* on some platform (e.g. CC7).
*/
template <>
std::vector<o2::dcs::DataPointCompositeObject> generateRandomDataPoints<bool>(const std::vector<std::string>& aliases, bool minValue, bool maxValue, std::string refDate)
{
std::vector<o2::dcs::DataPointCompositeObject> dpcoms;
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution dist{0, 1};
auto [seconds, msec] = getDate(refDate);
for (auto alias : expandAliases(aliases)) {
bool value = dist(mt);
dpcoms.emplace_back(o2::dcs::createDataPointCompositeObject(alias, value, seconds, msec));
}
return dpcoms;
}
/**
* Generate data points of type string, where each string is random, with
* a length between the length of the two input strings (minLength,maxLength)
*/
template <>
std::vector<o2::dcs::DataPointCompositeObject> generateRandomDataPoints<std::string>(const std::vector<std::string>& aliases, std::string minLength, std::string maxLength, std::string refDate)
{
std::vector<o2::dcs::DataPointCompositeObject> dpcoms;
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<std::string::size_type> dist{minLength.size(), maxLength.size()};
auto [seconds, msec] = getDate(refDate);
for (auto alias : expandAliases(aliases)) {
auto value = o2::dcs::random_string2(dist(mt));
dpcoms.emplace_back(o2::dcs::createDataPointCompositeObject(alias, value, seconds, msec));
}
return dpcoms;
}
} // namespace o2::dcs