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 "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}
89catch (const std::exception& err) {
90 L_(fatal) << "cbm::algo::qa::Data for " << fsTaskNames << " fatally aborted. Reason " << err.what();
91 assert(false);
92}
93
94// ---------------------------------------------------------------------------------------------------------------------
95//
96void Data::RegisterNewTask(std::string_view name)
97{
98 auto itH1 = fHistograms.fvH1.begin();
99 auto itH2 = fHistograms.fvH2.begin();
100 auto itP1 = fHistograms.fvP1.begin();
101 auto itP2 = fHistograms.fvP2.begin();
102 fvTaskProperties.emplace_back(TaskProperties{.fsName = {name.begin(), name.end()},
103 .fRangeH1 = std::make_pair(itH1, itH1),
104 .fRangeH2 = std::make_pair(itH2, itH2),
105 .fRangeP1 = std::make_pair(itP1, itP1),
106 .fRangeP2 = std::make_pair(itP2, itP2)});
107}
108
109// ---------------------------------------------------------------------------------------------------------------------
110//
111void Data::Send(std::shared_ptr<HistogramSender> histoSender)
112{
113 if (histoSender.get() && fbNotEmpty) {
114 histoSender->PrepareAndSendMsg(fHistograms, zmq::send_flags::none);
115 L_(info) << fsTaskNames << ": Published " << fNofH1 << " 1D- and " << fNofH2 << " 2D-histograms, " << fNofP1
116 << " 1D- and " << fNofP2 << " 2D-profiles";
117 this->Reset();
118 }
119}
#define L_(level)
Data class with information on a STS local track.
Class to handle QA-objects in the online reconstruction.
Definition QaData.h:29
bool fbNotEmpty
false: if no histograms were provided, do not perform initialization and sending
Definition QaData.h:94
void RegisterNewTask(std::string_view name)
Registers a new QA task.
Definition QaData.cxx:96
std::string fsTaskNames
A string containing names of tasks.
Definition QaData.h:86
void Send(std::shared_ptr< HistogramSender > histoSender)
Sends QA data to the HistogramSender.
Definition QaData.cxx:111
uint32_t fNofH1
Number of 1D-histograms.
Definition QaData.h:90
void Init(std::shared_ptr< HistogramSender > histoSender)
Sends QA initialization information to the HistogramSender.
Definition QaData.cxx:19
uint32_t fNofP1
Number of 1D-profiles.
Definition QaData.h:92
void Reset()
Resets the histograms.
Definition QaData.h:69
uint32_t fNofP2
Number of 2D-profiles.
Definition QaData.h:93
qa::HistogramContainer fHistograms
A container of histograms, which forms a zmq message.
Definition QaData.h:85
std::vector< std::string > fvsCanvCfgs
Vector of canvas configs.
Definition QaData.h:88
uint32_t fNofH2
Number of 2D-histograms.
Definition QaData.h:91
std::vector< qa::TaskProperties > fvTaskProperties
A vector to store properties for multiple QA-tasks.
Definition QaData.h:87
Structure to keep the histograms for sending them on the histogram server.
std::forward_list< qa::Prof1D > fvP1
List of 1D-profiles.
std::forward_list< qa::Prof2D > fvP2
List of 2D-profiles.
std::forward_list< qa::H1D > fvH1
List of 1D-histograms.
std::forward_list< qa::H2D > fvH2
List of 2D-histograms.