CbmRoot
Loading...
Searching...
No Matches
CbmRecoUnpack.h
Go to the documentation of this file.
1/* Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Volker Friese [committer], Pierre-Alain Loizeau, Pascal Raisig */
4
12#ifndef CBMRECOUNPACK_H
13#define CBMRECOUNPACK_H 1
14
15#include "CbmBmonUnpackConfig.h"
16#include "CbmMuchUnpackConfig.h"
17#include "CbmPsdUnpackConfig.h"
18#include "CbmRichUnpackConfig.h"
19#include "CbmStsUnpackConfig.h"
20#include "CbmTofUnpackConfig.h"
21#include "CbmTrdUnpackConfig.h"
23#include "CbmTsEventHeader.h"
24
25#include <MicrosliceDescriptor.hpp>
26#include <Timeslice.hpp>
27
28#include <FairParAsciiFileIo.h>
29#include <FairRootManager.h>
30
31#include <RtypesCore.h>
32#include <THnSparse.h>
33#include <TObject.h>
34
35#include <chrono>
36#include <cstddef>
37#include <cstdint>
38#include <ctime>
39#include <memory>
40#include <type_traits> // this is std::lib used for template is_member_function_pointer
41#include <utility>
42#include <vector>
43
44class CbmTimeSlice;
45class TH1;
46class TStopwatch;
47namespace cbm
48{
49 class Monitor;
50}
51
59class CbmRecoUnpack : public TObject {
60
61 public:
64
65
68
70 CbmRecoUnpack(const CbmRecoUnpack&) = delete;
71
74
76 void Finish();
77
79 Bool_t Init();
80
82 void Reset();
83
89 void SetDebugPrintout(bool value = true) { fDoDebugPrints = value; }
90
96 void SetDoPerfProfiling(bool value = true) { fDoPerfProf = value; }
97
103 void SetDoPerfProfilingPerTs(bool value = true)
104 {
105 if (value) fDoPerfProf = value;
106 fDoPerfProfPerTs = value;
107 }
108
114 void SetPublishProfMoni(bool value = true, std::string sUriMonitor = "", bool useProcTime = false,
115 std::string sJobId = "local")
116 {
117 if (value) {
118 fDoPerfProf = value;
119 fDoPerfProfPerTs = value;
120 }
121 fPublishProfMoni = value;
122 if ("" != sUriMonitor) { //
123 fUriPublishProfMoni = sUriMonitor;
124 }
125 fPubMoniProcTime = useProcTime;
126 fMoniJobId = sJobId;
127 }
128
134 void SetOutputFilename(std::string value)
135 {
136 if (!value.empty()) fOutfilename = value;
137 }
138
144 void SetTimeSorting(bool bIn = true) { bOutputFullTimeSorting = bIn; }
145
151 void SetMonitoringOnly(bool bIn = true) { bMonitoringOnly = bIn; }
152
154 void SetUnpackConfig(std::shared_ptr<CbmMuchUnpackConfig> config) { fMuchConfig = config; }
155
157 void SetUnpackConfig(std::shared_ptr<CbmPsdUnpackConfig> config) { fPsdConfig = config; }
158
160 void SetUnpackConfig(std::shared_ptr<CbmRichUnpackConfig> config) { fRichConfig = config; }
161
163 void SetUnpackConfig(std::shared_ptr<CbmStsUnpackConfig> config) { fStsConfig = config; }
164
165 // /** @brief Set the Tof Unpack Config @param config */
166 void SetUnpackConfig(std::shared_ptr<CbmTofUnpackConfig> config) { fTofConfig = config; }
167
169 void SetUnpackConfig(std::shared_ptr<CbmTrdUnpackConfig> config) { fTrd1DConfig = config; }
170
172 void SetUnpackConfig(std::shared_ptr<CbmTrdUnpackFaspConfig> config) { fTrd2DConfig = config; }
173
175 void SetUnpackConfig(std::shared_ptr<CbmBmonUnpackConfig> config) { fBmonConfig = config; }
176
178 void Unpack(std::unique_ptr<fles::Timeslice> ts);
179
180 private:
181 using Subsystem = fles::Subsystem;
182
184 bool fDoDebugPrints = false;
185
187 bool fDoPerfProf = false;
188
190 bool fDoPerfProfPerTs = false;
191
193 bool fPublishProfMoni = false;
194
196 std::string fUriPublishProfMoni = "influx1:localhost:8086:monitoring_tests";
197
199 bool fPubMoniProcTime = true;
200
202 std::map<Subsystem, std::pair<std::string, size_t>> fNameMap = {};
203
205 std::map<Subsystem, std::pair<double, double>> fTimeMap = {};
206
208 std::map<Subsystem, std::pair<double, double>> fDataSizeMap = {};
209
211 std::map<Subsystem, std::pair<std::string, size_t>> fNameMapPerTs = {};
212
214 std::map<Subsystem, std::pair<double, double>> fTimeMapPerTs = {};
215
217 std::map<Subsystem, std::pair<double, double>> fDataSizeMapPerTs = {};
218
219 TStopwatch* fTimerTs = nullptr;
220 TH1* fhCpuTimePerTs = nullptr;
221 TH1* fhRealTimePerTs = nullptr;
222 TH1* fhCpuTimePerTsHist = nullptr;
223 TH1* fhRealTimePerTsHist = nullptr;
224 std::map<Subsystem, TH1*> fvhInpRatioPerTs = {};
225 std::map<Subsystem, TH1*> fvhOutRatioPerTs = {};
226 std::map<Subsystem, TH1*> fvhUnpRatioPerTs = {};
227 TH1* fhUnpackingRatioPerTs = nullptr;
228
229 std::unique_ptr<cbm::Monitor> fMonitor;
230 std::string fMoniCurrrentHostname = "";
231 std::string fMoniJobId = "";
232 std::chrono::system_clock::time_point fMonitorSecCurrentTs = std::chrono::system_clock::time_point();
233 std::map<Subsystem, std::pair<double, double>> fDataSizeMapCurrSec = {};
234
237
240
247 void initPerformanceMaps(Subsystem subsystem, std::string name);
248
249
250 template<class TConfig>
251 void RegisterOutputs(FairRootManager* ioman, std::shared_ptr<TConfig> config)
252 {
253 /*
254 * FIXME: Compiling but leading to a segfault at runtime
255 auto pOutVect = config->GetOutputVec();
256 if (pOutVect) {
257 ioman->RegisterAny(config->GetOutputBranchName().data(), pOutVect, kTRUE);
258 LOG(info) << "CbmRecoUnpack::RegisterOutputs() " << config->GetOutputBranchName() << " at " << pOutVect;
259 }
260
261 auto pOutVectOptA = config->GetOptOutAVec();
262 if (pOutVectOptA) {
263 ioman->RegisterAny(config->GetOutputBranchNameOptA().data(), pOutVectOptA, kTRUE);
264 LOG(info) << "CbmRecoUnpack::RegisterOutputs() " << config->GetOutputBranchNameOptA() << " at " << pOutVectOptA;
265 }
266
267 auto pOutVectOptB = config->GetOptOutBVec();
268 if (pOutVectOptB) {
269 ioman->RegisterAny(config->GetOutputBranchNameOptB().data(), pOutVectOptB, kTRUE);
270 LOG(info) << "CbmRecoUnpack::RegisterOutputs() " << config->GetOutputBranchNameOptB() << " at " << pOutVectOptB;
271 }
272*/
274 config->RegisterOutput(ioman);
275 }
276
282 virtual Bool_t initParContainers(std::vector<std::pair<std::string, std::shared_ptr<FairParGenericSet>>>* reqparvec)
283 {
284 LOG(debug) << GetName() << "::Init - initParContainers";
285 if (!reqparvec) {
286 LOG(info) << GetName() << "::Init - initParContainers - empty requirements vector no parameters initialized.";
287 return kTRUE;
288 }
289
290 // Now get the actual ascii files and init the containers with the asciiIo
291 for (auto& pair : *reqparvec) {
292 auto filepath = pair.first;
293 auto parset = pair.second;
294 FairParAsciiFileIo asciiInput;
295 if (!filepath.empty()) {
296 if (asciiInput.open(filepath.data())) {
297 parset->init(&asciiInput);
298 }
299 }
300 }
301 return kTRUE;
302 }
303
305 template<typename TVecobj>
306 typename std::enable_if<std::is_same<TVecobj, std::nullptr_t>::value == true, void>::type
307 timesort(std::vector<TVecobj>* /*vec = nullptr*/)
308 {
309 LOG(debug) << "CbmRecoUnpack::timesort() got an object that has no member function GetTime(). Hence, we can and "
310 "will not timesort it!";
311 }
312
313 template<typename TVecobj>
314 typename std::enable_if<!std::is_member_function_pointer<decltype(&TVecobj::GetTime)>::value, void>::type
315 timesort(std::vector<TVecobj>* /*vec = nullptr*/)
316 {
317 LOG(debug) << "CbmRecoUnpack::timesort() " << TVecobj::Class_Name()
318 << "is an object that has no member function GetTime(). Hence, we can and "
319 "will not timesort it!";
320 }
321
322 template<typename TVecobj>
323 typename std::enable_if<std::is_member_function_pointer<decltype(&TVecobj::GetTime)>::value, void>::type
324 timesort(std::vector<TVecobj>* vec = nullptr)
325 {
326 if (vec == nullptr) return;
327 std::sort(vec->begin(), vec->end(),
328 [](const TVecobj& a, const TVecobj& b) -> bool { return a.GetTime() < b.GetTime(); });
329 }
330
331
345 template<class TConfig, class TOptOutA = std::nullptr_t, class TOptOutB = std::nullptr_t>
346 size_t unpack(const Subsystem subsystem, const fles::Timeslice* ts, std::uint16_t icomp, TConfig config,
347 std::vector<TOptOutA>* optouttargetvecA = nullptr, std::vector<TOptOutB>* optouttargetvecB = nullptr)
348 {
349
350 auto wallstarttime = std::chrono::high_resolution_clock::now();
351 std::clock_t cpustarttime = std::clock();
352
353 auto algo = config->GetUnpacker();
354 std::vector<TOptOutA> optoutAvec = {};
355 std::vector<TOptOutB> optoutBvec = {};
356 if (optouttargetvecA) {
357 algo->SetOptOutAVec(&optoutAvec);
358 }
359 if (optouttargetvecB) {
360 algo->SetOptOutBVec(&optoutBvec);
361 }
362
363 // Set the start time of the current TS for this algorithm
364 algo->SetTsStartTime(ts->start_time());
365
366 // Run the actual unpacking
367 auto digivec = algo->Unpack(ts, icomp);
368
369 // Check if we want to write the output to somewhere (in pure online monitoring mode for example this can/would/should be skipped)
370 if (!bMonitoringOnly) {
371 if (config->GetOutputVec()) {
372 // Lets do some time-sorting if we are not doing it later
373 if (!bOutputFullTimeSorting) timesort(&digivec);
374
375 // Transfer the data from the timeslice vector to the target branch vector
376 // Digis/default output retrieved as offered by the algorithm
377 for (auto digi : digivec)
378 config->GetOutputVec()->emplace_back(digi);
379 }
380 if (optouttargetvecA) {
381 // Lets do some timesorting
382 if (!bOutputFullTimeSorting) timesort(&optoutAvec);
383 // Transfer the data from the timeslice vector to the target branch vector
384 for (auto optoutA : optoutAvec)
385 optouttargetvecA->emplace_back(optoutA);
386 }
387 if (optouttargetvecB) {
388 // Second opt output is not time sorted to allow non GetTime data container.
389 // Lets do some timesorting
390 timesort(&optoutAvec);
391 // Transfer the data from the timeslice vector to the target branch vector
392 for (auto optoutB : optoutBvec)
393 optouttargetvecB->emplace_back(optoutB);
394 }
395 }
396
397 std::clock_t cpuendtime = std::clock();
398 auto wallendtime = std::chrono::high_resolution_clock::now();
399
400 // Cpu time in [µs]
401 auto cputime = 1e6 * (cpuendtime - cpustarttime) / CLOCKS_PER_SEC;
402 algo->AddCpuTime(cputime);
403 // Real time in [µs]
404 auto walltime = std::chrono::duration<double, std::micro>(wallendtime - wallstarttime).count();
405 algo->AddWallTime(walltime);
406
407
408 // Check some numbers from this timeslice
409 size_t nDigis = digivec.size();
410 LOG(debug) << "Component " << icomp << " connected to config " << config->GetName() << " n-Digis " << nDigis
411 << " processed in walltime(cputime) = " << walltime << "(" << cputime << cputime << ") µs"
412 << "this timeslice.";
413
414 if (fDoPerfProf) {
415 auto timeit = fTimeMap.find(subsystem);
416 timeit->second.first += cputime;
417 timeit->second.second += walltime;
418
419 auto datait = fDataSizeMap.find(subsystem);
420 datait->second.first += ts->size_component(icomp) / 1.0e6;
421 datait->second.second += nDigis * algo->GetOutputObjSize() / 1.0e6;
422
423 fNameMap.find(subsystem)->second.second += nDigis;
424 }
425
426 if (fDoPerfProfPerTs) {
427 auto timeit = fTimeMapPerTs.find(subsystem);
428 timeit->second.first += cputime;
429 timeit->second.second += walltime;
430
431 auto datait = fDataSizeMapPerTs.find(subsystem);
432 datait->second.first += ts->size_component(icomp) / 1.0e6;
433 datait->second.second += nDigis * algo->GetOutputObjSize() / 1.0e6;
434
435 fNameMapPerTs.find(subsystem)->second.second += nDigis;
436 }
437
438 return nDigis;
439 }
440 // ----------------------------------------------------------------------------
441
444
446 std::shared_ptr<CbmMuchUnpackConfig> fMuchConfig = nullptr;
447
449 std::shared_ptr<CbmPsdUnpackConfig> fPsdConfig = nullptr;
450
452 std::shared_ptr<CbmRichUnpackConfig> fRichConfig = nullptr;
453
455 std::shared_ptr<CbmStsUnpackConfig> fStsConfig = nullptr;
456
458 std::shared_ptr<CbmTofUnpackConfig> fTofConfig = nullptr;
459
461 std::shared_ptr<CbmTrdUnpackConfig> fTrd1DConfig = nullptr;
462
464 std::shared_ptr<CbmTrdUnpackFaspConfig> fTrd2DConfig = nullptr;
465
467 std::shared_ptr<CbmBmonUnpackConfig> fBmonConfig = nullptr;
468
471
473 std::string fOutfilename = "CbmRecoUnpack.perf.root";
474
477
479 bool bMonitoringOnly = false;
480
481
483};
484
485
486#endif
Configuration class for an unpacker algorithm.
Configuration class for an unpacker algorithm.
Configuration class for an unpacker algorithm.
Configuration class for an unpacker algorithm.
Configuration class for an unpacker algorithm.
Configuration class for an unpacker algorithm.
Configuration class for an unpacker algorithm.
Main steering class for unpacking in cbmroot.
void SetOutputFilename(std::string value)
Set the performance profiling Output Filename.
virtual Bool_t initParContainers(std::vector< std::pair< std::string, std::shared_ptr< FairParGenericSet > > > *reqparvec)
Initialise the parameter containers requested by the algorithm.
void Finish()
Actions at the end of the run.
void SetUnpackConfig(std::shared_ptr< CbmTofUnpackConfig > config)
Set the Tof Unpack Config.
void SetUnpackConfig(std::shared_ptr< CbmPsdUnpackConfig > config)
Set the Psd Unpack Config.
std::unique_ptr< cbm::Monitor > fMonitor
ratio of total unpacked size to input size vs TS in run
CbmTimeSlice * fTimeSlice
CbmTimeslice object, mostly redundant with the TsEventHeader, needed by L1 to switch timeslice mode.
std::string fUriPublishProfMoni
URI (type:hostname:port:db_name) for optional connection to monitoring DB.
CbmRecoUnpack()
Constructor.
std::map< Subsystem, TH1 * > fvhUnpRatioPerTs
ratio of system digi size in total output size vs TS in run
bool fPublishProfMoni
Flag if performance profiling data should be published to monitoring DB.
std::string fMoniJobId
std::string fMoniCurrrentHostname
The application's monitoring object.
void SetTimeSorting(bool bIn=true)
Enable/disable a full time sorting. If off, time sorting happens per link/FLIM source.
std::map< Subsystem, std::pair< double, double > > fDataSizeMap
Map to store the in and out data amount, key = Subsystem.
TH1 * fhRealTimePerTsHist
Plotting time per TS.
std::enable_if<!std::is_member_function_pointer< decltype(&TVecobj::GetTime)>::value, void >::type timesort(std::vector< TVecobj > *)
void SetMonitoringOnly(bool bIn=true)
Enable/disable the data output. If On, data unpacked and monitored but neither sorted nor written to ...
TH1 * fhCpuTimePerTsHist
Processing time per TS.
std::map< Subsystem, std::pair< double, double > > fDataSizeMapCurrSec
void SetUnpackConfig(std::shared_ptr< CbmTrdUnpackFaspConfig > config)
Set the Trd2D Unpack Config.
void Reset()
Clear the output vectors as preparation of the next timeslice. Called via FairSource::Reset()
std::string fOutfilename
Name of the performance profiling output file.
std::shared_ptr< CbmStsUnpackConfig > fStsConfig
Configuration of the Sts unpacker. Provides the configured algorithm.
bool bOutputFullTimeSorting
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.
std::map< Subsystem, std::pair< std::string, size_t > > fNameMapPerTs
Map to store a name for the unpackers and the processed amount of digis for a single TS,...
std::shared_ptr< CbmBmonUnpackConfig > fBmonConfig
Configuration of the Bmon unpacker. Provides the configured algorithm.
CbmTsEventHeader * fCbmTsEventHeader
Pointer to the Timeslice start time used to write it to the output tree.
std::map< Subsystem, TH1 * > fvhOutRatioPerTs
ratio of system data in total input size vs TS in run
std::shared_ptr< CbmTrdUnpackFaspConfig > fTrd2DConfig
Configuration of the Trd unpacker. Provides the configured algorithm.
bool fPubMoniProcTime
Flag if perf data should be published to moni DB using data time (false) or processing time (true).
std::map< Subsystem, std::pair< double, double > > fTimeMap
Map to store the cpu and wall time, key = Subsystem.
fles::Subsystem Subsystem
bool bMonitoringOnly
Flag to Enable/disable the output completely.
void SetDebugPrintout(bool value=true)
Set the Debug Printout Flag.
ClassDef(CbmRecoUnpack, 3)
std::map< Subsystem, TH1 * > fvhInpRatioPerTs
Plotting time per TS.
void initPerformanceMaps(Subsystem subsystem, std::string name)
Init the performance profiling maps for a given unpacker.
std::shared_ptr< CbmTofUnpackConfig > fTofConfig
Configuration of the Tof unpacker. Provides the configured algorithm.
~CbmRecoUnpack()
Destructor.
std::map< Subsystem, std::pair< double, double > > fDataSizeMapPerTs
Map to store the in and out data amount for a single TS, key = Subsystem.
void performanceProfiling()
Run the performance profiling based on the fTimeMap and fDataSizeMap members.
bool fDoDebugPrints
Flag if extended debug output is to be printed or not.
std::map< Subsystem, std::pair< double, double > > fTimeMapPerTs
Map to store the cpu and wall time for a single TS, key = Subsystem.
void SetUnpackConfig(std::shared_ptr< CbmBmonUnpackConfig > config)
Set the Bmon Unpack Config.
TH1 * fhRealTimePerTs
Processing time per TS.
void Unpack(std::unique_ptr< fles::Timeslice > ts)
Trigger the unpacking procedure.
TH1 * fhUnpackingRatioPerTs
ratio of selected digi vs TS in run
CbmRecoUnpack & operator=(const CbmRecoUnpack &)=delete
Assignment operator - not implemented.
std::shared_ptr< CbmMuchUnpackConfig > fMuchConfig
Current time slice.
void SetUnpackConfig(std::shared_ptr< CbmStsUnpackConfig > config)
Set the Sts Unpack Config.
std::shared_ptr< CbmTrdUnpackConfig > fTrd1DConfig
Configuration of the Trd unpacker. Provides the configured algorithm.
CbmRecoUnpack(const CbmRecoUnpack &)=delete
Copy constructor - not implemented.
bool fDoPerfProfPerTs
Flag if performance profiling per TS should be activated or not.
void SetDoPerfProfiling(bool value=true)
(De)Activate the performance profiling based on histograms
void SetUnpackConfig(std::shared_ptr< CbmMuchUnpackConfig > config)
Set the Much Unpack Config.
void performanceProfilingPerTs()
Run the performance profiling for a single TS based on the fTimeMapPerTs and fDataSizeMapPerTs member...
std::enable_if< std::is_member_function_pointer< decltype(&TVecobj::GetTime)>::value, void >::type timesort(std::vector< TVecobj > *vec=nullptr)
TStopwatch * fTimerTs
std::chrono::system_clock::time_point fMonitorSecCurrentTs
std::shared_ptr< CbmRichUnpackConfig > fRichConfig
Configuration of the Rich unpacker. Provides the configured algorithm.
void SetDoPerfProfilingPerTs(bool value=true)
(De)Activate the performance profiling based on histograms for each TS
void SetPublishProfMoni(bool value=true, std::string sUriMonitor="", bool useProcTime=false, std::string sJobId="local")
(De)Activate the publication of performance profiling data to monitoring DB
void RegisterOutputs(FairRootManager *ioman, std::shared_ptr< TConfig > config)
Bool_t Init()
Initialisation.
std::map< Subsystem, std::pair< std::string, size_t > > fNameMap
Map to store a name for the unpackers and the processed amount of digis, key = Subsystem.
std::shared_ptr< CbmPsdUnpackConfig > fPsdConfig
Configuration of the Psd unpacker. Provides the configured algorithm.
size_t unpack(const Subsystem subsystem, 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.
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< CbmTrdUnpackConfig > config)
Set the Trd Unpack Config.
bool fDoPerfProf
Flag if performance profiling should be activated or not.
Bookkeeping of time-slice content.