Skip to content

Commit 9cb6a80

Browse files
committed
TPC: add test workflow to create dummy CMV data
1 parent d961ddf commit 9cb6a80

2 files changed

Lines changed: 284 additions & 0 deletions

File tree

Detectors/TPC/workflow/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ o2_add_executable(idc-test-ft
198198
SOURCES test/test_ft_EPN_Aggregator.cxx
199199
PUBLIC_LINK_LIBRARIES O2::TPCWorkflow)
200200

201+
o2_add_executable(cmv-test-generator
202+
COMPONENT_NAME tpc
203+
SOURCES test/test_cmv_generator.cxx
204+
PUBLIC_LINK_LIBRARIES O2::TPCWorkflow)
205+
201206
o2_add_executable(miptrack-filter
202207
COMPONENT_NAME tpc
203208
SOURCES src/tpc-miptrack-filter.cxx
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
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+
12+
/// \file test_cmv_generator.cxx
13+
/// \brief DPL source workflow that generates dummy CMV data for testing the CMV FLP pipeline.
14+
///
15+
/// Replaces o2-tpc-cmv-to-vector in tests; directly emits CMVVECTOR and CMVORBITS
16+
/// messages per CRU per TF so the workflow can be piped straight into o2-tpc-cmv-flp:
17+
///
18+
/// o2-tpc-cmv-test-generator --crus 0-359 --timeframes 100 \
19+
/// | o2-tpc-cmv-flp --crus 0-359 --n-TFs-buffer 10 \
20+
/// | o2-dpl-output-proxy --dataspec "downstream:TPC/CMVGROUP;downstream:TPC/CMVORBITINFO" ...
21+
///
22+
/// \author Ernst Hellbar <ernst.hellbar@cern.ch>
23+
24+
#include "Framework/DataProcessorSpec.h"
25+
#include "Framework/Task.h"
26+
#include "Framework/ControlService.h"
27+
#include "Framework/ConfigParamRegistry.h"
28+
#include "Framework/ConfigParamSpec.h"
29+
#include "Framework/Logger.h"
30+
#include "Headers/DataHeader.h"
31+
#include "Algorithm/RangeTokenizer.h"
32+
#include "TPCBase/CRU.h"
33+
#include "DataFormatsTPC/CMV.h"
34+
#include "TPCWorkflow/ProcessingHelpers.h"
35+
#include "CommonUtils/TreeStreamRedirector.h"
36+
#include "TRandom.h"
37+
#include <fmt/format.h>
38+
#include <fmt/ranges.h>
39+
40+
#include <vector>
41+
#include <chrono>
42+
#include <thread>
43+
#include <cmath>
44+
#include <memory>
45+
#include <unordered_set>
46+
47+
using namespace o2::framework;
48+
using o2::header::gDataOriginTPC;
49+
50+
// ─────────────────────────────────────────────────────────────────────────────
51+
// workflow options
52+
// ─────────────────────────────────────────────────────────────────────────────
53+
void customize(std::vector<ConfigParamSpec>& workflowOptions)
54+
{
55+
const std::string cruDefault = "0-" + std::to_string(o2::tpc::CRU::MaxCRU - 1);
56+
std::vector<ConfigParamSpec> options{
57+
{"crus", VariantType::String, cruDefault.c_str(), {"List of CRUs, comma-separated ranges, e.g. 0-3,7,9-15"}},
58+
{"timeframes", VariantType::Int, 100, {"Number of TFs to generate"}},
59+
{"delay", VariantType::Bool, false, {"Add delay after sending all CRUs"}},
60+
{"delayTime", VariantType::Int, 1, {"Duration of the global per-TF delay in ms (requires --delay true)"}},
61+
{"delayCRUs", VariantType::String, "", {"CRUs for which to add an extra per-CRU delay before sending, comma-separated ranges"}},
62+
{"delayTimeCRUs", VariantType::Int, 1, {"Duration of the per-CRU delay in ms (requires --delayCRUs)"}},
63+
{"dropTFsRandom", VariantType::Int, 0, {"Drop a whole TF randomly: on average one every N TFs (0 = disabled)"}},
64+
{"dropTFsRange", VariantType::String, "", {"Drop all TFs in this range, e.g. 10-12"}},
65+
{"seed", VariantType::Int, 42, {"RNG seed for CMV value generation"}},
66+
{"amplitude", VariantType::Float, 5.0f, {"Amplitude of the sinusoidal CMV signal (ADC units)"}},
67+
{"noise", VariantType::Float, 1.0f, {"Gaussian noise std-dev added per time bin (ADC units)"}},
68+
};
69+
std::swap(workflowOptions, options);
70+
}
71+
72+
#include "Framework/runDataProcessing.h"
73+
74+
// ─────────────────────────────────────────────────────────────────────────────
75+
// generator device
76+
// ─────────────────────────────────────────────────────────────────────────────
77+
class CMVGeneratorDevice : public o2::framework::Task
78+
{
79+
public:
80+
static constexpr uint32_t sOrbitsPerPacket = 8; ///< each CMV packet covers 8 heartbeat orbits
81+
82+
CMVGeneratorDevice(const std::vector<uint32_t>& crus,
83+
const std::unordered_set<uint32_t>& delayCRUs,
84+
unsigned int maxTFs,
85+
bool delay,
86+
int delayTime,
87+
int delayTimeCRUs,
88+
int dropTFsRandom,
89+
const std::vector<int>& rangeTFsDrop,
90+
float amplitude,
91+
float noise)
92+
: mCRUs(crus), mDelayCRUs(delayCRUs), mMaxTFs(maxTFs), mDelay(delay), mDelayTime(delayTime), mDelayTimeCRUs(delayTimeCRUs), mDropTFsRandom(dropTFsRandom), mRangeTFsDrop(rangeTFsDrop), mAmplitude(amplitude), mNoise(noise) {}
93+
94+
void init(o2::framework::InitContext& ic) final
95+
{
96+
LOGP(info, "crus: {}", fmt::join(mCRUs, ", "));
97+
const std::vector<uint32_t> delayCRUsSorted(mDelayCRUs.begin(), mDelayCRUs.end());
98+
LOGP(info, "delayCRUs: {}", fmt::join(delayCRUsSorted, ", "));
99+
100+
mWriteDebug = ic.options().get<bool>("write-debug");
101+
if (mWriteDebug) {
102+
mDebugStreamFileName = ic.options().get<std::string>("debug-file-name");
103+
LOGP(info, "Creating debug stream {}", mDebugStreamFileName);
104+
mDebugStream = std::make_unique<o2::utils::TreeStreamRedirector>(mDebugStreamFileName.data(), "recreate");
105+
}
106+
}
107+
108+
void run(o2::framework::ProcessingContext& ctx) final
109+
{
110+
using timer = std::chrono::high_resolution_clock;
111+
const auto tf = o2::tpc::processing_helpers::getCurrentTF(ctx);
112+
113+
// ── TF dropping ──────────────────────────────────────────────────────────
114+
if (!mRangeTFsDrop.empty() && tf >= (uint32_t)mRangeTFsDrop.front() && tf <= (uint32_t)mRangeTFsDrop.back()) {
115+
LOGP(info, "Dropping TF {} (range drop)", tf);
116+
return;
117+
}
118+
if (mDropTFsRandom > 0 && !gRandom->Integer(mDropTFsRandom)) {
119+
LOGP(info, "Dropping TF {} (random drop)", tf);
120+
return;
121+
}
122+
123+
auto start = timer::now();
124+
125+
// Slow sinusoidal baseline that drifts across TFs
126+
const float signal = mAmplitude * std::sin(tf * 0.05f);
127+
128+
for (const auto cru : mCRUs) {
129+
const o2::header::DataHeader::SubSpecificationType subSpec{cru << 7};
130+
131+
// ── per-CRU delay ────────────────────────────────────────────────────
132+
if (mDelayCRUs.count(cru)) {
133+
std::this_thread::sleep_for(std::chrono::milliseconds(mDelayTimeCRUs));
134+
}
135+
136+
// ── CMV values ───────────────────────────────────────────────────────
137+
// NTimeBinsPerTF = NPacketsPerTFPerCRU (4) * NTimeBinsPerPacket (3564) = 14256
138+
std::vector<uint16_t> cmvVec;
139+
cmvVec.reserve(o2::tpc::cmv::NTimeBinsPerTF);
140+
for (uint32_t tb = 0; tb < o2::tpc::cmv::NTimeBinsPerTF; ++tb) {
141+
const float val = signal + mNoise * gRandom->Gaus(0, 1);
142+
o2::tpc::cmv::Data d;
143+
d.setCMVFloat(val);
144+
cmvVec.push_back(d.getCMV());
145+
}
146+
147+
// ── Orbit / BC info ──────────────────────────────────────────────────
148+
// One packed (orbit<<32|bc) entry per CMV packet (4 per TF).
149+
// Each packet covers 8 heartbeat orbits (NTimeBinsPerPacket = 3564 = 8 LHC orbits),
150+
// so the orbit advances by 8 per packet and by NPacketsPerTFPerCRU*8 = 32 per TF.
151+
std::vector<uint64_t> orbitBCVec;
152+
orbitBCVec.reserve(o2::tpc::cmv::NPacketsPerTFPerCRU);
153+
for (uint32_t pkt = 0; pkt < o2::tpc::cmv::NPacketsPerTFPerCRU; ++pkt) {
154+
const uint32_t orbit = static_cast<uint32_t>(tf * o2::tpc::cmv::NPacketsPerTFPerCRU * sOrbitsPerPacket + pkt * sOrbitsPerPacket);
155+
orbitBCVec.push_back(uint64_t(orbit) << 32); // bc = 0
156+
}
157+
158+
ctx.outputs().snapshot(Output{gDataOriginTPC, "CMVVECTOR", subSpec}, cmvVec);
159+
ctx.outputs().snapshot(Output{gDataOriginTPC, "CMVORBITS", subSpec}, orbitBCVec);
160+
161+
if (mWriteDebug) {
162+
auto& stream = (*mDebugStream) << "cmvs";
163+
stream << "cru=" << cru
164+
<< "tfCounter=" << tf
165+
<< "nCMVs=" << cmvVec.size()
166+
<< "cmvs=" << cmvVec
167+
<< "\n";
168+
}
169+
}
170+
171+
// ── global delay ─────────────────────────────────────────────────────────
172+
if (mDelay) {
173+
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(timer::now() - start).count();
174+
if (elapsed < mDelayTime) {
175+
std::this_thread::sleep_for(std::chrono::milliseconds(mDelayTime - elapsed));
176+
}
177+
}
178+
179+
if (!(tf % 100)) {
180+
LOGP(info, "Generated CMV data for TF {}", tf);
181+
}
182+
183+
if (tf >= mMaxTFs - 1) {
184+
ctx.services().get<ControlService>().endOfStream();
185+
ctx.services().get<ControlService>().readyToQuit(QuitRequest::Me);
186+
}
187+
}
188+
189+
void endOfStream(o2::framework::EndOfStreamContext&) final { closeFiles(); }
190+
void stop() final { closeFiles(); }
191+
192+
private:
193+
void closeFiles()
194+
{
195+
if (mDebugStream) {
196+
auto& stream = (*mDebugStream) << "cmvs";
197+
auto& tree = stream.getTree();
198+
tree.SetAlias("sector", "int(cru/10)");
199+
mDebugStream->Close();
200+
mDebugStream.reset(nullptr);
201+
}
202+
}
203+
204+
const std::vector<uint32_t> mCRUs{};
205+
const std::unordered_set<uint32_t> mDelayCRUs{};
206+
const unsigned int mMaxTFs{};
207+
const bool mDelay{false};
208+
const int mDelayTime{1};
209+
const int mDelayTimeCRUs{1};
210+
const int mDropTFsRandom{0};
211+
const std::vector<int> mRangeTFsDrop{};
212+
const float mAmplitude{5.f};
213+
const float mNoise{1.f};
214+
bool mWriteDebug{false};
215+
std::string mDebugStreamFileName{};
216+
std::unique_ptr<o2::utils::TreeStreamRedirector> mDebugStream{};
217+
};
218+
219+
// ─────────────────────────────────────────────────────────────────────────────
220+
DataProcessorSpec generateCMVsCRU(const std::vector<uint32_t>& crus,
221+
const std::unordered_set<uint32_t>& delayCRUs,
222+
unsigned int maxTFs,
223+
bool delay,
224+
int delayTime,
225+
int delayTimeCRUs,
226+
int dropTFsRandom,
227+
const std::vector<int>& rangeTFsDrop,
228+
float amplitude,
229+
float noise)
230+
{
231+
std::vector<OutputSpec> outputSpecs;
232+
outputSpecs.reserve(crus.size() * 2);
233+
for (const auto cru : crus) {
234+
const o2::header::DataHeader::SubSpecificationType subSpec{cru << 7};
235+
outputSpecs.emplace_back(ConcreteDataMatcher{gDataOriginTPC, "CMVVECTOR", subSpec}, Lifetime::Timeframe);
236+
outputSpecs.emplace_back(ConcreteDataMatcher{gDataOriginTPC, "CMVORBITS", subSpec}, Lifetime::Timeframe);
237+
}
238+
239+
return DataProcessorSpec{
240+
"tpc-cmv-generator",
241+
Inputs{},
242+
outputSpecs,
243+
AlgorithmSpec{adaptFromTask<CMVGeneratorDevice>(crus, delayCRUs, maxTFs, delay, delayTime, delayTimeCRUs, dropTFsRandom, rangeTFsDrop, amplitude, noise)},
244+
Options{
245+
{"write-debug", VariantType::Bool, false, {"Write a debug output tree"}},
246+
{"debug-file-name", VariantType::String, "./cmv_generator_debug.root", {"Name of the debug output file"}},
247+
}};
248+
}
249+
250+
// ─────────────────────────────────────────────────────────────────────────────
251+
WorkflowSpec defineDataProcessing(ConfigContext const& config)
252+
{
253+
const auto tpcCRUs = o2::RangeTokenizer::tokenize<int>(config.options().get<std::string>("crus"));
254+
const std::vector<uint32_t> crus(tpcCRUs.begin(), tpcCRUs.end());
255+
256+
const auto delayCRUsStr = config.options().get<std::string>("delayCRUs");
257+
std::unordered_set<uint32_t> delayCRUs;
258+
if (!delayCRUsStr.empty()) {
259+
for (const auto cru : o2::RangeTokenizer::tokenize<int>(delayCRUsStr)) {
260+
delayCRUs.insert(static_cast<uint32_t>(cru));
261+
}
262+
}
263+
264+
const auto rangeTFsDrop = o2::RangeTokenizer::tokenize<int>(config.options().get<std::string>("dropTFsRange"));
265+
const auto timeframes = static_cast<unsigned int>(config.options().get<int>("timeframes"));
266+
const auto delay = config.options().get<bool>("delay");
267+
const auto delayTime = config.options().get<int>("delayTime");
268+
const auto delayTimeCRUs = config.options().get<int>("delayTimeCRUs");
269+
const auto dropTFsRandom = config.options().get<int>("dropTFsRandom");
270+
const auto seed = config.options().get<int>("seed");
271+
const auto amplitude = config.options().get<float>("amplitude");
272+
const auto noise = config.options().get<float>("noise");
273+
274+
gRandom->SetSeed(seed);
275+
276+
WorkflowSpec workflow;
277+
workflow.emplace_back(generateCMVsCRU(crus, delayCRUs, timeframes, delay, delayTime, delayTimeCRUs, dropTFsRandom, rangeTFsDrop, amplitude, noise));
278+
return workflow;
279+
}

0 commit comments

Comments
 (0)