CbmRoot
Loading...
Searching...
No Matches
QaData.cxx
Go to the documentation of this file.
1/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#include "algo/qa/QaData.h"
11
12#include <algorithm>
13
15
16
17// ---------------------------------------------------------------------------------------------------------------------
18//
19void Data::Init(std::shared_ptr<HistogramSender> histSender)
20try {
21 size_t nHistograms = 0;
22 nHistograms += fNofH1;
23 nHistograms += fNofH2;
24 nHistograms += fNofP1;
25 nHistograms += fNofP2;
26 fbNotEmpty = static_cast<bool>(nHistograms);
27 if (!fbNotEmpty) {
28 L_(warn) << "no histograms were provided to a qa::Data instance (running in an idle mode)";
29 }
30
31 if (histSender.get() && fbNotEmpty) {
32
33 // Check, if the tasks list was initialized properly: at least one task must be initialized
34 if (fvTaskProperties.empty()) {
35 std::stringstream msg;
36 msg << "a qa::Data instance was not initialized properly: no task was registered. The list of the histograms:\n";
37 auto ShowName = [&](const auto& h) { msg << " - " << h.GetName() << '\n'; };
38 std::for_each(fHistograms.fvH1.begin(), fHistograms.fvH1.end(), ShowName);
39 std::for_each(fHistograms.fvH2.begin(), fHistograms.fvH2.end(), ShowName);
40 std::for_each(fHistograms.fvP1.begin(), fHistograms.fvP1.end(), ShowName);
41 std::for_each(fHistograms.fvP2.begin(), fHistograms.fvP2.end(), ShowName);
42 msg << "Please, insure that you either instantiate the qa::Data with the Data(std::string_view name) constructor";
43 msg << ", or provide a task name explicitly with the function Data::RegisterNewTask(std::string_view name)";
44 throw std::runtime_error(msg.str());
45 }
46
47 // Forming a histogram config message
48 std::vector<std::pair<std::string, std::string>> vHistCfgs;
49 // NOTE: Important to keep the order of filling the histograms: 1D -> 2D -> ..
50 vHistCfgs.reserve(nHistograms);
51
52 for (const auto& task : fvTaskProperties) {
53 auto RegHist = [&](const auto& h) {
54 if (!h.GetMetadata().CheckFlags()) {
55 std::stringstream msg;
56 msg << "attempt to pass a histogram " << h.GetName()
57 << " with inconsistent flags (see HistogramMetadata::CheckFlags for detailes)";
58 throw std::runtime_error(msg.str());
59 }
60 L_(info) << " - task: " << task.fsName << ", histogram: " << h.GetName();
61 vHistCfgs.emplace_back(h.GetName() + "!" + h.GetMetadataString(), task.fsName);
62 };
63 fsTaskNames += fmt::format("{} ", task.fsName);
64 std::for_each(task.fRangeH1.first, task.fRangeH1.second, RegHist);
65 std::for_each(task.fRangeH2.first, task.fRangeH2.second, RegHist);
66 std::for_each(task.fRangeP1.first, task.fRangeP1.second, RegHist);
67 std::for_each(task.fRangeP2.first, task.fRangeP2.second, RegHist);
68 }
69
70 // Forming a canvas config message
71 std::vector<std::pair<std::string, std::string>> vCanvCfgs;
72 vCanvCfgs.reserve(fvsCanvCfgs.size());
73 for (const auto& canv : fvsCanvCfgs) {
74 vCanvCfgs.emplace_back(std::make_pair(canv.substr(0, canv.find_first_of(';')), canv));
75 }
76
77 histSender->PrepareAndSendMsg(std::pair<uint32_t, uint32_t>(vHistCfgs.size(), vCanvCfgs.size()),
78 zmq::send_flags::sndmore);
79
80 auto RegCfg = [&](const auto& cfg) { histSender->PrepareAndSendMsg(cfg, zmq::send_flags::sndmore); };
81
82 std::for_each(vHistCfgs.begin(), vHistCfgs.end(), RegCfg);
83 std::for_each(vCanvCfgs.begin(), vCanvCfgs.end(), RegCfg);
84
85 // Histograms serialization and emission to close multi-part message
86 histSender->PrepareAndSendMsg(qa::HistogramContainer{}, zmq::send_flags::none);
87 }
88 else if (fbNotEmpty) {
89 // List histograms in a mode without a histogram server
90 for (const auto& task : fvTaskProperties) {
91 auto RegHist = [&](auto& h) {
92 if (!h.GetMetadata().CheckFlags()) {
93 std::stringstream msg;
94 msg << "attempt to pass a histogram " << h.GetName()
95 << " with inconsistent flags (see HistogramMetadata::CheckFlags for detailes)";
96 throw std::runtime_error(msg.str());
97 }
98 // NOTE: appending the name of the task to the histogram name in the batch mode
99 // TODO: use similar approach for the histogram server to distinguish between histograms
100 h.SetName(fmt::format("{}/{}", task.fsName, h.GetName()));
101 L_(info) << " - task: " << task.fsName << ", histogram: " << h.GetName();
102 };
103 fsTaskNames += fmt::format("{} ", task.fsName);
104 std::for_each(task.fRangeH1.first, task.fRangeH1.second, RegHist);
105 std::for_each(task.fRangeH2.first, task.fRangeH2.second, RegHist);
106 std::for_each(task.fRangeP1.first, task.fRangeP1.second, RegHist);
107 std::for_each(task.fRangeP2.first, task.fRangeP2.second, RegHist);
108 }
109 }
110}
111catch (const std::exception& err) {
112 L_(fatal) << "cbm::algo::qa::Data for " << fsTaskNames << " fatally aborted. Reason " << err.what();
113 assert(false);
114}
115
116// ---------------------------------------------------------------------------------------------------------------------
117//
118void Data::RegisterNewTask(std::string_view name)
119{
120 auto itH1 = fHistograms.fvH1.begin();
121 auto itH2 = fHistograms.fvH2.begin();
122 auto itP1 = fHistograms.fvP1.begin();
123 auto itP2 = fHistograms.fvP2.begin();
124 fvTaskProperties.emplace_back(TaskProperties{.fsName = {name.begin(), name.end()},
125 .fRangeH1 = std::make_pair(itH1, itH1),
126 .fRangeH2 = std::make_pair(itH2, itH2),
127 .fRangeP1 = std::make_pair(itP1, itP1),
128 .fRangeP2 = std::make_pair(itP2, itP2)});
129}
130
131// ---------------------------------------------------------------------------------------------------------------------
132//
133void Data::Send(std::shared_ptr<HistogramSender> histoSender)
134{
135 if (histoSender.get() && fbNotEmpty) {
136 histoSender->PrepareAndSendMsg(fHistograms, zmq::send_flags::none);
137 L_(info) << fsTaskNames << ": Published " << fNofH1 << " 1D- and " << fNofH2 << " 2D-histograms, " << fNofP1
138 << " 1D- and " << fNofP2 << " 2D-profiles";
139 this->Reset();
140 }
141}
#define L_(level)
A unified data-structure to handle QA objects for the online reconstruction.
Data class with information on a STS local track.
void Init(std::shared_ptr< HistogramSender > histoSender)
Sends QA initialization information to the HistogramSender.
Definition QaData.cxx:19
Class to handle QA-objects in the online reconstruction.
Definition QaData.h:27
bool fbNotEmpty
false: if no histograms were provided, do not perform initialization and sending
Definition QaData.h:99
void RegisterNewTask(std::string_view name)
Registers a new QA task.
Definition QaData.cxx:118
std::string fsTaskNames
A string containing names of tasks.
Definition QaData.h:91
void Send(std::shared_ptr< HistogramSender > histoSender)
Sends QA data to the HistogramSender.
Definition QaData.cxx:133
uint32_t fNofH1
Number of 1D-histograms.
Definition QaData.h:95
uint32_t fNofP1
Number of 1D-profiles.
Definition QaData.h:97
void Reset()
Resets the histograms.
Definition QaData.h:70
uint32_t fNofP2
Number of 2D-profiles.
Definition QaData.h:98
qa::HistogramContainer fHistograms
A container of histograms, which forms a zmq message.
Definition QaData.h:90
std::vector< std::string > fvsCanvCfgs
Vector of canvas configs.
Definition QaData.h:93
uint32_t fNofH2
Number of 2D-histograms.
Definition QaData.h:96
std::vector< qa::TaskProperties > fvTaskProperties
A vector to store properties for multiple QA-tasks.
Definition QaData.h:92
Structure to keep the histograms for sending them on the histogram server.