Project
Loading...
Searching...
No Matches
DCSRandomDataGeneratorSpec.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
18#include "Framework/Logger.h"
19#include "Framework/Task.h"
20#include <TDatime.h>
21#include <random>
22#include <variant>
23#include <string>
24#include <algorithm>
25
26using namespace o2::framework;
27
28namespace
29{
38std::vector<int> generateIntegers(size_t size, int min, int max)
39{
40 std::uniform_int_distribution<int> distribution(min, max);
41 std::mt19937 generator(std::random_device{}());
42 std::vector<int> data;
43 while (data.size() != size) {
44 data.emplace_back(distribution(generator));
45 std::sort(begin(data), end(data));
46 auto last = std::unique(begin(data), end(data)); // make sure we do not duplicate
47 data.erase(last, end(data));
48 }
49 std::shuffle(begin(data), end(data), generator);
50 for (auto i = 0; i < data.size(); ++i) {
51 LOG(debug) << "Generating randomly DP at index " << data[i];
52 }
53 return data;
54}
55
63std::vector<o2::dcs::DataPointCompositeObject> generate(const std::vector<o2::dcs::test::HintType> hints,
64 float fraction = 1.0,
65 uint64_t tfid = 0)
66{
67 std::vector<o2::dcs::DataPointCompositeObject> dataPoints;
68
69 TDatime d;
70 auto dsec = d.Convert();
71 dsec += tfid;
72 d.Set(dsec);
73
74 std::string refDate = d.AsSQLString();
75
76 auto GenerateVisitor = [refDate](const auto& t) {
77 return o2::dcs::generateRandomDataPoints({t.aliasPattern}, t.minValue, t.maxValue, refDate);
78 };
79
80 for (const auto& hint : hints) {
81 auto dpcoms = std::visit(GenerateVisitor, hint);
82 for (auto dp : dpcoms) {
83 dataPoints.push_back(dp);
84 }
85 }
86 if (fraction < 1.0) {
87 auto indices = generateIntegers(fraction * dataPoints.size(), 0, dataPoints.size() - 1);
88 std::vector<o2::dcs::DataPointCompositeObject> tmp;
89 tmp.swap(dataPoints);
90 dataPoints.clear();
91 for (auto i : indices) {
92 dataPoints.push_back(tmp[i]);
93 }
94 }
95 return dataPoints;
96}
97
105class DCSRandomDataGenerator : public o2::framework::Task
106{
107 public:
108 DCSRandomDataGenerator(std::vector<o2::dcs::test::HintType> hints, o2::header::DataDescription description);
109
110 void init(o2::framework::InitContext& ic) final;
111
113
114 private:
115 uint64_t mMaxTF;
116 uint64_t mTFs = 0;
117 uint64_t mMaxCyclesNoFullMap;
118 float mDeltaFraction;
119 std::vector<o2::dcs::test::HintType> mDataPointHints;
120 o2::header::DataDescription mDataDescription;
121};
122
123DCSRandomDataGenerator::DCSRandomDataGenerator(std::vector<o2::dcs::test::HintType> hints,
124 o2::header::DataDescription description) : mDataPointHints(hints),
125 mDataDescription(description) {}
126
127void DCSRandomDataGenerator::init(o2::framework::InitContext& ic)
128{
129 mMaxTF = ic.options().get<int64_t>("max-timeframes");
130 mDeltaFraction = ic.options().get<float>("delta-fraction");
131 mMaxCyclesNoFullMap = ic.options().get<int64_t>("max-cycles-no-full-map");
132}
133
134void DCSRandomDataGenerator::run(o2::framework::ProcessingContext& pc)
135{
136 auto input = pc.inputs().begin();
137 uint64_t tfid = o2::header::get<o2::framework::DataProcessingHeader*>((*input).header)->startTime;
138 if (tfid >= mMaxTF) {
139 LOG(info) << "Data generator reached TF " << tfid << ", stopping";
140 pc.services().get<o2::framework::ControlService>().endOfStream();
142 }
143
144 bool generateFBI = (mTFs % mMaxCyclesNoFullMap == 0);
145 // fraction is one if we generate FBI (Full Buffer Image)
146 float fraction = (generateFBI ? 1.0 : mDeltaFraction);
147
148 TDatime d;
149 auto dpcoms = generate(mDataPointHints, fraction, tfid);
150
151 LOG(info) << "***************** TF " << tfid << " has generated " << dpcoms.size() << " DPs";
152 auto& timingInfo = pc.services().get<o2::framework::TimingInfo>();
153 auto timeNow = std::chrono::system_clock::now();
154 timingInfo.creation = std::chrono::duration_cast<std::chrono::milliseconds>(timeNow.time_since_epoch()).count(); // in ms
155
156 pc.outputs().snapshot(Output{"DCS", mDataDescription, 0}, dpcoms);
157 mTFs++;
158}
159} // namespace
160
161namespace o2::dcs::test
162{
163o2::framework::DataProcessorSpec getDCSRandomDataGeneratorSpec(std::vector<o2::dcs::test::HintType> hints,
164 const char* detName)
165{
166 std::string desc{detName};
167 desc += "DATAPOINTS";
168
170
171 dd.runtimeInit(desc.c_str(), desc.size());
172
173 return DataProcessorSpec{
174 "dcs-random-data-generator",
175 Inputs{},
176 Outputs{{{"outputDCS"}, "DCS", dd}},
177 AlgorithmSpec{adaptFromTask<DCSRandomDataGenerator>(hints, dd)},
178 Options{
179 {"max-timeframes", VariantType::Int64, 99999999999ll, {"max TimeFrames to generate"}},
180 {"delta-fraction", VariantType::Float, 0.05f, {"fraction of data points to put in the delta"}},
181 {"max-cycles-no-full-map", VariantType::Int64, 6000ll, {"max num of cycles between the sending of 2 full maps"}}}};
182}
183} // namespace o2::dcs::test
int32_t i
std::ostringstream debug
void snapshot(const Output &spec, T const &object)
ConfigParamRegistry const & options()
Definition InitContext.h:33
const_iterator begin() const
DataAllocator & outputs()
The data allocator is used to allocate memory for the output data.
InputRecord & inputs()
The inputs associated with this processing context.
ServiceRegistryRef services()
The services registry associated with this processing context.
virtual void init(InitContext &context)
Definition Task.h:37
virtual void run(ProcessingContext &context)=0
GLsizeiptr size
Definition glcorearb.h:659
GLuint GLuint end
Definition glcorearb.h:469
GLboolean * data
Definition glcorearb.h:298
GLsizei GLenum const void * indices
Definition glcorearb.h:400
o2::framework::DataProcessorSpec getDCSRandomDataGeneratorSpec(std::vector< HintType > hints={}, const char *detName="TOF")
std::vector< DataPointCompositeObject > generateRandomDataPoints(const std::vector< std::string > &aliases, T min, T max, std::string refDate="")
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
@ Me
Only quit this data processor.
std::vector< ConfigParamSpec > Options
std::vector< InputSpec > Inputs
std::vector< OutputSpec > Outputs
Enum< T >::Iterator begin(Enum< T >)
Definition Defs.h:173
TLorentzVector generate(Vec3D &vtx, std::vector< o2::track::TrackParCov > &vctr, float bz, TGenPhaseSpace &genPHS, double parMass, const std::vector< double > &dtMass, std::vector< int > forceQ)
void runtimeInit(const char *string, short length=-1)
Definition DataHeader.h:261
constexpr size_t min
constexpr size_t max
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"