CbmRoot
Loading...
Searching...
No Matches
CbmDeviceUnpack.h
Go to the documentation of this file.
1/* Copyright (C) 2021 Facility for Antiproton and Ion Research in Europe, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Pierre-Alain Loizeau [committer] */
4
12#ifndef CBMDEVICEUNPACK_H_
13#define CBMDEVICEUNPACK_H_
14
15#include "CbmMqTMessage.h"
16#include "CbmTsEventHeader.h"
17
18#include "Timeslice.hpp"
19
20#include "FairMQDevice.h"
21#include "FairParGenericSet.h"
22
23#include "Rtypes.h"
24#include "TObjArray.h"
25
26#include <chrono>
27#include <map>
28#include <vector>
29
30class TList;
39
41
42class CbmTrdSpadic;
43
44class CbmDeviceUnpack : public FairMQDevice {
45public:
47 virtual ~CbmDeviceUnpack();
48
49protected:
50 virtual void InitTask();
51 bool ConditionalRun();
52 bool HandleCommand(FairMQMessagePtr&, int);
53
55 void SetUnpackConfig(std::shared_ptr<CbmBmonUnpackConfig> config) { fBmonConfig = config; }
56
58 void SetUnpackConfig(std::shared_ptr<CbmStsUnpackConfig> config) { fStsConfig = config; }
59
61 void SetUnpackConfig(std::shared_ptr<CbmMuchUnpackConfig> config) { fMuchConfig = config; }
62
64 void SetUnpackConfig(std::shared_ptr<CbmTrdUnpackConfig> config) { fTrd1DConfig = config; }
65
67 void SetUnpackConfig(std::shared_ptr<CbmTrdUnpackFaspConfig> config) { fTrd2DConfig = config; }
68
70 void SetUnpackConfig(std::shared_ptr<CbmTofUnpackConfig> config) { fTofConfig = config; }
71
73 void SetUnpackConfig(std::shared_ptr<CbmRichUnpackConfig> config) { fRichConfig = config; }
74
76 void SetUnpackConfig(std::shared_ptr<CbmPsdUnpackConfig> config) { fPsdConfig = config; }
77
78private:
80 static constexpr std::uint16_t fkFlesBmon = static_cast<std::uint16_t>(fles::Subsystem::BMON);
81 static constexpr std::uint16_t fkFlesMvd = static_cast<std::uint16_t>(fles::Subsystem::MVD);
82 static constexpr std::uint16_t fkFlesSts = static_cast<std::uint16_t>(fles::Subsystem::STS);
83 static constexpr std::uint16_t fkFlesMuch = static_cast<std::uint16_t>(fles::Subsystem::MUCH);
84 static constexpr std::uint16_t fkFlesTrd = static_cast<std::uint16_t>(fles::Subsystem::TRD);
85 static constexpr std::uint16_t fkFlesTrd2D = static_cast<std::uint16_t>(fles::Subsystem::TRD2D);
86 static constexpr std::uint16_t fkFlesTof = static_cast<std::uint16_t>(fles::Subsystem::TOF);
87 static constexpr std::uint16_t fkFlesRich = static_cast<std::uint16_t>(fles::Subsystem::RICH);
88 static constexpr std::uint16_t fkFlesPsd = static_cast<std::uint16_t>(fles::Subsystem::PSD);
89
90
92 Bool_t fbIgnoreOverlapMs = false;
93 Bool_t fbComponentsAddedToList = kFALSE;
94
96 bool fDoDebugPrints = false;
98 bool fDoPerfProf = false;
101
103 std::string fsSetupName = "mcbm_beam_2021_07_surveyed";
104 uint32_t fuRunId = 1588;
106 bool fbUnpBmon = false;
107 bool fbUnpSts = true;
108 bool fbUnpMuch = false;
109 bool fbUnpTrd1D = true;
110 bool fbUnpTrd2D = true;
111 bool fbUnpTof = true;
112 bool fbUnpRich = true;
113 bool fbUnpPsd = true;
115 std::string fsChannelNameDataInput = "ts-request";
116 std::string fsChannelNameDataOutput = "unpts_0";
117 std::string fsChannelNameCommands = "commands";
118 std::string fsChannelNameHistosInput = "histogram-in";
120 uint32_t fuPublishFreqTs = 100;
121 double_t fdMinPublishTime = 0.5;
122 double_t fdMaxPublishTime = 5.0;
123
125 // TList* fParCList = nullptr;
126 Bool_t InitParameters(std::vector<std::pair<std::string, std::shared_ptr<FairParGenericSet>>>* reqparvec);
127
129 uint64_t fulNumMessages = 0;
130 uint64_t fulTsCounter = 0;
131 std::chrono::system_clock::time_point fLastPublishTime = std::chrono::system_clock::now();
133 std::map<std::uint16_t, std::pair<std::string, size_t>> fNameMap = {};
135 std::map<std::uint16_t, std::pair<double, double>> fTimeMap = {};
137 std::map<std::uint16_t, std::pair<double, double>> fDataSizeMap = {};
138
140 std::shared_ptr<CbmBmonUnpackConfig> fBmonConfig = nullptr;
141 std::shared_ptr<CbmStsUnpackConfig> fStsConfig = nullptr;
142 std::shared_ptr<CbmMuchUnpackConfig> fMuchConfig = nullptr;
143 std::shared_ptr<CbmTrdUnpackFaspConfig> fTrd2DConfig = nullptr;
144 std::shared_ptr<CbmTrdUnpackConfig> fTrd1DConfig = nullptr;
145 std::shared_ptr<CbmTofUnpackConfig> fTofConfig = nullptr;
146 std::shared_ptr<CbmRichUnpackConfig> fRichConfig = nullptr;
147 std::shared_ptr<CbmPsdUnpackConfig> fPsdConfig = nullptr;
148
151
153 std::vector<std::string> fvsSetTimeOffs = {};
154
156 size_t fuNbCoreMsPerTs = 0;
157 size_t fuNbOverMsPerTs = 0;
158 Double_t fdMsSizeInNs = 0;
159 Double_t fdTsCoreSizeInNs = -1.0;
160 Double_t fdTsOverSizeInNs = -1.0;
161 Double_t fdTsFullSizeInNs = -1.0;
163
165 TObjArray fArrayHisto = {};
167 std::vector<std::pair<std::string, std::string>> fvpsHistosFolder = {};
171 std::vector<std::pair<std::string, std::string>> fvpsCanvasConfig = {};
173 bool fbConfigSent = false;
174
175 Bool_t InitContainers();
176 bool InitHistograms();
177 Bool_t DoUnpack(const fles::Timeslice& ts, size_t component);
178 void Finish();
179 bool SendUnpData();
181 bool SendHistograms();
182
183 std::shared_ptr<CbmTrdSpadic> GetTrdSpadic(bool useAvgBaseline);
184
186 template<typename TVecobj>
187 typename std::enable_if<std::is_same<TVecobj, std::nullptr_t>::value == true, void>::type
188 timesort(std::vector<TVecobj>* /*vec = nullptr*/)
189 {
190 LOG(debug) << "CbmDeviceUnpack::timesort() got an object that has no member function GetTime(). Hence, we can and "
191 "will not timesort it!";
192 }
193
194 template<typename TVecobj>
195 typename std::enable_if<!std::is_member_function_pointer<decltype(&TVecobj::GetTime)>::value, void>::type
196 timesort(std::vector<TVecobj>* /*vec = nullptr*/)
197 {
198 LOG(debug) << "CbmDeviceUnpack::timesort() " << TVecobj::Class_Name()
199 << "is an object that has no member function GetTime(). Hence, we can and "
200 "will not timesort it!";
201 }
202
203 template<typename TVecobj>
204 typename std::enable_if<std::is_member_function_pointer<decltype(&TVecobj::GetTime)>::value, void>::type
205 timesort(std::vector<TVecobj>* vec = nullptr)
206 {
207 if (vec == nullptr) return;
208 std::sort(vec->begin(), vec->end(),
209 [](const TVecobj& a, const TVecobj& b) -> bool { return a.GetTime() < b.GetTime(); });
210 }
211
225 template<class TConfig, class TOptOutA = std::nullptr_t, class TOptOutB = std::nullptr_t>
226 size_t unpack(const std::uint16_t subsysid, const fles::Timeslice* ts, std::uint16_t icomp, TConfig config,
227 std::vector<TOptOutA>* optouttargetvecA = nullptr, std::vector<TOptOutB>* optouttargetvecB = nullptr)
228 {
229
230 auto wallstarttime = std::chrono::high_resolution_clock::now();
231 std::clock_t cpustarttime = std::clock();
232
233 auto algo = config->GetUnpacker();
234 std::vector<TOptOutA> optoutAvec = {};
235 std::vector<TOptOutB> optoutBvec = {};
236 if (optouttargetvecA) { algo->SetOptOutAVec(&optoutAvec); }
237 if (optouttargetvecB) { algo->SetOptOutBVec(&optoutBvec); }
238
239 // Set the start time of the current TS for this algorithm
240 algo->SetTsStartTime(ts->start_time());
241
242 // Run the actual unpacking
243 auto digivec = algo->Unpack(ts, icomp);
244
245 // Check if we want to write the output to somewhere (in pure online monitoring mode for example this can/would/should be skipped)
246 if (config->GetOutputVec()) {
247 // Lets do some time-sorting if we are not doing it later
248 if (!fbOutputFullTimeSorting) timesort(&digivec);
249
250 // Transfer the data from the timeslice vector to the target branch vector
251 // Digis/default output retrieved as offered by the algorithm
252 for (auto digi : digivec)
253 config->GetOutputVec()->emplace_back(digi);
254 }
255 if (optouttargetvecA) {
256 // Lets do some timesorting
257 if (!fbOutputFullTimeSorting) timesort(&optoutAvec);
258 // Transfer the data from the timeslice vector to the target branch vector
259 for (auto optoutA : optoutAvec)
260 optouttargetvecA->emplace_back(optoutA);
261 }
262 if (optouttargetvecB) {
263 // Second opt output is not time sorted to allow non GetTime data container.
264 // Lets do some timesorting
265 timesort(&optoutAvec);
266 // Transfer the data from the timeslice vector to the target branch vector
267 for (auto optoutB : optoutBvec)
268 optouttargetvecB->emplace_back(optoutB);
269 }
270
271 std::clock_t cpuendtime = std::clock();
272 auto wallendtime = std::chrono::high_resolution_clock::now();
273
274 // Cpu time in [µs]
275 auto cputime = 1e6 * (cpuendtime - cpustarttime) / CLOCKS_PER_SEC;
276 algo->AddCpuTime(cputime);
277 // Real time in [µs]
278 auto walltime = std::chrono::duration<double, std::micro>(wallendtime - wallstarttime).count();
279 algo->AddWallTime(walltime);
280
281
282 // Check some numbers from this timeslice
283 size_t nDigis = digivec.size();
284 LOG(debug) << "Component " << icomp << " connected to config " << config->GetName() << " n-Digis " << nDigis
285 << " processed in walltime(cputime) = " << walltime << "(" << cputime << cputime << ") µs"
286 << "this timeslice.";
287
288 if (fDoPerfProf) {
289 auto timeit = fTimeMap.find(subsysid);
290 timeit->second.first += cputime;
291 timeit->second.second += walltime;
292
293 auto datait = fDataSizeMap.find(subsysid);
294 datait->second.first += ts->size_component(icomp) / 1.0e6;
295 datait->second.second += nDigis * algo->GetOutputObjSize() / 1.0e6;
296
297 fNameMap.find(subsysid)->second.second += nDigis;
298 }
299
300 return nDigis;
301 }
302};
303
304#endif /* CBMDEVICEMCBMUNPACK_H_ */
bool fDoDebugPrints
Flag if extended debug output is to be printed or not.
std::string fsChannelNameDataInput
message queues
std::enable_if<!std::is_member_function_pointer< decltype(&TVecobj::GetTime)>::value, void >::type timesort(std::vector< TVecobj > *)
bool HandleCommand(FairMQMessagePtr &, int)
static constexpr std::uint16_t fkFlesMuch
static constexpr std::uint16_t fkFlesTrd
std::string fsChannelNameCommands
CbmTsEventHeader * fCbmTsEventHeader
Pointer to the Timeslice header conatining start time and index.
std::shared_ptr< CbmTofUnpackConfig > fTofConfig
std::shared_ptr< CbmBmonUnpackConfig > fBmonConfig
Configuration of the unpackers. Provides the configured algorithm.
std::enable_if< std::is_member_function_pointer< decltype(&TVecobj::GetTime)>::value, void >::type timesort(std::vector< TVecobj > *vec=nullptr)
static constexpr std::uint16_t fkFlesMvd
uint32_t fuPublishFreqTs
Histograms management.
static constexpr std::uint16_t fkFlesBmon
Constants.
std::chrono::system_clock::time_point fLastPublishTime
static constexpr std::uint16_t fkFlesTrd2D
void SetUnpackConfig(std::shared_ptr< CbmMuchUnpackConfig > config)
Set the Much Unpack Config.
std::vector< std::pair< std::string, std::string > > fvpsCanvasConfig
uint64_t fulNumMessages
Statistics & first TS rejection.
TObjArray fArrayHisto
Array of histograms to send to the histogram server.
void SetUnpackConfig(std::shared_ptr< CbmTofUnpackConfig > config)
Set the Tof Unpack Config.
std::shared_ptr< CbmPsdUnpackConfig > fPsdConfig
Bool_t fbIgnoreOverlapMs
Control flags.
Bool_t fbComponentsAddedToList
Ignore Overlap Ms: all fuOverlapMsNb MS at the end of timeslice.
void SetUnpackConfig(std::shared_ptr< CbmTrdUnpackConfig > config)
Set the Trd Unpack Config.
void SetUnpackConfig(std::shared_ptr< CbmBmonUnpackConfig > config)
Set the Bmon Unpack Config.
std::vector< std::pair< std::string, std::string > > fvpsHistosFolder
Vector of string pairs with ( HistoName, FolderPath ) to send to the histogram server.
size_t fuNbCoreMsPerTs
TS MetaData storage: stable so should be moved somehow to parameters handling (not transmitted with e...
static constexpr std::uint16_t fkFlesSts
bool fDoPerfProf
Flag if performance profiling should be activated or not.
std::map< std::uint16_t, std::pair< double, double > > fDataSizeMap
Map to store the in and out data amount, key = fkFlesId.
std::shared_ptr< CbmMuchUnpackConfig > fMuchConfig
static constexpr std::uint16_t fkFlesPsd
TimesliceMetaData * fTsMetaData
Total size of all MS in a TS, [nanoseconds].
Bool_t DoUnpack(const fles::Timeslice &ts, size_t component)
std::enable_if< std::is_same< TVecobj, std::nullptr_t >::value==true, void >::type timesort(std::vector< TVecobj > *)
Sort a vector timewise vector type has to provide GetTime()
void SetUnpackConfig(std::shared_ptr< CbmStsUnpackConfig > config)
Set the Sts Unpack Config.
std::shared_ptr< CbmTrdSpadic > GetTrdSpadic(bool useAvgBaseline)
Get the Trd Spadic.
std::vector< std::string > fvsSetTimeOffs
Time offsets.
std::shared_ptr< CbmTrdUnpackConfig > fTrd1DConfig
virtual void InitTask()
void SetUnpackConfig(std::shared_ptr< CbmPsdUnpackConfig > config)
Set the Psd Unpack Config.
std::map< std::uint16_t, std::pair< std::string, size_t > > fNameMap
Map to store a name for the unpackers and the processed amount of digis, key = fkFlesId.
size_t unpack(const std::uint16_t subsysid, const fles::Timeslice *ts, std::uint16_t icomp, TConfig config, std::vector< TOptOutA > *optouttargetvecA=nullptr, std::vector< TOptOutB > *optouttargetvecB=nullptr)
Template for the unpacking call of a given algorithm.
static constexpr std::uint16_t fkFlesTof
bool fbConfigSent
Flag indicating whether the histograms and canvases configurations were already published.
Double_t fdTsOverSizeInNs
Total size of the core MS in a TS, [nanoseconds].
double_t fdMinPublishTime
std::shared_ptr< CbmStsUnpackConfig > fStsConfig
double_t fdMaxPublishTime
static constexpr std::uint16_t fkFlesRich
std::shared_ptr< CbmRichUnpackConfig > fRichConfig
std::string fsChannelNameDataOutput
std::string fsChannelNameHistosInput
Double_t fdTsCoreSizeInNs
Size of a single MS, [nanoseconds].
void SetUnpackConfig(std::shared_ptr< CbmTrdUnpackFaspConfig > config)
Set the Trd2D Unpack Config.
Bool_t InitParameters(std::vector< std::pair< std::string, std::shared_ptr< FairParGenericSet > > > *reqparvec)
Parameters management.
bool fbOutputFullTimeSorting
Flag to Enable/disable a full time sorting. If off, time sorting happens per link/FLIM source.
void SetUnpackConfig(std::shared_ptr< CbmRichUnpackConfig > config)
Set the Rich Unpack Config.
bool fbUnpBmon
---> for selective unpacking
std::map< std::uint16_t, std::pair< double, double > > fTimeMap
Map to store the cpu and wall time, key = fkFlesId.
std::shared_ptr< CbmTrdUnpackFaspConfig > fTrd2DConfig
std::string fsSetupName
User settings parameters.
Double_t fdTsFullSizeInNs
Total size of the overlap MS in a TS, [nanoseconds].